NetherlandsCelliesProjects/ESP32_VS1053_Stream
An audio library for Arduino esp32 with a vs1053 mp3/aac/ogg/flac decoder. Plays http, https and chunked streams. Stream metadata is parsed. Also plays ogg, mp3, flac and m4a files from sdcard.
Featured achievements
The project's latest awards
#1 Repository of the week
#1 Repository of the week
#2 Repository of the week
ESP32_VS1053_Stream
A streaming library for esp32, esp32-wrover, esp32-c3, esp32-s2 and esp32-s3 with a separate VS1053 codec chip.
This library plays mp3, ogg, aac and flac files and streams.
- Supported codecs are mp3, ogg, aac-adts, aac-adif, aac-m4a and 16 bit flac.
- Supported stream methods are http and insecure https.
- Streams can be chunked.
- Also plays mp3, ogg and flac files from sdcard or any mounted filesystem.
Very lightweight, has a binary footprint of less than 7kB excluding the psram buffer.
ESP_VS1053_Library is used to communicate with the decoder.
How to install and use
Use the latest Arduino ESP32 core version for Arduino IDE or the corresponding PIOArduino release if you use PlatformIO in VSCode.
platformio.ini example
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.37/platform-espressif32.zip
lib_deps =
https://github.com/baldram/ESP_VS1053_Library#master
https://github.com/CelliesProjects/ESP32_VS1053_Stream@3.0.10
Arduino IDE setup
The ESP_VS1053_Library library is not available in the Arduino IDE library manager.
You will have to install manually.
Follow these steps to install the vs1053 library in the Arduino IDE:
- Browse to the vs1053 repository and download the zip file found under the green button marked
<> Code. - In the Arduino IDE go to
Sketch->Include Library->Add .ZIP libraryand select the downloaded zip file to install. - Answer
YESwhen asked to overwrite the existing library.
That's it.
With the vs1053 library installed this library can be compiled in the Arduino IDE.
Example: play a stream
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <VS1053.h> // https://github.com/baldram/ESP_VS1053_Library
#include <ESP32_VS1053_Stream.h>
#define SPI_CLK_PIN 18
#define SPI_MISO_PIN 19
#define SPI_MOSI_PIN 23
#define VS1053_CS 5
#define VS1053_DCS 21
#define VS1053_DREQ 22
ESP32_VS1053_Stream stream;
const char* SSID = "xxx";
const char* PSK = "xxx";
// Called when codec is detected
void codecCallBack(const char *codec)
{
Serial.printf("codec: %s\n", codec);
}
// Called when bitrate is detected (cbr) and changes (vbr)
void bitrateCallback(uint32_t bitrate)
{
Serial.printf("bitrate: %lu kbps\n", bitrate);
}
// Called when a stream has an ICY name header set
void stationCallback(const char *name)
{
Serial.printf("station: %s\n", name);
}
// Called when stream metadata is available
void infoCallback(const char *info)
{
Serial.printf("info: %s\n", info);
}
// Called on stream errors
void errorCallback(const char *error)
{
Serial.printf("error: %s\n", error);
}
// Called on end-of-file
void eofCallback(const char *url)
{
Serial.printf("eof: %s\n", url);
}
void setup() {
Serial.begin(115200);
Serial.println("\n\nVS1053 Radio Streaming Example\n");
// Connect to Wi-Fi
Serial.printf("Connecting to WiFi network: %s\n", SSID);
WiFi.begin(SSID, PSK);
WiFi.setSleep(false); // Important to disable sleep to ensure stable connection
while (!WiFi.isConnected())
delay(10);
Serial.println("WiFi connected");
// Start SPI bus
SPI.begin(SPI_CLK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN);
// Initialize the VS1053 decoder
if (!stream.startDecoder(VS1053_CS, VS1053_DCS, VS1053_DREQ) || !stream.isChipConnected())
Serial.println("Decoder not running");
// Set the codec callback
stream.setCodecCB(codecCallBack);
// Set the bitrate callback
stream.setBitrateCB(bitrateCallback);
// Set the station name callback
stream.setStationCB(stationCallback);
// Set the stream metadata callback
stream.setInfoCB(infoCallback);
// Set the error callback
stream.setErrorCB(errorCallback);
// Set the EOF callback
stream.setEofCB(eofCallback);
Serial.println("Starting radio stream");
// Connect to the radio stream
stream.connectToHost("http://icecast.omroep.nl/radio6-bb-mp3");
if (!stream.isRunning())
Serial.println("Stream not running");
}
void loop() {
stream.loop();
delay(5);
}
Example: play from SD card
#include <Arduino.h>
#include <SD.h>
#include <VS1053.h> // https://github.com/baldram/ESP_VS1053_Library
#include <ESP32_VS1053_Stream.h>
#define SPI_CLK_PIN 18
#define SPI_MISO_PIN 19
#define SPI_MOSI_PIN 23
#define VS1053_CS 5
#define VS1053_DCS 21
#define VS1053_DREQ 22
#define SDREADER_CS 26
ESP32_VS1053_Stream stream;
// Called when codec is detected
void codecCallBack(const char *codec)
{
Serial.printf("codec: %s\n", codec);
}
// Called when bitrate is detected (cbr) and changes (vbr)
void bitrateCallback(uint32_t bitrate)
{
Serial.printf("bitrate: %lu kbps\n", bitrate);
}
// Called on errors
void errorCallback(const char *error)
{
Serial.printf("error: %s\n", error);
}
// Called on end-of-file
void eofCallback(const char *url)
{
Serial.printf("eof: %s\n", url);
}
bool mountSDcard() {
if (!SD.begin(SDREADER_CS)) {
Serial.println("Card mount failed");
return false;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return false;
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
return true;
}
void setup() {
Serial.begin(115200);
Serial.println("\n\nVS1053 SD Card Playback Example\n");
// Start SPI bus
SPI.begin(SPI_CLK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN);
// Mount SD card
if (!mountSDcard())
Serial.println("SD card not mounted");
Serial.println("Starting decoder...");
// Initialize the VS1053 decoder
if (!stream.startDecoder(VS1053_CS, VS1053_DCS, VS1053_DREQ) || !stream.isChipConnected())
Serial.println("Decoder not running");
// Set the codec callback
stream.setCodecCB(codecCallBack);
// Set the bitrate callback
stream.setBitrateCB(bitrateCallback);
// Set the error callback
stream.setErrorCB(errorCallback);
// Set the EOF callback
stream.setEofCB(eofCallback);
Serial.println("VS1053 running - starting SD playback");
// Start playback from an SD file
stream.connectToFile(SD, "/track1.mp3");
if (!stream.isRunning())
Serial.println("No file running");
}
void loop() {
stream.loop();
delay(5);
}
Known issues
Ogg and flac files can not be resumed from an offset without first playing a couple of seconds from the start of the file.
Tips for troublefree streaming
WiFi setup
- Switch off the BlueTooth radio if unused.
- Switch off power save mode.
...
btStop();
WiFi.begin(SSID, PSK);
WiFi.setSleep(false);
...
Prevent reboots when used on early esp32
Early versions of the esp32 have issues with the external psram cache, resulting in reboots.
Workarounds are possible depending on the hardware revision.
Revision V0.0
No workarounds are possible for this revision other than not using the psram.
Revision V1.0
On revision V1.0 psram can be used with the following build flags:
-D BOARD_HAS_PSRAM
-mfix-esp32-psram-cache-issue
-mfix-esp32-psram-cache-strategy=memw
Revision V3.0
On revision V3.0 psram can be used with the following build flag:
-D BOARD_HAS_PSRAM
Source: esp-idf api guide on external ram.
Find your hardware revision
In PIO you can find out what hardware revision you have by running esptool.py flash_id in a terminal.
In Arduino IDE go to File->Preferences and find the Show verbose output during option. Check the box marked upload.
You can now see the hardware revision when you upload a sketch.
Functions
Initialize the VS1053 codec
bool startDecoder(CS, DCS, DREQ);
Check if VS1053 is responding
bool isChipConnected();
Start or resume a stream
bool connectToHost(url);
bool connectToHost(url, offset);
bool connectToHost(url, user, pwd);
bool connectToHost(url, user, pwd, offset);
Note: When a stream does not start in this library but it does play on your desktop or laptop you can try increasing the connection timeout.
You can do this in ESP32_VS1053_Stream.h by increasing these values:
#define VS1053_CONNECT_TIMEOUT_MS 250
#define VS1053_CONNECT_TIMEOUT_MS_SSL 750
Start or resume a local file
bool connectToFile(filesystem, filename);
bool connectToFile(filesystem, filename, offset);
filesystem has to be mounted.
Stop a running stream
void stopSong();
Feed the decoder
void loop();
This function has to called every couple of ms to feed the decoder with data.
For bitrates up to 320kbps somewhere between 2-5 ms is about right.
Check if stream is running
bool isRunning();
Get the current volume
uint8_t getVolume();
Set the volume
void setVolume(newVolume);
newVolume should be in the range 0-100.
Set bass and treble
uint8_t rtone[4] = {toneha, tonehf, tonela, tonelf};
void setTone(rtone);
Values for rtone:
toneha = <0..15> // Setting treble gain (0 off, 1.5dB steps)
tonehf = <0..15> // Setting treble frequency lower limit x 1000 Hz
tonela = <0..15> // Setting bass gain (0 = off, 1dB steps)
tonelf = <0..15> // Setting bass frequency lower limit x 10 Hz
Get the current stream url
const char* lastUrl();
The current stream url might differ from the request url if the request url points to a playlist.
Get the filesize
size_t size();
Returns 0 if the stream is a radio stream.
Get the current position in the file
size_t position();
Returns 0 if the stream is a radio stream.
Get the buffer fill status
void bufferStatus(size_t &used, size_t &capacity);
Note: A buffer will only be allocated if there is enough free psram.
Event callback setup
With event callbacks you can run user defined routines on stream events.
Check out the examples to see how to setup event callbacks.
Station name callback
void setStationCB(callback);
Set a callback on the stream name.
Note that not all streams provide a name.
void clearStationCB();
Clear the stream name callback.
Stream metadata callback
void setInfoCB(callback);
Set a callback on stream metadata.
Note that not all streams provide metadata.
void clearInfoCB();
Clear the metadata callback.
End of file callback
void setEofCB(callback);
Set a callback on the end-of-file.
Also called if a stream or file times out/errors.
You can use the eof callback to code a playlist.
Use connectToHost() or connectToFile() inside the eof callback to start the next item.
void clearEofCB();
Clear the end-of-file callback.
Bitrate callback
void setBitrateCB(callback);
Set a callback on bitrate detection.
This callback will be called multiple times on a vbr stream.
void clearBitrateCB();
Clear the bitrate callback.
Error callback
void setErrorCB(callback);
Set a callback on stream or file errors.
This callback will give a short description of the occured error.
void clearErrorCB();
Clear the error callback.
License
MIT License
Copyright (c) 2021 Cellie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Similar Projects
Discover other similar projects and AI models.
html5 js 录音 mp3 wav ogg webm amr g711a g711u 格式,支持pc和Android、iOS部分Web浏览器、Hybrid App(提供Android iOS App源码)、微信,提供ASR语音识别转文字 H5版语音通话聊天示例 DTMF编码解码
Pure Rust multimedia format demuxing, tag reading, and audio decoding library
📟 JSON library for Arduino and embedded C++. Simple and efficient.
Infrared remote library for ESP8266/ESP32: send and receive infrared signals with multiple protocols. Based on: https://github.com/shirriff/Arduino-IRremote/
Bluetooth LE Keyboard library for the ESP32 (Arduino IDE compatible)
A Simple ESP32 Bluetooth A2DP Library (to implement a Music Receiver or Sender) that supports Arduino, PlatformIO and Espressif IDF