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/>.
20 #include "config-api.h"
27 #include "sampler_impl.h"
28 #include "sfzloader.h"
40 static void lfo_update_freq(struct sampler_lfo
*lfo
, struct sampler_lfo_params
*lfop
, int srate
, double srate_inv
)
42 lfo
->delta
= (uint32_t)(lfop
->freq
* 65536.0 * 65536.0 * CBOX_BLOCK_SIZE
* srate_inv
);
43 lfo
->delay
= (uint32_t)(lfop
->delay
* srate
);
44 lfo
->fade
= (uint32_t)(lfop
->fade
* srate
);
47 static void lfo_init(struct sampler_lfo
*lfo
, struct sampler_lfo_params
*lfop
, int srate
, double srate_inv
)
51 lfo_update_freq(lfo
, lfop
, srate
, srate_inv
);
54 static inline float lfo_run(struct sampler_lfo
*lfo
)
56 if (lfo
->age
< lfo
->delay
)
58 lfo
->age
+= CBOX_BLOCK_SIZE
;
62 const int FRAC_BITS
= 32 - 11;
63 lfo
->phase
+= lfo
->delta
;
64 uint32_t iphase
= lfo
->phase
>> FRAC_BITS
;
65 float frac
= (lfo
->phase
& ((1 << FRAC_BITS
) - 1)) * (1.0 / (1 << FRAC_BITS
));
67 float v
= sampler_sine_wave
[iphase
] + (sampler_sine_wave
[iphase
+ 1] - sampler_sine_wave
[iphase
]) * frac
;
68 if (lfo
->fade
&& lfo
->age
< lfo
->delay
+ lfo
->fade
)
70 v
*= (lfo
->age
- lfo
->delay
) * 1.0 / lfo
->fade
;
71 lfo
->age
+= CBOX_BLOCK_SIZE
;
77 static gboolean
is_tail_finished(struct sampler_voice
*v
)
79 if (v
->layer
->cutoff
== -1)
81 double eps
= 1.0 / 65536.0;
82 if (cbox_biquadf_is_audible(&v
->filter_left
, eps
))
84 if (cbox_biquadf_is_audible(&v
->filter_right
, eps
))
86 if (sampler_layer_data_is_4pole(v
->layer
))
88 if (cbox_biquadf_is_audible(&v
->filter_left2
, eps
))
90 if (cbox_biquadf_is_audible(&v
->filter_right2
, eps
))
101 static inline void mix_block_into_with_gain(cbox_sample_t
**outputs
, int oofs
, float *src_leftright
, float gain
)
103 float *dst_left
= outputs
[oofs
];
104 float *dst_right
= outputs
[oofs
+ 1];
105 float32x2_t gain2
= {gain
, gain
};
106 for (size_t i
= 0; i
< CBOX_BLOCK_SIZE
; i
+= 2)
108 float32x2_t lr1
= vld1_f32(&src_leftright
[2 * i
]);
109 float32x2_t lr2
= vld1_f32(&src_leftright
[2 * i
+ 2]);
110 float32x2x2_t lr12
= vtrn_f32(lr1
, lr2
);
111 float32x2_t dl1
= vld1_f32(&dst_left
[i
]);
112 float32x2_t dr1
= vld1_f32(&dst_right
[i
]);
114 float32x2_t l1
= vmla_f32(dl1
, lr12
.val
[0], gain2
);
115 vst1_f32(&dst_left
[i
], l1
);
116 float32x2_t r1
= vmla_f32(dr1
, lr12
.val
[1], gain2
);
117 vst1_f32(&dst_right
[i
], r1
);
121 static inline void mix_block_into(cbox_sample_t
**outputs
, int oofs
, float *src_leftright
)
123 float *dst_left
= outputs
[oofs
];
124 float *dst_right
= outputs
[oofs
+ 1];
125 for (size_t i
= 0; i
< CBOX_BLOCK_SIZE
; i
+= 2)
127 float32x2_t lr1
= vld1_f32(&src_leftright
[2 * i
]);
128 float32x2_t lr2
= vld1_f32(&src_leftright
[2 * i
+ 2]);
129 float32x2x2_t lr12
= vtrn_f32(lr1
, lr2
);
130 float32x2_t dl1
= vld1_f32(&dst_left
[i
]);
131 float32x2_t dr1
= vld1_f32(&dst_right
[i
]);
133 float32x2_t l1
= vadd_f32(dl1
, lr12
.val
[0]);
134 vst1_f32(&dst_left
[i
], l1
);
135 float32x2_t r1
= vadd_f32(dr1
, lr12
.val
[1]);
136 vst1_f32(&dst_right
[i
], r1
);
142 static inline void mix_block_into_with_gain(cbox_sample_t
**outputs
, int oofs
, float *src_leftright
, float gain
)
144 cbox_sample_t
*dst_left
= outputs
[oofs
];
145 cbox_sample_t
*dst_right
= outputs
[oofs
+ 1];
146 for (size_t i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
148 dst_left
[i
] += gain
* src_leftright
[2 * i
];
149 dst_right
[i
] += gain
* src_leftright
[2 * i
+ 1];
153 static inline void mix_block_into(cbox_sample_t
**outputs
, int oofs
, float *src_leftright
)
155 cbox_sample_t
*dst_left
= outputs
[oofs
];
156 cbox_sample_t
*dst_right
= outputs
[oofs
+ 1];
157 for (size_t i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
159 dst_left
[i
] += src_leftright
[2 * i
];
160 dst_right
[i
] += src_leftright
[2 * i
+ 1];
166 ////////////////////////////////////////////////////////////////////////////////
168 void sampler_voice_activate(struct sampler_voice
*v
, enum sampler_player_type mode
)
170 assert(v
->gen
.mode
== spt_inactive
);
171 sampler_voice_unlink(&v
->program
->module
->voices_free
, v
);
172 assert(mode
!= spt_inactive
);
175 sampler_voice_link(&v
->channel
->voices_running
, v
);
178 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
)
180 struct sampler_module
*m
= c
->module
;
181 sampler_gen_reset(&v
->gen
);
184 if (l
->trigger
== stm_release
)
186 // time since last 'note on' for that note
187 v
->age
= m
->current_time
- c
->prev_note_start_time
[note
];
188 double age
= v
->age
* m
->module
.srate_inv
;
189 // if attenuation is more than 84dB, ignore the release trigger
190 if (age
* l
->rt_decay
> 84)
193 uint32_t end
= l
->eff_waveform
->info
.frames
;
195 end
= (l
->end
== -1) ? 0 : l
->end
;
196 v
->last_waveform
= l
->eff_waveform
;
197 v
->gen
.cur_sample_end
= end
;
198 if (end
> l
->eff_waveform
->info
.frames
)
199 end
= l
->eff_waveform
->info
.frames
;
201 assert(!v
->current_pipe
);
202 if (end
> l
->eff_waveform
->preloaded_frames
)
204 if (l
->eff_loop_mode
== slm_loop_continuous
&& l
->loop_end
< l
->eff_waveform
->preloaded_frames
)
206 // Everything fits in prefetch, because loop ends in prefetch and post-loop part is not being played
210 uint32_t loop_start
= -1, loop_end
= end
;
211 // If in loop mode, set the loop over the looped part... unless we're doing sustain-only loop on prefetch area only. Then
212 // streaming will only cover the release part, and it shouldn't be looped.
213 if (l
->eff_loop_mode
== slm_loop_continuous
|| (l
->eff_loop_mode
== slm_loop_sustain
&& l
->loop_end
>= l
->eff_waveform
->preloaded_frames
))
215 loop_start
= l
->loop_start
;
216 loop_end
= l
->loop_end
;
218 // Those are initial values only, they will be adjusted in process function
219 v
->current_pipe
= cbox_prefetch_stack_pop(m
->pipe_stack
, l
->eff_waveform
, loop_start
, loop_end
, l
->count
);
220 if (!v
->current_pipe
)
222 g_warning("Prefetch pipe pool exhausted, no streaming playback will be possible");
223 end
= l
->eff_waveform
->preloaded_frames
;
224 v
->gen
.cur_sample_end
= end
;
229 v
->output_pair_no
= (l
->output
+ c
->output_shift
) % m
->output_pairs
;
230 v
->serial_no
= m
->serial_no
;
232 float delay
= l
->delay
;
234 delay
+= rand() * (1.0 / RAND_MAX
) * l
->delay_random
;
236 v
->delay
= (int)(delay
* m
->module
.srate
);
239 v
->gen
.loop_overlap
= l
->loop_overlap
;
240 v
->gen
.loop_overlap_step
= l
->loop_overlap
> 0 ? 1.0 / l
->loop_overlap
: 0;
241 v
->gain_fromvel
= 1.0 + (l
->eff_velcurve
[vel
] - 1.0) * l
->amp_veltrack
* 0.01;
247 v
->released_with_sustain
= 0;
248 v
->released_with_sostenuto
= 0;
249 v
->captured_sostenuto
= 0;
252 v
->program
= c
->program
;
253 v
->amp_env
.shape
= &l
->amp_env_shape
;
254 v
->filter_env
.shape
= &l
->filter_env_shape
;
255 v
->pitch_env
.shape
= &l
->pitch_env_shape
;
257 v
->cutoff_shift
= vel
* l
->fil_veltrack
/ 127.0 + (note
- l
->fil_keycenter
) * l
->fil_keytrack
;
258 v
->loop_mode
= l
->eff_loop_mode
;
259 v
->off_by
= l
->off_by
;
260 v
->reloffset
= l
->reloffset
;
261 int auxes
= (m
->module
.outputs
- m
->module
.aux_offset
) / 2;
262 if (l
->effect1bus
>= 1 && l
->effect1bus
< 1 + auxes
)
263 v
->send1bus
= l
->effect1bus
;
266 if (l
->effect2bus
>= 1 && l
->effect2bus
< 1 + auxes
)
267 v
->send2bus
= l
->effect2bus
;
270 v
->send1gain
= l
->effect1
* 0.01;
271 v
->send2gain
= l
->effect2
* 0.01;
272 if (l
->group
>= 1 && *pexgroupcount
< MAX_RELEASED_GROUPS
)
274 gboolean found
= FALSE
;
275 for (int j
= 0; j
< *pexgroupcount
; j
++)
277 if (exgroups
[j
] == l
->group
)
285 exgroups
[(*pexgroupcount
)++] = l
->group
;
288 lfo_init(&v
->amp_lfo
, &l
->amp_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
289 lfo_init(&v
->filter_lfo
, &l
->filter_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
290 lfo_init(&v
->pitch_lfo
, &l
->pitch_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
292 cbox_biquadf_reset(&v
->filter_left
);
293 cbox_biquadf_reset(&v
->filter_right
);
294 cbox_biquadf_reset(&v
->filter_left2
);
295 cbox_biquadf_reset(&v
->filter_right2
);
296 cbox_onepolef_reset(&v
->onepole_left
);
297 cbox_onepolef_reset(&v
->onepole_right
);
298 // set gain later (it's a less expensive operation)
299 if (l
->tonectl_freq
!= 0)
300 cbox_onepolef_set_highshelf_tonectl(&v
->onepole_coeffs
, l
->tonectl_freq
* M_PI
* m
->module
.srate_inv
, 1.0);
302 GSList
*nif
= v
->layer
->nifs
;
305 struct sampler_noteinitfunc
*p
= nif
->data
;
309 v
->offset
= l
->offset
;
310 if (v
->reloffset
!= 0)
312 uint32_t maxend
= v
->current_pipe
? (l
->eff_waveform
->preloaded_frames
>> 1) : l
->eff_waveform
->preloaded_frames
;
313 int32_t pos
= v
->offset
+ v
->reloffset
* maxend
* 0.01;
321 cbox_envelope_reset(&v
->amp_env
);
322 cbox_envelope_reset(&v
->filter_env
);
323 cbox_envelope_reset(&v
->pitch_env
);
325 v
->last_eq_bitmask
= 0;
327 sampler_voice_activate(v
, l
->eff_waveform
->info
.channels
== 2 ? spt_stereo16
: spt_mono16
);
329 uint32_t pos
= v
->offset
;
330 if (l
->offset_random
)
331 pos
+= ((uint32_t)(rand() + (rand() << 16))) % l
->offset_random
;
334 v
->gen
.bigpos
= ((uint64_t)pos
) << 32;
335 v
->gen
.virtpos
= ((uint64_t)pos
) << 32;
337 if (v
->current_pipe
&& v
->gen
.bigpos
)
338 cbox_prefetch_pipe_consumed(v
->current_pipe
, v
->gen
.bigpos
>> 32);
339 v
->layer_changed
= TRUE
;
342 void sampler_voice_link(struct sampler_voice
**pv
, struct sampler_voice
*v
)
351 void sampler_voice_unlink(struct sampler_voice
**pv
, struct sampler_voice
*v
)
356 v
->prev
->next
= v
->next
;
358 v
->next
->prev
= v
->prev
;
363 void sampler_voice_inactivate(struct sampler_voice
*v
, gboolean expect_active
)
365 assert((v
->gen
.mode
!= spt_inactive
) == expect_active
);
366 sampler_voice_unlink(&v
->channel
->voices_running
, v
);
367 v
->gen
.mode
= spt_inactive
;
370 cbox_prefetch_stack_push(v
->program
->module
->pipe_stack
, v
->current_pipe
);
371 v
->current_pipe
= NULL
;
374 sampler_voice_link(&v
->program
->module
->voices_free
, v
);
377 void sampler_voice_release(struct sampler_voice
*v
, gboolean is_polyaft
)
379 if ((v
->loop_mode
== slm_one_shot_chokeable
) != is_polyaft
)
381 if (v
->delay
>= v
->age
+ CBOX_BLOCK_SIZE
)
384 sampler_voice_inactivate(v
, TRUE
);
388 if (v
->loop_mode
!= slm_one_shot
&& !v
->layer
->count
)
391 if (v
->loop_mode
== slm_loop_sustain
&& v
->current_pipe
)
394 v
->current_pipe
->file_loop_end
= v
->gen
.cur_sample_end
;
395 v
->current_pipe
->file_loop_start
= -1;
401 void sampler_voice_update_params_from_layer(struct sampler_voice
*v
)
403 struct sampler_layer_data
*l
= v
->layer
;
404 struct sampler_module
*m
= v
->program
->module
;
405 lfo_update_freq(&v
->amp_lfo
, &l
->amp_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
406 lfo_update_freq(&v
->filter_lfo
, &l
->filter_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
407 lfo_update_freq(&v
->pitch_lfo
, &l
->pitch_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
408 cbox_envelope_update_shape(&v
->amp_env
, &l
->amp_env_shape
);
409 cbox_envelope_update_shape(&v
->filter_env
, &l
->filter_env_shape
);
410 cbox_envelope_update_shape(&v
->pitch_env
, &l
->pitch_env_shape
);
413 void sampler_voice_process(struct sampler_voice
*v
, struct sampler_module
*m
, cbox_sample_t
**outputs
)
415 struct sampler_layer_data
*l
= v
->layer
;
416 assert(v
->gen
.mode
!= spt_inactive
);
418 // if it's a DAHD envelope without sustain, consider the note finished
419 if (__builtin_expect(v
->amp_env
.cur_stage
== 4 && v
->amp_env
.shape
->stages
[3].end_value
<= 0.f
, 0))
420 cbox_envelope_go_to(&v
->amp_env
, 15);
422 struct sampler_channel
*c
= v
->channel
;
423 v
->age
+= CBOX_BLOCK_SIZE
;
425 if (__builtin_expect(v
->age
< v
->delay
, 0))
428 // XXXKF I'm sacrificing sample accuracy for delays for now
430 const float velscl
= v
->vel
* (1.f
/ 127.f
);
431 if (__builtin_expect(v
->layer_changed
, 0))
434 if (v
->last_waveform
!= v
->layer
->eff_waveform
)
436 v
->last_waveform
= v
->layer
->eff_waveform
;
437 if (v
->layer
->eff_waveform
)
439 v
->gen
.mode
= v
->layer
->eff_waveform
->info
.channels
== 2 ? spt_stereo16
: spt_mono16
;
440 v
->gen
.cur_sample_end
= v
->layer
->eff_waveform
->info
.frames
;
444 sampler_voice_inactivate(v
, TRUE
);
448 #define RECALC_EQ_IF(index) \
449 if (l->eq_bitmask & (1 << (index - 1))) \
451 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); \
452 if (!(v->last_eq_bitmask & (1 << (index - 1)))) \
454 cbox_biquadf_reset(&v->eq_left[index-1]); \
455 cbox_biquadf_reset(&v->eq_right[index-1]); \
462 v
->last_eq_bitmask
= l
->eq_bitmask
;
463 v
->layer_changed
= FALSE
;
466 float pitch
= (v
->note
- l
->pitch_keycenter
) * l
->pitch_keytrack
+ l
->tune
+ l
->transpose
* 100 + v
->pitch_shift
;
467 float modsrcs
[smsrc_pernote_count
];
468 modsrcs
[smsrc_vel
- smsrc_pernote_offset
] = v
->vel
* velscl
;
469 modsrcs
[smsrc_pitch
- smsrc_pernote_offset
] = pitch
* (1.f
/ 100.f
);
470 modsrcs
[smsrc_polyaft
- smsrc_pernote_offset
] = 0.f
; // XXXKF not supported yet
471 modsrcs
[smsrc_pitchenv
- smsrc_pernote_offset
] = cbox_envelope_get_next(&v
->pitch_env
, v
->released
) * 0.01f
;
472 modsrcs
[smsrc_filenv
- smsrc_pernote_offset
] = cbox_envelope_get_next(&v
->filter_env
, v
->released
) * 0.01f
;
473 modsrcs
[smsrc_ampenv
- smsrc_pernote_offset
] = cbox_envelope_get_next(&v
->amp_env
, v
->released
) * 0.01f
;
475 modsrcs
[smsrc_amplfo
- smsrc_pernote_offset
] = lfo_run(&v
->amp_lfo
);
476 modsrcs
[smsrc_fillfo
- smsrc_pernote_offset
] = lfo_run(&v
->filter_lfo
);
477 modsrcs
[smsrc_pitchlfo
- smsrc_pernote_offset
] = lfo_run(&v
->pitch_lfo
);
479 if (__builtin_expect(v
->amp_env
.cur_stage
< 0, 0))
481 if (__builtin_expect(is_tail_finished(v
), 0))
483 sampler_voice_inactivate(v
, TRUE
);
488 float moddests
[smdestcount
];
489 moddests
[smdest_gain
] = 0;
490 moddests
[smdest_pitch
] = pitch
;
491 moddests
[smdest_cutoff
] = v
->cutoff_shift
;
492 moddests
[smdest_resonance
] = 0;
493 moddests
[smdest_tonectl
] = 0;
494 GSList
*mod
= l
->modulations
;
495 if (__builtin_expect(l
->trigger
== stm_release
, 0))
496 moddests
[smdest_gain
] -= v
->age
* l
->rt_decay
* m
->module
.srate_inv
;
499 moddests
[smdest_pitch
] += c
->pitchwheel
* (c
->pitchwheel
> 0 ? l
->bend_up
: l
->bend_down
) >> 13;
501 static const int modoffset
[4] = {0, -1, -1, 1 };
502 static const int modscale
[4] = {1, 1, 2, -2 };
505 struct sampler_modulation
*sm
= mod
->data
;
506 float value
= 0.f
, value2
= 1.f
;
507 if (sm
->src
< smsrc_pernote_offset
)
508 value
= c
->cc
[sm
->src
] * (1.f
/ 127.f
);
510 value
= modsrcs
[sm
->src
- smsrc_pernote_offset
];
511 value
= modoffset
[sm
->flags
& 3] + value
* modscale
[sm
->flags
& 3];
513 if (sm
->src2
!= smsrc_none
)
515 if (sm
->src2
< smsrc_pernote_offset
)
516 value2
= c
->cc
[sm
->src2
] * (1.f
/ 127.f
);
518 value2
= modsrcs
[sm
->src2
- smsrc_pernote_offset
];
520 value2
= modoffset
[(sm
->flags
& 12) >> 2] + value2
* modscale
[(sm
->flags
& 12) >> 2];
523 moddests
[sm
->dest
] += value
* sm
->amount
;
525 mod
= g_slist_next(mod
);
528 double maxv
= 127 << 7;
529 double freq
= l
->eff_freq
* cent2factor(moddests
[smdest_pitch
]) ;
530 uint64_t freq64
= (uint64_t)(freq
* 65536.0 * 65536.0 * m
->module
.srate_inv
);
532 gboolean playing_sustain_loop
= !v
->released
&& v
->loop_mode
== slm_loop_sustain
;
533 uint32_t loop_start
, loop_end
;
534 gboolean bandlimited
= FALSE
;
536 if (!v
->current_pipe
)
538 v
->gen
.sample_data
= v
->last_waveform
->data
;
539 if (v
->last_waveform
->levels
)
541 gboolean use_cached
= v
->last_level
> 0 && v
->last_level
< v
->last_waveform
->level_count
542 && freq64
> v
->last_level_min_rate
&& freq64
<= v
->last_waveform
->levels
[v
->last_level
].max_rate
;
543 if (__builtin_expect(use_cached
, 1))
545 v
->gen
.sample_data
= v
->last_waveform
->levels
[v
->last_level
].data
;
550 for (int i
= 0; i
< v
->last_waveform
->level_count
; i
++)
552 if (freq64
<= v
->last_waveform
->levels
[i
].max_rate
)
555 v
->gen
.sample_data
= v
->last_waveform
->levels
[i
].data
;
560 v
->last_level_min_rate
= v
->last_waveform
->levels
[i
].max_rate
;
566 gboolean play_loop
= v
->layer
->loop_end
&& (v
->loop_mode
== slm_loop_continuous
|| playing_sustain_loop
) && v
->layer
->on_cc_number
== -1;
567 loop_start
= play_loop
? v
->layer
->loop_start
: (v
->layer
->count
? 0 : (uint32_t)-1);
568 loop_end
= play_loop
? v
->layer
->loop_end
: v
->gen
.cur_sample_end
;
572 v
->gen
.sample_data
= v
->gen
.loop_count
? v
->current_pipe
->data
: v
->last_waveform
->data
;
573 v
->gen
.streaming_buffer
= v
->current_pipe
->data
;
575 v
->gen
.prefetch_only_loop
= (loop_end
< v
->last_waveform
->preloaded_frames
);
576 v
->gen
.loop_overlap
= 0;
577 if (v
->gen
.prefetch_only_loop
)
579 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)
580 v
->gen
.loop_start
= loop_start
;
581 v
->gen
.loop_end
= loop_end
;
582 v
->gen
.streaming_buffer_frames
= 0;
586 v
->gen
.loop_start
= 0;
587 v
->gen
.loop_end
= v
->last_waveform
->preloaded_frames
;
588 v
->gen
.streaming_buffer_frames
= v
->current_pipe
->buffer_loop_end
;
593 v
->gen
.loop_count
= v
->layer
->count
;
594 v
->gen
.loop_start
= loop_start
;
595 v
->gen
.loop_end
= loop_end
;
599 // Use pre-calculated join
600 v
->gen
.scratch
= loop_start
== (uint32_t)-1 ? v
->layer
->scratch_end
: v
->layer
->scratch_loop
;
604 // The standard waveforms have extra MAX_INTERPOLATION_ORDER of samples from the loop start added past loop_end,
605 // to avoid wasting time generating the joins in all the practical cases. The slow path covers custom loops
606 // (i.e. partial loop or no loop) over bandlimited versions of the standard waveforms, and those are probably
607 // not very useful anyway, as changing the loop removes the guarantee of the waveform being bandlimited and
608 // may cause looping artifacts or introduce DC offset (e.g. if only a positive part of a sine wave is looped).
609 if (loop_start
== 0 && loop_end
== l
->eff_waveform
->info
.frames
)
610 v
->gen
.scratch
= v
->gen
.sample_data
+ l
->eff_waveform
->info
.frames
- MAX_INTERPOLATION_ORDER
;
613 // Generate the join for the current wave level
614 // XXXKF this could be optimised further, by checking if waveform and loops are the same as the last
615 // time. However, this code is not likely to be used... ever, so optimising it is not the priority.
616 int shift
= l
->eff_waveform
->info
.channels
== 2 ? 1 : 0;
617 uint32_t halfscratch
= MAX_INTERPOLATION_ORDER
<< shift
;
619 v
->gen
.scratch
= v
->gen
.scratch_bandlimited
;
620 memcpy(&v
->gen
.scratch_bandlimited
[0], &v
->gen
.sample_data
[(loop_end
- MAX_INTERPOLATION_ORDER
) << shift
], halfscratch
* sizeof(int16_t) );
621 if (loop_start
!= (uint32_t)-1)
622 memcpy(v
->gen
.scratch_bandlimited
+ halfscratch
, &v
->gen
.sample_data
[loop_start
<< shift
], halfscratch
* sizeof(int16_t));
624 memset(v
->gen
.scratch_bandlimited
+ halfscratch
, 0, halfscratch
* sizeof(int16_t));
631 v
->gen
.bigdelta
= freq64
;
632 v
->gen
.virtdelta
= (uint64_t)(l
->eff_freq
* 65536.0 * 65536.0 * m
->module
.srate_inv
);
633 v
->gen
.stretching_jump
= l
->timestretch_jump
;
634 v
->gen
.stretching_crossfade
= l
->timestretch_crossfade
;
638 v
->gen
.bigdelta
= freq64
;
639 v
->gen
.virtdelta
= freq64
;
641 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
);
642 if (moddests
[smdest_gain
] != 0.f
)
643 gain
*= dB2gain(moddests
[smdest_gain
]);
644 // http://drealm.info/sfz/plj-sfz.xhtml#amp "The overall gain must remain in the range -144 to 6 decibels."
647 float pan
= (l
->pan
+ 100.f
) * (1.f
/ 200.f
) + (c
->channel_pan_cc
* 1.f
/ maxv
- 0.5f
) * 2.f
;
652 v
->gen
.lgain
= gain
* (1.f
- pan
) / 32768.f
;
653 v
->gen
.rgain
= gain
* pan
/ 32768.f
;
654 struct cbox_biquadf_coeffs
*second_filter
= &v
->filter_coeffs
;
655 gboolean is4p
= sampler_layer_data_is_4pole(v
->layer
);
656 if (l
->cutoff
!= -1.f
)
658 float logcutoff
= l
->logcutoff
+ moddests
[smdest_cutoff
];
661 if (logcutoff
> 12798)
663 //float resonance = v->resonance*pow(32.0,c->cc[71]/maxv);
664 float resonance
= l
->resonance_linearized
* dB2gain((is4p
? 0.5 : 1) * moddests
[smdest_resonance
]);
665 if (resonance
< 0.7f
)
667 if (resonance
> 32.f
)
672 cbox_biquadf_set_lp_rbj_lookup(&v
->filter_coeffs
, &m
->sincos
[(int)logcutoff
], resonance
* resonance
);
673 cbox_biquadf_set_1plp_lookup(&v
->filter_coeffs_extra
, &m
->sincos
[(int)logcutoff
], 1);
674 second_filter
= &v
->filter_coeffs_extra
;
679 cbox_biquadf_set_lp_rbj_lookup(&v
->filter_coeffs
, &m
->sincos
[(int)logcutoff
], resonance
);
683 cbox_biquadf_set_hp_rbj_lookup(&v
->filter_coeffs
, &m
->sincos
[(int)logcutoff
], resonance
);
687 cbox_biquadf_set_bp_rbj_lookup(&v
->filter_coeffs
, &m
->sincos
[(int)logcutoff
], resonance
);
692 cbox_biquadf_set_1plp_lookup(&v
->filter_coeffs
, &m
->sincos
[(int)logcutoff
], l
->fil_type
!= sft_lp6
);
697 cbox_biquadf_set_1php_lookup(&v
->filter_coeffs
, &m
->sincos
[(int)logcutoff
], l
->fil_type
!= sft_hp6
);
703 if (__builtin_expect(l
->tonectl_freq
!= 0, 0))
705 float ctl
= l
->tonectl
+ moddests
[smdest_tonectl
];
706 if (fabs(ctl
) > 0.0001f
)
707 cbox_onepolef_set_highshelf_setgain(&v
->onepole_coeffs
, dB2gain(ctl
));
709 cbox_onepolef_set_highshelf_setgain(&v
->onepole_coeffs
, 1.0);
712 float leftright
[2 * CBOX_BLOCK_SIZE
];
714 uint32_t samples
= 0;
719 uint32_t limit
= cbox_prefetch_pipe_get_remaining(v
->current_pipe
);
721 v
->gen
.mode
= spt_inactive
;
724 samples
= sampler_gen_sample_playback(&v
->gen
, leftright
, limit
- 4);
725 cbox_prefetch_pipe_consumed(v
->current_pipe
, v
->gen
.consumed
);
731 samples
= sampler_gen_sample_playback(&v
->gen
, leftright
, (uint32_t)-1);
734 for (int i
= 2 * samples
; i
< 2 * CBOX_BLOCK_SIZE
; i
++)
738 cbox_biquadf_process_stereo(&v
->filter_left
, &v
->filter_right
, &v
->filter_coeffs
, leftright
);
740 cbox_biquadf_process_stereo(&v
->filter_left2
, &v
->filter_right2
, second_filter
, leftright
);
742 if (__builtin_expect(l
->tonectl_freq
!= 0, 0))
744 cbox_onepolef_process_stereo(&v
->onepole_left
, &v
->onepole_right
, &v
->onepole_coeffs
, leftright
);
746 if (__builtin_expect(l
->eq_bitmask
, 0))
748 for (int eq
= 0; eq
< 3; eq
++)
750 if (l
->eq_bitmask
& (1 << eq
))
752 cbox_biquadf_process_stereo(&v
->eq_left
[eq
], &v
->eq_right
[eq
], &v
->eq_coeffs
[eq
], leftright
);
757 mix_block_into(outputs
, v
->output_pair_no
* 2, leftright
);
758 if (__builtin_expect((v
->send1bus
> 0 && v
->send1gain
!= 0) || (v
->send2bus
> 0 && v
->send2gain
!= 0), 0))
760 if (v
->send1bus
> 0 && v
->send1gain
!= 0)
762 int oofs
= m
->module
.aux_offset
+ (v
->send1bus
- 1) * 2;
763 mix_block_into_with_gain(outputs
, oofs
, leftright
, v
->send1gain
);
765 if (v
->send2bus
> 0 && v
->send2gain
!= 0)
767 int oofs
= m
->module
.aux_offset
+ (v
->send2bus
- 1) * 2;
768 mix_block_into_with_gain(outputs
, oofs
, leftright
, v
->send2gain
);
771 if (v
->gen
.mode
== spt_inactive
)
772 sampler_voice_inactivate(v
, FALSE
);