At the very beginning of my Arduino AudioTools project, I was providing a codec for WAV files. It was just handling PCM audio, so it just needed to read or write the header and then could just copy the audio data.

In my last blog, I was writing that the ADPCM codec is perfectly suited for encoding and decoding audio on microcontrollers. So the next natural step is to provide a proper ADPCM support for WAV files and that’s what I have implemented this week.

Arduino Sketch

Here is a test sketch that encodes and decodes a sine test signal using the Microsoft ADPCM WAV format.

#include "AudioTools.h"
#include "AudioCodecs/CodecWAV.h" 
#include "AudioCodecs/CodecADPCM.h"

AudioInfo info(16000, 2, 16);
SineWaveGenerator<int16_t> sineWave( 32000);  
GeneratedSoundStream<int16_t> sound( sineWave); 
CsvStream<int16_t> out(Serial);
ADPCMDecoder adpcm_decoder(AV_CODEC_ID_ADPCM_IMA_WAV); 
ADPCMEncoder adpcm_encoder(AV_CODEC_ID_ADPCM_IMA_WAV);  
WAVDecoder wav_decoder(adpcm_decoder, AudioFormat::ADPCM);
WAVEncoder wav_encoder(adpcm_encoder, AudioFormat::ADPCM)
EncodedAudioStream decoder(&out, &wav_decoder); 
EncodedAudioStream encoder(&decoder, &wav_encoder); 
StreamCopy copier(encoder, sound);     

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

  // start Output
  auto cfgi = out.defaultConfig(TX_MODE);
  cfgi.copyFrom(info);
  out.begin(cfgi);

  // Setup sine wave
  auto cfgs = sineWave.defaultConfig();
  cfgs.copyFrom(info);
  sineWave.begin(info, N_B4);

  // start decoder
  decoder.begin(info);

  // start encoder
  encoder.begin(info);
}

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

When we create the WAVEncoder we just need to give it an encoder and the corresponding WAV format as arguments. The same applies for the WAVDecoder.

Summary

Now, we can easily provide compressed audio with the help of WAV files.

Dependencies

Do not forget to install the following libraries:


0 Comments

Leave a Reply

Avatar placeholder

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