Make sure program changes end up before notes on the same tick.
[calfbox.git] / pattern.c
blobf7e4590734ffcef5b566a23def5dba9bac4fd1d1
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 "blob.h"
20 #include "config-api.h"
21 #include "pattern.h"
22 #include "pattern-maker.h"
23 #include "song.h"
25 #include <glib.h>
27 extern void cbox_song_remove_pattern(struct cbox_song *song, struct cbox_midi_pattern *pattern);
29 CBOX_CLASS_DEFINITION_ROOT(cbox_midi_pattern)
31 struct cbox_midi_pattern *cbox_midi_pattern_new_metronome(struct cbox_song *song, int ts)
33 struct cbox_midi_pattern_maker *m = cbox_midi_pattern_maker_new();
35 int length = PPQN;
36 int channel = cbox_config_get_int("metronome", "channel", 10);
37 int accnote = cbox_config_get_note("metronome", "note_accent", 37);
38 int note = cbox_config_get_note("metronome", "note", 37);
40 for (int i = 0; i < ts; i++)
42 int accent = !i && ts != 1;
43 cbox_midi_pattern_maker_add(m, length * i, 0x90 + channel - 1, accent ? accnote : note, accent ? 127 : 100);
44 cbox_midi_pattern_maker_add(m, length * i + 1, 0x80 + channel - 1, accent ? accnote : note, 0);
47 struct cbox_midi_pattern *p = cbox_midi_pattern_maker_create_pattern(m, song, g_strdup_printf("click-%d", ts));
48 p->loop_end = length * ts;
50 cbox_midi_pattern_maker_destroy(m);
52 return p;
55 void cbox_midi_pattern_destroyfunc(struct cbox_objhdr *objhdr)
57 struct cbox_midi_pattern *pattern = CBOX_H2O(objhdr);
58 if (pattern->owner)
59 cbox_song_remove_pattern(pattern->owner, pattern);
60 g_free(pattern->name);
61 if (pattern->events != NULL)
62 free(pattern->events);
63 free(pattern);
66 static int cbox_midi_pattern_load_smf_into(struct cbox_midi_pattern_maker *m, const char *smf)
68 int length = 0;
69 if (!cbox_midi_pattern_maker_load_smf(m, smf, &length, NULL))
71 g_error("Cannot load SMF file %s", smf);
72 return -1;
74 return length;
77 static int cbox_midi_pattern_load_melodic_into(struct cbox_midi_pattern_maker *m, const char *name, int start_pos, int transpose, int transpose_to_note)
79 gchar *cfg_section = g_strdup_printf("pattern:%s", name);
81 if (!cbox_config_has_section(cfg_section))
83 g_error("Melodic pattern '%s' not found", name);
84 g_free(cfg_section);
85 return -1;
88 gchar *smf = cbox_config_get_string(cfg_section, "smf");
89 if (smf)
90 return cbox_midi_pattern_load_smf_into(m, smf);
92 int length = PPQN * cbox_config_get_int(cfg_section, "beats", 4);
93 int gchannel = cbox_config_get_int(cfg_section, "channel", 1);
94 int gswing = cbox_config_get_int(cfg_section, "swing", 0);
95 int gres = cbox_config_get_int(cfg_section, "resolution", 4);
96 int orignote = cbox_config_get_note(cfg_section, "base_note", 24);
97 if (transpose_to_note != -1)
98 transpose += transpose_to_note - orignote;
100 for (int t = 1; ; t++)
102 gchar *tname = g_strdup_printf("track%d", t);
103 char *trkname = cbox_config_get_string(cfg_section, tname);
104 g_free(tname);
105 if (trkname)
107 tname = g_strdup_printf("%s_vel", trkname);
108 int vel = cbox_config_get_note(cfg_section, tname, 100);
109 g_free(tname);
110 tname = g_strdup_printf("%s_res", trkname);
111 int res = cbox_config_get_note(cfg_section, tname, gres);
112 g_free(tname);
113 tname = g_strdup_printf("%s_channel", trkname);
114 int channel = cbox_config_get_note(cfg_section, tname, gchannel);
115 g_free(tname);
116 tname = g_strdup_printf("%s_swing", trkname);
117 int swing = cbox_config_get_int(cfg_section, tname, gswing);
118 g_free(tname);
119 tname = g_strdup_printf("%s_notes", trkname);
120 const char *notes = cbox_config_get_string(cfg_section, tname);
121 g_free(tname);
122 if (!notes)
124 g_error("Invalid track %s", trkname);
126 const char *s = notes;
127 int t = 0;
128 while(1)
130 if (!*s)
131 break;
133 gchar *note;
134 const char *comma = strchr(s, ',');
135 if (comma)
137 note = g_strndup(s, comma - s);
138 s = comma + 1;
140 else
142 note = g_strdup(s);
143 s += strlen(s);
146 if (*note)
148 int pitch = note_from_string(note);
150 int pos = t * PPQN / res + start_pos;
151 if (t & 1)
152 pos += PPQN * swing / (res * 24);
154 int pos2 = (t + 1) * PPQN / res + start_pos;
155 if (t & 1)
156 pos2 += PPQN * swing / (res * 24);
158 pitch += transpose;
160 cbox_midi_pattern_maker_add(m, pos, 0x90 + channel - 1, pitch, vel);
161 cbox_midi_pattern_maker_add(m, pos2 - 1, 0x80 + channel - 1, pitch, 0);
163 t++;
166 else
167 break;
170 g_free(cfg_section);
172 return length;
175 static int cbox_midi_pattern_load_drum_into(struct cbox_midi_pattern_maker *m, const char *name, int start_pos)
177 gchar *cfg_section = g_strdup_printf("drumpattern:%s", name);
179 if (!cbox_config_has_section(cfg_section))
181 g_error("Drum pattern '%s' not found", name);
182 g_free(cfg_section);
183 return -1;
186 gchar *smf = cbox_config_get_string(cfg_section, "smf");
187 if (smf)
188 return cbox_midi_pattern_load_smf_into(m, smf);
190 int length = PPQN * cbox_config_get_int(cfg_section, "beats", 4);
191 int channel = cbox_config_get_int(cfg_section, "channel", 10);
192 int gswing = cbox_config_get_int(cfg_section, "swing", 0);
193 int gres = cbox_config_get_int(cfg_section, "resolution", 4);
195 for (int t = 1; ; t++)
197 gchar *tname = g_strdup_printf("track%d", t);
198 char *trkname = cbox_config_get_string(cfg_section, tname);
199 g_free(tname);
200 if (trkname)
202 tname = g_strdup_printf("%s_note", trkname);
203 int note = cbox_config_get_note(cfg_section, tname, -1);
204 g_free(tname);
205 tname = g_strdup_printf("%s_res", trkname);
206 int res = cbox_config_get_note(cfg_section, tname, gres);
207 g_free(tname);
208 tname = g_strdup_printf("%s_swing", trkname);
209 int swing = cbox_config_get_int(cfg_section, tname, gswing);
210 g_free(tname);
211 tname = g_strdup_printf("%s_trigger", trkname);
212 const char *trigger = cbox_config_get_string(cfg_section, tname);
213 g_free(tname);
214 if (!trigger || note == -1)
216 g_error("Invalid track %s", trkname);
218 int t = 0;
219 for (int i = 0; trigger[i]; i++)
221 int pos = t * PPQN / res + start_pos;
222 if (t & 1)
223 pos += PPQN * swing / (res * 24);
224 if (trigger[i] >= '1' && trigger[i] <= '9')
226 int amt = (trigger[i] - '0') * 127 / 9;
227 cbox_midi_pattern_maker_add(m, pos, 0x90 + channel - 1, note, amt);
228 cbox_midi_pattern_maker_add(m, pos + 1, 0x80 + channel - 1, note, 0);
229 t++;
231 if (trigger[i] == 'F') // flam
233 int dflam = PPQN / 4;
234 int rnd = rand() & 7;
235 dflam += rnd / 2;
236 cbox_midi_pattern_maker_add(m, pos - dflam, 0x90 + channel - 1, note, 90+rnd);
237 cbox_midi_pattern_maker_add(m, pos - dflam + 1, 0x80 + channel - 1, note, 0);
238 cbox_midi_pattern_maker_add(m, pos , 0x90 + channel - 1, note, 120 + rnd);
239 cbox_midi_pattern_maker_add(m, pos + 1, 0x80 + channel - 1, note, 0);
240 t++;
242 if (trigger[i] == 'D') // drag
244 pos = (t + 1) * PPQN / res + start_pos;
245 //if (!(t & 1))
246 // pos += PPQN * swing / (res * 24);
247 float dflam = PPQN/8.0;
248 int rnd = rand() & 7;
249 cbox_midi_pattern_maker_add(m, pos - dflam*2, 0x90 + channel - 1, note, 70+rnd);
250 cbox_midi_pattern_maker_add(m, pos - dflam*2 + 1, 0x80 + channel - 1, note, 0);
251 cbox_midi_pattern_maker_add(m, pos - dflam, 0x90 + channel - 1, note, 60+rnd);
252 cbox_midi_pattern_maker_add(m, pos - dflam + 1, 0x80 + channel - 1, note, 0);
253 t++;
255 else if (trigger[i] == '.')
256 t++;
259 else
260 break;
263 g_free(cfg_section);
265 return length;
268 struct cbox_midi_pattern *cbox_midi_pattern_load(struct cbox_song *song, const char *name, int is_drum)
270 struct cbox_midi_pattern_maker *m = cbox_midi_pattern_maker_new();
272 int length = 0;
273 if (is_drum)
274 length = cbox_midi_pattern_load_drum_into(m, name, 0);
275 else
276 length = cbox_midi_pattern_load_melodic_into(m, name, 0, 0, -1);
277 struct cbox_midi_pattern *p = cbox_midi_pattern_maker_create_pattern(m, song, g_strdup(name));
278 p->loop_end = length;
280 cbox_midi_pattern_maker_destroy(m);
282 return p;
285 struct cbox_midi_pattern *cbox_midi_pattern_load_track(struct cbox_song *song, const char *name, int is_drum)
287 int length = 0;
288 struct cbox_midi_pattern_maker *m = cbox_midi_pattern_maker_new();
290 gchar *cfg_section = g_strdup_printf(is_drum ? "drumtrack:%s" : "track:%s", name);
292 if (!cbox_config_has_section(cfg_section))
294 g_error("Drum track '%s' not found", name);
295 g_free(cfg_section);
296 return NULL;
299 for (int p = 1; ; p++)
301 gchar *pname = g_strdup_printf("pos%d", p);
302 char *patname = cbox_config_get_string(cfg_section, pname);
303 g_free(pname);
304 if (patname)
306 int tplen = 0;
307 char *comma = strchr(patname, ',');
308 while(*patname)
310 char *v = comma ? g_strndup(patname, comma - patname) : g_strdup(patname);
311 patname = comma ? comma + 1 : patname + strlen(patname);
313 int xpval = 0, xpnote = -1;
314 if (!is_drum)
316 char *xp = strchr(v, '+');
317 if (xp)
319 *xp = '\0';
320 xpval = atoi(xp + 1);
322 else
324 xp = strchr(v, '=');
325 if (xp)
327 *xp = '\0';
328 xpnote = note_from_string(xp + 1);
332 int plen = 0;
333 int is_drum_pat = is_drum;
334 int nofs = 0;
335 if (*v == '@')
337 nofs = 1;
338 is_drum_pat = !is_drum_pat;
340 if (is_drum_pat)
341 plen = cbox_midi_pattern_load_drum_into(m, v + nofs, length);
342 else
343 plen = cbox_midi_pattern_load_melodic_into(m, v + nofs, length, xpval, xpnote);
344 g_free(v);
345 if (plen < 0)
347 cbox_midi_pattern_maker_destroy(m);
348 return NULL;
350 if (plen > tplen)
351 tplen = plen;
352 if (*patname)
353 comma = strchr(patname, ',');
355 length += tplen;
357 else
358 break;
361 g_free(cfg_section);
363 struct cbox_midi_pattern *p = cbox_midi_pattern_maker_create_pattern(m, song, g_strdup(name));
364 p->loop_end = length;
366 cbox_midi_pattern_maker_destroy(m);
368 return p;
371 struct cbox_midi_pattern *cbox_midi_pattern_new_from_blob(struct cbox_song *song, const struct cbox_blob *blob, int length)
373 struct cbox_midi_pattern_maker *m = cbox_midi_pattern_maker_new();
375 struct cbox_blob_serialized_event event;
376 for (size_t i = 0; i < blob->size; i += sizeof(event))
378 // not sure about alignment guarantees of Python buffers
379 memcpy(&event, ((uint8_t *)blob->data) + i, sizeof(event));
380 cbox_midi_pattern_maker_add(m, event.time, event.cmd, event.byte1, event.byte2);
383 struct cbox_midi_pattern *p = cbox_midi_pattern_maker_create_pattern(m, song, g_strdup("unnamed-blob"));
384 p->loop_end = length;
386 cbox_midi_pattern_maker_destroy(m);
388 return p;
391 struct cbox_blob *cbox_midi_pattern_to_blob(struct cbox_midi_pattern *pat, int *length)
393 if (length)
394 *length = pat->loop_end;
396 struct cbox_blob_serialized_event event;
397 int size = 0;
398 for (int i = 0; i < pat->event_count; i++)
400 // currently sysex events and the like are not supported
401 if (pat->events[i].size < 4)
402 size += sizeof(event);
405 struct cbox_blob *blob = cbox_blob_new(size);
407 size = 0;
408 uint8_t *data = blob->data;
409 for (int i = 0; i < pat->event_count; i++)
411 // currently sysex events and the like are not supported
412 const struct cbox_midi_event *src = &pat->events[i];
413 if (src->size < 4)
415 event.time = src->time;
416 event.len = src->size;
417 memcpy(&event.cmd, &src->data_inline[0], event.len);
418 memcpy(data + size, &event, sizeof(event));
419 size += sizeof(event);
422 return blob;
425 gboolean cbox_midi_pattern_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
427 struct cbox_midi_pattern *p = ct->user_data;
429 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
431 if (!cbox_check_fb_channel(fb, cmd->command, error))
432 return FALSE;
434 return cbox_execute_on(fb, NULL, "/event_count", "i", error, (int)p->event_count) &&
435 cbox_execute_on(fb, NULL, "/loop_end", "i", error, (int)p->loop_end) &&
436 cbox_execute_on(fb, NULL, "/name", "s", error, p->name) &&
437 CBOX_OBJECT_DEFAULT_STATUS(p, fb, error)
440 else if (!strcmp(cmd->command, "/name") && !strcmp(cmd->arg_types, "s"))
442 char *old_name = p->name;
443 p->name = g_strdup(CBOX_ARG_S(cmd, 0));
444 g_free(old_name);
445 return TRUE;
447 return cbox_object_default_process_cmd(ct, fb, cmd, error);