|
| 1 | + |
| 2 | +/** |
| 3 | + * @file receive-mp3.ino |
| 4 | + * @brief Example of receiving an mp3 stream over serial and playing it over I2S |
| 5 | + * using the AudioTools library. |
| 6 | + * The processing implements a flow control using a custom digical pin. We process |
| 7 | + * the data receving in a separate task and the playback in the main loop. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "AudioTools.h" |
| 11 | +#include "AudioTools/AudioCodecs/CodecMP3Helix.h" |
| 12 | +#include "AudioTools/AudioLibs/AudioBoardStream.h" |
| 13 | +#include "AudioTools/Concurrency/RTOS.h" |
| 14 | + |
| 15 | +const int flowControlPin = 17; // flow control pin acitive low |
| 16 | +const int min_percent = 10; |
| 17 | +const int max_percent = 90; |
| 18 | + |
| 19 | +AudioBoardStream i2s(AudioKitEs8388V1); // final output of decoded stream |
| 20 | +EncodedAudioStream dec(&i2s, new MP3DecoderHelix()); // Decoding stream |
| 21 | +// queue |
| 22 | +BufferRTOS<uint8_t> buffer(0); |
| 23 | +QueueStream<uint8_t> queue(buffer); |
| 24 | +// copy |
| 25 | +StreamCopy copierFill(queue, Serial1); |
| 26 | +StreamCopy copierPlay(dec, queue); |
| 27 | + |
| 28 | +Task task("mp3-copy", 10000, 1, 0); |
| 29 | + |
| 30 | +void setup() { |
| 31 | + Serial.begin(115200); |
| 32 | + AudioLogger::instance().begin(Serial, AudioLogger::Info); |
| 33 | + |
| 34 | + Serial1.begin(115200); |
| 35 | + pinMode(flowControlPin, OUTPUT); // flow control pin |
| 36 | + |
| 37 | + // set up buffer here to allow PSRAM usage |
| 38 | + buffer.resize(1024 * 10); // 10kB buffer |
| 39 | + queue.begin(50); // start when half full |
| 40 | + |
| 41 | + // setup i2s |
| 42 | + auto config = i2s.defaultConfig(TX_MODE); |
| 43 | + i2s.begin(config); |
| 44 | + |
| 45 | + // setup decoder |
| 46 | + dec.begin(); |
| 47 | + |
| 48 | + // start fill buffer copy task |
| 49 | + task.begin([]() { |
| 50 | + copierFill.copy(); |
| 51 | + // data synchronization to prevent buffer overflow |
| 52 | + if (buffer.levelPercent() > max_percent) { |
| 53 | + digitalWrite(flowControlPin, HIGH); // stop receiving |
| 54 | + } else if (buffer.levelPercent() < min_percent) { |
| 55 | + digitalWrite(flowControlPin, LOW); // start receiving |
| 56 | + } |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +void loop() { copierPlay.copy(); } |
0 commit comments