-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMORAD_Drums.ino
388 lines (322 loc) · 13.1 KB
/
MORAD_Drums.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
// Copyright 2019 Rich Heslip
//
// Author: Rich Heslip
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
//
// sample player inspired by Jan Ostman's ESP8266 drum machine http://janostman.wordpress.com
// completely rewritten for the Motivation Radio Eurorack module
// plays samples in response to gate/trigger inputs and MIDI note on messages
// will play any 22khz sample file converted to a header file in the appropriate format
// Feb 3/19 - initial version
// Feb 11/19 - sped up encoder/trigger ISR so it will catch 1ms pulses from Grids
#include <Arduino.h>
#include <WiFi.h>
//#include <i2s.h>
//#include <i2s_reg.h>
#include <pgmspace.h>
//#include "driver/i2s.h"
//#include "freertos/queue.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "MIDI.h"
#include <menu.h>
#include <menuIO/clickEncoderIn.h>
#include <menuIO/keyIn.h>
#include <menuIO/chainStream.h>
#include <menuIO/serialOut.h>
#include <menuIO/adafruitGfxOut.h>
#include <menuIO/serialIn.h>
#include "MORAD_IO.h"
// MIDI stuff
uint8_t MIDI_Channel=10; // default MIDI channel for percussion
struct SerialMIDISettings : public midi::DefaultSettings
{
static const long BaudRate = 31250;
};
// must use HardwareSerial for extra UARTs
HardwareSerial MIDISerial(2);
// instantiate the serial MIDI library
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, MIDISerial, MIDI, SerialMIDISettings);
// create OLED display device
#define OLED_RESET -1 // unused port - to keep the compiler happy
Adafruit_SSD1306 display(OLED_RESET);
// encoder
ClickEncoder clickEncoder(ENC_A,ENC_B,ENC_SW,4); // divide by 4 works best with my encoder
ClickEncoderStream encStream(clickEncoder,1);
// sample interrupt timer defs
#define DAC_TIMER_MICROS 46 // 22khz interrupt rate for DAC
#define ENC_TIMER_MICROS 250 // 4khz for encoder
hw_timer_t * timer0 = NULL;
hw_timer_t * timer1 = NULL;
// ADC readings
#define ADC_RANGE 4097 // 12 bit ADC
uint16_t CV_in[4];
// we have four voices that can play any sample when triggered by the Gate inputs
// this structure holds the settings for each voice
enum CV_MODE {NONE,VOLUME,SAMPLE};
#define NUM_VOICES 4
struct voice_t {
uint8_t sample;
uint8_t mix;
uint8_t cv_mode;
} voice[NUM_VOICES] = {
0, // default voice 0 assignment
127, // max volume
NONE, // CV not assigned
3, // default voice 1 assignment
127, // max volume
NONE, // CV not assigned
7, // default voice 2 assignment
127, // max volume
NONE, // CV not assigned
17, // default voice 3 assignment
127, // max volume
NONE, // CV not assigned
};
// we can have an arbitrary number of samples but don't go too crazy because they have to be processed in the ISR at 22khz
// sound sample files must be 22khz 16 bit signed PCM format - see the sample include files for examples
// sample files are compiled into arrays and stored in ESP32 program flash
// if your sketch is too big you may have to fiddle with the flash partition scheme to enable more of the flash to be used for program space
// reading samples from the SPIFFs file system would be cool but its a lot more overhead
// calling the SPIFFs libraries from an ISR will probably not work anyway
// the header files can be auto generated by the wav2header utility
// put your 22khz or 44khz PCM wav files in a sample directory, run the utility and it will generate all the header files
//#include "GMsamples/samples.h" // general MIDI set - do not run wav2header on these or it will mess up the midi mapping
// #include "808samples/samples.h" // 808 sounds
//#include "PNSsamples/samples.h" // PNS soundfont set
#include "Angular_Jungle_Set/samples.h" // Jungle soundfont set - great!
//#include "WSA1_DRUMKIT/samples.h" // WSA1 soundfont set
//#include "Angular_Techno_Set/samples.h" // Techno
#define NUM_SAMPLES (sizeof(sample)/sizeof(sample_t))
// ISR at 22khz writes the MCP4822 DAC using soft SPI and samples one ADC channel at a time
// single hardware SPI transactions are very slow on ESP32 Arduino and you can't use the SPI library from an ISR
// this ISR takes approx 25us to run but it depends how many samples are playing
void ICACHE_RAM_ATTR onTimer(){
int32_t samplesum=0;
uint32_t spiword,adcdata;
static uint8_t channel;
// digitalWrite(GATEout_0,1); // to time ISR
for (int i=0; i< NUM_SAMPLES;++i) { // look for samples that are playing, scale their volume, and add them up
if (sample[i].sampleindex < sample[i].samplesize) samplesum+=(int32_t)(sample[i].samplearray[sample[i].sampleindex++]*sample[i].play_volume); // thats a mouthful!
}
samplesum=samplesum>>7; // adjust for play_volume multiply above
if (samplesum>32767) samplesum=32767; // clip if sample sum is too large
if (samplesum<-32767) samplesum=-32767;
samplesum+=32768; // convert to unsigned for DAC output
spiword=(uint16_t) samplesum>> 4; // scale sum of samples from 16 bits to 12 bits
// send sum of samples to DAC
spiword = spiword & 0x0fff; // mask off 4 msbs which are DAC control
spiword=spiword | 0x3000; // send to 1st DAC, use 2.048v reference
digitalWrite(DAC0_CS,0); // assert DAC0 CS
for (int i=0;i<16;++i) { // write it out SPI mode 0, MSB first
digitalWrite(MOSI,(bool)(spiword & 0x8000)); // MSB first
digitalWrite(SCLK,0);
digitalWrite(SCLK,1);
spiword=spiword<<1;
}
digitalWrite(DAC0_CS,1); // deassert CS
// sample the ADCs round robin style
channel=channel&0x3; // ADC channel 0-3
spiword=0x00060000 | (channel << 14); // start bit, single ended mode and channel #
// read the ADC
digitalWrite(ADC_CS,0); // assert DAC0 CS
for (int i=0;i<19;++i) { // write it out SPI mode 0, MSB first
digitalWrite(MOSI,(bool)(spiword & 0x00040000)); // MSB first
digitalWrite(SCLK,0);
spiword=spiword<<1; // do this here to add some data setup time. also squares up the clock a bit
adcdata|=(bool)digitalRead(MISO);
digitalWrite(SCLK,1);
adcdata=adcdata<<1;
}
digitalWrite(ADC_CS,1); // deassert CS
CV_in[channel++]=(uint16_t)(~adcdata & 0xfff); // invert and save the ADC result - external buffer amp is inverting
// digitalWrite(GATEout_0,0); // to time ISR
}
// encoder timer interrupt handler at 1khz
// we also sample the gate/trigger inputs and trigger sample playback
// trigger counters
// the CV inputs are sampled at a different rate which could cause an "accent" input to be missed due to timing skew
// so we trigger the sample the second time we see the gate active
uint8_t trigcnt[NUM_VOICES] = {0,0,0,0}; // trigger counters
void ICACHE_RAM_ATTR encTimer(){
uint8_t sample_toplay,volume_toplay;
uint32_t offset;
clickEncoder.service(); // check the encoder inputs
// handle drum triggers
bool trigger;
for (int i=0; i< NUM_VOICES;++i) {
switch (i) { // sample gate inputs
case 0:
trigger=!digitalRead(GATEin_0); // active low gate inputs
break;
case 1:
trigger=!digitalRead(GATEin_1);
break;
case 2:
trigger=!digitalRead(GATEin_2);
break;
case 3:
trigger=!digitalRead(GATEin_3);
break;
}
if (trigger) {
if (trigcnt[i]<=3) ++trigcnt[i];
if (trigcnt[i]==2) { // trigger on second sample of gate active
sample_toplay=voice[i].sample; // default sample to play
volume_toplay=voice[i].mix; // default volume level to play
switch (voice[i].cv_mode) { // handle CV inputs
case NONE:
break;
case VOLUME:
//volume_toplay=(voice[i].mix*CV_in[i]*CV_in[i]/(ADC_RANGE*ADC_RANGE)); // set play volume from mix level and CV in - square law response gives more punch
volume_toplay=(voice[i].mix*CV_in[i]/ADC_RANGE*ADC_RANGE); // set play volume from mix level and CV in
break;
case SAMPLE:
offset=CV_in[i]*NUM_SAMPLES/ADC_RANGE+NUM_SAMPLES/2+1; // 0V in = sample selected in menu
sample_toplay=(sample_toplay+(uint8_t)offset) % NUM_SAMPLES; // select sample using CV as an offset into the sample table
break;
default:
voice[i].cv_mode=NONE; // if its messed up fix it
}
sample[voice[i].sample].play_volume=volume_toplay; // set volume
sample[sample_toplay].sampleindex=0; // start sample playback on selected sample
}
}
else trigcnt[i]=0;
}
}
// serial MIDI handler
void HandleNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
if (channel==MIDI_Channel) {
for (int i=0; i< NUM_SAMPLES;++i) { //
if (sample[i].MIDINOTE == note) {
sample[i].play_volume=velocity; // use MIDI volume
sample[i].sampleindex=0; // if note matches sample MIDI note number, start it playing
}
}
}
}
#include "menusystem.h" // has to follow encoder instance and sampledefs.h
void maindisplay(void) {
display.clearDisplay();
display.setCursor(0,0);
display.setTextColor(WHITE);
display.println(" MORAD DRUMS");
display.println();
display.println("Press for Setup Menu");
display.display();
}
//when menu is suspended
result idle(menuOut& o,idleEvent e) {
maindisplay();
return proceed;
}
void setup() {
// WiFi.forceSleepBegin();
// delay(1);
// WiFi.disconnect();
// WiFi.mode(WIFI_OFF);
// WiFi.forceSleepBegin();
// delay(1);
// system_update_cpu_freq(160);
Serial.begin(115200);
Serial.print("Number of Samples ");
Serial.println(NUM_SAMPLES);
/*
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Serial.print(F("IP address is "));
// Serial.println(WiFi.localIP());
AppleMIDI.begin("MORAD"); // 'MORAD' will show up as the session name
AppleMIDI.OnReceiveNoteOn(OnAppleMidiNoteOn);
*/
// set up I/O pins
// set up SPI pins for soft SPI
pinMode(SCLK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(DAC0_CS, OUTPUT);
pinMode(DAC1_CS, OUTPUT);
pinMode(ADC_CS, OUTPUT);
digitalWrite(DAC0_CS, HIGH); // deassert chip selects
digitalWrite(DAC1_CS, HIGH);
digitalWrite(ADC_CS, HIGH);
pinMode(GATEout_0, OUTPUT);
pinMode(GATEout_1, OUTPUT);
pinMode(GATEout_2, OUTPUT);
pinMode(GATEout_3, OUTPUT);
pinMode(GATEin_0, INPUT);
pinMode(GATEin_1, INPUT);
pinMode(GATEin_2, INPUT);
pinMode(GATEin_3, INPUT);
// Set up serial MIDI port
MIDISerial.begin(31250, SERIAL_8N1, MIDIRX,MIDITX ); // midi port
// set up serial MIDI library callbacks
MIDI.setHandleNoteOn(HandleNoteOn); //
// Initiate serial MIDI communications, listen to all channels
MIDI.begin(MIDI_CHANNEL_OMNI);
// set up timer interrupt to update DAC
// Use 1st timer of 4 (counted from zero).
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more info).
timer0 = timerBegin(0, 80, true);
// Attach timer ISR to our timer.
timerAttachInterrupt(timer0, &onTimer, true);
// Set alarm to trigger ISR (value in microseconds).
// Repeat the alarm (third parameter)
timerAlarmWrite(timer0, DAC_TIMER_MICROS, true);
// Start the timer
timerAlarmEnable(timer0);
// 2nd timer for encoder and trigger sampling
timer1 = timerBegin(1, 80, true);
timerAttachInterrupt(timer1, &encTimer, true);
// Set alarm to trigger ISR (value in microseconds).
// Repeat the alarm (third parameter)
timerAlarmWrite(timer1, ENC_TIMER_MICROS, true);
timerAlarmEnable(timer1);
// start up the OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.println("MORAD DRUMS Feb 3/19");
display.display();
delay(3000);
// set up the menu system
nav.idleTask=idle;//point a function to be used when menu is suspended
nav.idleOn(); // start up in idle state
nav.navRoot::timeOut=30; // inactivity timeout
maindisplay();
}
void loop() {
MIDI.read(); // do serial MIDI
//do menus on a need to draw basis:
nav.doInput();
if (nav.changed(0)) {//only draw if changed
nav.doOutput();
display.display();
}
}