forked from striso/striso-control-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synth_control.cpp
2189 lines (2058 loc) · 81.1 KB
/
synth_control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2019 Piers Titus van der Torren
*
* This file is part of Striso Control.
*
* Striso Control is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Striso Control is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Striso Control. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "ch.h"
#include "hal.h"
#include "ccportab.h"
extern "C" {
#include "synth.h"
#include "ws2812.h"
#include "led.h"
#include "aux_jack.h"
#include "messaging.h"
}
#include "config.h"
#include "config_store.h"
#include "striso.h"
#include "midi_usb.h"
#include "midi_serial.h"
#include "midi.h"
#ifndef USE_WS2812
#define ws2812_write_led(n,r,g,b) led_rgb3(14*r,14*g,14*b)
#endif
#define BUTTONCOUNT 68
#define MAX_PORTAMENTO_BUTTONS 8
#define VOL_TICK (0.0005) // (1.0 / (SAMPLINGFREQ / CHANNEL_BUFFER_SIZE) / 0.5) // decay time of estimated volume
#define VOL_TICK_FACT (0.998) // 0.5**(1/(SAMPLINGFREQ / CHANNEL_BUFFER_SIZE)/0.1)
#define CLEAR_TIMER TIME_MS2I(500) // interval to clear dead notes
float volume_linear = 90.0f; // volume in range 0-127
void set_volume(float vol) {
volume_linear = vol;
volume = volume_linear * volume_linear * (1.0f / 16129.0f);
}
void MidiSend1(uint8_t b0) {
midi_usb_MidiSend1(1, b0);
#ifdef USE_MIDI_SERIAL
serial_MidiSend1(b0);
#endif
}
void MidiSend2(uint8_t b0, uint8_t b1) {
midi_usb_MidiSend2(1, b0, b1);
#ifdef USE_MIDI_SERIAL
serial_MidiSend2(b0, b1);
#endif
}
void MidiSend3(uint8_t b0, uint8_t b1, uint8_t b2) {
midi_usb_MidiSend3(1, b0, b1, b2);
#ifdef USE_MIDI_SERIAL
serial_MidiSend3(b0, b1, b2);
#endif
}
// Schlick power function, approximation of power function
float powf_schlick(const float a, const float b)
{
return (a / (b - a * b + a));
}
// Derivative of schlick power function
float powf_schlick_d(const float a, const float b)
{
float t = (-a*b+a+b);
return b/(t*t);
}
#define pow2(x) ((x)*(x))
#define pow3(x) ((x)*(x)*(x))
#define max(x, y) ((x)>(y)?(x):(y))
#define min(x, y) ((x)<(y)?(x):(y))
#define clamp(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
float clamp_rem(float x, float low, float high, float* rem) {
if (x > high) {
*rem = x - high;
return high;
} else if (x < low) {
*rem = x - low;
return low;
} else {
return x;
}
}
static inline uint32_t log2i(const uint32_t x){return (31 - __builtin_clz (x));}
void update_leds(void);
void set_midi_mode(midi_mode_t mode);
float config_but(int but, int type, float adjust);
typedef enum {
STATE_OFF = 0,
STATE_ON = 1,
STATE_PORTAMENTO = 2,
STATE_ALT = 3,
STATE_TRANSPOSE = 4,
} button_state_t;
/*
* Send midi messages with hysteresis filtering
*/
class MidiParam {
public:
int last_value = INT32_MAX;
int midi_cc = CFG_DISABLE;
MidiParam(int cc) {
midi_cc = cc;
}
/*
* Send midi message if more than 0.75 different from last value
*/
void send(int value_x4, int midi_channel) {
int d = (value_x4 < last_value*4) * 2 + 1; // calculate direction for hysteresis
int value = __USAT((value_x4 + d) >> 2, 7) & 0x7F;
if (value != last_value) {
if (midi_cc < 120) {
MidiSend3(MIDI_CONTROL_CHANGE | midi_channel,
midi_cc, value);
} else if (midi_cc == CFG_CHANNEL_PRESSURE) {
MidiSend2(MIDI_CHANNEL_PRESSURE | midi_channel,
value);
}
last_value = value;
}
}
};
class MotionSensor {
public:
int send_motion_time = 0;
int last_acc_x = INT32_MAX;
int last_acc_y = INT32_MAX;
int last_acc_z = INT32_MAX;
int last_acc_abs = INT32_MAX;
int last_rot_x = INT32_MAX;
int last_rot_y = INT32_MAX;
int last_rot_z = INT32_MAX;
MotionSensor() {}
void message(int* msg) {
int acc_x = msg[0];
int acc_y = msg[1];
int acc_z = msg[2];
int acc_abs = msg[3];
int rot_x = msg[4];
int rot_y = msg[5];
int rot_z = msg[6];
#ifdef USE_MIDI_OUT
if (config.send_motion_interval && (--send_motion_time <= 0)) {
send_motion_time = config.send_motion_interval;
if (config.send_motion_14bit) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
16|MIDI_C_LSB, acc_x&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
16, (64+(acc_x>>7))&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
17|MIDI_C_LSB, acc_y&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
17, (64+(acc_y>>7))&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
18|MIDI_C_LSB, acc_z&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
18, (64+(acc_z>>7))&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
19|MIDI_C_LSB, acc_abs&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
19, (acc_abs>>6)&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
80|MIDI_C_LSB, rot_x&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
80, (64+(rot_x>>7))&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
81|MIDI_C_LSB, rot_y&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
81, (64+(rot_y>>7))&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
82|MIDI_C_LSB, rot_z&0x7F);
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
82, (64+(rot_z>>7))&0x7F);
} else {
// for binary protocol the max is +/-8g, for MIDI max 2g feels better
// -(1<<3) for rounding correctly
int d; // calculate direction for hysteresis
d = ((((last_acc_x-64)<<6) > acc_x)<<4)-(1<<3);
acc_x = __USAT(64+((acc_x+(1<<5)+d)>>5), 7)&0x7F;
d = ((((last_acc_y-64)<<6) > acc_y)<<4)-(1<<3);
acc_y = __USAT(64+((acc_y+(1<<5)+d)>>5), 7)&0x7F;
d = ((((last_acc_z-64)<<6) > acc_z)<<4)-(1<<3);
acc_z = __USAT(64+((acc_z+(1<<5)+d)>>5), 7)&0x7F;
d = (((last_acc_abs<<5) > acc_abs)<<3)-(1<<2);
acc_abs = __USAT((acc_abs+(1<<4)+d)>>4, 7)&0x7F;
d = ((((last_rot_x-64)<<6) > rot_x)<<5)-(1<<4);
rot_x = __USAT(64+((rot_x+(1<<5)+d)>>6), 7)&0x7F;
d = ((((last_rot_y-64)<<6) > rot_y)<<5)-(1<<4);
rot_y = __USAT(64+((rot_y+(1<<5)+d)>>6), 7)&0x7F;
d = ((((last_rot_z-64)<<6) > rot_z)<<5)-(1<<4);
rot_z = __USAT(64+((rot_z+(1<<5)+d)>>6), 7)&0x7F;
if (acc_x != last_acc_x) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
16, acc_x);
last_acc_x = acc_x;
}
if (acc_y != last_acc_y) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
17, acc_y);
last_acc_y = acc_y;
}
if (acc_z != last_acc_z) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
18, acc_z);
last_acc_z = acc_z;
}
if (acc_abs != last_acc_abs) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
19, acc_abs);
last_acc_abs = acc_abs;
}
if (rot_x != last_rot_x) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
80, rot_x);
last_rot_x = rot_x;
}
if (rot_y != last_rot_y) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
81, rot_y);
last_rot_y = rot_y;
}
if (rot_z != last_rot_z) {
midi_usb_MidiSend3(1, MIDI_CONTROL_CHANGE,
82, rot_z);
last_rot_z = rot_z;
}
}
}
#endif
}
};
class Button {
public:
int coord0;
int coord1;
float note;
int midinote;
int midinote_base;
float start_note_offset;
float tuning_note_offset = 0.0f;
float pres;
float vpres;
int last_pres = INT32_MAX;
int last_velo = INT32_MAX;
int last_rvelo = INT32_MAX;
int last_tilt = INT32_MAX;
int last_bend = INT32_MAX;
int last_pitchbend = INT32_MAX;
float but_x;
float but_y;
float vol0;
float vol = 0.0;
button_state_t state = STATE_OFF;
systime_t timer = -1;
int voice = -1;
Button() {
}
void message(float* msg) {
pres = msg[0];
vpres = msg[1];
but_x = msg[2];
but_y = msg[3];
// allow vol below zero to take over oldest voice
if (pres <= 0.0)
vol0 = -1.0;
else
vol0 = pres + vpres;
if (vol < vol0)
vol = vol0;
}
};
class Instrument {
public:
Button buttons[BUTTONCOUNT];
int voices[MAX_VOICECOUNT];
int portamento_buttons[MAX_PORTAMENTO_BUTTONS];
float notegen0 = 12.00;
float notegen1 = 7.00;
int tuning_color = 0x00aa00;
int cur_tuning = 0;
float tuning_note_offset = 0;
float note_offset = 0;
float start_note_offset = 62;
float min_note_offset = 32;
float max_note_offset = 92;
int altmode = 0;
int portamento = 0;
int transposemode = 0;
int flipdir = 1;
int last_button = 0;
int master_button = -1;
int transpose_button = -1;
int transpose_button2 = -1;
synth_interface_t* synth_interface;
int voicecount = VOICECOUNT;
int midi_channel_offset = 1;
float midi_bend_range = 48.0;
float bend_sensitivity = 1.0f;
float y_sensitivity = 1.0f;
float pres_sensitivity = 1.0f;
float velo_sensitivity = 1.0f;
int midi_velo_offset = 0;
Instrument(int* c0, int* c1, int n_buttons, synth_interface_t* si) {
int n;
for (n = 0; n < n_buttons; n++) {
buttons[n].coord0 = c0[n];
buttons[n].coord1 = c1[n];
// calculate note number
buttons[n].note = start_note_offset + buttons[n].tuning_note_offset +
notegen0 * buttons[n].coord0 +
notegen1 * buttons[n].coord1;
buttons[n].midinote_base = (int)(notegen0 * buttons[n].coord0 +
notegen1 * buttons[n].coord1 + 0.5 + 100) - 100; // careful to keep rounded value above zero
buttons[n].midinote = buttons[n].midinote_base + (int)(start_note_offset + 0.5);
}
for (n = 0; n < MAX_VOICECOUNT; n++) {
voices[n] = -1;
}
for (n = 0; n < MAX_PORTAMENTO_BUTTONS; n++) {
portamento_buttons[n] = -1;
}
synth_interface = si;
set_volume(volume_linear);
}
void set_portamento(int p) {
// in mono mode portamento should stay on
if (config.midi_mode != MIDI_MODE_MPE || transposemode) {
return;
}
if (p) {
if (!portamento) {
portamento = 1;
if (buttons[last_button].state == STATE_ON) {
master_button = last_button;
}
// TODO: else check if only one button is pressed
}
} else if (portamento) {
portamento = 0;
}
}
void set_altmode(int a) {
if (a) {
altmode |= 1;
} else {
altmode &= 2;
}
}
void set_free_transpose_mode(int p) {
if (portamento) {
return;
}
if (p) {
if (!transposemode) {
transposemode = 1;
if (buttons[last_button].state == STATE_ON) {
transpose_button = last_button;
} else {
transpose_button = -1;
}
transpose_button2 = -1;
}
} else if (transposemode) {
transposemode = 0;
transpose_button = -1;
transpose_button2 = -1;
}
}
int change_note_offset(float offset) {
return set_note_offset(start_note_offset + offset);
}
int set_note_offset(float offset) {
if (offset >= min_note_offset && offset <= max_note_offset) {
start_note_offset = offset;
update_leds();
return 0;
}
led_rgb3(255,0,0);
return 1;
}
void set_notegen1(float g) {
// unset cur_tuning so the tuning will be reset on tuning switch
cur_tuning = -1;
// keep generator within syntonic continuum range
notegen1 = clamp(g, 6.85714285714286f, 7.2f);
// base color depending on tuning system
if (notegen1 < 6.94) {tuning_color = 0x550055;}
else if (notegen1 < 6.957) {tuning_color = 0x0000aa;}
else if (notegen1 < 6.984) {tuning_color = 0x005555;}
else if (notegen1 < 7.010) {tuning_color = 0x00aa00;}
else if (notegen1 < 7.03) {tuning_color = 0x555500;}
else {tuning_color = 0xaa0000;}
update_leds();
}
void reset_note_offsets(void) {
for (int n = 0; n < 61; n++) {
int but = button_number_map[n];
buttons[but].tuning_note_offset = 0.0f;
}
}
void set_note_offsets(float* offsets, int n_buttons) {
if (n_buttons == 61) {
// all 61 offsets are given
for (int n = 0; n < 61; n++) {
int but = button_number_map[n];
buttons[but].tuning_note_offset = offsets[n] / 100;
}
}
else if (n_buttons == 17) {
// one repeating octave starting with C
for (int n = 0; n < 61; n++) {
int but = button_number_map[n];
buttons[but].tuning_note_offset = offsets[(n + 7) % 17] / 100;
}
}
}
void load_tuning(int n) {
if (cur_tuning == n) {
led_rgb(tuning_color);
return;
}
if (n == 0) {
// tuning 0 hard coded to 12tet
notegen0 = 12.0f;
set_notegen1(7.0f);
reset_note_offsets();
note_offset = tuning_note_offset = 0.0f;
led_rgb(tuning_color);
cur_tuning = n;
return;
}
float f;
CC_ALIGN(8) char key[] = "fT0fifth";
key[2] = '0' + n;
f = getConfigFloat(key);
if (f == CONFIG_UNDEFINED) f = 700.0f;
notegen1 = f / 100;
strset(key, 3, "oct ");
f = getConfigFloat(key);
if (f == CONFIG_UNDEFINED) f = 1200.0f;
notegen0 = f / 100;
strset(key, 3, "off ");
f = getConfigFloat(key);
if (f == CONFIG_UNDEFINED) f = 0.0f;
note_offset = tuning_note_offset = f / 100;
for (int n = 0; n < 61; n++) {
put_button_name(n, &key[3]);
int but = button_number_map[n];
f = getConfigFloat(key);
if (f == CONFIG_UNDEFINED) f = 0.0f;
buttons[but].tuning_note_offset = f / 100;
}
key[0] = 'h';
strset(key, 3, "color");
tuning_color = getConfigHex(key);
led_rgb(tuning_color);
cur_tuning = n;
}
/* Rotate the layout 180 degrees */
void flip(void) {
flipdir = -flipdir;
for (int n = 0; n < BUTTONCOUNT; n++) {
buttons[n].coord0 = -buttons[n].coord0;
buttons[n].coord1 = -buttons[n].coord1;
// invert midi note number
buttons[n].midinote_base = -buttons[n].midinote_base;
}
}
void button_message(int but, float* msg) {
static float old_angle = -1000.0f;
static bool nudged = false;
static systime_t next_knobchange;
// process button message and send osc messages
buttons[but].message(msg);
// handle alternative functions of note buttons
if ((altmode == 1 && buttons[but].state == STATE_OFF)
|| (buttons[but].state == STATE_ALT)) {
// only handle on new press
if (buttons[but].state == STATE_OFF && buttons[but].pres > 0.05) {
buttons[but].state = STATE_ALT;
altmode |= 2;
config_but(but, 0, 0); // show current setting
} else if (buttons[but].pres == 0.0) {
buttons[but].state = STATE_OFF;
altmode &= 1;
old_angle = -1000.0f;
nudged = false;
if (!altmode) update_leds();
}
// handle knob mode
float a = pow2(buttons[but].but_x) + pow2(buttons[but].but_y);
if (a > pow2(0.5f)) {
float angle = atan2f(buttons[but].but_x, buttons[but].but_y)*(8/3.1415926536) + 8.0f;
if (old_angle == -1000.0f) {
// nudge up or down
if (buttons[but].but_x < 0.3 && buttons[but].but_x > -0.3) {
if (buttons[but].but_y > 0) {
config_but(but, 1, 1.0f);
} else {
config_but(but, 1, -1.0f);
}
nudged = true;
next_knobchange = chVTGetSystemTime() + TIME_MS2I(500);
}
old_angle = angle;
} else {
float adjust = angle - old_angle;
if (adjust > 8) adjust -= 16;
else if (adjust < -8) adjust += 16;
if (nudged) {
if (fabsf(adjust) > 4.0f) {
nudged = false;
old_angle = angle;
} else {
if (chVTGetSystemTime() > next_knobchange) {
// nudge up or down
if (buttons[but].but_y > 0) {
config_but(but, 1, 1.0f);
} else {
config_but(but, 1, -1.0f);
}
next_knobchange = chVTGetSystemTime() + TIME_MS2I(200 / (4*pow2(buttons[but].but_y)));
}
}
} else {
if (chVTGetSystemTime() > next_knobchange) {
adjust = config_but(but, 2, adjust);
old_angle = angle - clamp(adjust, -4, 4);
next_knobchange = chVTGetSystemTime() + TIME_MS2I(20);
}
}
}
} else if (a < pow2(0.2f)) {
old_angle = -1000.0f;
nudged = false;
}
return;
}
if (config.midi_mode == MIDI_MODE_POLY) {
// in single channel poly mode just send out the notes on a single channel,
// skip the whole channel assignment stuff.
#ifdef USE_MIDI_OUT
if (buttons[but].pres > 0.0) {
// Note on detection
if (buttons[but].state == STATE_OFF) {
buttons[but].state = STATE_ON;
buttons[but].midinote = buttons[but].midinote_base + (int)(start_note_offset + 0.5);
buttons[but].start_note_offset = start_note_offset;
// multiply velo by 2 to cover full midi range on note on
int velo = midi_velo_offset + buttons[but].vpres * velo_sensitivity * 128 * 2;
velo = clamp(velo, 1, 127);
MidiSend3(MIDI_NOTE_ON | midi_channel_offset,
buttons[but].midinote, velo);
master_button = but;
}
if (master_button == -1) {
master_button = but;
}
if (config.midi_pres == CFG_POLY_PRESSURE) {
float presf = buttons[but].pres * pres_sensitivity;
float d; // calculate direction for hysteresis
d = (buttons[but].last_pres > (presf)) * 0.5 - 0.25;
int pres = presf * 127 + 0.5 + d;
pres = clamp(pres, 0, 127);
if (pres != buttons[but].last_pres) {
MidiSend3(MIDI_POLY_PRESSURE | midi_channel_offset,
buttons[but].midinote, pres);
buttons[but].last_pres = pres;
}
}
if (but == master_button) {
// reduce pres, bend and tilt of all pressed buttons to single values
float presf = 0.0f;
float x = 0.0f;
float xw = 0.0f;
float y = 0.0f;
float yw = 0.0f;
for (int i=0; i<BUTTONCOUNT; i++) {
if (buttons[i].state == STATE_ON) {
if (buttons[i].pres > presf) presf = buttons[i].pres;
float w = pow2(buttons[i].but_x);
x += w * buttons[i].but_x;
xw += w;
w = pow2(buttons[i].but_y);
y += w * buttons[i].but_y;
yw += w;
}
}
if (xw > 0.0000001f) x /= xw;
if (yw > 0.0000001f) y /= yw;
float d; // calculate direction for hysteresis
if (config.midi_pres != CFG_POLY_PRESSURE) {
d = (buttons[0].last_pres > (presf)) * 0.5 - 0.25;
int pres = presf * 127 + 0.5 + d;
pres = clamp(pres, 0, 127);
if (pres != buttons[0].last_pres) {
if (config.midi_pres == CFG_CHANNEL_PRESSURE) {
MidiSend2(MIDI_CHANNEL_PRESSURE | midi_channel_offset,
pres);
} else if (config.midi_pres < 120) {
MidiSend3(MIDI_CONTROL_CHANGE | midi_channel_offset,
config.midi_pres, pres);
}
buttons[0].last_pres = pres;
}
}
if (config.midi_x == CFG_PITCH_BEND) {
x = pow3(x) * bend_sensitivity;
d = (buttons[0].last_bend > (0x2000 + x * 0x2000)) * 0.5 - 0.25;
int bend = 0x2000 + x * 0x2000 + 0.5 + d;
bend = clamp(bend, 0, 0x3fff);
if (bend != buttons[0].last_bend) {
MidiSend3(MIDI_PITCH_BEND | midi_channel_offset,
bend & 0x7f, (bend >> 7) & 0x7f);
buttons[0].last_bend = bend;
}
} else if (config.midi_x < 120) {
d = (buttons[0].last_bend > (64 + x * 64)) * 0.5 - 0.25;
int bend = 64 + x * 64 + 0.5 + d;
bend = clamp(bend, 0, 127);
if (bend != buttons[0].last_bend) {
MidiSend3(MIDI_CONTROL_CHANGE | midi_channel_offset,
config.midi_x, bend);
buttons[0].last_bend = bend;
}
}
y = y * y_sensitivity;
d = (buttons[0].last_tilt > (64 + y * 64)) * 0.5 - 0.25;
int tilt = 64 + y * 64 + 0.5 + d;
tilt = clamp(tilt, 0, 127);
if (tilt != buttons[0].last_tilt) {
if (config.midi_y < 120 && y_sensitivity != 0.0f) {
MidiSend3(MIDI_CONTROL_CHANGE | midi_channel_offset,
config.midi_y, tilt);
}
buttons[0].last_tilt = tilt;
}
}
} else { // Note off
buttons[but].state = STATE_OFF;
int velo = 0 - buttons[but].vpres * velo_sensitivity * 128 * 2;
velo = clamp(velo, 0, 127);
MidiSend3(MIDI_NOTE_OFF | midi_channel_offset,
buttons[but].midinote, velo);
if (but == master_button) {
master_button = -1;
}
}
return;
#endif
}
// Note on detection
if (buttons[but].state == STATE_OFF && buttons[but].pres > 0.0) {
// calculate midinote only at note on
if (config.midinote_mode == MIDINOTE_MODE_DEFAULT) {
buttons[but].midinote = buttons[but].midinote_base + (int)(start_note_offset + 0.5);
} else if (config.midinote_mode == MIDINOTE_MODE_TUNING) {
buttons[but].midinote = (int)(notegen0 * buttons[but].coord0 +
notegen1 * buttons[but].coord1 +
start_note_offset +
note_offset + 0.5);
} else if (config.midinote_mode == MIDINOTE_MODE_BUTTON) {
buttons[but].midinote = 17 * buttons[but].coord0 + 10 * buttons[but].coord1 + 30;
}
buttons[but].start_note_offset = start_note_offset;
if (portamento) {
if (master_button == -1) {
if (get_voice(but) >= 0) {
master_button = but;
}
} else {
// add button to portamento button
for (int n = 0; n < MAX_PORTAMENTO_BUTTONS; n++) {
if (portamento_buttons[n] == -1) {
portamento_buttons[n] = but;
buttons[but].state = STATE_PORTAMENTO;
break;
}
}
}
} else if (transposemode) {
if (transpose_button == -1) {
if (get_voice(but) >= 0) {
transpose_button = but;
}
} else if (buttons[transpose_button].state == STATE_ON
&& buttons[but].pres > 0.1) {
buttons[but].state = STATE_TRANSPOSE;
transpose_button2 = but;
transposemode = 0;
}
} else {
get_voice(but);
}
}
if (buttons[but].state) {
// calculate note pitch
buttons[but].note = buttons[but].start_note_offset + buttons[but].tuning_note_offset +
notegen0 * buttons[but].coord0 +
notegen1 * buttons[but].coord1
+ note_offset;
buttons[but].timer = chVTGetSystemTime() + CLEAR_TIMER;
if (but == master_button) {
// calculate average of portamento buttons
float temp_pres = buttons[but].pres; // save pres for note off detection
float pres = buttons[but].pres;
float sw = pres*pres;
float vpres = buttons[but].vpres;
float note = sw * buttons[but].note;
float but_x = sw * buttons[but].but_x;
float but_y = sw * buttons[but].but_y;
for (int n = 0; n < MAX_PORTAMENTO_BUTTONS; n++) {
int b = portamento_buttons[n];
if (b >= 0) {
float w = buttons[b].pres;
w = w*w;
sw += w;
pres += buttons[b].pres;
vpres += buttons[b].vpres;
note += w * buttons[b].note;
but_x += w * buttons[b].but_x;
but_y += w * buttons[b].but_y;
}
}
buttons[but].pres = min(pres, 1.0f);
buttons[but].vpres = clamp(vpres, -1.0f, 1.0f);
if (sw > 0.0f) {
buttons[but].note = note / sw;
buttons[but].but_x = but_x / sw;
buttons[but].but_y = but_y / sw;
}
update_voice(but);
buttons[but].pres = temp_pres;
}
else if (but == transpose_button && transpose_button2) {
// calculate combined transpose buttons
float temp_pres = buttons[but].pres; // save pres for note off detection
float sw = buttons[but].pres + buttons[transpose_button2].pres;
if (sw > 0.0f) {
buttons[but].but_x = (buttons[but].pres * buttons[but].but_x +
buttons[transpose_button2].pres * buttons[transpose_button2].but_x)
/ sw;
buttons[but].but_y = (buttons[but].pres * buttons[but].but_y +
buttons[transpose_button2].pres * buttons[transpose_button2].but_y)
/ sw;
}
buttons[but].pres = max(buttons[but].pres, buttons[transpose_button2].pres);
buttons[but].vpres = buttons[but].vpres + buttons[transpose_button2].vpres;
update_voice(but);
buttons[but].pres = temp_pres;
}
else if (buttons[but].state == STATE_ON) {
// send synth parameters
update_voice(but);
}
// Note off detection
if (buttons[but].pres <= 0) {
if (buttons[but].state == STATE_PORTAMENTO) {
int count = 0;
for (int n = 0; n < MAX_PORTAMENTO_BUTTONS; n++) {
if (portamento_buttons[n] == but) {
portamento_buttons[n] = -1;
}
count += portamento_buttons[n] >= 0;
}
buttons[but].state = STATE_OFF;
if (!portamento && count == 0) {
// if no portamento buttons left turn off portamento
master_button = -1;
}
} else if (but == master_button) {
// find other portamento button to take over portamento voice
for (int n = 0; n < MAX_PORTAMENTO_BUTTONS; n++) {
if (portamento_buttons[n] >= 0) {
buttons[portamento_buttons[n]].midinote = buttons[but].midinote;
buttons[portamento_buttons[n]].voice = buttons[but].voice;
master_button = portamento_buttons[n];
voices[buttons[master_button].voice] = master_button;
portamento_buttons[n] = -1;
buttons[master_button].state = STATE_ON;
buttons[but].state = STATE_OFF;
break;
}
}
// if no other portamento button is found turn off portamento
if (buttons[but].state) {
master_button = -1;
}
} else if (but == transpose_button) {
if (transpose_button2 >= 0
&& buttons[transpose_button2].state == STATE_TRANSPOSE) {
// do free transpose
change_note_offset(buttons[transpose_button].note
- buttons[transpose_button2].note);
buttons[transpose_button2].midinote = buttons[transpose_button].midinote;
buttons[transpose_button2].voice = buttons[transpose_button].voice;
buttons[transpose_button2].start_note_offset = start_note_offset;
voices[buttons[transpose_button2].voice] = transpose_button2;
buttons[transpose_button2].state = STATE_ON;
buttons[but].state = STATE_OFF;
}
transpose_button = -1;
transpose_button2 = -1;
} else if (buttons[but].state == STATE_TRANSPOSE) {
buttons[but].state = STATE_OFF;
transpose_button = -1;
transpose_button2 = -1;
}
if (but == last_button) {
// if last pressed button is released check if other button can take its place
for (int n = 0; n < voicecount; n++) {
if (voices[n] >= 0 && buttons[voices[n]].state == STATE_ON) {
last_button = voices[n];
break;
}
}
}
release_voice(but);
};
}
}
void release_voice(int but) {
if (buttons[but].state == STATE_ON) {
buttons[but].state = STATE_OFF;
#ifdef USE_MIDI_OUT
int velo = 0 - buttons[but].vpres * velo_sensitivity * 128 * 2;
velo = clamp(velo, 0, 127);
MidiSend3(MIDI_NOTE_OFF | (midi_channel_offset + buttons[but].voice),
buttons[but].midinote, velo);
#endif
}
}
int get_voice(int but) {
int voice = -1;
float min_vol = buttons[but].vol;// * 0.9 - 0.05; // * factor for hysteresis
for (int n = 0; n < voicecount; n++) {
// check if empty or last used voice is available
if (voices[n] == -1 || voices[n] == but)
{
voice = n;
break;
}
// else find voice with minimum approximated volume
float vol = buttons[voices[n]].vol;
if (vol < min_vol) {
min_vol = vol;
voice = n;
}
}
if (voice >= 0) {
// take over the voice
if (voices[voice] >= 0) {
release_voice(voices[voice]);
}
buttons[but].state = STATE_ON;
buttons[but].voice = voice;
buttons[but].last_pitchbend = INT32_MAX;
voices[voice] = but;
last_button = but;
#ifdef USE_MIDI_OUT
// multiply velo by 2 to cover full midi range on note on
int velo = midi_velo_offset + buttons[but].vpres * velo_sensitivity * 128 * 2;
velo = clamp(velo, 1, 127);
MidiSend3(MIDI_NOTE_ON | (midi_channel_offset + buttons[but].voice),
buttons[but].midinote, velo);
buttons[but].last_velo = velo;
#endif
return 0;
}
return -1;
}
void update_voice(int but) {
float pb = bend_sensitivity * pow3(buttons[but].but_x);
float presf = buttons[but].pres * pres_sensitivity;
float velof = buttons[but].vpres * velo_sensitivity;
float y = clamp(buttons[but].but_y * y_sensitivity, -1.0f, 1.0f);
#ifdef USE_INTERNAL_SYNTH
int voice = buttons[but].voice;
*(synth_interface->note[voice]) = buttons[but].note + pb;
*(synth_interface->pres[voice]) = presf;
*(synth_interface->vpres[voice]) = velof;
*(synth_interface->but_x[voice]) = buttons[but].but_x;
*(synth_interface->but_y[voice]) = y;
#endif
#ifdef USE_MIDI_OUT
float d; // calculate direction for hysteresis
// TODO: fix hysteresis!
// TODO: last_... values per midi channel instead of per button
d = (buttons[but].last_pres > (presf)) * 0.5 - 0.25;
int pres = presf * 127 + 0.5 + d;
pres = clamp(pres, 0, 127);
d = (buttons[but].last_tilt > (64 + y * 64)) * 0.5 - 0.25;
int tilt = 64 + y * 64 + 0.5 + d;
tilt = clamp(tilt, 0, 127);
pb = (pb
+ buttons[but].note - buttons[but].midinote)
* (0x2000 / midi_bend_range) + 0x2000;
d = (buttons[but].last_pitchbend > pb) * 0.5 - 0.25;
int pitchbend = pb + 0.5 + d;
pitchbend = clamp(pitchbend, 0, 0x3fff);
// pitchbend is also used for tuning and glissando
if (pitchbend != buttons[but].last_pitchbend) {
MidiSend3(MIDI_PITCH_BEND | (midi_channel_offset + buttons[but].voice),
pitchbend & 0x7f, (pitchbend >> 7) & 0x7f);
buttons[but].last_pitchbend = pitchbend;
}
if (pres != buttons[but].last_pres) {
if (config.mpe_pres == CFG_CHANNEL_PRESSURE) {
MidiSend2(MIDI_CHANNEL_PRESSURE | (midi_channel_offset + buttons[but].voice),
pres);
} else if (config.mpe_pres < 120) {
MidiSend3(MIDI_CONTROL_CHANGE | (midi_channel_offset + buttons[but].voice),
config.mpe_pres, pres);
}
buttons[but].last_pres = pres;
}