3#include "AudioTools/CoreAudio/AudioStreams.h"
4#include "AudioTools/AudioCodecs/CodecCopy.h"
5#include "AudioTools/AudioCodecs/AudioEncoded.h"
6#include "AudioTools/AudioCodecs/CodecWAV.h"
9# include "VS1053Driver.h"
16enum VS1053Mode {ENCODED_MODE, PCM_MODE, MIDI_MODE };
33 uint8_t dcs_pin = VS1053_DCS;
34 uint8_t dreq_pin = VS1053_DREQ;
35 int16_t reset_pin = VS1053_RESET;
36 int16_t cs_sd_pin = VS1053_CS_SD;
44 VS1053_INPUT input_device = VS1053_MIC;
62 VS1053StreamOut(VS1053 *vs){
65 size_t write(
const uint8_t *data,
size_t len)
override {
66 if (p_VS1053==
nullptr) {
71 p_VS1053->playChunk((uint8_t*)data, len);
75 VS1053 *p_VS1053=
nullptr;
119 LOGI(
"is_midi: %s", cfg.
is_midi?
"true":
"false");
120 LOGI(
"cs_pin: %d", cfg.
cs_pin);
121 LOGI(
"dcs_pin: %d", cfg.dcs_pin);
122 LOGI(
"dreq_pin: %d", cfg.dreq_pin);
123 LOGI(
"reset_pin: %d", cfg.reset_pin);
124 LOGI(
"cs_sd_pin: %d", cfg.cs_sd_pin);
126 if (p_vs1053==
nullptr){
127 p_vs1053 =
new VS1053(cfg.
cs_pin,cfg.dcs_pin,cfg.dreq_pin);
128 p_vs1053_out =
new VS1053StreamOut(p_vs1053);
134 LOGI(
"SPI not started");
137 if (cfg.reset_pin!=-1){
138 LOGI(
"Setting reset pin to high: %d", cfg.reset_pin);
139 pinMode(cfg.reset_pin, OUTPUT);
140 digitalWrite(cfg.reset_pin, HIGH);
152 const int MIDI_MODE = 100;
165 result = beginMidi();
173 LOGD(
"Mode not supported");
187 if (p_vs1053!=
nullptr){
189 p_vs1053->stopSong();
190 p_vs1053->softReset();
202 LOGD(
"setVolume: %f",
volume);
203 if (p_vs1053!=
nullptr){
205 p_vs1053->setVolume(
volume*100.0);
213 if (p_vs1053==
nullptr)
return -1.0;
214 return p_vs1053->getVolume()/100.0;;
222 LOGD(
"setBalance: %f",
balance);
223 if (p_vs1053!=
nullptr){
224 p_vs1053->setBalance(
balance*100.0);
230 if (p_vs1053==
nullptr)
return -1.0;
231 return static_cast<float>(p_vs1053->getBalance())/100.0;
235 virtual size_t write(
const uint8_t *data,
size_t len)
override {
237 if (len==0)
return 0;
238 if (p_out==
nullptr) {
239 LOGE(
"vs1053 is closed");
242 return p_out->write(data, len);
235 virtual size_t write(
const uint8_t *data,
size_t len)
override {
…}
255 logError(
"setEncoder");
258 if (p_encoder!=
nullptr){
266 int available()
override {
268 LOGI(
"available: %d", result);
271 size_t readBytes(uint8_t* data,
size_t len)
override {
279 return static_cast<float>(
getVS1053().treble())/100.0;
283 void setTreble(
float val){
285 if (value<0.0) value = 0.0;
286 if (value>1.0) value = 1.0;
287 LOGD(
"setTreble: %f", value);
294 return static_cast<float>(
getVS1053().bass())/100.0;
298 void setBass(
float val){
300 if (value<0.0) value = 0.0;
301 if (value>1.0) value = 1.0;
302 LOGD(
"setBass: %f", value);
307 void setTrebleFrequencyLimit(uint16_t value){
308 LOGD(
"setTrebleFrequencyLimit: %u", value);
309 getVS1053().setTrebleFrequencyLimit(value);
312 void setBassFrequencyLimit(uint16_t value){
313 LOGD(
"setBassFrequencyLimit: %u", value);
314 getVS1053().setBassFrequencyLimit(value);
318 void sendMidiMessage(uint8_t cmd, uint8_t data1, uint8_t data2) {
322 LOGE(
"start with is_midi=true");
325 if (p_vs1053==
nullptr) {
326 logError(__FUNCTION__);
329 p_vs1053->sendMidiMessage(cmd, data1, data2);
337 VS1053 *p_vs1053 =
nullptr;
338 VS1053StreamOut *p_vs1053_out =
nullptr;
339 EncodedAudioStream *p_out =
nullptr;
340 AudioEncoder *p_encoder =
new WAVEncoder();
346 bool result = p_vs1053->begin();
347 p_vs1053->startSong();
348 p_vs1053->switchToMp3Mode();
349 if (p_vs1053->getChipVersion() == 4) {
350 p_vs1053->loadDefaultVs1053Patches();
362 rec.setSampleRate(cfg.sample_rate);
363 rec.setChannels(cfg.channels);
364 rec.setInput(cfg.input_device);
365 return p_vs1053->beginInput(rec);
372 bool result = p_vs1053->beginMidi();
382 void logError(
const char* str){
383 LOGE(
"Call %s after begin()", str);
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:28