There are some DACs out there that only support 32 bits: E.g the ES9018K2M module which is sold as high end HIFI DAC.

Therefore sometimes it is necessary to change the sample size e.g. from 16 to 32 bits when your codec produces 16 bits.

This can be easily done with the help of the NumberFormatConverterStream class of the Arduino Audio Tools library:

I2SStream i2s; // final output of decoded stream
NumberFormatConverterStream fc(i2s); // write the 16bit data to fc
// conigure 16 -> 32 bit
fc.begin(16, 32);

Arduino Sketch

Here is a complete sketch of an Internet Radio:

#include "AudioTools.h"
#include "AudioCodecs/CodecMP3Helix.h"

URLStream url("ssid","password");
I2SStream i2s; // final output of decoded stream
NumberFormatConverterStream nfc(i2s);
EncodedAudioStream dec(&nfc, new MP3DecoderHelix()); // Decoding stream
StreamCopy copier(dec, url); // copy url to decoder

void setup(){
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Info);  

  // convert 16 bits to 32, you could also change the gain
  nfc.begin(16, 32); 

  // setup i2s
  auto config = i2s.defaultConfig(TX_MODE);
  // you could define e.g your pins and change other settings
  //config.pin_ws = 10;
  //config.pin_bck = 11;
  //config.pin_data = 12;
  //config.mode = I2S_STD_FORMAT;
  //config.bits_per_sample = 32; // we coult do this explicitly
  i2s.begin(config);

  // setup I2S based on sampling rate provided by decoder
  dec.begin();

// mp3 radio
  url.begin("http://stream.srg-ssr.ch/m/rsj/mp3_128","audio/mp3");

}

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

The decoder automatically sends any audio format changes to the number format converter which forwards them to i2s. Therefore you end up automatically with the correct sample rate and number of channels w/o the need to define them in the sketch!

Please double check the potentially updated code from the examples directory.

Dependencies

You need to install the following libraries:


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *