13 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
19 #define DEFAULT_BYTE_COUNT 40
21 #define LC3_DEFAULT_DT_US 7500
34 uint16_t inputByteCount = DEFAULT_BYTE_COUNT) {
37 this->input_byte_count = inputByteCount;
41 uint16_t inputByteCount = DEFAULT_BYTE_COUNT) {
43 this->input_byte_count = inputByteCount;
49 virtual bool begin() {
53 num_frames = lc3_frame_samples(dt_us, info.
sample_rate);
54 dec_size = lc3_decoder_size(dt_us, info.
sample_rate);
58 LOGI(
"input_byte_count: %d", input_byte_count);
59 LOGI(
"dt_us: %d", dt_us);
60 LOGI(
"num_frames: %d", num_frames);
61 LOGI(
"dec_size: %d", dec_size);
64 LOGE(
"Invalid Parameters");
69 input_buffer.resize(input_byte_count);
70 output_buffer.resize(num_frames * 2);
71 lc3_decoder_memory.resize(dec_size);
74 lc3_decoder = lc3_setup_decoder(dt_us, info.
sample_rate, 0,
75 (
void *)lc3_decoder_memory.data());
76 notifyAudioChange(info);
90 operator bool() {
return active; }
92 virtual size_t write(
const uint8_t *data,
size_t len) {
93 if (!active)
return 0;
94 LOGD(
"write %u", len);
96 uint8_t *p_ptr8 = (uint8_t *)data;
98 for (
int j = 0; j < len; j++) {
99 input_buffer[input_pos++] = p_ptr8[j];
100 if (input_pos >= input_buffer.size()) {
101 if (lc3_decode(lc3_decoder, input_buffer.data(), input_buffer.size(),
102 pcm_format, (int16_t *)output_buffer.data(), 1) != 0) {
107 int requested = output_buffer.size();
109 p_print->write((
const uint8_t *)output_buffer.data(), requested);
110 if (written != requested) {
111 LOGE(
"Decoder Bytes requested: %d - written: %d", requested, written);
120 Print *p_print =
nullptr;
121 lc3_decoder_t lc3_decoder =
nullptr;
122 lc3_pcm_format pcm_format;
123 Vector<uint8_t> lc3_decoder_memory;
124 Vector<uint16_t> output_buffer;
125 Vector<uint8_t> input_buffer;
126 size_t input_pos = 0;
128 uint16_t input_byte_count = 20;
134 if (p_print ==
nullptr) {
135 LOGE(
"Output is not defined");
139 if (!LC3_CHECK_DT_US(dt_us)) {
140 LOGE(
"dt_us: %d", dt_us);
150 LOGE(
"channels: %d", info.
channels);
153 if (num_frames == -1) {
154 LOGE(
"num_frames could not be determined - using m");
165 pcm_format = LC3_PCM_FORMAT_S16;
168 pcm_format = LC3_PCM_FORMAT_S24;
189 uint16_t outputByteCount = DEFAULT_BYTE_COUNT) {
194 output_byte_count = outputByteCount;
200 unsigned enc_size = lc3_encoder_size(dt_us, info.
sample_rate);
201 num_frames = lc3_frame_samples(dt_us, info.
sample_rate);
204 LOGI(
"channels: %d", info.
channels);
205 LOGI(
"dt_us: %d", dt_us);
206 LOGI(
"output_byte_count: %d", output_byte_count);
207 LOGI(
"enc_size: %d", enc_size);
208 LOGI(
"num_frames: %d", num_frames);
210 if (!checkValues()) {
215 lc3_encoder_memory.resize(enc_size);
216 input_buffer.resize(num_frames * 2);
217 output_buffer.resize(output_byte_count);
220 lc3_encoder = lc3_setup_encoder(dt_us, info.
sample_rate, 0,
221 lc3_encoder_memory.data());
233 virtual const char *
mime() {
return "audio/lc3"; }
235 virtual void setOutput(
Print &out_stream) { p_print = &out_stream; }
237 operator bool() {
return lc3_encoder !=
nullptr; }
239 virtual size_t write(
const uint8_t *data,
size_t len) {
240 if (!active)
return 0;
241 LOGD(
"write %u", len);
242 uint8_t *p_ptr8 = (uint8_t *)data;
244 for (
int j = 0; j < len; j++) {
245 input_buffer[input_pos++] = p_ptr8[j];
246 if (input_pos >= num_frames * 2) {
247 if (lc3_encode(lc3_encoder, pcm_format,
248 (
const int16_t *)input_buffer.data(), 1,
249 output_buffer.size(), output_buffer.data()) != 0) {
254 int requested = output_buffer.size();
255 int written = p_print->write(output_buffer.data(), requested);
256 if (written != requested) {
257 LOGE(
"Encoder Bytes requested: %d - written: %d", requested, written);
266 Print *p_print =
nullptr;
267 unsigned dt_us = 1000;
269 lc3_encoder_t lc3_encoder =
nullptr;
270 lc3_pcm_format pcm_format;
271 uint16_t output_byte_count = 20;
272 Vector<uint8_t> lc3_encoder_memory;
273 Vector<uint8_t> output_buffer;
274 Vector<uint8_t> input_buffer;
279 if (p_print ==
nullptr) {
280 LOGE(
"Output is not defined");
284 if (!LC3_CHECK_DT_US(dt_us)) {
285 LOGE(
"dt_us: %d", dt_us);
295 LOGE(
"channels: %d", info.
channels);
298 if (num_frames == -1) {
299 LOGE(
"Invalid num_frames");
305 pcm_format = LC3_PCM_FORMAT_S16;
308 pcm_format = LC3_PCM_FORMAT_S24;