View Full Version : Recording multiple audios one after another with different name and playback

08-20-2016, 09:17 PM
Hi all,

I am working on a project which requires to record multiple audios in the memory card in different unique names. Then playing back by selecting the unique name.
In the example code it will record and when recording next time it will delete the old file.

void startRecording() {
if (SD.exists("RECORD.RAW")) {
// The SD library writes new data to the end of the
// file, so to start a new recording, the old file
// must be deleted before new data is written.
Is there any example which shows recording of multiple audio files in different name. Then playing back by selecting different names.
Also does any one know how many minutes we can store in a 8 GB memory card.

Thanks in advance

08-21-2016, 04:05 PM
Waiting for reply...:(

08-21-2016, 04:44 PM

I took a look at the example code when you initially posted your question, but didn't feel qualified enough to give an answer. Anyway, here's my "opinion":
I guess you need to instantiate multiple files:

File frec1;
File frec2;
File frec3;

and you can then record into different *.raw files like so:

void startRecording1() {
if (SD.exists("RECORD1.RAW")) {
frec1 = SD.open("RECORD1.RAW", FILE_WRITE);
if (frec1) {
mode = 1;
void startRecording2() {
if (SD.exists("RECORD2.RAW")) {
frec2 = SD.open("RECORD2.RAW", FILE_WRITE);
if (frec2) {
mode = 1;
// and so on...

Or you could use a single record(int number); function where "number" tells the function which file to write to.

This is just my guess and untested, I hope it helps...

08-22-2016, 03:23 PM
Thank you very much for the reply.


11-10-2020, 06:54 AM
I know this is extremely late but in case it helps anyone else in the future I found a more elegant way to do this vs Ben's suggestion (which was worth exploring, but proved to be too messy and unwieldy).

If you want to playback each new recording with the play button you will probably want to use some flag variables and modify what I have here. I feel like this method should work for playback with little modification that I don't care to do as I'm not going to be using that for my application. I used sprintf to increment the record number each time, there was very little modification to the existing Record.ino file from github:

// Record sound as raw data to a SD card, and play it back.
// Requires the audio shield:
// http://www.pjrc.com/store/teensy3_audio.html
// Three pushbuttons need to be connected:
// Record Button: pin 0 to GND
// Stop Button: pin 1 to GND
// Play Button: pin 2 to GND
// This example code is in the public domain.

#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S i2s2; //xy=105,63
AudioAnalyzePeak peak1; //xy=278,108
AudioRecordQueue queue1; //xy=281,63
AudioPlaySdRaw playRaw1; //xy=302,157
AudioOutputI2S i2s1; //xy=470,120
AudioConnection patchCord1(i2s2, 0, queue1, 0);
AudioConnection patchCord2(i2s2, 0, peak1, 0);
AudioConnection patchCord3(playRaw1, 0, i2s1, 0);
AudioConnection patchCord4(playRaw1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=265,212
// GUItool: end automatically generated code

// For a stereo recording version, see this forum thread:
// https://forum.pjrc.com/threads/46150?p=158388&viewfull=1#post158388

// A much more advanced sound recording and data logging project:
// https://github.com/WMXZ-EU/microSoundRecorder
// https://github.com/WMXZ-EU/microSoundRecorder/wiki/Hardware-setup
// https://forum.pjrc.com/threads/52175?p=185386&viewfull=1#post185386

// Bounce objects to easily and reliably read the buttons
Bounce buttonRecord = Bounce(0, 8);
Bounce buttonStop = Bounce(1, 8); // 8 = 8 ms debounce time
Bounce buttonPlay = Bounce(2, 8);

// which input on the audio shield will be used?
const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;

// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 11
#define SDCARD_SCK_PIN 13

// Use these with the Teensy 3.5 & 3.6 SD card
//#define SDCARD_MOSI_PIN 11 // not actually used
//#define SDCARD_SCK_PIN 13 // not actually used

// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN 4
//#define SDCARD_MOSI_PIN 11
//#define SDCARD_SCK_PIN 13

// Remember which mode we're doing
int mode = 0; // 0=stopped, 1=recording, 2=playing

// The file where data is recorded
File frec;

char recCount = 0;
char fileName[14];

void setup() {

// Configure the pushbutton pins
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);

// Audio connections require memory, and the record queue
// uses this memory to buffer incoming audio.

// Enable the audio shield, select input, and enable output

// Initialize the SD card
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here if no SD card, but print a message
while (1) {
Serial.println("Unable to access the SD card");

void loop() {

sprintf(fileName, "RECORD%d.RAW", recCount);

// First, read the buttons

// Respond to button presses
if (buttonRecord.fallingEdge()) {
Serial.println("Record Button Press");
if (mode == 2) stopPlaying();
if (mode == 0) startRecording(fileName);
if (buttonStop.fallingEdge()) {
Serial.println("Stop Button Press");
if (mode == 1) stopRecording();
if (mode == 2) stopPlaying();
if (buttonPlay.fallingEdge()) {
Serial.println("Play Button Press");
if (mode == 1) stopRecording();
if (mode == 0) startPlaying();

// If we're playing or recording, carry on...
if (mode == 1) {
if (mode == 2) {

// when using a microphone, continuously adjust gain
if (myInput == AUDIO_INPUT_MIC) adjustMicLevel();

void startRecording(char fileName[]) {
if (SD.exists(fileName)) {
// The SD library writes new data to the end of the
// file, so to start a new recording, the old file
// must be deleted before new data is written.

frec = SD.open(fileName, FILE_WRITE);
if (frec) {
mode = 1;

void continueRecording() {
if (queue1.available() >= 2) {
byte buffer[512];
// Fetch 2 blocks from the audio library and copy
// into a 512 byte buffer. The Arduino SD library
// is most efficient when full 512 byte sector size
// writes are used.
memcpy(buffer, queue1.readBuffer(), 256);
memcpy(buffer+256, queue1.readBuffer(), 256);
// write all 512 bytes to the SD card
//elapsedMicros usec = 0;
frec.write(buffer, 512);
// Uncomment these lines to see how long SD writes
// are taking. A pair of audio blocks arrives every
// 5802 microseconds, so hopefully most of the writes
// take well under 5802 us. Some will take more, as
// the SD library also must write to the FAT tables
// and the SD card controller manages media erase and
// wear leveling. The queue1 object can buffer
// approximately 301700 us of audio, to allow time
// for occasional high SD card latency, as long as
// the average write time is under 5802 us.
//Serial.print("SD write, us=");

void stopRecording() {
if (mode == 1) {
while (queue1.available() > 0) {
frec.write((byte*)queue1.readBuffer(), 256);
mode = 0;

void startPlaying() {
mode = 2;

void continuePlaying() {
if (!playRaw1.isPlaying()) {
mode = 0;

void stopPlaying() {
if (mode == 2) playRaw1.stop();
mode = 0;

void adjustMicLevel() {
// TODO: read the peak1 object and adjust sgtl5000_1.micGain()
// if anyone gets this working, please submit a github pull request :-)

I added recCount to count each time the record button is pressed, a fileName array to write the incremented recording number to, and sprintf to write the incremented filename to the array each time around the loop for when you press record next. As is, the program won't remember what recording number you're at once you power off but I'm sure there is an easy solution to this. Not necessary for my application though.

I hope this can save someone else future headaches on this subject, I know it's not much but I hope it helps.