Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A streaming library for Arduino esp32 with a vs1053 mp3/aac/ogg/flac decoder. Plays http, https (insecure mode) and chunked streams and parses the metadata. Also plays ogg and mp3 from sdcard.

License

NotificationsYou must be signed in to change notification settings

CelliesProjects/ESP32_VS1053_Stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codacy Badge

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, aac+ andflac files and streams and usesESP_VS1053_Library to communicate with the decoder.

Supported stream methods are http and insecure https. Streams can be chunked.
Also plays mp3 and ogg files from sdcard or any mounted filesystem.

How to install and use

InstallESP_VS1053_Library and this library in your Arduino library folder.

Take care to install the master branch of the VS1053 library or at least a version from commitba1803f or later because thegetChipVersion() call that is needed is not included in the latest release.
See#23

Use thelatest Arduino ESP32 core version.

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>#defineSPI_CLK_PIN18#defineSPI_MISO_PIN19#defineSPI_MOSI_PIN23#defineVS1053_CS5#defineVS1053_DCS21#defineVS1053_DREQ22ESP32_VS1053_Stream stream;constchar* SSID ="xxx";constchar* PSK ="xxx";voidsetup() {    Serial.begin(115200);while (!Serial)delay(10);    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 connectionwhile (!WiFi.isConnected())delay(10);    Serial.println("WiFi connected - starting decoder...");// Start SPI bus    SPI.setHwCs(true);    SPI.begin(SPI_CLK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN);// Initialize the VS1053 decoderif (!stream.startDecoder(VS1053_CS, VS1053_DCS, VS1053_DREQ) || !stream.isChipConnected()) {        Serial.println("Decoder not running - system halted");while (1)delay(100);    }    Serial.println("VS1053 running - 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 - system halted");while (1)delay(100);    }    Serial.print("Codec:");    Serial.println(stream.currentCodec());    Serial.print("Bitrate:");    Serial.print(stream.bitrate());    Serial.println(" kbps");}voidloop() {    stream.loop();delay(5);}voidaudio_showstation(constchar* info) {    Serial.printf("Station: %s\n", info);}voidaudio_showstreamtitle(constchar* info) {    Serial.printf("Stream title: %s\n", info);}voidaudio_eof_stream(constchar* info) {    Serial.printf("End of stream: %s\n", info);}

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>#defineSPI_CLK_PIN18#defineSPI_MISO_PIN19#defineSPI_MOSI_PIN23#defineVS1053_CS5#defineVS1053_DCS21#defineVS1053_DREQ22#defineSDREADER_CS26ESP32_VS1053_Stream stream;boolmountSDcard() {if (!SD.begin(SDREADER_CS)) {        Serial.println("Card mount failed");returnfalse;    }uint8_t cardType = SD.cardType();if (cardType == CARD_NONE) {        Serial.println("No SD card attached");returnfalse;    }uint64_t cardSize = SD.cardSize() / (1024 *1024);    Serial.printf("SD Card Size: %lluMB\n", cardSize);returntrue;}voidsetup() {    Serial.begin(115200);while (!Serial)delay(10);    Serial.println("\n\nVS1053 SD Card Playback Example\n");// Start SPI bus    SPI.setHwCs(true);    SPI.begin(SPI_CLK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN);// Mount SD cardif (!mountSDcard()) {        Serial.println("SD card not mounted - system halted");while (1)delay(100);    }    Serial.println("SD card mounted - starting decoder...");// Initialize the VS1053 decoderif (!stream.startDecoder(VS1053_CS, VS1053_DCS, VS1053_DREQ) || !stream.isChipConnected()) {        Serial.println("Decoder not running - system halted");while (1)delay(100);    }    Serial.println("VS1053 running - starting SD playback");// Start playback from an SD file    stream.connecttofile(SD,"/test.mp3");if (!stream.isRunning()) {        Serial.println("No file running - system halted");while (1)delay(100);    }    Serial.print("Codec:");    Serial.println(stream.currentCodec());}voidloop() {    stream.loop();delay(5);}voidaudio_eof_stream(constchar* info) {    Serial.printf("End of file: %s\n", info);}

Known issues

Ogg files can not be started with an offset without first playing a couple of seconds from the start of the file.

Tips for troublefree streaming

WiFi setup

Do not forget to switch WiFi out of power save mode:

...WiFi.begin(SSID, PSK);WiFi.setSleep(false); ...

Prevent reboots while playing

Early version 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 runningesptool.py flash_id in a terminal.

In Arduino IDE go toFile->Preferences and find theShow verbose output during option. Check the box markedupload.
You can now see the hardware revision when you upload a sketch.

Functions

Initialize the VS1053 codec

boolstartDecoder(CS, DCS, DREQ)

Check if VS1053 is responding

boolisChipConnected()

Start or resume a stream

boolconnecttohost(url)
boolconnecttohost(url, offset)
boolconnecttohost(url, user, pwd)
boolconnecttohost(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 inESP32_VS1053_Stream.h by increasing these values:

#defineVS1053_CONNECT_TIMEOUT_MS250#defineVS1053_CONNECT_TIMEOUT_MS_SSL750

Start or resume a local file

boolconnecttofile(filesystem, filename)
boolconnecttofile(filesystem, filename, offset)

filesystem has to be mounted.

Stop a running stream

voidstopSong()

Feed the decoder

voidloop()

This function has to called every couple of ms to feed the decoder with data.
For bitrates up to 320kbps somewhere between 5-25 ms is about right.

Check if stream is running

boolisRunning()

Get the current volume

uint8_tgetVolume()

Set the volume

voidsetVolume(newVolume)

newVolume should be in the range 0-100.

Set bass and treble

uint8_t rtone[4]  = {toneha, tonehf, tonela, tonelf};voidsetTone(rtone)

Values forrtone:

toneha       = <0..15>// Setting treble gain (0 off, 1.5dB steps)tonehf       = <0..15>// Setting treble frequency lower limit x 1000 Hztonela       = <0..15>// Setting bass gain (0 = off, 1dB steps)tonelf       = <0..15>// Setting bass frequency lower limit x 10 Hz

Get the current used codec

constchar*currentCodec()

ReturnsSTOPPED if no stream is running.

Get the current stream url

constchar*lastUrl()

The current stream url might differ from the request url if the request url points to a playlist.

Get the filesize

size_tsize()

Returns0 if the stream is a radio stream.

Get the current position in the file

size_tposition()

Returns0 if the stream is a radio stream.

Get the buffer fill status

constchar *bufferStatus()

Returns0/0 if there is no buffer.
Otherwise returns something like4096/65536 which means 4kB waiting in a 64kB buffer.

voidbufferStatus(size_t &used,size_t &capacity)

This version takes twosize_t variables by reference.
Works the same as theconst char * version.

NOTE: A buffer will only be allocated if there is enough free psram.

Event callbacks

Station name callback.

voidaudio_showstation(constchar* info)

Stream information callback.

voidaudio_showstreamtitle(constchar* info)

End of file callback.

voidaudio_eof_stream(constchar* info)

Returns the eof url or path.
Also called if a stream or file times out/errors.

You can use this function for coding a playlist.
Useconnecttohost() orconnecttofile() inside this function to start the next item.

License

MIT License

Copyright (c) 2021 Cellie

Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.

About

A streaming library for Arduino esp32 with a vs1053 mp3/aac/ogg/flac decoder. Plays http, https (insecure mode) and chunked streams and parses the metadata. Also plays ogg and mp3 from sdcard.

Topics

Resources

License

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp