-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.c
323 lines (293 loc) · 9.47 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
#include <stdlib.h>
#include <stdint.h>
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "hardware/adc.h"
#include "common.h"
#include "music/musicdata.c"
#define MINIMAL_DELAY sleep_us(10);
#define SLEEP_DELAY sleep_us(sleepUnit);
static void instSilence(channel *channel, state *state);
static void instRegular(channel *channel, state *state);
static void initializePWMSlices(uint8_t first, uint8_t count, uint8_t intdiv, uint8_t fracdiv);
static void setPWMSlices(uint8_t first, uint8_t count, uint8_t value);
static void setPWMPorts(uint8_t first, uint8_t count);
static void initializeAnalogInput(void);
static float readAnalogInput();
static void initializeChannel(channel *channel, uint8_t device);
static channel* initializeChannels(uint8_t *numChannels);
static void initializeTrack(track *track, channel *channel, uint16_t *sequence, const uint16_t sequenceLength);
static track* initializeTracks(uint8_t *numTracks, channel* channels);
static void readTrack(track *target);
static void readTracks(const uint8_t numTracks, track *tracks);
static void playChannels(const uint8_t numChannels, channel *channels, state *state);
static const uint64_t sleepUnit = 512;
static uint8_t rhythmUnit = 255; // also set dynamically by tracks
const instrument instruments[] =
{
instSilence, instRegular
};
int main(void)
{
state *state = malloc(sizeof(struct state));
composition *composition = malloc(sizeof(struct composition));
initializePWMSlices(0, 5, 255, 15);
setPWMSlices(0, 5, true);
setPWMPorts(0, 10);
// skipping ADC for now
//initializeAnalogInput();
// initialize the channels - device usage & state management
composition->channels = initializeChannels(&composition->numChannels);
// initialize the tracks - parallel streams of commands to the channels
composition->tracks = initializeTracks(&composition->numTracks, composition->channels);
sleep_ms(500);
// giving up on ADC volume for now -
// clearly not as straightforward as it was in the atmega,
// and it wasn't the right way anyway
state->volume = 1.0;
for(;;)
{
readTracks(composition->numTracks, composition->tracks);
playChannels(composition->numChannels, composition->channels, state);
SLEEP_DELAY
}
}
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;
}
uint16_t pitch = channel->currentPitches[channel->nextPitchIndex];
pwm_set_wrap(channel->device, pitch);
uint16_t finalTone = (channel->currentTone)*(state->volume);
pwm_set_both_levels(channel->device, finalTone, finalTone);
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;
}
}
}
static void initializePWMSlices(uint8_t first, uint8_t count, uint8_t intdiv, uint8_t fracdiv)
{
for (int i = first; i < count; i++)
{
pwm_set_clkdiv_int_frac(i, intdiv, fracdiv);
pwm_set_phase_correct(i, true);
}
}
static void setPWMSlices(uint8_t first, uint8_t count, uint8_t value)
{
for (int i = first; i < count; i++)
{
pwm_set_enabled(i, value);
}
}
static void setPWMPorts(uint8_t first, uint8_t count)
{
for (int i = first; i < count; i++)
{
gpio_set_function(i, GPIO_FUNC_PWM);
}
}
static void initializeAnalogInput(void)
{
adc_init();
adc_gpio_init(29);
adc_select_input(3);
}
static float readAnalogInput()
{
static const float conversion_factor = 3.3f / (1 << 12);
return adc_read()*conversion_factor;
}
static void initializeChannel(channel *channel, uint8_t 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)
{
*numChannels = 3;
channel *channels = malloc((*numChannels) * sizeof(channel));
for (int i = 0; i < *numChannels; i++)
{
// add 2 for first port test, use whole range later
// TODO: remove +2
initializeChannel(&channels[i], i + 2);
}
return channels;
}
static void initializeTrack(track *track, channel *channel, uint16_t *sequence, const 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, voiceOneLength);
initializeTrack(&tracks[1], &channels[1], voiceTwo, voiceTwoLength);
initializeTrack(&tracks[2], &channels[2], voiceThree, 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;
}
const uint16_t *tSequence = target->sequence;
uint16_t position = target->sPosition;
uint8_t code = tSequence[position];
channel *tChannel = target->channel;
switch (code)
{
case 0:
// sleep for duration
target->remainingSleepTime =
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 =
tSequence[position+code+1] * sleepUnit * rhythmUnit;
tChannel->currentPitchCount = code;
target->sPosition = position + code + 2;
for (int i = 0; i < code; i++)
{
tChannel->currentPitches[i] = 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 = tSequence[position+code+1];
target->remainingSleepTime =
tSequence[position+code+2] * sleepUnit * rhythmUnit;
target->sPosition = position + code + 3;
tChannel->currentPitchCount = code;
for (int i = 0; i < code; i++)
{
tChannel->currentPitches[i] = tSequence[position+i+1];
}
tChannel->nextPitchIndex = 0;
break;
case 5:
// Set "volume" (voltage)
tChannel->currentTone =
tSequence[position+1];
target->sPosition = position+2;
break;
case 6:
// Set instrument function
tChannel->instrument =
instruments[tSequence[position+1]];
target->sPosition = position+2;
break;
case 7:
// Set rhythm unit (tempo)
rhythmUnit =
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-(tSequence[position+1]);
}
break;
case 9:
// volume + sleep
tChannel->currentTone =
tSequence[position+1];
target->remainingSleepTime =
tSequence[position+2] * sleepUnit * rhythmUnit;
target->sPosition = position+3;
break;
default:
// TODO: add some sort of warning behavior
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);
}
}