Simultaneous Hearing through headphone or line_out while recording

Status
Not open for further replies.

Adarsha

Member
Hi all,

I am using Teensy 3.2 for audio application. In the Microphone example it is possible to hear continuously. And in the recording example we can record and playback. But I want to hear what I am recording. So I tried to combine the two codes. But it is not working.

Please provide any suggestions and inputs.

Following is the combined code for hearing and recording simultaneously.

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

// GUItool: begin automatically generated code
AudioInputI2S is23; //xy=278,1027
AudioInputI2S i2s2; //xy=296,829
AudioAnalyzePeak peak1; //xy=451,1072
AudioRecordQueue queue1; //xy=454,1027
AudioPlaySdRaw playRaw1; //xy=475,1121
AudioOutputI2S i2s1; //xy=577,836
AudioOutputI2S i2s3; //xy=643,1084
AudioConnection patchCord1(is23, 0, queue1, 0);
AudioConnection patchCord2(is23, 1, peak1, 0);
AudioConnection patchCord3(i2s2, 0, i2s1, 0);
AudioConnection patchCord4(i2s2, 0, i2s1, 1);
AudioConnection patchCord5(playRaw1, 0, i2s3, 0);
AudioConnection patchCord6(playRaw1, 0, i2s3, 1);
//AudioControlSGTL5000 18d488b2.ed3e97; //xy=438,1176
AudioControlSGTL5000 sgtl5000_1; //xy=454,953
// GUItool: end automatically generated code

// 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;

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

// The file where data is recorded
File frec;

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.
AudioMemory(8);

// Enable the audio shield, select input, and enable output
Serial.begin(9600);
sgtl5000_1.enable();
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.volume(0.5);

// Initialize the SD card
SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(10))) {
// stop here if no SD card, but print a message
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
}


void loop() {
// First, read the buttons
buttonRecord.update();
buttonStop.update();
buttonPlay.update();

// Respond to button presses
if (buttonRecord.fallingEdge()) {
Serial.println("Record Button Press");
if (mode == 2) stopPlaying();
if (mode == 0) startRecording();
}
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) {
continueRecording();
}
if (mode == 2) {
continuePlaying();
}

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


void startRecording() {
Serial.println("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.
SD.remove("RECORD.RAW");
}
frec = SD.open("RECORD.RAW", FILE_WRITE);
if (frec) {
queue1.begin();
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);
queue1.freeBuffer();
memcpy(buffer+256, queue1.readBuffer(), 256);
queue1.freeBuffer();
// 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=");
//Serial.println(usec);
}
}

void stopRecording() {
Serial.println("stopRecording");
queue1.end();
if (mode == 1) {
while (queue1.available() > 0) {
frec.write((byte*)queue1.readBuffer(), 256);
queue1.freeBuffer();
}
frec.close();
}
mode = 0;
}


void startPlaying() {
Serial.println("startPlaying");
playRaw1.play("RECORD.RAW");
mode = 2;
}

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

void stopPlaying() {
Serial.println("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 don't think having two objects for input and two for output will work. You need to add two mixers. Copy this code and import it into the Audio Design Tool.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s2;           //xy=340,256
AudioPlaySdRaw           playRaw1;       //xy=522,597
AudioAnalyzePeak         peak1;          //xy=523,537
AudioRecordQueue         queue1;         //xy=552,479
AudioMixer4              mixer1;         //xy=776,457
AudioMixer4              mixer2;         //xy=786,551
AudioOutputI2S           i2s1;           //xy=1074,487
AudioConnection          patchCord1(i2s2, 0, queue1, 0);
AudioConnection          patchCord2(i2s2, 0, mixer1, 0);
AudioConnection          patchCord3(i2s2, 0, mixer2, 0);
AudioConnection          patchCord4(i2s2, 1, peak1, 0);
AudioConnection          patchCord5(playRaw1, 0, mixer1, 1);
AudioConnection          patchCord6(playRaw1, 0, mixer2, 1);
AudioConnection          patchCord7(mixer1, 0, i2s1, 0);
AudioConnection          patchCord8(mixer2, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=431,142
// GUItool: end automatically generated code

You'll need to add code to set the gains for the mixers.

Pete
 
Status
Not open for further replies.
Back
Top