arduino-audio-tools
Loading...
Searching...
No Matches
CodecG7xx.h
Go to the documentation of this file.
1#pragma once
3
4extern "C"{
5 #include "g72x.h"
6}
7
8
9namespace audio_tools {
10
18
27class G7xxDecoder : public AudioDecoder {
28 public:
30 info.channels = 1;
31 info.sample_rate = 8000;
33
34 switch(codec){
35 case g723_24:
37 dec_bits = 3;
38 break;
39
40 case g721:
42 dec_bits = 4;
43 break;
44
45 case g723_40:
47 dec_bits = 5;
48 break;
49 }
50 }
51
52 void setAudioInfo(AudioInfo info) override {
53 bool ok = true;
54 if (info.channels!=1){
55 LOGE("channels must be 1 instead of %d", info.channels);
56 ok = false;
57 }
58 if (info.sample_rate!=8000){
59 LOGE("sample_rate must be 8000 instead of %d", info.sample_rate);
60 ok = false;
61 }
62 if (info.bits_per_sample!=16){
63 LOGE("bits_per_sample must be 16 instead of %d", info.bits_per_sample);
64 ok = false;
65 }
67 }
68
69 bool begin() override {
70 TRACEI();
71 in_buffer = 0;
72 in_bits = 0;
73 out_size = sizeof(int16_t);
75
76 is_active = true;
77 return true;
78 }
79
80 void end() override {
81 TRACEI();
82 is_active = false;
83 }
84
86
87 operator bool() { return is_active; }
88
89 size_t write(const uint8_t *data, size_t len) override {
90 LOGD("write: %d", len);
91 if (!is_active) {
92 LOGE("inactive");
93 return 0;
94 }
95
96 uint8_t *p_byte = (uint8_t *)data;
97 for (int j = 0; j < len; j++) {
98 sample = (*dec_routine)(p_byte[j], AUDIO_ENCODING_LINEAR, &state);
100 }
101
102 return len;
103 }
104
105 protected:
106 Print *p_print = nullptr;
107 int input_pos = 0;
108 bool is_active = false;
110 unsigned char code;
111 int n;
116 unsigned int in_buffer = 0;
117 int in_bits = 0;
118
119};
120
128class G7xxEncoder : public AudioEncoder {
129 public:
131 info.channels = 1;
132 info.sample_rate = 8000;
134
135 switch(codec){
136
137 case g721:
139 enc_bits = 4;
140 p_mime = "audio/g721";
141 break;
142
143 case g723_24:
145 enc_bits = 3;
146 p_mime = "audio/g723_24";
147 break;
148
149 case g723_40:
151 enc_bits = 5;
152 p_mime = "audio/g723_40";
153 break;
154 }
155 }
156
157 bool begin() override {
158 TRACEI();
160 out_buffer = 0;
161 out_bits = 0;
162
163 is_active = true;
164 return true;
165 }
166
167 void end() override {
168 TRACEI();
169 is_active = false;
170 }
171
172 const char *mime() override { return p_mime; }
173
174 virtual void setAudioInfo(AudioInfo info) {
175 bool ok = true;
176 if (info.channels!=1){
177 LOGE("channels must be 1 instead of %d", info.channels);
178 ok = false;
179 }
180 if (info.sample_rate!=8000){
181 LOGE("sample_rate must be 8000 instead of %d", info.sample_rate);
182 ok = false;
183 }
184 if (info.bits_per_sample!=16){
185 LOGE("bits_per_sample must be 16 instead of %d", info.bits_per_sample);
186 ok = false;
187 }
189 }
190
192
193 operator bool() { return is_active; }
194
195 size_t write(const uint8_t *data, size_t len) override {
196 LOGD("write: %d", len);
197 if (!is_active) {
198 LOGE("inactive");
199 return 0;
200 }
201 // encode bytes
202 int16_t *p_16 = (int16_t *)data;
203 int samples = len / sizeof(int16_t);
204 for (int j = 0; j < samples; j++) {
205 code = (*enc_routine)(p_16[j], AUDIO_ENCODING_LINEAR, &state);
206 p_print->write(&code, 1);
207 }
208
209 return len;
210 }
211
212 protected:
213 Print *p_print = nullptr;
214 bool is_active = false;
215 const char *p_mime = nullptr;
216 int resid;
218 unsigned char sample_char;
220 unsigned char code;
221 int (*enc_routine)(int sample, int in_coding, struct g72x_state* state_ptr);
223 unsigned int out_buffer = 0;
224 int out_bits = 0;
225
226};
227
235class G721Decoder : public G7xxDecoder {
236 public:
238};
246class G721Encoder : public G7xxEncoder {
247 public:
249};
258 public:
260};
269 public:
271};
280 public:
282};
291 public:
293};
294
303class G711Encoder : public G7xxEncoder {
304 public:
306 this->enc = enc;
307 assert(this->enc!=nullptr);
308 };
309 size_t write(const uint8_t *data, size_t len) override {
310 LOGD("write: %d", len);
311 if (!is_active) {
312 LOGE("inactive");
313 return 0;
314 }
315 // encode bytes
316 int samples = len/2;
317 int16_t *p_16 = (int16_t *)data;
318 uint8_t buffer[samples];
319 for (int j = 0; j < samples; j++) {
320 buffer[j] = enc(p_16[j]);
321 }
322 p_print->write(buffer,samples);
323 return len;
324 }
325 protected:
326 uint8_t(*enc)(int)=nullptr;
327};
328
337class G711Decoder : public G7xxDecoder {
338 public:
340 this->dec = dec;
341 assert(this->dec!=nullptr);
342 };
343
344 size_t write(const uint8_t *data, size_t len) override {
345 LOGD("write: %d", len);
346 if (!is_active) {
347 LOGE("inactive");
348 return 0;
349 }
350 // decode bytes
351 uint8_t *p_8 = (uint8_t *)data;
352 for (int j = 0; j < len; j++) {
353 int16_t result = dec(p_8[j]);
354 p_print->write((uint8_t*)&result,sizeof(int16_t));
355 }
356 return len;
357 }
358 protected:
359 int (*dec)(uint8_t a_val)=nullptr;
360};
361
362
371 public:
373};
374
383 public:
385};
386
395 public:
397};
398
407 public:
409};
410
411} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define assert(T)
Definition avr.h:10
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
AudioInfo info
Definition AudioCodecsBase.h:76
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition AudioCodecsBase.h:28
Encoding of PCM data.
Definition AudioCodecsBase.h:97
AudioInfo info
Definition AudioCodecsBase.h:116
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:106
64 kbit/s g711 ALOW Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:382
G711_ALAWDecoder()
Definition CodecG7xx.h:384
64 kbit/s g711 ALOW Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:370
G711_ALAWEncoder()
Definition CodecG7xx.h:372
64 kbit/s g711 ULOW Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:406
G711_ULAWDecoder()
Definition CodecG7xx.h:408
64 kbit/s g711 ULOW Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:394
G711_ULAWEncoder()
Definition CodecG7xx.h:396
64 kbit/s g711 ULOW Decoder based on https://github.com/pschatzmann/arduino-libg7xx Supported decoder...
Definition CodecG7xx.h:337
G711Decoder(int(*dec)(uint8_t a_val))
Definition CodecG7xx.h:339
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:344
int(* dec)(uint8_t a_val)
Definition CodecG7xx.h:359
64 kbit/s g711 ULOW Encoder based on https://github.com/pschatzmann/arduino-libg7xx Supported encoder...
Definition CodecG7xx.h:303
uint8_t(* enc)(int)
Definition CodecG7xx.h:326
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:309
G711Encoder(uint8_t(*enc)(int))
Definition CodecG7xx.h:305
32Kbps G721 Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:235
G721Decoder()
Definition CodecG7xx.h:237
32Kbps G721 Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:246
G721Encoder()
Definition CodecG7xx.h:248
24Kbps G723 Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:257
G723_24Decoder()
Definition CodecG7xx.h:259
24Kbps G723 Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:268
G723_24Encoder()
Definition CodecG7xx.h:270
40Kbps G723 Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:279
G723_40Decoder()
Definition CodecG7xx.h:281
40Kbps G723 Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:290
G723_40Encoder()
Definition CodecG7xx.h:292
g723_24, g721, g723_40 Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:27
void setOutput(Print &out_stream) override
Defines where the decoded result is written to.
Definition CodecG7xx.h:85
struct g72x_state state
Definition CodecG7xx.h:112
int input_pos
Definition CodecG7xx.h:107
int(* dec_routine)(int code, int out_coding, struct g72x_state *state_ptr)
Definition CodecG7xx.h:114
unsigned char code
Definition CodecG7xx.h:110
bool is_active
Definition CodecG7xx.h:108
int in_bits
Definition CodecG7xx.h:117
void end() override
Definition CodecG7xx.h:80
int out_size
Definition CodecG7xx.h:113
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:89
int n
Definition CodecG7xx.h:111
int16_t sample
Definition CodecG7xx.h:109
unsigned int in_buffer
Definition CodecG7xx.h:116
bool begin() override
Definition CodecG7xx.h:69
Print * p_print
Definition CodecG7xx.h:106
void setAudioInfo(AudioInfo info) override
for most decoders this is not needed
Definition CodecG7xx.h:52
int dec_bits
Definition CodecG7xx.h:115
G7xxDecoder(G7xxCODEC_e codec)
Definition CodecG7xx.h:29
g723_24, g721, g723_40 Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:128
int enc_bits
Definition CodecG7xx.h:222
void setOutput(Print &out_stream) override
Default output assignment (encoders may override to store Print reference)
Definition CodecG7xx.h:191
struct g72x_state state
Definition CodecG7xx.h:217
int resid
Definition CodecG7xx.h:216
unsigned char sample_char
Definition CodecG7xx.h:218
unsigned char code
Definition CodecG7xx.h:220
const char * p_mime
Definition CodecG7xx.h:215
unsigned int out_buffer
Definition CodecG7xx.h:223
bool is_active
Definition CodecG7xx.h:214
void end() override
Definition CodecG7xx.h:167
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:195
int16_t sample_int16
Definition CodecG7xx.h:219
virtual void setAudioInfo(AudioInfo info)
Defines the sample rate, number of channels and bits per sample.
Definition CodecG7xx.h:174
int(* enc_routine)(int sample, int in_coding, struct g72x_state *state_ptr)
Definition CodecG7xx.h:221
const char * mime() override
Provides the mime type of the encoded result.
Definition CodecG7xx.h:172
int out_bits
Definition CodecG7xx.h:224
bool begin() override
Definition CodecG7xx.h:157
Print * p_print
Definition CodecG7xx.h:213
G7xxEncoder(G7xxCODEC_e codec)
Definition CodecG7xx.h:130
Definition NoArduino.h:62
virtual size_t write(const uint8_t *data, size_t len)
Definition NoArduino.h:126
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
G7xxCODEC_e
Supported codecs by G7xxDecoder and G7xxEncoder.
Definition CodecG7xx.h:17
@ g723_24
Definition CodecG7xx.h:17
@ g723_40
Definition CodecG7xx.h:17
@ others
Definition CodecG7xx.h:17
@ g721
Definition CodecG7xx.h:17
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61