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:


5 Comments

AMEUR · 6. March 2024 at 9:05

Hi
i am using this sketch as (radio internet) it is usefull and very nice when playing stream without any trouble in buffering
but with some radio links having (mp3) extention , i got this message in Serial :
[W] HttpLineReader.h : 27 – HttpLineReader readlnInternal->no Data
[E] URLStream.h : 249 – Error code recieved … stop waiting for reply

when i check the link in browser it works!
so this is a link that works fine without any prob:
http://46.43.69.146:8008/
an this is a link that dos not work with this sketch but works with others
https://webradio.tda.dz/Internationale_64K.mp3

i would like to know how can i resolve this ?

ameur · 17. January 2024 at 8:38

hi
thank you for this usefull sketch, i would like to know more about (Volume) in this example, how to increase/decrease volume ?? it is ok in the player, when reading files, but what about stream??
thanks

Leave a Reply

Avatar placeholder

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