-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.c
403 lines (367 loc) · 11.1 KB
/
program.c
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
#include <stdlib.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "program.h"
#include "common.c"
#include "music/musicdata.c"
static uint64_t sleepUnit = 512;
static uint8_t rhythmUnit = 255; // also set dynamically by tracks
#define SLEEP_DELAY _delay_us(sleepUnit);
static void instSilence(channel *channel, state *state)
{
}
static void instRegular(channel *channel, state *state)
{
if (channel->currentPitchCount == 0) return;
if (channel->nextPitchIndex >= channel->currentPitchCount)
{
channel->nextPitchIndex = 0;
}
uint8_t finalTone = ((channel->currentTone)*(state->volume))/1024;
VSP_Write(finalTone, &channel->device->width);
VSP_Write(channel->currentPitches[channel->nextPitchIndex],
&channel->device->pitch);
channel->polyCycleCounter++;
if (channel->polyCycleCounter >= channel->polyCycleThreshold)
{
channel->polyCycleCounter = 0;
if (channel->nextPitchIndex < channel->currentPitchCount-1)
{
channel->nextPitchIndex++;
}
else if (channel->nextPitchIndex == channel->currentPitchCount-1)
{
channel->nextPitchIndex = 0;
}
}
}
const instrument instruments[] =
{
instSilence, instRegular
};
// timer counter 0
static void initializeTimerCounter0(void)
{
TCCR0A = (1 << COM0A1) | (1 << COM0B1) | (1 << WGM00);
TCCR0B = (1 << WGM02);
}
static void startTimerCounter0(void)
{
// 1 0 0 - clk/256
//SET_BIT(TCCR0B, CS00);
//SET_BIT(TCCR0B, CS01);
SET_BIT(TCCR0B, CS02);
}
static void stopTimerCounter0(void)
{
//UNSET_BIT(TCCR0B, CS00);
//UNSET_BIT(TCCR0B, CS01);
UNSET_BIT(TCCR0B, CS02);
}
// timer counter 1
static void initializeTimerCounter1(void)
{
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10);
TCCR1B = (1 << WGM13);
}
static void startTimerCounter1(void)
{
// 1 0 0 - clk/256
//SET_BIT(TCCR1B, CS10);
//SET_BIT(TCCR1B, CS11);
SET_BIT(TCCR1B, CS12);
}
static void stopTimerCounter1(void)
{
//UNSET_BIT(TCCR1B, CS10);
//UNSET_BIT(TCCR1B, CS11);
UNSET_BIT(TCCR1B, CS12);
}
// timer counter 2
static void initializeTimerCounter2(void)
{
TCCR2A = (1 << COM2A1) | (1 << COM2B1) | (1 << WGM20);
TCCR2B = (1 << WGM22);
}
static void startTimerCounter2(void)
{
// 1 0 0 - clk/256
//SET_BIT(TCCR2B, CS20);
SET_BIT(TCCR2B, CS21);
SET_BIT(TCCR2B, CS22);
}
static void stopTimerCounter2(void)
{
//UNSET_BIT(TCCR2B, CS20);
UNSET_BIT(TCCR2B, CS21);
UNSET_BIT(TCCR2B, CS22);
}
static void initializePortB(void)
{
DDRB = 0;
SET_BIT(DDRB, DDB1);
SET_BIT(DDRB, DDB2);
SET_BIT(DDRB, DDB3);
SET_BIT(DDRB, DDB5);
}
static void initializePortD(void)
{
DDRD = 0;
SET_BIT(DDRD, DDD3);
SET_BIT(DDRD, DDD5);
SET_BIT(DDRD, DDD6);
}
static void initializeAnalogInput(void)
{
ADMUX = 0;
ADCSRA = 0;
// reference voltage is vcc (5v?), 10 bit mode, using pin A0
ADMUX |= (1 << REFS0) | (0 << ADLAR);
// enable ADC module, set prescaler divisor value to 128
ADCSRA |= (1 << ADEN) | (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2);
}
static uint16_t readAnalogInput()
{
SET_BIT(ADCSRA, ADSC);
while (GET_BIT(ADCSRA, ADSC) == 1)
{
_delay_us(10);
}
return ADC;
}
static void initializeDevice8(device *device, volatile uint8_t *pitch, volatile uint8_t *width)
{
device->pitch.pointerSize = 8;
device->width.pointerSize = 8;
device->pitch.pointer.eight = pitch;
device->width.pointer.eight = width;
}
static void initializeDevice16(device *device, volatile uint16_t *pitch, volatile uint16_t *width)
{
device->pitch.pointerSize = 16;
device->width.pointerSize = 16;
device->pitch.pointer.sixteen = pitch;
device->width.pointer.sixteen = width;
}
static device* initializeDevices(uint8_t *numDevices)
{
*numDevices = 3;
device *devices = malloc((*numDevices) * sizeof(device));
// timer counter 0
initializeDevice8(&devices[0], &OCR0A, &OCR0B);
// timer counter 1
initializeDevice16(&devices[1], &OCR1A, &OCR1B);
// timer counter 2
initializeDevice8(&devices[2], &OCR2A, &OCR2B);
return devices;
}
static void initializeChannel(channel *channel, device *device)
{
channel->device = device;
channel->currentTone = 0;
for(int i = 0; i < 4; i++)
{
channel->currentPitches[i] = 255;
}
channel->currentPitchCount = 0;
channel->nextPitchIndex = 0;
channel->polyCycleThreshold = 96;
channel->polyCycleCounter = 0;
channel->instrument = instruments[0];
}
static channel* initializeChannels(uint8_t *numChannels, device *devices)
{
*numChannels = 3;
channel *channels = malloc((*numChannels) * sizeof(channel));
for (int i = 0; i < *numChannels; i++)
{
initializeChannel(&channels[i], &devices[i]);
}
return channels;
}
static void initializeTrack(track *track, channel *channel, sequence_t *sequence, uint16_t sequenceLength)
{
track->channel = channel;
track->sequence = sequence;
track->sLength = sequenceLength;
track->sPosition = 0;
track->remainingSleepTime = 0;
track->jPosition = 0;
}
static track* initializeTracks(uint8_t *numTracks, channel* channels)
{
*numTracks = 3;
track *tracks = malloc((*numTracks) * sizeof(track));
initializeTrack(&tracks[0], &channels[0], voiceOne, pgm_read_word(&voiceOneLength));
initializeTrack(&tracks[1], &channels[1], voiceTwo, pgm_read_word(&voiceTwoLength));
initializeTrack(&tracks[2], &channels[2], voiceThree, pgm_read_word(&voiceThreeLength));
return tracks;
}
static void readTrack(track *target)
{
if (target->remainingSleepTime > 0)
{
target->remainingSleepTime-=sleepUnit;
return;
}
if (target->sPosition >= target->sLength - 1)
{
target->remainingSleepTime = 0;
return;
}
sequence_t *tSequence = target->sequence;
uint16_t position = target->sPosition;
uint8_t code = pgm_read_byte(&tSequence[position]);
channel *tChannel = target->channel;
switch (code)
{
case 0:
// sleep for duration
target->remainingSleepTime =
pgm_read_byte(&tSequence[position+1]) * sleepUnit * rhythmUnit;
target->sPosition = position+2;
break;
case 1:
case 2:
case 3:
case 4:
// Set pitches
// pitches + sleep combo to save space
target->remainingSleepTime =
pgm_read_byte(&tSequence[position+code+1]) * sleepUnit * rhythmUnit;
tChannel->currentPitchCount = code;
target->sPosition = position + code + 2;
for (int i = 0; i < code; i++)
{
tChannel->currentPitches[i] = pgm_read_word(&tSequence[position+i+1]);
}
tChannel->nextPitchIndex = 0;
break;
case 11:
case 12:
case 13:
case 14:
code -= 10;
// Set pitches
// pitches + sleep + volume combo to save space
target->channel->currentTone = pgm_read_byte(&tSequence[position+code+1]);
target->remainingSleepTime =
pgm_read_byte(&tSequence[position+code+2]) * sleepUnit * rhythmUnit;
target->sPosition = position + code + 3;
tChannel->currentPitchCount = code;
for (int i = 0; i < code; i++)
{
tChannel->currentPitches[i] = pgm_read_word(&tSequence[position+i+1]);
}
tChannel->nextPitchIndex = 0;
break;
case 5:
// Set "volume" (voltage)
tChannel->currentTone =
pgm_read_byte(&tSequence[position+1]);
target->sPosition = position+2;
break;
case 6:
// Set instrument function
tChannel->instrument =
instruments[pgm_read_byte(&tSequence[position+1])];
target->sPosition = position+2;
break;
case 7:
// Set rhythm unit (tempo)
rhythmUnit =
pgm_read_byte(&tSequence[position+1]);
target->sPosition = position+2;
break;
case 8:
// Jump back (repeat)
if (target->jPosition == position)
{
target->sPosition = position+2;
target->jPosition = 0;
}
else
{
target->jPosition = position;
target->sPosition = position-(pgm_read_byte(&tSequence[position+1]));
}
break;
case 9:
// volume + sleep
tChannel->currentTone =
pgm_read_byte(&tSequence[position+1]);
target->remainingSleepTime =
pgm_read_byte(&tSequence[position+2]) * sleepUnit * rhythmUnit;
target->sPosition = position+3;
break;
default:
/*for (int i = 0; i < code; i++)
{
SET_BIT(PORTB, 5);
_delay_ms(500);
UNSET_BIT(PORTB, 5);
_delay_ms(500);
}*/
break;
}
}
static void readTracks(const uint8_t numTracks, track *tracks)
{
// check if it's time to loop
// and manually resync tracks
for (int i = 0; i < numTracks; i++)
{
if (tracks[i].sPosition >= tracks[i].sLength-1
&& tracks[i].remainingSleepTime <= 0)
{
// if true for ONE, sync ALL then continue!
for(int j = 0; j < numTracks; j++)
{
tracks[j].sPosition = 0;
tracks[j].jPosition = 0;
tracks[j].remainingSleepTime = 0;
}
break;
}
}
// proceed to execute track commands
for (int i = 0; i < numTracks; i++)
{
readTrack(&tracks[i]);
}
}
static void playChannels(const uint8_t numChannels, channel *channels, state *state)
{
for (int i = 0; i < numChannels; i++)
{
channels[i].instrument(&channels[i], state);
}
}
int main(void)
{
state *state = malloc(sizeof(struct state));
composition *composition = malloc(sizeof(struct composition));
initializePortB();
initializePortD();
initializeAnalogInput();
initializeTimerCounter0();
initializeTimerCounter1();
initializeTimerCounter2();
startTimerCounter0();
startTimerCounter1();
startTimerCounter2();
// initialize the devices - interfaces to audio emitting hardware
composition->devices = initializeDevices(&composition->numDevices);
// initialize the channels - device usage & state management
composition->channels = initializeChannels(&composition->numChannels, composition->devices);
// initialize the tracks - parallel streams of commands to the channels
composition->tracks = initializeTracks(&composition->numTracks, composition->channels);
for(;;)
{
state->volume = readAnalogInput();
readTracks(composition->numTracks, composition->tracks);
playChannels(composition->numChannels, composition->channels, state);
SLEEP_DELAY
}
}