Initial implementation of tone control (= high shelf filter) in the sampler.
[calfbox.git] / sampler_voice.c
blob8df20baebb411d22fc4391c4f7ee24cfcd55941d
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_left, float *src_right, 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 += 4)
103 float32x2_t l1 = vld1_f32(&src_left[i]);
104 float32x2_t l2 = vld1_f32(&src_left[i + 2]);
105 float32x2_t r1 = vld1_f32(&src_right[i]);
106 float32x2_t r2 = vld1_f32(&src_right[i + 2]);
107 float32x2_t dl1 = vld1_f32(&dst_left[i]);
108 float32x2_t dl2 = vld1_f32(&dst_left[i + 2]);
109 float32x2_t dr1 = vld1_f32(&dst_right[i]);
110 float32x2_t dr2 = vld1_f32(&dst_right[i + 2]);
112 l1 = vmla_f32(dl1, l1, gain2);
113 l2 = vmla_f32(dl2, l2, gain2);
114 vst1_f32(&dst_left[i], l1);
115 vst1_f32(&dst_left[i + 2], l2);
116 r1 = vmla_f32(dr1, r1, gain2);
117 r2 = vmla_f32(dr2, r2, gain2);
118 vst1_f32(&dst_right[i], r1);
119 vst1_f32(&dst_right[i + 2], r2);
123 static inline void mix_block_into(cbox_sample_t **outputs, int oofs, float *src_left, float *src_right)
125 float *dst_left = outputs[oofs];
126 float *dst_right = outputs[oofs + 1];
127 for (size_t i = 0; i < CBOX_BLOCK_SIZE; i += 4)
129 float32x2_t l1 = vld1_f32(&src_left[i]);
130 float32x2_t l2 = vld1_f32(&src_left[i + 2]);
131 float32x2_t r1 = vld1_f32(&src_right[i]);
132 float32x2_t r2 = vld1_f32(&src_right[i + 2]);
133 float32x2_t dl1 = vld1_f32(&dst_left[i]);
134 float32x2_t dl2 = vld1_f32(&dst_left[i + 2]);
135 float32x2_t dr1 = vld1_f32(&dst_right[i]);
136 float32x2_t dr2 = vld1_f32(&dst_right[i + 2]);
138 l1 = vadd_f32(dl1, l1);
139 l2 = vadd_f32(dl2, l2);
140 vst1_f32(&dst_left[i], l1);
141 vst1_f32(&dst_left[i + 2], l2);
142 r1 = vadd_f32(dr1, r1);
143 r2 = vadd_f32(dr2, r2);
144 vst1_f32(&dst_right[i], r1);
145 vst1_f32(&dst_right[i + 2], r2);
149 #else
151 static inline void mix_block_into_with_gain(cbox_sample_t **outputs, int oofs, float *src_left, float *src_right, float gain)
153 cbox_sample_t *dst_left = outputs[oofs];
154 cbox_sample_t *dst_right = outputs[oofs + 1];
155 for (size_t i = 0; i < CBOX_BLOCK_SIZE; i++)
157 dst_left[i] += gain * src_left[i];
158 dst_right[i] += gain * src_right[i];
162 static inline void mix_block_into(cbox_sample_t **outputs, int oofs, float *src_left, float *src_right)
164 cbox_sample_t *dst_left = outputs[oofs];
165 cbox_sample_t *dst_right = outputs[oofs + 1];
166 for (size_t i = 0; i < CBOX_BLOCK_SIZE; i++)
168 dst_left[i] += src_left[i];
169 dst_right[i] += src_right[i];
173 #endif
175 ////////////////////////////////////////////////////////////////////////////////
177 void sampler_voice_activate(struct sampler_voice *v, enum sampler_player_type mode)
179 assert(v->gen.mode == spt_inactive);
180 sampler_voice_unlink(&v->program->module->voices_free, v);
181 assert(mode != spt_inactive);
182 assert(v->channel);
183 v->gen.mode = mode;
184 sampler_voice_link(&v->channel->voices_running, v);
187 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)
189 struct sampler_module *m = c->module;
190 sampler_gen_reset(&v->gen);
192 v->age = 0;
193 if (l->trigger == stm_release)
195 // time since last 'note on' for that note
196 v->age = m->current_time - c->prev_note_start_time[note];
197 double age = v->age * m->module.srate_inv;
198 // if attenuation is more than 84dB, ignore the release trigger
199 if (age * l->rt_decay > 84)
200 return;
202 uint32_t end = l->eff_waveform->info.frames;
203 if (l->end != 0)
204 end = (l->end == -1) ? 0 : l->end;
205 v->last_waveform = l->eff_waveform;
206 v->gen.cur_sample_end = end;
207 if (end > l->eff_waveform->info.frames)
208 end = l->eff_waveform->info.frames;
210 assert(!v->current_pipe);
211 if (end > l->eff_waveform->preloaded_frames)
213 if (l->loop_mode == slm_loop_continuous && l->loop_end < l->eff_waveform->preloaded_frames)
215 // Everything fits in prefetch, because loop ends in prefetch and post-loop part is not being played
217 else
219 uint32_t loop_start = -1, loop_end = end;
220 // If in loop mode, set the loop over the looped part... unless we're doing sustain-only loop on prefetch area only. Then
221 // streaming will only cover the release part, and it shouldn't be looped.
222 if (l->loop_mode == slm_loop_continuous || (l->loop_mode == slm_loop_sustain && l->loop_end >= l->eff_waveform->preloaded_frames))
224 loop_start = l->loop_start;
225 loop_end = l->loop_end;
227 // Those are initial values only, they will be adjusted in process function
228 v->current_pipe = cbox_prefetch_stack_pop(m->pipe_stack, l->eff_waveform, loop_start, loop_end, l->count);
229 if (!v->current_pipe)
230 g_warning("Prefetch pipe pool exhausted, no streaming playback will be possible");
234 v->output_pair_no = l->output % m->output_pairs;
235 v->serial_no = m->serial_no;
237 uint32_t pos = l->offset;
238 pos = l->offset;
239 if (l->offset_random)
240 pos += ((uint32_t)(rand() + (rand() << 16))) % l->offset_random;
241 if (pos >= end)
242 pos = end;
243 v->gen.bigpos = ((uint64_t)pos) << 32;
245 float delay = l->delay;
246 if (l->delay_random)
247 delay += rand() * (1.0 / RAND_MAX) * l->delay_random;
248 if (delay > 0)
249 v->delay = (int)(delay * m->module.srate);
250 else
251 v->delay = 0;
252 v->gen.loop_overlap = l->loop_overlap;
253 v->gen.loop_overlap_step = l->loop_overlap > 0 ? 1.0 / l->loop_overlap : 0;
254 v->gain_fromvel = 1.0 + (l->eff_velcurve[vel] - 1.0) * l->amp_veltrack * 0.01;
255 v->gain_shift = 0.0;
256 v->note = note;
257 v->vel = vel;
258 v->pitch_shift = 0;
259 v->released = 0;
260 v->released_with_sustain = 0;
261 v->released_with_sostenuto = 0;
262 v->captured_sostenuto = 0;
263 v->channel = c;
264 v->layer = l;
265 v->program = c->program;
266 v->amp_env.shape = &l->amp_env_shape;
267 v->filter_env.shape = &l->filter_env_shape;
268 v->pitch_env.shape = &l->pitch_env_shape;
270 v->cutoff_shift = vel * l->fil_veltrack / 127.0 + (note - l->fil_keycenter) * l->fil_keytrack;
271 v->loop_mode = l->loop_mode;
272 v->off_by = l->off_by;
273 int auxes = (m->module.outputs - m->module.aux_offset) / 2;
274 if (l->effect1bus >= 1 && l->effect1bus < 1 + auxes)
275 v->send1bus = l->effect1bus;
276 else
277 v->send1bus = 0;
278 if (l->effect2bus >= 1 && l->effect2bus < 1 + auxes)
279 v->send2bus = l->effect2bus;
280 else
281 v->send2bus = 0;
282 v->send1gain = l->effect1 * 0.01;
283 v->send2gain = l->effect2 * 0.01;
284 if (l->group >= 1 && *pexgroupcount < MAX_RELEASED_GROUPS)
286 gboolean found = FALSE;
287 for (int j = 0; j < *pexgroupcount; j++)
289 if (exgroups[j] == l->group)
291 found = TRUE;
292 break;
295 if (!found)
297 exgroups[(*pexgroupcount)++] = l->group;
300 lfo_init(&v->amp_lfo, &l->amp_lfo, m->module.srate, m->module.srate_inv);
301 lfo_init(&v->filter_lfo, &l->filter_lfo, m->module.srate, m->module.srate_inv);
302 lfo_init(&v->pitch_lfo, &l->pitch_lfo, m->module.srate, m->module.srate_inv);
304 cbox_biquadf_reset(&v->filter_left);
305 cbox_biquadf_reset(&v->filter_right);
306 cbox_biquadf_reset(&v->filter_left2);
307 cbox_biquadf_reset(&v->filter_right2);
308 cbox_onepolef_reset(&v->onepole_left);
309 cbox_onepolef_reset(&v->onepole_right);
311 GSList *nif = v->layer->nifs;
312 while(nif)
314 struct sampler_noteinitfunc *p = nif->data;
315 p->notefunc(p, v);
316 nif = nif->next;
319 cbox_envelope_reset(&v->amp_env);
320 cbox_envelope_reset(&v->filter_env);
321 cbox_envelope_reset(&v->pitch_env);
323 v->last_eq_bitmask = 0;
325 sampler_voice_activate(v, l->eff_waveform->info.channels == 2 ? spt_stereo16 : spt_mono16);
327 if (v->current_pipe && v->gen.bigpos)
328 cbox_prefetch_pipe_consumed(v->current_pipe, v->gen.bigpos >> 32);
329 v->layer_changed = TRUE;
332 void sampler_voice_link(struct sampler_voice **pv, struct sampler_voice *v)
334 v->prev = NULL;
335 v->next = *pv;
336 if (*pv)
337 (*pv)->prev = v;
338 *pv = v;
341 void sampler_voice_unlink(struct sampler_voice **pv, struct sampler_voice *v)
343 if (*pv == v)
344 *pv = v->next;
345 if (v->prev)
346 v->prev->next = v->next;
347 if (v->next)
348 v->next->prev = v->prev;
349 v->prev = NULL;
350 v->next = NULL;
353 void sampler_voice_inactivate(struct sampler_voice *v, gboolean expect_active)
355 assert((v->gen.mode != spt_inactive) == expect_active);
356 sampler_voice_unlink(&v->channel->voices_running, v);
357 v->gen.mode = spt_inactive;
358 if (v->current_pipe)
360 cbox_prefetch_stack_push(v->program->module->pipe_stack, v->current_pipe);
361 v->current_pipe = NULL;
363 v->channel = NULL;
364 sampler_voice_link(&v->program->module->voices_free, v);
367 void sampler_voice_release(struct sampler_voice *v, gboolean is_polyaft)
369 if ((v->loop_mode == slm_one_shot_chokeable) != is_polyaft)
370 return;
371 if (v->delay >= v->age + CBOX_BLOCK_SIZE)
373 v->released = 1;
374 sampler_voice_inactivate(v, TRUE);
376 else
378 if (v->loop_mode != slm_one_shot && !v->layer->count)
380 v->released = 1;
381 if (v->loop_mode == slm_loop_sustain && v->current_pipe)
383 // Break the loop
384 v->current_pipe->file_loop_end = v->gen.cur_sample_end;
385 v->current_pipe->file_loop_start = -1;
391 void sampler_voice_process(struct sampler_voice *v, struct sampler_module *m, cbox_sample_t **outputs)
393 struct sampler_layer_data *l = v->layer;
394 assert(v->gen.mode != spt_inactive);
396 // if it's a DAHD envelope without sustain, consider the note finished
397 if (__builtin_expect(v->amp_env.cur_stage == 4 && v->amp_env.shape->stages[3].end_value <= 0.f, 0))
398 cbox_envelope_go_to(&v->amp_env, 15);
400 struct sampler_channel *c = v->channel;
401 v->age += CBOX_BLOCK_SIZE;
403 if (__builtin_expect(v->age < v->delay, 0))
404 return;
406 // XXXKF I'm sacrificing sample accuracy for delays for now
407 v->delay = 0;
408 const float velscl = v->vel * (1.f / 127.f);
409 if (__builtin_expect(v->layer_changed, 0))
411 v->last_level = -1;
412 if (v->last_waveform != v->layer->eff_waveform)
414 v->last_waveform = v->layer->eff_waveform;
415 if (v->layer->eff_waveform)
417 v->gen.mode = v->layer->eff_waveform->info.channels == 2 ? spt_stereo16 : spt_mono16;
418 v->gen.cur_sample_end = v->layer->eff_waveform->info.frames;
420 else
422 sampler_voice_inactivate(v, TRUE);
423 return;
426 #define RECALC_EQ_IF(index) \
427 if (l->eq_bitmask & (1 << (index - 1))) \
429 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); \
430 if (!(v->last_eq_bitmask & (1 << (index - 1)))) \
432 cbox_biquadf_reset(&v->eq_left[index-1]); \
433 cbox_biquadf_reset(&v->eq_right[index-1]); \
437 RECALC_EQ_IF(1)
438 RECALC_EQ_IF(2)
439 RECALC_EQ_IF(3)
440 v->last_eq_bitmask = l->eq_bitmask;
441 v->layer_changed = FALSE;
444 float pitch = (v->note - l->pitch_keycenter) * l->pitch_keytrack + l->tune + l->transpose * 100 + v->pitch_shift;
445 float modsrcs[smsrc_pernote_count];
446 modsrcs[smsrc_vel - smsrc_pernote_offset] = v->vel * velscl;
447 modsrcs[smsrc_pitch - smsrc_pernote_offset] = pitch * (1.f / 100.f);
448 modsrcs[smsrc_polyaft - smsrc_pernote_offset] = 0.f; // XXXKF not supported yet
449 modsrcs[smsrc_pitchenv - smsrc_pernote_offset] = cbox_envelope_get_next(&v->pitch_env, v->released) * 0.01f;
450 modsrcs[smsrc_filenv - smsrc_pernote_offset] = cbox_envelope_get_next(&v->filter_env, v->released) * 0.01f;
451 modsrcs[smsrc_ampenv - smsrc_pernote_offset] = cbox_envelope_get_next(&v->amp_env, v->released) * 0.01f;
453 modsrcs[smsrc_amplfo - smsrc_pernote_offset] = lfo_run(&v->amp_lfo);
454 modsrcs[smsrc_fillfo - smsrc_pernote_offset] = lfo_run(&v->filter_lfo);
455 modsrcs[smsrc_pitchlfo - smsrc_pernote_offset] = lfo_run(&v->pitch_lfo);
457 if (__builtin_expect(v->amp_env.cur_stage < 0, 0))
459 if (is_tail_finished(v))
461 sampler_voice_inactivate(v, TRUE);
462 return;
466 float moddests[smdestcount];
467 moddests[smdest_gain] = 0;
468 moddests[smdest_pitch] = pitch;
469 moddests[smdest_cutoff] = v->cutoff_shift;
470 moddests[smdest_resonance] = 0;
471 moddests[smdest_tonectl] = 0;
472 GSList *mod = l->modulations;
473 if (__builtin_expect(l->trigger == stm_release, 0))
474 moddests[smdest_gain] -= v->age * l->rt_decay * m->module.srate_inv;
476 if (c->pitchwheel)
477 moddests[smdest_pitch] += c->pitchwheel * (c->pitchwheel > 0 ? l->bend_up : l->bend_down) >> 13;
479 static const int modoffset[4] = {0, -1, -1, 1 };
480 static const int modscale[4] = {1, 1, 2, -2 };
481 while(mod)
483 struct sampler_modulation *sm = mod->data;
484 float value = 0.f, value2 = 1.f;
485 if (sm->src < smsrc_pernote_offset)
486 value = c->cc[sm->src] * (1.f / 127.f);
487 else
488 value = modsrcs[sm->src - smsrc_pernote_offset];
489 value = modoffset[sm->flags & 3] + value * modscale[sm->flags & 3];
491 if (sm->src2 != smsrc_none)
493 if (sm->src2 < smsrc_pernote_offset)
494 value2 = c->cc[sm->src2] * (1.f / 127.f);
495 else
496 value2 = modsrcs[sm->src2 - smsrc_pernote_offset];
498 value2 = modoffset[(sm->flags & 12) >> 2] + value2 * modscale[(sm->flags & 12) >> 2];
499 value *= value2;
501 moddests[sm->dest] += value * sm->amount;
503 mod = g_slist_next(mod);
506 double maxv = 127 << 7;
507 double freq = l->eff_freq * cent2factor(moddests[smdest_pitch]) ;
508 uint64_t freq64 = (uint64_t)(freq * 65536.0 * 65536.0 * m->module.srate_inv);
510 gboolean playing_sustain_loop = !v->released && v->loop_mode == slm_loop_sustain;
511 uint32_t loop_start, loop_end;
512 gboolean bandlimited = FALSE;
514 if (!v->current_pipe)
516 v->gen.sample_data = v->last_waveform->data;
517 if (v->last_waveform->levels)
519 gboolean use_cached = v->last_level > 0 && v->last_level < v->last_waveform->level_count
520 && freq64 > v->last_level_min_rate && freq64 <= v->last_waveform->levels[v->last_level].max_rate;
521 if (__builtin_expect(use_cached, 1))
523 v->gen.sample_data = v->last_waveform->levels[v->last_level].data;
524 bandlimited = TRUE;
526 else
528 for (int i = 0; i < v->last_waveform->level_count; i++)
530 if (freq64 <= v->last_waveform->levels[i].max_rate)
532 v->last_level = i;
533 v->gen.sample_data = v->last_waveform->levels[i].data;
534 bandlimited = TRUE;
536 break;
538 v->last_level_min_rate = v->last_waveform->levels[i].max_rate;
544 gboolean play_loop = v->layer->loop_end && (v->loop_mode == slm_loop_continuous || playing_sustain_loop) && v->layer->on_cc_number == -1;
545 loop_start = play_loop ? v->layer->loop_start : (v->layer->count ? 0 : (uint32_t)-1);
546 loop_end = play_loop ? v->layer->loop_end : v->gen.cur_sample_end;
548 if (v->current_pipe)
550 v->gen.sample_data = v->gen.loop_count ? v->current_pipe->data : v->last_waveform->data;
551 v->gen.streaming_buffer = v->current_pipe->data;
553 v->gen.prefetch_only_loop = (loop_end < v->last_waveform->preloaded_frames);
554 v->gen.loop_overlap = 0;
555 if (v->gen.prefetch_only_loop)
557 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)
558 v->gen.loop_start = loop_start;
559 v->gen.loop_end = loop_end;
560 v->gen.streaming_buffer_frames = 0;
562 else
564 v->gen.loop_start = 0;
565 v->gen.loop_end = v->last_waveform->preloaded_frames;
566 v->gen.streaming_buffer_frames = v->current_pipe->buffer_loop_end;
569 else
571 v->gen.loop_count = v->layer->count;
572 v->gen.loop_start = loop_start;
573 v->gen.loop_end = loop_end;
575 if (!bandlimited)
577 // Use pre-calculated join
578 v->gen.scratch = loop_start == (uint32_t)-1 ? v->layer->scratch_end : v->layer->scratch_loop;
580 else
582 // The standard waveforms have extra MAX_INTERPOLATION_ORDER of samples from the loop start added past loop_end,
583 // to avoid wasting time generating the joins in all the practical cases. The slow path covers custom loops
584 // (i.e. partial loop or no loop) over bandlimited versions of the standard waveforms, and those are probably
585 // not very useful anyway, as changing the loop removes the guarantee of the waveform being bandlimited and
586 // may cause looping artifacts or introduce DC offset (e.g. if only a positive part of a sine wave is looped).
587 if (loop_start == 0 && loop_end == l->eff_waveform->info.frames)
588 v->gen.scratch = v->gen.sample_data + l->eff_waveform->info.frames - MAX_INTERPOLATION_ORDER;
589 else
591 // Generate the join for the current wave level
592 // XXXKF this could be optimised further, by checking if waveform and loops are the same as the last
593 // time. However, this code is not likely to be used... ever, so optimising it is not the priority.
594 int shift = l->eff_waveform->info.channels == 2 ? 1 : 0;
595 uint32_t halfscratch = MAX_INTERPOLATION_ORDER << shift;
597 v->gen.scratch = v->gen.scratch_bandlimited;
598 memcpy(&v->gen.scratch_bandlimited[0], &v->gen.sample_data[(loop_end - MAX_INTERPOLATION_ORDER) << shift], halfscratch * sizeof(int16_t) );
599 if (loop_start != (uint32_t)-1)
600 memcpy(v->gen.scratch_bandlimited + halfscratch, &v->gen.sample_data[loop_start << shift], halfscratch * sizeof(int16_t));
601 else
602 memset(v->gen.scratch_bandlimited + halfscratch, 0, halfscratch * sizeof(int16_t));
608 v->gen.bigdelta = freq64;
609 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);
610 if (moddests[smdest_gain] != 0.f)
611 gain *= dB2gain(moddests[smdest_gain]);
612 // http://drealm.info/sfz/plj-sfz.xhtml#amp "The overall gain must remain in the range -144 to 6 decibels."
613 if (gain > 2.f)
614 gain = 2.f;
615 float pan = (l->pan + 100.f) * (1.f / 200.f) + (c->channel_pan_cc * 1.f / maxv - 0.5f) * 2.f;
616 if (pan < 0.f)
617 pan = 0.f;
618 if (pan > 1.f)
619 pan = 1.f;
620 v->gen.lgain = gain * (1.f - pan) / 32768.f;
621 v->gen.rgain = gain * pan / 32768.f;
622 struct cbox_biquadf_coeffs *second_filter = &v->filter_coeffs;
623 gboolean is4p = sampler_layer_data_is_4pole(v->layer);
624 if (l->cutoff != -1.f)
626 float logcutoff = l->logcutoff + moddests[smdest_cutoff];
627 if (logcutoff < 0)
628 logcutoff = 0;
629 if (logcutoff > 12798)
630 logcutoff = 12798;
631 //float resonance = v->resonance*pow(32.0,c->cc[71]/maxv);
632 float resonance = l->resonance_linearized * dB2gain((is4p ? 0.5 : 1) * moddests[smdest_resonance]);
633 if (resonance < 0.7f)
634 resonance = 0.7f;
635 if (resonance > 32.f)
636 resonance = 32.f;
637 switch(l->fil_type)
639 case sft_lp24hybrid:
640 cbox_biquadf_set_lp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance * resonance);
641 cbox_biquadf_set_1plp_lookup(&v->filter_coeffs_extra, &m->sincos[(int)logcutoff], 1);
642 second_filter = &v->filter_coeffs_extra;
643 break;
645 case sft_lp12:
646 case sft_lp24:
647 cbox_biquadf_set_lp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance);
648 break;
649 case sft_hp12:
650 case sft_hp24:
651 cbox_biquadf_set_hp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance);
652 break;
653 case sft_bp6:
654 case sft_bp12:
655 cbox_biquadf_set_bp_rbj_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], resonance);
656 break;
657 case sft_lp6:
658 case sft_lp12nr:
659 case sft_lp24nr:
660 cbox_biquadf_set_1plp_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], l->fil_type != sft_lp6);
661 break;
662 case sft_hp6:
663 case sft_hp12nr:
664 case sft_hp24nr:
665 cbox_biquadf_set_1php_lookup(&v->filter_coeffs, &m->sincos[(int)logcutoff], l->fil_type != sft_hp6);
666 break;
667 default:
668 assert(0);
671 if (l->tonectl_freq != 0)
673 float ctl = l->tonectl + moddests[smdest_tonectl];
674 if (fabs(ctl) > 0.0001f)
675 cbox_onepolef_set_highshelf_tonectl(&v->onepole_coeffs, l->tonectl_freq * M_PI * m->module.srate_inv, dB2gain(l->tonectl + moddests[smdest_tonectl]));
676 else
677 cbox_onepolef_set_highshelf_tonectl(&v->onepole_coeffs, l->tonectl_freq * M_PI * m->module.srate_inv, 1.0);
680 float left[CBOX_BLOCK_SIZE], right[CBOX_BLOCK_SIZE];
682 uint32_t samples = 0;
685 if (v->current_pipe)
687 uint32_t limit = cbox_prefetch_pipe_get_remaining(v->current_pipe) - 4;
688 if (!limit)
689 v->gen.mode = spt_inactive;
690 else
692 samples = sampler_gen_sample_playback(&v->gen, left, right, limit);
693 cbox_prefetch_pipe_consumed(v->current_pipe, v->gen.consumed);
694 v->gen.consumed = 0;
697 else
699 samples = sampler_gen_sample_playback(&v->gen, left, right, (uint32_t)-1);
702 for (int i = samples; i < CBOX_BLOCK_SIZE; i++)
703 left[i] = right[i] = 0.f;
704 if (l->cutoff != -1)
706 cbox_biquadf_process(&v->filter_left, &v->filter_coeffs, left);
707 if (is4p)
708 cbox_biquadf_process(&v->filter_left2, second_filter, left);
709 cbox_biquadf_process(&v->filter_right, &v->filter_coeffs, right);
710 if (is4p)
711 cbox_biquadf_process(&v->filter_right2, second_filter, right);
713 if (l->tonectl != -1)
715 cbox_onepolef_process(&v->onepole_left, &v->onepole_coeffs, left);
716 cbox_onepolef_process(&v->onepole_right, &v->onepole_coeffs, right);
718 if (__builtin_expect(l->eq_bitmask, 0))
720 for (int eq = 0; eq < 3; eq++)
722 if (l->eq_bitmask & (1 << eq))
724 cbox_biquadf_process(&v->eq_left[eq], &v->eq_coeffs[eq], left);
725 cbox_biquadf_process(&v->eq_right[eq], &v->eq_coeffs[eq], right);
730 mix_block_into(outputs, v->output_pair_no * 2, left, right);
731 if (__builtin_expect((v->send1bus > 0 && v->send1gain != 0) || (v->send2bus > 0 && v->send2gain != 0), 0))
733 if (v->send1bus > 0 && v->send1gain != 0)
735 int oofs = m->module.aux_offset + (v->send1bus - 1) * 2;
736 mix_block_into_with_gain(outputs, oofs, left, right, v->send1gain);
738 if (v->send2bus > 0 && v->send2gain != 0)
740 int oofs = m->module.aux_offset + (v->send2bus - 1) * 2;
741 mix_block_into_with_gain(outputs, oofs, left, right, v->send2gain);
744 if (v->gen.mode == spt_inactive)
745 sampler_voice_inactivate(v, FALSE);