Skip to content

Commit 81767b1

Browse files
committed
lc3: add single rate init and deinit functions
- added sw_codec_lc3_single_rate_init() function that enables only one requested sample rate for encoder and decoder, which decreases RAM usage and time to initialize codec compared to sw_codec_lc3_init() which enables all supported sample rates; - added sw_codec_lc3_uninit() function to deinit LC3 completely. This change allows changing a codec usage flow where we initialize it with only one sample rate, and de-initialize the codec when audio stream has stopped; Signed-off-by: Ivan Iushkov <ivan.iushkov@nordicsemi.no>
1 parent f155b82 commit 81767b1

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

lc3/include/sw_codec_lc3.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,38 @@ int sw_codec_lc3_enc_uninit_all(void);
110110
*/
111111
int sw_codec_lc3_dec_uninit_all(void);
112112

113+
/**@brief Uninitializes the LC3 Codec and frees allocated RAM
114+
*
115+
* @return 0 on success. -EIO in case of error.
116+
*/
117+
int sw_codec_lc3_uninit(void);
118+
113119
/**@brief Initializes the LC3 Codec
114120
*
115121
* @note: For documentation, see LC3API.h (/codec/inc/LC3API.h)
116122
*/
117123
int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
118124
uint16_t framesize_us);
119125

126+
/**@brief Initializes the LC3 Codec with a single sample rate for encoder and decoder
127+
*
128+
* @details This function initializes the LC3 codec enabling only the requested sample rate
129+
* for encoder and decoder, which decreases RAM usage and initialization time
130+
* compared to sw_codec_lc3_init() which enables all supported sample rates.
131+
*
132+
* @param[in] encoder_sample_rate Encoder sample rate in Hz
133+
* @param[in] decoder_sample_rate Decoder sample rate in Hz
134+
* @param[in] sw_codec_lc3_buffer Pointer to a memory buffer
135+
* @param[in,out] sw_codec_lc3_buffer_size Size of the memory buffer passed in, number of bytes
136+
* used returned
137+
* @param[in] framesize_us Frame size in microseconds
138+
*
139+
*/
140+
int sw_codec_lc3_single_rate_init(uint16_t encoder_sample_rate, uint16_t decoder_sample_rate,
141+
uint8_t *sw_codec_lc3_buffer,
142+
uint32_t *sw_codec_lc3_buffer_size,
143+
uint16_t framesize_us);
144+
120145
/**@brief Initializes the LC3 encoder and allocates required RAM
121146
*
122147
* @param[in] pcm_sample_rate Sample rate in Hz (typ. 16000 or 48000)

lc3/src/sw_codec_lc3.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ int sw_codec_lc3_dec_uninit_all(void)
187187
return 0;
188188
}
189189

190+
int sw_codec_lc3_uninit(void)
191+
{
192+
int ret;
193+
ret = LC3Deinitialize();
194+
if (ret) {
195+
LOG_ERR("Failed to uninitialize LC3 Codec: %d", ret);
196+
return -EIO;
197+
}
198+
return 0;
199+
}
200+
190201
int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
191202
uint16_t framesize_us)
192203
{
@@ -260,6 +271,93 @@ int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffe
260271
return ret;
261272
}
262273

274+
static uint8_t enc_sample_rate_hz_to_bit(uint16_t sample_rate)
275+
{
276+
switch (sample_rate) {
277+
case 8000:
278+
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_8KHZ_SUPPORT) ? LC3_SAMPLE_RATE_8_KHZ : 0;
279+
case 16000:
280+
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_16KHZ_SUPPORT) ? LC3_SAMPLE_RATE_16_KHZ : 0;
281+
case 24000:
282+
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_24KHZ_SUPPORT) ? LC3_SAMPLE_RATE_24_KHZ : 0;
283+
case 32000:
284+
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_32KHZ_SUPPORT) ? LC3_SAMPLE_RATE_32_KHZ : 0;
285+
case 44100:
286+
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_441KHZ_SUPPORT) ? LC3_SAMPLE_RATE_441_KHZ : 0;
287+
case 48000:
288+
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_48KHZ_SUPPORT) ? LC3_SAMPLE_RATE_48_KHZ : 0;
289+
default:
290+
return 0;
291+
}
292+
}
293+
294+
static uint8_t dec_sample_rate_hz_to_bit(uint16_t sample_rate)
295+
{
296+
switch (sample_rate) {
297+
case 8000:
298+
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_8KHZ_SUPPORT) ? LC3_SAMPLE_RATE_8_KHZ : 0;
299+
case 16000:
300+
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_16KHZ_SUPPORT) ? LC3_SAMPLE_RATE_16_KHZ : 0;
301+
case 24000:
302+
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_24KHZ_SUPPORT) ? LC3_SAMPLE_RATE_24_KHZ : 0;
303+
case 32000:
304+
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_32KHZ_SUPPORT) ? LC3_SAMPLE_RATE_32_KHZ : 0;
305+
case 44100:
306+
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_441KHZ_SUPPORT) ? LC3_SAMPLE_RATE_441_KHZ : 0;
307+
case 48000:
308+
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_48KHZ_SUPPORT) ? LC3_SAMPLE_RATE_48_KHZ : 0;
309+
default:
310+
return 0;
311+
}
312+
}
313+
314+
int sw_codec_lc3_single_rate_init(uint16_t encoder_sample_rate, uint16_t decoder_sample_rate,
315+
uint8_t *sw_codec_lc3_buffer,
316+
uint32_t *sw_codec_lc3_buffer_size,
317+
uint16_t framesize_us)
318+
{
319+
int ret;
320+
uint8_t enc_sample_rate_bit;
321+
uint8_t dec_sample_rate_bit;
322+
323+
/* Set unique session to 0 for using the default sharing memory setting.
324+
*
325+
* This could lead to higher heap consumption, but is able to manipulate
326+
* different sample rate setting between encoder/decoder.
327+
*/
328+
uint8_t unique_session = 0;
329+
330+
enc_sample_rate_bit = enc_sample_rate_hz_to_bit(encoder_sample_rate);
331+
if (enc_sample_rate_bit == 0 && encoder_sample_rate != 0) {
332+
LOG_ERR("Unsupported encoder sample rate: %d", encoder_sample_rate);
333+
return -EINVAL;
334+
}
335+
336+
dec_sample_rate_bit = dec_sample_rate_hz_to_bit(decoder_sample_rate);
337+
if (dec_sample_rate_bit == 0 && decoder_sample_rate != 0) {
338+
LOG_ERR("Unsupported decoder sample rate: %d", decoder_sample_rate);
339+
return -EINVAL;
340+
}
341+
342+
LC3FrameSizeConfig_t framesize;
343+
344+
switch (framesize_us) {
345+
case 7500:
346+
framesize = LC3FrameSize7_5MsConfig;
347+
break;
348+
case 10000:
349+
framesize = LC3FrameSize10MsConfig;
350+
break;
351+
default:
352+
LOG_ERR("Unsupported framesize: %d", framesize_us);
353+
return -EINVAL;
354+
}
355+
356+
ret = LC3Initialize(enc_sample_rate_bit, dec_sample_rate_bit, framesize, unique_session,
357+
sw_codec_lc3_buffer, sw_codec_lc3_buffer_size);
358+
return ret;
359+
}
360+
263361
int sw_codec_lc3_enc_init(uint16_t pcm_sample_rate, uint8_t pcm_bit_depth, uint16_t framesize_us,
264362
uint32_t enc_bitrate, uint8_t num_channels, uint16_t *const pcm_bytes_req)
265363
{

0 commit comments

Comments
 (0)