Hello,
I don't know what happening but I either stumble on a bug, or more likely, I'm missing something obvious. As part of a larger project, I'm trying to play a wav file from the SD card (right now a 1000Hz Tone) through the headphone/lineout outputs while, at the same time, reading the line in. The line in (via the audio queue) is being sent to the Serial Monitor. It doesn't have to be perfectly continuous, I just need about 50 ms worth or 18 audio queue packets. I had this working last week so I don't know what happened as I can't think of whats changed. I do think I updated the Arduino IDE to the newest version (V2.2.1 updated 4 days ago). Has anyone else experienced this issue and how did you solve it.
Thus far I've tried tested this code using 2 different computers, and two different Teensy/audio board setups (Teensy 4.1 & Audio RevD Board and a Teensy 3.5 and Audio RevC board). Same results on every combination. I've also disconnected everything to make sure I didn't cross any wires.
I'm hoping its something stupid I'm missing but I haven't been able to figure it out.
I've attached a screenshot of the Audio System Design Tool, a screenshot of the output of the Serial Plotter and the .wav file I'm using
Thank you,
Steve
Here is code
I don't know what happening but I either stumble on a bug, or more likely, I'm missing something obvious. As part of a larger project, I'm trying to play a wav file from the SD card (right now a 1000Hz Tone) through the headphone/lineout outputs while, at the same time, reading the line in. The line in (via the audio queue) is being sent to the Serial Monitor. It doesn't have to be perfectly continuous, I just need about 50 ms worth or 18 audio queue packets. I had this working last week so I don't know what happened as I can't think of whats changed. I do think I updated the Arduino IDE to the newest version (V2.2.1 updated 4 days ago). Has anyone else experienced this issue and how did you solve it.
Thus far I've tried tested this code using 2 different computers, and two different Teensy/audio board setups (Teensy 4.1 & Audio RevD Board and a Teensy 3.5 and Audio RevC board). Same results on every combination. I've also disconnected everything to make sure I didn't cross any wires.
I'm hoping its something stupid I'm missing but I haven't been able to figure it out.
I've attached a screenshot of the Audio System Design Tool, a screenshot of the output of the Serial Plotter and the .wav file I'm using
Thank you,
Steve
Here is code
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S i2s1; //xy=378,193
AudioPlaySdWav playSDWav; //xy=485,429
AudioRecordQueue queue1; //xy=621,179
AudioAmplifier ampR; //xy=682,502
AudioAmplifier ampL; //xy=699,387
AudioOutputI2S i2s2; //xy=883,435
AudioConnection patchCord1(i2s1, 0, queue1, 0);
AudioConnection patchCord2(playSDWav, 0, ampL, 0);
AudioConnection patchCord3(playSDWav, 1, ampR, 0);
AudioConnection patchCord5(ampL, 0, i2s2, 0);
AudioConnection patchCord4(ampR, 0, i2s2, 1);
AudioControlSGTL5000 audioboard; //xy=724,623
// GUItool: end automatically generated code
//define pins for audio shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// which input on the audio shield will be used?
const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;
const long interval = 100; // how read out queue data
unsigned long previousMillis = 0;
const int numPackets = 18; // number of packets to read from queue
const int bufferleng = 128*numPackets; // each packet is 128 samples long
int16_t data[bufferleng];
int fs = 44100; // not used - sample rate used by audioboard
File rootdir;
bool go = 1;
bool done = 0;
void setup() {
Serial.begin(230400);
delay(300); //wait for init of serial
while (Serial.available() == 0) {
// do nothing, wait for Serial Input to start
}
Serial.println("CONNECTED");
// Setup SD card
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
//make sure accessing SD
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
// read out and print all the file names on SD
rootdir = SD.open("/");
printDirectory(rootdir);
// Audio connections require memory, and the record queue uses this memory to buffer incoming audio.
AudioMemory(25);
// Enable the audio shield, select input, and enable output
audioboard.enable();
audioboard.inputSelect(myInput);
audioboard.volume(0.8);
ampL.gain(1);
ampR.gain(1);
Serial.println("START");
delay(1);
}
void loop() {
playSDWav.play("1000Hz_CALIB.WAV");
delay(10);
while (playSDWav.isPlaying()){
// for pause without delay() function at bottom
unsigned long currentMillis = millis();
if (go){
go = 0;
// begin queue for recording data
queue1.begin();
// wait till queue has enough packets then stop queue and stop playing
while(!done){
if (queue1.available() >= numPackets) {
queue1.end();
done = 1;
}
}
// *********** wait till after playing to do more stuff *************
// organize data from queue into buffer array
for (int i = 1; i <= numPackets; i++){
uint16_t (*buffer) = (queue1.readBuffer());
queue1.freeBuffer();
for (int j = 0; j < 128; j++){
data[128*(i-1) + j] = buffer[j];
}
}
// clear queue
queue1.clear();
// Serial print data to Arduino Plotter
for (int i = 0; i < bufferleng; i++){
Serial.print("LowBound:");
Serial.print(-1000);
Serial.print(",");
Serial.print("UpBound:");
Serial.print(1000);
Serial.print(",");
Serial.print("Data:");
Serial.println(data[i]);
}
}
if (currentMillis - previousMillis >= interval){
// save the last time you blinked the LED
previousMillis = currentMillis;
done = 0;
go = 1;
}
}
}
// ******* function to print all the file names on the SD card and return the number of files*******
int printDirectory(File rootdir) {
int count = 0;
Serial.println("FILENAMES...");
while (true) {
File entry = rootdir.openNextFile();
if (!entry) { // no more files
break;
}
if (!entry.isDirectory()) {
Serial.println(entry.name());
count++;
}
entry.close();
}
rootdir.rewindDirectory();
return count;
}