Skip to content

Commit

Permalink
applications: nrf5340_audio: Add lc3_playback module
Browse files Browse the repository at this point in the history
  • Loading branch information
mVaardal committed Aug 1, 2023
1 parent 8eef146 commit 4051b3d
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 256 deletions.
1 change: 0 additions & 1 deletion applications/nrf5340_audio/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ CONFIG_DEVICE_SHELL=n
# assume card is in slot. Thus error message is always shown if card is not inserted
CONFIG_SD_LOG_LEVEL_OFF=y
CONFIG_LC3_DEC_CHAN_MAX=2

CONFIG_MAIN_STACK_SIZE=1800
CONFIG_LC3_PLAYBACK=y
3 changes: 1 addition & 2 deletions applications/nrf5340_audio/src/audio/audio_datapath.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,6 @@ static void alt_buffer_free_both(void)
* the in.fifo message queue.
*/

uint8_t sound_mix_buf[BLK_MONO_SIZE_OCTETS];
static void audio_datapath_i2s_blk_complete(uint32_t frame_start_ts, uint32_t *rx_buf_released,
uint32_t const *tx_buf_released)
{
Expand Down Expand Up @@ -853,7 +852,7 @@ void audio_datapath_stream_out(const uint8_t *buf, size_t size, uint32_t sdu_ref
int ret;
size_t pcm_size;

ret = sw_codec_decode(buf, size, bad_frame, &ctrl_blk.decoded_data, &pcm_size, true);
ret = sw_codec_decode(buf, size, bad_frame, &ctrl_blk.decoded_data, &pcm_size);

if (ret) {
LOG_WRN("SW codec decode error: %d", ret);
Expand Down
3 changes: 1 addition & 2 deletions applications/nrf5340_audio/src/audio/audio_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "audio_usb.h"
#include "streamctrl.h"
#include "pcm_mix.h"
#include <hal/nrf_gpio.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(audio_system, CONFIG_AUDIO_SYSTEM_LOG_LEVEL);
Expand Down Expand Up @@ -242,7 +241,7 @@ int audio_decode(void const *const encoded_data, size_t encoded_data_size, bool
}

ret = sw_codec_decode(encoded_data, encoded_data_size, bad_frame, &pcm_raw_data,
&pcm_block_size, true);
&pcm_block_size);
if (ret) {
LOG_ERR("Failed to decode");
return ret;
Expand Down
108 changes: 60 additions & 48 deletions applications/nrf5340_audio/src/audio/sw_codec_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int sw_codec_encode(void *pcm_data, size_t pcm_size, uint8_t **encoded_data, siz
}

int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool bad_frame,
void **decoded_data, size_t *decoded_size, bool lc3_playback_is_active)
void **decoded_data, size_t *decoded_size)
{
if (!m_config.decoder.enabled) {
LOG_ERR("Decoder has not been initialized");
Expand All @@ -126,8 +126,8 @@ int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool
#if (CONFIG_SW_CODEC_LC3)
/* Typically used for right channel if stereo signal */
char pcm_data_mono_right[PCM_NUM_BYTES_MONO] = {0};

if (m_config.decoder.num_ch == SW_CODEC_MONO) {
/* NB! For now, playing stereo files from SD card is unsupported */
if (IS_ENABLED(CONFIG_LC3_PLAYBACK)) {
if (bad_frame && IS_ENABLED(CONFIG_SW_CODEC_OVERRIDE_PLC)) {
memset(pcm_data_mono, 0, PCM_NUM_BYTES_MONO);
pcm_size_session = PCM_NUM_BYTES_MONO;
Expand All @@ -150,64 +150,76 @@ int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool
if (ret) {
return ret;
}
} else if (m_config.decoder.num_ch == SW_CODEC_STEREO && !lc3_playback_is_active) {
if (bad_frame && IS_ENABLED(CONFIG_SW_CODEC_OVERRIDE_PLC)) {
memset(pcm_data_mono, 0, PCM_NUM_BYTES_MONO);
memset(pcm_data_mono_right, 0, PCM_NUM_BYTES_MONO);
pcm_size_session = PCM_NUM_BYTES_MONO;
} else {
/* Decode left channel */
ret = sw_codec_lc3_dec_run(
encoded_data, encoded_size / 2, LC3_PCM_NUM_BYTES_MONO,
AUDIO_CH_L, pcm_data_mono, (uint16_t *)&pcm_size_session,
bad_frame);
if (ret) {
return ret;
} else {
switch (m_config.decoder.num_ch) {
case SW_CODEC_MONO: {
if (bad_frame && IS_ENABLED(CONFIG_SW_CODEC_OVERRIDE_PLC)) {
memset(pcm_data_mono, 0, PCM_NUM_BYTES_MONO);
pcm_size_session = PCM_NUM_BYTES_MONO;
} else {
ret = sw_codec_lc3_dec_run(
encoded_data, encoded_size, LC3_PCM_NUM_BYTES_MONO,
0, pcm_data_mono, (uint16_t *)&pcm_size_session,
bad_frame);
if (ret) {
return ret;
}
}
/* Decode right channel */
ret = sw_codec_lc3_dec_run(
(encoded_data + (encoded_size / 2)), encoded_size / 2,
LC3_PCM_NUM_BYTES_MONO, AUDIO_CH_R, pcm_data_mono_right,
(uint16_t *)&pcm_size_session, bad_frame);

/* For now, i2s is only stereo, so in order to send
* just one channel, we need to insert 0 for the
* other channel
*/
ret = pscm_zero_pad(pcm_data_mono, pcm_size_session,
m_config.decoder.audio_ch,
CONFIG_AUDIO_BIT_DEPTH_BITS, pcm_data_stereo,
&pcm_size_stereo);
if (ret) {
return ret;
}
break;
}
ret = pscm_combine(pcm_data_mono, pcm_data_mono_right, pcm_size_session,
CONFIG_AUDIO_BIT_DEPTH_BITS, pcm_data_stereo,
&pcm_size_stereo);
if (ret) {
return ret;
}
} else if (m_config.decoder.num_ch == SW_CODEC_STEREO && lc3_playback_is_active) {
if (bad_frame && IS_ENABLED(CONFIG_SW_CODEC_OVERRIDE_PLC)) {
memset(pcm_data_mono, 0, PCM_NUM_BYTES_MONO);
pcm_size_session = PCM_NUM_BYTES_MONO;
} else {
ret = sw_codec_lc3_dec_run(
encoded_data, encoded_size, LC3_PCM_NUM_BYTES_MONO, 0,
pcm_data_mono, (uint16_t *)&pcm_size_session, bad_frame);
case SW_CODEC_STEREO: {
if (bad_frame && IS_ENABLED(CONFIG_SW_CODEC_OVERRIDE_PLC)) {
memset(pcm_data_mono, 0, PCM_NUM_BYTES_MONO);
memset(pcm_data_mono_right, 0, PCM_NUM_BYTES_MONO);
pcm_size_session = PCM_NUM_BYTES_MONO;
} else {
/* Decode left channel */
ret = sw_codec_lc3_dec_run(
encoded_data, encoded_size / 2,
LC3_PCM_NUM_BYTES_MONO, AUDIO_CH_L, pcm_data_mono,
(uint16_t *)&pcm_size_session, bad_frame);
if (ret) {
return ret;
}
/* Decode right channel */
ret = sw_codec_lc3_dec_run(
(encoded_data + (encoded_size / 2)),
encoded_size / 2, LC3_PCM_NUM_BYTES_MONO,
AUDIO_CH_R, pcm_data_mono_right,
(uint16_t *)&pcm_size_session, bad_frame);
if (ret) {
return ret;
}
}
ret = pscm_combine(pcm_data_mono, pcm_data_mono_right,
pcm_size_session, CONFIG_AUDIO_BIT_DEPTH_BITS,
pcm_data_stereo, &pcm_size_stereo);
if (ret) {
return ret;
}
break;
}

/* For now, i2s is only stereo, so in order to send
* just one channel, we need to insert 0 for the
* other channel
*/
ret = pscm_zero_pad(pcm_data_mono, pcm_size_session,
m_config.decoder.audio_ch, CONFIG_AUDIO_BIT_DEPTH_BITS,
pcm_data_stereo, &pcm_size_stereo);
if (ret) {
return ret;
default:
LOG_ERR("Unsupported number of channels: %d",
m_config.encoder.num_ch);
return -ENODEV;
}
} else {
LOG_ERR("Unsupported number of channels: %d", m_config.encoder.num_ch);
return -ENODEV;
}
*decoded_size = pcm_size_stereo;
*decoded_data = pcm_data_stereo;

#endif /* (CONFIG_SW_CODEC_LC3) */
break;
}
Expand Down
2 changes: 1 addition & 1 deletion applications/nrf5340_audio/src/audio/sw_codec_select.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int sw_codec_encode(void *pcm_data, size_t pcm_size, uint8_t **encoded_data, siz
* @return 0 if success, error codes depends on sw_codec selected
*/
int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool bad_frame,
void **decoded_data, size_t *decoded_size, bool lc3_playback_is_active);
void **decoded_data, size_t *decoded_size);

/**@brief Uninitialize sw_codec and free allocated space
*
Expand Down
4 changes: 4 additions & 0 deletions applications/nrf5340_audio/src/modules/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ module = MODULE_SD_CARD
module-str = module-sd-card
source "subsys/logging/Kconfig.template.log_config"

module = MODULE_LC3_PLAYBACK
module-str = module-lc3-playback
source "subsys/logging/Kconfig.template.log_config"

endmenu # Log levels

#----------------------------------------------------------------------------#
Expand Down
Loading

0 comments on commit 4051b3d

Please sign in to comment.