Simple Audio Recorder

Description

This example shows how to use the Audio library of the Arduino STAR OTTO, to create a sound file of 30 seconds named "record.wav" in the root directory of a Micro SD card.

Note:
Only 48kHz supported for now.

Hardware

Circuit

Mount the SD card in the dedicated slot and connect the board to PC.
STAR OTTO

Code

Now upload the code below, using the Arduino IDE.

Warning:
Use the Arduino IDE 1.8.x and download the corresponding platform (Arduino STM32F4 Boards) from the Board Manager
The Audio library is already included in the IDE. Select the Arduino STAR OTTO as board to see the Audio Examples in the menu:

Simple Recorder sketch

/*
Simple Audio Recorder

Demonstrates the use of the Audio library for the Arduino OTTO

Hardware required :
* SD card in the Arduino OTTO slot
* Only 48kHz supported for now

Original by Frederic Pillon November 09, 2016

This example code is in the public domain

* A sound file named "record.wav" in the root directory of the SD card
* will be created. Record duration is 30 seconds.

*/

#include <SD.h>
#include <Audio.h>

const char recFile[] = "record.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)
#define RECORD_5PERCENT ((5*REC_SAMPLE_LENGTH)/100)

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

while (SD.begin(SD_DETECT_PIN) != TRUE)
{
delay(10);
}
}

void loop() {
uint8_t status = 0;
WAVE_FormatTypeDef WaveFormat;
uint8_t pHeaderBuff[44];
const int S = 1024; // Number of samples to read in block
uint32_t byteswritten = 0;
uint32_t count = 0;
delay(4000); // delay for console

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);
while (true);
} else {
SerialUSB.print(recFile);
SerialUSB.println(" created succesfully.");
}

// Initialize header file
status = WavProcess_EncInit(&WaveFormat, AUDIO_IN_FREQUENCY, pHeaderBuff);
if( status != 0 ) {
SerialUSB.print("Error: could not encode wav header: ");
SerialUSB.println(status, DEC);
while (true);
}
if (myFile.write(pHeaderBuff, sizeof(pHeaderBuff)) != sizeof(pHeaderBuff)){
SerialUSB.print("Error: could not write header to ");
SerialUSB.println(recFile);
while (true);
}

delay(1000);
SerialUSB.println("Start AUDIO record");
delay(1000);
status = Audio.begin(WaveFormat.SampleRate, 100, AUDIO_IN);
if (status != 0) {
SerialUSB.print("Error: Audio could not begin: ");
SerialUSB.println(status, DEC);
while (true);
} else {
SerialUSB.println("Audio begin: OK");
}

delay(1000);

// Prepare samples
int volume = 100;
Audio.prepare(NULL, S, volume);
delay(1000);

// MAX Recording time reached, so stop audio interface and close file
while ( Audio.getSampleIn() < REC_SAMPLE_LENGTH) {
if ( Audio.isBufferInFull()) {
byteswritten = myFile.write((uint8_t*)Audio.getBufferInWithOffset(), Audio.getBufferInSize());
Audio.setBufferInEmpty(byteswritten);
}

// Display percent of recording every 5%
if ( ((Audio.getSampleIn() / RECORD_5PERCENT) - count) == 1)
{
count++;
SerialUSB.print(5*count, DEC);
SerialUSB.println("%");
}

}

// Update wav header file
if (myFile.seek(0) == TRUE)
{
// Update the wav file header and save it into wav file
WavProcess_HeaderUpdate(pHeaderBuff, Audio.getSampleIn());
if (myFile.write(pHeaderBuff, sizeof(pHeaderBuff)) != sizeof(pHeaderBuff)){
SerialUSB.print("Error: header not updated to ");
SerialUSB.println(recFile);
while (true);
}
}
else
{
SerialUSB.println("Error: could not update wav header.");
while (true);
}

SerialUSB.println("End of recording. Thank you!");
Audio.end();
myFile.close();
while(1);
}

Output

Search in the root the record.wav file to listen the recording.