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 #define MAX_RELEASED_GROUPS 4
40 static float sine_wave
[2049];
42 GQuark
cbox_sampler_error_quark()
44 return g_quark_from_string("cbox-sampler-error-quark");
47 static void sampler_process_block(struct cbox_module
*module
, cbox_sample_t
**inputs
, cbox_sample_t
**outputs
);
48 static void sampler_process_event(struct cbox_module
*module
, const uint8_t *data
, uint32_t len
);
49 static void sampler_destroyfunc(struct cbox_module
*module
);
50 static void sampler_voice_activate(struct sampler_voice
*v
, enum sampler_player_type mode
);
52 static void lfo_init(struct sampler_lfo
*lfo
, struct sampler_lfo_params
*lfop
, int srate
, double srate_inv
)
56 lfo
->delta
= (uint32_t)(lfop
->freq
* 65536.0 * 65536.0 * CBOX_BLOCK_SIZE
* srate_inv
);
57 lfo
->delay
= (uint32_t)(lfop
->delay
* srate
);
58 lfo
->fade
= (uint32_t)(lfop
->fade
* srate
);
62 static void sampler_voice_release(struct sampler_voice
*v
, gboolean is_polyaft
);
64 static void sampler_start_voice(struct sampler_module
*m
, struct sampler_channel
*c
, struct sampler_voice
*v
, struct sampler_layer_data
*l
, int note
, int vel
, int *exgroups
, int *pexgroupcount
)
66 sampler_gen_reset(&v
->gen
);
68 if (l
->trigger
== stm_release
)
70 // time since last 'note on' for that note
71 v
->age
= m
->current_time
- c
->prev_note_start_time
[note
];
72 double age
= v
->age
* m
->module
.srate_inv
;
73 // if attenuation is more than 84dB, ignore the release trigger
74 if (age
* l
->rt_decay
> 84)
77 uint32_t end
= l
->waveform
->info
.frames
;
79 end
= (l
->end
== -1) ? 0 : l
->end
;
80 v
->last_waveform
= l
->waveform
;
81 v
->gen
.cur_sample_end
= end
;
82 if (end
> l
->waveform
->info
.frames
)
83 end
= l
->waveform
->info
.frames
;
85 v
->output_pair_no
= l
->output
% m
->output_pairs
;
86 v
->serial_no
= m
->serial_no
;
88 uint32_t pos
= l
->offset
;
91 pos
+= ((uint32_t)(rand() + (rand() << 16))) % l
->offset_random
;
96 float delay
= l
->delay
;
98 delay
+= rand() * (1.0 / RAND_MAX
) * l
->delay_random
;
100 v
->delay
= (int)(delay
* m
->module
.srate
);
103 v
->gen
.loop_overlap
= l
->loop_overlap
;
104 v
->gen
.loop_overlap_step
= 1.0 / l
->loop_overlap
;
105 v
->gain_fromvel
= 1.0 + (l
->eff_velcurve
[vel
] - 1.0) * l
->amp_veltrack
* 0.01;
111 v
->released_with_sustain
= 0;
112 v
->released_with_sostenuto
= 0;
113 v
->captured_sostenuto
= 0;
117 v
->program
= c
->program
;
118 v
->amp_env
.shape
= &l
->amp_env_shape
;
119 v
->filter_env
.shape
= &l
->filter_env_shape
;
120 v
->pitch_env
.shape
= &l
->pitch_env_shape
;
122 v
->cutoff_shift
= vel
* l
->fil_veltrack
/ 127.0 + (note
- l
->fil_keycenter
) * l
->fil_keytrack
;
123 v
->loop_mode
= l
->loop_mode
;
124 v
->off_by
= l
->off_by
;
125 int auxes
= (m
->module
.outputs
- m
->module
.aux_offset
) / 2;
126 if (l
->effect1bus
>= 1 && l
->effect1bus
< 1 + auxes
)
127 v
->send1bus
= l
->effect1bus
;
130 if (l
->effect2bus
>= 1 && l
->effect2bus
< 1 + auxes
)
131 v
->send2bus
= l
->effect2bus
;
134 v
->send1gain
= l
->effect1
* 0.01;
135 v
->send2gain
= l
->effect2
* 0.01;
136 if (l
->group
>= 1 && *pexgroupcount
< MAX_RELEASED_GROUPS
)
138 gboolean found
= FALSE
;
139 for (int j
= 0; j
< *pexgroupcount
; j
++)
141 if (exgroups
[j
] == l
->group
)
149 exgroups
[(*pexgroupcount
)++] = l
->group
;
152 lfo_init(&v
->amp_lfo
, &l
->amp_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
153 lfo_init(&v
->filter_lfo
, &l
->filter_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
154 lfo_init(&v
->pitch_lfo
, &l
->pitch_lfo
, m
->module
.srate
, m
->module
.srate_inv
);
156 cbox_biquadf_reset(&v
->filter_left
);
157 cbox_biquadf_reset(&v
->filter_right
);
158 cbox_biquadf_reset(&v
->filter_left2
);
159 cbox_biquadf_reset(&v
->filter_right2
);
161 GSList
*nif
= v
->layer
->nifs
;
164 struct sampler_noteinitfunc
*p
= nif
->data
;
169 cbox_envelope_reset(&v
->amp_env
);
170 cbox_envelope_reset(&v
->filter_env
);
171 cbox_envelope_reset(&v
->pitch_env
);
173 sampler_voice_activate(v
, l
->waveform
->info
.channels
== 2 ? spt_stereo16
: spt_mono16
);
176 #define FOREACH_VOICE(var, p) \
177 for (struct sampler_voice *p = (var), *p##_next = NULL; p && (p##_next = p->next, TRUE); p = p##_next)
179 static void sampler_release_groups(struct sampler_module
*m
, struct sampler_channel
*c
, int note
, int exgroups
[MAX_RELEASED_GROUPS
], int exgroupcount
)
183 FOREACH_VOICE(c
->voices_running
, v
)
185 for (int j
= 0; j
< exgroupcount
; j
++)
187 if (v
->off_by
== exgroups
[j
] && v
->note
!= note
)
189 if (v
->layer
->off_mode
== som_fast
)
192 cbox_envelope_go_to(&v
->amp_env
, 15);
205 void sampler_start_note(struct sampler_module
*m
, struct sampler_channel
*c
, int note
, int vel
, gboolean is_release_trigger
)
207 float random
= rand() * 1.0 / (RAND_MAX
+ 1.0);
208 if (!is_release_trigger
)
210 c
->switchmask
[note
>> 5] |= 1 << (note
& 31);
211 c
->prev_note_velocity
[note
] = vel
;
212 c
->prev_note_start_time
[note
] = m
->current_time
;
214 struct sampler_program
*prg
= c
->program
;
215 if (!prg
|| !prg
->rll
|| prg
->deleting
)
217 GSList
*next_layer
= sampler_program_get_next_layer(prg
, c
, !is_release_trigger
? prg
->rll
->layers
: prg
->rll
->layers_release
, note
, vel
, random
);
220 if (!is_release_trigger
)
221 c
->previous_note
= note
;
225 // this might perhaps be optimized by mapping the group identifiers to flat-array indexes
226 // but I'm not going to do that until someone gives me an SFZ worth doing that work ;)
227 int exgroups
[MAX_RELEASED_GROUPS
], exgroupcount
= 0;
229 FOREACH_VOICE(m
->voices_free
, v
)
231 struct sampler_layer
*l
= next_layer
->data
;
232 // Maybe someone forgot to call sampler_update_layer?
234 sampler_start_voice(m
, c
, v
, l
->runtime
, note
, vel
, exgroups
, &exgroupcount
);
235 next_layer
= sampler_program_get_next_layer(prg
, c
, g_slist_next(next_layer
), note
, vel
, random
);
239 if (!is_release_trigger
)
240 c
->previous_note
= note
;
241 sampler_release_groups(m
, c
, note
, exgroups
, exgroupcount
);
244 void sampler_channel_start_release_triggered_voices(struct sampler_channel
*c
, int note
)
246 if (c
->program
&& c
->program
->rll
&& c
->program
->rll
->layers_release
)
248 if (c
->prev_note_velocity
[note
])
250 sampler_start_note(c
->module
, c
, note
, c
->prev_note_velocity
[note
], TRUE
);
251 c
->prev_note_velocity
[note
] = 0;
256 void sampler_voice_link(struct sampler_voice
**pv
, struct sampler_voice
*v
)
265 void sampler_voice_unlink(struct sampler_voice
**pv
, struct sampler_voice
*v
)
270 v
->prev
->next
= v
->next
;
272 v
->next
->prev
= v
->prev
;
277 void sampler_voice_inactivate(struct sampler_voice
*v
, gboolean expect_active
)
279 assert((v
->gen
.mode
!= spt_inactive
) == expect_active
);
280 sampler_voice_unlink(&v
->channel
->voices_running
, v
);
281 v
->gen
.mode
= spt_inactive
;
283 sampler_voice_link(&v
->program
->module
->voices_free
, v
);
286 void sampler_voice_activate(struct sampler_voice
*v
, enum sampler_player_type mode
)
288 assert(v
->gen
.mode
== spt_inactive
);
289 sampler_voice_unlink(&v
->program
->module
->voices_free
, v
);
290 assert(mode
!= spt_inactive
);
293 sampler_voice_link(&v
->channel
->voices_running
, v
);
296 void sampler_voice_release(struct sampler_voice
*v
, gboolean is_polyaft
)
298 if ((v
->loop_mode
== slm_one_shot_chokeable
) != is_polyaft
)
300 if (v
->delay
>= v
->age
+ CBOX_BLOCK_SIZE
)
303 sampler_voice_inactivate(v
, TRUE
);
307 if (v
->loop_mode
!= slm_one_shot
&& !v
->layer
->count
)
312 void sampler_stop_note(struct sampler_module
*m
, struct sampler_channel
*c
, int note
, int vel
, gboolean is_polyaft
)
314 c
->switchmask
[note
>> 5] &= ~(1 << (note
& 31));
315 FOREACH_VOICE(c
->voices_running
, v
)
317 if (v
->note
== note
&& v
->layer
->trigger
!= stm_release
)
319 if (v
->captured_sostenuto
)
320 v
->released_with_sostenuto
= 1;
321 else if (c
->cc
[64] >= 64)
322 v
->released_with_sustain
= 1;
324 sampler_voice_release(v
, is_polyaft
);
329 sampler_channel_start_release_triggered_voices(c
, note
);
331 c
->sustainmask
[note
>> 5] |= (1 << (note
& 31));
334 void sampler_stop_sustained(struct sampler_module
*m
, struct sampler_channel
*c
)
336 FOREACH_VOICE(c
->voices_running
, v
)
338 if (v
->channel
== c
&& v
->released_with_sustain
&& v
->layer
->trigger
!= stm_release
)
340 sampler_voice_release(v
, FALSE
);
341 v
->released_with_sustain
= 0;
344 // Start release layers for the newly released keys
345 if (c
->program
->rll
&& c
->program
->rll
->layers_release
)
347 for (int i
= 0; i
< 128; i
++)
349 if (c
->sustainmask
[i
>> 5] & (1 << (i
& 31)))
350 sampler_channel_start_release_triggered_voices(c
, i
);
353 memset(c
->sustainmask
, 0, sizeof(c
->sustainmask
));
356 void sampler_stop_sostenuto(struct sampler_module
*m
, struct sampler_channel
*c
)
358 FOREACH_VOICE(c
->voices_running
, v
)
360 if (v
->released_with_sostenuto
&& v
->layer
->trigger
!= stm_release
)
362 sampler_channel_start_release_triggered_voices(c
, v
->note
);
363 sampler_voice_release(v
, FALSE
);
364 v
->released_with_sostenuto
= 0;
365 // XXXKF unsure what to do with sustain
368 // Start release layers for the newly released keys
369 if (c
->program
->rll
&& c
->program
->rll
->layers_release
)
371 for (int i
= 0; i
< 128; i
++)
373 if (c
->sostenutomask
[i
>> 5] & (1 << (i
& 31)))
374 sampler_channel_start_release_triggered_voices(c
, i
);
377 memset(c
->sostenutomask
, 0, sizeof(c
->sostenutomask
));
380 void sampler_capture_sostenuto(struct sampler_module
*m
, struct sampler_channel
*c
)
382 FOREACH_VOICE(c
->voices_running
, v
)
384 if (!v
->released
&& v
->loop_mode
!= slm_one_shot
&& v
->loop_mode
!= slm_one_shot_chokeable
&& !v
->layer
->count
)
386 // XXXKF unsure what to do with sustain
387 v
->captured_sostenuto
= 1;
388 c
->sostenutomask
[v
->note
>> 5] |= (1 << (v
->note
& 31));
393 void sampler_stop_all(struct sampler_module
*m
, struct sampler_channel
*c
)
395 FOREACH_VOICE(c
->voices_running
, v
)
397 sampler_voice_release(v
, v
->loop_mode
== slm_one_shot_chokeable
);
398 v
->released_with_sustain
= 0;
399 v
->released_with_sostenuto
= 0;
400 v
->captured_sostenuto
= 0;
404 void sampler_steal_voice(struct sampler_module
*m
)
407 struct sampler_voice
*voice_found
= NULL
;
408 for (int i
= 0; i
< 16; i
++)
410 FOREACH_VOICE(m
->channels
[i
].voices_running
, v
)
412 if (v
->amp_env
.cur_stage
== 15)
414 int age
= m
->serial_no
- v
->serial_no
;
415 if (v
->gen
.loop_start
== -1)
416 age
+= (int)(v
->gen
.pos
* 100.0 / v
->gen
.cur_sample_end
);
429 voice_found
->released
= 1;
430 cbox_envelope_go_to(&voice_found
->amp_env
, 15);
434 static inline void mix_block_into_with_gain(cbox_sample_t
**outputs
, int oofs
, float *src_left
, float *src_right
, float gain
)
436 cbox_sample_t
*dst_left
= outputs
[oofs
];
437 cbox_sample_t
*dst_right
= outputs
[oofs
+ 1];
438 for (size_t i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
440 dst_left
[i
] += gain
* src_left
[i
];
441 dst_right
[i
] += gain
* src_right
[i
];
445 static inline float lfo_run(struct sampler_lfo
*lfo
)
447 if (lfo
->age
< lfo
->delay
)
449 lfo
->age
+= CBOX_BLOCK_SIZE
;
453 const int FRAC_BITS
= 32 - 11;
454 lfo
->phase
+= lfo
->delta
;
455 uint32_t iphase
= lfo
->phase
>> FRAC_BITS
;
456 float frac
= (lfo
->phase
& ((1 << FRAC_BITS
) - 1)) * (1.0 / (1 << FRAC_BITS
));
458 float v
= sine_wave
[iphase
] + (sine_wave
[iphase
+ 1] - sine_wave
[iphase
]) * frac
;
459 if (lfo
->fade
&& lfo
->age
< lfo
->delay
+ lfo
->fade
)
461 v
*= (lfo
->age
- lfo
->delay
) * 1.0 / lfo
->fade
;
462 lfo
->age
+= CBOX_BLOCK_SIZE
;
468 static inline float clip01(float v
)
477 static gboolean
is_4pole(struct sampler_layer_data
*v
)
481 return v
->fil_type
== sft_lp24
|| v
->fil_type
== sft_hp24
|| v
->fil_type
== sft_bp12
;
484 static gboolean
is_tail_finished(struct sampler_voice
*v
)
486 if (v
->layer
->cutoff
== -1)
488 double eps
= 1.0 / 65536.0;
489 if (cbox_biquadf_is_audible(&v
->filter_left
, eps
))
491 if (cbox_biquadf_is_audible(&v
->filter_right
, eps
))
493 if (is_4pole(v
->layer
))
495 if (cbox_biquadf_is_audible(&v
->filter_left2
, eps
))
497 if (cbox_biquadf_is_audible(&v
->filter_right2
, eps
))
504 static inline int addcc(struct sampler_channel
*c
, int cc_no
)
506 return (((int)c
->cc
[cc_no
]) << 7) + c
->cc
[cc_no
+ 32];
509 void sampler_voice_process(struct sampler_voice
*v
, struct sampler_module
*m
, cbox_sample_t
**outputs
)
511 struct sampler_layer_data
*l
= v
->layer
;
512 assert(v
->gen
.mode
!= spt_inactive
);
514 // if it's a DAHD envelope without sustain, consider the note finished
515 if (v
->amp_env
.cur_stage
== 4 && v
->amp_env
.shape
->stages
[3].end_value
== 0)
516 cbox_envelope_go_to(&v
->amp_env
, 15);
518 struct sampler_channel
*c
= v
->channel
;
519 v
->age
+= CBOX_BLOCK_SIZE
;
521 if (v
->age
< v
->delay
)
524 if (v
->last_waveform
!= v
->layer
->waveform
)
526 v
->last_waveform
= v
->layer
->waveform
;
527 if (v
->layer
->waveform
)
529 v
->gen
.mode
= v
->layer
->waveform
->info
.channels
== 2 ? spt_stereo16
: spt_mono16
;
530 v
->gen
.cur_sample_end
= v
->layer
->waveform
->info
.frames
;
534 sampler_voice_inactivate(v
, TRUE
);
538 // XXXKF I'm sacrificing sample accuracy for delays for now
541 float pitch
= (v
->note
- l
->pitch_keycenter
) * l
->pitch_keytrack
+ l
->tune
+ l
->transpose
* 100 + v
->pitch_shift
;
542 float modsrcs
[smsrc_pernote_count
];
543 modsrcs
[smsrc_vel
- smsrc_pernote_offset
] = v
->vel
* (1.0 / 127.0);
544 modsrcs
[smsrc_pitch
- smsrc_pernote_offset
] = pitch
* (1.0 / 100.0);
545 modsrcs
[smsrc_polyaft
- smsrc_pernote_offset
] = 0; // XXXKF not supported yet
546 modsrcs
[smsrc_pitchenv
- smsrc_pernote_offset
] = cbox_envelope_get_next(&v
->pitch_env
, v
->released
) * 0.01f
;
547 modsrcs
[smsrc_filenv
- smsrc_pernote_offset
] = cbox_envelope_get_next(&v
->filter_env
, v
->released
) * 0.01f
;
548 modsrcs
[smsrc_ampenv
- smsrc_pernote_offset
] = cbox_envelope_get_next(&v
->amp_env
, v
->released
) * 0.01f
;
550 modsrcs
[smsrc_amplfo
- smsrc_pernote_offset
] = lfo_run(&v
->amp_lfo
);
551 modsrcs
[smsrc_fillfo
- smsrc_pernote_offset
] = lfo_run(&v
->filter_lfo
);
552 modsrcs
[smsrc_pitchlfo
- smsrc_pernote_offset
] = lfo_run(&v
->pitch_lfo
);
554 if (v
->amp_env
.cur_stage
< 0)
556 if (is_tail_finished(v
))
558 sampler_voice_inactivate(v
, TRUE
);
563 float moddests
[smdestcount
];
564 moddests
[smdest_gain
] = 0;
565 moddests
[smdest_pitch
] = pitch
;
566 moddests
[smdest_cutoff
] = v
->cutoff_shift
;
567 moddests
[smdest_resonance
] = 0;
568 GSList
*mod
= l
->modulations
;
569 if (l
->trigger
== stm_release
)
570 moddests
[smdest_gain
] -= v
->age
* l
->rt_decay
* m
->module
.srate_inv
;
573 moddests
[smdest_pitch
] += c
->pitchwheel
* (c
->pitchwheel
> 0 ? l
->bend_up
: l
->bend_down
) >> 13;
575 static const int modoffset
[4] = {0, -1, -1, 1 };
576 static const int modscale
[4] = {1, 1, 2, -2 };
579 struct sampler_modulation
*sm
= mod
->data
;
580 float value
= 0.f
, value2
= 1.f
;
581 if (sm
->src
< smsrc_pernote_offset
)
582 value
= c
->cc
[sm
->src
] / 127.0;
584 value
= modsrcs
[sm
->src
- smsrc_pernote_offset
];
585 value
= modoffset
[sm
->flags
& 3] + value
* modscale
[sm
->flags
& 3];
587 if (sm
->src2
!= smsrc_none
)
589 if (sm
->src2
< smsrc_pernote_offset
)
590 value2
= c
->cc
[sm
->src2
] / 127.0;
592 value2
= modsrcs
[sm
->src2
- smsrc_pernote_offset
];
594 value2
= modoffset
[(sm
->flags
& 12) >> 2] + value2
* modscale
[(sm
->flags
& 12) >> 2];
597 moddests
[sm
->dest
] += value
* sm
->amount
;
599 mod
= g_slist_next(mod
);
602 double maxv
= 127 << 7;
603 double freq
= l
->eff_freq
* cent2factor(moddests
[smdest_pitch
]) ;
604 uint64_t freq64
= (uint64_t)(freq
* 65536.0 * 65536.0 * m
->module
.srate_inv
);
605 v
->gen
.sample_data
= v
->last_waveform
->data
;
606 if (v
->last_waveform
->levels
)
608 // XXXKF: optimise later by caching last lookup value
609 // XXXKF: optimise later by using binary search
610 for (int i
= 0; i
< v
->last_waveform
->level_count
; i
++)
612 if (freq64
<= v
->last_waveform
->levels
[i
].max_rate
)
614 v
->gen
.sample_data
= v
->last_waveform
->levels
[i
].data
;
619 gboolean post_sustain
= v
->released
&& v
->loop_mode
== slm_loop_sustain
;
620 if (v
->layer
->count
> 0)
622 // End the loop on the last time
623 gboolean play_loop
= (v
->play_count
< v
->layer
->count
- 1);
624 v
->gen
.loop_start
= play_loop
? 0 : (uint32_t)-1;
625 v
->gen
.loop_end
= v
->gen
.cur_sample_end
;
629 gboolean play_loop
= v
->layer
->loop_end
&& (v
->loop_mode
== slm_loop_continuous
|| (v
->loop_mode
== slm_loop_sustain
&& !post_sustain
)) && v
->layer
->on_cc_number
== -1;
630 v
->gen
.loop_start
= play_loop
? v
->layer
->loop_start
: (uint32_t)-1;
631 v
->gen
.loop_end
= play_loop
? v
->layer
->loop_end
: v
->gen
.cur_sample_end
;
634 v
->gen
.delta
= freq64
>> 32;
635 v
->gen
.frac_delta
= freq64
& 0xFFFFFFFF;
636 float gain
= modsrcs
[smsrc_ampenv
- smsrc_pernote_offset
] * l
->volume_linearized
* v
->gain_fromvel
* addcc(c
, 7) * addcc(c
, 11) / (maxv
* maxv
);
637 if (moddests
[smdest_gain
] != 0.0)
638 gain
*= dB2gain(moddests
[smdest_gain
]);
639 // http://drealm.info/sfz/plj-sfz.xhtml#amp "The overall gain must remain in the range -144 to 6 decibels."
642 float pan
= (l
->pan
+ 100) * (1.0 / 200.0) + (addcc(c
, 10) * 1.0 / maxv
- 0.5) * 2;
647 v
->gen
.lgain
= gain
* (1 - pan
) / 32768.0;
648 v
->gen
.rgain
= gain
* pan
/ 32768.0;
651 float cutoff
= l
->cutoff
* cent2factor(moddests
[smdest_cutoff
]);
654 if (cutoff
> m
->module
.srate
* 0.45)
655 cutoff
= m
->module
.srate
* 0.45;
656 //float resonance = v->resonance*pow(32.0,c->cc[71]/maxv);
657 float resonance
= l
->resonance_linearized
* dB2gain(moddests
[smdest_resonance
]);
662 // XXXKF this is found experimentally and probably far off from correct formula
663 if (is_4pole(v
->layer
))
664 resonance
= sqrt(resonance
/ 0.707) * 0.5;
669 cbox_biquadf_set_lp_rbj(&v
->filter_coeffs
, cutoff
, resonance
, m
->module
.srate
);
673 cbox_biquadf_set_hp_rbj(&v
->filter_coeffs
, cutoff
, resonance
, m
->module
.srate
);
677 cbox_biquadf_set_bp_rbj(&v
->filter_coeffs
, cutoff
, resonance
, m
->module
.srate
);
680 cbox_biquadf_set_1plp(&v
->filter_coeffs
, cutoff
, m
->module
.srate
);
683 cbox_biquadf_set_1php(&v
->filter_coeffs
, cutoff
, m
->module
.srate
);
690 float left
[CBOX_BLOCK_SIZE
], right
[CBOX_BLOCK_SIZE
];
691 float *tmp_outputs
[2] = {left
, right
};
692 uint32_t samples
= 0;
693 uint32_t pos
= v
->gen
.pos
;
694 samples
= sampler_gen_sample_playback(&v
->gen
, tmp_outputs
);
695 if (v
->layer
->count
&& v
->gen
.pos
< pos
)
698 for (int i
= samples
; i
< CBOX_BLOCK_SIZE
; i
++)
699 left
[i
] = right
[i
] = 0.f
;
702 cbox_biquadf_process(&v
->filter_left
, &v
->filter_coeffs
, left
);
703 if (is_4pole(v
->layer
))
704 cbox_biquadf_process(&v
->filter_left2
, &v
->filter_coeffs
, left
);
705 cbox_biquadf_process(&v
->filter_right
, &v
->filter_coeffs
, right
);
706 if (is_4pole(v
->layer
))
707 cbox_biquadf_process(&v
->filter_right2
, &v
->filter_coeffs
, right
);
709 mix_block_into_with_gain(outputs
, v
->output_pair_no
* 2, left
, right
, 1.0);
710 if ((v
->send1bus
> 0 && v
->send1gain
!= 0) || (v
->send2bus
> 0 && v
->send2gain
!= 0))
712 if (v
->send1bus
> 0 && v
->send1gain
!= 0)
714 int oofs
= m
->module
.aux_offset
+ (v
->send1bus
- 1) * 2;
715 mix_block_into_with_gain(outputs
, oofs
, left
, right
, v
->send1gain
);
717 if (v
->send2bus
> 0 && v
->send2gain
!= 0)
719 int oofs
= m
->module
.aux_offset
+ (v
->send2bus
- 1) * 2;
720 mix_block_into_with_gain(outputs
, oofs
, left
, right
, v
->send2gain
);
723 if (v
->gen
.mode
== spt_inactive
)
724 sampler_voice_inactivate(v
, FALSE
);
727 void sampler_process_block(struct cbox_module
*module
, cbox_sample_t
**inputs
, cbox_sample_t
**outputs
)
729 struct sampler_module
*m
= (struct sampler_module
*)module
;
731 //float channels[2][CBOX_BLOCK_SIZE];
733 for (int c
= 0; c
< m
->output_pairs
+ m
->aux_pairs
; c
++)
736 for (int i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
737 outputs
[oo
][i
] = outputs
[oo
+ 1][i
] = 0.f
;
740 int vcount
= 0, vrel
= 0;
741 for (int i
= 0; i
< 16; i
++)
744 FOREACH_VOICE(m
->channels
[i
].voices_running
, v
)
746 sampler_voice_process(v
, m
, outputs
);
748 if (v
->amp_env
.cur_stage
== 15)
752 m
->channels
[i
].active_voices
= cvcount
;
755 m
->active_voices
= vcount
;
756 if (vcount
- vrel
> m
->max_voices
)
757 sampler_steal_voice(m
);
759 m
->current_time
+= CBOX_BLOCK_SIZE
;
762 void sampler_process_cc(struct sampler_module
*m
, struct sampler_channel
*c
, int cc
, int val
)
764 // Handle CC triggering.
765 if (c
->program
&& c
->program
->rll
&& c
->program
->rll
->layers_oncc
&& m
->voices_free
)
767 struct sampler_rll
*rll
= c
->program
->rll
;
768 if (!(rll
->cc_trigger_bitmask
[cc
>> 5] & (1 << (cc
& 31))))
770 int old_value
= c
->cc
[cc
];
771 for (GSList
*p
= rll
->layers_oncc
; p
; p
= p
->next
)
773 struct sampler_layer
*layer
= p
->data
;
774 assert(layer
->runtime
);
775 // Only trigger on transition between 'out of range' and 'in range' values.
776 // XXXKF I'm not sure if it's what is expected here, but don't have
777 // the reference implementation handy.
778 if (layer
->runtime
->on_cc_number
== cc
&&
779 (val
>= layer
->runtime
->on_locc
&& val
<= layer
->runtime
->on_hicc
) &&
780 !(old_value
>= layer
->runtime
->on_locc
&& old_value
<= layer
->runtime
->on_hicc
))
782 struct sampler_voice
*v
= m
->voices_free
;
783 int exgroups
[MAX_RELEASED_GROUPS
], exgroupcount
= 0;
784 sampler_start_voice(m
, c
, v
, layer
->runtime
, layer
->runtime
->pitch_keycenter
, 127, exgroups
, &exgroupcount
);
785 sampler_release_groups(m
, c
, -1, exgroups
, exgroupcount
);
789 int was_enabled
= c
->cc
[cc
] >= 64;
790 int enabled
= val
>= 64;
794 if (was_enabled
&& !enabled
)
796 sampler_stop_sustained(m
, c
);
800 if (was_enabled
&& !enabled
)
801 sampler_stop_sostenuto(m
, c
);
802 else if (!was_enabled
&& enabled
)
803 sampler_capture_sostenuto(m
, c
);
808 sampler_stop_all(m
, c
);
811 // Recommended Practice (RP-015) Response to Reset All Controllers
812 // http://www.midi.org/techspecs/rp15.php
813 sampler_process_cc(m
, c
, 64, 0);
814 sampler_process_cc(m
, c
, 66, 0);
818 c
->cc
[smsrc_chanaft
] = 0;
819 // XXXKF reset polyphonic pressure values when supported
826 void sampler_program_change_byidx(struct sampler_module
*m
, struct sampler_channel
*c
, int program_idx
)
828 sampler_channel_set_program(c
, m
->programs
[program_idx
]);
831 void sampler_program_change(struct sampler_module
*m
, struct sampler_channel
*c
, int program
)
833 // XXXKF replace with something more efficient
834 for (int i
= 0; i
< m
->program_count
; i
++)
836 // XXXKF support banks
837 if (m
->programs
[i
]->prog_no
== program
)
839 sampler_program_change_byidx(m
, c
, i
);
843 g_warning("Unknown program %d", program
);
844 sampler_program_change_byidx(m
, c
, 0);
847 void sampler_process_event(struct cbox_module
*module
, const uint8_t *data
, uint32_t len
)
849 struct sampler_module
*m
= (struct sampler_module
*)module
;
852 int cmd
= data
[0] >> 4;
853 int chn
= data
[0] & 15;
854 struct sampler_channel
*c
= &m
->channels
[chn
];
858 sampler_stop_note(m
, c
, data
[1], data
[2], FALSE
);
863 sampler_start_note(m
, c
, data
[1], data
[2], FALSE
);
865 sampler_stop_note(m
, c
, data
[1], data
[2], FALSE
);
869 // handle chokeable one shot layers
871 sampler_stop_note(m
, c
, data
[1], data
[2], TRUE
);
872 // polyphonic pressure not handled
876 sampler_process_cc(m
, c
, data
[1], data
[2]);
880 sampler_program_change(m
, c
, data
[1]);
884 c
->cc
[smsrc_chanaft
] = data
[1];
888 c
->pitchwheel
= data
[1] + 128 * data
[2] - 8192;
895 static void init_channel(struct sampler_module
*m
, struct sampler_channel
*c
)
898 c
->voices_running
= NULL
;
899 c
->active_voices
= 0;
901 memset(c
->cc
, 0, sizeof(c
->cc
));
907 c
->previous_note
= -1;
909 sampler_channel_set_program(c
, m
->program_count
? m
->programs
[0] : NULL
);
910 memset(c
->switchmask
, 0, sizeof(c
->switchmask
));
911 memset(c
->sustainmask
, 0, sizeof(c
->sustainmask
));
912 memset(c
->sostenutomask
, 0, sizeof(c
->sostenutomask
));
915 void sampler_channel_set_program(struct sampler_channel
*c
, struct sampler_program
*prg
)
918 c
->program
->in_use
--;
922 for(GSList
*p
= prg
->ctrl_init_list
; p
; p
= p
->next
)
924 union sampler_ctrlinit_union u
;
926 // printf("Setting controller %d -> %d\n", u.cinit.controller, u.cinit.value);
927 c
->cc
[u
.cinit
.controller
] = u
.cinit
.value
;
929 c
->program
->in_use
++;
933 static int get_first_free_program_no(struct sampler_module
*m
)
938 // XXXKF this has a N-squared complexity - but I'm not seeing
939 // this being used with more than 10 programs at the same time
940 // in the near future
944 for (int i
= 0; i
< m
->program_count
; i
++)
946 if (m
->programs
[i
]->prog_no
== prog_no
)
957 static int find_program(struct sampler_module
*m
, int prog_no
)
959 for (int i
= 0; i
< m
->program_count
; i
++)
961 if (m
->programs
[i
]->prog_no
== prog_no
)
967 struct release_program_voices_data
969 struct sampler_module
*module
;
971 struct sampler_program
*old_pgm
, *new_pgm
;
974 static int release_program_voices_execute(void *data
)
976 struct release_program_voices_data
*rpv
= data
;
977 struct sampler_module
*m
= rpv
->module
;
980 for (int i
= 0; i
< 16; i
++)
982 struct sampler_channel
*c
= &m
->channels
[i
];
983 if (c
->program
== rpv
->old_pgm
|| c
->program
== NULL
)
985 sampler_channel_set_program(c
, rpv
->new_pgm
);
986 FOREACH_VOICE(c
->voices_running
, v
)
990 if (v
->amp_env
.cur_stage
!= 15)
993 cbox_envelope_go_to(&v
->amp_env
, 15);
1002 static void swap_program(struct sampler_module
*m
, int index
, struct sampler_program
*pgm
, gboolean delete_old
)
1004 static struct cbox_rt_cmd_definition release_program_voices
= { NULL
, release_program_voices_execute
, NULL
};
1006 struct sampler_program
*old_program
= NULL
;
1008 old_program
= cbox_rt_swap_pointers(m
->module
.rt
, (void **)&m
->programs
[index
], pgm
);
1010 cbox_rt_array_remove(m
->module
.rt
, (void ***)&m
->programs
, &m
->program_count
, index
);
1012 struct release_program_voices_data data
= {m
, old_program
, pgm
};
1014 cbox_rt_execute_cmd_sync(m
->module
.rt
, &release_program_voices
, &data
);
1016 if (delete_old
&& old_program
)
1017 CBOX_DELETE(old_program
);
1020 static gboolean
load_program_at(struct sampler_module
*m
, const char *cfg_section
, const char *name
, int prog_no
, struct sampler_program
**ppgm
, GError
**error
)
1022 struct sampler_program
*pgm
= NULL
;
1023 int index
= find_program(m
, prog_no
);
1024 pgm
= sampler_program_new_from_cfg(m
, cfg_section
, name
, prog_no
, error
);
1030 swap_program(m
, index
, pgm
, TRUE
);
1034 struct sampler_program
**programs
= malloc(sizeof(struct sampler_program
*) * (m
->program_count
+ 1));
1035 memcpy(programs
, m
->programs
, sizeof(struct sampler_program
*) * m
->program_count
);
1036 programs
[m
->program_count
] = pgm
;
1039 free(cbox_rt_swap_pointers_and_update_count(m
->module
.rt
, (void **)&m
->programs
, programs
, &m
->program_count
, m
->program_count
+ 1));
1043 void sampler_unselect_program(struct sampler_module
*m
, struct sampler_program
*prg
)
1045 // Ensure no new notes are played on that program
1046 prg
->deleting
= TRUE
;
1047 // Remove from the list of available programs, so that it cannot be selected again
1048 for (int i
= 0; i
< m
->program_count
; i
++)
1050 if (m
->programs
[i
] == prg
)
1051 swap_program(m
, i
, NULL
, FALSE
);
1055 static gboolean
load_from_string(struct sampler_module
*m
, const char *sample_dir
, const char *sfz_data
, const char *name
, int prog_no
, struct sampler_program
**ppgm
, GError
**error
)
1057 int index
= find_program(m
, prog_no
);
1058 struct sampler_program
*pgm
= sampler_program_new(m
, prog_no
, name
, sample_dir
);
1059 pgm
->source_file
= g_strdup("string");
1060 if (!sampler_module_load_program_sfz(m
, pgm
, sfz_data
, TRUE
, error
))
1068 swap_program(m
, index
, pgm
, TRUE
);
1072 struct sampler_program
**programs
= calloc((m
->program_count
+ 1), sizeof(struct sampler_program
*));
1073 memcpy(programs
, m
->programs
, sizeof(struct sampler_program
*) * m
->program_count
);
1074 programs
[m
->program_count
] = pgm
;
1077 free(cbox_rt_swap_pointers_and_update_count(m
->module
.rt
, (void **)&m
->programs
, programs
, &m
->program_count
, m
->program_count
+ 1));
1081 gboolean
sampler_process_cmd(struct cbox_command_target
*ct
, struct cbox_command_target
*fb
, struct cbox_osc_command
*cmd
, GError
**error
)
1083 struct sampler_module
*m
= (struct sampler_module
*)ct
->user_data
;
1085 if (!strcmp(cmd
->command
, "/status") && !strcmp(cmd
->arg_types
, ""))
1087 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
1089 for (int i
= 0; i
< 16; i
++)
1091 struct sampler_channel
*channel
= &m
->channels
[i
];
1093 if (channel
->program
)
1094 result
= cbox_execute_on(fb
, NULL
, "/patch", "iis", error
, i
+ 1, channel
->program
->prog_no
, channel
->program
->name
);
1096 result
= cbox_execute_on(fb
, NULL
, "/patch", "iis", error
, i
+ 1, -1, "");
1099 if (!(cbox_execute_on(fb
, NULL
, "/channel_voices", "ii", error
, i
+ 1, channel
->active_voices
) &&
1100 cbox_execute_on(fb
, NULL
, "/volume", "ii", error
, i
+ 1, addcc(channel
, 7)) &&
1101 cbox_execute_on(fb
, NULL
, "/pan", "ii", error
, i
+ 1, addcc(channel
, 10))))
1105 return cbox_execute_on(fb
, NULL
, "/active_voices", "i", error
, m
->active_voices
) &&
1106 cbox_execute_on(fb
, NULL
, "/polyphony", "i", error
, MAX_SAMPLER_VOICES
) &&
1107 CBOX_OBJECT_DEFAULT_STATUS(&m
->module
, fb
, error
);
1110 if (!strcmp(cmd
->command
, "/patches") && !strcmp(cmd
->arg_types
, ""))
1112 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
1114 for (int i
= 0; i
< m
->program_count
; i
++)
1116 struct sampler_program
*prog
= m
->programs
[i
];
1117 if (!cbox_execute_on(fb
, NULL
, "/patch", "isoi", error
, prog
->prog_no
, prog
->name
, prog
, prog
->in_use
))
1122 else if (!strcmp(cmd
->command
, "/polyphony") && !strcmp(cmd
->arg_types
, "i"))
1124 int polyphony
= CBOX_ARG_I(cmd
, 0);
1125 if (polyphony
< 1 || polyphony
> MAX_SAMPLER_VOICES
)
1127 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Invalid polyphony %d (must be between 1 and %d)", polyphony
, (int)MAX_SAMPLER_VOICES
);
1130 m
->max_voices
= polyphony
;
1133 else if (!strcmp(cmd
->command
, "/set_patch") && !strcmp(cmd
->arg_types
, "ii"))
1135 int channel
= CBOX_ARG_I(cmd
, 0);
1136 if (channel
< 1 || channel
> 16)
1138 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Invalid channel %d", channel
);
1141 int value
= CBOX_ARG_I(cmd
, 1);
1142 struct sampler_program
*pgm
= NULL
;
1143 for (int i
= 0; i
< m
->program_count
; i
++)
1145 if (m
->programs
[i
]->prog_no
== value
)
1147 pgm
= m
->programs
[i
];
1151 cbox_rt_swap_pointers(m
->module
.rt
, (void **)&m
->channels
[channel
- 1].program
, pgm
);
1154 else if (!strcmp(cmd
->command
, "/load_patch") && !strcmp(cmd
->arg_types
, "iss"))
1156 struct sampler_program
*pgm
= NULL
;
1157 if (!load_program_at(m
, CBOX_ARG_S(cmd
, 1), CBOX_ARG_S(cmd
, 2), CBOX_ARG_I(cmd
, 0), &pgm
, error
))
1160 return cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, pgm
);
1163 else if (!strcmp(cmd
->command
, "/load_patch_from_file") && !strcmp(cmd
->arg_types
, "iss"))
1165 struct sampler_program
*pgm
= NULL
;
1166 char *cfg_section
= g_strdup_printf("spgm:!%s", CBOX_ARG_S(cmd
, 1));
1167 gboolean res
= load_program_at(m
, cfg_section
, CBOX_ARG_S(cmd
, 2), CBOX_ARG_I(cmd
, 0), &pgm
, error
);
1168 g_free(cfg_section
);
1169 if (res
&& pgm
&& fb
)
1170 return cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, pgm
);
1173 else if (!strcmp(cmd
->command
, "/load_patch_from_string") && !strcmp(cmd
->arg_types
, "isss"))
1175 struct sampler_program
*pgm
= NULL
;
1176 if (!load_from_string(m
, CBOX_ARG_S(cmd
, 1), CBOX_ARG_S(cmd
, 2), CBOX_ARG_S(cmd
, 3), CBOX_ARG_I(cmd
, 0), &pgm
, error
))
1179 return cbox_execute_on(fb
, NULL
, "/uuid", "o", error
, pgm
);
1182 else if (!strcmp(cmd
->command
, "/get_unused_program") && !strcmp(cmd
->arg_types
, ""))
1184 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
1186 return cbox_execute_on(fb
, NULL
, "/program_no", "i", error
, get_first_free_program_no(m
));
1189 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
1193 gboolean
sampler_select_program(struct sampler_module
*m
, int channel
, const gchar
*preset
, GError
**error
)
1195 for (int i
= 0; i
< m
->program_count
; i
++)
1197 if (!strcmp(m
->programs
[i
]->name
, preset
))
1199 sampler_program_change_byidx(m
, &m
->channels
[channel
], i
);
1203 g_set_error(error
, CBOX_MODULE_ERROR
, CBOX_MODULE_ERROR_FAILED
, "Preset not found: %s", preset
);
1207 MODULE_CREATE_FUNCTION(sampler
)
1210 static int inited
= 0;
1213 for (int i
= 0; i
< 2049; i
++)
1214 sine_wave
[i
] = sin(i
* M_PI
/ 1024.0);
1218 int max_voices
= cbox_config_get_int(cfg_section
, "polyphony", MAX_SAMPLER_VOICES
);
1219 if (max_voices
< 1 || max_voices
> MAX_SAMPLER_VOICES
)
1221 g_set_error(error
, CBOX_SAMPLER_ERROR
, CBOX_SAMPLER_ERROR_INVALID_LAYER
, "%s: invalid polyphony value", cfg_section
);
1224 int output_pairs
= cbox_config_get_int(cfg_section
, "output_pairs", 1);
1225 if (output_pairs
< 1 || output_pairs
> 16)
1227 g_set_error(error
, CBOX_SAMPLER_ERROR
, CBOX_SAMPLER_ERROR_INVALID_LAYER
, "%s: invalid output pairs value", cfg_section
);
1230 int aux_pairs
= cbox_config_get_int(cfg_section
, "aux_pairs", 0);
1231 if (aux_pairs
< 0 || aux_pairs
> 4)
1233 g_set_error(error
, CBOX_SAMPLER_ERROR
, CBOX_SAMPLER_ERROR_INVALID_LAYER
, "%s: invalid aux pairs value", cfg_section
);
1237 struct sampler_module
*m
= calloc(1, sizeof(struct sampler_module
));
1238 CALL_MODULE_INIT(m
, 0, (output_pairs
+ aux_pairs
) * 2, sampler
);
1239 m
->output_pairs
= output_pairs
;
1240 m
->aux_pairs
= aux_pairs
;
1241 m
->module
.aux_offset
= m
->output_pairs
* 2;
1242 m
->module
.process_event
= sampler_process_event
;
1243 m
->module
.process_block
= sampler_process_block
;
1245 m
->max_voices
= max_voices
;
1247 m
->deleting
= FALSE
;
1251 gchar
*s
= g_strdup_printf("program%d", i
);
1252 char *p
= cbox_config_get_string(cfg_section
, s
);
1257 m
->program_count
= i
;
1261 m
->programs
= calloc(m
->program_count
, sizeof(struct sampler_program
*));
1263 for (i
= 0; i
< m
->program_count
; i
++)
1265 gchar
*s
= g_strdup_printf("program%d", i
);
1266 char *pgm_section
= NULL
;
1268 const char *pgm_name
= cbox_config_get_string(cfg_section
, s
);
1270 char *at
= strchr(pgm_name
, '@');
1273 pgm_id
= atoi(at
+ 1);
1274 s
= g_strndup(pgm_name
, at
- pgm_name
);
1275 pgm_section
= g_strdup_printf("spgm:%s", s
);
1281 pgm_section
= g_strdup_printf("spgm:%s", pgm_name
);
1284 m
->programs
[i
] = sampler_program_new_from_cfg(m
, pgm_section
, pgm_section
+ 5, pgm_id
, error
);
1285 g_free(pgm_section
);
1286 if (!m
->programs
[i
])
1294 // XXXKF free programs/layers, first ensuring that they're fully initialised
1298 m
->voices_free
= NULL
;
1299 memset(m
->voices_all
, 0, sizeof(m
->voices_all
));
1300 for (i
= 0; i
< MAX_SAMPLER_VOICES
; i
++)
1302 struct sampler_voice
*v
= &m
->voices_all
[i
];
1303 v
->gen
.mode
= spt_inactive
;
1304 sampler_voice_link(&m
->voices_free
, v
);
1306 m
->active_voices
= 0;
1308 for (i
= 0; i
< 16; i
++)
1309 init_channel(m
, &m
->channels
[i
]);
1311 for (i
= 0; i
< 16; i
++)
1313 gchar
*key
= g_strdup_printf("channel%d", i
+ 1);
1314 gchar
*preset
= cbox_config_get_string(cfg_section
, key
);
1317 if (!sampler_select_program(m
, i
, preset
, error
))
1319 CBOX_DELETE(&m
->module
);
1330 void sampler_destroyfunc(struct cbox_module
*module
)
1332 struct sampler_module
*m
= (struct sampler_module
*)module
;
1335 for (int i
= 0; i
< m
->program_count
;)
1338 CBOX_DELETE(m
->programs
[i
]);
1345 #define MAKE_TO_STRING_CONTENT(name, v) \
1346 case v: return name;
1348 #define MAKE_FROM_STRING_CONTENT(n, v) \
1349 if (!strcmp(name, n)) { *value = v; return TRUE; }
1351 #define MAKE_FROM_TO_STRING(enumtype) \
1352 const char *enumtype##_to_string(enum enumtype value) \
1355 ENUM_VALUES_##enumtype(MAKE_TO_STRING_CONTENT) \
1356 default: return NULL; \
1360 gboolean enumtype##_from_string(const char *name, enum enumtype *value) \
1362 ENUM_VALUES_##enumtype(MAKE_FROM_STRING_CONTENT) \
1366 ENUM_LIST(MAKE_FROM_TO_STRING
)
1368 //////////////////////////////////////////////////////////////////////////
1370 struct sampler_update_layer_cmd
1372 struct sampler_module
*module
;
1373 struct sampler_layer
*layer
;
1374 struct sampler_layer_data
*new_data
;
1375 struct sampler_layer_data
*old_data
;
1378 static int sampler_update_layer_cmd_prepare(void *data
)
1380 struct sampler_update_layer_cmd
*cmd
= data
;
1381 cmd
->old_data
= cmd
->layer
->runtime
;
1382 cmd
->new_data
= calloc(1, sizeof(struct sampler_layer_data
));
1384 sampler_layer_data_clone(cmd
->new_data
, &cmd
->layer
->data
, TRUE
);
1385 sampler_layer_data_finalize(cmd
->new_data
, cmd
->layer
->parent_group
? &cmd
->layer
->parent_group
->data
: NULL
, cmd
->module
);
1386 if (cmd
->layer
->runtime
== NULL
)
1388 // initial update of the layer, so none of the voices need updating yet
1389 // because the layer hasn't been allocated to any voice
1390 cmd
->layer
->runtime
= cmd
->new_data
;
1396 static int sampler_update_layer_cmd_execute(void *data
)
1398 struct sampler_update_layer_cmd
*cmd
= data
;
1400 for (int i
= 0; i
< 16; i
++)
1402 FOREACH_VOICE(cmd
->module
->channels
[i
].voices_running
, v
)
1404 if (v
->layer
== cmd
->old_data
)
1405 v
->layer
= cmd
->new_data
;
1408 cmd
->layer
->runtime
= cmd
->new_data
;
1412 static void sampler_update_layer_cmd_cleanup(void *data
)
1414 struct sampler_update_layer_cmd
*cmd
= data
;
1416 sampler_layer_data_destroy(cmd
->old_data
);
1419 void sampler_update_layer(struct sampler_module
*m
, struct sampler_layer
*l
)
1421 // if changing a group, update all child regions instead
1422 if (g_hash_table_size(l
->child_layers
))
1424 GHashTableIter iter
;
1425 g_hash_table_iter_init(&iter
, l
->child_layers
);
1426 gpointer key
, value
;
1427 while(g_hash_table_iter_next(&iter
, &key
, &value
))
1429 sampler_layer_data_finalize(&((struct sampler_layer
*)key
)->data
, &l
->data
, m
);
1430 sampler_update_layer(m
, (struct sampler_layer
*)key
);
1434 static struct cbox_rt_cmd_definition rtcmd
= {
1435 .prepare
= sampler_update_layer_cmd_prepare
,
1436 .execute
= sampler_update_layer_cmd_execute
,
1437 .cleanup
= sampler_update_layer_cmd_cleanup
,
1440 struct sampler_update_layer_cmd lcmd
;
1443 lcmd
.new_data
= NULL
;
1444 lcmd
.old_data
= NULL
;
1446 // In order to be able to use the async call, it would be necessary to
1447 // identify old data by layer pointer, not layer data pointer. For now,
1448 // it might be good enough to just use sync calls for this.
1449 cbox_rt_execute_cmd_sync(m
->module
.rt
, &rtcmd
, &lcmd
);
1452 void sampler_update_program_layers(struct sampler_module
*m
, struct sampler_program
*prg
)
1454 struct sampler_rll
*new_rll
= sampler_rll_new_from_program(prg
);
1455 struct sampler_rll
*old_rll
= cbox_rt_swap_pointers(m
->module
.rt
, (void **)&prg
->rll
, new_rll
);
1457 sampler_rll_destroy(old_rll
);
1460 //////////////////////////////////////////////////////////////////////////
1461 // Note initialisation functions
1463 void sampler_nif_vel2pitch(struct sampler_noteinitfunc
*nif
, struct sampler_voice
*v
)
1465 v
->pitch_shift
+= nif
->param
* v
->vel
* (1.0 / 127.0);
1468 void sampler_nif_cc2delay(struct sampler_noteinitfunc
*nif
, struct sampler_voice
*v
)
1470 v
->delay
+= nif
->param
* v
->channel
->cc
[nif
->variant
] * (1.0 / 127.0) * v
->channel
->module
->module
.srate
;
1473 void sampler_nif_addrandom(struct sampler_noteinitfunc
*nif
, struct sampler_voice
*v
)
1475 float rnd
= rand() * 1.0 / RAND_MAX
;
1476 switch(nif
->variant
)
1479 v
->gain_shift
+= rnd
* nif
->param
;
1482 v
->cutoff_shift
+= rnd
* nif
->param
;
1485 v
->pitch_shift
+= rnd
* nif
->param
; // this is in cents
1490 void sampler_nif_vel2env(struct sampler_noteinitfunc
*nif
, struct sampler_voice
*v
)
1492 int env_type
= (nif
->variant
) >> 4;
1493 struct cbox_envelope
*env
= NULL
;
1500 env
= &v
->filter_env
;
1503 env
= &v
->pitch_env
;
1508 if (env
->shape
!= &v
->dyn_envs
[env_type
])
1510 memcpy(&v
->dyn_envs
[env_type
], env
->shape
, sizeof(struct cbox_envelope_shape
));
1511 env
->shape
= &v
->dyn_envs
[env_type
];
1513 float param
= nif
->param
* v
->vel
* (1.0 / 127.0);
1514 if ((nif
->variant
& 15) == 4)
1516 cbox_envelope_modify_dahdsr(env
->shape
, nif
->variant
& 15, param
, v
->channel
->module
->module
.srate
* (1.0 / CBOX_BLOCK_SIZE
));
1519 //////////////////////////////////////////////////////////////////////////
1521 struct cbox_module_livecontroller_metadata sampler_controllers
[] = {
1524 struct cbox_module_keyrange_metadata sampler_keyranges
[] = {
1527 DEFINE_MODULE(sampler
, 0, 2)