Skip to content

Commit af3e2c5

Browse files
committed
VS1053 sdfat
1 parent d27d12a commit af3e2c5

File tree

8 files changed

+77
-10
lines changed

8 files changed

+77
-10
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @file player-sdfat-vs1053.ino
3+
* @brief Audio player which sends the output to a VS1053 module. Using a module with built in SD card
4+
*
5+
* @author Phil Schatzmann
6+
* @copyright GPLv3
7+
*/
8+
9+
// install https://github.com/pschatzmann/arduino-vs1053.git
10+
// install https://github.com/greiman/SdFat
11+
12+
#include "AudioTools.h"
13+
#include "AudioLibs/VS1053Stream.h"
14+
#include "AudioLibs/AudioSourceSDFAT.h"
15+
#include "AudioCodecs/CodecCopy.h"
16+
17+
const char *startFilePath="/";
18+
const char* ext="mp3";
19+
AudioSouSdSpiConfig sd_cfg(PIN_CS, SHARED_SPI, SPI_CLOCK);
20+
AudioSourceSDFAT source(startFilePath, ext, cfg);
21+
VS1053Stream vs1053; // final output
22+
AudioPlayer player(source, vs1053, *new CopyDecoder());
23+
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
28+
29+
// setup output
30+
auto cfg = vs1053.defaultConfig();
31+
cfg.is_encoded_data = true; // vs1053 is accepting encoded data
32+
// Use your custom pins or define in AudioCodnfig.h
33+
//cfg.cs_pin = VS1053_CS;
34+
//cfg.dcs_pin = VS1053_DCS;
35+
//cfg.dreq_pin = VS1053_DREQ;
36+
//cfg.reset_pin = VS1053_RESET;
37+
vs1053.begin(cfg);
38+
vs1053.setVolume(1.0); // full volume
39+
40+
// setup player
41+
player.begin();
42+
43+
// select file with setPath() or setIndex()
44+
//player.setPath("/ZZ Top/Unknown Album/Lowrider.mp3");
45+
//player.setIndex(1); // 2nd file
46+
47+
}
48+
49+
void loop() {
50+
player.copy();
51+
}

examples/examples-vs1053/streams-generator-vs1053/streams-generator-vs1053.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void setup(){
2626
AudioLogger::instance().begin(Serial, AudioLogger::Warning); // Info is causing a lot of noise
2727

2828
// Setup sine wave
29-
sineWave.begin(channels, sample_rate, N_A4);
29+
sineWave.begin(info, N_A4);
3030

3131
// setup output
3232
auto cfg = out.defaultConfig();

src/AudioCodecs/AudioEncoded.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ class EncodedAudioOutput : public AudioStream {
367367

368368
/// encodeor decode the data
369369
virtual size_t write(const uint8_t *data, size_t len) override {
370-
LOGD("EncodedAudioOutput::write: %zu", len);
370+
LOGD("EncodedAudioOutput::write: %d", (int)len);
371371
if (len == 0) {
372372
LOGI("write: %d", 0);
373373
return 0;
@@ -379,6 +379,7 @@ class EncodedAudioOutput : public AudioStream {
379379
}
380380

381381
size_t result = writer_ptr->write(data, len);
382+
LOGD("EncodedAudioOutput::write: %d -> %d", (int)len, (int)result);
382383
return result;
383384
}
384385

src/AudioCodecs/CodecCopy.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class CopyDecoder : public AudioDecoder {
3636

3737
AudioInfo audioInfo() { AudioInfo dummy; return dummy; }
3838

39-
size_t write(const void *data, size_t len) { return pt_print->write((uint8_t*)data,len); }
39+
size_t write(const void *data, size_t len) {
40+
TRACED();
41+
return pt_print->write((uint8_t*)data,len);
42+
}
4043

4144
operator bool() { return true; }
4245

src/AudioConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ using WiFiServerSecure = BearSSL::WiFiServerSecure;
610610
#define VS1053_DEFINED
611611
#define PIN_CS 9
612612

613-
#ifdef ARDUINO
613+
#if defined(ARDUINO) && !defined(ARDUINO_MINIMA)
614614
# define USE_WIFI
615615
# define USE_URL_ARDUINO
616616
# define USE_AUDIO_SERVER

src/AudioLibs/AudioSourceSDFAT.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ namespace audio_tools {
4848
class AudioSourceSDFAT : public AudioSource {
4949
public:
5050
/// Default constructor
51-
AudioSourceSDFAT(const char* startFilePath = "/", const char* ext = ".mp3", int chipSelect = PIN_CS, int speedMHz = 2, bool setupIndex=true) {
51+
AudioSourceSDFAT(const char* startFilePath = "/", const char* ext = ".mp3", int chipSelect = PIN_CS, int speedMHz = 2,int spi_mode=DEDICATED_SPI, bool setupIndex=true) {
5252
TRACED();
5353
LOGI("SD chipSelect: %d", chipSelect);
5454
LOGI("SD speedMHz: %d", speedMHz);
5555
LOGI("ext: %s", ext);
56-
p_cfg = new SdSpiConfig(chipSelect, DEDICATED_SPI, SD_SCK_MHZ(speedMHz));
56+
p_cfg = new SdSpiConfig(chipSelect, spi_mode, SD_SCK_MHZ(speedMHz));
5757
owns_cfg = true;
5858
start_path = startFilePath;
5959
exension = ext;

src/AudioLibs/VS1053Stream.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ class VS1053Stream : public AudioStream {
6060
p_VS1053 = vs;
6161
}
6262
size_t write(const uint8_t *buffer, size_t size) override {
63-
if (p_VS1053==nullptr) return 0;
63+
if (p_VS1053==nullptr) {
64+
LOGE("NPE");
65+
return 0;
66+
}
67+
TRACED();
6468
p_VS1053->playChunk((uint8_t*)buffer, size);
6569
return size;
6670
}
@@ -224,8 +228,12 @@ class VS1053Stream : public AudioStream {
224228
}
225229

226230
/// Write audio data
227-
virtual size_t write(const uint8_t *buffer, size_t size) override{
228-
if (p_out==nullptr) return 0;
231+
virtual size_t write(const uint8_t *buffer, size_t size) override {
232+
TRACED();
233+
if (p_out==nullptr) {
234+
LOGE("NPE");
235+
return 0;
236+
}
229237
return p_out->write(buffer, size);
230238
}
231239

src/AudioTools/StreamCopy.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,21 @@ class StreamCopyT {
138138
#ifndef COPY_LOG_OFF
139139
LOGI("StreamCopy::copy %u -> %u -> %u bytes - in %u hops", (unsigned int)bytes_to_read,(unsigned int) bytes_read, (unsigned int)result, (unsigned int)delayCount);
140140
#endif
141+
TRACED();
141142

142143
if (result==0){
143-
// give the processor some time
144+
TRACED();
145+
// give the processor some time
144146
delay(delay_on_no_data);
145147
}
146148

149+
TRACED();
147150
CHECK_MEMORY();
148151
} else {
149152
// give the processor some time
150153
delay(delay_on_no_data);
151154
}
155+
TRACED();
152156
return result;
153157
}
154158

0 commit comments

Comments
 (0)