|
| 1 | +#pragma once |
| 2 | +#include "AudioTools/Concurrency/RP2040.h" |
| 3 | +#include "SnapOutput.h" |
| 4 | + |
| 5 | +namespace snap_arduino { |
| 6 | + |
| 7 | +/** |
| 8 | + * @brief Processor for which the encoded output is buffered in a queue. The decoding and |
| 9 | + * audio output can be done on the second core by calling loop1(); |
| 10 | + * |
| 11 | + * @author Phil Schatzmann |
| 12 | + * @version 0.1 |
| 13 | + * @date 2024-02-26 |
| 14 | + * @copyright Copyright (c) 2023 |
| 15 | + */ |
| 16 | +class SnapProcessorRP2040 : public SnapProcessor { |
| 17 | + public: |
| 18 | + /// Default constructor |
| 19 | + SnapProcessorRP2040(SnapOutput &output, int bufferSizeBytes, int activationAtPercent = 75) |
| 20 | + : SnapProcessor(output) { |
| 21 | + active_percent = activationAtPercent; |
| 22 | + initQueues(bufferSizeBytes); |
| 23 | + } |
| 24 | + /// Default constructor |
| 25 | + SnapProcessorRP2040(int bufferSizeBytes, int activationAtPercent = 75) |
| 26 | + : SnapProcessor() { |
| 27 | + active_percent = activationAtPercent; |
| 28 | + initQueues(bufferSizeBytes); |
| 29 | + } |
| 30 | + |
| 31 | + bool begin() override { |
| 32 | + ESP_LOGW(TAG, "begin: %d", buffer_count * 1024); |
| 33 | + // regular begin logic |
| 34 | + bool result = SnapProcessor::begin(); |
| 35 | + // allocate buffer, so that we could use psram |
| 36 | + size_queue.resize(RTOS_MAX_QUEUE_ENTRY_COUNT); |
| 37 | + buffer.resize(buffer_count * 1024); |
| 38 | + is_active = false; |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + void end(void) override { |
| 43 | + size_queue.clear(); |
| 44 | + buffer.reset(); |
| 45 | + SnapProcessor::end(); |
| 46 | + } |
| 47 | + |
| 48 | + bool doLoop1() override { |
| 49 | + ESP_LOGD(TAG, "doLoop1 %d / %d", size_queue.available(), buffer.available()); |
| 50 | + if (!isBufferActive()) return true; |
| 51 | + |
| 52 | + size_t size = 0; |
| 53 | + if (size_queue.readArray(&size, 1)) { |
| 54 | + uint8_t data[size]; |
| 55 | + int read = buffer.readArray(data, size); |
| 56 | + if (read != size){ |
| 57 | + ESP_LOGE(TAG, "readArray failed %d -> %d", size, read); |
| 58 | + } |
| 59 | + int written = p_snap_output->audioWrite(data, size); |
| 60 | + if (written != size) { |
| 61 | + ESP_LOGE(TAG, "write error: %d of %d", written, size); |
| 62 | + } |
| 63 | + } |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + protected: |
| 68 | + const char *TAG = "SnapProcessorRP2040"; |
| 69 | + audio_tools::BufferRP2040T<size_t> size_queue{1, 0}; |
| 70 | + audio_tools::BufferRP2040T<uint8_t> buffer{1024, 0}; // size defined in begin |
| 71 | + int buffer_count = 0; |
| 72 | + bool is_active = false; |
| 73 | + int active_percent = 0; |
| 74 | + |
| 75 | + bool isBufferActive() { |
| 76 | + if (!is_active && buffer.available()>0) { |
| 77 | + |
| 78 | + int limit = buffer.size() * active_percent / 100; |
| 79 | + if (buffer.available() >= limit) { |
| 80 | + LOGI("Setting buffer active"); |
| 81 | + is_active = true; |
| 82 | + } |
| 83 | + delay(10); |
| 84 | + } |
| 85 | + return is_active; |
| 86 | + } |
| 87 | + |
| 88 | + /// store parameters provided by constructor |
| 89 | + void initQueues(int bufferSizeBytes) { |
| 90 | + buffer_count = bufferSizeBytes / 1024; |
| 91 | + if (buffer_count <= 2){ |
| 92 | + buffer_count = 2; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Writes the encoded audio data to a queue |
| 97 | + size_t writeAudio(const uint8_t *data, size_t size) override { |
| 98 | + |
| 99 | + if (size > buffer.size()) { |
| 100 | + ESP_LOGE(TAG, "The buffer is too small with %d. Use a multiple of %d", buffer.size(), size); |
| 101 | + stop(); |
| 102 | + } |
| 103 | + |
| 104 | + ESP_LOGI(TAG, "size: %zu / buffer %d", size, buffer.available()); |
| 105 | + if (!p_snap_output->isStarted() || size == 0) { |
| 106 | + ESP_LOGW(TAG, "not started"); |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + // if (!p_snap_output->synchronizePlayback()) { |
| 111 | + // return size; |
| 112 | + // } |
| 113 | + |
| 114 | + if (!size_queue.writeArray(&size, 1)) { |
| 115 | + ESP_LOGW(TAG, "size_queue full"); |
| 116 | + return 0; |
| 117 | + } |
| 118 | + |
| 119 | + size_t size_written = buffer.writeArray(data, size); |
| 120 | + if (size_written != size) { |
| 121 | + ESP_LOGE(TAG, "buffer-overflow"); |
| 122 | + } |
| 123 | + |
| 124 | + return size; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +} // namespace snap_arduino |
0 commit comments