11 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
12 #include "g722_codec.h"
15 #define G722_PCM_SIZE 80
16 #define G722_ENC_SIZE 40
35 this->options = options;
38 virtual bool begin() {
40 input_buffer.resize(10);
41 result_buffer.resize(40);
43 g722_dctx = g722_decoder_new(info.
sample_rate, options);
44 if (g722_dctx ==
nullptr) {
45 LOGE(
"g722_decoder_new");
49 notifyAudioChange(info);
56 g722_decoder_destroy(g722_dctx);
62 operator bool() {
return is_active; }
64 virtual size_t write(
const uint8_t *data,
size_t len) {
65 LOGD(
"write: %d", len);
71 uint8_t *p_byte = (uint8_t *)data;
72 for (
int j = 0; j < len; j++) {
80 Print *p_print =
nullptr;
81 G722_DEC_CTX *g722_dctx=
nullptr;
82 Vector<uint8_t> input_buffer;
83 Vector<uint8_t> result_buffer;
84 int options = G722_SAMPLE_RATE_8000;
86 bool is_active =
false;
91 input_buffer[input_pos++] = byte;
94 if (input_pos >= input_buffer.size()) {
95 int result_samples = g722_decode(g722_dctx, input_buffer.data(), input_buffer.size(),
96 (int16_t *)result_buffer.data());
98 if (result_samples*2>result_buffer.size()){
99 LOGE(
"Decoder:Result buffer too small: %d -> %d",result_buffer.size(),result_samples*2);
102 p_print->write(result_buffer.data(), result_samples);
123 this->options = options;
129 LOGW(
"1 channel expected, was: %d", info.
channels);
132 g722_ectx = g722_encoder_new(info.
sample_rate, options);
133 if (g722_ectx == NULL) {
134 LOGE(
"g722_encoder_new");
138 input_buffer.resize(G722_PCM_SIZE);
139 result_buffer.resize(G722_ENC_SIZE);
146 g722_encoder_destroy(g722_ectx);
150 virtual const char *
mime() {
return "audio/g722"; }
152 virtual void setOutput(
Print &out_stream) { p_print = &out_stream; }
154 operator bool() {
return is_active; }
156 virtual size_t write(
const uint8_t *data,
size_t len) {
157 LOGD(
"write: %d", len);
163 uint8_t *p_byte = (uint8_t *)data;
164 for (
int j = 0; j < len; j++) {
165 processByte(p_byte[j]);
171 Print *p_print =
nullptr;
172 G722_ENC_CTX *g722_ectx =
nullptr;
173 Vector<uint8_t> input_buffer;
174 Vector<uint8_t> result_buffer;
175 int options = G722_SAMPLE_RATE_8000;
177 bool is_active =
false;
180 void processByte(uint8_t
byte) {
181 input_buffer[buffer_pos++] = byte;
182 if (buffer_pos >= input_buffer.size()) {
184 int samples = input_buffer.size() / 2;
186 int result_len = g722_encode(g722_ectx,(
const int16_t*) input_buffer.data(), samples,
187 result_buffer.data());
188 if (result_len>result_buffer.size()){
189 LOGE(
"Encoder:Result buffer too small: %d -> %d",result_buffer.size(),result_len);
191 p_print->write(result_buffer.data(), result_len);