Skip to content

Commit ae5361c

Browse files
AEC: add a kConfig switch to use 16int or 32float API
Google AEC library allows 2 APIs to be used. This PR makes possible switching between APIs. kConfig COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
1 parent 23e36d6 commit ae5361c

3 files changed

Lines changed: 91 additions & 12 deletions

File tree

src/audio/google/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ config COMP_GOOGLE_RTC_AUDIO_PROCESSING
2525
This component takes raw microphones input and playback reference
2626
and outputs an echo-free microphone signal.
2727

28+
config COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
29+
depends on COMP_GOOGLE_RTC_AUDIO_PROCESSING
30+
bool "Use 32bit API in Google Audio processing"
31+
default n
32+
help
33+
Selects an API to be used in communication with the Google real-time
34+
communication audio processing: 32bit float or 16bit integer
35+
2836
config COMP_GOOGLE_RTC_AUDIO_PROCESSING_SAMPLE_RATE_HZ
2937
depends on COMP_GOOGLE_RTC_AUDIO_PROCESSING
3038
int "Sample rate for Google Real Time Communication Audio processing"

src/audio/google/google_rtc_audio_processing.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
#define GOOGLE_RTC_NUM_INPUT_PINS 2
4444
#define GOOGLE_RTC_NUM_OUTPUT_PINS 1
4545

46+
#if CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
47+
#define BUF_TYPE float
48+
#else
49+
#define BUF_TYPE int16_t
50+
#endif
51+
4652
LOG_MODULE_REGISTER(google_rtc_audio_processing, CONFIG_SOF_LOG_LEVEL);
4753

4854
/* b780a0a6-269f-466f-b477-23dfa05af758 */
@@ -59,10 +65,10 @@ struct google_rtc_audio_processing_comp_data {
5965
int num_aec_reference_channels;
6066
int num_capture_channels;
6167
GoogleRtcAudioProcessingState *state;
62-
float *aec_reference_buffer;
63-
float *aec_reference_buffer_ptrs[SOF_IPC_MAX_CHANNELS];
64-
float *process_buffer;
65-
float *process_buffer_ptrs[SOF_IPC_MAX_CHANNELS];
68+
BUF_TYPE *aec_reference_buffer;
69+
BUF_TYPE *aec_reference_buffer_ptrs[SOF_IPC_MAX_CHANNELS];
70+
BUF_TYPE *process_buffer;
71+
BUF_TYPE *process_buffer_ptrs[SOF_IPC_MAX_CHANNELS];
6672
uint8_t *memory_buffer;
6773
struct comp_data_blob_handler *tuning_handler;
6874
bool reconfigure;
@@ -591,8 +597,9 @@ static int google_rtc_audio_processing_reset(struct processing_module *mod)
591597
return 0;
592598
}
593599

594-
static int16_t convert_float_to_uint16_hifi(float data)
600+
static inline int16_t convert_google_aec_format_to_int16(BUF_TYPE data)
595601
{
602+
#if CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
596603
const xtfloat ratio = 2 << 14;
597604
xtfloat x0 = data;
598605
xtfloat x1;
@@ -602,17 +609,24 @@ static int16_t convert_float_to_uint16_hifi(float data)
602609
x = XT_TRUNC_S(x1, 0);
603610

604611
return x;
612+
#else /* CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API */
613+
return data;
614+
#endif
605615
}
606616

607-
static float convert_uint16_to_float_hifi(int16_t data)
617+
static inline BUF_TYPE convert_int16_to_google_aec_format(int16_t data)
608618
{
619+
#if CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
609620
const xtfloat ratio = 2 << 14;
610621
xtfloat x0 = data;
611622
float x;
612623

613624
x = XT_DIV_S(x0, ratio);
614625

615626
return x;
627+
#else /* CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API */
628+
return data;
629+
#endif
616630
}
617631

