Skip to content

Commit

Permalink
VBAN from player
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 10, 2023
1 parent fbd9edf commit 1035d2b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @file player-sdmmc-vban.ino
* @author Phil Schatzmann
* @brief decodes mp3 to vban: We need to use a queue to prevent delays to get too big.
* We try to keep the queue filled, so that we can always provide pcm data to vban
*/

#include "AudioTools.h"
#include "AudioCodecs/CodecMP3Helix.h"
#include "AudioLibs/VBANStream.h"
#include "AudioLibs/AudioSourceSDMMC.h" // or AudioSourceIdxSDMMC.h

const char *startFilePath="/";
const char* ext="mp3";
const int mp3_pcm_size = 14000;
AudioInfo info(44100,2,16);
AudioSourceSDMMC source(startFilePath, ext);
RingBuffer<uint8_t> ring_buffer(mp3_pcm_size*3);
QueueStream<uint8_t> queue(ring_buffer);
MP3DecoderHelix decoder; // or change to MP3DecoderMAD
AudioPlayer player(source, queue, decoder);

VBANStream out;
StreamCopy copier(out, queue, 2048); // 44100 needs 2048


void fillQueue(){
// fill queue when smaller then limit
if(ring_buffer.availableForWrite() >= mp3_pcm_size){
player.copy();
// activate copy when limit is reached
if (!copier.isActive() && ring_buffer.available()>=23000){
copier.setActive(true);
LOGI("copier active");
}
}
LOGI("available: %d - avail to write: %d", ring_buffer.available(), ring_buffer.availableForWrite());
}


void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Warning);

// setup output
auto cfg = out.defaultConfig(TX_MODE);
cfg.copyFrom(info);
cfg.ssid = "ssid";
cfg.password = "password";
cfg.stream_name = "Stream1";
cfg.target_ip = IPAddress{192,168,1,37};
cfg.throttle_active = true;
cfg.throttle_correction_ms = -20;
if (!out.begin(cfg)) stop();

// setup player
player.setVolume(0.7);
player.begin();

// select file with setPath() or setIndex()
//player.setPath("/ZZ Top/Unknown Album/Lowrider.mp3");
//player.setIndex(1); // 2nd file

queue.begin(); // start queue
copier.setActive(false); // deactivate copy

}

void loop() {
fillQueue();
// output from queue
copier.copy();
}
15 changes: 15 additions & 0 deletions src/AudioTools/StreamCopy.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class StreamCopyT {
/// copies the data from the source to the destination - the result is in bytes
inline size_t copy(){
TRACED();
if (!active) return 0;
// if not initialized we do nothing
if (from==nullptr || to==nullptr) return 0;

Expand Down Expand Up @@ -177,6 +178,7 @@ class StreamCopyT {

/// Copies pages * buffersize samples
size_t copyN(size_t pages){
if (!active) return 0;
size_t total=0;
for (size_t j=0;j<pages;j++){
total+=copy();
Expand All @@ -186,13 +188,15 @@ class StreamCopyT {

/// Copies audio for the indicated number of milliseconds: note that the resolution is determined by the buffer_size
size_t copyMs(size_t millis, AudioInfo info){
if (!active) return 0;
size_t pages = AudioTime::toBytes(millis, info) / buffer_size;
return copyN(pages);
}

/// copies all data - returns true if we copied anything
size_t copyAll(int retryCount=5, int retryWaitMs=200){
TRACED();
if (!active) return 0;
size_t result = 0;
int retry = 0;

Expand Down Expand Up @@ -268,6 +272,16 @@ class StreamCopyT {
buffer.resize(buffer_size);
}

/// deactivate/activate copy - active by default
void setActive(bool flag){
active = flag;
}

/// Check if copier is active
bool isActive(){
return active;
}

protected:
AudioStream *from = nullptr;
Print *to = nullptr;
Expand All @@ -282,6 +296,7 @@ class StreamCopyT {
const char* actual_mime = nullptr;
int retryLimit = COPY_RETRY_LIMIT;
int delay_on_no_data = COPY_DELAY_ON_NODATA;
bool active = true;

/// blocking write - until everything is processed
size_t write(size_t len, size_t &delayCount ){
Expand Down
2 changes: 1 addition & 1 deletion src/AudioTools/VolumeStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class VolumeStream : public AudioStream {

/// Writes raw PCM audio data, which will be the input for the volume control
virtual size_t write(const uint8_t *buffer, size_t size) override {
TRACED();
LOGD("VolumeStream::write: %d", size);
if (buffer==nullptr || p_out==nullptr){
LOGE("NPE");
return 0;
Expand Down

0 comments on commit 1035d2b

Please sign in to comment.