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"
26 #include "sampler_impl.h"
27 #include "sfzloader.h"
38 void sampler_channel_init(struct sampler_channel
*c
, struct sampler_module
*m
)
41 c
->voices_running
= NULL
;
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;
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);
63 c
->previous_note
= -1;
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))))
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;
106 if (!c
->module
->disable_mixer_controls
)
107 c
->channel_pan_cc
= sampler_channel_addcc(c
, 10);
112 if (!c
->module
->disable_mixer_controls
)
113 c
->channel_volume_cc
= sampler_channel_addcc(c
, 7);
116 if (was_enabled
&& !enabled
)
118 sampler_channel_stop_sustained(c
);
122 if (was_enabled
&& !enabled
)
123 sampler_channel_stop_sostenuto(c
);
124 else if (!was_enabled
&& enabled
)
125 sampler_channel_capture_sostenuto(c
);
130 sampler_channel_stop_all(c
);
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);
140 c
->cc
[smsrc_chanaft
] = 0;
141 // XXXKF reset polyphonic pressure values when supported
148 void sampler_channel_release_groups(struct sampler_channel
*c
, int note
, int exgroups
[MAX_RELEASED_GROUPS
], int 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
)
161 cbox_envelope_go_to(&v
->amp_env
, 15);
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
;
185 FOREACH_VOICE(c
->voices_running
, v
)
187 if (!v
->released
&& v
->layer
->trigger
!= stm_release
)
194 struct sampler_program
*prg
= c
->program
;
195 if (!prg
|| !prg
->rll
|| prg
->deleting
)
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
);
200 if (!is_release_trigger
)
201 c
->previous_note
= note
;
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?
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
);
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;
248 sampler_voice_release(v
, is_polyaft
);
253 sampler_channel_start_release_triggered_voices(c
, note
);
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
)
331 c
->program
->in_use
--;
335 for(GSList
*p
= prg
->ctrl_init_list
; p
; p
= p
->next
)
337 union sampler_ctrlinit_union u
;
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
]);
366 g_warning("Unknown program %d", program
);
367 sampler_channel_set_program_RT(c
, m
->programs
[0]);