Merge pull request #1 from atsampson/master
[calfbox.git] / midi.c
blob6e5371e435453855935d9056de004f23c549f071
1 /*
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"
20 #include "midi.h"
21 #include <stdarg.h>
23 int cbox_midi_buffer_write_inline(struct cbox_midi_buffer *buffer, uint32_t time, ...)
25 uint8_t buf[4];
26 va_list va;
27 va_start(va, 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)
40 return 0;
41 if (size > 4 && size > CBOX_MIDI_MAX_LONG_DATA - buffer->long_data_size)
42 return 0;
43 evt = &buffer->events[buffer->count++];
44 evt->time = time;
45 evt->size = size;
46 if (size <= 4)
48 memcpy(evt->data_inline, data, size);
50 else
52 evt->data_ext = buffer->long_data + buffer->long_data_size;
53 memcpy(evt->data_ext, data, size);
54 buffer->long_data_size += size;
56 return 1;
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)
64 return 0;
65 if (event->size > 4 && event->size > CBOX_MIDI_MAX_LONG_DATA - buffer->long_data_size)
66 return 0;
67 evt = &buffer->events[buffer->count++];
68 evt->time = new_time;
69 evt->size = event->size;
70 if (event->size <= 4)
72 memcpy(evt->data_inline, event->data_inline, event->size);
74 else
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;
80 return 1;
83 int note_from_string(const char *note)
85 static const int semis[] = {9, 11, 0, 2, 4, 5, 7};
86 int pos;
87 int nn = tolower(note[0]);
88 int nv;
89 if (nn >= '0' && nn <= '9')
90 return atoi(note);
91 if (nn < 'a' && nn > 'g')
92 return -1;
93 nv = semis[nn - 'a'];
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));
103 return -1;
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);
109 if (cv)
110 return note_from_string(cv);
111 return def_value;