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:
36 dec_routine = g723_24_decoder;
37 dec_bits = 3;
38 p_mime = "audio/g723_24";
39 break;
40
41 case g721:
42 dec_routine = g721_decoder;
43 dec_bits = 4;
44 p_mime = "audio/g721";
45 break;
46
47 case g723_40:
48 dec_routine = g723_40_decoder;
49 dec_bits = 5;
50 p_mime = "audio/g723_40";
51 break;
52 }
53 }
54
55 const char *mime() override { return p_mime; }
56
57 void setAudioInfo(AudioInfo info) override {
58 bool ok = true;
59 if (info.channels!=1){
60 LOGE("channels must be 1 instead of %d", info.channels);
61 ok = false;
62 }
63 if (info.sample_rate!=8000){
64 LOGE("sample_rate must be 8000 instead of %d", info.sample_rate);
65 ok = false;
66 }
67 if (info.bits_per_sample!=16){
68 LOGE("bits_per_sample must be 16 instead of %d", info.bits_per_sample);
69 ok = false;
70 }
72 }
73
74 bool begin() override {
75 TRACEI();
76 in_buffer = 0;
77 in_bits = 0;
78 out_size = sizeof(int16_t);
79 g72x_init_state(&state);
80
81 is_active = true;
82 return true;
83 }
84
85 void end() override {
86 TRACEI();
87 is_active = false;
88 }
89
90 void setOutput(Print &out_stream) override { p_print = &out_stream; }
91
92 operator bool() { return is_active; }
93
94 size_t write(const uint8_t *data, size_t len) override {
95 LOGD("write: %d", len);
96 if (!is_active) {
97 LOGE("inactive");
98 return 0;
99 }
100
101 uint8_t *p_byte = (uint8_t *)data;
102 for (int j = 0; j < len; j++) {
103 sample = (*dec_routine)(p_byte[j], AUDIO_ENCODING_LINEAR, &state);
104 p_print->write((uint8_t*)&sample, out_size);
105 }
106
107 return len;
108 }
109
110 protected:
111 Print *p_print = nullptr;
112 int input_pos = 0;
113 bool is_active = false;
114 int16_t sample;
115 unsigned char code;
116 int n;
117 struct g72x_state state;
119 int (*dec_routine)(int code, int out_coding, struct g72x_state* state_ptr);
121 unsigned int in_buffer = 0;
122 int in_bits = 0;
123 const char *p_mime = nullptr;
124
125};
126
134class G7xxEncoder : public AudioEncoder {
135 public:
137 info.channels = 1;
138 info.sample_rate = 8000;
140
141 switch(codec){
142
143 case g721:
144 enc_routine = g721_encoder;
145 enc_bits = 4;
146 p_mime = "audio/g721";
147 break;
148
149 case g723_24:
150 enc_routine = g723_24_encoder;
151 enc_bits = 3;
152 p_mime = "audio/g723_24";
153 break;
154
155 case g723_40:
156 enc_routine = g723_40_encoder;
157 enc_bits = 5;
158 p_mime = "audio/g723_40";
159 break;
160 }
161 }
162
163 bool begin() override {
164 TRACEI();
165 g72x_init_state(&state);
166 out_buffer = 0;
167 out_bits = 0;
168
169 is_active = true;
170 return true;
171 }
172
173 void end() override {
174 TRACEI();
175 is_active = false;
176 }
177
178 const char *mime() override { return p_mime; }
179
180 virtual void setAudioInfo(AudioInfo info) {
181 bool ok = true;
182 if (info.channels!=1){
183 LOGE("channels must be 1 instead of %d", info.channels);
184 ok = false;
185 }
186 if (info.sample_rate!=8000){
187 LOGE("sample_rate must be 8000 instead of %d", info.sample_rate);
188 ok = false;
189 }
190 if (info.bits_per_sample!=16){
191 LOGE("bits_per_sample must be 16 instead of %d", info.bits_per_sample);
192 ok = false;
193 }
195 }
196
197 void setOutput(Print &out_stream) override { p_print = &out_stream; }
198
199 operator bool() { return is_active; }
200
201 size_t write(const uint8_t *data, size_t len) override {
202 LOGD("write: %d", len);
203 if (!is_active) {
204 LOGE("inactive");
205 return 0;
206 }
207 // encode bytes
208 int16_t *p_16 = (int16_t *)data;
209 int samples = len / sizeof(int16_t);
210 for (int j = 0; j < samples; j++) {
211 code = (*enc_routine)(p_16[j], AUDIO_ENCODING_LINEAR, &state);
212 p_print->write(&code, 1);
213 }
214
215 return len;
216 }
217
218 protected:
219 Print *p_print = nullptr;
220 bool is_active = false;
221 const char *p_mime = nullptr;
222 int resid;
223 struct g72x_state state;
224 unsigned char sample_char;
226 unsigned char code;
227 int (*enc_routine)(int sample, int in_coding, struct g72x_state* state_ptr);
229 unsigned int out_buffer = 0;
230 int out_bits = 0;
231
232};
233
241class G721Decoder : public G7xxDecoder {
242 public:
244};
252class G721Encoder : public G7xxEncoder {
253 public:
255};
264 public:
266};
275 public:
277};
286 public:
288};
297 public:
299};
300
309class G711Encoder : public G7xxEncoder {
310 public:
311 G711Encoder(uint8_t(*enc)(int)) : G7xxEncoder(others) {
312 this->enc = enc;
313 assert(this->enc!=nullptr);
314 };
315 size_t write(const uint8_t *data, size_t len) override {
316 LOGD("write: %d", len);
317 if (!is_active) {
318 LOGE("inactive");
319 return 0;
320 }
321 // encode bytes
322 int samples = len/2;
323 int16_t *p_16 = (int16_t *)data;
324 uint8_t buffer[samples];
325 for (int j = 0; j < samples; j++) {
326 buffer[j] = enc(p_16[j]);
327 }
328 p_print->write(buffer,samples);
329 return len;
330 }
331 protected:
332 uint8_t(*enc)(int)=nullptr;
333};
334
343class G711Decoder : public G7xxDecoder {
344 public:
345 G711Decoder(int (*dec)(uint8_t a_val)) : G7xxDecoder(others) {
346 this->dec = dec;
347 assert(this->dec!=nullptr);
348 };
349
350 size_t write(const uint8_t *data, size_t len) override {
351 LOGD("write: %d", len);
352 if (!is_active) {
353 LOGE("inactive");
354 return 0;
355 }
356 // decode bytes
357 uint8_t *p_8 = (uint8_t *)data;
358 for (int j = 0; j < len; j++) {
359 int16_t result = dec(p_8[j]);
360 p_print->write((uint8_t*)&result,sizeof(int16_t));
361 }
362 return len;
363 }
364 protected:
365 int (*dec)(uint8_t a_val)=nullptr;
366};
367
368
377 public:
378 G711_ALAWEncoder() : G711Encoder(linear2alaw) {};
379};
380
389 public:
390 G711_ALAWDecoder() : G711Decoder(alaw2linear) {};
391};
392
401 public:
402 G711_ULAWEncoder() : G711Encoder(linear2ulaw) {};
403};
404
413 public:
414 G711_ULAWDecoder() : G711Decoder(ulaw2linear) {};
415};
416
417} // 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
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
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition AudioCodecsBase.h:32
Encoding of PCM data.
Definition AudioCodecsBase.h:101
AudioInfo info
Definition AudioCodecsBase.h:120
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:110
64 kbit/s g711 ALOW Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:388
G711_ALAWDecoder()
Definition CodecG7xx.h:390
64 kbit/s g711 ALOW Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:376
G711_ALAWEncoder()
Definition CodecG7xx.h:378
64 kbit/s g711 ULOW Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:412
G711_ULAWDecoder()
Definition CodecG7xx.h:414
64 kbit/s g711 ULOW Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:400
G711_ULAWEncoder()
Definition CodecG7xx.h:402
64 kbit/s g711 ULOW Decoder based on https://github.com/pschatzmann/arduino-libg7xx Supported decoder...
Definition CodecG7xx.h:343
G711Decoder(int(*dec)(uint8_t a_val))
Definition CodecG7xx.h:345
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:350
int(* dec)(uint8_t a_val)
Definition CodecG7xx.h:365
64 kbit/s g711 ULOW Encoder based on https://github.com/pschatzmann/arduino-libg7xx Supported encoder...
Definition CodecG7xx.h:309
uint8_t(* enc)(int)
Definition CodecG7xx.h:332
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:315
G711Encoder(uint8_t(*enc)(int))
Definition CodecG7xx.h:311
32Kbps G721 Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:241
G721Decoder()
Definition CodecG7xx.h:243
32Kbps G721 Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:252
G721Encoder()
Definition CodecG7xx.h:254
24Kbps G723 Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:263
G723_24Decoder()
Definition CodecG7xx.h:265
24Kbps G723 Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:274
G723_24Encoder()
Definition CodecG7xx.h:276
40Kbps G723 Decoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:285
G723_40Decoder()
Definition CodecG7xx.h:287
40Kbps G723 Encoder based on https://github.com/pschatzmann/arduino-libg7xx
Definition CodecG7xx.h:296
G723_40Encoder()
Definition CodecG7xx.h:298
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:90
struct g72x_state state
Definition CodecG7xx.h:117
int input_pos
Definition CodecG7xx.h:112
int(* dec_routine)(int code, int out_coding, struct g72x_state *state_ptr)
Definition CodecG7xx.h:119
unsigned char code
Definition CodecG7xx.h:115
const char * p_mime
Definition CodecG7xx.h:123
bool is_active
Definition CodecG7xx.h:113
int in_bits
Definition CodecG7xx.h:122
void end() override
Definition CodecG7xx.h:85
int out_size
Definition CodecG7xx.h:118
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:94
int n
Definition CodecG7xx.h:116
int16_t sample
Definition CodecG7xx.h:114
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecG7xx.h:55
unsigned int in_buffer
Definition CodecG7xx.h:121
bool begin() override
Definition CodecG7xx.h:74
Print * p_print
Definition CodecG7xx.h:111
void setAudioInfo(AudioInfo info) override
for most decoders this is not needed
Definition CodecG7xx.h:57
int dec_bits
Definition CodecG7xx.h:120
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:134
int enc_bits
Definition CodecG7xx.h:228
void setOutput(Print &out_stream) override
Default output assignment (encoders may override to store Print reference)
Definition CodecG7xx.h:197
struct g72x_state state
Definition CodecG7xx.h:223
int resid
Definition CodecG7xx.h:222
unsigned char sample_char
Definition CodecG7xx.h:224
unsigned char code
Definition CodecG7xx.h:226
const char * p_mime
Definition CodecG7xx.h:221
unsigned int out_buffer
Definition CodecG7xx.h:229
bool is_active
Definition CodecG7xx.h:220
void end() override
Definition CodecG7xx.h:173
size_t write(const uint8_t *data, size_t len) override
Definition CodecG7xx.h:201
int16_t sample_int16
Definition CodecG7xx.h:225
virtual void setAudioInfo(AudioInfo info)
Defines the sample rate, number of channels and bits per sample.
Definition CodecG7xx.h:180
int(* enc_routine)(int sample, int in_coding, struct g72x_state *state_ptr)
Definition CodecG7xx.h:227
const char * mime() override
Provides the mime type of the encoded result.
Definition CodecG7xx.h:178
int out_bits
Definition CodecG7xx.h:230
bool begin() override
Definition CodecG7xx.h:163
Print * p_print
Definition CodecG7xx.h:219
G7xxEncoder(G7xxCODEC_e codec)
Definition CodecG7xx.h:136
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
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
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57