Skip to content

Commit

Permalink
applications: nrf5340_audio: Integrate sample rate converter
Browse files Browse the repository at this point in the history
- Have fixed system sample rate
- Convert frames based on config from LE Audio

Signed-off-by: Andreas Vibeto <[email protected]>
Signed-off-by: Alexander Svensen <[email protected]>
  • Loading branch information
andvib authored and jfischer-no committed Feb 28, 2024
1 parent f13cac6 commit 0a2ef78
Show file tree
Hide file tree
Showing 24 changed files with 744 additions and 151 deletions.
10 changes: 7 additions & 3 deletions applications/nrf5340_audio/broadcast_sink/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static void le_audio_msg_sub_thread(void)
break;

case LE_AUDIO_EVT_CONFIG_RECEIVED:
LOG_DBG("Config received");
LOG_DBG("LE audio config received");

ret = broadcast_sink_config_get(&bitrate_bps, &sampling_rate_hz,
&pres_delay_us);
Expand All @@ -231,8 +231,12 @@ static void le_audio_msg_sub_thread(void)
break;
}

LOG_DBG("Sampling rate: %d Hz", sampling_rate_hz);
LOG_DBG("Bitrate: %d bps", bitrate_bps);
LOG_DBG("\tSampling rate: %d Hz", sampling_rate_hz);
LOG_DBG("\tBitrate (compressed): %d bps", bitrate_bps);

ret = audio_system_config_set(VALUE_NOT_SET, VALUE_NOT_SET,
sampling_rate_hz);
ERR_CHK(ret);

