|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "AudioTools/AudioTypes.h" |
| 4 | + |
| 5 | +namespace audio_tools { |
| 6 | + |
| 7 | +const char* raw_mime = "audio/pcm"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @brief RAWDecoder - Actually this class does no encoding or decoding at all. It just passes on the |
| 11 | + * data. The reason that this class exists is that we can use the same processing chain for different |
| 12 | + * file types and just replace the decoder. |
| 13 | + * |
| 14 | + * @author Phil Schatzmann |
| 15 | + * @copyright GPLv3 |
| 16 | + */ |
| 17 | +class RAWDecoder : public AudioDecoder { |
| 18 | + public: |
| 19 | + /** |
| 20 | + * @brief Construct a new RAWDecoder object |
| 21 | + */ |
| 22 | + |
| 23 | + RAWDecoder(){ |
| 24 | + LOGD(LOG_METHOD); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @brief Construct a new RAWDecoder object |
| 29 | + * |
| 30 | + * @param out_stream Output Stream to which we write the decoded result |
| 31 | + */ |
| 32 | + RAWDecoder(Print &out_stream, bool active=true){ |
| 33 | + LOGD(LOG_METHOD); |
| 34 | + p_print = &out_stream; |
| 35 | + this->active = active; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Construct a new RAWDecoder object |
| 40 | + * |
| 41 | + * @param out_stream Output Stream to which we write the decoded result |
| 42 | + * @param bi Object that will be notified about the Audio Formt (Changes) |
| 43 | + */ |
| 44 | + |
| 45 | + RAWDecoder(Print &out_stream, AudioBaseInfoDependent &bi){ |
| 46 | + LOGD(LOG_METHOD); |
| 47 | + p_print = &out_stream; |
| 48 | + } |
| 49 | + |
| 50 | + /// Defines the output Stream |
| 51 | + void setOutputStream(Print &out_stream) override { |
| 52 | + p_print = &out_stream; |
| 53 | + } |
| 54 | + |
| 55 | + void setNotifyAudioChange(AudioBaseInfoDependent &bi) override { |
| 56 | + this->bid = &bi; |
| 57 | + } |
| 58 | + |
| 59 | + AudioBaseInfo audioInfo() override { |
| 60 | + return cfg; |
| 61 | + } |
| 62 | + |
| 63 | + void begin(AudioBaseInfo info) { |
| 64 | + LOGD(LOG_METHOD); |
| 65 | + cfg = info; |
| 66 | + if (bid!=nullptr){ |
| 67 | + bid->setAudioInfo(cfg); |
| 68 | + } |
| 69 | + active = true; |
| 70 | + } |
| 71 | + |
| 72 | + void begin() override { |
| 73 | + LOGD(LOG_METHOD); |
| 74 | + active = true; |
| 75 | + } |
| 76 | + |
| 77 | + void end() override { |
| 78 | + LOGD(LOG_METHOD); |
| 79 | + active = false; |
| 80 | + } |
| 81 | + |
| 82 | + virtual size_t write(const void *in_ptr, size_t in_size) override { |
| 83 | + if (p_print!=nullptr) return 0; |
| 84 | + return p_print->write((uint8_t*)in_ptr, in_size); |
| 85 | + } |
| 86 | + |
| 87 | + virtual operator boolean() override { |
| 88 | + return active; |
| 89 | + } |
| 90 | + |
| 91 | + protected: |
| 92 | + Print *p_print=nullptr; |
| 93 | + AudioBaseInfoDependent *bid=nullptr; |
| 94 | + AudioBaseInfo cfg; |
| 95 | + bool active; |
| 96 | + |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief RAWDecoder - Actually this class does no encoding or decoding at all. It just passes on the |
| 101 | + * data. |
| 102 | + * @author Phil Schatzmann |
| 103 | + * @copyright GPLv3 |
| 104 | + */ |
| 105 | +class RAWEncoder : public AudioEncoder { |
| 106 | + public: |
| 107 | + // Empty Constructor - the output stream must be provided with begin() |
| 108 | + RAWEncoder(){ |
| 109 | + } |
| 110 | + |
| 111 | + // Constructor providing the output stream |
| 112 | + RAWEncoder(Print &out){ |
| 113 | + p_print = &out; |
| 114 | + } |
| 115 | + |
| 116 | + /// Defines the output Stream |
| 117 | + void setOutputStream(Print &out_stream) override { |
| 118 | + p_print = &out_stream; |
| 119 | + } |
| 120 | + |
| 121 | + /// Provides "audio/pcm" |
| 122 | + const char* mime() override{ |
| 123 | + return raw_mime; |
| 124 | + } |
| 125 | + |
| 126 | + /// We actually do nothing with this |
| 127 | + virtual void setAudioInfo(AudioBaseInfo from) override { |
| 128 | + } |
| 129 | + |
| 130 | + /// starts the processing using the actual RAWAudioInfo |
| 131 | + virtual void begin() override{ |
| 132 | + is_open = true; |
| 133 | + } |
| 134 | + |
| 135 | + /// starts the processing |
| 136 | + void begin(Print &out) { |
| 137 | + p_print = &out; |
| 138 | + begin(); |
| 139 | + } |
| 140 | + |
| 141 | + /// stops the processing |
| 142 | + void end() override { |
| 143 | + is_open = false; |
| 144 | + } |
| 145 | + |
| 146 | + /// Writes PCM data to be encoded as RAW |
| 147 | + virtual size_t write(const void *in_ptr, size_t in_size) override { |
| 148 | + if (p_print!=nullptr) return 0; |
| 149 | + return p_print->write((uint8_t*)in_ptr, in_size); |
| 150 | + } |
| 151 | + |
| 152 | + operator boolean() override { |
| 153 | + return is_open; |
| 154 | + } |
| 155 | + |
| 156 | + bool isOpen(){ |
| 157 | + return is_open; |
| 158 | + } |
| 159 | + |
| 160 | + protected: |
| 161 | + Print* p_print=nullptr;; |
| 162 | + volatile bool is_open; |
| 163 | + |
| 164 | +}; |
| 165 | + |
| 166 | +} |
0 commit comments