From 1731a8b1db9c1c5ee1a9273ee04788bded7632f8 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 10 Apr 2017 20:04:49 +0200 Subject: [PATCH] callback for new audio data --- src/sound.cpp | 12 ++++++++++++ src/sound.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/sound.cpp b/src/sound.cpp index 25fafbf..38545e3 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -85,3 +85,15 @@ void Sound::stop() { void Sound::end() { i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver } + +/* callback(?) for new audio data */ +int Sound::render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples) { + int num_pushed_samples = 0; + for (int i=0; i < num_samples; i++) { + unsigned int sample = ((unsigned short) sample_buf_left[i] << 16 & 0xffff0000) | ((unsigned short) sample_buf_right[i]); + int num_pushed_bytes = i2s_push_sample(i2s_num, (char *)&sample, portMAX_DELAY); + if (num_pushed_bytes > 0) num_pushed_samples += num_pushed_bytes; + else break; + } + return num_pushed_samples; +} diff --git a/src/sound.h b/src/sound.h index d41763d..928c1d0 100644 --- a/src/sound.h +++ b/src/sound.h @@ -25,6 +25,7 @@ class Sound { void play(); // void pause(); void stop(); + int render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples); private: i2sbuffer_t buffer; const i2s_port_t i2s_num = (i2s_port_t)I2S_NUM;