618632
/* todo CONFIG_FORMAT_S32LE */
@@ -679,17 +693,22 @@ static int google_rtc_audio_processing_process(struct processing_module *mod,
679693
for (int i = 0; i < cd->num_frames; i++) {
680694
for (channel = 0; channel < cd->num_aec_reference_channels; ++channel) {
681695
cd->aec_reference_buffer_ptrs[channel][i] =
682-
convert_uint16_to_float_hifi(ref[channel]);
696+
convert_int16_to_google_aec_format(ref[channel]);
683697
}
684698
ref += cd->num_aec_reference_channels;
685699
if ((void *)ref >= (void *)ref_buf_end)
686700
ref = (void *)ref_buf_start;
687701
}
688702

703+
#if CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
689704
GoogleRtcAudioProcessingAnalyzeRender_float32(
690705
cd->state,
691706
(const float **)cd->aec_reference_buffer_ptrs);
692-
707+
#else
708+
GoogleRtcAudioProcessingAnalyzeRender_int16(
709+
cd->state,
710+
(const int16_t *)cd->aec_reference_buffer);
711+
#endif
693712
source_release_data(ref_stream, num_of_bytes_to_process);
694713

695714
/* process main stream - de interlace and convert */
@@ -702,7 +721,7 @@ static int google_rtc_audio_processing_process(struct processing_module *mod,
702721
for (int i = 0; i < cd->num_frames; i++) {
703722
for (channel = 0; channel < cd->num_capture_channels; channel++)
704723
cd->process_buffer_ptrs[channel][i] =
705-
convert_uint16_to_float_hifi(src[channel]);
724+
convert_int16_to_google_aec_format(src[channel]);
706725

707726
src += cd->num_capture_channels;
708727
if ((void *)src >= (void *)src_buf_end)
@@ -712,9 +731,15 @@ static int google_rtc_audio_processing_process(struct processing_module *mod,
712731
source_release_data(src_stream, num_of_bytes_to_process);
713732

714733
/* call the library, use same in/out buffers */
734+
#if CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
715735
GoogleRtcAudioProcessingProcessCapture_float32(cd->state,
716736
(const float **)cd->process_buffer_ptrs,
717737
cd->process_buffer_ptrs);
738+
#else
739+
GoogleRtcAudioProcessingProcessCapture_int16(cd->state,
740+
(const int16_t *)cd->process_buffer,
741+
cd->process_buffer);
742+
#endif
718743

719744
/* same numnber of bytes to process for output stream as for mic stream */
720745
ret = sink_get_buffer(dst_stream, num_of_bytes_to_process, (void **)&dst,
@@ -724,8 +749,8 @@ static int google_rtc_audio_processing_process(struct processing_module *mod,
724749

725750
for (int i = 0; i < cd->num_frames; i++) {
726751
for (channel = 0; channel < cd->num_capture_channels; channel++)
727-
dst[channel] =
728-
convert_float_to_uint16_hifi(cd->process_buffer_ptrs[channel][i]);
752+
dst[channel] = convert_google_aec_format_to_int16(
753+
cd->process_buffer_ptrs[channel][i]);
729754
dst += cd->num_capture_channels;
730755
if ((void *)dst >= (void *)dst_buf_end)
731756
dst = (void *)dst_buf_start;

src/audio/google/google_rtc_audio_processing_mock.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <string.h>
1111
#include <stdint.h>
1212

13+
#include <sof/audio/format.h>
14+
#include <sof/math/numbers.h>
15+
1316
#include <rtos/alloc.h>
1417
#include "ipc/topology.h"
1518

@@ -21,7 +24,11 @@ struct GoogleRtcAudioProcessingState {
2124
int num_aec_reference_channels;
2225
int num_output_channels;
2326
int num_frames;
27+
#if CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
2428
float *aec_reference;
29+
#else
30+
int16_t *aec_reference;
31+
#endif
2532
};
2633

2734
static void SetFormats(GoogleRtcAudioProcessingState *const state,
@@ -138,13 +145,14 @@ int GoogleRtcAudioProcessingReconfigure(GoogleRtcAudioProcessingState *const sta
138145
return 0;
139146
}
140147

148+
#if CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API
141149
int GoogleRtcAudioProcessingProcessCapture_float32(GoogleRtcAudioProcessingState *const state,
142150
const float *const *src,
143151
float * const *dest)
144152
{
145153
float *ref = state->aec_reference;
146154
float **mic = (float **)src;
147-
int n, chan, ref_chan;
155+
int n, chan;
148156

149157
for (chan = 0; chan < state->num_output_channels; chan++) {
150158
for (n = 0; n < state->num_frames; ++n) {
@@ -174,6 +182,44 @@ int GoogleRtcAudioProcessingAnalyzeRender_float32(GoogleRtcAudioProcessingState
174182

175183
return 0;
176184
}
185+
#else /* CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API */
186+
int GoogleRtcAudioProcessingProcessCapture_int16(GoogleRtcAudioProcessingState *const state,
187+
const int16_t *const src,
188+
int16_t *const dest)
189+
{
190+
int16_t *ref = state->aec_reference;
191+
int16_t *mic = (int16_t *) src;
192+
int16_t *out = dest;
193+
int n, chan;
194+
195+
for (chan = 0; chan < state->num_output_channels; chan++) {
196+
for (n = 0; n < state->num_frames; ++n) {
197+
int16_t mic_save = mic[n + (chan * state->num_frames)];
198+
199+
if (chan < state->num_aec_reference_channels)
200+
dest[n + (chan * state->num_frames)] =
201+
mic_save + ref[n + (chan * state->num_frames)];
202+
else
203+
dest[n + (chan * state->num_frames)] = mic_save;
204+
}
205+
}
206+
207+
return 0;
208+
}
209+
210+
int GoogleRtcAudioProcessingAnalyzeRender_int16(GoogleRtcAudioProcessingState *const state,
211+
const int16_t *const data)
212+
{
213+
const size_t buffer_size =
214+
sizeof(state->aec_reference[0])
215+
* state->num_frames
216+
* state->num_aec_reference_channels;
217+
memcpy_s(state->aec_reference, buffer_size,
218+
data, buffer_size);
219+
return 0;
220+
}
221+
222+
#endif /* CONFIG_COMP_GOOGLE_RTC_USE_32_BIT_FLOAT_API */
177223

178224
void GoogleRtcAudioProcessingParseSofConfigMessage(uint8_t *message,
179225
size_t message_size,

0 commit comments

Comments
 (0)