3 #include "AudioTools/CoreAudio/AudioStreams.h"
4 #include "AudioTools/AudioCodecs/CodecCopy.h"
6 # include "VS1053Driver.h"
13 enum VS1053Mode {ENCODED_MODE, PCM_MODE, MIDI_MODE };
30 uint8_t dcs_pin = VS1053_DCS;
31 uint8_t dreq_pin = VS1053_DREQ;
32 int16_t reset_pin = VS1053_RESET;
33 int16_t cs_sd_pin = VS1053_CS_SD;
41 VS1053_INPUT input_device = VS1053_MIC;
59 VS1053StreamOut(VS1053 *vs){
62 size_t write(
const uint8_t *data,
size_t len)
override {
63 if (p_VS1053==
nullptr) {
68 p_VS1053->playChunk((uint8_t*)data, len);
72 VS1053 *p_VS1053=
nullptr;
116 LOGI(
"is_midi: %s", cfg.
is_midi?
"true":
"false");
117 LOGI(
"cs_pin: %d", cfg.
cs_pin);
118 LOGI(
"dcs_pin: %d", cfg.dcs_pin);
119 LOGI(
"dreq_pin: %d", cfg.dreq_pin);
120 LOGI(
"reset_pin: %d", cfg.reset_pin);
121 LOGI(
"cs_sd_pin: %d", cfg.cs_sd_pin);
123 if (p_vs1053==
nullptr){
124 p_vs1053 =
new VS1053(cfg.
cs_pin,cfg.dcs_pin,cfg.dreq_pin);
125 p_vs1053_out =
new VS1053StreamOut(p_vs1053);
131 LOGI(
"SPI not started");
134 if (cfg.reset_pin!=-1){
135 LOGI(
"Setting reset pin to high: %d", cfg.reset_pin);
136 pinMode(cfg.reset_pin, OUTPUT);
137 digitalWrite(cfg.reset_pin, HIGH);
149 const int MIDI_MODE = 100;
162 result = beginMidi();
170 LOGD(
"Mode not supported");
184 if (p_vs1053!=
nullptr){
186 p_vs1053->stopSong();
187 p_vs1053->softReset();
199 LOGD(
"setVolume: %f",
volume);
200 if (p_vs1053!=
nullptr){
202 p_vs1053->setVolume(
volume*100.0);
210 if (p_vs1053==
nullptr)
return -1.0;
211 return p_vs1053->getVolume()/100.0;;
219 LOGD(
"setBalance: %f",
balance);
220 if (p_vs1053!=
nullptr){
221 p_vs1053->setBalance(
balance*100.0);
227 if (p_vs1053==
nullptr)
return -1.0;
228 return static_cast<float>(p_vs1053->getBalance())/100.0;
232 virtual size_t write(
const uint8_t *data,
size_t len)
override {
234 if (len==0)
return 0;
235 if (p_out==
nullptr) {
236 LOGE(
"vs1053 is closed");
239 return p_out->write(data, len);
252 logError(
"setEncoder");
255 if (p_encoder!=
nullptr){
263 int available()
override {
265 LOGI(
"available: %d", result);
268 size_t readBytes(uint8_t* data,
size_t len)
override {
276 return static_cast<float>(
getVS1053().treble())/100.0;
280 void setTreble(
float val){
282 if (value<0.0) value = 0.0;
283 if (value>1.0) value = 1.0;
284 LOGD(
"setTreble: %f", value);
291 return static_cast<float>(
getVS1053().bass())/100.0;
295 void setBass(
float val){
297 if (value<0.0) value = 0.0;
298 if (value>1.0) value = 1.0;
299 LOGD(
"setBass: %f", value);
304 void setTrebleFrequencyLimit(uint16_t value){
305 LOGD(
"setTrebleFrequencyLimit: %u", value);
306 getVS1053().setTrebleFrequencyLimit(value);
309 void setBassFrequencyLimit(uint16_t value){
310 LOGD(
"setBassFrequencyLimit: %u", value);
311 getVS1053().setBassFrequencyLimit(value);
315 void sendMidiMessage(uint8_t cmd, uint8_t data1, uint8_t data2) {
319 LOGE(
"start with is_midi=true");
322 if (p_vs1053==
nullptr) {
323 logError(__FUNCTION__);
326 p_vs1053->sendMidiMessage(cmd, data1, data2);
334 VS1053 *p_vs1053 =
nullptr;
335 VS1053StreamOut *p_vs1053_out =
nullptr;
336 EncodedAudioStream *p_out =
nullptr;
337 AudioEncoder *p_encoder =
new WAVEncoder();
343 bool result = p_vs1053->begin();
344 p_vs1053->startSong();
345 p_vs1053->switchToMp3Mode();
346 if (p_vs1053->getChipVersion() == 4) {
347 p_vs1053->loadDefaultVs1053Patches();
359 rec.setSampleRate(cfg.sample_rate);
360 rec.setChannels(cfg.channels);
361 rec.setInput(cfg.input_device);
362 return p_vs1053->beginInput(rec);
369 bool result = p_vs1053->beginMidi();
379 void logError(
const char* str){
380 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