Skip to content

Instantly share code, notes, and snippets.

@Palatis
Last active September 15, 2016 19:34
Show Gist options
  • Save Palatis/0f1b9b5f91fe7cb59bf5c418452f1147 to your computer and use it in GitHub Desktop.
Save Palatis/0f1b9b5f91fe7cb59bf5c418452f1147 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <FS.h>
#include <i2s.h>
#include <opusfile_spiffs.hpp>
#define SAMPLE_RATE_HZ (32000)
void setup() {
Serial.begin(115200);
SPIFFS.begin();
i2s_set_rate(SAMPLE_RATE_HZ);
i2s_init();
}
void play_opus() {
Serial.printf("play with i2s_get_buffer() at %d hz\n", SAMPLE_RATE_HZ);
Palatis::OpusFileSPIFFS audio = Palatis::OpusFileSPIFFS("/test.opus");
// Serial.printf("file is %d bytes\n", audio.size());
i2s_begin();
time_t time1 = micros();
size_t pos = 0;
size_t total = SAMPLE_RATE_HZ * 4;
size_t delayed = 0;
while (audio.available()) {
uint32_t * buf;
time_t time3 = micros();
while ((buf = i2s_get_buffer()) == nullptr)
delay(0); // make the watchdog happy
time_t time4 = micros();
delayed += time4 - time3;
int num = audio.read_stereo(reinterpret_cast<opus_int16 *>(buf), 64 * 2);
i2s_put_buffer();
pos += num * 2;
if (pos >= total) {
time_t time2 = micros();
total += SAMPLE_RATE_HZ * 4;
Serial.printf("read %d bytes, took %.03f ms, idled %.03f ms, i2s %.03f ms, free heap %d bytes\n",
pos, (time2 - time1) / 1000.0, delayed / 1000.0, ((time2 - time1) - delayed) / 1000.0, ESP.getFreeHeap());
time1 = time2;
delayed = 0;
}
}
i2s_end();
}
void loop() {
time_t time1, time2;
time1 = millis();
play_opus();
time2 = millis();
Serial.printf("free %d bytes, total %d ms\n", ESP.getFreeHeap(), time2 - time1);
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment