|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "AudioCodecs/AudioEncoded.h" |
| 4 | + |
| 5 | +namespace audio_tools { |
| 6 | + |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief DecoderFloat - Converts an 8 Bit Stream into Floats |
| 10 | + * |
| 11 | + * @author Phil Schatzmann |
| 12 | + * @copyright GPLv3 |
| 13 | + */ |
| 14 | +class DecoderFloat : public AudioDecoder { |
| 15 | + public: |
| 16 | + /** |
| 17 | + * @brief Construct a new RAWDecoder object |
| 18 | + */ |
| 19 | + |
| 20 | + DecoderFloat(){ |
| 21 | + TRACED(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @brief Construct a new RAWDecoder object |
| 26 | + * |
| 27 | + * @param out_stream Output Stream to which we write the decoded result |
| 28 | + */ |
| 29 | + DecoderFloat(Print &out_stream, bool active=true){ |
| 30 | + TRACED(); |
| 31 | + p_print = &out_stream; |
| 32 | + this->active = active; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @brief Construct a new RAWDecoder object |
| 37 | + * |
| 38 | + * @param out_stream Output Stream to which we write the decoded result |
| 39 | + * @param bi Object that will be notified about the Audio Formt (Changes) |
| 40 | + */ |
| 41 | + |
| 42 | + DecoderFloat(Print &out_stream, AudioBaseInfoDependent &bi){ |
| 43 | + TRACED(); |
| 44 | + p_print = &out_stream; |
| 45 | + } |
| 46 | + |
| 47 | + /// Defines the output Stream |
| 48 | + void setOutputStream(Print &out_stream) override { |
| 49 | + p_print = &out_stream; |
| 50 | + } |
| 51 | + |
| 52 | + void setNotifyAudioChange(AudioBaseInfoDependent &bi) override { |
| 53 | + this->bid = &bi; |
| 54 | + } |
| 55 | + |
| 56 | + AudioBaseInfo audioInfo() override { |
| 57 | + return cfg; |
| 58 | + } |
| 59 | + |
| 60 | + void begin(AudioBaseInfo info) { |
| 61 | + TRACED(); |
| 62 | + cfg = info; |
| 63 | + if (bid!=nullptr){ |
| 64 | + bid->setAudioInfo(cfg); |
| 65 | + } |
| 66 | + active = true; |
| 67 | + } |
| 68 | + |
| 69 | + void begin() override { |
| 70 | + TRACED(); |
| 71 | + active = true; |
| 72 | + } |
| 73 | + |
| 74 | + void end() override { |
| 75 | + TRACED(); |
| 76 | + active = false; |
| 77 | + } |
| 78 | + |
| 79 | + /// Converts data from float to int16_t |
| 80 | + virtual size_t write(const void *data, size_t in_size) override { |
| 81 | + if (p_print==nullptr) return 0; |
| 82 | + int samples = in_size/sizeof(float); |
| 83 | + buffer.resize(samples); |
| 84 | + float* p_float = (float*) data; |
| 85 | + for (int j=0;j<samples;j++){ |
| 86 | + buffer[j] = p_float[j]*32767; |
| 87 | + } |
| 88 | + return p_print->write((uint8_t*)buffer.data(), samples*sizeof(int16_t)); |
| 89 | + } |
| 90 | + |
| 91 | + virtual operator bool() override { |
| 92 | + return active; |
| 93 | + } |
| 94 | + |
| 95 | + protected: |
| 96 | + Print *p_print=nullptr; |
| 97 | + AudioBaseInfoDependent *bid=nullptr; |
| 98 | + AudioBaseInfo cfg; |
| 99 | + bool active; |
| 100 | + Vector<int16_t> buffer; |
| 101 | + |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * @brief EncoderFloats - Encodes 16 bit PCM data stream to floats |
| 106 | + * data. |
| 107 | + * @author Phil Schatzmann |
| 108 | + * @copyright GPLv3 |
| 109 | + */ |
| 110 | +class EncoderFloat : public AudioEncoder { |
| 111 | + public: |
| 112 | + // Empty Constructor - the output stream must be provided with begin() |
| 113 | + EncoderFloat(){ |
| 114 | + } |
| 115 | + |
| 116 | + // Constructor providing the output stream |
| 117 | + EncoderFloat(Print &out){ |
| 118 | + p_print = &out; |
| 119 | + } |
| 120 | + |
| 121 | + /// Defines the output Stream |
| 122 | + void setOutputStream(Print &out_stream) override { |
| 123 | + p_print = &out_stream; |
| 124 | + } |
| 125 | + |
| 126 | + /// Provides "audio/pcm" |
| 127 | + const char* mime() override{ |
| 128 | + return mime_pcm; |
| 129 | + } |
| 130 | + |
| 131 | + /// We actually do nothing with this |
| 132 | + virtual void setAudioInfo(AudioBaseInfo from) override { |
| 133 | + } |
| 134 | + |
| 135 | + /// starts the processing using the actual RAWAudioInfo |
| 136 | + virtual void begin() override{ |
| 137 | + is_open = true; |
| 138 | + } |
| 139 | + |
| 140 | + /// starts the processing |
| 141 | + void begin(Print &out) { |
| 142 | + p_print = &out; |
| 143 | + begin(); |
| 144 | + } |
| 145 | + |
| 146 | + /// stops the processing |
| 147 | + void end() override { |
| 148 | + is_open = false; |
| 149 | + } |
| 150 | + |
| 151 | + /// Converts data from int16_t to float |
| 152 | + virtual size_t write(const void *in_ptr, size_t in_size) override { |
| 153 | + if (p_print==nullptr) return 0; |
| 154 | + int16_t *pt16 = (int16_t*)in_ptr; |
| 155 | + size_t samples = in_size / sizeof(int16_t); |
| 156 | + buffer.resize(samples); |
| 157 | + for (int j=0;j<samples;j++){ |
| 158 | + buffer[j] = static_cast<float>(pt16[j]) / 32768.0; |
| 159 | + } |
| 160 | + return p_print->write((uint8_t*)buffer.data(), samples*sizeof(float)); |
| 161 | + } |
| 162 | + |
| 163 | + operator bool() override { |
| 164 | + return is_open; |
| 165 | + } |
| 166 | + |
| 167 | + bool isOpen(){ |
| 168 | + return is_open; |
| 169 | + } |
| 170 | + |
| 171 | + protected: |
| 172 | + Print* p_print=nullptr;; |
| 173 | + volatile bool is_open; |
| 174 | + Vector<float> buffer; |
| 175 | + |
| 176 | + |
| 177 | +}; |
| 178 | + |
| 179 | +} |
0 commit comments