Merge pull request #1 from atsampson/master
[calfbox.git] / sampler.c
blob8e35e9bca3edf8fac771d468471bea12b58c98bb
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-api.h"
20 #include "dspmath.h"
21 #include "errors.h"
22 #include "midi.h"
23 #include "module.h"
24 #include "rt.h"
25 #include "sampler.h"
26 #include "sampler_impl.h"
27 #include "sfzloader.h"
28 #include "stm.h"
29 #include <assert.h>
30 #include <errno.h>
31 #include <glib.h>
32 #include <math.h>
33 #include <memory.h>
34 #include <sndfile.h>
35 #include <stdio.h>
36 #include <stdlib.h>
38 float sampler_sine_wave[2049];
40 GQuark cbox_sampler_error_quark()
42 return g_quark_from_string("cbox-sampler-error-quark");
45 static void sampler_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs);
46 static void sampler_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len);
47 static void sampler_destroyfunc(struct cbox_module *module);
49 void sampler_steal_voice(struct sampler_module *m)
51 int max_age = 0;
52 struct sampler_voice *voice_found = NULL;
53 for (int i = 0; i < 16; i++)
55 FOREACH_VOICE(m->channels[i].voices_running, v)
57 if (v->amp_env.cur_stage == 15)
58 continue;
59 int age = m->serial_no - v->serial_no;
60 if (v->gen.loop_start == -1)
61 age += (int)((v->gen.bigpos >> 32) * 100.0 / v->gen.cur_sample_end);
62 else
63 if (v->released)
64 age += 10;
65 if (age > max_age)
67 max_age = age;
68 voice_found = v;
72 if (voice_found)
74 voice_found->released = 1;
75 cbox_envelope_go_to(&voice_found->amp_env, 15);
79 static inline float clip01(float v)
81 if (v < 0.f)
82 return 0;
83 if (v > 1.f)
84 return 1;
85 return v;
88 void sampler_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
90 struct sampler_module *m = (struct sampler_module *)module;
92 //float channels[2][CBOX_BLOCK_SIZE];
94 for (int c = 0; c < m->output_pairs + m->aux_pairs; c++)
96 int oo = 2 * c;
97 for (int i = 0; i < CBOX_BLOCK_SIZE; i++)
98 outputs[oo][i] = outputs[oo + 1][i] = 0.f;
101 int vcount = 0, vrel = 0;
102 for (int i = 0; i < 16; i++)
104 int cvcount = 0;
105 FOREACH_VOICE(m->channels[i].voices_running, v)
107 sampler_voice_process(v, m, outputs);
109 if (v->amp_env.cur_stage == 15)
110 vrel++;
111 cvcount++;
113 m->channels[i].active_voices = cvcount;
114 vcount += cvcount;
116 m->active_voices = vcount;
117 if (vcount - vrel > m->max_voices)
118 sampler_steal_voice(m);
119 m->serial_no++;
120 m->current_time += CBOX_BLOCK_SIZE;
123 void sampler_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
125 struct sampler_module *m = (struct sampler_module *)module;
126 if (len > 0)
128 int cmd = data[0] >> 4;
129 int chn = data[0] & 15;
130 struct sampler_channel *c = &m->channels[chn];
131 switch(cmd)
133 case 8:
134 sampler_channel_stop_note(c, data[1], data[2], FALSE);
135 break;
137 case 9:
138 if (data[2] > 0)
139 sampler_channel_start_note(c, data[1], data[2], FALSE);
140 else
141 sampler_channel_stop_note(c, data[1], data[2], FALSE);
142 break;
144 case 10:
145 // handle chokeable one shot layers
146 if (data[2] == 127)
147 sampler_channel_stop_note(c, data[1], data[2], TRUE);
148 // polyphonic pressure not handled
149 break;
151 case 11:
152 sampler_channel_process_cc(c, data[1], data[2]);
153 break;
155 case 12:
156 sampler_channel_program_change(c, data[1]);
157 break;
159 case 13:
160 c->cc[smsrc_chanaft] = data[1];
161 break;
163 case 14:
164 c->pitchwheel = data[1] + 128 * data[2] - 8192;
165 break;
171 static int get_first_free_program_no(struct sampler_module *m)
173 int prog_no = -1;
174 gboolean found;
176 // XXXKF this has a N-squared complexity - but I'm not seeing
177 // this being used with more than 10 programs at the same time
178 // in the near future
179 do {
180 prog_no++;
181 found = FALSE;
182 for (int i = 0; i < m->program_count; i++)
184 if (m->programs[i]->prog_no == prog_no)
186 found = TRUE;
187 break;
190 } while(found);
192 return prog_no;
195 static int find_program(struct sampler_module *m, int prog_no)
197 for (int i = 0; i < m->program_count; i++)
199 if (m->programs[i]->prog_no == prog_no)
200 return i;
202 return -1;
205 struct release_program_voices_data
207 struct sampler_module *module;
209 struct sampler_program *old_pgm, *new_pgm;
210 uint16_t channels_to_wait_for;
213 static int release_program_voices_execute(void *data)
215 struct release_program_voices_data *rpv = data;
216 struct sampler_module *m = rpv->module;
217 int finished = 1;
219 for (int i = 0; i < 16; i++)
221 uint16_t mask = 1 << i;
222 struct sampler_channel *c = &m->channels[i];
223 if (c->program == rpv->old_pgm || c->program == NULL)
225 sampler_channel_set_program_RT(c, rpv->new_pgm);
226 rpv->channels_to_wait_for |= mask;
228 if (rpv->channels_to_wait_for & mask)
230 FOREACH_VOICE(c->voices_running, v)
232 if (m->deleting)
234 sampler_voice_inactivate(v, TRUE);
235 continue;
237 // This is a new voice, started after program change, so it
238 // should not be terminated and waited for.
239 if (v->program == rpv->new_pgm)
240 continue;
241 // The voice is still going, so repeat until it fades out
242 finished = 0;
243 // If not in final fadeout stage, force final fadeout.
244 if (v->amp_env.cur_stage != 15)
246 v->released = 1;
247 cbox_envelope_go_to(&v->amp_env, 15);
253 return finished;
256 static void swap_program(struct sampler_module *m, int index, struct sampler_program *pgm, gboolean delete_old)
258 static struct cbox_rt_cmd_definition release_program_voices = { NULL, release_program_voices_execute, NULL };
260 struct sampler_program *old_program = NULL;
261 if (pgm)
262 old_program = cbox_rt_swap_pointers(m->module.rt, (void **)&m->programs[index], pgm);
263 else
264 old_program = cbox_rt_array_remove(m->module.rt, (void ***)&m->programs, &m->program_count, index);
266 struct release_program_voices_data data = {m, old_program, pgm, 0};
268 cbox_rt_execute_cmd_sync(m->module.rt, &release_program_voices, &data);
270 if (delete_old && old_program)
271 CBOX_DELETE(old_program);
274 static void select_initial_program(struct sampler_module *m)
276 static struct cbox_rt_cmd_definition release_program_voices = { NULL, release_program_voices_execute, NULL };
277 struct release_program_voices_data data = {m, NULL, m->programs[0], 0};
278 cbox_rt_execute_cmd_sync(m->module.rt, &release_program_voices, &data);
281 void sampler_register_program(struct sampler_module *m, struct sampler_program *pgm)
283 struct sampler_program **programs = malloc(sizeof(struct sampler_program *) * (m->program_count + 1));
284 memcpy(programs, m->programs, sizeof(struct sampler_program *) * m->program_count);
285 programs[m->program_count] = pgm;
286 free(cbox_rt_swap_pointers_and_update_count(m->module.rt, (void **)&m->programs, programs, &m->program_count, m->program_count + 1));
287 if (m->program_count == 1)
288 select_initial_program(m);
291 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)
293 struct sampler_program *pgm = NULL;
294 int index = find_program(m, prog_no);
295 pgm = sampler_program_new_from_cfg(m, cfg_section, name, prog_no, error);
296 if (!pgm)
297 return FALSE;
299 if (index != -1)
301 swap_program(m, index, pgm, TRUE);
302 return TRUE;
305 sampler_register_program(m, pgm);
306 if (ppgm)
307 *ppgm = pgm;
308 return TRUE;
311 void sampler_unselect_program(struct sampler_module *m, struct sampler_program *prg)
313 // Ensure no new notes are played on that program
314 prg->deleting = TRUE;
315 // Remove from the list of available programs, so that it cannot be selected again
316 for (int i = 0; i < m->program_count; i++)
318 if (m->programs[i] == prg)
319 swap_program(m, i, NULL, FALSE);
323 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)
325 int index = find_program(m, prog_no);
326 struct sampler_program *pgm = sampler_program_new(m, prog_no, name, NULL, sample_dir, error);
327 if (!pgm)
328 return FALSE;
329 pgm->source_file = g_strdup("string");
330 if (!sampler_module_load_program_sfz(m, pgm, sfz_data, TRUE, error))
332 free(pgm);
333 return FALSE;
336 if (index != -1)
338 swap_program(m, index, pgm, TRUE);
339 if (ppgm)
340 *ppgm = pgm;
341 return TRUE;
344 struct sampler_program **programs = calloc((m->program_count + 1), sizeof(struct sampler_program *));
345 memcpy(programs, m->programs, sizeof(struct sampler_program *) * m->program_count);
346 programs[m->program_count] = pgm;
347 if (ppgm)
348 *ppgm = pgm;
349 free(cbox_rt_swap_pointers_and_update_count(m->module.rt, (void **)&m->programs, programs, &m->program_count, m->program_count + 1));
350 if (m->program_count == 1)
351 select_initial_program(m);
352 return TRUE;
355 gboolean sampler_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
357 struct sampler_module *m = (struct sampler_module *)ct->user_data;
359 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
361 if (!cbox_check_fb_channel(fb, cmd->command, error))
362 return FALSE;
363 for (int i = 0; i < 16; i++)
365 struct sampler_channel *channel = &m->channels[i];
366 gboolean result;
367 if (channel->program)
368 result = cbox_execute_on(fb, NULL, "/patch", "iis", error, i + 1, channel->program->prog_no, channel->program->name);
369 else
370 result = cbox_execute_on(fb, NULL, "/patch", "iis", error, i + 1, -1, "");
371 if (!result)
372 return FALSE;
373 if (!(cbox_execute_on(fb, NULL, "/channel_voices", "ii", error, i + 1, channel->active_voices) &&
374 cbox_execute_on(fb, NULL, "/output", "ii", error, i + 1, channel->output_shift) &&
375 cbox_execute_on(fb, NULL, "/volume", "ii", error, i + 1, sampler_channel_addcc(channel, 7)) &&
376 cbox_execute_on(fb, NULL, "/pan", "ii", error, i + 1, sampler_channel_addcc(channel, 10))))
377 return FALSE;
380 return cbox_execute_on(fb, NULL, "/active_voices", "i", error, m->active_voices) &&
381 cbox_execute_on(fb, NULL, "/active_pipes", "i", error, cbox_prefetch_stack_get_active_pipe_count(m->pipe_stack)) &&
382 cbox_execute_on(fb, NULL, "/polyphony", "i", error, m->max_voices) &&
383 CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error);
385 else
386 if (!strcmp(cmd->command, "/patches") && !strcmp(cmd->arg_types, ""))
388 if (!cbox_check_fb_channel(fb, cmd->command, error))
389 return FALSE;
390 for (int i = 0; i < m->program_count; i++)
392 struct sampler_program *prog = m->programs[i];
393 if (!cbox_execute_on(fb, NULL, "/patch", "isoi", error, prog->prog_no, prog->name, prog, prog->in_use))
394 return FALSE;
396 return TRUE;
398 else if (!strcmp(cmd->command, "/polyphony") && !strcmp(cmd->arg_types, "i"))
400 int polyphony = CBOX_ARG_I(cmd, 0);
401 if (polyphony < 1 || polyphony > MAX_SAMPLER_VOICES)
403 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);
404 return FALSE;
406 m->max_voices = polyphony;
407 return TRUE;
409 else if (!strcmp(cmd->command, "/set_patch") && !strcmp(cmd->arg_types, "ii"))
411 int channel = CBOX_ARG_I(cmd, 0);
412 if (channel < 1 || channel > 16)
414 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid channel %d", channel);
415 return FALSE;
417 int value = CBOX_ARG_I(cmd, 1);
418 struct sampler_program *pgm = NULL;
419 for (int i = 0; i < m->program_count; i++)
421 if (m->programs[i]->prog_no == value)
423 pgm = m->programs[i];
424 break;
427 sampler_channel_set_program(&m->channels[channel - 1], pgm);
428 return TRUE;
430 else if (!strcmp(cmd->command, "/set_output") && !strcmp(cmd->arg_types, "ii"))
432 int channel = CBOX_ARG_I(cmd, 0);
433 int output = CBOX_ARG_I(cmd, 1);
434 if (channel < 1 || channel > 16)
436 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid channel %d", channel);
437 return FALSE;
439 if (output < 0 || output >= m->output_pairs)
441 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Invalid output %d", output);
442 return FALSE;
444 m->channels[channel - 1].output_shift = output;
445 return TRUE;
447 else if (!strcmp(cmd->command, "/load_patch") && !strcmp(cmd->arg_types, "iss"))
449 struct sampler_program *pgm = NULL;
450 if (!load_program_at(m, CBOX_ARG_S(cmd, 1), CBOX_ARG_S(cmd, 2), CBOX_ARG_I(cmd, 0), &pgm, error))
451 return FALSE;
452 if (fb)
453 return cbox_execute_on(fb, NULL, "/uuid", "o", error, pgm);
454 return TRUE;
456 else if (!strcmp(cmd->command, "/load_patch_from_file") && !strcmp(cmd->arg_types, "iss"))
458 struct sampler_program *pgm = NULL;
459 char *cfg_section = g_strdup_printf("spgm:!%s", CBOX_ARG_S(cmd, 1));
460 gboolean res = load_program_at(m, cfg_section, CBOX_ARG_S(cmd, 2), CBOX_ARG_I(cmd, 0), &pgm, error);
461 g_free(cfg_section);
462 if (res && pgm && fb)
463 return cbox_execute_on(fb, NULL, "/uuid", "o", error, pgm);
464 return res;
466 else if (!strcmp(cmd->command, "/load_patch_from_string") && !strcmp(cmd->arg_types, "isss"))
468 struct sampler_program *pgm = NULL;
469 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))
470 return FALSE;
471 if (fb && pgm)
472 return cbox_execute_on(fb, NULL, "/uuid", "o", error, pgm);
473 return TRUE;
475 else if (!strcmp(cmd->command, "/get_unused_program") && !strcmp(cmd->arg_types, ""))
477 if (!cbox_check_fb_channel(fb, cmd->command, error))
478 return FALSE;
479 return cbox_execute_on(fb, NULL, "/program_no", "i", error, get_first_free_program_no(m));
481 else
482 return cbox_object_default_process_cmd(ct, fb, cmd, error);
483 return TRUE;
486 gboolean sampler_select_program(struct sampler_module *m, int channel, const gchar *preset, GError **error)
488 for (int i = 0; i < m->program_count; i++)
490 if (!strcmp(m->programs[i]->name, preset))
492 sampler_channel_set_program(&m->channels[channel], m->programs[i]);
493 return TRUE;
496 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Preset not found: %s", preset);
497 return FALSE;
500 MODULE_CREATE_FUNCTION(sampler)
502 int i;
503 static int inited = 0;
504 if (!inited)
506 for (int i = 0; i < 2049; i++)
507 sampler_sine_wave[i] = sin(i * M_PI / 1024.0);
508 inited = 1;
511 int max_voices = cbox_config_get_int(cfg_section, "polyphony", MAX_SAMPLER_VOICES);
512 if (max_voices < 1 || max_voices > MAX_SAMPLER_VOICES)
514 g_set_error(error, CBOX_SAMPLER_ERROR, CBOX_SAMPLER_ERROR_INVALID_LAYER, "%s: invalid polyphony value", cfg_section);
515 return NULL;
517 int output_pairs = cbox_config_get_int(cfg_section, "output_pairs", 1);
518 if (output_pairs < 1 || output_pairs > 16)
520 g_set_error(error, CBOX_SAMPLER_ERROR, CBOX_SAMPLER_ERROR_INVALID_LAYER, "%s: invalid output pairs value", cfg_section);
521 return NULL;
523 int aux_pairs = cbox_config_get_int(cfg_section, "aux_pairs", 0);
524 if (aux_pairs < 0 || aux_pairs > 4)
526 g_set_error(error, CBOX_SAMPLER_ERROR, CBOX_SAMPLER_ERROR_INVALID_LAYER, "%s: invalid aux pairs value", cfg_section);
527 return NULL;
530 struct sampler_module *m = calloc(1, sizeof(struct sampler_module));
531 CALL_MODULE_INIT(m, 0, (output_pairs + aux_pairs) * 2, sampler);
532 m->output_pairs = output_pairs;
533 m->aux_pairs = aux_pairs;
534 m->module.aux_offset = m->output_pairs * 2;
535 m->module.process_event = sampler_process_event;
536 m->module.process_block = sampler_process_block;
537 m->programs = NULL;
538 m->max_voices = max_voices;
539 m->serial_no = 0;
540 m->deleting = FALSE;
541 // XXXKF read defaults from some better place, like config
542 // XXXKF allow dynamic change of the number of the pipes
543 m->pipe_stack = cbox_prefetch_stack_new(MAX_SAMPLER_VOICES, cbox_config_get_int("streaming", "streambuf_size", 65536));
544 m->disable_mixer_controls = cbox_config_get_int("sampler", "disable_mixer_controls", 0);
546 float srate = m->module.srate;
547 for (i = 0; i < 12800; i++)
549 float freq = 440 * pow(2, (i - 5700) / 1200.0);
550 if (freq < 20.0)
551 freq = 20.0;
552 if (freq > srate * 0.45)
553 freq = srate * 0.45;
554 float omega=(float)(2*M_PI*freq/srate);
555 m->sincos[i].sine = sinf(omega);
556 m->sincos[i].cosine = cosf(omega);
557 m->sincos[i].prewarp = 2.0 * tan(hz2w(freq, srate) * 0.5f);
560 for (i = 0; ; i++)
562 gchar *s = g_strdup_printf("program%d", i);
563 char *p = cbox_config_get_string(cfg_section, s);
564 g_free(s);
566 if (!p)
568 m->program_count = i;
569 break;
572 m->programs = calloc(m->program_count, sizeof(struct sampler_program *));
573 int success = 1;
574 for (i = 0; i < m->program_count; i++)
576 gchar *s = g_strdup_printf("program%d", i);
577 char *pgm_section = NULL;
578 int pgm_id = -1;
579 const char *pgm_name = cbox_config_get_string(cfg_section, s);
580 g_free(s);
581 char *at = strchr(pgm_name, '@');
582 if (at)
584 pgm_id = atoi(at + 1);
585 s = g_strndup(pgm_name, at - pgm_name);
586 pgm_section = g_strdup_printf("spgm:%s", s);
587 g_free(s);
589 else
591 pgm_id = i;
592 pgm_section = g_strdup_printf("spgm:%s", pgm_name);
595 m->programs[i] = sampler_program_new_from_cfg(m, pgm_section, pgm_section + 5, pgm_id, error);
596 g_free(pgm_section);
597 if (!m->programs[i])
599 success = 0;
600 break;
603 if (!success)
605 // XXXKF free programs/layers, first ensuring that they're fully initialised
606 free(m);
607 return NULL;
609 m->voices_free = NULL;
610 memset(m->voices_all, 0, sizeof(m->voices_all));
611 for (i = 0; i < MAX_SAMPLER_VOICES; i++)
613 struct sampler_voice *v = &m->voices_all[i];
614 v->gen.mode = spt_inactive;
615 sampler_voice_link(&m->voices_free, v);
617 m->active_voices = 0;
619 for (i = 0; i < 16; i++)
620 sampler_channel_init(&m->channels[i], m);
622 for (i = 0; i < 16; i++)
624 gchar *key = g_strdup_printf("channel%d", i + 1);
625 gchar *preset = cbox_config_get_string(cfg_section, key);
626 if (preset)
628 if (!sampler_select_program(m, i, preset, error))
630 CBOX_DELETE(&m->module);
631 return NULL;
634 g_free(key);
635 key = g_strdup_printf("channel%d_output", i + 1);
636 m->channels[i].output_shift = cbox_config_get_int(cfg_section, key, 1) - 1;
637 g_free(key);
641 return &m->module;
644 void sampler_destroyfunc(struct cbox_module *module)
646 struct sampler_module *m = (struct sampler_module *)module;
647 int i;
648 m->deleting = TRUE;
650 for (i = 0; i < m->program_count;)
652 if (m->programs[i])
653 CBOX_DELETE(m->programs[i]);
654 else
655 i++;
657 for (i = 0; i < 16; i++)
659 assert (m->channels[i].voices_running == NULL);
661 cbox_prefetch_stack_destroy(m->pipe_stack);
662 free(m->programs);
665 #define MAKE_TO_STRING_CONTENT(name, v) \
666 case v: return name;
668 #define MAKE_FROM_STRING_CONTENT(n, v) \
669 if (!strcmp(name, n)) { *value = v; return TRUE; }
671 #define MAKE_FROM_TO_STRING(enumtype) \
672 const char *enumtype##_to_string(enum enumtype value) \
674 switch(value) { \
675 ENUM_VALUES_##enumtype(MAKE_TO_STRING_CONTENT) \
676 default: return NULL; \
680 gboolean enumtype##_from_string(const char *name, enum enumtype *value) \
682 ENUM_VALUES_##enumtype(MAKE_FROM_STRING_CONTENT) \
683 return FALSE; \
686 ENUM_LIST(MAKE_FROM_TO_STRING)
688 //////////////////////////////////////////////////////////////////////////
689 // Note initialisation functions
691 void sampler_nif_vel2pitch(struct sampler_noteinitfunc *nif, struct sampler_voice *v)
693 v->pitch_shift += nif->param * v->vel * (1.0 / 127.0);
696 void sampler_nif_vel2reloffset(struct sampler_noteinitfunc *nif, struct sampler_voice *v)
698 v->reloffset += nif->param * v->vel * (1.0 / 127.0);
701 void sampler_nif_cc2delay(struct sampler_noteinitfunc *nif, struct sampler_voice *v)
703 v->delay += nif->param * v->channel->cc[nif->variant] * (1.0 / 127.0) * v->channel->module->module.srate;
706 void sampler_nif_cc2reloffset(struct sampler_noteinitfunc *nif, struct sampler_voice *v)
708 v->reloffset += nif->param * v->channel->cc[nif->variant] * (1.0 / 127.0);
711 void sampler_nif_addrandom(struct sampler_noteinitfunc *nif, struct sampler_voice *v)
713 float rnd = rand() * 1.0 / RAND_MAX;
714 switch(nif->variant)
716 case 0:
717 v->gain_shift += rnd * nif->param;
718 break;
719 case 1:
720 v->cutoff_shift += rnd * nif->param;
721 break;
722 case 2:
723 v->pitch_shift += rnd * nif->param; // this is in cents
724 break;
728 void sampler_nif_vel2env(struct sampler_noteinitfunc *nif, struct sampler_voice *v)
730 int env_type = (nif->variant) >> 4;
731 struct cbox_envelope *env = NULL;
732 switch(env_type)
734 case 0:
735 env = &v->amp_env;
736 break;
737 case 1:
738 env = &v->filter_env;
739 break;
740 case 2:
741 env = &v->pitch_env;
742 break;
743 default:
744 assert(0);
746 if (env->shape != &v->dyn_envs[env_type])
748 memcpy(&v->dyn_envs[env_type], env->shape, sizeof(struct cbox_envelope_shape));
749 env->shape = &v->dyn_envs[env_type];
751 float param = nif->param * v->vel * (1.0 / 127.0);
752 if ((nif->variant & 15) == 4)
753 param *= 0.01;
754 cbox_envelope_modify_dahdsr(env->shape, nif->variant & 15, param, v->channel->module->module.srate * (1.0 / CBOX_BLOCK_SIZE));
757 //////////////////////////////////////////////////////////////////////////
759 struct cbox_module_livecontroller_metadata sampler_controllers[] = {
762 struct cbox_module_keyrange_metadata sampler_keyranges[] = {
765 DEFINE_MODULE(sampler, 0, 2)