arduino-audio-tools
Loading...
Searching...
No Matches
CodecL8.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace audio_tools {
6
19class DecoderL8 : public AudioDecoder {
20 public:
25 DecoderL8(bool isSigned = false) {
26 TRACED();
27 setSigned(isSigned);
28 }
29
35 DecoderL8(Print &out_stream, bool active = true) {
36 TRACED();
37 p_print = &out_stream;
38 }
39
47 DecoderL8(Print &out_stream, AudioInfoSupport &bi) {
48 TRACED();
49 setOutput(out_stream);
51 }
52
53 const char *mime() override { return "audio/l8"; }
54
57 void setSigned(bool isSigned) { is_signed = isSigned; }
58
60 virtual void setAudioInfo(AudioInfo from) override {
61 TRACED();
62 if (from.bits_per_sample!=16){
63 LOGE("Bits per sample not supported: %d", from.bits_per_sample);
64 }
65 from.bits_per_sample = 16;
66 if (info != from) {
68 }
69 info = from;
70 }
71
72 virtual size_t write(const uint8_t *data, size_t len) override {
73 if (p_print == nullptr) return 0;
74 buffer.resize(len);
75 memset(buffer.data(), 0, len * 2);
76 if (is_signed) {
77 int8_t *pt8 = (int8_t *)data;
78 for (size_t j = 0; j < len; j++) {
79 buffer[j] = convertSample(pt8[j]);
80 }
81 } else {
82 uint8_t *pt8 = (uint8_t *)data;
83 for (size_t j = 0; j < len; j++) {
84 buffer[j] = convertSample(pt8[j]);
85 }
86 }
87 int write_byte_count = len * sizeof(int16_t);
88 size_t result = p_print->write((uint8_t *)buffer.data(), write_byte_count);
89 LOGD("DecoderL8 %d -> %d -> %d", (int)len, write_byte_count, (int)result);
90 return result / sizeof(int16_t);
91 }
92
93 int16_t convertSample(int16_t in) {
94 int32_t tmp = in;
95 if (!is_signed) {
96 tmp -= 129;
97 }
98 return NumberConverter::clipT<int16_t>(tmp * 258);
99 }
100
101 virtual operator bool() override { return p_print!=nullptr; }
102
103
104 protected:
105 bool is_signed = false;
107};
108
122class EncoderL8 : public AudioEncoder {
123 public:
124 // Empty Constructor - the output stream must be provided with begin()
125 EncoderL8(bool isSigned = false) {
126 TRACED();
127 setSigned(isSigned);
128 }
129
130 // Constructor providing the output stream
131 EncoderL8(Print &out) { p_print = &out; }
132
134 void setSigned(bool isSigned) { is_signed = isSigned; }
135
137 void setOutput(Print &out_stream) override { p_print = &out_stream; }
138
140 const char *mime() override { return "audio/l8"; }
141
143 bool begin() override { is_open = true; return true;}
144
146 bool begin(Print &out) {
147 p_print = &out;
148 return begin();
149 }
150
152 void end() override { is_open = false; }
153
155 size_t write(const uint8_t *data, size_t len) override {
156 if (p_print == nullptr) return 0;
157 int16_t *pt16 = (int16_t *)data;
158 size_t samples = len / 2;
159 buffer.resize(samples);
160 memset(buffer.data(), 0, samples);
161 for (size_t j = 0; j < samples; j++) {
162 buffer[j] = convertSample(pt16[j]);
163 }
164
165 size_t result = p_print->write((uint8_t *)buffer.data(), samples);
166 LOGD("EncoderL8 %d -> %d -> %d", (int)len,(int) samples, (int)result);
167 return result * sizeof(int16_t);
168 }
169
170 operator bool() override { return is_open; }
171
172 int16_t convertSample(int16_t sample) {
173 int16_t tmp = NumberConverter::clipT<int8_t>(sample / 258);
174 if (!is_signed) {
175 tmp += 129;
176 // clip to range
177 if (tmp < 0) {
178 tmp = 0;
179 } else if (tmp > 255) {
180 tmp = 255;
181 }
182 }
183 return tmp;
184 }
185
186 bool isOpen() { return is_open; }
187
188 protected:
189 Print *p_print = nullptr;
191 bool is_signed = false;
193};
194
195} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
AudioInfo info
Definition AudioCodecsBase.h:80
virtual void setOutput(AudioStream &out_stream)
Defines where the decoded result is written to.
Definition AudioCodecsBase.h:40
Print * p_print
Definition AudioCodecsBase.h:79
Encoding of PCM data.
Definition AudioCodecsBase.h:101
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:175
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:150
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:132
DecoderL8 - Converts an 8 Bit Stream into 16Bits Most microcontrollers can not output 8 bit data dire...
Definition CodecL8.h:19
virtual size_t write(const uint8_t *data, size_t len) override
Definition CodecL8.h:72
void setSigned(bool isSigned)
Definition CodecL8.h:57
bool is_signed
Definition CodecL8.h:105
DecoderL8(Print &out_stream, AudioInfoSupport &bi)
Construct a new DecoderL8 object.
Definition CodecL8.h:47
int16_t convertSample(int16_t in)
Definition CodecL8.h:93
Vector< int16_t > buffer
Definition CodecL8.h:106
DecoderL8(Print &out_stream, bool active=true)
Construct a new DecoderL8 object.
Definition CodecL8.h:35
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecL8.h:53
virtual void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition CodecL8.h:60
DecoderL8(bool isSigned=false)
Construct a new DecoderL8 object.
Definition CodecL8.h:25
EncoderL8s - Condenses 16 bit PCM data stream to 8 bits data. Most microcontrollers can not process 8...
Definition CodecL8.h:122
bool is_open
Definition CodecL8.h:190
void setSigned(bool isSigned)
By default the encoded values are unsigned, but can change them to signed.
Definition CodecL8.h:134
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecL8.h:137
int16_t convertSample(int16_t sample)
Definition CodecL8.h:172
bool is_signed
Definition CodecL8.h:191
EncoderL8(Print &out)
Definition CodecL8.h:131
bool isOpen()
Definition CodecL8.h:186
void end() override
stops the processing
Definition CodecL8.h:152
EncoderL8(bool isSigned=false)
Definition CodecL8.h:125
bool begin(Print &out)
starts the processing
Definition CodecL8.h:146
size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as RAW.
Definition CodecL8.h:155
const char * mime() override
Provides "audio/pcm".
Definition CodecL8.h:140
Vector< int8_t > buffer
Definition CodecL8.h:192
bool begin() override
starts the processing using the actual RAWAudioInfo
Definition CodecL8.h:143
Print * p_print
Definition CodecL8.h:189
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57