Skip to content

Propagation of Audio Format Changes

Phil Schatzmann edited this page Apr 23, 2024 · 10 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.

Automatic Notification

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. In the case of chained audio objects, the successor is automatically calling setAudioInfo on it's successor objects, if this is subclass of AudioStream (e.g. I2SStream) or AudioOutput.

In the case of the decoding scenario following automatic logic is in place:

  • The decoder notifies the EncodedAudioStream object.
  • The EncodedAudioStream object notifies the registered destination object.

This covers most of the cases including all components on a pipeline.

Programmatic Notification

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();
}
Clone this wiki locally