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
;
44 memset(c
->cc
, 0, sizeof(c
->cc
));
46 // default to maximum and pan=centre if MIDI mixing disabled
47 if (m
->disable_mixer_controls
)
49 c
->channel_volume_cc
= 16383;
50 c
->channel_pan_cc
= 8192;
54 sampler_channel_process_cc(c
, 7, 100);
55 sampler_channel_process_cc(c
, 7 + 32, 0);
56 sampler_channel_process_cc(c
, 10, 64);
57 sampler_channel_process_cc(c
, 10 + 32, 0);
62 c
->previous_note
= -1;
64 sampler_channel_set_program_RT(c
, m
->program_count
? m
->programs
[0] : NULL
);
65 memset(c
->switchmask
, 0, sizeof(c
->switchmask
));
66 memset(c
->sustainmask
, 0, sizeof(c
->sustainmask
));
67 memset(c
->sostenutomask
, 0, sizeof(c
->sostenutomask
));
70 void sampler_channel_process_cc(struct sampler_channel
*c
, int cc
, int val
)
72 struct sampler_module
*m
= c
->module
;
73 // Handle CC triggering.
74 if (c
->program
&& c
->program
->rll
&& c
->program
->rll
->layers_oncc
&& m
->voices_free
)
76 struct sampler_rll
*rll
= c
->program
->rll
;
77 if (!(rll
->cc_trigger_bitmask
[cc
>> 5] & (1 << (cc
& 31))))
79 int old_value
= c
->cc
[cc
];
80 for (GSList
*p
= rll
->layers_oncc
; p
; p
= p
->next
)
82 struct sampler_layer
*layer
= p
->data
;
83 assert(layer
->runtime
);
84 // Only trigger on transition between 'out of range' and 'in range' values.
85 // XXXKF I'm not sure if it's what is expected here, but don't have
86 // the reference implementation handy.
87 if (layer
->runtime
->on_cc_number
== cc
&&
88 (val
>= layer
->runtime
->on_locc
&& val
<= layer
->runtime
->on_hicc
) &&
89 !(old_value
>= layer
->runtime
->on_locc
&& old_value
<= layer
->runtime
->on_hicc
))
91 struct sampler_voice
*v
= m
->voices_free
;
92 int exgroups
[MAX_RELEASED_GROUPS
], exgroupcount
= 0;
93 sampler_voice_start(v
, c
, layer
->runtime
, layer
->runtime
->pitch_keycenter
, 127, exgroups
, &exgroupcount
);
94 sampler_channel_release_groups(c
, -1, exgroups
, exgroupcount
);
98 int was_enabled
= c
->cc
[cc
] >= 64;
99 int enabled
= val
>= 64;
105 if (!c
->module
->disable_mixer_controls
)
106 c
->channel_pan_cc
= sampler_channel_addcc(c
, 10);
111 if (!c
->module
->disable_mixer_controls
)
112 c
->channel_volume_cc
= sampler_channel_addcc(c
, 7);
115 if (was_enabled
&& !enabled
)
117 sampler_channel_stop_sustained(c
);
121 if (was_enabled
&& !enabled
)
122 sampler_channel_stop_sostenuto(c
);
123 else if (!was_enabled
&& enabled
)
124 sampler_channel_capture_sostenuto(c
);
129 sampler_channel_stop_all(c
);
132 // Recommended Practice (RP-015) Response to Reset All Controllers
133 // http://www.midi.org/techspecs/rp15.php
134 sampler_channel_process_cc(c
, 64, 0);
135 sampler_channel_process_cc(c
, 66, 0);
139 c
->cc
[smsrc_chanaft
] = 0;
140 // XXXKF reset polyphonic pressure values when supported
147 void sampler_channel_release_groups(struct sampler_channel
*c
, int note
, int exgroups
[MAX_RELEASED_GROUPS
], int exgroupcount
)
151 FOREACH_VOICE(c
->voices_running
, v
)
153 for (int j
= 0; j
< exgroupcount
; j
++)
155 if (v
->off_by
== exgroups
[j
] && v
->note
!= note
)
157 if (v
->layer
->off_mode
== som_fast
)
160 cbox_envelope_go_to(&v
->amp_env
, 15);
173 void sampler_channel_start_note(struct sampler_channel
*c
, int note
, int vel
, gboolean is_release_trigger
)
175 struct sampler_module
*m
= c
->module
;
176 float random
= rand() * 1.0 / (RAND_MAX
+ 1.0);
177 gboolean is_first
= FALSE
;
178 if (!is_release_trigger
)
180 c
->switchmask
[note
>> 5] |= 1 << (note
& 31);
181 c
->prev_note_velocity
[note
] = vel
;
182 c
->prev_note_start_time
[note
] = m
->current_time
;
184 FOREACH_VOICE(c
->voices_running
, v
)
186 if (!v
->released
&& v
->layer
->trigger
!= stm_release
)
193 struct sampler_program
*prg
= c
->program
;
194 if (!prg
|| !prg
->rll
|| prg
->deleting
)
196 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
);
199 if (!is_release_trigger
)
200 c
->previous_note
= note
;
204 // this might perhaps be optimized by mapping the group identifiers to flat-array indexes
205 // but I'm not going to do that until someone gives me an SFZ worth doing that work ;)
206 int exgroups
[MAX_RELEASED_GROUPS
], exgroupcount
= 0;
208 FOREACH_VOICE(m
->voices_free
, v
)
210 struct sampler_layer
*l
= next_layer
->data
;
211 // Maybe someone forgot to call sampler_update_layer?
213 sampler_voice_start(v
, c
, l
->runtime
, note
, vel
, exgroups
, &exgroupcount
);
214 next_layer
= sampler_program_get_next_layer(prg
, c
, g_slist_next(next_layer
), note
, vel
, random
, is_first
);
218 if (!is_release_trigger
)
219 c
->previous_note
= note
;
220 sampler_channel_release_groups(c
, note
, exgroups
, exgroupcount
);
223 void sampler_channel_start_release_triggered_voices(struct sampler_channel
*c
, int note
)
225 if (c
->program
&& c
->program
->rll
&& c
->program
->rll
->layers_release
)
227 if (c
->prev_note_velocity
[note
])
229 sampler_channel_start_note(c
, note
, c
->prev_note_velocity
[note
], TRUE
);
230 c
->prev_note_velocity
[note
] = 0;
235 void sampler_channel_stop_note(struct sampler_channel
*c
, int note
, int vel
, gboolean is_polyaft
)
237 c
->switchmask
[note
>> 5] &= ~(1 << (note
& 31));
238 FOREACH_VOICE(c
->voices_running
, v
)
240 if (v
->note
== note
&& v
->layer
->trigger
!= stm_release
)
242 if (v
->captured_sostenuto
)
243 v
->released_with_sostenuto
= 1;
244 else if (c
->cc
[64] >= 64)
245 v
->released_with_sustain
= 1;
247 sampler_voice_release(v
, is_polyaft
);
252 sampler_channel_start_release_triggered_voices(c
, note
);
254 c
->sustainmask
[note
>> 5] |= (1 << (note
& 31));
257 void sampler_channel_stop_sustained(struct sampler_channel
*c
)
259 FOREACH_VOICE(c
->voices_running
, v
)
261 if (v
->channel
== c
&& v
->released_with_sustain
&& v
->layer
->trigger
!= stm_release
)
263 sampler_voice_release(v
, FALSE
);
264 v
->released_with_sustain
= 0;
267 // Start release layers for the newly released keys
268 if (c
->program
&& c
->program
->rll
&& c
->program
->rll
->layers_release
)
270 for (int i
= 0; i
< 128; i
++)
272 if (c
->sustainmask
[i
>> 5] & (1 << (i
& 31)))
273 sampler_channel_start_release_triggered_voices(c
, i
);
276 memset(c
->sustainmask
, 0, sizeof(c
->sustainmask
));
279 void sampler_channel_stop_sostenuto(struct sampler_channel
*c
)
281 FOREACH_VOICE(c
->voices_running
, v
)
283 if (v
->released_with_sostenuto
&& v
->layer
->trigger
!= stm_release
)
285 sampler_channel_start_release_triggered_voices(c
, v
->note
);
286 sampler_voice_release(v
, FALSE
);
287 v
->released_with_sostenuto
= 0;
288 // XXXKF unsure what to do with sustain
291 // Start release layers for the newly released keys
292 if (c
->program
&& c
->program
->rll
&& c
->program
->rll
->layers_release
)
294 for (int i
= 0; i
< 128; i
++)
296 if (c
->sostenutomask
[i
>> 5] & (1 << (i
& 31)))
297 sampler_channel_start_release_triggered_voices(c
, i
);
300 memset(c
->sostenutomask
, 0, sizeof(c
->sostenutomask
));
303 void sampler_channel_capture_sostenuto(struct sampler_channel
*c
)
305 FOREACH_VOICE(c
->voices_running
, v
)
307 if (!v
->released
&& v
->loop_mode
!= slm_one_shot
&& v
->loop_mode
!= slm_one_shot_chokeable
&& !v
->layer
->count
)
309 // XXXKF unsure what to do with sustain
310 v
->captured_sostenuto
= 1;
311 c
->sostenutomask
[v
->note
>> 5] |= (1 << (v
->note
& 31));
316 void sampler_channel_stop_all(struct sampler_channel
*c
)
318 FOREACH_VOICE(c
->voices_running
, v
)
320 sampler_voice_release(v
, v
->loop_mode
== slm_one_shot_chokeable
);
321 v
->released_with_sustain
= 0;
322 v
->released_with_sostenuto
= 0;
323 v
->captured_sostenuto
= 0;
327 void sampler_channel_set_program_RT(struct sampler_channel
*c
, struct sampler_program
*prg
)
330 c
->program
->in_use
--;
334 for(GSList
*p
= prg
->ctrl_init_list
; p
; p
= p
->next
)
336 union sampler_ctrlinit_union u
;
338 // printf("Setting controller %d -> %d\n", u.cinit.controller, u.cinit.value);
339 c
->cc
[u
.cinit
.controller
] = u
.cinit
.value
;
341 c
->program
->in_use
++;
345 #define sampler_channel_set_program_args(ARG) ARG(struct sampler_program *, prg)
347 DEFINE_RT_VOID_FUNC(sampler_channel
, c
, sampler_channel_set_program
)
349 sampler_channel_set_program_RT(c
, prg
);
352 void sampler_channel_program_change(struct sampler_channel
*c
, int program
)
354 struct sampler_module
*m
= c
->module
;
355 // XXXKF replace with something more efficient
356 for (int i
= 0; i
< m
->program_count
; i
++)
358 // XXXKF support banks
359 if (m
->programs
[i
]->prog_no
== program
)
361 sampler_channel_set_program_RT(c
, m
->programs
[i
]);
365 g_warning("Unknown program %d", program
);
366 sampler_channel_set_program_RT(c
, m
->programs
[0]);