forked from AloyseTech/SamdAudio
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSamdAudio.cpp
514 lines (415 loc) · 15.4 KB
/
SamdAudio.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
Copyright (c) 2016 Théo Meyer aka AloyseTech. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SamdAudio.h"
//*********************************************************************
//Global variables
File __audioFile[MAX_N_CHANNELS];
volatile bool __audioPlaying[MAX_N_CHANNELS]={false};
volatile bool __audioFileReady[MAX_N_CHANNELS] = {false};
volatile uint32_t __SampleIndex[MAX_N_CHANNELS];
uint8_t __WavSamples[MAX_N_CHANNELS][2][AUDIO_BUFFER_SIZE];
uint8_t rampDivisor[MAX_N_CHANNELS]={1};
uint8_t whichBuffer[MAX_N_CHANNELS]={0};
volatile bool fillNextBuffer[MAX_N_CHANNELS]={true};
volatile uint16_t __audioData;
void (*__onSoundCompletion[MAX_N_CHANNELS])(void); //function pointer to users completion callback function
SdFat* SamdAudioSdFat;
//int __Volume;
bool __criticalSection = false;
int __numOfChannelsUsed = 4;
// unused declarations, commented out
void TC5_Handler (void) __attribute__ ((weak, alias("AudioPlay_Handler")));
void TC3_Handler (void) __attribute__ ((weak, alias("AudioRead_Handler")));
//*********************************************************************
int SamdAudio::begin(uint32_t sampleRate, uint8_t numOfChannels, SdFat* sdfatToUse)
{
// Initialize pointer back to already initialized sdfat object
SamdAudioSdFat = sdfatToUse;
if(numOfChannels == 1 || numOfChannels == 2 || numOfChannels == 4)
{
// initialize the global variable with the channels to use
__numOfChannelsUsed = numOfChannels;
}
else
{
// bad input passed, assume all channels to be safe
__numOfChannelsUsed = MAX_N_CHANNELS;
}
// initialize arrays
for(uint8_t index=0; index<MAX_N_CHANNELS; index++)
{
__audioFileReady[index]=false;
__SampleIndex[index]=0;
__onSoundCompletion[index] = NULL;
}
/*Modules configuration */
dacConfigure();
configurePlayerTimer(sampleRate);
configureReaderTimer();
return 0;
}
void SamdAudio::end() {
disablePlayerTimer();
resetPlayerTimer();
disableReaderTimer();
analogWrite(A0, 0);
// disable the DAC so it doesn't leak noise
DAC->CTRLA.bit.ENABLE = 0x00;
}
//*********************************************************************
void SamdAudio::play(char *fname, uint8_t channel) {
// enable the DAC again here since originally it is
// just done in dacConfigure
DAC->CTRLA.bit.ENABLE = 0x01;
//if(channel<0 || channel>=__numOfChannelsUsed)//unsigned, cant be negative
if(channel>=__numOfChannelsUsed)
return;
disableReaderTimer();
if(__audioFileReady[channel])
__audioFile[channel].close();
__audioFile[channel] = SamdAudioSdFat->open(fname);
if(!__audioFile[channel])
{
//end();
//SerialUSB.println("Error opening file");
return;
}
whichBuffer[channel]=0;
fillNextBuffer[channel]=1;
__audioFile[channel].read(__WavSamples[channel][whichBuffer[channel]], AUDIO_BUFFER_SIZE);
__SampleIndex[channel]=0;
__audioFileReady[channel] = true;
rampDivisor[channel]=RAMPIN;
/*once the buffer is filled for the first time the counter can be started*/
if(alonePlaying(channel))
{
enablePlayerTimer();
}
__audioPlaying[channel]=true;
enableReaderTimer();
}
void SamdAudio::play(char *fname, uint8_t channel, void (*functionToCallWhenComplete)(void))
{
// assign the callback function pointer
__onSoundCompletion[channel] = functionToCallWhenComplete;
// pass the sound to be played
// will call callback when done
play(fname, channel);
}
void SamdAudio::stopChannel(uint8_t c)
{
//if(c>=0 && c<__numOfChannelsUsed) //unsigned, cant be less than zero
if(c<__numOfChannelsUsed)
{
__audioFileReady[c]=false;
__audioFile[c].close();
__SampleIndex[c]=0;
__audioPlaying[c]=false;
}
}
//*********************************************************************
bool SamdAudio::alonePlaying(uint8_t channel)
{
for(uint8_t index=0; index<__numOfChannelsUsed; index++)
{
if(index!=channel && __audioPlaying[index])
return false;
}
return true;
}
bool __channelsPlaying()
{
for(uint8_t index=0; index<__numOfChannelsUsed; index++)
{
if(__audioPlaying[index])
return true;
}
return false;
}
//*********************************************************************
void SamdAudio::criticalON() {
__criticalSection = true;
}
void SamdAudio::criticalOFF() {
__criticalSection = false;
}
//*********************************************************************
/**
* Configures the DAC in event triggered mode.
*
* Configures the DAC to use the module's default configuration, with output
* channel mode configured for event triggered conversions.
*/
void SamdAudio::dacConfigure(void){
analogWriteResolution(10);
// analogWrite(A0, 0);
DAC->CTRLA.bit.ENABLE = 0x01;
DAC->DATA.reg = 0;
while (DAC->STATUS.bit.SYNCBUSY == 1);
}
/**
* Configures the TC to generate output events at the sample frequency.
*
* Configures the TC in Frequency Generation mode, with an event output once
* each time the audio sample frequency period expires.
*/
void SamdAudio::configurePlayerTimer(uint32_t sampleRate)
{
// Enable GCLK for TCC2 and TC5 (timer counter input clock)
GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_TC4_TC5)) ;
while (GCLK->STATUS.bit.SYNCBUSY);
resetPlayerTimer();
// Set Timer counter Mode to 16 bits
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
// Set TC5 mode as match frequency
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ;
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1 | TC_CTRLA_ENABLE;
TC5->COUNT16.CC[0].reg = (uint16_t) (SystemCoreClock / sampleRate - 1);
while (syncPlayerTimer());
// Configure interrupt request
NVIC_DisableIRQ(TC5_IRQn);
NVIC_ClearPendingIRQ(TC5_IRQn);
NVIC_SetPriority(TC5_IRQn, 0);
NVIC_EnableIRQ(TC5_IRQn);
// Enable the TC5 interrupt request
TC5->COUNT16.INTENSET.bit.MC0 = 1;
while (syncPlayerTimer());
}
bool SamdAudio::syncPlayerTimer()
{
return TC5->COUNT16.STATUS.reg & TC_STATUS_SYNCBUSY;
}
void SamdAudio::enablePlayerTimer()
{
// Enable TC
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
while (syncPlayerTimer());
}
void SamdAudio::resetPlayerTimer()
{
// Reset TCx
TC5->COUNT16.CTRLA.reg = TC_CTRLA_SWRST;
while (syncPlayerTimer());
while (TC5->COUNT16.CTRLA.bit.SWRST);
}
void SamdAudio::disablePlayerTimer()
{
// Disable TC5
TC5->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (syncPlayerTimer());
}
void SamdAudio::configureReaderTimer()
{
// The GCLK clock provider to use
// GCLK0, GCLK1 & GCLK3 are used already, see startup.c
const uint8_t GCLK_SRC = 5;
// Configure the XOSC32K to run in standby
//SYSCTRL->XOSC32K.bit.RUNSTDBY = 1;
// Setup clock provider GCLK_SRC with a /2 source divider
// GCLK_GENDIV_ID(X) specifies which GCLK we are configuring
// GCLK_GENDIV_DIV(Y) specifies the clock prescalar / divider
// If GENCTRL.DIVSEL is set (see further below) the divider
// is 2^(Y+1). If GENCTRL.DIVSEL is 0, the divider is simply Y
// This register has to be written in a single operation
GCLK->GENDIV.reg = GCLK_GENDIV_ID(GCLK_SRC) | GCLK_GENDIV_DIV(2);
while ( GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
// Configure the GCLK module
// GCLK_GENCTRL_GENEN, enable the specific GCLK module
// GCLK_GENCTRL_SRC_XOSC32K, set the source to the XOSC32K
// GCLK_GENCTRL_ID(X), specifies which GCLK we are configuring
// GCLK_GENCTRL_DIVSEL, specify which prescalar mode we are using
// GCLK_RUNSTDBY, keep the GCLK running when in standby mode
// Output from this module is 16khz (32khz / 2)
// This register has to be written in a single operation.
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN |
GCLK_GENCTRL_SRC_XOSC32K |
GCLK_GENCTRL_ID(GCLK_SRC);
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
// Turn the power to the TC3 module on
PM->APBCMASK.reg |= PM_APBCMASK_TC3;
// Set TC3 (shared with TCC2) GCLK source to GCLK_SRC
// GCLK_CLKCTRL_CLKEN, enable the generic clock
// GCLK_CLKCTRL_GEN(X), specifies the GCLK generator source
// GCLK_CLKCTRL_ID(X), specifies which generic clock we are configuring
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN |
GCLK_CLKCTRL_GEN(GCLK_SRC) |
GCLK_CLKCTRL_ID(GCM_TCC2_TC3);
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
// Disable TC3. This is required (if enabled already)
// before setting certain registers
TC3->COUNT8.CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (TC3->COUNT8.STATUS.reg & TC_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
// Set the mode to 8 bit and set it to run in standby
// TC_CTRLA_MODE_COUNT8, specify 8bit mode
// TC_CTRLA_RUNSTDBY, keep the module running when in standby
// TC_CTRLA_PRESCALER_DIVxx, set the prescalar to 64
// Prescalar options include: DIV1, DIV2, DIV4, DIV8,
// DIV16, DIV64, DIV256, DIV1024
TC3->COUNT8.CTRLA.reg = TC_CTRLA_MODE_COUNT8 |
TC_CTRLA_RUNSTDBY |
TC_CTRLA_PRESCALER_DIV1;
while (TC3->COUNT8.STATUS.reg & TC_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
// Enable the TC3 interrupt vector
// Set the priority to second (less important than feeding the DAC
NVIC_DisableIRQ(TC3_IRQn);
NVIC_ClearPendingIRQ(TC3_IRQn);
NVIC_SetPriority(TC3_IRQn, 0xFFFF);
NVIC_EnableIRQ(TC3_IRQn);
// Enable interrupt on overflow
// TC_INTENSET_OVF, enable an interrupt on overflow
TC3->COUNT8.INTENSET.reg = TC_INTENSET_OVF;
while (TC3->COUNT8.STATUS.reg & TC_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
// Enable TC3
TC3->COUNT8.CTRLA.reg |= TC_CTRLA_ENABLE;
while (TC3->COUNT8.STATUS.reg & TC_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
}
void SamdAudio::enableReaderTimer()
{
NVIC_EnableIRQ(TC3_IRQn);
TC3->COUNT8.CTRLA.reg |= TC_CTRLA_ENABLE;
while (TC3->COUNT8.STATUS.reg & TC_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
}
void SamdAudio::disableReaderTimer()
{
TC3->COUNT8.CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (TC3->COUNT8.STATUS.reg & TC_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
NVIC_DisableIRQ(TC3_IRQn);
}
//*********************************************************************
#ifdef __cplusplus
extern "C" {
#endif
// TC5 ISR
void AudioPlay_Handler (void)
{
__audioData=0;
for(uint8_t index=0; index<__numOfChannelsUsed; index++)
{
if (__audioPlaying[index])
{
if(__audioFile[index].available())
{
if (__SampleIndex[index] < AUDIO_BUFFER_SIZE - 1)
{
__audioData+=__WavSamples[index][whichBuffer[index]][__SampleIndex[index]++]/rampDivisor[index];
}
else //last sample from buffer
{
__audioData+=__WavSamples[index][whichBuffer[index]][__SampleIndex[index]++]/rampDivisor[index];
__SampleIndex[index] = 0;
if(!fillNextBuffer[index]) //we have been able to load next buffer : continue; else, loop the buffer...
whichBuffer[index]=1-whichBuffer[index];
fillNextBuffer[index]=1;
}
if(rampDivisor[index]>1)
rampDivisor[index]--;
}
//end of file, now play ramp out
else if (__audioFileReady[index])
{
__audioFile[index].close();
__audioFileReady[index] = false;
rampDivisor[index]=__WavSamples[index][whichBuffer[index]][__SampleIndex[index]]; //start ramp out from last audio sample
__audioData+=rampDivisor[index];
}
else if(rampDivisor[index]>0)//ramp out finish, end of activity on the channel
{
__audioData+=rampDivisor[index]--;
}
else
{
__audioPlaying[index]=false;
if(!__channelsPlaying())
{
//tc disable
TC5->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (TC5->COUNT16.STATUS.reg & TC_STATUS_SYNCBUSY);
TC3->COUNT8.CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (TC3->COUNT8.STATUS.reg & TC_STATUS_SYNCBUSY) {
/* Wait for synchronization */
}
}
// call this channels callback function when done if available
if(__onSoundCompletion[index] != NULL)
{
// call the callback function
__onSoundCompletion[index]();
// clear the callback function
__onSoundCompletion[index] = NULL;
}
}
}
}
if(__channelsPlaying())
{
if(__numOfChannelsUsed == 4 || __numOfChannelsUsed == 3)
{
__audioData>>=0;
}
else if(__numOfChannelsUsed == 2)
{
__audioData<<=1;
}
else if(__numOfChannelsUsed == 1)
{
__audioData<<=2;
}
DAC->DATA.reg = __audioData & 0x3FF; // DAC on 10 bits.
while (DAC->STATUS.bit.SYNCBUSY == 1);
}
// Clear the interrupt
TC5->COUNT16.INTFLAG.bit.MC0 = 1;
}
// TC3 ISR
void AudioRead_Handler()
{
if (TC3->COUNT8.INTFLAG.bit.OVF)
{
for(uint8_t index=0; index<__numOfChannelsUsed; index++)
{
if(__audioPlaying[index])
{
if(__audioFile[index].available() && !__criticalSection && fillNextBuffer[index])
{
__audioFile[index].read(__WavSamples[index][1-whichBuffer[index]], AUDIO_BUFFER_SIZE);
fillNextBuffer[index]=0;
}
}
}
// Reset interrupt flag
TC3->COUNT8.INTFLAG.bit.OVF = 1;
}
}
#ifdef __cplusplus
}
#endif