20 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
28 switch (bits_per_second) {
30 return CODEC2_MODE_3200;
32 return CODEC2_MODE_2400;
34 return CODEC2_MODE_1600;
36 return CODEC2_MODE_1400;
38 return CODEC2_MODE_1300;
40 return CODEC2_MODE_1200;
42 return CODEC2_MODE_700C;
44 return CODEC2_MODE_450;
47 "Unsupported sample rate: use 3200, 2400, 1600, 1400, 1300, 1200, "
73 int bitsPerSecond() {
return bits_per_second; }
75 virtual bool begin() {
80 LOGE(
"invalid bits_per_second")
83 if (info.channels != 1) {
84 LOGE(
"Only 1 channel supported")
87 if (info.bits_per_sample != 16) {
88 LOGE(
"Only 16 bps are supported")
91 if (info.sample_rate != 8000) {
92 LOGW(
"Sample rate should be 8000: %d", info.
sample_rate);
95 p_codec2 = codec2_create(mode);
96 if (p_codec2 ==
nullptr) {
97 LOGE(
"codec2_create");
101 result_buffer.resize(bytesCompressed());
102 input_buffer.resize(bytesCompressed() );
104 assert(input_buffer.size()>0);
105 assert(result_buffer.size()>0);
107 notifyAudioChange(info);
108 LOGI(
"bytesCompressed:%d", bytesCompressed());
109 LOGI(
"bytesUncompressed:%d", bytesUncompressed());
114 int bytesCompressed() {
115 return p_codec2 !=
nullptr ? codec2_bytes_per_frame(p_codec2) : 0;
118 int bytesUncompressed() {
119 return p_codec2 !=
nullptr
120 ? codec2_samples_per_frame(p_codec2) *
sizeof(int16_t)
127 codec2_destroy(p_codec2);
133 operator bool() {
return is_active; }
135 size_t write(
const uint8_t *data,
size_t len)
override {
136 LOGD(
"write: %d", len);
142 uint8_t *p_byte = (uint8_t *)data;
143 for (
int j = 0; j < len; j++) {
151 Print *p_print =
nullptr;
152 struct CODEC2 *p_codec2;
153 bool is_active =
false;
154 Vector<uint8_t> input_buffer;
155 Vector<uint8_t> result_buffer;
157 int bits_per_second = 0;
162 input_buffer[input_pos++] = byte;
165 if (input_pos >= input_buffer.size()) {
166 codec2_decode(p_codec2, (
short*)result_buffer.data(), input_buffer.data());
167 int written = p_print->write((uint8_t *)result_buffer.data(), result_buffer.size());
168 if (written != result_buffer.size()){
169 LOGE(
"write: %d written: %d", result_buffer.size(), written);
171 LOGD(
"write: %d written: %d", result_buffer.size(), written);
190 info.sample_rate = 8000;
192 info.bits_per_sample = 16;
200 int bitsPerSecond() {
return bits_per_second; }
202 int bytesCompressed() {
203 return p_codec2 !=
nullptr ? codec2_bytes_per_frame(p_codec2) : 0;
206 int bytesUncompressed() {
207 return p_codec2 !=
nullptr
208 ? codec2_samples_per_frame(p_codec2) *
sizeof(int16_t)
217 LOGE(
"invalid bits_per_second")
220 if (info.channels != 1) {
221 LOGE(
"Only 1 channel supported")
224 if (info.bits_per_sample != 16) {
225 LOGE(
"Only 16 bps are supported")
228 if (info.sample_rate != 8000) {
229 LOGW(
"Sample rate should be 8000: %d", info.sample_rate);
232 p_codec2 = codec2_create(mode);
233 if (p_codec2 ==
nullptr) {
234 LOGE(
"codec2_create");
238 input_buffer.resize(bytesCompressed());
239 result_buffer.resize(bytesUncompressed());
240 assert(input_buffer.size()>0);
241 assert(result_buffer.size()>0);
242 LOGI(
"bytesCompressed:%d", bytesCompressed());
243 LOGI(
"bytesUncompressed:%d", bytesUncompressed());
250 codec2_destroy(p_codec2);
254 virtual const char *
mime() {
return "audio/codec2"; }
256 virtual void setOutput(
Print &out_stream) { p_print = &out_stream; }
258 operator bool() {
return is_active; }
260 size_t write(
const uint8_t *in_ptr,
size_t in_size)
override {
261 LOGD(
"write: %d", in_size);
267 uint8_t *p_byte = (uint8_t *)in_ptr;
268 for (
int j = 0; j < in_size; j++) {
275 Print *p_print =
nullptr;
276 struct CODEC2 *p_codec2 =
nullptr;
277 bool is_active =
false;
279 Vector<uint8_t> input_buffer;
280 Vector<uint8_t> result_buffer;
281 int bits_per_second = 0;
285 input_buffer[buffer_pos++] = byte;
286 if (buffer_pos >= input_buffer.size()) {
288 codec2_encode(p_codec2, result_buffer.data(),
289 (
short*)input_buffer.data());
290 int written = p_print->write(result_buffer.data(), result_buffer.size());
291 if(written!=result_buffer.size()){
292 LOGE(
"write: %d written: %d", result_buffer.size(), written);
294 LOGD(
"write: %d written: %d", result_buffer.size(), written);