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
|
/* 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
*
* dgreen - nutty portamento added!
*/
#include <MIDI.h>
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <Line.h> // for envelope
#include <mozzi_midi.h>
#include <ADSR.h>
#include <mozzi_fixmath.h>
#include <Portamento.h>
#include <tables/cos512_int8.h> // table for Oscils to play
#include <tables/square_analogue512_int8.h>
#include <tables/triangle_analogue512_int8.h>
#include <tables/saw_analogue512_int8.h> // table for Oscils to play
// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 256 // powers of 2 please
// Mozzi example uses COS waves for carrier and modulator
// Shit gets brutal very fast if you use a saw/square carrier
// Shit gets subtly more brutal if you use smaller wavetables (fewer samples per cycle)
Oscil<SAW_ANALOGUE512_NUM_CELLS, AUDIO_RATE> oscils[3] =
{(SAW_ANALOGUE512_DATA), (SAW_ANALOGUE512_DATA), (SAW_ANALOGUE512_DATA)};
Oscil<COS512_NUM_CELLS, AUDIO_RATE> oscModulator(COS512_DATA);
Oscil<COS512_NUM_CELLS, AUDIO_RATE> oscLFO(COS512_DATA);
enum TableType {
Cosine,
Square,
Saw,
Triangle
} currentTable;
const int8_t *tables[4] =
{ COS512_DATA, SQUARE_ANALOGUE512_DATA, SAW_ANALOGUE512_DATA, TRIANGLE_ANALOGUE512_DATA };
// envelope generator
ADSR <CONTROL_RATE, AUDIO_RATE> envelope;
Portamento <CONTROL_RATE>aPortamento;
MIDI_CREATE_DEFAULT_INSTANCE();
#define LED 13 // to see if MIDI is being recieved
#define TABLE_TOGGLE 10
// workaround for a idle buzzing bug
int sustainKillTimer = 0;
unsigned long vibrato = 0;
float carrierFreq = 10.f;
float modFreq = 10.f;
float modDepth = 0.5;
float amplitude = 0.f;
float modOffset = 1;
byte lastnote = 0;
//int portSpeed = 100;
byte detuneCents = 0;
int tableToggleTimer = 0;
int TABLE_TOGGLE_TIMER_MAX = 100;
float detuneCoefficient1 = 1;
float detuneCoefficient2 = 1;
float modOffsets[] = {
4, 3.5, 3, 2.5,
2, 1.5, 1, 0.6666667,
0.5, 0.4, 0.3333333, 0.2857,
0.25, 0, 0, 0,
0, 0, 0
}; // freq ratios corresponding to DP's preferred intervals of 7, 12, 7, 19, 24, 0, 12, -12, etc
int attackTime, decayTime, sustainTime, releaseTime;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(TABLE_TOGGLE, INPUT);
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setHandleNoteOn(HandleNoteOn);
MIDI.setHandleNoteOff(HandleNoteOff);
MIDI.setHandleControlChange(HandleControlChange);
MIDI.setHandlePitchBend(HandlePitchBend);
MIDI.setHandleProgramChange(HandleProgramChange);
envelope.setADLevels(255, 174);
attackTime = 188;
decayTime = 345;
sustainTime = 65000000; // long enough
releaseTime = 345;
envelope.setTimes(attackTime, decayTime, sustainTime, releaseTime); // 20000 is so the note will sustain 20 seconds unless a noteOff comes
envelope.setSustainLevel(255);
//aPortamento.setTime(50u);
oscLFO.setFreq(10); // default frequency
startMozzi(CONTROL_RATE);
}
void HandleProgramChange (byte channel, byte number)
{
// Use MIDI Program Change to select an interval between carrier and modulator oscillator
modOffset = modOffsets[number % 15];
}
void HandleNoteOn(byte channel, byte note, byte velocity)
{
//aPortamento.start((byte)(((int) note) - 5));
lastnote = note;
envelope.noteOn();
oscLFO.setPhase(0);
oscModulator.setPhase(0);
sustainKillTimer = -1;
digitalWrite(LED, HIGH);
}
void HandleNoteOff(byte channel, byte note, byte velocity)
{
if (note == lastnote)
{
envelope.noteOff();
sustainKillTimer = releaseTime;
digitalWrite(LED, LOW);
}
}
void HandlePitchBend (byte channel, int bend)
{
float shifted = float ((bend + 8500) / 2048.f) + 0.1f;
oscLFO.setFreq(shifted);
}
void HandleControlChange (byte channel, byte number, byte value)
{
if(number == 1)
{
float divided = float(value / 46.f);
modDepth = (divided * divided);
if (modDepth > 5)
{
modDepth = 5;
}
if (modDepth < 0.2)
{
modDepth = 0;
}
}
}
void setTables(const int8_t *TABLE_NAME) {
for (int i = 0; i < sizeof(oscils)/sizeof(oscils[0]); i++) {
oscils[i].setTable(TABLE_NAME);
}
}
void readPotsAndUpdate() {
if (tableToggleTimer > TABLE_TOGGLE_TIMER_MAX) {
byte toggled = digitalRead(TABLE_TOGGLE);
digitalWrite(LED, toggled);
if (toggled) {
currentTable = (TableType)(((byte)currentTable + 1) % 4);
setTables(tables[(unsigned) currentTable]);
tableToggleTimer = 0;
}
} else {
tableToggleTimer++;
}
int pot1 = mozziAnalogRead(1);
detuneCents = map(pot1, 0, 1024, 0, 100);
}
void updateControl()
{
MIDI.read();
envelope.update();
carrierFreq = mtof(lastnote);
modFreq = carrierFreq * modOffset;
// update oscil types
readPotsAndUpdate();
detuneCoefficient1 = 1 + 0.0005946 * detuneCents;
detuneCoefficient2 = 1 - 0.0005946 * detuneCents;
/*
byte toggled = digitalRead(TABLE_TOGGLE);
digitalWrite(LED, toggled);
*/
// set carrier frequencies
oscils[0].setFreq(carrierFreq);
oscils[1].setFreq(carrierFreq * detuneCoefficient1);
oscils[2].setFreq(carrierFreq * detuneCoefficient2);
oscModulator.setFreq(modFreq);
}
int updateAudio()
{
vibrato = (unsigned long) (oscLFO.next() * oscModulator.next()) >> 7;
vibrato *= (unsigned long) (carrierFreq * modDepth) >> 3;
//int carrierSum = (oscCarrierMaster.phMod(vibrato) >> 3) + (oscCarrierSlave1.phMod(vibrato) >> 3) + (oscCarrierSlave2.phMod(vibrato) >> 3);
int carrierSum = 0;
for (int i = 0; i < sizeof(oscils)/sizeof(oscils[0]); i++) {
carrierSum += (oscils[i].next() >> 3);
}
int total = (carrierSum * (envelope.next() >> 1)) >> 8;
if (abs(total) < 5 || sustainKillTimer == 0)
total = 0;
if (sustainKillTimer > 0) {
sustainKillTimer--;
}
return total;
}
void loop()
{
audioHook();
}
|