BufferedAudioSink.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "BufferedAudioSink.h"
  2. #include "driver/i2s.h"
  3. #include "freertos/ringbuf.h"
  4. #include "freertos/task.h"
  5. RingbufHandle_t dataBuffer;
  6. static void i2sFeed(void* pvParameters) {
  7. while (true) {
  8. size_t itemSize;
  9. char* item = (char*)xRingbufferReceiveUpTo(dataBuffer, &itemSize,
  10. portMAX_DELAY, 512);
  11. if (item != NULL) {
  12. size_t written = 0;
  13. while (written < itemSize) {
  14. i2s_write((i2s_port_t)0, item, itemSize, &written, portMAX_DELAY);
  15. }
  16. vRingbufferReturnItem(dataBuffer, (void*)item);
  17. }
  18. }
  19. }
  20. void BufferedAudioSink::startI2sFeed(size_t buf_size) {
  21. dataBuffer = xRingbufferCreate(buf_size, RINGBUF_TYPE_BYTEBUF);
  22. xTaskCreatePinnedToCore(&i2sFeed, "i2sFeed", 4096, NULL, 10, NULL,
  23. tskNO_AFFINITY);
  24. }
  25. void BufferedAudioSink::feedPCMFrames(const uint8_t* buffer, size_t bytes) {
  26. feedPCMFramesInternal(buffer, bytes);
  27. }
  28. void BufferedAudioSink::feedPCMFramesInternal(const void* pvItem,
  29. size_t xItemSize) {
  30. xRingbufferSend(dataBuffer, pvItem, xItemSize, portMAX_DELAY);
  31. }
  32. bool BufferedAudioSink::setParams(uint32_t sampleRate, uint8_t channelCount,
  33. uint8_t bitDepth) {
  34. // TODO override this for sinks with custom mclk
  35. i2s_set_clk((i2s_port_t)0, sampleRate, (i2s_bits_per_sample_t)bitDepth,
  36. (i2s_channel_t)channelCount);
  37. return true;
  38. }