ret = audio_datapath_pres_delay_us_set(pres_delay_us);
if (ret) {
Expand Down
6 changes: 6 additions & 0 deletions applications/nrf5340_audio/broadcast_source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "streamctrl.h"

#include <zephyr/bluetooth/audio/audio.h>
#include <zephyr/kernel.h>
#include <zephyr/zbus/zbus.h>

Expand Down Expand Up @@ -337,6 +338,11 @@ int main(void)
ret = broadcast_source_enable();
ERR_CHK_MSG(ret, "Failed to enable broadcaster");

ret = audio_system_config_set(
bt_audio_codec_cfg_freq_to_freq_hz(CONFIG_BT_AUDIO_PREF_SAMPLE_RATE_VALUE),
CONFIG_BT_AUDIO_BITRATE_BROADCAST_SRC, VALUE_NOT_SET);
ERR_CHK_MSG(ret, "Failed to set sample- and bitrate");

broadcast_source_adv_get(&ext_adv, &ext_adv_size, &per_adv, &per_adv_size);

ret = bt_mgmt_adv_start(ext_adv, ext_adv_size, per_adv, per_adv_size, false);
Expand Down
3 changes: 3 additions & 0 deletions applications/nrf5340_audio/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# nRF5340 Audio
CONFIG_NRF5340_AUDIO=y

CONFIG_SAMPLE_RATE_CONVERTER=y
CONFIG_SAMPLE_RATE_CONVERTER_FILTER_SIMPLE=y

# General
CONFIG_DEBUG=y
CONFIG_DEBUG_INFO=y
Expand Down
3 changes: 3 additions & 0 deletions applications/nrf5340_audio/prj_release.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# nRF5340 Audio
CONFIG_NRF5340_AUDIO=y

CONFIG_SAMPLE_RATE_CONVERTER=y
CONFIG_SAMPLE_RATE_CONVERTER_FILTER_SIMPLE=y

# General
CONFIG_DEBUG=n
CONFIG_ASSERT=n
Expand Down
19 changes: 9 additions & 10 deletions applications/nrf5340_audio/src/audio/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ config AUDIO_MAX_PRES_DLY_US
help
The maximum presentation delay in micro seconds.

choice AUDIO_SAMPLE_RATE
prompt "Audio sample rate"
default AUDIO_SAMPLE_RATE_16000_HZ if BT_BAP_BROADCAST_16_2_1 || BT_BAP_BROADCAST_16_2_2 || BT_BAP_UNICAST_16_2_1
default AUDIO_SAMPLE_RATE_24000_HZ if BT_BAP_BROADCAST_24_2_1 || BT_BAP_BROADCAST_24_2_2 || BT_BAP_UNICAST_24_2_1
choice AUDIO_SYSTEM_SAMPLE_RATE
prompt "System audio sample rate"
default AUDIO_SAMPLE_RATE_48000_HZ
help
This will be selected based on the BAP settings.
This configuration reflects the system sample rate, but the audio data may be resampled to
another sample rate before encoding, and after decoding.

config AUDIO_SAMPLE_RATE_16000_HZ
bool "16 kHz"
Expand Down Expand Up @@ -213,7 +212,7 @@ visible if SW_CODEC_LC3

config LC3_BITRATE_MAX
int "Max bitrate for LC3"
default 124000
default 96000

config LC3_BITRATE_MIN
int "Min bitrate for LC3"
Expand Down Expand Up @@ -343,13 +342,13 @@ menu "Stack sizes"

config ENCODER_STACK_SIZE
int "Stack size for encoder thread"
default 7500 if AUDIO_BIT_DEPTH_16
default 11264 if AUDIO_BIT_DEPTH_32
default 11000 if AUDIO_BIT_DEPTH_16
default 21400 if AUDIO_BIT_DEPTH_32

config AUDIO_DATAPATH_STACK_SIZE
int "Stack size for audio datapath thread"
default 4096 if AUDIO_BIT_DEPTH_16
default 8192 if AUDIO_BIT_DEPTH_32
default 7600 if AUDIO_BIT_DEPTH_16
default 14700 if AUDIO_BIT_DEPTH_32

config BUTTON_MSG_SUB_STACK_SIZE
int "Stack size for button subscriber"
Expand Down
3 changes: 2 additions & 1 deletion applications/nrf5340_audio/src/audio/audio_datapath.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,8 @@ void audio_datapath_stream_out(const uint8_t *buf, size_t size, uint32_t sdu_ref
}

if (pcm_size != (BLK_STEREO_SIZE_OCTETS * NUM_BLKS_IN_FRAME)) {
LOG_WRN("Decoded audio has wrong size");
LOG_WRN("Decoded audio has wrong size: %d. Expected: %d", pcm_size,
(BLK_STEREO_SIZE_OCTETS * NUM_BLKS_IN_FRAME));
/* Discard frame */
return;
}
Expand Down
45 changes: 33 additions & 12 deletions applications/nrf5340_audio/src/audio/audio_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ static struct sw_codec_config sw_codec_cfg;
static int16_t test_tone_buf[CONFIG_AUDIO_SAMPLE_RATE_HZ / 1000];
static size_t test_tone_size;

static bool sample_rate_valid(uint32_t sample_rate_hz)
{
if (sample_rate_hz == 16000 || sample_rate_hz == 24000 || sample_rate_hz == 48000) {
return true;
}

return false;
}

static void audio_gateway_configure(void)
{
if (IS_ENABLED(CONFIG_SW_CODEC_LC3)) {
Expand All @@ -64,12 +73,6 @@ static void audio_gateway_configure(void)
sw_codec_cfg.decoder.channel_mode = SW_CODEC_MONO;
#endif /* (CONFIG_STREAM_BIDIRECTIONAL) */

if (IS_ENABLED(CONFIG_SW_CODEC_LC3)) {
sw_codec_cfg.encoder.bitrate = CONFIG_LC3_BITRATE;
} else {
ERR_CHK_MSG(-EINVAL, "No codec selected");
}

if (IS_ENABLED(CONFIG_MONO_TO_ALL_RECEIVERS)) {
sw_codec_cfg.encoder.num_ch = 1;
} else {
Expand All @@ -93,12 +96,6 @@ static void audio_headset_configure(void)
sw_codec_cfg.encoder.enabled = true;
sw_codec_cfg.encoder.num_ch = 1;
sw_codec_cfg.encoder.channel_mode = SW_CODEC_MONO;

if (IS_ENABLED(CONFIG_SW_CODEC_LC3)) {
sw_codec_cfg.encoder.bitrate = CONFIG_LC3_BITRATE;
} else {
ERR_CHK_MSG(-EINVAL, "No codec selected");
}
#endif /* (CONFIG_STREAM_BIDIRECTIONAL) */

sw_codec_cfg.decoder.num_ch = 1;
Expand Down Expand Up @@ -259,6 +256,30 @@ int audio_system_encode_test_tone_step(void)
return 0;
}

int audio_system_config_set(uint32_t encoder_sample_rate_hz, uint32_t encoder_bitrate,
uint32_t decoder_sample_rate_hz)
{
if (sample_rate_valid(encoder_sample_rate_hz)) {
sw_codec_cfg.encoder.sample_rate_hz = encoder_sample_rate_hz;
} else if (encoder_sample_rate_hz) {
LOG_ERR("%d is not a valid sample rate", encoder_sample_rate_hz);
return -EINVAL;
}

if (sample_rate_valid(decoder_sample_rate_hz)) {
sw_codec_cfg.decoder.sample_rate_hz = decoder_sample_rate_hz;
} else if (decoder_sample_rate_hz) {
LOG_ERR("%d is not a valid sample rate", decoder_sample_rate_hz);
return -EINVAL;
}

if (encoder_bitrate) {
sw_codec_cfg.encoder.bitrate = encoder_bitrate;
}

return 0;
}

/* This function is only used on gateway using USB as audio source and bidirectional stream */
int audio_system_decode(void const *const encoded_data, size_t encoded_data_size, bool bad_frame)
{
Expand Down
17 changes: 17 additions & 0 deletions applications/nrf5340_audio/src/audio/audio_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <stdbool.h>
#include <stdint.h>

#define VALUE_NOT_SET 0

/**
* @brief Start the execution of the encoder thread.
*/
Expand Down Expand Up @@ -46,6 +48,21 @@ int audio_system_encode_test_tone_set(uint32_t freq);
*/
int audio_system_encode_test_tone_step(void);

/**
* @brief Set the sample rates for the encoder and the decoder, and the bit rate for encoder.
*
* @note If any of the values are 0, the corresponding configuration will not be set.
*
* @param[in] encoder_sample_rate_hz Sample rate to be used by the encoder; can be 0.
* @param[in] encoder_bitrate Bit rate to be used by the encoder (bps); can be 0.
* @param[in] decoder_sample_rate_hz Sample rate to be used by the decoder; can be 0.
*
* @retval -EINVAL Invalid sample rate given.
* @retval 0 On success.
*/
int audio_system_config_set(uint32_t encoder_sample_rate_hz, uint32_t encoder_bitrate,
uint32_t decoder_sample_rate_hz);

/**
* @brief Decode data and then add it to TX FIFO buffer.
*
Expand Down
Loading

0 comments on commit 0a2ef78

Please sign in to comment.