-
Notifications
You must be signed in to change notification settings - Fork 2
/
playroutine.cpp
331 lines (295 loc) · 8.28 KB
/
playroutine.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
#include "playroutine.h"
// frequencies for first 8 octaves (notes C0-B7)
uint16_t pitches[] =
{
0x0043,0x0046,0x004b,0x004f,0x0054,0x0059,0x005e,0x0064,0x006a,0x0070,0x0077,0x007e,
0x0086,0x008d,0x0096,0x009f,0x00a8,0x00b2,0x00bd,0x00c8,0x00d4,0x00e1,0x00ee,0x00fc,
0x010b,0x011b,0x012c,0x013e,0x0151,0x0165,0x017a,0x0191,0x01a9,0x01c2,0x01dd,0x01f9,
0x0217,0x0237,0x0259,0x027d,0x02a3,0x02cb,0x02f5,0x0322,0x0352,0x0385,0x03ba,0x03f3,
0x042f,0x046f,0x04b2,0x04fa,0x0546,0x0596,0x05eb,0x0645,0x06a5,0x070a,0x0775,0x07e6,
0x085f,0x08de,0x0965,0x09f4,0x0a8c,0x0b2c,0x0bd6,0x0c8a,0x0d49,0x0e14,0x0eea,0x0fcd,
0x10bd,0x11bc,0x12ca,0x13e8,0x1517,0x1658,0x17ac,0x1915,0x1a93,0x1c27,0x1dd4,0x1f9a,
0x217b,0x2378,0x2594,0x27d0,0x2a2e,0x2cb0,0x2f58,0x3229,0x3524,0x384d,0x3ba6,0x3f32,
};
// sinetable for vibrato effect
int8_t sintab[] =
{
0,6,12,17,22,26,29,31,31,31,29,26,22,17,12,6,0,-7,-13,-18,-23,-27,-30,-32,-32,-32,
-30,-27,-23,-18,-13,-7
};
Instrument instruments[INSTRUMENTS] =
{
{ 1,0x71,0x59,0x14,0x10,0x7f,0x9,0,0,0 },
{ 3,127,0x0f,0,127,0,0,81,76,0 },
{ 1,113,89,20,16,208,21,0,0,0 },
{ 1,22,65,0,127,76,29,31,98,0 },
{ 0x01,0x61,0x0d,0x00,0x00,0x7f,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
};
Channel channel[OSCILLATORS];
Track tracks[TRACKS];
Song song;
uint8_t songpos = 0;
uint8_t trackpos = 0;
uint8_t trackcounter = 0;
uint8_t tempo = 5; // 10 = 150bpm, smaller values = faster tempo
bool loopPattern = false;
void initPlayroutine()
{
memset(channel, 0, sizeof(channel));
memset(tracks, 0, sizeof(tracks));
for(int i = 0; i < TRACKS; i++)
for(int j = 0; j < TRACK_LENGTH; j++)
tracks[i].lines[j].note = 0xff;
for(int i = 0; i < CHANNELS; i++)
song.tracks[0][i] = i;
for(int i = 1; i < SONG_LENGTH; i++)
for(int j = 0; j < CHANNELS; j++)
song.tracks[i][j] = 0xff;
}
void resetPlayroutine()
{
memset(channel, 0, sizeof(channel));
}
void playNote(uint8_t voice, uint8_t note, uint8_t instrNum)
{
Instrument* instr = &instruments[instrNum];
uint16_t freq = pitches[note];
osc[voice].frequency = freq;
osc[voice].ctrl = 1;
osc[voice].waveform = instr->waveform;
osc[voice].attack = instr->attack;
osc[voice].decay = instr->decay;
osc[voice].sustain = instr->sustain;
osc[voice].release = instr->release;
osc[voice].pulseWidth = instr->pulseWidth;
channel[voice].instrument = instrNum;
channel[voice].note = note;
channel[voice].frequency = freq;
channel[voice].vibratoPhase = 0;
channel[voice].effect = 0;
channel[voice].arpPhase1 = 0;
channel[voice].arpPhase2 = 0;
instr->pulseWidthPhase = 0;
// reseed noise
if(instr->waveform == NOISE)
{
noise = 0xACE1;
osc[voice].phase = 0;
}
}
void noteOff(uint8_t voice)
{
osc[voice].ctrl = 0;
// in case the oscillator was in attack phase, the amplitude may have been higher than sustain amplitude
// with low sustain and long release, this produces odd behavior so we'll clamp oscillator amplitude
// to sustain level when note is released
osc[voice].amp = min(osc[voice].amp, osc[voice].sustain<<8);
}
void recordNote(uint8_t voice, uint8_t note, uint8_t instrNum)
{
// round trackpos to nearest track slot index
uint8_t tpos = trackpos;
if(trackcounter >= tempo>>1)
tpos = (tpos+1) & (TRACK_LENGTH-1);
Track* tr = &tracks[voice];
tr->lines[tpos].note = note;
tr->lines[tpos].instrument = instrNum;
}
// Updates track playback. Called at 50hz.
void playroutine()
{
trackcounter++;
if(trackcounter < tempo)
return;
// we get here 8 times per beat (1/8th notes)
// play notes
for(int i = 0; i < CHANNELS; i++)
{
Track* tr = &tracks[song.tracks[songpos][i]];
uint8_t note = tr->lines[trackpos].note;
uint8_t effect = tr->lines[trackpos].effect >> 4;
if(note < 0x80)
{
if(effect != SLIDE_NOTE)
playNote(i, note, tr->lines[trackpos].instrument);
}
else if(note == 0xfe)
{
noteOff(i);
}
// set channel effect
if(effect)
{
channel[i].effect = tr->lines[trackpos].effect;
if(effect == SLIDE_NOTE)
{
if(note < 0x80)
channel[i].targetFrequency = pitches[note];
else
channel[i].effect = 0; // no note to slide to
}
else if(effect == DRUM)
{
noise = 0xACE1;
osc[i].waveform = NOISE;
osc[i].phase = 0;
}
else if(effect == VOLUME_DOWN || effect == VOLUME_UP)
{
osc[i].ctrl = 3; // manual volume control
}
}
}
// increment track pos
trackpos = (trackpos+1);
trackcounter = 0;
// track finished?
if(trackpos == TRACK_LENGTH)
{
if(!loopPattern)
{
songpos++;
// restart song?
if(songpos == SONG_LENGTH)
songpos = 0;
if(song.tracks[songpos][0] == 0xff)
songpos = 0;
}
trackpos = 0;
}
// #ifdef ARDUINO
// // flash led 4 times per beat
// if((trackpos & 1) == 0)
// digitalWrite(beatLedPin, HIGH); //trackpos & 1 == 0);
// else
// digitalWrite(beatLedPin, LOW); //trackpos & 1 == 0);
// #endif
}
// called at 50hz
void updateEffects()
{
for(int i=0; i<OSCILLATORS; i++)
{
Channel* chan = &channel[i];
Instrument* instr = &instruments[chan->instrument];
uint16_t f = chan->frequency;
// vibrato
uint8_t vibscaler = f>>4;
f += ((sintab[chan->vibratoPhase>>3] * instr->vibratoDepth) >> 5) * vibscaler >> 3;
chan->vibratoPhase += instr->vibratoSpeed;
// pulse width
if(instr->pulseWidthSpeed != 0)
{
instr->pulseWidthPhase += instr->pulseWidthSpeed<<4;
uint8_t phase = instr->pulseWidthPhase>>8;
if(phase < 128)
osc[i].pulseWidth = phase*2;
else
osc[i].pulseWidth = (255 - phase)*2;
// remap pulse width from [0,255] to [10,245]
osc[i].pulseWidth = ((osc[i].pulseWidth * (245-10))>>8) + 10;
}
// effect
uint8_t effect = chan->effect>>4;
uint8_t param = chan->effect & 0xf;
switch(effect)
{
case SLIDEUP:
chan->frequency += param*4;
break;
case SLIDEDOWN:
chan->frequency = max((int16_t)chan->frequency - param*4, 0);
break;
case ARPEGGIO:
if(param == ARPEGGIO_MAJOR)
{
// note +0,+4,+7
chan->arpPhase1++;
if(chan->arpPhase1 >= 2)
{
chan->arpPhase2++;
if(chan->arpPhase2 >= 3)
chan->arpPhase2 = 0;
chan->arpPhase1 = 0;
}
if(chan->arpPhase2 == 0)
f = pitches[chan->note];
if(chan->arpPhase2 == 1)
f = pitches[chan->note+4];
if(chan->arpPhase2 == 2)
f = pitches[chan->note+7];
}
else if(param == ARPEGGIO_MINOR)
{
// note +0,+3,+7
chan->arpPhase1++;
if(chan->arpPhase1 >= 2)
{
chan->arpPhase2++;
if(chan->arpPhase2 >= 3)
chan->arpPhase2 = 0;
chan->arpPhase1 = 0;
}
if(chan->arpPhase2 == 0)
f = pitches[chan->note];
if(chan->arpPhase2 == 1)
f = pitches[chan->note+3];
if(chan->arpPhase2 == 2)
f = pitches[chan->note+7];
}
else if(param == ARPEGGIO_DOMINANT_SEVENTH)
{
// note +0,+4,+7,10
chan->arpPhase1++;
if(chan->arpPhase1 >= 2)
{
chan->arpPhase2++;
if(chan->arpPhase2 >= 4)
chan->arpPhase2 = 0;
chan->arpPhase1 = 0;
}
if(chan->arpPhase2 == 0)
f = pitches[chan->note];
if(chan->arpPhase2 == 1)
f = pitches[chan->note+4];
if(chan->arpPhase2 == 2)
f = pitches[chan->note+7];
if(chan->arpPhase2 == 3)
f = pitches[chan->note+10];
}
else if(param == ARPEGGIO_OCTAVE)
{
if(chan->arpPhase1++ & 2)
f *= 2; // +1 octave
}
break;
case DRUM:
if(chan->arpPhase1++ == param+1)
osc[i].waveform = instr->waveform;
break;
case SLIDE_NOTE:
if(chan->targetFrequency > chan->frequency)
chan->frequency = min(chan->frequency + param*2, chan->targetFrequency);
else if(chan->targetFrequency < chan->frequency)
chan->frequency = max(chan->frequency - param*2, chan->targetFrequency);
break;
case VOLUME_DOWN:
osc[i].amp = max(osc[i].amp - param*32, 0);
break;
case VOLUME_UP:
osc[i].amp = min(osc[i].amp + param*8, 0x7fff);
break;
}
osc[i].frequency = f;
}
}