Implement a nicer version of str() for Python wrapper objects.
[calfbox.git] / master.c
blob594eb031638f53337a85a69c4838911c540882fa
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 "engine.h"
20 #include "errors.h"
21 #include "master.h"
22 #include "seq.h"
23 #include "rt.h"
24 #include "song.h"
25 #include <string.h>
27 static gboolean master_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
29 struct cbox_master *m = ct->user_data;
30 if (!strcmp(cmd->command, "/status") && !*cmd->arg_types)
32 if (!cbox_check_fb_channel(fb, cmd->command, error))
33 return FALSE;
34 if (!cbox_execute_on(fb, NULL, "/sample_rate", "i", error, m->srate))
35 return FALSE;
36 if (!m->spb)
37 return TRUE;
38 return cbox_execute_on(fb, NULL, "/tempo", "f", error, m->tempo) &&
39 cbox_execute_on(fb, NULL, "/timesig", "ii", error, m->timesig_nom, m->timesig_denom) &&
40 cbox_execute_on(fb, NULL, "/playing", "i", error, (int)m->state) &&
41 cbox_execute_on(fb, NULL, "/pos", "i", error, m->spb->song_pos_samples) &&
42 cbox_execute_on(fb, NULL, "/pos_ppqn", "i", error, m->spb->song_pos_ppqn);
44 else
45 if (!strcmp(cmd->command, "/tell") && !*cmd->arg_types)
47 if (!cbox_check_fb_channel(fb, cmd->command, error))
48 return FALSE;
49 if (!m->spb)
50 return TRUE;
51 return cbox_execute_on(fb, NULL, "/playing", "i", error, (int)m->state) &&
52 cbox_execute_on(fb, NULL, "/pos", "i", error, m->spb->song_pos_samples) &&
53 cbox_execute_on(fb, NULL, "/pos_ppqn", "i", error, m->spb->song_pos_ppqn);
55 else
56 if (!strcmp(cmd->command, "/set_tempo") && !strcmp(cmd->arg_types, "f"))
58 cbox_master_set_tempo(m, CBOX_ARG_F(cmd, 0));
59 return TRUE;
61 else
62 if (!strcmp(cmd->command, "/set_timesig") && !strcmp(cmd->arg_types, "ii"))
64 cbox_master_set_timesig(m, CBOX_ARG_I(cmd, 0), CBOX_ARG_I(cmd, 1));
65 return TRUE;
67 else
68 if (!strcmp(cmd->command, "/play") && !strcmp(cmd->arg_types, ""))
70 cbox_master_play(m);
71 return TRUE;
73 else
74 if (!strcmp(cmd->command, "/stop") && !strcmp(cmd->arg_types, ""))
76 cbox_master_stop(m);
77 return TRUE;
79 else
80 if (!strcmp(cmd->command, "/panic") && !strcmp(cmd->arg_types, ""))
82 cbox_master_panic(m);
83 return TRUE;
85 else
86 if (!strcmp(cmd->command, "/seek_samples") && !strcmp(cmd->arg_types, "i"))
88 if (m->spb)
89 cbox_master_seek_samples(m, CBOX_ARG_I(cmd, 0));
90 return TRUE;
92 else
93 if (!strcmp(cmd->command, "/seek_ppqn") && !strcmp(cmd->arg_types, "i"))
95 if (m->spb)
96 cbox_master_seek_ppqn(m, CBOX_ARG_I(cmd, 0));
97 return TRUE;
99 else
100 if ((!strcmp(cmd->command, "/samples_to_ppqn") || !strcmp(cmd->command, "/ppqn_to_samples")) &&
101 !strcmp(cmd->arg_types, "i"))
103 if (!cbox_check_fb_channel(fb, cmd->command, error))
104 return FALSE;
105 if (m->spb)
107 if (cmd->command[1] == 's')
108 return cbox_execute_on(fb, NULL, "/value", "i", error, cbox_master_samples_to_ppqn(m, CBOX_ARG_I(cmd, 0)));
109 else
110 return cbox_execute_on(fb, NULL, "/value", "i", error, cbox_master_ppqn_to_samples(m, CBOX_ARG_I(cmd, 0)));
112 else
114 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Song playback not initialised.");
115 return FALSE;
118 else
120 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Unknown combination of target path and argument: '%s', '%s'", cmd->command, cmd->arg_types);
121 return FALSE;
125 static void cbox_master_init(struct cbox_master *master, struct cbox_engine *engine)
127 master->srate = 0;
128 master->tempo = 120.0;
129 master->new_tempo = 120.0;
130 master->timesig_nom = 4;
131 master->timesig_denom = 4;
132 master->state = CMTS_STOP;
133 master->engine = engine;
134 master->song = NULL;
135 master->spb = NULL;
136 cbox_command_target_init(&master->cmd_target, master_process_cmd, master);
139 struct cbox_master *cbox_master_new(struct cbox_engine *engine)
141 struct cbox_master *master = malloc(sizeof(struct cbox_master));
142 cbox_master_init(master, engine);
143 return master;
147 void cbox_master_set_sample_rate(struct cbox_master *master, int srate)
149 master->srate = srate;
152 void cbox_master_set_tempo(struct cbox_master *master, float tempo)
154 // XXXKF not realtime-safe; won't crash, but may lose tempo
155 // changes when used multiple times in rapid succession
156 master->new_tempo = tempo;
159 void cbox_master_set_timesig(struct cbox_master *master, int beats, int unit)
161 master->timesig_nom = beats;
162 master->timesig_denom = unit;
166 void cbox_master_to_bbt(const struct cbox_master *master, struct cbox_bbt *bbt, int time_samples)
168 double second = ((double)time_samples) / master->srate;
169 double beat = master->tempo * second / 60;
170 int beat_int = (int)beat;
171 bbt->bar = beat_int / master->timesig_nom;
172 bbt->beat = beat_int % master->timesig_nom;
173 bbt->tick = (beat - beat_int) * PPQN; // XXXKF what if timesig_denom is not 4?
177 uint32_t cbox_master_song_pos_from_bbt(struct cbox_master *master, const struct cbox_bbt *bbt)
179 double beat = bbt->bar * master->timesig_nom + bbt->beat + bbt->tick * 1.0 / PPQN;
180 return (uint32_t)(master->srate * 60 / master->tempo);
184 #define cbox_master_play_args(ARG)
186 DEFINE_RT_VOID_FUNC(cbox_master, master, cbox_master_play)
188 // wait for the notes to be released
189 if (master->state == CMTS_STOPPING)
191 RT_CALL_AGAIN_LATER();
192 return;
195 master->state = CMTS_ROLLING;
198 #define cbox_master_stop_args(ARG)
200 DEFINE_RT_VOID_FUNC(cbox_master, master, cbox_master_stop)
202 if (master->state == CMTS_ROLLING)
203 master->state = CMTS_STOPPING;
205 if (master->state != CMTS_STOP)
206 RT_CALL_AGAIN_LATER();
209 struct seek_command_arg
211 struct cbox_master *master;
212 gboolean is_ppqn;
213 uint32_t target_pos;
214 gboolean was_rolling;
215 gboolean status_known;
218 static int seek_transport_execute(void *arg_)
220 struct seek_command_arg *arg = arg_;
221 // On first pass, check if transport is rolling from the DSP thread
222 if (!arg->status_known)
224 arg->status_known = TRUE;
225 arg->was_rolling = arg->master->state == CMTS_ROLLING;
227 // If transport was rolling, stop, release notes, seek, then restart
228 if (arg->master->state == CMTS_ROLLING)
229 arg->master->state = CMTS_STOPPING;
231 // wait until transport stopped
232 if (arg->master->state != CMTS_STOP)
233 return 0;
235 if (arg->is_ppqn)
236 cbox_song_playback_seek_ppqn(arg->master->spb, arg->target_pos, FALSE);
237 else
238 cbox_song_playback_seek_samples(arg->master->spb, arg->target_pos);
239 if (arg->was_rolling)
240 arg->master->state = CMTS_ROLLING;
241 return 1;
244 void cbox_master_seek_ppqn(struct cbox_master *master, uint32_t pos_ppqn)
246 if (master->spb)
248 static struct cbox_rt_cmd_definition cmd = { NULL, seek_transport_execute, NULL };
249 struct seek_command_arg arg = { master, TRUE, pos_ppqn, FALSE, FALSE };
250 cbox_rt_execute_cmd_sync(master->engine->rt, &cmd, &arg);
254 void cbox_master_seek_samples(struct cbox_master *master, uint32_t pos_samples)
256 if (master->spb)
258 static struct cbox_rt_cmd_definition cmd = { NULL, seek_transport_execute, NULL };
259 struct seek_command_arg arg = { master, FALSE, pos_samples, FALSE, FALSE };
260 cbox_rt_execute_cmd_sync(master->engine->rt, &cmd, &arg);
264 void cbox_master_panic(struct cbox_master *master)
266 cbox_master_stop(master);
267 struct cbox_midi_buffer buf;
268 cbox_midi_buffer_init(&buf);
269 for (int ch = 0; ch < 16; ch++)
271 cbox_midi_buffer_write_inline(&buf, ch, 0xB0 + ch, 120, 0);
272 cbox_midi_buffer_write_inline(&buf, ch, 0xB0 + ch, 123, 0);
273 cbox_midi_buffer_write_inline(&buf, ch, 0xB0 + ch, 121, 0);
275 // Send to all outputs
276 cbox_engine_send_events_to(master->engine, NULL, &buf);
279 int cbox_master_ppqn_to_samples(struct cbox_master *master, int time_ppqn)
281 double tempo = master->tempo;
282 int offset = 0;
283 if (master->spb)
285 int idx = cbox_song_playback_tmi_from_ppqn(master->spb, time_ppqn);
286 if (idx != -1)
288 const struct cbox_tempo_map_item *tmi = &master->spb->tempo_map_items[idx];
289 tempo = tmi->tempo;
290 time_ppqn -= tmi->time_ppqn;
291 offset = tmi->time_samples;
294 return offset + (int)(master->srate * 60.0 * time_ppqn / (tempo * PPQN));
297 int cbox_master_samples_to_ppqn(struct cbox_master *master, int time_samples)
299 double tempo = master->tempo;
300 int offset = 0;
301 if (master->spb)
303 int idx = cbox_song_playback_tmi_from_samples(master->spb, time_samples);
304 if (idx != -1)
306 const struct cbox_tempo_map_item *tmi = &master->spb->tempo_map_items[idx];
307 tempo = tmi->tempo;
308 time_samples -= tmi->time_samples;
309 offset = tmi->time_ppqn;
312 return offset + (int)(tempo * PPQN * time_samples / (master->srate * 60.0));
315 void cbox_master_destroy(struct cbox_master *master)
317 if (master->spb)
319 cbox_song_playback_destroy(master->spb);
320 master->spb = NULL;
322 free(master);