2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2011 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "config-api.h"
23 int cbox_midi_buffer_write_inline(struct cbox_midi_buffer
*buffer
, uint32_t time
, ...)
28 buf
[0] = va_arg(va
, int);
29 int size
= midi_cmd_size(buf
[0]);
30 for (int i
= 1; i
< size
; i
++)
31 buf
[i
] = va_arg(va
, int);
32 return cbox_midi_buffer_write_event(buffer
, time
, buf
, size
);
35 int cbox_midi_buffer_write_event(struct cbox_midi_buffer
*buffer
, uint32_t time
, uint8_t *data
, uint32_t size
)
37 struct cbox_midi_event
*evt
;
39 if (buffer
->count
>= CBOX_MIDI_MAX_EVENTS
)
41 if (size
> 4 && size
> CBOX_MIDI_MAX_LONG_DATA
- buffer
->long_data_size
)
43 evt
= &buffer
->events
[buffer
->count
++];
48 memcpy(evt
->data_inline
, data
, size
);
52 evt
->data_ext
= buffer
->long_data
+ buffer
->long_data_size
;
53 memcpy(evt
->data_ext
, data
, size
);
54 buffer
->long_data_size
+= size
;
59 int cbox_midi_buffer_copy_event(struct cbox_midi_buffer
*buffer
, const struct cbox_midi_event
*event
, int new_time
)
61 struct cbox_midi_event
*evt
;
63 if (buffer
->count
>= CBOX_MIDI_MAX_EVENTS
)
65 if (event
->size
> 4 && event
->size
> CBOX_MIDI_MAX_LONG_DATA
- buffer
->long_data_size
)
67 evt
= &buffer
->events
[buffer
->count
++];
69 evt
->size
= event
->size
;
72 memcpy(evt
->data_inline
, event
->data_inline
, event
->size
);
76 evt
->data_ext
= buffer
->long_data
+ buffer
->long_data_size
;
77 memcpy(evt
->data_ext
, event
->data_ext
, event
->size
);
78 buffer
->long_data_size
+= event
->size
;
83 int note_from_string(const char *note
)
85 static const int semis
[] = {9, 11, 0, 2, 4, 5, 7};
87 int nn
= tolower(note
[0]);
89 if (nn
>= '0' && nn
<= '9')
91 if (nn
< 'a' && nn
> 'g')
95 for (pos
= 1; note
[pos
] == 'b' || note
[pos
] == '#'; pos
++)
96 nv
+= (note
[pos
] == 'b') ? -1 : +1;
98 if ((note
[pos
] == '-' && note
[pos
+ 1] >= '1' && note
[pos
+ 1] <= '2' && note
[pos
+ 2] == '\0') || (note
[pos
] >= '0' && note
[pos
] <= '9' && note
[pos
+ 1] == '\0'))
100 return nv
+ 12 * (2 + atoi(note
+ pos
));
106 int cbox_config_get_note(const char *cfg_section
, const char *key
, int def_value
)
108 const char *cv
= cbox_config_get_string(cfg_section
, key
);
110 return note_from_string(cv
);