Unable to get OutputMixer to work properly #2186
-
Hi, I'm currently working on migrating our product (a connected audiobook reader) to this library because we want to use the mixing feature you offer and some other stuff in the future. First, I'd like to create an example that plays an MP3 file alone and another one with an MP3 file and a WAV file simultaneously, but I'm running into some issues. ...
I2SStream i2s;
OutputMixer<int16_t> mixer(i2s, 2);
// MP3 File
File mp3File;
EncodedAudioStream mp3DecoderStream(&mixer, new MP3DecoderHelix());
StreamCopy copier1;
void setup() {
Serial.begin(MONITOR_SPEED);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// I2S config
auto i2sConfig = i2s.defaultConfig(TX_MODE);
i2sConfig.pin_bck = I2S_SCLK;
i2sConfig.pin_data = I2S_DSDIN;
i2sConfig.pin_mck = I2S_MCLK;
i2sConfig.pin_ws = I2S_LRCK;
i2sConfig.i2s_format = I2S_PHILIPS_FORMAT;
i2s.begin(i2sConfig);
mp3File = SD_MMC.open("/audiobooks/6aa7aec4-5dc7-4f3d-8524-27280d4d7953/001_9791036645419.mp3");
mp3DecoderStream.begin();
copier1.begin(mp3DecoderStream, mp3File);
mixer.begin(4096);
}
void loop() {
copier1.copy();
mixer.flushMixer();
} By running this code the generated sound is filled with noise and crackle and is also sped up to about x2. Now when I add the WAV file, the sound is still pretty bad and I can't ear the WAV. Furthermore, the serial console is spamming me with:
This is the code I use to try to mix both MP3 and WAV: ...
I2SStream i2s;
OutputMixer<int16_t> mixer(i2s, 2);
// MP3 File
File mp3File;
EncodedAudioStream mp3DecoderStream(&mixer, new MP3DecoderHelix());
StreamCopy copier1;
// WAV File
File wavFile;
StreamCopy copier2;
void setup() {
Serial.begin(MONITOR_SPEED);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// I2S config
auto i2sConfig = i2s.defaultConfig(TX_MODE);
i2sConfig.pin_bck = I2S_SCLK;
i2sConfig.pin_data = I2S_DSDIN;
i2sConfig.pin_mck = I2S_MCLK;
i2sConfig.pin_ws = I2S_LRCK;
i2sConfig.i2s_format = I2S_PHILIPS_FORMAT;
i2s.begin(i2sConfig);
mp3File = SD_MMC.open("/audiobooks/6aa7aec4-5dc7-4f3d-8524-27280d4d7953/001_9791036645419.mp3");
mp3DecoderStream.begin();
copier1.begin(mp3DecoderStream, mp3File);
wavFile = SD_MMC.open("/voice_feedback/global/startup_sound.wav");
wavFile.seek(44);
copier2.begin(mixer, wavFile);
mixer.begin(4096);
}
void loop() {
copier1.copy();
copier2.copy();
} I might be missing something obvious, so if you spot it or have suggestions for a better approach, I’d love to hear your thoughts. Antoine |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Mixing expects that you write an equal amout of data from all sources to the Mixer! Mixing mp3 is tricky because the decoder provides huge arrays of PCM data: You might get away by making the buffers really big, but in any case you will need to query the system on how many WAV data you need to write, to keep things balanced! A more effient way would be to split up the decoding result into smaller pieces (With a BufferedStream) and do the mixing of these smaller pieces in a CallbackStream. See also this similar discussion.... |
Beta Was this translation helpful? Give feedback.
Mixing expects that you write an equal amout of data from all sources to the Mixer!
Mixing mp3 is tricky because the decoder provides huge arrays of PCM data: You might get away by making the buffers really big, but in any case you will need to query the system on how many WAV data you need to write, to keep things balanced!
A more effient way would be to split up the decoding result into smaller pieces (With a BufferedStream) and do the mixing of these smaller pieces in a CallbackStream.
See also this similar discussion....