We're including config.h everywhere, so we should pass compatible compiler options.
[calfbox.git] / sampler_channel.c
blob6531db94ac9a96057df62b51a1b889cb4eac9f93
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 memset(c->cc, 0, sizeof(c->cc));
45 c->cc[7] = 100;
46 c->cc[10] = 64;
47 c->cc[11] = 127;
48 c->cc[71] = 64;
49 c->cc[74] = 64;
50 c->previous_note = -1;
51 c->program = NULL;
52 sampler_channel_set_program_RT(c, m->program_count ? m->programs[0] : NULL);
53 memset(c->switchmask, 0, sizeof(c->switchmask));
54 memset(c->sustainmask, 0, sizeof(c->sustainmask));
55 memset(c->sostenutomask, 0, sizeof(c->sostenutomask));
58 void sampler_channel_release_groups(struct sampler_channel *c, int note, int exgroups[MAX_RELEASED_GROUPS], int exgroupcount)
60 if (exgroupcount)
62 FOREACH_VOICE(c->voices_running, v)
64 for (int j = 0; j < exgroupcount; j++)
66 if (v->off_by == exgroups[j] && v->note != note)
68 if (v->layer->off_mode == som_fast)
70 v->released = 1;
71 cbox_envelope_go_to(&v->amp_env, 15);
73 else
75 v->released = 1;
77 break;
84 void sampler_channel_start_note(struct sampler_channel *c, int note, int vel, gboolean is_release_trigger)
86 struct sampler_module *m = c->module;
87 float random = rand() * 1.0 / (RAND_MAX + 1.0);
88 gboolean is_first = FALSE;
89 if (!is_release_trigger)
91 c->switchmask[note >> 5] |= 1 << (note & 31);
92 c->prev_note_velocity[note] = vel;
93 c->prev_note_start_time[note] = m->current_time;
94 is_first = TRUE;
95 FOREACH_VOICE(c->voices_running, v)
97 if (!v->released && v->layer->trigger != stm_release)
99 is_first = FALSE;
100 break;
104 struct sampler_program *prg = c->program;
105 if (!prg || !prg->rll || prg->deleting)
106 return;
107 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);
108 if (!next_layer)
110 if (!is_release_trigger)
111 c->previous_note = note;
112 return;
115 // this might perhaps be optimized by mapping the group identifiers to flat-array indexes
116 // but I'm not going to do that until someone gives me an SFZ worth doing that work ;)
117 int exgroups[MAX_RELEASED_GROUPS], exgroupcount = 0;
119 FOREACH_VOICE(m->voices_free, v)
121 struct sampler_layer *l = next_layer->data;
122 // Maybe someone forgot to call sampler_update_layer?
123 assert(l->runtime);
124 sampler_voice_start(v, c, l->runtime, note, vel, exgroups, &exgroupcount);
125 next_layer = sampler_program_get_next_layer(prg, c, g_slist_next(next_layer), note, vel, random, is_first);
126 if (!next_layer)
127 break;
129 if (!is_release_trigger)
130 c->previous_note = note;
131 sampler_channel_release_groups(c, note, exgroups, exgroupcount);
134 void sampler_channel_start_release_triggered_voices(struct sampler_channel *c, int note)
136 if (c->program && c->program->rll && c->program->rll->layers_release)
138 if (c->prev_note_velocity[note])
140 sampler_channel_start_note(c, note, c->prev_note_velocity[note], TRUE);
141 c->prev_note_velocity[note] = 0;
146 void sampler_channel_stop_note(struct sampler_channel *c, int note, int vel, gboolean is_polyaft)
148 c->switchmask[note >> 5] &= ~(1 << (note & 31));
149 FOREACH_VOICE(c->voices_running, v)
151 if (v->note == note && v->layer->trigger != stm_release)
153 if (v->captured_sostenuto)
154 v->released_with_sostenuto = 1;
155 else if (c->cc[64] >= 64)
156 v->released_with_sustain = 1;
157 else
158 sampler_voice_release(v, is_polyaft);
162 if (c->cc[64] < 64)
163 sampler_channel_start_release_triggered_voices(c, note);
164 else
165 c->sustainmask[note >> 5] |= (1 << (note & 31));
168 void sampler_channel_stop_sustained(struct sampler_channel *c)
170 FOREACH_VOICE(c->voices_running, v)
172 if (v->channel == c && v->released_with_sustain && v->layer->trigger != stm_release)
174 sampler_voice_release(v, FALSE);
175 v->released_with_sustain = 0;
178 // Start release layers for the newly released keys
179 if (c->program && c->program->rll && c->program->rll->layers_release)
181 for (int i = 0; i < 128; i++)
183 if (c->sustainmask[i >> 5] & (1 << (i & 31)))
184 sampler_channel_start_release_triggered_voices(c, i);
187 memset(c->sustainmask, 0, sizeof(c->sustainmask));
190 void sampler_channel_stop_sostenuto(struct sampler_channel *c)
192 FOREACH_VOICE(c->voices_running, v)
194 if (v->released_with_sostenuto && v->layer->trigger != stm_release)
196 sampler_channel_start_release_triggered_voices(c, v->note);
197 sampler_voice_release(v, FALSE);
198 v->released_with_sostenuto = 0;
199 // XXXKF unsure what to do with sustain
202 // Start release layers for the newly released keys
203 if (c->program && c->program->rll && c->program->rll->layers_release)
205 for (int i = 0; i < 128; i++)
207 if (c->sostenutomask[i >> 5] & (1 << (i & 31)))
208 sampler_channel_start_release_triggered_voices(c, i);
211 memset(c->sostenutomask, 0, sizeof(c->sostenutomask));
214 void sampler_channel_capture_sostenuto(struct sampler_channel *c)
216 FOREACH_VOICE(c->voices_running, v)
218 if (!v->released && v->loop_mode != slm_one_shot && v->loop_mode != slm_one_shot_chokeable && !v->layer->count)
220 // XXXKF unsure what to do with sustain
221 v->captured_sostenuto = 1;
222 c->sostenutomask[v->note >> 5] |= (1 << (v->note & 31));
227 void sampler_channel_stop_all(struct sampler_channel *c)
229 FOREACH_VOICE(c->voices_running, v)
231 sampler_voice_release(v, v->loop_mode == slm_one_shot_chokeable);
232 v->released_with_sustain = 0;
233 v->released_with_sostenuto = 0;
234 v->captured_sostenuto = 0;
238 void sampler_channel_set_program_RT(struct sampler_channel *c, struct sampler_program *prg)
240 if (c->program)
241 c->program->in_use--;
242 c->program = prg;
243 if (prg)
245 for(GSList *p = prg->ctrl_init_list; p; p = p->next)
247 union sampler_ctrlinit_union u;
248 u.ptr = p->data;
249 // printf("Setting controller %d -> %d\n", u.cinit.controller, u.cinit.value);
250 c->cc[u.cinit.controller] = u.cinit.value;
252 c->program->in_use++;
256 #define sampler_channel_set_program_args(ARG) ARG(struct sampler_program *, prg)
258 DEFINE_RT_VOID_FUNC(sampler_channel, c, sampler_channel_set_program)
260 sampler_channel_set_program_RT(c, prg);
263 void sampler_channel_program_change(struct sampler_channel *c, int program)
265 struct sampler_module *m = c->module;
266 // XXXKF replace with something more efficient
267 for (int i = 0; i < m->program_count; i++)
269 // XXXKF support banks
270 if (m->programs[i]->prog_no == program)
272 sampler_channel_set_program_RT(c, m->programs[i]);
273 return;
276 g_warning("Unknown program %d", program);
277 sampler_channel_set_program_RT(c, m->programs[0]);