I added support for the VS1053 breakout boards to my Arduino Audio Tools project.
This module is interesting if you have a microcontroller that does not support I2S because all communication is going via SPI. Some boards also include an SD drive.

The major disadvantage is the number of pins that need to be connected and the fact that they might be named differently.

Dependencies

As a precondition you need to install the arduino-vs1053 driver library. There you can also find the necessary information how to connect the module.

Encoded Audio Output Example

In the audio-tools you can find a couple of examples that demonstrate how to use the module: In a nutshell you can just use it like any other audio audio sink with the following main difference: You can directly send aac, mp3, midi and wav files because the module can decode these format itself.

Just to show how easy it is, here is the code for a streaming web music player on an ESP32:

#include "AudioTools.h"
#include "AudioLibs/VS1053Stream.h"

URLStream url("ssid","password");  // or replace with ICYStream to get metadata
VS1053Stream vs1053; // final output
StreamCopy copier(vs1053, url); // copy url to decoder

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

  // setup vs1053
  auto cfg = vs1053.defaultConfig();
  cfg.is_encoded_data = true; // vs1053 is accepting encoded data
  // Use your custom pins or define in AudioCodnfig.h
  //cfg.cs_pin = VS1053_CS; 
  //cfg.dcs_pin = VS1053_DCS;
  //cfg.dreq_pin = VS1053_DREQ;
  //cfg.reset_pin = VS1053_RESET;
  vs1053.begin(cfg);

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

}

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

File Formats

The module supports the decoding of the following audio file formats:

  • Ogg Vorbis
  • AAC
  • WMA
  • MP3
  • MP1 & MP2
  • ADPCM
  • PCM
  • WAV
  • FLAC
  • Midi

Midi Files and Conversion to Format 0

General MIDI and SP-MIDI format 0 files are played. Format 1 and 2 files must be converted to format 0 by the user!

I was looking for a tool to do this, but did not find anything useful. Finally I used the following Python Code to convert the format 1 files to format 0 with the help of the ‘mido’ library:

import mido
from mido import MidiFile,MidiTrack

mid1 = MidiFile('file-1.midi')
mid0 = MidiFile()
track0 = MidiTrack(mido.merge_tracks(mid1.tracks))
mid0.tracks.append(track0)
mid0.type = 0
mid0.save('file-0.midi')

Format 1 files have multiple tracks whereas format 0 files have only 1 single track. The code simply merges the tracks into 1.

Output of PCM Audio Data

Most of the functionality of the library is using PCM data. I wanted to be able to use this functionality as well, so all I needed to do was to automatically convert the PCM stream to the WAV format. Therefore we can use the usual fuctionality w/o any changes. Here is the example that outputs a sine wave:


#include "AudioTools.h" #include "AudioLibs/VS1053Stream.h" uint16_t sample_rate=44100; uint8_t channels = 2; // The stream will have 2 channels uint8_t bits_per_sample = 16; // 2 bytes SineWaveGenerator<int16_t> sineWave(32000); // max amplitude of 32000 GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave VS1053Stream out; // VS1053 output StreamCopy copier(out, sound); // copy sound to VS1053 void setup(){ Serial.begin(115200); AudioLogger::instance().begin(Serial, AudioLogger::Warning); // Info is causing noise // Setup sine wave sineWave.begin(channels, sample_rate, N_A4); // setup output auto cfg = out.defaultConfig(); cfg.sample_rate = sample_rate; cfg.channels = channels; cfg.bits_per_sample = bits_per_sample; // Use your custom pins or define in AudioCodnfig.h //cfg.cs_pin = VS1053_CS; //cfg.dcs_pin = VS1053_DCS; //cfg.dreq_pin = VS1053_DREQ; //cfg.reset_pin = VS1053_RESET; out.begin(cfg); } void loop(){ copier.copy(); }

Because cfg.is_encoded_data = false is the default setting, we can omit this in the configuration!

Examples

Here are a couple of additional examples:


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.