Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lc3/include/sw_codec_lc3.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,38 @@ int sw_codec_lc3_enc_uninit_all(void);
*/
int sw_codec_lc3_dec_uninit_all(void);
Comment thread
koffes marked this conversation as resolved.

/**@brief Uninitializes the LC3 Codec and frees allocated RAM
*
* @return 0 on success. -EIO in case of error.
*/
int sw_codec_lc3_uninit(void);

/**@brief Initializes the LC3 Codec
*
* @note: For documentation, see LC3API.h (/codec/inc/LC3API.h)
*/
int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
uint16_t framesize_us);

/**@brief Initializes the LC3 Codec with a single sample rate for encoder and decoder
*
* @details This function initializes the LC3 codec enabling only the requested sample rate
* for encoder and decoder, which decreases RAM usage and initialization time
* compared to sw_codec_lc3_init() which enables all supported sample rates.
*
* @param[in] encoder_sample_rate Encoder sample rate in Hz
* @param[in] decoder_sample_rate Decoder sample rate in Hz
* @param[in] sw_codec_lc3_buffer Pointer to a memory buffer
* @param[in,out] sw_codec_lc3_buffer_size Size of the memory buffer passed in, number of bytes
* used returned
* @param[in] framesize_us Frame size in microseconds
*
*/
int sw_codec_lc3_single_rate_init(uint16_t encoder_sample_rate, uint16_t decoder_sample_rate,
uint8_t *sw_codec_lc3_buffer,
uint32_t *sw_codec_lc3_buffer_size,
uint16_t framesize_us);

/**@brief Initializes the LC3 encoder and allocates required RAM
*
* @param[in] pcm_sample_rate Sample rate in Hz (typ. 16000 or 48000)
Expand Down
98 changes: 98 additions & 0 deletions lc3/src/sw_codec_lc3.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ int sw_codec_lc3_dec_uninit_all(void)
return 0;
}

int sw_codec_lc3_uninit(void)
{
int ret;
ret = LC3Deinitialize();
if (ret) {
LOG_ERR("Failed to uninitialize LC3 Codec: %d", ret);
return -EIO;
}
return 0;
}

int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffer_size,
uint16_t framesize_us)
{
Expand Down Expand Up @@ -260,6 +271,93 @@ int sw_codec_lc3_init(uint8_t *sw_codec_lc3_buffer, uint32_t *sw_codec_lc3_buffe
return ret;
}

static uint8_t enc_sample_rate_hz_to_bit(uint16_t sample_rate)
{
switch (sample_rate) {
case 8000:
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_8KHZ_SUPPORT) ? LC3_SAMPLE_RATE_8_KHZ : 0;
case 16000:
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_16KHZ_SUPPORT) ? LC3_SAMPLE_RATE_16_KHZ : 0;
case 24000:
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_24KHZ_SUPPORT) ? LC3_SAMPLE_RATE_24_KHZ : 0;
case 32000:
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_32KHZ_SUPPORT) ? LC3_SAMPLE_RATE_32_KHZ : 0;
case 44100:
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_441KHZ_SUPPORT) ? LC3_SAMPLE_RATE_441_KHZ : 0;
case 48000:
return IS_ENABLED(CONFIG_LC3_ENC_SAMPLE_RATE_48KHZ_SUPPORT) ? LC3_SAMPLE_RATE_48_KHZ : 0;
default:
return 0;
}
}

static uint8_t dec_sample_rate_hz_to_bit(uint16_t sample_rate)
{
switch (sample_rate) {
case 8000:
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_8KHZ_SUPPORT) ? LC3_SAMPLE_RATE_8_KHZ : 0;
case 16000:
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_16KHZ_SUPPORT) ? LC3_SAMPLE_RATE_16_KHZ : 0;
case 24000:
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_24KHZ_SUPPORT) ? LC3_SAMPLE_RATE_24_KHZ : 0;
case 32000:
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_32KHZ_SUPPORT) ? LC3_SAMPLE_RATE_32_KHZ : 0;
case 44100:
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_441KHZ_SUPPORT) ? LC3_SAMPLE_RATE_441_KHZ : 0;
case 48000:
return IS_ENABLED(CONFIG_LC3_DEC_SAMPLE_RATE_48KHZ_SUPPORT) ? LC3_SAMPLE_RATE_48_KHZ : 0;
default:
return 0;
}
}

int sw_codec_lc3_single_rate_init(uint16_t encoder_sample_rate, uint16_t decoder_sample_rate,
uint8_t *sw_codec_lc3_buffer,
uint32_t *sw_codec_lc3_buffer_size,
uint16_t framesize_us)
{
int ret;
uint8_t enc_sample_rate_bit;
uint8_t dec_sample_rate_bit;

/* Set unique session to 0 for using the default sharing memory setting.
*
* This could lead to higher heap consumption, but is able to manipulate
* different sample rate setting between encoder/decoder.
*/
uint8_t unique_session = 0;

enc_sample_rate_bit = enc_sample_rate_hz_to_bit(encoder_sample_rate);
if (enc_sample_rate_bit == 0 && encoder_sample_rate != 0) {
LOG_ERR("Unsupported encoder sample rate: %d", encoder_sample_rate);
return -EINVAL;
}

dec_sample_rate_bit = dec_sample_rate_hz_to_bit(decoder_sample_rate);
if (dec_sample_rate_bit == 0 && decoder_sample_rate != 0) {
LOG_ERR("Unsupported decoder sample rate: %d", decoder_sample_rate);
return -EINVAL;
}

LC3FrameSizeConfig_t framesize;

switch (framesize_us) {
case 7500:
framesize = LC3FrameSize7_5MsConfig;
break;
case 10000:
framesize = LC3FrameSize10MsConfig;
break;
default:
LOG_ERR("Unsupported framesize: %d", framesize_us);
return -EINVAL;
}

ret = LC3Initialize(enc_sample_rate_bit, dec_sample_rate_bit, framesize, unique_session,
sw_codec_lc3_buffer, sw_codec_lc3_buffer_size);
return ret;
}

int sw_codec_lc3_enc_init(uint16_t pcm_sample_rate, uint8_t pcm_bit_depth, uint16_t framesize_us,
uint32_t enc_bitrate, uint8_t num_channels, uint16_t *const pcm_bytes_req)
{
Expand Down
Loading