Skip to content

Commit 92611e0

Browse files
committed
lc3: refactor codec init/deinit functions
- added encoder and decoder sample rate to sw_codec_lc3_init(). The function used to enable all the supported sample rates, but it adds redundant overhead to LC3 run-time, so the function changed to enable only one requested sample rate for both encoder and decoder; - 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; - changed type of `framesize` from `LC3FrameSize_t` to `LC3FrameSizeConfig_t` to avoid implicit enum conversion when passing it to `LC3Initialize()` Changes in this commit allow enabling only the required sample rate in LC3 codec, which decreases RAM usage and time to initalize codec. Signed-off-by: Ivan Iushkov <ivan.iushkov@nordicsemi.no>
1 parent 8626598 commit 92611e0

2 files changed

Lines changed: 90 additions & 43 deletions

File tree

lc3/include/sw_codec_lc3.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,24 @@ 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
*
115-
* @note: For documentation, see LC3API.h (/codec/inc/LC3API.h)
121+
* @param[in] encoder_sample_rate Encoder sample rate in Hz
122+
* @param[in] decoder_sample_rate Decoder sample rate in Hz
123+
* @param[in] sw_codec_lc3_buffer Pointer to a memory buffer
124+
* @param[in,out] sw_codec_lc3_buffer_size Size of the memory buffer passed in, number of bytes
125+
* used returned
126+
* @param[in] framesize_us Frame size in microseconds
127+
*
116128
*/
117-
int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
129+
int sw_codec_lc3_init(uint16_t encoder_sample_rate, uint16_t decoder_sample_rate,
130+
uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
118131
uint16_t framesize_us);
119132

120133
/**@brief Initializes the LC3 encoder and allocates required RAM

lc3/src/sw_codec_lc3.c

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

190-
int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
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+
201+
int sw_codec_lc3_init(uint16_t encoder_sample_rate, uint16_t decoder_sample_rate,
202+
uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
191203
uint16_t framesize_us)
192204
{
193205
int ret;
194-
uint8_t enc_sample_rates = 0;
195-
uint8_t dec_sample_rates = 0;
206+
uint8_t enc_sample_rate_bit = 0;
207+
uint8_t dec_sample_rate_bit = 0;
196208

197209
/* Set unique session to 0 for using the default sharing memory setting.
198210
*
@@ -201,61 +213,83 @@ int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffe
201213
*/
202214
uint8_t unique_session = 0;
203215

204-
/* Check supported sample rates for encoder */
205-
if (IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_8KHZ_SUPPORT)) {
206-
enc_sample_rates |= LC3_SAMPLE_RATE_8_KHZ;
207-
}
208-
if (IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_16KHZ_SUPPORT)) {
209-
enc_sample_rates |= LC3_SAMPLE_RATE_16_KHZ;
210-
}
211-
if (IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_24KHZ_SUPPORT)) {
212-
enc_sample_rates |= LC3_SAMPLE_RATE_24_KHZ;
213-
}
214-
if (IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_32KHZ_SUPPORT)) {
215-
enc_sample_rates |= LC3_SAMPLE_RATE_32_KHZ;
216-
}
217-
if (IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_441KHZ_SUPPORT)) {
218-
enc_sample_rates |= LC3_SAMPLE_RATE_441_KHZ;
219-
}
220-
if (IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_48KHZ_SUPPORT)) {
221-
enc_sample_rates |= LC3_SAMPLE_RATE_48_KHZ;
216+
/* Set a single bit in enc_sample_rate_bit corresponding to encoder_sample_rate */
217+
switch (encoder_sample_rate)
218+
{
219+
case 8000:
220+
enc_sample_rate_bit = IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_8KHZ_SUPPORT) ? LC3_SAMPLE_RATE_8_KHZ : 0;
221+
break;
222+
case 16000:
223+
enc_sample_rate_bit = IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_16KHZ_SUPPORT) ? LC3_SAMPLE_RATE_16_KHZ : 0;
224+
break;
225+
case 24000:
226+
enc_sample_rate_bit = IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_24KHZ_SUPPORT) ? LC3_SAMPLE_RATE_24_KHZ : 0;
227+
break;
228+
case 32000:
229+
enc_sample_rate_bit = IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_32KHZ_SUPPORT) ? LC3_SAMPLE_RATE_32_KHZ : 0;
230+
break;
231+
case 44100:
232+
enc_sample_rate_bit = IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_441KHZ_SUPPORT) ? LC3_SAMPLE_RATE_441_KHZ : 0;
233+
break;
234+
case 48000:
235+
enc_sample_rate_bit = IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_48KHZ_SUPPORT) ? LC3_SAMPLE_RATE_48_KHZ : 0;
236+
break;
237+
default:
238+
break;
222239
}
223240

