Skip to content

Propagation of Audio Format Changes

Phil Schatzmann edited this page Mar 11, 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 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. 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.

Programmatic Notification

However, accordingly you need to make sure yourself that the audio destination is notified when there is no direct connection. This is e.g. the case when you do the decoding on the input side:

#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);

  // setup pcm_source
  pcm_source.begin();
}

void loop(){
  copier.copy();
}