Mix sound from mic with SDcard playing on teensy?

Status
Not open for further replies.

lazybaby7

Member
Hi I am wondering if it is possible to mix the audio from a microphone with the audio played from the microSD card(Currently I have a teensy 3.2, teensy 3.6 and a teensy audio adapter). It seems that the audio adapter samples the analog signal from the microphone at 44.1kHz by default so there should not be up-sampling problem if the music from the SD card is sampled at 44.1kHz as well. However, teensy definitely fetches data much faster from the SD card than it samples the analog signal from mic, so I am wondering if I need to find a way to solve the asynchronous problem or maybe the awesome teensy audio library has already taken care of it(which means I can simply wire the mic and sdplay into a mixer using the GUI and export the code). Hopefully somebody can give me any hint if this has already been successfully accomplished.
 
I am assuming when you say mix in a mic with a file playing on the SD card do you simply mean gain levels. In which case it is certainly possible. There are several example codes that individually handle these operations that you may use as a base. They may be merged with ease, just make sure the auto generated code from the audio tool GUI don't conflict with each other. Also make sure there is only one instance of the audio shield control function. The line of code I'm specifying is:

AudioControlSGTL5000 sgtl5000_1;

Just make sure each instance of talking to the audio shield you keep the same name. In the GUI you may create any circuit you feel is necessary to accomplish the goal at hand and it will only create connections for you to pass data. Each device in the GUI has a set of commands that may be used to change parameters otherwise they just pass data.

So create your circuit and merge the example codes and post questions if you need help.

Steven
 
Hello Steven,

Thank you so much for your detailed replies. I tried to connect the mic and sdpaly together with the mixer, and unfortunately still it does not work. Attached is the code I am using. If I put "sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);" I would only hear to the output from mic, while if I delete that line of code, than teensy would only play the music from the SDcard. It seems that there is not a "inputselect" function in "control_sgtl5000.cpp" file(where "micGain" function locates) so I do not know if there are other options I can use. Would you mind take a look at the code below and point out what is wrong?


// code starts
///////////////////////////////////
// copy the Design Tool code here
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S i2s1; //xy=197.1999969482422,114.19999694824219
AudioPlaySdWav playSdWav1; //xy=199.1999969482422,258.1999969482422
AudioMixer4 mixer2; //xy=429.1999969482422,256.1999969482422
AudioMixer4 mixer1; //xy=433.2000274658203,122.19999694824219
AudioOutputI2S i2s2; //xy=655.1999969482422,194.1999969482422
AudioConnection patchCord1(i2s1, 0, mixer1, 0);
AudioConnection patchCord2(i2s1, 0, mixer2, 0);
AudioConnection patchCord3(playSdWav1, 0, mixer1, 1);
AudioConnection patchCord4(playSdWav1, 1, mixer2, 1);
AudioConnection patchCord5(mixer2, 0, i2s2, 1);
AudioConnection patchCord6(mixer1, 0, i2s2, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=485.1999969482422,356.1999969482422
// GUItool: end automatically generated code


// GUItool: end automatically generated code

///////////////////////////////////




void setup() {
Serial.begin(9600);
AudioMemory(8);
sgtl5000_1.enable();
sgtl5000_1.volume(0.2);
//sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
sgtl5000_1.micGain(18);
SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(10))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
mixer1.gain(0, 0.4);
mixer1.gain(1, 0.4);
mixer2.gain(0, 0.4);
mixer2.gain(1, 0.4);
delay(1000);
}

void loop() {

if (playSdWav1.isPlaying() == false) {
Serial.println("Start playing 1");
playSdWav1.play("SDTEST1.WAV");
delay(10); // wait for library to parse WAV info
}
}
Capture.jpg
 
Last edited:
Status
Not open for further replies.
Back
Top