Allow Python access to engine's master effect.
[calfbox.git] / sampler_channel.c
blob759f6026d045198ea641fe6e1f235c95f73a10b9
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2013 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 "dspmath.h"
21 #include "errors.h"
22 #include "midi.h"
23 #include "module.h"
24 #include "rt.h"
25 #include "sampler.h"
26 #include "sampler_impl.h"
27 #include "sfzloader.h"
28 #include "stm.h"
29 #include <assert.h>
30 #include <errno.h>
31 #include <glib.h>
32 #include <math.h>
33 #include <memory.h>
34 #include <sndfile.h>
35 #include <stdio.h>
36 #include <stdlib.h>
38 void sampler_channel_init(struct sampler_channel *c, struct sampler_module *m)
40 c->module = m;
41 c->voices_running = NULL;
42 c->active_voices = 0;
43 c->pitchwheel = 0;
44 c->output_shift = 0;
45 memset(c->cc, 0, sizeof(c->cc));
47 // default to maximum and pan=centre if MIDI mixing disabled
48 if (m->disable_mixer_controls)
50 c->channel_volume_cc = 16383;
51 c->channel_pan_cc = 8192;
53 else
55 sampler_channel_process_cc(c, 7, 100);
56 sampler_channel_process_cc(c, 7 + 32, 0);
57 sampler_channel_process_cc(c, 10, 64);
58 sampler_channel_process_cc(c, 10 + 32, 0);
60 c->cc[11] = 127;
61 c->cc[71] = 64;
62 c->cc[74] = 64;
63 c->previous_note = -1;
64 c->program = NULL;
65 sampler_channel_set_program_RT(c, m->program_count ? m->programs[0] : NULL);
66 memset(c->switchmask, 0, sizeof(c->switchmask));
67 memset(c->sustainmask, 0, sizeof(c->sustainmask));
68 memset(c->sostenutomask, 0, sizeof(c->sostenutomask));
71 void sampler_channel_process_cc(struct sampler_channel *c, int cc, int val)
73 struct sampler_module *m = c->module;
74 // Handle CC triggering.
75 if (c->program && c->program->rll && c->program->rll->layers_oncc && m->voices_free)
77 struct sampler_rll *rll = c->program->rll;
78 if (!(rll->cc_trigger_bitmask[cc >> 5] & (1 << (cc & 31))))
79 return;
80 int old_value = c->cc[cc];
81 for (GSList *p = rll->layers_oncc; p; p = p->next)
83 struct sampler_layer *layer = p->data;
84 assert(layer->runtime);
85 // Only trigger on transition between 'out of range' and 'in range' values.
86 // XXXKF I'm not sure if it's what is expected here, but don't have
87 // the reference implementation handy.
88 if (layer->runtime->on_cc_number == cc &&
89 (val >= layer->runtime->on_locc && val <= layer->runtime->on_hicc) &&
90 !(old_value >= layer->runtime->on_locc && old_value <= layer->runtime->on_hicc))
92 struct sampler_voice *v = m->voices_free;
93 int exgroups[MAX_RELEASED_GROUPS], exgroupcount = 0;
94 sampler_voice_start(v, c, layer->runtime, layer->runtime->pitch_keycenter, 127, exgroups, &exgroupcount);
95 sampler_channel_release_groups(c, -1, exgroups, exgroupcount);
99 int was_enabled = c->cc[cc] >= 64;
100 int enabled = val >= 64;
101 switch(cc)
103 case 10:
104 case 10 + 32:
105 c->cc[cc] = val;
106 if (!c->module->disable_mixer_controls)
107 c->channel_pan_cc = sampler_channel_addcc(c, 10);
108 break;
109 case 7:
110 case 7 + 32:
111 c->cc[cc] = val;
112 if (!c->module->disable_mixer_controls)
113 c->channel_volume_cc = sampler_channel_addcc(c, 7);
114 break;
115 case 64:
116 if (was_enabled && !enabled)
118 sampler_channel_stop_sustained(c);
120 break;
121 case 66:
122 if (was_enabled && !enabled)
123 sampler_channel_stop_sostenuto(c);
124 else if (!was_enabled && enabled)
125 sampler_channel_capture_sostenuto(c);
126 break;
128 case 120:
129 case 123:
130 sampler_channel_stop_all(c);
131 break;
132 case 121:
133 // Recommended Practice (RP-015) Response to Reset All Controllers
134 // http://www.midi.org/techspecs/rp15.php
135 sampler_channel_process_cc(c, 64, 0);
136 sampler_channel_process_cc(c, 66, 0);
137 c->cc[11] = 127;
138 c->cc[1] = 0;
139 c->pitchwheel = 0;
140 c->cc[smsrc_chanaft] = 0;
141 // XXXKF reset polyphonic pressure values when supported
142 return;
144 if (cc < 120)
145 c->cc[cc] = val;
148 void sampler_channel_release_groups(struct sampler_channel *c, int note, int exgroups[MAX_RELEASED_GROUPS], int exgroupcount)
150 if (exgroupcount)
152 FOREACH_VOICE(c->voices_running, v)
154 for (int j = 0; j < exgroupcount; j++)
156 if (v->off_by == exgroups[j] && v->note != note)
158 if (v->layer->off_mode == som_fast)
160 v->released = 1;
161 cbox_envelope_go_to(&v->amp_env, 15);
163 else
165 v->released = 1;
167 break;
174 void sampler_channel_start_note(struct sampler_channel *c, int note, int vel, gboolean is_release_trigger)
176 struct sampler_module *m = c->module;
177 float random = rand() * 1.0 / (RAND_MAX + 1.0);
178 gboolean is_first = FALSE;
179 if (!is_release_trigger)
181 c->switchmask[note >> 5] |= 1 << (note & 31);
182 c->prev_note_velocity[note] = vel;
183 c->prev_note_start_time[note] = m->current_time;
184 is_first = TRUE;
185 FOREACH_VOICE(c->voices_running, v)
187 if (!v->released && v->layer->trigger != stm_release)
189 is_first = FALSE;
190 break;
194 struct sampler_program *prg = c->program;
195 if (!prg || !prg->rll || prg->deleting)
196 return;
197 GSList *next_layer = sampler_program_get_next_layer(prg, c, !is_release_trigger ? prg->rll->layers : prg->rll->layers_release, note, vel, random, is_first);
198 if (!next_layer)
200 if (!is_release_trigger)
201 c->previous_note = note;
202 return;
205 // this might perhaps be optimized by mapping the group identifiers to flat-array indexes
206 // but I'm not going to do that until someone gives me an SFZ worth doing that work ;)
207 int exgroups[MAX_RELEASED_GROUPS], exgroupcount = 0;
209 FOREACH_VOICE(m->voices_free, v)
211 struct sampler_layer *l = next_layer->data;
212 // Maybe someone forgot to call sampler_update_layer?
213 assert(l->runtime);
214 sampler_voice_start(v, c, l->runtime, note, vel, exgroups, &exgroupcount);
215 next_layer = sampler_program_get_next_layer(prg, c, g_slist_next(next_layer), note, vel, random, is_first);
216 if (!next_layer)
217 break;
219 if (!is_release_trigger)
220 c->previous_note = note;
221 sampler_channel_release_groups(c, note, exgroups, exgroupcount);
224 void sampler_channel_start_release_triggered_voices(struct sampler_channel *c, int note)
226 if (c->program && c->program->rll && c->program->rll->layers_release)
228 if (c->prev_note_velocity[note])
230 sampler_channel_start_note(c, note, c->prev_note_velocity[note], TRUE);
231 c->prev_note_velocity[note] = 0;
236 void sampler_channel_stop_note(struct sampler_channel *c, int note, int vel, gboolean is_polyaft)
238 c->switchmask[note >> 5] &= ~(1 << (note & 31));
239 FOREACH_VOICE(c->voices_running, v)
241 if (v->note == note && v->layer->trigger != stm_release)
243 if (v->captured_sostenuto)
244 v->released_with_sostenuto = 1;
245 else if (c->cc[64] >= 64)
246 v->released_with_sustain = 1;
247 else
248 sampler_voice_release(v, is_polyaft);
252 if (c->cc[64] < 64)
253 sampler_channel_start_release_triggered_voices(c, note);
254 else
255 c->sustainmask[note >> 5] |= (1 << (note & 31));
258 void sampler_channel_stop_sustained(struct sampler_channel *c)
260 FOREACH_VOICE(c->voices_running, v)
262 if (v->channel == c && v->released_with_sustain && v->layer->trigger != stm_release)
264 sampler_voice_release(v, FALSE);
265 v->released_with_sustain = 0;
268 // Start release layers for the newly released keys
269 if (c->program && c->program->rll && c->program->rll->layers_release)
271 for (int i = 0; i < 128; i++)
273 if (c->sustainmask[i >> 5] & (1 << (i & 31)))
274 sampler_channel_start_release_triggered_voices(c, i);
277 memset(c->sustainmask, 0, sizeof(c->sustainmask));
280 void sampler_channel_stop_sostenuto(struct sampler_channel *c)
282 FOREACH_VOICE(c->voices_running, v)
284 if (v->released_with_sostenuto && v->layer->trigger != stm_release)
286 sampler_channel_start_release_triggered_voices(c, v->note);
287 sampler_voice_release(v, FALSE);
288 v->released_with_sostenuto = 0;
289 // XXXKF unsure what to do with sustain
292 // Start release layers for the newly released keys
293 if (c->program && c->program->rll && c->program->rll->layers_release)
295 for (int i = 0; i < 128; i++)
297 if (c->sostenutomask[i >> 5] & (1 << (i & 31)))
298 sampler_channel_start_release_triggered_voices(c, i);
301 memset(c->sostenutomask, 0, sizeof(c->sostenutomask));
304 void sampler_channel_capture_sostenuto(struct sampler_channel *c)
306 FOREACH_VOICE(c->voices_running, v)
308 if (!v->released && v->loop_mode != slm_one_shot && v->loop_mode != slm_one_shot_chokeable && !v->layer->count)
310 // XXXKF unsure what to do with sustain
311 v->captured_sostenuto = 1;
312 c->sostenutomask[v->note >> 5] |= (1 << (v->note & 31));
317 void sampler_channel_stop_all(struct sampler_channel *c)
319 FOREACH_VOICE(c->voices_running, v)
321 sampler_voice_release(v, v->loop_mode == slm_one_shot_chokeable);
322 v->released_with_sustain = 0;
323 v->released_with_sostenuto = 0;
324 v->captured_sostenuto = 0;
328 void sampler_channel_set_program_RT(struct sampler_channel *c, struct sampler_program *prg)
330 if (c->program)
331 c->program->in_use--;
332 c->program = prg;
333 if (prg)
335 for(GSList *p = prg->ctrl_init_list; p; p = p->next)
337 union sampler_ctrlinit_union u;
338 u.ptr = p->data;
339 // printf("Setting controller %d -> %d\n", u.cinit.controller, u.cinit.value);
340 c->cc[u.cinit.controller] = u.cinit.value;
342 c->program->in_use++;
346 #define sampler_channel_set_program_args(ARG) ARG(struct sampler_program *, prg)
348 DEFINE_RT_VOID_FUNC(sampler_channel, c, sampler_channel_set_program)
350 sampler_channel_set_program_RT(c, prg);
353 void sampler_channel_program_change(struct sampler_channel *c, int program)
355 struct sampler_module *m = c->module;
356 // XXXKF replace with something more efficient
357 for (int i = 0; i < m->program_count; i++)
359 // XXXKF support banks
360 if (m->programs[i]->prog_no == program)
362 sampler_channel_set_program_RT(c, m->programs[i]);
363 return;
366 g_warning("Unknown program %d", program);
367 sampler_channel_set_program_RT(c, m->programs[0]);