forked from copych/AcidBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcidBanger.ino
1306 lines (1171 loc) · 36.9 KB
/
AcidBanger.ino
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
#ifdef JUKEBOX
// This is The "Endless Acid Banger"
//
// Pattern generator code taken from
// https://www.vitling.xyz/toys/acid-banger/
// created by Vitling (David Whiting) i.am@thewit.ch
//
// The rest of the code by written by thement@ibawizard.net.
// Some related info probably at http://tips.ibawizard.net/
//
// This work is licensed under a Creative Commons Attribution 4.0 International License.
//
//
// What is this?
// =============
// The endless acid banger is autonomous pattern generator that spits acid-like
// note and drum sequences via MIDI.
//
// How to make this work?
// ======================
// Attach 10 buttons to Arduino (pinout specified bellow), connect serial TX
// to DIN-5 female socket (TX to pin 3 via 220 Ohm resistor, 5V to pin 4 via
// 220 Ohm resistor, GND to pin 2) and let it play.
//
// How does the interface work?
// ============================
// - "Play" button starts/stops the sequencer
// - "Gen synth1" button generates new melody for synth 1 from current note set
// - "Gen synth2" button ditto for synth 1
// - "Gen notes" creates new note set and regenerates the synth1/synth2
// melodies from it
// - "Gen drum" generates new drum pattern
// - "Mem1-5" buttons switch to different patterns. If you hold one memory and
// then press another one, the first one gets copied to the second.
//
// Have fun!
//
// ===============================
// 2023 edit by Copych
// added auto CC ramps for reso, cutoff etc.
// added drum parts categorizing
// added auto breaks/fills
// added crash cymbal
// removed/modified buttons processing
//
#define KICK_NOTE 0 //001
#define SNARE_NOTE 1 //002
#define CLOSED_HAT_NOTE CH_NUMBER //007
#define OPEN_HAT_NOTE OH_NUMBER //008
#define PERCUSSION_NOTE 11 //005
#define CRASH_NOTE 9 //010
// Pin numbers to which are buttons attached (connect one side of button to pin, the other to ground)
#define GEN_SYNTH1_BUTTON_PIN 16
#define GEN_SYNTH2_BUTTON_PIN 17
#define GEN_NOTES_BUTTON 18
#define GEN_DRUM_BUTTON 15
#define PLAY_BUTTON 8
#define MEM1_BUTTON 23
#define MEM2_BUTTON 23
#define MEM3_BUTTON 23
#define MEM4_BUTTON 23
#define MEM5_BUTTON 23
#define send_midi_start() {}
#define send_midi_stop() {}
#define send_midi_tick() {}
#define NUM_RAMPS 6 // simultaneous knob rotatings
#ifndef NO_PSRAM
#define NUM_SYNTH_CCS 12 // how many synth CC params do we have to play
#define NUM_DRUM_CCS 7 // how many drum CC params do we have to play
#define VOL_SYNTH1 100
#define VOL_SYNTH2 100
#define VOL_DRUMS 100
#else
#define NUM_SYNTH_CCS 11 // how many synth CC params do we have to play
#define NUM_DRUM_CCS 5 // how many drum CC params do we have to play
#define VOL_SYNTH1 100
#define VOL_SYNTH2 100
#define VOL_DRUMS 100
#endif
uint8_t current_drumkit = (DEFAULT_DRUMKIT*12); // offset for drum note numbers (instruments are groupped by 12)
struct sSynthCCs {
uint8_t cc_number;
uint8_t cc_couple;
uint8_t cc_default_value;
uint8_t cc_min_value;
uint8_t cc_max_value;
bool reset_after_use;
};
sSynthCCs synth1_ramps[NUM_SYNTH_CCS] = {
//cc cpl def min max reset
#ifndef NO_PSRAM
{CC_303_REVERB_SEND, 0, 5, 2, 127, true},
#endif
{CC_303_PAN, 0, 47, 0, 127, true},
{CC_303_WAVEFORM, 0, 0, 0, 64, true}, // SQUARE
{CC_303_RESO, CC_303_CUTOFF, 64, 40, 125, true},
{CC_303_CUTOFF, CC_303_RESO, 30, 0, 127, true},
{CC_303_DECAY, 0, 20, 15, 64, true},
{CC_303_ATTACK, 0, 1, 3, 10, true},
{CC_303_ENVMOD_LVL, 0, 100, 0, 127, false},
{CC_303_ACCENT_LVL, 0, 10, 0, 70, true},
{CC_303_DELAY_SEND, 0, 0, 64, 127, false},
{CC_303_DISTORTION, 0, 0, 0, 3, true},
{CC_303_OVERDRIVE, 0, 5, 0, 50, true}
};
sSynthCCs synth2_ramps[NUM_SYNTH_CCS] = {
//cc cpl def min max reset
#ifndef NO_PSRAM
{CC_303_REVERB_SEND, 0, 5, 2, 127, true},
#endif
{CC_303_RESO, CC_303_CUTOFF, 64, 60, 127, true},
{CC_303_CUTOFF, CC_303_RESO, 20, 0, 100, false},
{CC_303_PAN, 0, 80, 0, 127, true},
{CC_303_ENVMOD_LVL, 0, 100, 15, 127, false},
{CC_303_WAVEFORM, 0, 127, 64, 127, true}, // SAW
{CC_303_DELAY_SEND, 0, 0, 64, 127, false},
{CC_303_ACCENT_LVL, 0, 0, 0, 127, true},
{CC_303_DECAY, 0, 20, 15, 64, true},
{CC_303_ATTACK, 0, 1, 3, 15, true},
{CC_303_OVERDRIVE, 0, 0, 0, 0, true},
{CC_303_DISTORTION, 0, 20, 0, 40, true}
};
sSynthCCs drum_ramps[NUM_DRUM_CCS] = {
//cc cpl def min max reset
#ifndef NO_PSRAM
{CC_808_REVERB_SEND, 0, 5, 30, 127, true}, // reverb is not available with no psram
{CC_808_DELAY_SEND, 0, 0, 64, 127, true}, // delay for drums needs more delay time (read 'RAM') than we can afford
#endif
{CC_808_CUTOFF, 0, 64, 64, 127, true},
{CC_808_RESO, 0, 64, 0, 127, true},
{CC_808_SD_TONE, 0, 64, 64, 127, true},
{CC_808_BD_DECAY, 0, 127, 50, 127, true},
{CC_808_BD_TONE, 0, 64, 40, 64, true}
};
struct sMidiRamp {
uint8_t chan = 0;
uint8_t cc_number = 0;
float value = 0.0f;
float min_val = 0.0f;
float max_val = 127.0f;
float def_val = 64.0f;
bool need_reset = false;
float stepPer16th = 0.0f;
int16_t leftBars = 0;
} midiRamps[NUM_RAMPS];
typedef enum drum_kinds {
DrumBreak,
DrumStraight,
DrumHang,
DrumAny,
DrumNone
} drum_kinds ;
enum {
KickElectro,
KickFourFloor,
KickBigbeat,
KickNone,
/* anything bellow this line will never be picked */
};
enum {
SnareBackbeat,
SnareSkip,
SnareFill,
SnareBreak,
SnareStraight,
SnareNone,
/* anything bellow this line will never be picked */
};
enum {
HatsOffbeats,
HatsClosed,
HatsPop,
HatsPat1,
HatsNone,
/* anything bellow this line will never be picked */
};
enum {
PercFiller,
PercXor1,
PercXor2,
PercEcho,
PercRolls,
PercNone,
/* anything bellow this line will never be picked */
};
enum {
NumInstruments = 2 + 6,
AccentedMidiVol = 120,
NormalMidiVol = 70,
};
enum {
NumMemories = 5,
MaxNoteSet = 16,
PatternLength = 16,
};
enum {
ButPat1 = 0,
ButNotes = 2,
ButDrums = 3,
ButPlay = 4,
ButMem1 = 5,
ButLast = ButMem1 + NumMemories,
};
typedef struct Pattern Pattern;
typedef struct Memory Memory;
typedef struct Instrument Instrument;
typedef enum {sIdle, sPlaying} bStatus;
struct Button {
uint8_t history;
byte pin;
uint8_t numb;
};
struct Instrument {
// 1-base indexed MIDI channel (first channel is 1)
byte midi_channel;
byte is_drum, drum_note;
void (*noteon)(byte chan, byte note, byte vel);
void (*noteoff)(byte chan, byte note);
byte playing_note;
};
static Instrument instruments[NumInstruments];
static uint32_t bar_current = 0; // it counts bars
struct sBreak {
bStatus status = sIdle;
byte length = 0;
uint32_t start = 0;
uint32_t after = 0;
} Break;
struct Pattern {
uint16_t accent, glide;
uint8_t notes[PatternLength];
};
struct Memory {
Pattern patterns[NumInstruments];
byte note_set[MaxNoteSet];
byte num_notes_in_set;
uint16_t random_seed;
};
static Memory memories[NumMemories];
static byte cur_memory;
#define is_pressed(x) (buttons[x].history == 0)
#define just_pressed(x) (buttons[x].history == 0x80)
static struct Button buttons[ButLast];
static byte button_divider;
static unsigned long now;
static unsigned long last_midi_tick;
static const byte button_pins[ButLast] = {
GEN_SYNTH1_BUTTON_PIN,
GEN_SYNTH2_BUTTON_PIN,
GEN_NOTES_BUTTON,
GEN_DRUM_BUTTON,
PLAY_BUTTON,
MEM1_BUTTON,
MEM2_BUTTON,
MEM3_BUTTON,
MEM4_BUTTON,
MEM5_BUTTON,
};
static void send_midi_noteon(byte chan, byte note, byte vol) {
#ifdef MIDI_VIA_SERIAL
MIDI.sendNoteOn(note, vol, chan);
#endif
#ifdef MIDI_VIA_SERIAL2
MIDI2.sendNoteOn(note, vol, chan);
#endif
handleNoteOn( chan, note, vol) ;
}
static void send_midi_noteoff(byte chan, byte note) {
#ifdef MIDI_VIA_SERIAL
MIDI.sendNoteOn(note, 0, chan);
#endif
#ifdef MIDI_VIA_SERIAL2
MIDI2.sendNoteOn(note, 0, chan);
#endif
handleNoteOff( chan, note, 0) ;
}
static void init_midi() {
// Serial.begin(115200);
// MIDI.begin(MIDI_CHANNEL_OMNI);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_BEAT_PIN, OUTPUT); // AVS Additional LED
for (int i = 0; i < ButLast; i++) {
init_button(&buttons[i], button_pins[i], i + 1 );
}
init_instruments();
init_patterns();
#ifdef JUKEBOX_PLAY_ON_START
do_midi_start();
#endif
}
static void send_midi_control(byte chan, byte cc_number, byte cc_value) {
//MIDI.sendControlChange ( cc_number, cc_value, chan);
handleCC( chan, cc_number, cc_value);
}
/*
Pseudo-random generator with restorable state
*/
static inline uint16_t lfsr16_next(uint16_t x)
{
uint16_t y = x >> 1;
if (x & 1)
y ^= 0xb400;
return y;
}
union seeder_t {
double dseed= 0.9876543021203450678091203456/random(3,0xffff);
struct{
uint8_t x;
uint16_t useed ;
};
} seeder ;
//static uint16_t myRandomState = 0x1234;
static uint16_t myRandomState = seeder.useed ;
static uint16_t myRandomAddEntropy(uint16_t data) {
myRandomState = lfsr16_next((myRandomState << 1) ^ data);
return myRandomState;
}
static uint16_t myRandomRaw() {
myRandomState = lfsr16_next(myRandomState);
return myRandomState;
}
static inline uint16_t myRandom(uint16_t max) {
if (max==0) return 0;
return myRandomRaw() % max;
}
/*
Buttons
*/
static void read_button(struct Button *button)
{
// AVS Fix so all buttons work
if (true){ //if (button->numb == 5) { // start/stop is a real button "boot" ( GPIO0 )
button->history = (button->history << 1) | (digitalRead(button->pin) == HIGH);
} else {
button->history = 0;
}
}
static void init_button(struct Button *button, byte pin, uint8_t num)
{
button->history = 0xff;
button->pin = pin;
button->numb = num;
pinMode(pin, INPUT_PULLUP);
}
/*
Instruments
*/
static void instr_noteoff(byte instr) {
Instrument *ins = &instruments[instr];
if (ins->playing_note != 0) {
if (ins->noteoff != NULL)
ins->noteoff(ins->midi_channel, ins->playing_note);
ins->playing_note = 0;
}
}
static void instr_allnotesoff() {
for (int i = 0; i < NumInstruments; i++) {
instr_noteoff(i);
}
}
static void instr_noteon_raw(byte instr, byte note, byte vol, byte do_glide) {
Instrument *ins = &instruments[instr];
// All instruments are monophonic, so noteoff before noteon
if (ins->playing_note != 0) {
if (do_glide && !ins->is_drum) {
// Implement glide by playing two notes at once. Also known as "fingered glide"
if (ins->noteon != NULL)
ins->noteon(ins->midi_channel, note, vol);
instr_noteoff(instr);
ins->playing_note = note;
return;
}
instr_noteoff(instr);
}
if (ins->noteon != NULL)
ins->noteon(ins->midi_channel, note, vol);
ins->playing_note = note;
}
static void instr_noteon(byte instr, byte value, byte do_glide, byte do_accent) {
Instrument *ins = &instruments[instr];
#ifdef DEBUG_JUKEBOX_
DEBF("glide=%d accent=%d\r\n", do_glide, do_accent);
#endif
if (ins->is_drum) {
// For drums: value is volume, accent and glide are ignored
instr_noteon_raw(instr, current_drumkit + ins->drum_note, value, 0);
} else {
// For non-drums: value is note, volume is accent, glide is used
instr_noteon_raw(instr, value, do_accent ? AccentedMidiVol : NormalMidiVol, do_glide);
}
}
/*
Sequencer
*/
void sequencer_step(byte step) {
#ifdef MIDI_RAMPS
do_midi_ramps();
#endif
#ifdef DEBUG_JUKEBOX_
DEBF("midi step %d\r\n", step);
#endif
// Play all notes in current step
for (int i = 0; i < NumInstruments; i++) {
Pattern *pat = &memories[cur_memory].patterns[i];
byte accent = (pat->accent >> step) & 1;
byte glide = (pat->glide >> step) & 1;
byte value = pat->notes[step];
if (value > 0)
instr_noteon(i, value, glide, accent);
else
instr_noteoff(i);
}
if (Break.after == bar_current && step == 0) {
instr_noteon(NumInstruments - 1, 127, 0, 0);
// instr_noteon_raw(NumInstruments-1, CRASH_NOTE, 127, 0);
if (flip(30)) {
//change drumkit
current_drumkit = myRandom(((Drums.GetSamplesCount()-1)/12)) * 12 ;
//#ifdef DEBUG_JUKEBOX
DEBF("Selected drumkit: %d\r\n" , current_drumkit);
//#endif
}
#ifdef DEBUG_JUKEBOX
DEBUG("CRASH!!!!!!!!!!!!!!!!!!!!!");
#endif
#ifdef MIDI_RAMPS
check_midi_ramps(true);
#endif
}
if (step % 4 == 0 || step == 1) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(LED_BEAT_PIN, HIGH);
#ifdef LOLIN_RGB
pixels.setPixelColor(0, pixels.Color(rand() % 32, rand() % 32, rand() % 32));
pixels.show();
#endif
#ifdef DEBUG_JUKEBOX
DEBUG(step);
#endif
} else {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LED_BEAT_PIN, LOW);
#ifdef LOLIN_RGB
pixels.setPixelColor(0, pixels.Color(0,0,0));
pixels.show();
#endif
}
}
/*
Endless Acid Banger pattern generator
Adapted from https://www.vitling.xyz/toys/acid-banger/
created by Vitling (David Whiting) i.am@thewit.ch
*/
#define NOTE_LIST(x...) (int8_t[]) { x, -1 }
//#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
static const int8_t *const offset_choices[] = {
NOTE_LIST(0, 0, 12, 24, 27),
NOTE_LIST(0, 0, 0, 12, 10, 19, 26, 27),
NOTE_LIST(0, 0, 0, 1, 7, 10, 12, 13),
NOTE_LIST(0),
NOTE_LIST(0, 0, 0, 0, 0, 0, 1, 13, 25),
NOTE_LIST(0, 0, 0, 12, 24),
NOTE_LIST(0, 0, 12, 12, 18, 24, 24),
NOTE_LIST(0, 0, 7, 14, 24, 24),
NOTE_LIST(0, 0, 12, 14, 15, 19),
NOTE_LIST(0, 0, 0, 12, 12, 13, 16, 19, 22, 24, 25),
NOTE_LIST(0, 0, 0, 7, 12, 15, 17, 20, 24),
};
static byte generate_note_set(uint8_t *note_set) {
// Random root note
byte root = myRandom(15) + 28;
// Random note set
byte set = myRandom(ARRAY_SIZE(offset_choices));
// Copy notes from note set and offset them by "root note"
int i;
for (i = 0; i < MaxNoteSet; i++) {
int8_t note = offset_choices[set][i];
if (note < 0)
break;
*note_set++ = root + note;
}
return i;
}
// Flip a coin
static byte flip(byte percent_chance) {
return myRandom(100) < percent_chance;
}
static void generate_melody(uint8_t *note_set, byte note_set_len,
uint8_t *pattern, byte pattern_len,
uint16_t *accent, uint16_t *glide) {
uint8_t density = 255;
*accent = 0;
*glide = 0;
for (int i = 0; i < pattern_len; i++) {
// uint8_t chance = ((uint16_t) density * (i % 4 == 0 ? 60 : (i % 3 == 0 ? 50 : (i % 2 == 0 ? 30 : 10)))) >> 8;
uint8_t chance = ((uint16_t) density * (i % 4 == 0 ? 90 : (i % 3 == 0 ? 80 : (i % 2 == 0 ? 50 : 10)))) >> 8;
if (flip(chance)) {
pattern[i] = note_set[myRandom(note_set_len)];
if (flip(30))
*accent |= 1u << i;
if (flip(70))
*glide |= 1u << i;
} else {
pattern[i] = 0;
}
}
}
static void generate_drums(byte *kick, byte *snare, byte *oh, byte *ch, byte *perc, byte *crash, drum_kinds drum_kind );
static void generate_drums(byte *kick, byte *snare, byte *oh, byte *ch, byte *perc, byte *crash, drum_kinds drum_kind ) {
memset(kick, 0, PatternLength); // zero patterns
memset(snare, 0, PatternLength);
memset(oh, 0, PatternLength);
memset(ch, 0, PatternLength);
memset(perc, 0, PatternLength);
byte kick_mode = KickNone;
byte hat_mode = HatsNone;
byte snare_mode = SnareNone;
byte perc_mode = PercNone;
byte rndVal ;
switch (drum_kind) {
case DrumBreak:
rndVal = myRandom(100);
if (rndVal < 10) {
kick_mode = KickBigbeat;
} else if (rndVal < 60) {
kick_mode = KickFourFloor;
} else {
kick_mode = KickNone;
}
rndVal = myRandom(100);
if (rndVal < 40) {
snare_mode = SnareFill;
} else if (rndVal < 80) {
snare_mode = SnareBreak;
} else {
snare_mode = SnareBackbeat;
}
hat_mode = myRandom(HatsNone);
perc_mode = myRandom(PercNone);
break;
case DrumStraight:
rndVal = myRandom(100);
if (rndVal < 20) {
kick_mode = KickBigbeat;
} else {
kick_mode = KickFourFloor;
}
rndVal = myRandom(100);
if (rndVal < 60) {
snare_mode = SnareStraight;
} else {
snare_mode = SnareBackbeat;
}
if (flip(70)) hat_mode = HatsPop;
else hat_mode = myRandom(HatsNone);
perc_mode = myRandom(PercNone);
break;
case DrumHang:
rndVal = myRandom(100);
if (rndVal < 50) {
kick_mode = KickBigbeat;
} else {
kick_mode = KickNone;
}
rndVal = myRandom(100);
if (rndVal < 60) {
snare_mode = SnareStraight;
} else {
snare_mode = SnareBackbeat;
}
hat_mode = myRandom(HatsNone);
perc_mode = myRandom(PercNone);
break;
case DrumNone:
// nothing
break;
case DrumAny:
default:
kick_mode = myRandom(KickNone);
hat_mode = myRandom(HatsNone);
snare_mode = myRandom(SnareNone);
perc_mode = myRandom(PercNone);
}
if (kick_mode == KickFourFloor) {
for (int i = 0; i < PatternLength; i++) {
if (i % 4 == 0)
kick[i] = 120;
}
} else if (kick_mode == KickElectro) {
for (int i = 0; i < PatternLength; i++) {
if (i == 0)
kick[i] = 127;
else if (i % 2 == 0 && i % 8 != 4 && flip(50))
kick[i] = myRandom(110);
else if (flip(5))
kick[i] = myRandom(110);
}
} else if (kick_mode == KickBigbeat) {
for (int i = 0; i < PatternLength; i++) {
if (i == 0)
kick[i] = 127;
else if (i == 14 && flip(20))
kick[i] = myRandom(80);
}
}
if (snare_mode == SnareBackbeat) {
for (int i = 0; i < PatternLength; i++) {
if (i % 8 == 0)
snare[i] = 110;
}
} else if (snare_mode == SnareFill) {
for (int i = 0; i < PatternLength; i++) {
snare[i] = 120;
}
} else if (snare_mode == SnareStraight) {
for (int i = 0; i < PatternLength; i++) {
if (i % 8 == 4)
snare[i] = 80;
}
} else if (snare_mode == SnareBreak) {
for (int i = 0; i < PatternLength; i++) {
switch (i) {
case 2:
case 8:
break;
case 4:
case 5:
case 6:
case 10:
snare[i] = myRandom(100);
break;
default:
snare[i] = 120;
}
}
} else if (snare_mode == SnareSkip) {
for (int i = 0; i < PatternLength; i++) {
if (i % 8 == 3 || i % 8 == 6)
snare[i] = 90 + myRandom(37);
else if (i % 2 == 0 && flip(20))
snare[i] = 40 + myRandom(25);
else if (flip(10))
snare[i] = 25 + myRandom(25);
}
}
if (hat_mode == HatsOffbeats) {
for (int i = 0; i < PatternLength; i++) {
if (i % 4 == 2)
oh[i] = 50;
else if (flip(30)) {
if (flip(50))
ch[i] = myRandom(25);
else
oh[i] = myRandom(25);
}
}
} else if (hat_mode == HatsClosed) {
for (int i = 0; i < PatternLength; i++) {
if (i % 2 == 0)
ch[i] = 50;
else if (flip(50))
ch[i] = myRandom(40);
}
} else if (hat_mode == HatsPop) {
for (int i = 0; i < PatternLength; i++) {
if (i % 4 == 2)
oh[i] = 60;
else {
ch[i] = 40 + myRandom(40);
}
}
} else if (hat_mode == HatsPat1) {
for (int i = 0; i < PatternLength; i++) {
if (i % 8 != 1 && i % 8 != 4 && i % 8 != 7 )
ch[i] = 80;
}
}
if (perc_mode == PercFiller) {
for (int i = 0; i < PatternLength; i++) {
if (oh[i] == 0 && ch[i] == 0 && kick[i] == 0 && snare[i] == 0)
perc[i] = 50 + myRandom(37);
}
} else if (perc_mode == PercXor1) {
for (int i = 0; i < PatternLength; i++) {
if ((kick[i] == 0) ^ (snare[i] == 0))
perc[i] = 50 + myRandom(37);
}
} else if (perc_mode == PercXor2) {
for (int i = 0; i < PatternLength; i++) {
if ((oh[i] == 0) ^ (ch[i] == 0))
perc[i] = 50 + myRandom(37);
}
} else if (perc_mode == PercEcho) {
byte distance = 1 + myRandom(7);
for (int i = 0; i < PatternLength; i++) {
byte prev_step = (i + PatternLength - distance) % PatternLength;
if (ch[prev_step] || oh[prev_step])
perc[i] = 50 + myRandom(37);
}
} else if (perc_mode == PercRolls) {
byte roll = 0, roll_vol = 0;
for (int i = 0; i < PatternLength; i++) {
byte do_roll = 0;
if (i % 8 == 3 && flip(40))
do_roll = 1;
else if (i % 2 == 0 && flip(20))
do_roll = 1;
else if (flip(10))
do_roll = 1;
if (do_roll) {
roll_vol = 50 + myRandom(37);
roll = 4;
}
if (roll > 0) {
perc[i] = roll_vol;
roll_vol /= 2;
roll--;
}
}
}
}
/*
Generator-to-pattern binding
*/
void mem_generate_melody(byte mem, byte voice) {
Memory *m = &memories[mem];
Pattern *p = &m->patterns[voice];
// Temporarily change random seed to a pre-defined state so that we can generate
// identical melody.
uint16_t random_state = myRandomState;
myRandomState = (m->random_seed << 1) ^ voice;
#ifdef DEBUG_JUKEBOX_
DEBF("generating %d/%d with seed %u \r\n", mem, voice, myRandomState);
#endif
generate_melody(
m->note_set, m->num_notes_in_set,
p->notes, sizeof(p->notes),
&p->accent, &p->glide);
myRandomState = random_state;
}
void mem_generate_melody_and_seed(byte mem, byte voice) {
Memory *m = &memories[mem];
m->random_seed = myRandomRaw();
mem_generate_melody(mem, voice);
}
void mem_generate_note_set(byte mem) {
Memory *m = &memories[mem];
m->num_notes_in_set = generate_note_set(m->note_set);
for (int i = 0; i < 2; i++)
mem_generate_melody(mem, i);
}
void mem_generate_drums(byte mem, enum drum_kinds drum_kind);
void mem_generate_drums(byte mem, enum drum_kinds drum_kind) {
Memory *m = &memories[mem];
generate_drums(
m->patterns[2].notes,
m->patterns[3].notes,
m->patterns[5].notes,
m->patterns[4].notes,
m->patterns[6].notes,
m->patterns[7].notes,
drum_kind);
}
void mem_generate_all(byte mem) {
mem_generate_note_set(mem);
mem_generate_drums(mem, DrumStraight);
for (int i = 0; i < 2; i++)
mem_generate_melody_and_seed(mem, i);
}
void print_pattern(struct Pattern *p, byte is_drum) {
#ifdef DEBUG_JUKEBOX
for (int i = 0; i < PatternLength; i++)
DEBF("%3d ", p->notes[i]);
if (!is_drum) {
for (int i = 0; i < PatternLength; i++)
DEBF(" %c%c \r\n", (p->accent & (1u << i)) ? 'A' : ' ', (p->glide & (1u << i)) ? '~' : ' ');
}
#endif
}
void print_memory(byte mem) {
Memory *m = &memories[mem];
#ifdef DEBUG_JUKEBOX
DEBF("--- memory %d ---", mem);
DEBF("noteset[%d]:", m->num_notes_in_set);
#endif
for (int i = 0; i < m->num_notes_in_set; i++)
#ifdef DEBUG_JUKEBOX
DEBF(" %d", m->note_set[i]);
#endif
for (int i = 0; i < NumInstruments; i++)
print_pattern(&m->patterns[i], instruments[i].is_drum);
}
void init_patterns() {
for (int i = 0; i < NumMemories; i++)
mem_generate_all(i);
}
/*
MIDI clock
*/
#define MIDI_TICKS_PER_16TH 1
static byte midi_playing, midi_tick, midi_step;
const float tick_coef = 1000ul * 15 / MIDI_TICKS_PER_16TH;
static unsigned long midi_tick_ms = tick_coef / bpm;
inline void set_bpm(float newBpm) {
bpm = newBpm;
midi_tick_ms = tick_coef / newBpm;
}
static void decide_on_break() {
uint32_t bars_played = bar_current - Break.after ;
if ( Break.status == sIdle ) {
// plan a break ?
if ( bars_played == 28 ) {
// 100% 1-bar break
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 4;
Break.after = Break.start + Break.length;
} else if ( bars_played == 15 ) {
// 50% 1-bar break
if (flip(20)) {
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 1;
Break.after = Break.start + Break.length;
}
} else if (bars_played == 14) {
if (flip(15)) {
// 2-bars break
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 2;
Break.after = Break.start + Break.length;
}
} else if (bars_played == 13) {
if (flip(15)) {
// 3-bars break
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 3;
Break.after = Break.start + Break.length;
}
} else if (bars_played == 12) {
if (flip(15)) {
// 4-bars break
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 4;
Break.after = Break.start + Break.length;
}
} else if (bars_played == 7) {
if (flip(15)) {
// 1-bar break
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 1;
Break.after = Break.start + Break.length;
}
} else if (bars_played == 6) {
if (flip(15)) {
// 2-bars break
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 2;
Break.after = Break.start + Break.length;
}
} else if (bars_played == 3) {
if (flip(10)) {
// 1-bar break
Break.status = sPlaying;
Break.start = bar_current ;
Break.length = 1;
Break.after = Break.start + Break.length;
}
}
if (Break.start == bar_current ) mem_generate_drums(cur_memory, DrumBreak);
} else { // Break.status != sIdle
if (Break.after == bar_current) {
Break.status = sIdle;
mem_generate_drums(cur_memory, DrumStraight);
if (flip(10)) mem_generate_drums(cur_memory, DrumHang);
if (flip(80)) mem_generate_melody_and_seed(cur_memory, 0);
if (flip(60)) mem_generate_melody_and_seed(cur_memory, 1);
if (flip(15)) mem_generate_note_set(cur_memory);
}
}// Break.status ?? sIdle ?
#ifdef DEBUG_JUKEBOX
DEBF("bar=%d break.len=%d break.status=%d \r\n", bars_played, Break.length, Break.status);
#endif
}