-
-
Notifications
You must be signed in to change notification settings - Fork 237
Propagation of Audio Format Changes
Phil Schatzmann edited this page Apr 23, 2024
·
11 revisions
It is important that the audio source and audio destination use the same audio format (sample rate, channels and bits per sample). Usually you can do this yourself when setting up your sketch.
However, there are situations where the format is not known or can dynamically change. E.g when using an AudioDecoder, the decoder determines the audio format when processing the file. The following automatic logic is in place:
- The decoder notifies the EncodedAudioStream object.
- The EncodedAudioStream object notifies the registered destination object, if this is subclass of AudioStream (e.g. I2SStream) or AudioOutput. The registration is done automatically e.g. when you provide the destination in the constructor.
This covers most of the cases including all components on a pipeline.
However, accordingly you need to make sure yourself that the audio destination is notified when there is no connection. This is e.g. the case when you do the decoding on the input side. There are 2 ways to implement this:
- using addNotifyAudioChange() to notify the output
- using setSynchAudioInfo(true) on the copier
#include "AudioTools.h"
#include "AudioLibs/AudioBoardStream.h"
#include "AudioCodecs/CodecMP3Helix.h"
#include "BabyElephantWalk60_mp3.h"
MemoryStream mp3(BabyElephantWalk60_mp3, BabyElephantWalk60_mp3_len);
I2SOutputStream out;
EncodedAudioStream pcm_source(&mp3, new MP3DecoderHelix()); // output to decoder
StreamCopy copier(out, pcm_source);
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
// setup I2s
out.begin(out.defaultConfig());
// make sure that i2s is updated
pcm_source.addNotifyAudioChange(out);
// copier.setSynchAudioInfo(true); // alternative way
// setup pcm_source
pcm_source.begin();
}
void loop(){
copier.copy();
}