224-
/* Check supported sample rates for decoder */
225-
if (IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_8KHZ_SUPPORT)) {
226-
dec_sample_rates |= LC3_SAMPLE_RATE_8_KHZ;
227-
}
228-
if (IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_16KHZ_SUPPORT)) {
229-
dec_sample_rates |= LC3_SAMPLE_RATE_16_KHZ;
230-
}
231-
if (IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_24KHZ_SUPPORT)) {
232-
dec_sample_rates |= LC3_SAMPLE_RATE_24_KHZ;
233-
}
234-
if (IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_32KHZ_SUPPORT)) {
235-
dec_sample_rates |= LC3_SAMPLE_RATE_32_KHZ;
241+
if (enc_sample_rate_bit == 0 && encoder_sample_rate != 0)
242+
{
243+
LOG_ERR("Unsupported encoder sample rate: %d", encoder_sample_rate);
244+
return -EINVAL;
236245
}
237-
if (IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_441KHZ_SUPPORT)) {
238-
dec_sample_rates |= LC3_SAMPLE_RATE_441_KHZ;
246+
247+
/* Set a single bit in dec_sample_rate_bit corresponding to decoder_sample_rate */
248+
switch (decoder_sample_rate)
249+
{
250+
case 8000:
251+
dec_sample_rate_bit = IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_8KHZ_SUPPORT) ? LC3_SAMPLE_RATE_8_KHZ : 0;
252+
break;
253+
case 16000:
254+
dec_sample_rate_bit = IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_16KHZ_SUPPORT) ? LC3_SAMPLE_RATE_16_KHZ : 0;
255+
break;
256+
case 24000:
257+
dec_sample_rate_bit = IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_24KHZ_SUPPORT) ? LC3_SAMPLE_RATE_24_KHZ : 0;
258+
break;
259+
case 32000:
260+
dec_sample_rate_bit = IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_32KHZ_SUPPORT) ? LC3_SAMPLE_RATE_32_KHZ : 0;
261+
break;
262+
case 44100:
263+
dec_sample_rate_bit = IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_441KHZ_SUPPORT) ? LC3_SAMPLE_RATE_441_KHZ : 0;
264+
break;
265+
case 48000:
266+
dec_sample_rate_bit = IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_48KHZ_SUPPORT) ? LC3_SAMPLE_RATE_48_KHZ : 0;
267+
break;
268+
default:
269+
break;
239270
}
240-
if (IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_48KHZ_SUPPORT)) {
241-
dec_sample_rates |= LC3_SAMPLE_RATE_48_KHZ;
271+
272+
if (dec_sample_rate_bit == 0 && decoder_sample_rate != 0)
273+
{
274+
LOG_ERR("Unsupported decoder sample rate: %d", decoder_sample_rate);
275+
return -EINVAL;
242276
}
243277

244-
LC3FrameSize_t framesize;
278+
LC3FrameSizeConfig_t framesize;
245279

246280
switch (framesize_us) {
247281
case 7500:
248-
framesize = LC3FrameSize7_5Ms;
282+
framesize = LC3FrameSize7_5MsConfig;
249283
break;
250284
case 10000:
251-
framesize = LC3FrameSize10Ms;
285+
framesize = LC3FrameSize10MsConfig;
252286
break;
253287
default:
254288
LOG_ERR("Unsupported framesize: %d", framesize_us);
255289
return -EINVAL;
256290
}
257291

258-
ret = LC3Initialize(enc_sample_rates, dec_sample_rates, framesize, unique_session,
292+
ret = LC3Initialize(enc_sample_rate_bit, dec_sample_rate_bit, framesize, unique_session,
259293
sw_codec_lc3_buffer, sw_codec_lc3_buffer_size);
260294
return ret;
261295
}

0 commit comments

Comments
 (0)