diff --git a/plugins/puredata/sfizz_puredata.c b/plugins/puredata/sfizz_puredata.c index 89422f7a..16b97c6d 100644 --- a/plugins/puredata/sfizz_puredata.c +++ b/plugins/puredata/sfizz_puredata.c @@ -16,6 +16,10 @@ static t_class* cls_sfizz_tilde; typedef struct _sfizz_tilde { t_object obj; t_outlet* outputs[2]; + t_inlet* input_cc; + t_inlet* input_bend; + t_inlet* input_touch; + t_inlet* input_polytouch; sfizz_synth_t* synth; int midi[3]; int midinum; @@ -23,6 +27,20 @@ typedef struct _sfizz_tilde { char* filepath; } t_sfizz_tilde; +static t_float clamp01(t_float x) +{ + x = (x > 0) ? x : 0; + x = (x < 1) ? x : 1; + return x; +} + +static t_float clampB1(t_float x) +{ + x = (x > -1) ? x : -1; + x = (x < 1) ? x : 1; + return x; +} + static void sfizz_tilde_set_file(t_sfizz_tilde* self, const char* file) { const char* dir = self->dir->s_name; @@ -69,6 +87,11 @@ static void* sfizz_tilde_new(t_symbol* sym, int argc, t_atom argv[]) self->outputs[0] = outlet_new(&self->obj, &s_signal); self->outputs[1] = outlet_new(&self->obj, &s_signal); + self->input_cc = inlet_new(&self->obj, &self->obj.ob_pd, &s_float, gensym("cc")); + self->input_bend = inlet_new(&self->obj, &self->obj.ob_pd, &s_float, gensym("bend")); + self->input_touch = inlet_new(&self->obj, &self->obj.ob_pd, &s_float, gensym("touch")); + self->input_polytouch = inlet_new(&self->obj, &self->obj.ob_pd, &s_float, gensym("polytouch")); + sfizz_synth_t* synth = sfizz_create_synth(); self->synth = synth; @@ -94,6 +117,14 @@ static void sfizz_tilde_free(t_sfizz_tilde* self) outlet_free(self->outputs[0]); if (self->outputs[1]) outlet_free(self->outputs[1]); + if (self->input_cc) + inlet_free(self->input_cc); + if (self->input_bend) + inlet_free(self->input_bend); + if (self->input_touch) + inlet_free(self->input_touch); + if (self->input_polytouch) + inlet_free(self->input_polytouch); } static t_int* sfizz_tilde_perform(t_int* w) @@ -120,6 +151,22 @@ static void sfizz_tilde_dsp(t_sfizz_tilde* self, t_signal** sp) (t_int)sp[0]->s_vec, (t_int)sp[1]->s_vec, (t_int)sp[0]->s_n); } +static void sfizz_tilde_list(t_sfizz_tilde* self, t_symbol* sym, int argc, t_atom* argv) +{ + (void)sym; + + if (argc == 2 && argv[0].a_type == A_FLOAT && argv[1].a_type == A_FLOAT) { + int key = (int)argv[0].a_w.w_float; + if (key < 0 || key > 127) + return; + t_float vel = clamp01(argv[1].a_w.w_float / 127); + if (vel > 0) + sfizz_send_hd_note_on(self->synth, 0, key, vel); + else + sfizz_send_hd_note_off(self->synth, 0, key, 0); + } +} + static void sfizz_tilde_midiin(t_sfizz_tilde* self, t_float f) { int byte = (int)f; @@ -187,17 +234,55 @@ static void sfizz_tilde_reload(t_sfizz_tilde* self, t_float value) sfizz_tilde_do_load(self); } -static void sfizz_tilde_hdcc(t_sfizz_tilde* self, t_float cc, t_float value) +static void sfizz_tilde_hdcc(t_sfizz_tilde* self, t_float f1, t_float f2) { - sfizz_automate_hdcc(self->synth, 0, (int)cc, value); + int cc = (int)f1; + if (cc < 0 || cc > 127) + return; + sfizz_automate_hdcc(self->synth, 0, (int)cc, clamp01(f2)); } -static void sfizz_tilde_voices(t_sfizz_tilde* self, t_float value) +static void sfizz_tilde_cc(t_sfizz_tilde* self, t_float f1, t_float f2) { - int numvoices = (int)value; - if (numvoices < 1) - numvoices = 1; + sfizz_tilde_hdcc(self, f1, f2 / 127); +} +static void sfizz_tilde_hdbend(t_sfizz_tilde* self, t_float f1) +{ + sfizz_send_hd_pitch_wheel(self->synth, 0, clampB1(f1)); +} + +static void sfizz_tilde_bend(t_sfizz_tilde* self, t_float f1) +{ + return sfizz_tilde_hdbend(self, f1 / 8191); +} + +static void sfizz_tilde_hdtouch(t_sfizz_tilde* self, t_float f1) +{ + sfizz_send_hd_channel_aftertouch(self->synth, 0, clamp01(f1)); +} + +static void sfizz_tilde_touch(t_sfizz_tilde* self, t_float f1) +{ + sfizz_tilde_hdtouch(self, f1 / 127); +} + +static void sfizz_tilde_hdpolytouch(t_sfizz_tilde* self, t_float key, t_float f2) +{ + if (key < 0 || key > 127) + return; + sfizz_send_hd_poly_aftertouch(self->synth, 0, (int)key, clamp01(f2)); +} + +static void sfizz_tilde_polytouch(t_sfizz_tilde* self, t_float f1, t_float f2) +{ + sfizz_tilde_hdpolytouch(self, f1, f2 / 127); +} + +static void sfizz_tilde_voices(t_sfizz_tilde* self, t_float f1) +{ + int numvoices = (int)f1; + numvoices = (numvoices < 1) ? 1 : numvoices; sfizz_set_num_voices(self->synth, numvoices); } @@ -218,14 +303,30 @@ void sfizz_setup() CLASS_DEFAULT, A_GIMME, A_NULL); class_addmethod( cls_sfizz_tilde, (t_method)&sfizz_tilde_dsp, gensym("dsp"), A_CANT, A_NULL); + class_addlist( + cls_sfizz_tilde, (t_method)&sfizz_tilde_list); class_addmethod( cls_sfizz_tilde, (t_method)&sfizz_tilde_midiin, &s_float, A_FLOAT, A_NULL); class_addmethod( cls_sfizz_tilde, (t_method)&sfizz_tilde_load, gensym("load"), A_DEFSYM, A_NULL); class_addmethod( cls_sfizz_tilde, (t_method)&sfizz_tilde_reload, gensym("reload"), A_DEFFLOAT, A_NULL); + class_addmethod( + cls_sfizz_tilde, (t_method)&sfizz_tilde_cc, gensym("cc"), A_FLOAT, A_FLOAT, A_NULL); class_addmethod( cls_sfizz_tilde, (t_method)&sfizz_tilde_hdcc, gensym("hdcc"), A_FLOAT, A_FLOAT, A_NULL); + class_addmethod( + cls_sfizz_tilde, (t_method)&sfizz_tilde_bend, gensym("bend"), A_FLOAT, A_NULL); + class_addmethod( + cls_sfizz_tilde, (t_method)&sfizz_tilde_hdbend, gensym("hdbend"), A_FLOAT, A_NULL); + class_addmethod( + cls_sfizz_tilde, (t_method)&sfizz_tilde_touch, gensym("touch"), A_FLOAT, A_NULL); + class_addmethod( + cls_sfizz_tilde, (t_method)&sfizz_tilde_hdtouch, gensym("hdtouch"), A_FLOAT, A_NULL); + class_addmethod( + cls_sfizz_tilde, (t_method)&sfizz_tilde_polytouch, gensym("polytouch"), A_FLOAT, A_FLOAT, A_NULL); + class_addmethod( + cls_sfizz_tilde, (t_method)&sfizz_tilde_hdpolytouch, gensym("hdpolytouch"), A_FLOAT, A_FLOAT, A_NULL); class_addmethod( cls_sfizz_tilde, (t_method)&sfizz_tilde_voices, gensym("voices"), A_FLOAT, A_NULL); } diff --git a/plugins/puredata/sfizz~-help.pd b/plugins/puredata/sfizz~-help.pd index 98d0e0f0..c3024d32 100644 --- a/plugins/puredata/sfizz~-help.pd +++ b/plugins/puredata/sfizz~-help.pd @@ -1,36 +1,48 @@ -#N canvas 614 464 659 405 12; +#N canvas 614 464 709 426 12; #X declare -lib sfizz; -#X obj 4 3 cnv 15 650 80 empty empty sfizz 20 30 0 40 -261682 -66577 +#X obj 4 3 cnv 15 700 80 empty empty sfizz 20 30 0 40 -261682 -66577 0; #X text 45 54 A synthesizer for instruments in SFZ format; #X text 33 167 create a SFZ instrument; -#X obj 29 324 midiin; -#X text 26 301 connect MIDI-in; +#X obj 26 323 midiin; +#X text 23 300 connect MIDI-in; #X obj 162 223 dac~; -#X msg 180 346 voices \$1; -#X text 178 302 modify the polyphony; -#X obj 381 327 hsl 127 15 0 1 0 1 empty empty empty -2 -8 0 10 -262144 +#X msg 177 345 voices \$1; +#X text 175 301 modify the polyphony; +#X obj 376 326 hsl 127 15 0 1 0 1 empty empty empty -2 -8 0 10 -262144 -1 -1 6300 1; -#X floatatom 471 347 5 0 0 0 - - -; -#X text 377 303 modulate a parameter; -#X msg 378 347 hdcc 300 \$1; +#X floatatom 466 346 5 0 0 0 - - -; +#X text 372 302 modulate a parameter; +#X msg 373 346 hdcc 300 \$1; #X obj 27 115 declare -lib sfizz; #X obj 163 192 sfizz~ example.sfz; #X text 18 93 load the sfizz library; #X msg 221 115 \; pd dsp 1; #X text 218 94 click to turn on DSP; #X text 153 247 output stereo audio; -#X msg 366 223 reload; -#X msg 466 223 load example.sfz; -#X text 364 200 reload the instrument or load another; +#X msg 398 223 reload; +#X msg 498 223 load example.sfz; +#X text 396 200 reload the instrument or load another; #X obj 241 165 r \$0-input; -#X obj 29 349 s \$0-input; -#X obj 180 371 s \$0-input; -#X obj 378 372 s \$0-input; -#X obj 366 248 s \$0-input; -#X obj 466 248 s \$0-input; -#X obj 180 327 nbx 5 14 1 256 0 1 empty empty empty 0 -8 0 10 -262144 +#X obj 26 348 s \$0-input; +#X obj 177 370 s \$0-input; +#X obj 373 371 s \$0-input; +#X obj 398 248 s \$0-input; +#X obj 498 248 s \$0-input; +#X obj 177 326 nbx 5 14 1 256 0 1 empty empty empty 0 -8 0 10 -262144 -1 -1 64 256; +#X text 567 302 send a note; +#X obj 568 348 makenote 100 500; +#X obj 568 373 list; +#X obj 568 398 s \$0-input; +#X msg 568 323 60; +#X obj 386 92 cnv 15 300 100 empty empty Inlets 20 12 0 14 -233017 +-66577 0; +#X text 395 140 3: pitch bend; +#X text 395 154 4: channel aftertouch; +#X text 395 168 5: polyphonic aftertouch and key; +#X text 395 126 2: controller and value; +#X text 395 112 1: note and velocity; #X connect 3 0 22 0; #X connect 6 0 23 0; #X connect 8 0 11 0; @@ -42,3 +54,7 @@ #X connect 19 0 26 0; #X connect 21 0 13 0; #X connect 27 0 6 0; +#X connect 29 0 30 0; +#X connect 29 1 30 1; +#X connect 30 0 31 0; +#X connect 32 0 29 0;