Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions libraries/Audio/example/SimpleAudioPlayer/SimpleAudioPlayer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@
#include <SD.h>
#include <Audio.h>

const char recFile[] = "record.wav";

void setup() {
// initialize serial communication at 9600 bits per second:
SerialUSB.begin(115200);
while(!SerialUSB);

/* Test begin() method */
SerialUSB.print("Detecting SD");
int counter = 0;
while (SD.begin(SD_DETECT_PIN) != TRUE)
{
delay(10);
SerialUSB.print(".");
delay(500);
if (counter == 10)
{
SerialUSB.println(".");
SerialUSB.println("SD Card not detected!");
SerialUSB.print("Detecting SD");
counter = 0;
}
counter++;
}
SerialUSB.println("...done.");
}

void loop() {
Expand All @@ -40,19 +52,21 @@ void loop() {
int duration;
delay(1000); // delay for console

File myFile = SD.open("test.wav");
File myFile = SD.open(recFile);
if (!myFile.available()) {
// if the file didn't open, print an error and stop
SerialUSB.println("error opening test.wav");
SerialUSB.print("error opening ");
SerialUSB.println(recFile);
while (true);
} else {
SerialUSB.println("test.wav open OK");
SerialUSB.println(recFile);
SerialUSB.println(" open OK");
}

myFile.read((void*) &WaveFormat, sizeof(WaveFormat));

delay(1000);
SerialUSB.println("STARTUP AUDIO\r\n");
SerialUSB.println("Starting Playback");
delay(1000);
Audio.begin(WaveFormat.SampleRate, 100);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Only 48kHz supported for now

Original by Frederic Pillon November 09, 2016
Modified by Francesco Alessi March 15 2017

This example code is in the public domain

Expand All @@ -19,7 +20,7 @@
#include <SD.h>
#include <Audio.h>

const char recFile[] = "record.wav";
const char recFile[] = "test.wav";
#define AUDIO_IN_FREQUENCY BSP_AUDIO_FREQUENCY_48K
#define DEFAULT_TIME_REC 30 // Recording time in second (default: 30s)
#define REC_SAMPLE_LENGTH (DEFAULT_TIME_REC * AUDIO_IN_FREQUENCY * DEFAULT_AUDIO_IN_CHANNEL_NBR * 2)
Expand All @@ -29,11 +30,22 @@ void setup() {
// initialize SerialUSB communication at 115200 bits per second:
SerialUSB.begin(115200);
while(!SerialUSB);

SerialUSB.print("Detecting SD");
int counter = 0;
while (SD.begin(SD_DETECT_PIN) != TRUE)
{
delay(10);
SerialUSB.print(".");
delay(500);
if (counter == 10)
{
SerialUSB.println(".");
SerialUSB.println("SD Card not detected!");
SerialUSB.print("Detecting SD");
counter = 0;
}
counter++;
}
SerialUSB.println("...done.");
}

void loop() {
Expand All @@ -43,10 +55,22 @@ void loop() {
const int S = 1024; // Number of samples to read in block
uint32_t byteswritten = 0;
uint32_t count = 0;
delay(4000); // delay for console
delay(1000);
// removing old file
while(SD.exists(recFile) == TRUE)
{
SerialUSB.print("Removing file ");
SerialUSB.print(recFile);
SerialUSB.print("...");
SD.remove(recFile);
SerialUSB.println("done");
}


File myFile = SD.open(recFile, FILE_WRITE);

if (!SD.exists(recFile)) {

// If the file didn't open, print an error and stop
SerialUSB.print("Error: failed to create ");
SerialUSB.println(recFile);
Expand All @@ -70,7 +94,7 @@ void loop() {
}

delay(1000);
SerialUSB.println("Start AUDIO record");
SerialUSB.println("Starting AUDIO recorder");
delay(1000);
status = Audio.begin(WaveFormat.SampleRate, 100, AUDIO_IN);
if (status != 0) {
Expand All @@ -84,6 +108,7 @@ void loop() {
delay(1000);

// Prepare samples
SerialUSB.println("Recording...");
int volume = 100;
Audio.prepare(NULL, S, volume);
delay(1000);
Expand Down
82 changes: 46 additions & 36 deletions libraries/Audio/src/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "Audio.h"
#include "otto_audio_inout.h"



AudioClass Audio;

/* Begin class can be extended to support more options */
Expand All @@ -33,12 +31,12 @@ int AudioClass::begin(uint32_t sampleRate, uint32_t msPreBuffer, AudioMode audio
next = bufferOut;
}

ret = BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, volume, sampleRate);
ret = BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, 0, sampleRate);

/* Audio In */
if(mode != AUDIO_OUT)
{
ret += BSP_AUDIO_IN_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, volume, sampleRate);
ret += BSP_AUDIO_IN_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, 0, sampleRate);
ret += BSP_AUDIO_IN_Record((uint16_t*)&bufferIn[0], bufferInSize);
bufferIn_fptr += 44;
}
Expand All @@ -53,7 +51,7 @@ void AudioClass::end() {
BSP_AUDIO_IN_Stop(CODEC_PDWN_SW);
}
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
if (bufferOut)
if (bufferOut)
{
free(bufferOut);
bufferOut = NULL;
Expand All @@ -64,47 +62,59 @@ void AudioClass::end() {
void AudioClass::prepare(int16_t *buffer, int S, int volume){
uint16_t *ubuffer = (uint16_t*) buffer;

if(volume >= 100)
if (volume >= 100)
volume = 100;
if (volume <= 10)
volume = 10;
if (volume <= 0)
volume = 0;

if (mode == AUDIO_OUT)
BSP_AUDIO_OUT_SetVolume(volume);
if (mode == AUDIO_IN)
BSP_AUDIO_IN_SetVolume(volume);
if (mode == AUDIO_BOTH) {
BSP_AUDIO_OUT_SetVolume(volume);
BSP_AUDIO_IN_SetVolume(volume);
}


}

size_t AudioClass::write(const uint32_t *data, size_t size) {

int i;

if(size > (bufferOutSize / 2))
return size;

/* not running yet, need to fill-in full FIFO */
if(running == NULL) {
memcpy(next, (uint8_t *) data, size);
/* First half FIFO */
if(next == bufferOut) {
next = half;
/* Second half FIfO, when copied, start playing */
} else {
next = bufferOut;
running = bufferOut;
BSP_AUDIO_OUT_Play((uint16_t*)bufferOut, bufferOutSize);
}
_receivedBytes += size;
return size;
}
if(size > (bufferOutSize / 2))
return size;

/* not running yet, need to fill-in full FIFO */
if(running == NULL) {
memcpy(next, (uint8_t *) data, size);
/* First half FIFO */
if(next == bufferOut) {
next = half;
/* Second half FIfO, when copied, start playing */
} else {
next = bufferOut;
running = bufferOut;
BSP_AUDIO_OUT_Play((uint16_t*)bufferOut, bufferOutSize);
}
_receivedBytes += size;
return size;
}

/* Wait for room in FIFO*/
while((int)next == (int) running);
/* Wait for room in FIFO*/
while((int)next == (int) running);

_receivedBytes += size;
_receivedBytes += size;

/* If running is not next there is room in fifo */
memcpy(next,(uint8_t *) data, size);
/* If running is not next there is room in fifo */
memcpy(next,(uint8_t *) data, size);

if(next == bufferOut) {
next = half;
} else {
next = bufferOut;
}
if(next == bufferOut) {
next = half;
} else {
next = bufferOut;
}

return size;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/Audio/src/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AudioClass : public Print {
uint32_t bufferIn_fptr = 0; //fptr

private:
uint32_t volume = 60;
uint32_t volume = 100;
AudioMode mode;
};

Expand Down