Skip to content

Encoding and Decoding of Audio

Phil Schatzmann edited this page May 5, 2022 · 174 revisions

Compressed Audio

Unfortunately the available memory on Microcontrollers is quite restricted and we do not get very far by storing a (uncompressed) WAV file e.g. in program/flesh memory, so I started to look into compressed audio formats: The compression and uncompression can be done with the help of Codecs. Codecs are also important if you need to transmit audio data at a high sampling rate over a line with limited capacity.

On the desktop we can use the FFmpeg project which comes with a rich set of functionality. Unfortunately the situation is much more fragmented for Microcontrollers.

I started to collect the relevant libraries and in order to make things simple to use I also added a simple C++ API on top of the available libraries:

  • libhelix A MP3 and AAC Decoder from Realnetworks
  • fdk-aac A AAC Encoder and Decoder from the Frauenhofer Institute
  • libmad A open source MP3 Decoder from Underbit
  • liblame A open source MP3 Encoder uing LAME
  • etc

All these projects can be used as Arduino Libraries, but they compile and work also outside of Arduino with the help of cmake.

Decoding

I am also providing an integration into my Arduino Audio Tools where you can use these libraries with the EncodedAudioStream class:

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

using namespace audio_tools;  

MemoryStream mp3(BabyElephantWalk60_mp3, BabyElephantWalk60_mp3_len); // MP3 data source
I2SStream i2s; // final output of decoded stream
EncodedAudioStream dec(new i2s, new MP3DecoderHelix()); // Decoding stream
StreamCopy copier(dec, mp3); // copy in to out

void setup(){
  Serial.begin(115200);

  dec.setNotifyAudioChange(i2s);
  dec.begin();

  i2s.begin();
}

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

Encoding

The encoding of audio data to a different format is also done with the help of the EncodedAudioStream class. The only difference (to the decoding examples) is that we pass an Encoder as argument.

Here is the related Arduino sketch:

#include "AudioTools.h"
#include "SdFat.h"

uint16_t sample_rate = 44100;
uint8_t channels = 2;                                             // The stream will have 2 channels 
NoiseGenerator<int16_t> noise(32000);                             // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in_stream(noise);                   // Stream generated from sine wave
SdFat SD;
File audioFile;                                                   // final output stream
EncodedAudioStream out_stream(&audioFile, new WAVEncoder());             // encode as wav file
StreamCopy copier(out_stream, in_stream);                                // copies sound to out

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

  auto cfg = noise.defaultConfig();
  cfg.sample_rate = sample_rate;
  cfg.channels = channels;
  cfg.bits_per_sample = 16;
  noise.begin(cfg);
  in_stream.begin();

  // we need to provide the audio information to the encoder
  out_stream.begin(cfg);
  // open the output file
  SD.begin(SdSpiConfig(PIN_CS, DEDICATED_SPI, SD_SCK_MHZ(2)));
  audioFile = SD.open("/test/002.wav", O_WRITE | O_CREAT);
}

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

We create an input stream which is based on some sound generator. In the out_stream we indicate the final output stream (which is a file in the example) and the encoder that is used when the data is written.

Supported Codecs

Project Class Include Function Format
libhelix AACDecoderHelix AudioCodecs/CodecAACHelix.h Decoding AAC
fdk-aac AACDecoderFDK AudioCodecs/CodecAACFDK.h Encoding, Decoding AAC
libhelix MP3DecoderHelix AudioCodecs/CodecMP3Helix.h Decoding MP3
libmad MP3DecoderMAD AudioCodecs/CodecMP3MAD.h Encoding MP3
libsbc SBCDecoder AudioCodecs/CodecSBC.h Decoding SBC
libsbc SBCEncoder AudioCodecs/CodecSBC.h Encoding SBC
liblc3 LC3Decoder AudioCodecs/CodecLC3.h Decoding LC3
liblc3 LC3Encoder AudioCodecs/CodecLC3.h Encoding LC3
Clone this wiki locally