Hi there,
I'm running into a weird problem with a project I'm working on where I'm wanting to record and playback audio using a Teensy 3.6. I've copied the code I'm working with below, but the important parts should be the #define SAMPLE_NAME line and the record() and playSample() functions at the bottom.
Anyways, the gist of the code is that I want the Teensy (1) to record from the AudioInputAnalog object when a button is pressed and the audio level passes a certain threshold and (2) to play back the recorded audio from the AudioOutputAnalogStereo object when a trigger signal appears.
The problem I'm running into is that the sketch works as is, but if I define SAMPLE_NAME to be something else the Teensy crashes when I try to play back the recorded audio. So far, I've found that the sketch works when SAMPLE_NAME is defined as 'Sample.raw' and 'TstAudIO.raw', but not when it is defined as 'Beta.raw', 'BetaOne.raw', 'BetaOnee.raw', 'Test.raw', or 'Z.raw'.
I'm guessing there are some sorts of naming conventions that .raw files need to follow in order for them to be played with an AudioPlaySdRaw object, and that 'Sample.raw' and 'TstAudIO.raw' follow them while the others don't. However, the only convention I'm aware of is that the name needs to be 8 or fewer characters and the extension needs to be 3 or fewer. Are there other conventions that I'm not aware of?
Or, is there something substantive about how SAMPLE_NAME is working in this sketch that I'm missing? The only places 'SAMPLE_NAME' shows up are in the #define line at the top and the record() and playSample() functions at the bottom, and I'm not seeing any issues in those places that are jumping out to me.
Anyways, any thoughts you could spare would be a huge help. Thanks!
I'm running into a weird problem with a project I'm working on where I'm wanting to record and playback audio using a Teensy 3.6. I've copied the code I'm working with below, but the important parts should be the #define SAMPLE_NAME line and the record() and playSample() functions at the bottom.
Anyways, the gist of the code is that I want the Teensy (1) to record from the AudioInputAnalog object when a button is pressed and the audio level passes a certain threshold and (2) to play back the recorded audio from the AudioOutputAnalogStereo object when a trigger signal appears.
The problem I'm running into is that the sketch works as is, but if I define SAMPLE_NAME to be something else the Teensy crashes when I try to play back the recorded audio. So far, I've found that the sketch works when SAMPLE_NAME is defined as 'Sample.raw' and 'TstAudIO.raw', but not when it is defined as 'Beta.raw', 'BetaOne.raw', 'BetaOnee.raw', 'Test.raw', or 'Z.raw'.
I'm guessing there are some sorts of naming conventions that .raw files need to follow in order for them to be played with an AudioPlaySdRaw object, and that 'Sample.raw' and 'TstAudIO.raw' follow them while the others don't. However, the only convention I'm aware of is that the name needs to be 8 or fewer characters and the extension needs to be 3 or fewer. Are there other conventions that I'm not aware of?
Or, is there something substantive about how SAMPLE_NAME is working in this sketch that I'm missing? The only places 'SAMPLE_NAME' shows up are in the #define line at the top and the record() and playSample() functions at the bottom, and I'm not seeing any issues in those places that are jumping out to me.
Anyways, any thoughts you could spare would be a huge help. Thanks!
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Bounce.h>
#include <SerialFlash.h>
#define REC_PIN_0 7
#define INPUT_PIN A1
#define TRIG_PIN_0 0
#define SAMPLE_NAME "Sample.raw"
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define REC_THRESHOLD .05
AudioInputAnalog input(INPUT_PIN);
AudioAnalyzePeak level;
AudioRecordQueue recQueue;
AudioPlaySdRaw samplePlayer;
AudioOutputAnalogStereo outputs;
AudioConnection inToRec(input, 0, recQueue, 0);
AudioConnection inToLevel(input, 0, level, 0);
AudioConnection sampToOuts(samplePlayer, 0, outputs, 0);
Bounce recButton = Bounce(REC_PIN_0, 10);
Bounce trigIn = Bounce(TRIG_PIN_0, 10);
void setup() {
//Audio Setup
AudioMemory(80);
//Serial Setup
Serial.begin(9600);
while (!Serial) {}
//Trig/Rec Setup
pinMode(REC_PIN_0, INPUT_PULLUP);
pinMode(TRIG_PIN_0, INPUT_PULLDOWN);
//SD Setup
while (!(SD.begin(SDCARD_CS_PIN))) {
Serial.println("Unable to access the SD card");
delay(500);
}
Serial.println("setup() complete");
}
void loop() {
updateRecButtons();
updateTrigIns();
}
void updateRecButtons() {
recButton.update();
if (recButton.fallingEdge()) {
Serial.println("Record Button Pressed");
record();
}
}
void updateTrigIns() {
trigIn.update();
if (trigIn.risingEdge()) {
Serial.println("Sample Triggered");
playSample();
}
}
void record() {
Serial.println("Entering record()");
//Setup file to record into
if(SD.exists(SAMPLE_NAME)) {
SD.remove(SAMPLE_NAME);
Serial.print("Deleting ");
Serial.println(SAMPLE_NAME);
}
File file = SD.open(SAMPLE_NAME, FILE_WRITE);
if(!file) {
Serial.println("Error: File Not Found");
while (digitalRead(REC_PIN_0) == LOW) {}
return;
}
//Wait until Signal is Present
if (level.available()) level.read(); //flushes any old values in level
while(1) {
recButton.update();
if (recButton.risingEdge()) {
Serial.println("Button Released before Recording Started");
return;
}
if (level.available() && (level.read() > REC_THRESHOLD)) break;
}
//Start Recording
Serial.println("Starting to Record");
recQueue.begin();
while (!recButton.risingEdge()) {
recButton.update();
if (recQueue.available() >= 2) {
byte buffer[512];
memcpy(buffer, recQueue.readBuffer(), 256);
recQueue.freeBuffer();
memcpy(buffer+256, recQueue.readBuffer(), 256);
recQueue.freeBuffer();
file.write(buffer, 512);
}
}
//Finish Recording
Serial.println("Record Button Released");
recQueue.end();
while (recQueue.available() >= 2) {
file.write((byte*)recQueue.readBuffer(), 256);
recQueue.freeBuffer();
}
file.close();
Serial.println("Exiting record()");
}
void playSample() {
if (!SD.exists(SAMPLE_NAME)) {
Serial.print("File ");
Serial.print(SAMPLE_NAME);
Serial.println(" not found");
} else {
Serial.println("Playing sample");
samplePlayer.stop();
samplePlayer.play(SAMPLE_NAME);
}
}