-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_stream.ino
320 lines (259 loc) · 8.21 KB
/
audio_stream.ino
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
// define one of these to determine mode of operation
//#define STREAM_PCM
//#define STREAM_DFPWM
//#define STORED_PCM
#define STORED_DFPWM
#if defined(STORED_PCM) || defined(STORED_DFPWM)
#include "sounddata.h"
#else
//////////////////// adjustable variables ////////////////////
// tested maximum sample rates before audio begins skipping at a baud rate of 2_000_000:
// STREAM_DFPWM: 42000Hz
// STREAM_PCM: 90000Hz
#define SAMPLE_RATE 48000UL
#endif
#define BAUD_RATE 2000000
// must be a multiple of 8
#define PLAYBACK_BUFFER_SIZE 512
#define BUFFER_COUNT 3
// DFPWM parameters
#define RESP_PREC 10
#define RESP_INC 1
#define RESP_DEC 1
#define LPF_STRENGTH 140
//////////////////////////////////////////////////////////////
volatile int currentReadBuffer = 0;
volatile int currentWriteBuffer = 0;
volatile int8_t buffers[BUFFER_COUNT][PLAYBACK_BUFFER_SIZE];
volatile boolean bufferEmpty[BUFFER_COUNT];
typedef struct {
int16_t level { 0 };
int16_t response { 0 };
boolean lastBit { false };
void predict(boolean input) {
int16_t target = input ? 127 : -128;
int16_t newLevel = (uint16_t) ((uint32_t) level + (uint16_t) (((uint32_t) response * (target - level) + (1 << (RESP_PREC - 1))) >> RESP_PREC));
if (level == newLevel && level != target) {
newLevel += input ? 1 : -1;
}
int16_t responseDelta;
int16_t responseTarget;
if (input == lastBit) {
responseDelta = RESP_INC;
responseTarget = (1 << RESP_PREC) - 1;
} else {
responseDelta = RESP_DEC;
responseTarget = 0;
}
int16_t newResponse = (uint16_t) ((uint32_t) response + ((uint32_t)responseDelta * (responseTarget - response) + 128) / 256);
if (response == newResponse && response != responseTarget) {
if (responseTarget < response) {
newResponse -= 1;
}
if (responseTarget > response) {
newResponse += 1;
}
}
if (RESP_PREC > 8 && newResponse < (2 << (RESP_PREC - 8))) {
newResponse = 2 << (RESP_PREC - 8);
}
response = newResponse;
level = newLevel;
lastBit = input;
}
} DfpwmPredictor;
typedef struct {
DfpwmPredictor* predictor;
int16_t filterLastLevel { 0 };
int16_t lpfLevel { 0 };
void dfpwmDecode(uint8_t data, int16_t* output) {
for (int i = 0; i < 8; i++) {
boolean b = ((data >> i) & 1) != 0;
boolean lastBit = predictor->lastBit;
predictor->predict(b);
int16_t blevel;
if (b == lastBit) {
blevel = predictor->level;
} else {
blevel = (filterLastLevel + predictor->level + 1) / 2;
}
filterLastLevel = predictor->level;
lpfLevel += (LPF_STRENGTH * (blevel - lpfLevel) + 128) / 256;
output[i] = lpfLevel;
}
}
} DfpwmDecoder;
DfpwmPredictor predictor;
DfpwmDecoder decoder;
void setup() {
Serial.begin(BAUD_RATE);
Serial.setTimeout(50);
decoder.predictor = &predictor;
memset(&bufferEmpty[0], true, BUFFER_COUNT);
memset(&buffers[0][0], 0, BUFFER_COUNT * PLAYBACK_BUFFER_SIZE);
pinMode(10, OUTPUT); //Speaker on pin 10
//disable interrupts while registers are configured
cli();
//set Timer2 to fast PWM mode (doubles PWM frequency)
bitSet(TCCR2A, WGM20);
bitSet(TCCR2A, WGM21);
// sets prescaler to 1: 62.5kHz
bitSet(TCCR2B, CS20);
bitClear(TCCR2B, CS21);
bitClear(TCCR2B, CS22);
// enable pin 10 output
bitClear(TCCR2A, COM2A0); bitSet( TCCR2A, COM2A1);
// stores { enum register value, divisor }
const uint16_t prescalers[5][2] = {
{1, 1},
{2, 8},
{3, 64},
{4, 256},
{5, 1024}
};
uint8_t prescaler = 1;
uint32_t ocr = 1;
for (int i = 0; i < 5; i++) {
uint16_t divisor = prescalers[i][1];
uint32_t count = F_CPU / (divisor * SAMPLE_RATE) - 1;
if (count < (1UL << 16) - 1) {
if (count + 1 < (1UL << 16) - 1) {
// round up if more precise
if (F_CPU - (count + 1) * divisor * SAMPLE_RATE
> (count + 2) * divisor * SAMPLE_RATE - F_CPU)
{
count += 1;
}
}
ocr = count;
prescaler = prescalers[i][0];
Serial.print("Chosen values for sample rate of ");
Serial.print(SAMPLE_RATE);
Serial.print("Hz: ");
Serial.print("OCR = ");
Serial.print(ocr);
Serial.print(", prescaler = ");
Serial.print(divisor);
Serial.print(", effective sample rate: ");
Serial.print((float) F_CPU / (divisor * (count + 1)));
Serial.println("Hz");
Serial.flush();
break;
}
}
#ifdef STREAM_PCM
Serial.println("Decoding serial as PCM u8");
#endif
#ifdef STREAM_DFPWM
Serial.println("Decoding serial as DFPWM");
#endif
// disconnect pins from our interrupt timer (COM4{A,B,C}{0, 1}
TCCR4A = 0;
TCCR4B = prescaler & 0b111;
TCCR4C = 0;
TIMSK4 = 0;
TIFR4 = 0;
// wave generation mode 4: CTC, counts up to value stored in OCR4A
bitSet(TCCR4B, WGM42);
bitSet(TIMSK4, OCIE4A); // Output Compare Interrupt Enable A
// 16 bit register (output compare A of timer 4)
OCR4A = (uint16_t) (ocr & 0xFFFF);
//enable interrupts
sei();
}
int playback = 0;
int stallCounter = 4 * -PLAYBACK_BUFFER_SIZE;
boolean playbackStarted = false;
ISR(TIMER4_COMPA_vect) {
if (bufferEmpty[currentReadBuffer]) {
if (playbackStarted) {
stallCounter++;
if (stallCounter > PLAYBACK_BUFFER_SIZE) {
Serial.println("Playback is stalling, reduce sample rate!");
stallCounter = 0;
}
}
currentReadBuffer = (currentReadBuffer + 1) % BUFFER_COUNT;
} else {
playbackStarted = true;
if (playback >= PLAYBACK_BUFFER_SIZE) {
bufferEmpty[currentReadBuffer] = true;
playback = 0;
currentReadBuffer = (currentReadBuffer + 1) % BUFFER_COUNT;
return;
}
// set duty cycle of timer 2 wave (-> analog voltage)
#if defined(STREAM_PCM) || defined(STORED_PCM)
OCR2A = buffers[currentReadBuffer][playback];
#endif
#if defined(STREAM_DFPWM) || defined(STORED_DFPWM)
OCR2A = buffers[currentReadBuffer][playback] + 128;
#endif
playback += 1;
}
}
// must be 8
#define DFPWM_BUF_SIZE 8
// stores decoded dfpwm samples
int16_t dfpwmBuf[DFPWM_BUF_SIZE];
#if defined(STORED_PCM) || defined(STORED_DFPWM)
uint32_t storedIndex = 0;
#endif
void loop() {
if (currentWriteBuffer == currentReadBuffer) {
// decoder is going too fast
return;
}
if (bufferEmpty[currentWriteBuffer]) {
/////////////////////// pcm modes ///////////////////////
#ifdef STREAM_PCM
uint8_t download[PLAYBACK_BUFFER_SIZE];
int read = 0;
while (read < PLAYBACK_BUFFER_SIZE) {
read += Serial.readBytes(&download[read], PLAYBACK_BUFFER_SIZE - read);
}
memcpy(&buffers[currentWriteBuffer][0], &download[0], PLAYBACK_BUFFER_SIZE);
#endif
#ifdef STORED_PCM
uint8_t download[PLAYBACK_BUFFER_SIZE];
sounddataRead(&download[0], PLAYBACK_BUFFER_SIZE, storedIndex);
storedIndex = (storedIndex + PLAYBACK_BUFFER_SIZE) % SOUNDDATA_LENGTH;
memcpy(&buffers[currentWriteBuffer][0], &download[0], PLAYBACK_BUFFER_SIZE);
#endif
/////////////////////// dfpwm modes ///////////////////////
#ifdef STREAM_DFPWM
uint8_t download[PLAYBACK_BUFFER_SIZE / DFPWM_BUF_SIZE];
int read = 0;
while (read < PLAYBACK_BUFFER_SIZE / DFPWM_BUF_SIZE) {
read += Serial.readBytes(&download[read], PLAYBACK_BUFFER_SIZE / DFPWM_BUF_SIZE - read);
}
#endif
#ifdef STORED_DFPWM
uint8_t download[PLAYBACK_BUFFER_SIZE / DFPWM_BUF_SIZE];
sounddataRead(&download[0], PLAYBACK_BUFFER_SIZE / DFPWM_BUF_SIZE, storedIndex);
storedIndex = (storedIndex + PLAYBACK_BUFFER_SIZE / DFPWM_BUF_SIZE) % SOUNDDATA_LENGTH;
#endif
#if defined(STREAM_DFPWM) || defined(STORED_DFPWM)
for (int i = 0; i < PLAYBACK_BUFFER_SIZE / DFPWM_BUF_SIZE; i++) {
decoder.dfpwmDecode(download[i], &dfpwmBuf[0]);
for (int j = 0; j < DFPWM_BUF_SIZE; j++) {
int16_t sample = dfpwmBuf[j];
int16_t clipped;
if (sample < 127) {
if (sample > -128) {
clipped = sample;
} else {
clipped = -128;
}
} else {
clipped = 127;
}
buffers[currentWriteBuffer][i * DFPWM_BUF_SIZE + j] = (int8_t) clipped;
}
}
#endif
// buffer is now filled
bufferEmpty[currentWriteBuffer] = false;
}
currentWriteBuffer = (currentWriteBuffer + 1) % BUFFER_COUNT;
}