Skip to content

Commit 0a2ef78

Browse files
andvibjfischer-no
authored andcommitted
applications: nrf5340_audio: Integrate sample rate converter
- Have fixed system sample rate - Convert frames based on config from LE Audio Signed-off-by: Andreas Vibeto <andreas.vibeto@nordicsemi.no> Signed-off-by: Alexander Svensen <alexander.svensen@nordicsemi.no>
1 parent f13cac6 commit 0a2ef78

File tree

24 files changed

+744
-151
lines changed

24 files changed

+744
-151
lines changed

applications/nrf5340_audio/broadcast_sink/main.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static void le_audio_msg_sub_thread(void)
222222
break;
223223

224224
case LE_AUDIO_EVT_CONFIG_RECEIVED:
225-
LOG_DBG("Config received");
225+
LOG_DBG("LE audio config received");
226226

227227
ret = broadcast_sink_config_get(&bitrate_bps, &sampling_rate_hz,
228228
&pres_delay_us);
@@ -231,8 +231,12 @@ static void le_audio_msg_sub_thread(void)
231231
break;
232232
}
233233

234-
LOG_DBG("Sampling rate: %d Hz", sampling_rate_hz);
235-
LOG_DBG("Bitrate: %d bps", bitrate_bps);
234+
LOG_DBG("\tSampling rate: %d Hz", sampling_rate_hz);
235+
LOG_DBG("\tBitrate (compressed): %d bps", bitrate_bps);
236+
237+
ret = audio_system_config_set(VALUE_NOT_SET, VALUE_NOT_SET,
238+
sampling_rate_hz);
239+
ERR_CHK(ret);
236240

