2012-06-18 01:53:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 by Cristian Maglie <c.maglie@bug.st>
|
|
|
|
* Audio library for Arduino Due.
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either the GNU General Public License version 2
|
|
|
|
* or the GNU Lesser General Public License version 2.1, both as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
2012-06-17 18:20:01 +02:00
|
|
|
|
|
|
|
#include "Audio.h"
|
|
|
|
|
2012-08-01 11:51:29 +02:00
|
|
|
void AudioClass::begin(uint32_t sampleRate, uint32_t msPreBuffer) {
|
|
|
|
// Allocate a buffer to keep msPreBuffer milliseconds of audio
|
|
|
|
bufferSize = msPreBuffer * sampleRate / 1000;
|
2012-09-29 00:12:25 +02:00
|
|
|
if (bufferSize < 1024)
|
|
|
|
bufferSize = 1024;
|
2012-08-01 11:51:29 +02:00
|
|
|
buffer = (uint32_t *) malloc(bufferSize * sizeof(uint32_t));
|
2012-09-29 00:12:25 +02:00
|
|
|
half = buffer + bufferSize / 2;
|
2012-08-01 11:51:29 +02:00
|
|
|
last = buffer + bufferSize;
|
|
|
|
|
|
|
|
// Buffering starts from the beginning
|
2012-09-29 00:12:25 +02:00
|
|
|
running = buffer;
|
2012-08-01 11:51:29 +02:00
|
|
|
next = buffer;
|
|
|
|
|
2012-09-29 00:12:25 +02:00
|
|
|
// Start DAC
|
2012-08-01 11:51:29 +02:00
|
|
|
dac->begin(VARIANT_MCK / sampleRate);
|
|
|
|
dac->setOnTransmitEnd_CB(onTransmitEnd, this);
|
2012-06-17 18:20:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioClass::end() {
|
2012-07-29 01:57:12 +02:00
|
|
|
dac->end();
|
2012-09-29 00:12:25 +02:00
|
|
|
free( buffer);
|
2012-08-01 11:51:29 +02:00
|
|
|
}
|
|
|
|
|
2012-10-20 00:46:02 +02:00
|
|
|
void AudioClass::prepare(int16_t *buffer, int S, int volume){
|
|
|
|
uint16_t *ubuffer = (uint16_t*) buffer;
|
|
|
|
for (int i=0; i<S; i++) {
|
|
|
|
// set volume amplitude (signed multiply)
|
|
|
|
buffer[i] = buffer[i] * volume / 1024;
|
|
|
|
// convert from signed 16 bit to unsigned 12 bit for DAC.
|
|
|
|
ubuffer[i] += 0x8000;
|
|
|
|
ubuffer[i] >>= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 11:51:29 +02:00
|
|
|
size_t AudioClass::write(const uint32_t *data, size_t size) {
|
|
|
|
const uint32_t TAG = 0x10000000;
|
|
|
|
int i;
|
2012-09-29 00:12:25 +02:00
|
|
|
for (i = 0; i < size; i++) {
|
2012-08-01 11:51:29 +02:00
|
|
|
*next = data[i] | TAG;
|
|
|
|
next++;
|
|
|
|
|
2012-09-29 00:12:25 +02:00
|
|
|
if (next == half || next == last) {
|
|
|
|
enqueue();
|
|
|
|
while (next == running)
|
|
|
|
;
|
|
|
|
}
|
2012-08-01 11:51:29 +02:00
|
|
|
}
|
2012-09-29 00:12:25 +02:00
|
|
|
|
2012-08-01 11:51:29 +02:00
|
|
|
return i;
|
2012-06-17 18:20:01 +02:00
|
|
|
}
|
|
|
|
|
2012-08-01 11:51:29 +02:00
|
|
|
void AudioClass::enqueue() {
|
2012-09-29 00:12:25 +02:00
|
|
|
if (!dac->canQueue()) {
|
2012-08-01 11:51:29 +02:00
|
|
|
// DMA queue full
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-29 00:12:25 +02:00
|
|
|
if (next == half) {
|
|
|
|
// Enqueue the first half
|
|
|
|
dac->queueBuffer(buffer, bufferSize / 2);
|
2012-06-18 01:53:53 +02:00
|
|
|
} else {
|
2012-09-29 00:12:25 +02:00
|
|
|
// Enqueue the second half
|
|
|
|
dac->queueBuffer(half, bufferSize / 2);
|
|
|
|
next = buffer; // wrap around
|
2012-06-18 01:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 00:12:25 +02:00
|
|
|
void AudioClass::onTransmitEnd(void *_me) {
|
|
|
|
AudioClass *me = reinterpret_cast<AudioClass *> (_me);
|
|
|
|
if (me->running == me->buffer)
|
|
|
|
me->running = me->half;
|
|
|
|
else
|
|
|
|
me->running = me->buffer;
|
2012-08-01 11:51:29 +02:00
|
|
|
}
|
|
|
|
|
2012-07-29 01:57:12 +02:00
|
|
|
AudioClass Audio(DAC);
|