Merge pull request #1 from atsampson/master
[calfbox.git] / sampler_channel.c
blob5b36c3dacbeab6996025fa43d20814eaf209cdd8
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)
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 if (!v)
94 break;
95 int exgroups[MAX_RELEASED_GROUPS], exgroupcount = 0;
96 sampler_voice_start(v, c, layer->runtime, layer->runtime->pitch_keycenter, 127, exgroups, &exgroupcount);
97 sampler_channel_release_groups(c, -1, exgroups, exgroupcount);
101 int was_enabled = c->cc[cc] >= 64;
102 int enabled = val >= 64;
103 switch(cc)
105 case 10:
106 case 10 + 32:
107 c->cc[cc] = val;
108 if (!c->module->disable_mixer_controls)
109 c->channel_pan_cc = sampler_channel_addcc(c, 10);
110 break;
111 case 7:
112 case 7 + 32:
113 c->cc[cc] = val;
114 if (!c->module->disable_mixer_controls)
115 c->channel_volume_cc = sampler_channel_addcc(c, 7);
116 break;
117 case 64:
118 if (was_enabled && !enabled)
120 sampler_channel_stop_sustained(c);
122 break;
123 case 66:
124 if (was_enabled && !enabled)
125 sampler_channel_stop_sostenuto(c);
126 else if (!was_enabled && enabled)
127 sampler_channel_capture_sostenuto(c);
128 break;
130 case 120:
131 case 123:
132 sampler_channel_stop_all(c);
133 break;
134 case 121:
135 // Recommended Practice (RP-015) Response to Reset All Controllers
136 // http://www.midi.org/techspecs/rp15.php
137 sampler_channel_process_cc(c, 64, 0);
138 sampler_channel_process_cc(c, 66, 0);
139 c->cc[11] = 127;
140 c->cc[1] = 0;
141 c->pitchwheel = 0;
142 c->cc[smsrc_chanaft] = 0;
143 // XXXKF reset polyphonic pressure values when supported
144 return;
146 if (cc < 120)
147 c->cc[cc] = val;
150 void sampler_channel_release_groups(struct sampler_channel *c, int note, int exgroups[MAX_RELEASED_GROUPS], int exgroupcount)
152 if (exgroupcount)
154 FOREACH_VOICE(c->voices_running, v)
156 for (int j = 0; j < exgroupcount; j++)
158 if (v->off_by == exgroups[j] && v->note != note)
160 if (v->layer->off_mode == som_fast)
162 v->released = 1;
163 cbox_envelope_go_to(&v->amp_env, 15);
165 else
167 v->released = 1;
169 break;
176 void sampler_channel_start_note(struct sampler_channel *c, int note, int vel, gboolean is_release_trigger)
178 struct sampler_module *m = c->module;
179 float random = rand() * 1.0 / (RAND_MAX + 1.0);
180 gboolean is_first = FALSE;
181 if (!is_release_trigger)
183 c->switchmask[note >> 5] |= 1 << (note & 31);
184 c->prev_note_velocity[note] = vel;
185 c->prev_note_start_time[note] = m->current_time;
186 is_first = TRUE;
187 FOREACH_VOICE(c->voices_running, v)
189 if (!v->released && v->layer->trigger != stm_release)
191 is_first = FALSE;
192 break;
196 struct sampler_program *prg = c->program;
197 if (!prg || !prg->rll || prg->deleting)
198 return;
199 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);
200 if (!next_layer)
202 if (!is_release_trigger)
203 c->previous_note = note;
204 return;
207 // this might perhaps be optimized by mapping the group identifiers to flat-array indexes
208 // but I'm not going to do that until someone gives me an SFZ worth doing that work ;)
209 int exgroups[MAX_RELEASED_GROUPS], exgroupcount = 0;
211 FOREACH_VOICE(m->voices_free, v)
213 struct sampler_layer *l = next_layer->data;
214 // Maybe someone forgot to call sampler_update_layer?
215 assert(l->runtime);
216 sampler_voice_start(v, c, l->runtime, note, vel, exgroups, &exgroupcount);
217 next_layer = sampler_program_get_next_layer(prg, c, g_slist_next(next_layer), note, vel, random, is_first);
218 if (!next_layer)
219 break;
221 if (!is_release_trigger)
222 c->previous_note = note;
223 sampler_channel_release_groups(c, note, exgroups, exgroupcount);
226 void sampler_channel_start_release_triggered_voices(struct sampler_channel *c, int note)
228 if (c->program && c->program->rll && c->program->rll->layers_release)
230 if (c->prev_note_velocity[note])
232 sampler_channel_start_note(c, note, c->prev_note_velocity[note], TRUE);
233 c->prev_note_velocity[note] = 0;
238 void sampler_channel_stop_note(struct sampler_channel *c, int note, int vel, gboolean is_polyaft)
240 c->switchmask[note >> 5] &= ~(1 << (note & 31));
241 FOREACH_VOICE(c->voices_running, v)
243 if (v->note == note && v->layer->trigger != stm_release)
245 if (v->captured_sostenuto)
246 v->released_with_sostenuto = 1;
247 else if (c->cc[64] >= 64)
248 v->released_with_sustain = 1;
249 else
250 sampler_voice_release(v, is_polyaft);
254 if (c->cc[64] < 64)
255 sampler_channel_start_release_triggered_voices(c, note);
256 else
257 c->sustainmask[note >> 5] |= (1 << (note & 31));
260 void sampler_channel_stop_sustained(struct sampler_channel *c)
262 FOREACH_VOICE(c->voices_running, v)
264 if (v->channel == c && v->released_with_sustain && v->layer->trigger != stm_release)
266 sampler_voice_release(v, FALSE);
267 v->released_with_sustain = 0;
270 // Start release layers for the newly released keys
271 if (c->program && c->program->rll && c->program->rll->layers_release)
273 for (int i = 0; i < 128; i++)
275 if (c->sustainmask[i >> 5] & (1 << (i & 31)))
276 sampler_channel_start_release_triggered_voices(c, i);
279 memset(c->sustainmask, 0, sizeof(c->sustainmask));
282 void sampler_channel_stop_sostenuto(struct sampler_channel *c)
284 FOREACH_VOICE(c->voices_running, v)
286 if (v->released_with_sostenuto && v->layer->trigger != stm_release)
288 sampler_channel_start_release_triggered_voices(c, v->note);
289 sampler_voice_release(v, FALSE);
290 v->released_with_sostenuto = 0;
291 // XXXKF unsure what to do with sustain
294 // Start release layers for the newly released keys
295 if (c->program && c->program->rll && c->program->rll->layers_release)
297 for (int i = 0; i < 128; i++)
299 if (c->sostenutomask[i >> 5] & (1 << (i & 31)))
300 sampler_channel_start_release_triggered_voices(c, i);
303 memset(c->sostenutomask, 0, sizeof(c->sostenutomask));
306 void sampler_channel_capture_sostenuto(struct sampler_channel *c)
308 FOREACH_VOICE(c->voices_running, v)
310 if (!v->released && v->loop_mode != slm_one_shot && v->loop_mode != slm_one_shot_chokeable && !v->layer->count)
312 // XXXKF unsure what to do with sustain
313 v->captured_sostenuto = 1;
314 c->sostenutomask[v->note >> 5] |= (1 << (v->note & 31));
319 void sampler_channel_stop_all(struct sampler_channel *c)
321 FOREACH_VOICE(c->voices_running, v)
323 sampler_voice_release(v, v->loop_mode == slm_one_shot_chokeable);
324 v->released_with_sustain = 0;
325 v->released_with_sostenuto = 0;
326 v->captured_sostenuto = 0;
330 void sampler_channel_set_program_RT(struct sampler_channel *c, struct sampler_program *prg)
332 if (c->program)
333 c->program->in_use--;
334 c->program = prg;
335 if (prg)
337 for(GSList *p = prg->ctrl_init_list; p; p = p->next)
339 union sampler_ctrlinit_union u;
340 u.ptr = p->data;
341 // printf("Setting controller %d -> %d\n", u.cinit.controller, u.cinit.value);
342 c->cc[u.cinit.controller] = u.cinit.value;
344 c->program->in_use++;
348 #define sampler_channel_set_program_args(ARG) ARG(struct sampler_program *, prg)
350 DEFINE_RT_VOID_FUNC(sampler_channel, c, sampler_channel_set_program)
352 sampler_channel_set_program_RT(c, prg);
355 void sampler_channel_program_change(struct sampler_channel *c, int program)
357 struct sampler_module *m = c->module;
358 // XXXKF replace with something more efficient
359 for (int i = 0; i < m->program_count; i++)
361 // XXXKF support banks
362 if (m->programs[i]->prog_no == program)
364 sampler_channel_set_program_RT(c, m->programs[i]);
365 return;
368 g_warning("Unknown program %d", program);
369 sampler_channel_set_program_RT(c, m->programs[0]);