Experimental/risky: convert sampler voice to use AoS instead of SoA for left/right.
[calfbox.git] / sampler_voice.c
blob13957f621e950c618a4d965c38bf5af7e1d2c439
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.h"
20 #include "config-api.h"
21 #include "dspmath.h"
22 #include "errors.h"
23 #include "midi.h"
24 #include "module.h"
25 #include "rt.h"
26 #include "sampler.h"
27 #include "sampler_impl.h"
28 #include "sfzloader.h"
29 #include "stm.h"
30 #include <assert.h>
31 #include <errno.h>
32 #include <glib.h>
33 #include <math.h>
34 #include <memory.h>
35 #include <sndfile.h>
36 #include <stdio.h>
37 #include <stdlib.h>
40 static void lfo_init(struct sampler_lfo *lfo, struct sampler_lfo_params *lfop, int srate, double srate_inv)
42 lfo->phase = 0;
43 lfo->age = 0;
44 lfo->delta = (uint32_t)(lfop->freq * 65536.0 * 65536.0 * CBOX_BLOCK_SIZE * srate_inv);
45 lfo->delay = (uint32_t)(lfop->delay * srate);
46 lfo->fade = (uint32_t)(lfop->fade * srate);
49 static inline float lfo_run(struct sampler_lfo *lfo)
51 if (lfo->age < lfo->delay)
53 lfo->age += CBOX_BLOCK_SIZE;
54 return 0.f;
57 const int FRAC_BITS = 32 - 11;
58 lfo->phase += lfo->delta;
59 uint32_t iphase = lfo->phase >> FRAC_BITS;
60 float frac = (lfo->phase & ((1 << FRAC_BITS) - 1)) * (1.0 / (1 << FRAC_BITS));
62 float v = sampler_sine_wave[iphase] + (sampler_sine_wave[iphase + 1] - sampler_sine_wave[iphase]) * frac;
63 if (lfo->fade && lfo->age < lfo->delay + lfo->fade)
65 v *= (lfo->age - lfo->delay) * 1.0 / lfo->fade;
66 lfo->age += CBOX_BLOCK_SIZE;
69 return v;
72 static gboolean is_tail_finished(struct sampler_voice *v)
74 if (v->layer->cutoff == -1)
75 return TRUE;
76 double eps = 1.0 / 65536.0;
77 if (cbox_biquadf_is_audible(&v->filter_left, eps))
78 return FALSE;
79 if (cbox_biquadf_is_audible(&v->filter_right, eps))
80 return FALSE;
81 if (sampler_layer_data_is_4pole(v->layer))
83 if (cbox_biquadf_is_audible(&v->filter_left2, eps))
84 return FALSE;
85 if (cbox_biquadf_is_audible(&v->filter_right2, eps))
86 return FALSE;
89 return TRUE;
92 #if USE_NEON
94 #include <arm_neon.h>
96 static inline void mix_block_into_with_gain(cbox_sample_t **outputs, int oofs, float *src_leftright, float gain)
98 float *dst_left = outputs[oofs];
99 float *dst_right = outputs[oofs + 1];
100 float32x2_t gain2 = {gain, gain};
101 for (size_t i = 0; i < CBOX_BLOCK_SIZE; i += 2)
103 float32x2_t lr1 = vld1_f32(&src_leftright[2 * i]);
104 float32x2_t lr2 = vld1_f32(&src_leftright[2 * i + 2]);
105 float32x2x2_t lr12 = vtrn_f32(lr1, lr2);
106 float32x2_t dl1 = vld1_f32(&dst_left[i]);
107 float32x2_t dr1 = vld1_f32(&dst_right[i]);
109 float32x2_t l1 = vmla_f32(dl1, lr12.val[0], gain2);
110 vst1_f32(&dst_left[i], l1);
111 float32x2_t r1 = vmla_f32(dr1, lr12.val[1], gain2);
112 vst1_f32(&dst_right[i], r1);
116 static inline void mix_block_into(cbox_sample_t **outputs, int oofs, float *src_leftright)
118 float *dst_left = outputs[oofs];
119 float *dst_right = outputs[oofs + 1];
120 for (size_t i = 0; i < CBOX_BLOCK_SIZE; i += 2)
122 float32x2_t lr1 = vld1_f32(&src_leftright[2 * i]);
123 float32x2_t lr2 = vld1_f32(&src_leftright[2 * i + 2]);
124 float32x2x2_t lr12 = vtrn_f32(lr1, lr2);
125 float32x2_t dl1 = vld1_f32(&dst_left[i]);
126 float32x2_t dr1 = vld1_f32(&dst_right[i]);
128 float32x2_t l1 = vadd_f32(dl1, lr12.val[0]);
129 vst1_f32(&dst_left[i], l1);
130 float32x2_t r1 = vadd_f32(dr1, lr12.val[1]);
131 vst1_f32(&dst_right[i], r1);
135 #else
137 static inline void mix_block_into_with_gain(cbox_sample_t **outputs, int oofs, float *src_leftright, float gain)
139 cbox_sample_t *dst_left = outputs[oofs];
140 cbox_sample_t *dst_right = outputs[oofs + 1];
141 for (size_t i = 0; i < CBOX_BLOCK_SIZE; i++)
143 dst_left[i] += gain * src_leftright[2 * i];
144 dst_right[i] += gain * src_leftright[2 * i + 1];
148 static inline void mix_block_into(cbox_sample_t **outputs, int oofs, float *src_leftright)
150 cbox_sample_t *dst_left = outputs[oofs];
151 cbox_sample_t *dst_right = outputs[oofs + 1];
152 for (size_t i = 0; i < CBOX_BLOCK_SIZE; i++)
154 dst_left[i] += src_leftright[2 * i];
155 dst_right[i] += src_leftright[2 * i + 1];
159 #endif
161 ////////////////////////////////////////////////////////////////////////////////
163 void sampler_voice_activate(struct sampler_voice *v, enum sampler_player_type mode)
165 assert(v->gen.mode == spt_inactive);
166 sampler_voice_unlink(&v->program->module->voices_free, v);
167 assert(mode != spt_inactive);
168 assert(v->channel);
169 v->gen.mode = mode;
170 sampler_voice_link(&v->channel->voices_running, v);
173 void sampler_voice_start(struct sampler_voice *v, struct sampler_channel *c, struct sampler_layer_data *l, int note, int vel, int *exgroups, int *pexgroupcount)
175 struct sampler_module *m = c->module;
176 sampler_gen_reset(&v->gen);
178 v->age = 0;
179 if (l->trigger == stm_release)
181 // time since last 'note on' for that note
182 v->age = m->current_time - c->prev_note_start_time[note];
183 double age = v->age * m->module.srate_inv;
184 // if attenuation is more than 84dB, ignore the release trigger
185 if (age * l->rt_decay > 84)
186 return;
188 uint32_t end = l->eff_waveform->info.frames;
189 if (l->end != 0)
190 end = (l->end == -1) ? 0 : l->end;
191 v->last_waveform = l->eff_waveform;
192 v->gen.cur_sample_end = end;
193 if (end > l->eff_waveform->info.frames)
194 end = l->eff_waveform->info.frames;
196 assert(!v->current_pipe);
197 if (end > l->eff_waveform->preloaded_frames)
199 if (l->loop_mode == slm_loop_continuous && l->loop_end < l->eff_waveform->preloaded_frames)
201 // Everything fits in prefetch, because loop ends in prefetch and post-loop part is not being played
203 else
205 uint32_t loop_start = -1, loop_end = end;
206 // If in loop mode, set the loop over the looped part... unless we're doing sustain-only loop on prefetch area only. Then
207 // streaming will only cover the release part, and it shouldn't be looped.
208 if (l->loop_mode == slm_loop_continuous || (l->loop_mode == slm_loop_sustain && l->loop_end >= l->eff_waveform->preloaded_frames))
210 loop_start = l->loop_start;
211 loop_end = l->loop_end;
213 // Those are initial values only, they will be adjusted in process function
214 v->current_pipe = cbox_prefetch_stack_pop(m->pipe_stack, l->eff_waveform, loop_start, loop_end, l->count);
215 if (!v->current_pipe)
216 g_warning("Prefetch pipe pool exhausted, no streaming playback will be possible");
220 v->output_pair_no = l->output % m->output_pairs;
221 v->serial_no = m->serial_no;
223 uint32_t pos = l->offset;
224 pos = l->offset;
225 if (l->offset_random)
226 pos += ((uint32_t)(rand() + (rand() << 16))) % l->offset_random;
227 if (pos >= end)
228 pos = end;
229 v->gen.bigpos = ((uint64_t)pos) << 32;
231 float delay = l->delay;
232 if (l->delay_random)
233 delay += rand() * (1.0 / RAND_MAX) * l->delay_random;
234 if (delay > 0)
235 v->delay = (int)(delay * m->module.srate);
236 else
237 v->delay = 0;
238 v->gen.loop_overlap = l->loop_overlap;
239 v->gen.loop_overlap_step = l->loop_overlap > 0 ? 1.0 / l->loop_overlap : 0;
240 v->gain_fromvel = 1.0 + (l->eff_velcurve[vel] - 1.0) * l->amp_veltrack * 0.01;
241 v->gain_shift = 0.0;
242 v->note = note;
243 v->vel = vel;
244 v->pitch_shift = 0;
245 v->released = 0;
246 v->released_with_sustain = 0;
247 v->released_with_sostenuto = 0;
248 v->captured_sostenuto = 0;
249 v->channel = c;
250 v->layer = l;
251 v->program = c->program;
252 v->amp_env.shape = &l->amp_env_shape;
253 v->filter_env.shape = &l->filter_env_shape;
254 v->pitch_env.shape = &l->pitch_env_shape;
256 v->cutoff_shift = vel * l->fil_veltrack / 127.0 + (note - l->fil_keycenter) * l->fil_keytrack;
257 v->loop_mode = l->loop_mode;
258 v->off_by = l->off_by;
259 int auxes = (m->module.outputs - m->module.aux_offset) / 2;
260 if (l->effect1bus >= 1 && l->effect1bus < 1 + auxes)
261 v->send1bus = l->effect1bus;
262 else
263 v->send1bus = 0;
264 if (l->effect2bus >= 1 && l->effect2bus < 1 + auxes)
265 v->send2bus = l->effect2bus;
266 else
267 v->send2bus = 0;
268 v->send1gain = l->effect1 * 0.01;
269 v->send2gain = l->effect2 * 0.01;
270 if (l->group >= 1 && *pexgroupcount < MAX_RELEASED_GROUPS)
272 gboolean found = FALSE;
273 for (int j = 0; j < *pexgroupcount; j++)
275 if (exgroups[j] == l->group)
277 found = TRUE;
278 break;
281 if (!found)
283 exgroups[(*pexgroupcount)++] = l->group;
286 lfo_init(&v->amp_lfo, &l->amp_lfo, m->module.srate, m->module.srate_inv);
287 lfo_init(&v->filter_lfo, &l->filter_lfo, m->module.srate, m->module.srate_inv);
288 lfo_init(&v->pitch_lfo, &l->pitch_lfo, m->module.srate, m->module.srate_inv);
290 cbox_biquadf_reset(&v->filter_left);
291 cbox_biquadf_reset(&v->filter_right);
292 cbox_biquadf_reset(&v->filter_left2);
293 cbox_biquadf_reset(&v->filter_right2);
294 cbox_onepolef_reset(&v->onepole_left);
295 cbox_onepolef_reset(&v->onepole_right);
296 // set gain later (it's a less expensive operation)
297 if (l->tonectl_freq != 0)
298 cbox_onepolef_set_highshelf_tonectl(&v->onepole_coeffs, l->tonectl_freq * M_PI * m->module.srate_inv, 1.0);
300 GSList *nif = v->layer->nifs;
301 while(nif)
303 struct sampler_noteinitfunc *p = nif->data;
304 p->notefunc(p, v);
305 nif = nif->next;
308 cbox_envelope_reset(&v->amp_env);
309 cbox_envelope_reset(&v->filter_env);
310 cbox_envelope_reset(&v->pitch_env);
312 v->last_eq_bitmask = 0;
314 sampler_voice_activate(v, l->eff_waveform->info.channels == 2 ? spt_stereo16 : spt_mono16);
316 if (v->current_pipe && v->gen.bigpos)
317 cbox_prefetch_pipe_consumed(v->current_pipe, v->gen.bigpos >> 32);
318 v->layer_changed = TRUE;
321 void sampler_voice_link(struct sampler_voice **pv, struct sampler_voice *v)
323 v->prev = NULL;
324 v->next = *pv;
325 if (*pv)
326 (*pv)->prev = v;
327 *pv = v;
330 void sampler_voice_unlink(struct sampler_voice **pv, struct sampler_voice *v)
332 if (*pv == v)
333 *pv = v->next;
334 if (v->prev)
335 v->prev->next = v->next;
336 if (v->next)
337 v->next->prev = v->prev;
338 v->prev = NULL;
339 v->next = NULL;
342 void sampler_voice_inactivate(struct sampler_voice *v, gboolean expect_active)
344 assert((v->gen.mode != spt_inactive) == expect_active);
345 sampler_voice_unlink(&v->channel->voices_running, v);
346 v->gen.mode = spt_inactive;
347 if (v->current_pipe)
349 cbox_prefetch_stack_push(v->program->module->pipe_stack, v->current_pipe);
350 v->current_pipe = NULL;
352 v->channel = NULL;
353 sampler_voice_link(&v->program->module->voices_free, v);
356 void sampler_voice_release(struct sampler_voice *v, gboolean is_polyaft)
358 if ((v->loop_mode == slm_one_shot_chokeable) != is_polyaft)
359 return;
360 if (v->delay >= v->age + CBOX_BLOCK_SIZE)
362 v->released = 1;
363 sampler_voice_inactivate(v, TRUE);
365 else
367 if (v->loop_mode != slm_one_shot && !v->layer->count)
369 v->released = 1;
370 if (v->loop_mode == slm_loop_sustain && v->current_pipe)
372 // Break the loop
373 v->current_pipe->file_loop_end = v->gen.cur_sample_end;
374 v->current_pipe->file_loop_start = -1;
380 void sampler_voice_process(struct sampler_voice *v, struct sampler_module *m, cbox_sample_t **outputs)
382 struct sampler_layer_data *l = v->layer;
383 assert(v->gen.mode != spt_inactive);
385 // if it's a DAHD envelope without sustain, consider the note finished
386 if (__builtin_expect(v->amp_env.cur_stage == 4 && v->amp_env.shape->stages[3].end_value <= 0.f, 0))
387 cbox_envelope_go_to(&v->amp_env, 15);
389 struct sampler_channel *c = v->channel;
390 v->age += CBOX_BLOCK_SIZE;
392 if (__builtin_expect(v->age < v->delay, 0))
393 return;
395 // XXXKF I'm sacrificing sample accuracy for delays for now
396 v->delay = 0;
397 const float velscl = v->vel * (1.f / 127.f);
398 if (__builtin_expect(v->layer_changed, 0))
400 v->last_level = -1;
401 if (v->last_waveform != v->layer->eff_waveform)
403 v->last_waveform = v->layer->eff_waveform;
404 if (v->layer->eff_waveform)
406 v->gen.mode = v->layer->eff_waveform->info.channels == 2 ? spt_stereo16 : spt_mono16;
407 v->gen.cur_sample_end = v->layer->eff_waveform->info.frames;
409 else
411 sampler_voice_inactivate(v, TRUE);
412 return;
415 #define RECALC_EQ_IF(index) \
416 if (l->eq_bitmask & (1 << (index - 1))) \
418 cbox_biquadf_set_peakeq_rbj_scaled(&v->eq_coeffs[index - 1], l->eq##index.effective_freq + velscl * l->eq##index.vel2freq, 1.0 / l->eq##index.bw, dB2gain(0.5 * (l->eq##index.gain + velscl * l->eq##index.vel2gain)), m->module.srate); \
419 if (!(v->last_eq_bitmask & (1 << (index - 1)))) \
421 cbox_biquadf_reset(&v->eq_left[index-1]); \
422 cbox_biquadf_reset(&v->eq_right[index-1]); \
426 RECALC_EQ_IF(1)
427 RECALC_EQ_IF(2)
428 RECALC_EQ_IF(3)
429 v->last_eq_bitmask = l->eq_bitmask;
430 v->layer_changed = FALSE;
433 float pitch = (v->note - l->pitch_keycenter) * l->pitch_keytrack + l->tune + l->transpose * 100 + v->pitch_shift;
434 float modsrcs[smsrc_pernote_count];
435 modsrcs[smsrc_vel - smsrc_pernote_offset] = v->vel * velscl;
436 modsrcs[smsrc_pitch - smsrc_pernote_offset] = pitch * (1.f / 100.f);
437 modsrcs[smsrc_polyaft - smsrc_pernote_offset] = 0.f; // XXXKF not supported yet
438 modsrcs[smsrc_pitchenv - smsrc_pernote_offset] = cbox_envelope_get_next(&v->pitch_env, v->released) * 0.01f;
439 modsrcs[smsrc_filenv - smsrc_pernote_offset] = cbox_envelope_get_next(&v->filter_env, v->released) * 0.01f;
440 modsrcs[smsrc_ampenv - smsrc_pernote_offset] = cbox_envelope_get_next(&v->amp_env, v->released) * 0.01f;
442 modsrcs[smsrc_amplfo - smsrc_pernote_offset] = lfo_run(&v->amp_lfo);
443 modsrcs[smsrc_fillfo - smsrc_pernote_offset] = lfo_run(&v->filter_lfo);
444 modsrcs[smsrc_pitchlfo - smsrc_pernote_offset] = lfo_run(&v->pitch_lfo);
446 if (__builtin_expect(v->amp_env.cur_stage < 0, 0))
448 if (__builtin_expect(is_tail_finished(v), 0))
450 sampler_voice_inactivate(v, TRUE);
451 return;
455 float moddests[smdestcount];
456 moddests[smdest_gain] = 0;
457 moddests[smdest_pitch] = pitch;
458 moddests[smdest_cutoff] = v->cutoff_shift;
459 moddests[smdest_resonance] = 0;
460 moddests[smdest_tonectl] = 0;
461 GSList *mod = l->modulations;
462 if (__builtin_expect(l->trigger == stm_release, 0))
463 moddests[smdest_gain] -= v->age * l->rt_decay * m->module.srate_inv;
465 if (c->pitchwheel)
466 moddests[smdest_pitch] += c->pitchwheel * (c->pitchwheel > 0 ? l->bend_up : l->bend_down) >> 13;
468 static const int modoffset[4] = {0, -1, -1, 1 };
469 static const int modscale[4] = {1, 1, 2, -2 };
470 while(mod)
472 struct sampler_modulation *sm = mod->data;
473 float value = 0.f, value2 = 1.f;
474 if (sm->src < smsrc_pernote_offset)
475 value = c->cc[sm->src] * (1.f / 127.f);
476 else
477 value = modsrcs[sm->src - smsrc_pernote_offset];
478 value = modoffset[sm->flags & 3] + value * modscale[sm->flags & 3];
480 if (sm->src2 != smsrc_none)
482 if (sm->src2 < smsrc_pernote_offset)
483 value2 = c->cc[sm->src2] * (1.f / 127.f);
484 else
485 value2 = modsrcs[sm->src2 - smsrc_pernote_offset];
487 value2 = modoffset[(sm->flags & 12) >> 2] + value2 * modscale[(sm->flags & 12) >> 2];
488 value *= value2;
490 moddests[sm->dest] += value * sm->amount;
492 mod = g_slist_next(mod);
495 double maxv = 127 << 7;
496 double freq = l->eff_freq * cent2factor(moddests[smdest_pitch]) ;
497 uint64_t freq64 = (uint64_t)(freq * 65536.0 * 65536.0 * m->module.srate_inv);
499 gboolean playing_sustain_loop = !v->released && v->loop_mode == slm_loop_sustain;
500 uint32_t loop_start, loop_end;
501 gboolean bandlimited = FALSE;
503 if (!v->current_pipe)
505 v->gen.sample_data = v->last_waveform->data;
506 if (v->last_waveform->levels)
508 gboolean use_cached = v->last_level > 0 && v->last_level < v->last_waveform->level_count
509 && freq64 > v->last_level_min_rate && freq64 <= v->last_waveform->levels[v->last_level].max_rate;
510 if (__builtin_expect(use_cached, 1))
512 v->gen.sample_data = v->last_waveform->levels[v->last_level].data;
513 bandlimited = TRUE;
515 else
517 for (int i = 0; i < v->last_waveform->level_count; i++)
519 if (freq64 <= v->last_waveform->levels[i].max_rate)
521 v->last_level = i;
522 v->gen.sample_data = v->last_waveform->levels[i].data;
523 bandlimited = TRUE;
525 break;
527 v->last_level_min_rate = v->last_waveform->levels[i].max_rate;
533 gboolean play_loop = v->layer->loop_end && (v->loop_mode == slm_loop_continuous || playing_sustain_loop) && v->layer->on_cc_number == -1;
534 loop_start = play_loop ? v->layer->loop_start : (v->layer->count ? 0 : (uint32_t)-1);
535 loop_end = play_loop ? v->layer->loop_end : v->gen.cur_sample_end;
537 if (v->current_pipe)
539 v->gen.sample_data = v->gen.loop_count ? v->current_pipe->data : v->last_waveform->data;
540 v->gen.streaming_buffer = v->current_pipe->data;
542 v->gen.prefetch_only_loop = (loop_end < v->last_waveform->preloaded_frames);
543 v->gen.loop_overlap = 0;
544 if (v->gen.prefetch_only_loop)
546 assert(!v->gen.in_streaming_buffer); // XXXKF this won't hold true when loops are edited while sound is being played (but that's not supported yet anyway)
547 v->gen.loop_start = loop_start;
548 v->gen.loop_end = loop_end;
549 v->gen.streaming_buffer_frames = 0;
551 else
553 v->gen.loop_start = 0;
554 v->gen.loop_end = v->last_waveform->preloaded_frames;
555 v->gen.streaming_buffer_frames = v->current_pipe->buffer_loop_end;
558 else
560 v->gen.loop_count = v->layer->count;
561 v->gen.loop_start = loop_start;
562 v->gen.loop_end = loop_end;
564 if (!bandlimited)
566 // Use pre-calculated join
567 v->gen.scratch = loop_start == (uint32_t)-1 ? v->layer->scratch_end : v->layer->scratch_loop;
569 else
571 // The standard waveforms have extra MAX_INTERPOLATION_ORDER of samples from the loop start added past loop_end,
572 // to avoid wasting time generating the joins in all the practical cases. The slow path covers custom loops
573 // (i.e. partial loop or no loop) over bandlimited versions of the standard waveforms, and those are probably
574 // not very useful anyway, as changing the loop removes the guarantee of the waveform being bandlimited and
575 // may cause looping artifacts or introduce DC offset (e.g. if only a positive part of a sine wave is looped).
576 if (loop_start == 0 && loop_end == l->eff_waveform->info.frames)
577 v->gen.scratch = v->gen.sample_data + l->eff_waveform->info.frames - MAX_INTERPOLATION_ORDER;
578 else
580 // Generate the join for the current wave level
581 // XXXKF this could be optimised further, by checking if waveform and loops are the same as the last
582 // time. However, this code is not likely to be used... ever, so optimising it is not the priority.
583 int shift = l->eff_waveform->info.channels == 2 ? 1 : 0;
584 uint32_t halfscratch = MAX_INTERPOLATION_ORDER << shift;
586 v->gen.scratch = v->gen.scratch_bandlimited;
587 memcpy(&v->gen.scratch_bandlimited[0], &v->gen.sample_data[(loop_end - MAX_INTERPOLATION_ORDER) << shift], halfscratch * sizeof(int16_t) );
588 if (loop_start != (uint32_t)-1)
589 memcpy(v->gen.scratch_bandlimited + halfscratch, &v->gen.sample_data[loop_start << shift], halfscratch * sizeof(int16_t));
590 else
591 memset(v->gen.scratch_bandlimited + halfscratch, 0, halfscratch * sizeof(int16_t));
597 v->gen.bigdelta = freq64;
598 float gain = modsrcs[smsrc_ampenv - smsrc_pernote_offset] * l->volume_linearized * v->gain_fromvel * c->channel_volume_cc * sampler_channel_addcc(c, 11) / (maxv * maxv);
599 if (moddests[smdest_gain] != 0.f)
600 gain *= dB2gain(moddests[smdest_gain]);
601 // http://drealm.info/sfz/plj-sfz.xhtml#amp "The overall gain must remain in the range -144 to 6 decibels."
602 if (gain > 2.f)
603 gain = 2.f;
604 float pan = (l->pan + 100.f) * (1.f / 200.f) + (c->channel_pan_cc * 1.f / maxv - 0.5f) * 2.f;
605 if (pan < 0.f)
606 pan = 0.f;
607 if (pan > 1.f)
608 pan = 1.f;
609 v->gen.lgain = gain * (1.f - pan) / 32768.f;
610 v->gen.rgain = gain * pan / 32768.f;
611 struct cbox_biquadf_coeffs *second_filter = &v->filter_coeffs;
612 gboolean is4p = sampler_layer_data_is_4pole(v->layer);
613 if (l->cutoff != -1.f)
615 float logcutoff = l->logcutoff + moddests[smdest_cutoff];
616 if (logcutoff < 0)
617 logcutoff = 0;
618 if (logcutoff > 12798)
619 logcutoff = 12798;
620 //float resonance = v->resonance*pow(32.0,c->cc[71]/maxv);
621 float resonance = l->resonance_linearized * dB2gain((is4p ? 0.5 : 1) * moddests[smdest_resonance]);
622 if (resonance < 0.7f)
623 resonance = 0.7f;
624 if (resonance > 32.f)
625 resonance = 32.f;
626 switch(l->fil_type)
628 case sft_lp24hybrid:
629 cbox_biquadf_set_lp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance * resonance);
630 cbox_biquadf_set_1plp_lookup(&v->filter_coeffs_extra, &m->sincos[(int)logcutoff], 1);
631 second_filter = &v->filter_coeffs_extra;
632 break;
634 case sft_lp12:
635 case sft_lp24:
636 cbox_biquadf_set_lp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance);
637 break;
638 case sft_hp12:
639 case sft_hp24:
640 cbox_biquadf_set_hp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance);
641 break;
642 case sft_bp6:
643 case sft_bp12:
644 cbox_biquadf_set_bp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance);
645 break;
646 case sft_lp6:
647 case sft_lp12nr:
648 case sft_lp24nr:
649 cbox_biquadf_set_1plp_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], l->fil_type != sft_lp6);
650 break;
651 case sft_hp6:
652 case sft_hp12nr:
653 case sft_hp24nr:
654 cbox_biquadf_set_1php_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], l->fil_type != sft_hp6);
655 break;
656 default:
657 assert(0);
660 if (__builtin_expect(l->tonectl_freq != 0, 0))
662 float ctl = l->tonectl + moddests[smdest_tonectl];
663 if (fabs(ctl) > 0.0001f)
664 cbox_onepolef_set_highshelf_setgain(&v->onepole_coeffs, dB2gain(ctl));
665 else
666 cbox_onepolef_set_highshelf_setgain(&v->onepole_coeffs, 1.0);
669 float leftright[2 * CBOX_BLOCK_SIZE];
671 uint32_t samples = 0;
674 if (v->current_pipe)
676 uint32_t limit = cbox_prefetch_pipe_get_remaining(v->current_pipe) - 4;
677 if (!limit)
678 v->gen.mode = spt_inactive;
679 else
681 samples = sampler_gen_sample_playback(&v->gen, leftright, limit);
682 cbox_prefetch_pipe_consumed(v->current_pipe, v->gen.consumed);
683 v->gen.consumed = 0;
686 else
688 samples = sampler_gen_sample_playback(&v->gen, leftright, (uint32_t)-1);
691 for (int i = 2 * samples; i < 2 * CBOX_BLOCK_SIZE; i++)
692 leftright[i] = 0.f;
693 if (l->cutoff != -1)
695 cbox_biquadf_process_stereo(&v->filter_left, &v->filter_right, &v->filter_coeffs, leftright);
696 if (is4p)
697 cbox_biquadf_process_stereo(&v->filter_left, &v->filter_right, second_filter, leftright);
699 if (__builtin_expect(l->tonectl_freq != 0, 0))
701 cbox_onepolef_process_stereo(&v->onepole_left, &v->onepole_right, &v->onepole_coeffs, leftright);
703 if (__builtin_expect(l->eq_bitmask, 0))
705 for (int eq = 0; eq < 3; eq++)
707 if (l->eq_bitmask & (1 << eq))
709 cbox_biquadf_process_stereo(&v->eq_left[eq], &v->eq_right[eq], &v->eq_coeffs[eq], leftright);
714 mix_block_into(outputs, v->output_pair_no * 2, leftright);
715 if (__builtin_expect((v->send1bus > 0 && v->send1gain != 0) || (v->send2bus > 0 && v->send2gain != 0), 0))
717 if (v->send1bus > 0 && v->send1gain != 0)
719 int oofs = m->module.aux_offset + (v->send1bus - 1) * 2;
720 mix_block_into_with_gain(outputs, oofs, leftright, v->send1gain);
722 if (v->send2bus > 0 && v->send2gain != 0)
724 int oofs = m->module.aux_offset + (v->send2bus - 1) * 2;
725 mix_block_into_with_gain(outputs, oofs, leftright, v->send2gain);
728 if (v->gen.mode == spt_inactive)
729 sampler_voice_inactivate(v, FALSE);