237241
ret = audio_datapath_pres_delay_us_set(pres_delay_us);
238242
if (ret) {

applications/nrf5340_audio/broadcast_source/main.c

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "streamctrl.h"
88

9+
#include <zephyr/bluetooth/audio/audio.h>
910
#include <zephyr/kernel.h>
1011
#include <zephyr/zbus/zbus.h>
1112

@@ -337,6 +338,11 @@ int main(void)
337338
ret = broadcast_source_enable();
338339
ERR_CHK_MSG(ret, "Failed to enable broadcaster");
339340

341+
ret = audio_system_config_set(
342+
bt_audio_codec_cfg_freq_to_freq_hz(CONFIG_BT_AUDIO_PREF_SAMPLE_RATE_VALUE),
343+
CONFIG_BT_AUDIO_BITRATE_BROADCAST_SRC, VALUE_NOT_SET);
344+
ERR_CHK_MSG(ret, "Failed to set sample- and bitrate");
345+
340346
broadcast_source_adv_get(&ext_adv, &ext_adv_size, &per_adv, &per_adv_size);
341347

342348
ret = bt_mgmt_adv_start(ext_adv, ext_adv_size, per_adv, per_adv_size, false);

applications/nrf5340_audio/prj.conf

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# nRF5340 Audio
88
CONFIG_NRF5340_AUDIO=y
99

10+
CONFIG_SAMPLE_RATE_CONVERTER=y
11+
CONFIG_SAMPLE_RATE_CONVERTER_FILTER_SIMPLE=y
12+
1013
# General
1114
CONFIG_DEBUG=y
1215
CONFIG_DEBUG_INFO=y

applications/nrf5340_audio/prj_release.conf

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# nRF5340 Audio
88
CONFIG_NRF5340_AUDIO=y
99

10+
CONFIG_SAMPLE_RATE_CONVERTER=y
11+
CONFIG_SAMPLE_RATE_CONVERTER_FILTER_SIMPLE=y
12+
1013
# General
1114
CONFIG_DEBUG=n
1215
CONFIG_ASSERT=n

applications/nrf5340_audio/src/audio/Kconfig

+9-10
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ config AUDIO_MAX_PRES_DLY_US
4343
help
4444
The maximum presentation delay in micro seconds.
4545

46-
choice AUDIO_SAMPLE_RATE
47-
prompt "Audio sample rate"
48-
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
49-
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
46+
choice AUDIO_SYSTEM_SAMPLE_RATE
47+
prompt "System audio sample rate"
5048
default AUDIO_SAMPLE_RATE_48000_HZ
5149
help
52-
This will be selected based on the BAP settings.
50+
This configuration reflects the system sample rate, but the audio data may be resampled to
51+
another sample rate before encoding, and after decoding.
5352

5453
config AUDIO_SAMPLE_RATE_16000_HZ
5554
bool "16 kHz"
@@ -213,7 +212,7 @@ visible if SW_CODEC_LC3
213212

214213
config LC3_BITRATE_MAX
215214
int "Max bitrate for LC3"
216-
default 124000
215+
default 96000
217216

218217
config LC3_BITRATE_MIN
219218
int "Min bitrate for LC3"
@@ -343,13 +342,13 @@ menu "Stack sizes"
343342

344343
config ENCODER_STACK_SIZE
345344
int "Stack size for encoder thread"
346-
default 7500 if AUDIO_BIT_DEPTH_16
347-
default 11264 if AUDIO_BIT_DEPTH_32
345+
default 11000 if AUDIO_BIT_DEPTH_16
346+
default 21400 if AUDIO_BIT_DEPTH_32
348347

349348
config AUDIO_DATAPATH_STACK_SIZE
350349
int "Stack size for audio datapath thread"
351-
default 4096 if AUDIO_BIT_DEPTH_16
352-
default 8192 if AUDIO_BIT_DEPTH_32
350+
default 7600 if AUDIO_BIT_DEPTH_16
351+
default 14700 if AUDIO_BIT_DEPTH_32
353352

354353
config BUTTON_MSG_SUB_STACK_SIZE
355354
int "Stack size for button subscriber"

applications/nrf5340_audio/src/audio/audio_datapath.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,8 @@ void audio_datapath_stream_out(const uint8_t *buf, size_t size, uint32_t sdu_ref
980980
}
981981

982982
if (pcm_size != (BLK_STEREO_SIZE_OCTETS * NUM_BLKS_IN_FRAME)) {
983-
LOG_WRN("Decoded audio has wrong size");
983+
LOG_WRN("Decoded audio has wrong size: %d. Expected: %d", pcm_size,
984+
(BLK_STEREO_SIZE_OCTETS * NUM_BLKS_IN_FRAME));
984985
/* Discard frame */
985986
return;
986987
}

applications/nrf5340_audio/src/audio/audio_system.c

+33-12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ static struct sw_codec_config sw_codec_cfg;
5050
static int16_t test_tone_buf[CONFIG_AUDIO_SAMPLE_RATE_HZ / 1000];
5151
static size_t test_tone_size;
5252

53+
static bool sample_rate_valid(uint32_t sample_rate_hz)
54+
{
55+
if (sample_rate_hz == 16000 || sample_rate_hz == 24000 || sample_rate_hz == 48000) {
56+
return true;
57+
}
58+
59+
return false;
60+
}
61+
5362
static void audio_gateway_configure(void)
5463
{
5564
if (IS_ENABLED(CONFIG_SW_CODEC_LC3)) {
@@ -64,12 +73,6 @@ static void audio_gateway_configure(void)
6473
sw_codec_cfg.decoder.channel_mode = SW_CODEC_MONO;
6574
#endif /* (CONFIG_STREAM_BIDIRECTIONAL) */
6675

67-
if (IS_ENABLED(CONFIG_SW_CODEC_LC3)) {
68-
sw_codec_cfg.encoder.bitrate = CONFIG_LC3_BITRATE;
69-
} else {
70-
ERR_CHK_MSG(-EINVAL, "No codec selected");
71-
}
72-
7376
if (IS_ENABLED(CONFIG_MONO_TO_ALL_RECEIVERS)) {
7477
sw_codec_cfg.encoder.num_ch = 1;
7578
} else {
@@ -93,12 +96,6 @@ static void audio_headset_configure(void)
9396
sw_codec_cfg.encoder.enabled = true;
9497
sw_codec_cfg.encoder.num_ch = 1;
9598
sw_codec_cfg.encoder.channel_mode = SW_CODEC_MONO;
96-
97-
if (IS_ENABLED(CONFIG_SW_CODEC_LC3)) {
98-
sw_codec_cfg.encoder.bitrate = CONFIG_LC3_BITRATE;
99-
} else {
100-
ERR_CHK_MSG(-EINVAL, "No codec selected");
101-
}
10299
#endif /* (CONFIG_STREAM_BIDIRECTIONAL) */
103100

104101
sw_codec_cfg.decoder.num_ch = 1;
@@ -259,6 +256,30 @@ int audio_system_encode_test_tone_step(void)
259256
return 0;
260257
}
261258

259+
int audio_system_config_set(uint32_t encoder_sample_rate_hz, uint32_t encoder_bitrate,
260+
uint32_t decoder_sample_rate_hz)
261+
{
262+
if (sample_rate_valid(encoder_sample_rate_hz)) {
263+
sw_codec_cfg.encoder.sample_rate_hz = encoder_sample_rate_hz;
264+
} else if (encoder_sample_rate_hz) {
265+
LOG_ERR("%d is not a valid sample rate", encoder_sample_rate_hz);
266+
return -EINVAL;
267+
}
268+
269+
if (sample_rate_valid(decoder_sample_rate_hz)) {
270+
sw_codec_cfg.decoder.sample_rate_hz = decoder_sample_rate_hz;
271+
} else if (decoder_sample_rate_hz) {
272+
LOG_ERR("%d is not a valid sample rate", decoder_sample_rate_hz);
273+
return -EINVAL;
274+
}
275+
276+
if (encoder_bitrate) {
277+
sw_codec_cfg.encoder.bitrate = encoder_bitrate;
278+
}
279+
280+
return 0;
281+
}
282+
262283
/* This function is only used on gateway using USB as audio source and bidirectional stream */
263284
int audio_system_decode(void const *const encoded_data, size_t encoded_data_size, bool bad_frame)
264285
{

applications/nrf5340_audio/src/audio/audio_system.h

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <stdbool.h>
1212
#include <stdint.h>
1313

14+
#define VALUE_NOT_SET 0
15+
1416
/**
1517
* @brief Start the execution of the encoder thread.
1618
*/
@@ -46,6 +48,21 @@ int audio_system_encode_test_tone_set(uint32_t freq);
4648
*/
4749
int audio_system_encode_test_tone_step(void);
4850

51+
/**
52+
* @brief Set the sample rates for the encoder and the decoder, and the bit rate for encoder.
53+
*
54+
* @note If any of the values are 0, the corresponding configuration will not be set.
55+
*
56+
* @param[in] encoder_sample_rate_hz Sample rate to be used by the encoder; can be 0.
57+
* @param[in] encoder_bitrate Bit rate to be used by the encoder (bps); can be 0.
58+
* @param[in] decoder_sample_rate_hz Sample rate to be used by the decoder; can be 0.
59+
*
60+
* @retval -EINVAL Invalid sample rate given.
61+
* @retval 0 On success.
62+
*/
63+
int audio_system_config_set(uint32_t encoder_sample_rate_hz, uint32_t encoder_bitrate,
64+
uint32_t decoder_sample_rate_hz);
65+
4966
/**
5067
* @brief Decode data and then add it to TX FIFO buffer.
5168
*

0 commit comments

Comments
 (0)