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
|
/* Example of a sound being triggered by MIDI input.
*
* Demonstrates playing notes with Mozzi in response to MIDI input,
* using the standard Arduino MIDI library:
* http://playground.arduino.cc/Main/MIDILibrary
*
* Mozzi help/discussion/announcements:
* https://groups.google.com/forum/#!forum/mozzi-users
*
* Tim Barrass 2013.
* This example code is in the public domain.
*
* sgreen - modified to use standard Arduino midi library, added saw wave, lowpass filter
* Audio output from pin 9 (pwm)
* Midi plug pin 2 (centre) to Arduino gnd, pin 5 to RX (0)
* http://www.philrees.co.uk/midiplug.htm
* Now with drums! (still has all this code in there, commented out : )
S Green:
mod controller/ribbon is low pass filter cut-off
big button switches between tri/rectangle/noise/saw
(noise is at constant frequency)
NB quite easy to crash the filter with loud sounds, hit reset!
*/
#include <MIDI.h>
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <Line.h> // for envelope
#include <Sample.h>
#include <mozzi_utils.h>
#include <mozzi_analog.h>
#include <tables/sin2048_int8.h> // sine table for oscillator
//#include <tables/saw2048_int8.h>
//#include <tables/triangle2048_int8.h>
#include <mozzi_midi.h>
#include <ADSR.h>
#include <fixedMath.h>
#include <LowPassFilter.h>
#include "Wave.h"
#define MAX_NOTES 3
//Dave G: seems to do 3 note polyphony without clicking
#define DRUM_SAMPLES 0
#if DRUM_SAMPLES
#include "kick909.h"
#include "snare909.h"
#include "hihatc909.h"
#include "hihato909.h"
#endif
// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 128 // powers of 2 please
#define ENABLE_MIDI 1
#define DEBUG 0
#define BPM 120
#define STEPS_PER_BEAT 4
unsigned long stepCounter = 0;
unsigned long ticksPerStep = (60*CONTROL_RATE) / (BPM*STEPS_PER_BEAT);
// audio sinewave oscillator
//Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> osc(SIN2048_DATA);
//Oscil <SAW2048_NUM_CELLS, AUDIO_RATE> osc(SAW2048_DATA);
//Oscil <TRIANGLE2048_NUM_CELLS, AUDIO_RATE> osc(TRIANGLE2048_DATA);
//Wave myosc;
//Wave lfo;
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> lfo(SIN2048_DATA);
struct Channel {
Wave osc;
//Oscil <TRIANGLE2048_NUM_CELLS, AUDIO_RATE> osc;
ADSR <CONTROL_RATE> env;
byte note;
};
Channel chan[MAX_NOTES];
byte currentChan = 0;
// envelope generator
//ADSR <CONTROL_RATE> envelope;
LowPassFilter lpf;
int crushCtrl = 0;
int gain = 32;
float octave = 1.0f;
boolean enableDrums = false;
int step = 0;
int waveType = 1;
int button0_old = 0;
int button1_old = 0;
#if DRUM_SAMPLES
// drums
Sample <kick909_NUM_CELLS, AUDIO_RATE> kickSamp(kick909_DATA);
Sample <snare909_NUM_CELLS, AUDIO_RATE> snareSamp(snare909_DATA);
Sample <hihatc909_NUM_CELLS, AUDIO_RATE> hihatcSamp(hihatc909_DATA);
Sample <hihato_NUM_CELLS, AUDIO_RATE> hihatoSamp(hihato_DATA);
#endif
byte pattern[4][16] = {
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 hhc
0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, // 1 hho
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // 2 s
1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 // 3 k
};
// pin defines
#define LED 13 // to see if MIDI is being recieved
#define BUTTON0_PIN 2
#define BUTTON1_PIN 3
// forward declarations
void HandleNoteOn(byte channel, byte note, byte velocity);
void HandleNoteOff(byte channel, byte note, byte velocity);
void HandleControlChange (byte channel, byte number, byte value);
void HandlePitchBend (byte channel, int bend);
void setup() {
// init IO pins
pinMode(LED, OUTPUT);
#if 0
pinMode(BUTTON0_PIN, INPUT);
digitalWrite(BUTTON0_PIN, HIGH); // turn on pull-up resistor
pinMode(BUTTON1_PIN, INPUT);
digitalWrite(BUTTON1_PIN, HIGH); // turn on pull-up resistor
#endif
#if DEBUG
Serial.begin(57600); // for debugging
#endif
#if ENABLE_MIDI
// Initiate MIDI communications, listen to all channels
MIDI.begin(MIDI_CHANNEL_OMNI);
// Connect the HandleNoteOn function to the library, so it is called upon reception of a NoteOn.
MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
MIDI.setHandleNoteOff(HandleNoteOff); // Put only the name of the function
MIDI.setHandleControlChange(HandleControlChange);
MIDI.setHandlePitchBend(HandlePitchBend);
MIDI.setHandleContinue(HandleContinue);
MIDI.setHandleStop(HandleStop);
#endif
#if 0
//osc.setFreq(440u); // default frequency
//myosc.setFreq(440.0f);
myosc.setFreq(440.0f*4.0f);
myosc.setPulseWidth(32);
envelope.setADLevels(255, 200);
envelope.setTimes(50, 200, 65535, 200);
#endif
for(int i=0; i<MAX_NOTES; i++)
{
chan[i].osc.setType(WAVE_TRI);
//chan[i].osc.setTable(TRIANGLE2048_DATA);
chan[i].osc.setPulseWidth(16);
chan[i].env.setADLevels(128, 100);
chan[i].env.setTimes(100, 200, 65535, 200);
}
//lfo.setType(WAVE_TRI);
lfo.setFreq(10.0f);
lpf.setResonance(128);
lpf.setCutoffFreq(255);
#if DRUM_SAMPLES
kickSamp.setFreq((float) kick909_SAMPLERATE / (float) kick909_NUM_CELLS);
snareSamp.setFreq((float) snare909_SAMPLERATE / (float) snare909_NUM_CELLS);
hihatcSamp.setFreq((float) hihatc909_SAMPLERATE / (float) hihatc909_NUM_CELLS);
hihatoSamp.setFreq((float) hihato_SAMPLERATE / (float) hihato_NUM_CELLS);
//snareSamp.start();
#endif
//setupFastAnalogRead(); // optional
adcEnableInterrupt(); // for analog reads
startMozzi(CONTROL_RATE);
}
void HandleNoteOn(byte channel, byte note, byte velocity) {
//osc.setFreq(mtof(note)); // simple but less accurate frequency
//osc.setFreq_Q16n16(Q16n16_mtof(Q8n0_to_Q16n16(note))); // accurate frequency
//myosc.setFreq(mtof(note)); // accurate frequency
//envelope.noteOn();
// start note on next channel - NB no "note priority" at this stage
chan[currentChan].note = note;
chan[currentChan].osc.setFreq( mtof(note) ); // accurate frequency
chan[currentChan].env.noteOn();
currentChan = (currentChan + 1) % MAX_NOTES; // just wraps around for now
digitalWrite(LED,HIGH);
}
void HandleNoteOff(byte channel, byte note, byte velocity) {
//envelope.noteOff();
// find which channel was playing this note
for(int i=0; i<MAX_NOTES; i++) {
if (chan[i].note == note) {
// kill it
chan[i].env.noteOff();
}
}
digitalWrite(LED,LOW);
}
void HandleControlChange (byte channel, byte number, byte value)
{
// http://www.indiana.edu/~emusic/cntrlnumb.html
switch(number) {
case 1: // modulation wheel
//gain = value*2;
lpf.setCutoffFreq(value*2); // control messages are in [0, 127] range
break;
case 105:
lpf.setCutoffFreq(value*2); // control messages are in [0, 127] range
break;
case 106:
//lpf.setResonance(value*2);
crushCtrl = value;
break;
}
}
// pitchbend = control strip + button on Keytar
void HandlePitchBend (byte channel, int bend)
{
//bend value from +/-8192
//lfo.setFreq((unsigned int) (bend+8192)>>7);
for(int i=0; i<MAX_NOTES; i++) {
chan[i].osc.setPulseWidth((bend+8192) >> 8);
}
}
void HandleStop () { //this is the Back button on the Xbox keyboard
// octave *= 2.0f;
// if (octave > 16.0f) octave = 1.0f;
//Dave G: no longer used : )
}
void HandleContinue () { // the big round button on all keytars
// change wave
waveType = (waveType + 1) & 3;
for(int i=0; i<MAX_NOTES; i++)
{
#if 0
switch(waveType) {
case 0:
chan[i].osc.setTable(TRIANGLE2048_DATA);
break;
case 1:
chan[i].osc.setTable(SAW2048_DATA);
break;
case 2:
chan[i].osc.setTable(SIN2048_DATA);
break;
}
#else
for(int i=0; i<MAX_NOTES; i++)
{
chan[i].osc.setType((WaveType) waveType);
}
#endif
}
}
void updateControl(){
#if ENABLE_MIDI
MIDI.read();
#endif
//envelope.update();
// update playing envelopes
for(int i=0; i<MAX_NOTES; i++) {
chan[i].env.update();
}
#if 0
// push buttons
int button0 = !digitalRead(BUTTON0_PIN); // active low
int button1 = !digitalRead(BUTTON1_PIN);
/*
if (button0) {
// trigger
//envelope.noteOn();
//lpf.reset();
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
*/
if (button0 && !button0_old) {
lfo.setType((WaveType) ((((int) lfo.getType()) + 1) % 3));
}
button0_old = button0;
if (button1 & !button1_old) {
waveType = (waveType + 1) & 3;
//myosc.setType((WaveType) waveType);
for(int i=0; i<MAX_NOTES; i++)
{
chan[i].osc.setType((WaveType) waveType);
}
}
button1_old = button1;
#endif
#if DRUM_SAMPLES
if (enableDrums) {
// drums
stepCounter++;
if (stepCounter > ticksPerStep) {
step = (step+1) & 0xf;
stepCounter = 0;
if (pattern[0][step]) hihatcSamp.start();
if (pattern[1][step]) hihatoSamp.start();
if (pattern[2][step]) snareSamp.start();
if (pattern[3][step]) kickSamp.start();
}
}
#endif
#if 0
// knobs
int knob0 = adcGetResult(0); // [0, 1023]
int knob1 = adcGetResult(1);
//lpf.setCutoffFreq(knob0>>2);
//lpf.setResonance(knob1>>2);
//crushCtrl = knob1>>7;
lfo.setFreq((float) knob0);
//myosc.setFreq((float) knob1);
//myosc.setFreq((unsigned int) knob0*20);
//myosc.setPulseWidth((unsigned char) (knob1>>2));
// start the next read cycle in the background
adcReadAllChannels();
#endif
#if DEBUG
Serial.println(knob0);
#endif
}
// strip the low bits off!
int bitCrush(int x, int a)
{
return (x>>a)<<a;
}
int updateAudio()
{
//int x = (int) (envelope.next() * osc.next())>>8;
//int x = (int) (envelope.next() * myosc.next())>>8;
//int x = (int) (envelope.next() * (lfo.next() * myosc.next())>>8)>>8;
//int x = (int) lfo.next();
//int x = (int) (lfo.next() * myosc.next())>>8;
//int x = (int) (envelope.next() * lpf.next(myosc.next()))>>8;
//int x = (envelope.next() * lpf.next(osc.next()))>>8;
//x = bitCrush(x, crushCtrl>>4);
//x = bitCrush(x, crushCtrl);
//x = (x * crushCtrl)>>4; // simple gain
//x = x>>2;
//x = lpf.next(x);
// sum up channels
int x = 0;
for(int i=0; i<MAX_NOTES; i++) {
x += (int) (chan[i].env.next() * chan[i].osc.next())>>8;
}
//Dave G: might crash less if this went through some sort of waveshaper here?
//x = (x*gain)>>8;
//x = (x * lfo.next())>>8;
x = lpf.next(x);
#if DRUM_SAMPLES
if (enableDrums) {
// drums please!
int drums = kickSamp.next() + snareSamp.next() + hihatcSamp.next() + hihatoSamp.next();
//drums = (drums>>4)<<4;
x += drums*2;
}
#endif
return x;
}
void loop() {
audioHook(); // required here
}
|