forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_rtc_audio_processing_mock.c
More file actions
211 lines (189 loc) · 6.1 KB
/
google_rtc_audio_processing_mock.c
File metadata and controls
211 lines (189 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2021 Google LLC.
//
// Author: Lionel Koenig <lionelk@google.com>
#include "google_rtc_audio_processing.h"
#include "google_rtc_audio_processing_sof_message_reader.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sof/audio/format.h>
#include <sof/math/numbers.h>
#include <rtos/alloc.h>
#include "ipc/topology.h"
#define GOOGLE_RTC_AUDIO_PROCESSING_FREQENCY_TO_PERIOD_FRAMES 100
#define GOOGLE_RTC_AUDIO_PROCESSING_MS_PER_SECOND 1000
struct GoogleRtcAudioProcessingState {
int num_capture_channels;
int num_aec_reference_channels;
int num_output_channels;
int num_frames;
int16_t *aec_reference;
};
static void SetFormats(GoogleRtcAudioProcessingState *const state,
int capture_sample_rate_hz,
int num_capture_input_channels,
int num_capture_output_channels,
int render_sample_rate_hz,
int num_render_channels)
{
state->num_capture_channels = num_capture_input_channels;
state->num_output_channels = num_capture_output_channels;
state->num_frames = capture_sample_rate_hz /
GOOGLE_RTC_AUDIO_PROCESSING_FREQENCY_TO_PERIOD_FRAMES;
state->num_aec_reference_channels = num_render_channels;
rfree(state->aec_reference);
state->aec_reference = rballoc(0,
SOF_MEM_CAPS_RAM,
sizeof(state->aec_reference[0]) *
state->num_frames *
state->num_aec_reference_channels);
}
void GoogleRtcAudioProcessingAttachMemoryBuffer(uint8_t *const buffer,
int buffer_size)
{
}
void GoogleRtcAudioProcessingDetachMemoryBuffer(void)
{
}
GoogleRtcAudioProcessingState *GoogleRtcAudioProcessingCreateWithConfig(int capture_sample_rate_hz,
int num_capture_input_channels,
int num_capture_output_channels,
int render_sample_rate_hz,
int num_render_channels,
const uint8_t *const config,
int config_size)
{
struct GoogleRtcAudioProcessingState *s =
rballoc(0, SOF_MEM_CAPS_RAM, sizeof(GoogleRtcAudioProcessingState));
if (!s)
return NULL;
s->aec_reference = NULL;
SetFormats(s,
capture_sample_rate_hz,
num_capture_input_channels,
num_capture_output_channels,
render_sample_rate_hz,
num_render_channels);
if (!s->aec_reference) {
rfree(s);
return NULL;
}
return s;
}
GoogleRtcAudioProcessingState *GoogleRtcAudioProcessingCreate(void)
{
return GoogleRtcAudioProcessingCreateWithConfig(CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_SAMPLE_RATE_HZ,
1,
1,
CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_SAMPLE_RATE_HZ,
2,
NULL,
0);
}
void GoogleRtcAudioProcessingFree(GoogleRtcAudioProcessingState *state)
{
if (state != NULL) {
rfree(state->aec_reference);
rfree(state);
}
}
int GoogleRtcAudioProcessingSetStreamFormats(GoogleRtcAudioProcessingState *const state,
int capture_sample_rate_hz,
int num_capture_input_channels,
int num_capture_output_channels,
int render_sample_rate_hz,
int num_render_channels)
{
SetFormats(state,
capture_sample_rate_hz,
num_capture_input_channels,
num_capture_output_channels,
render_sample_rate_hz,
num_render_channels);
return 0;
}
int GoogleRtcAudioProcessingParameters(GoogleRtcAudioProcessingState *const state,
float *capture_headroom_linear,
float *echo_path_delay_ms)
{
return 0;
}
int GoogleRtcAudioProcessingGetFramesizeInMs(GoogleRtcAudioProcessingState *state)
{
return state->num_frames *
GOOGLE_RTC_AUDIO_PROCESSING_MS_PER_SECOND /
CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_SAMPLE_RATE_HZ;
}
int GoogleRtcAudioProcessingReconfigure(GoogleRtcAudioProcessingState *const state,
const uint8_t *const config,
int config_size)
{
return 0;
}
int GoogleRtcAudioProcessingProcessCapture_int16(GoogleRtcAudioProcessingState *const state,
const int16_t *const src,
int16_t *const dest)
{
int16_t *ref = state->aec_reference;
int16_t *mic = (int16_t *) src;
int16_t *out = dest;
int n, io, im, ir;
/* Mix input and reference channels to output. The matching channels numbers
* are mixed. If e.g. microphone and output channels count is 4, and reference
* has 2 channels, output channels 3 and 4 are copy of microphone channels 3 and 4,
* and output channels 1 and 2 are sum of microphone and reference.
*/
memset(dest, 0, sizeof(int16_t) * state->num_output_channels * state->num_frames);
for (n = 0; n < state->num_frames; ++n) {
im = 0;
ir = 0;
for (io = 0; io < state->num_output_channels; io++) {
out[io] = sat_int16(
(im < state->num_capture_channels ? (int32_t)mic[im++] : 0) +
(ir < state->num_aec_reference_channels ? (int32_t)ref[ir++] : 0));
}
ref += state->num_aec_reference_channels;
out += state->num_output_channels;
mic += state->num_capture_channels;
}
return 0;
}
int GoogleRtcAudioProcessingAnalyzeRender_int16(GoogleRtcAudioProcessingState *const state,
const int16_t *const data)
{
const size_t buffer_size =
sizeof(state->aec_reference[0])
* state->num_frames
* state->num_aec_reference_channels;
memcpy_s(state->aec_reference, buffer_size,
data, buffer_size);
return 0;
}
void GoogleRtcAudioProcessingParseSofConfigMessage(uint8_t *message,
size_t message_size,
uint8_t **google_rtc_audio_processing_config,
size_t *google_rtc_audio_processing_config_size,
int *num_capture_input_channels,
int *num_capture_output_channels,
float *aec_reference_delay,
float *mic_gain,
bool *google_rtc_audio_processing_config_present,
bool *num_capture_input_channels_present,
bool *num_capture_output_channels_present,
bool *aec_reference_delay_present,
bool *mic_gain_present)
{
*google_rtc_audio_processing_config = NULL;
*google_rtc_audio_processing_config_size = 0;
*num_capture_input_channels = 1;
*num_capture_output_channels = 1;
*aec_reference_delay = 0;
*mic_gain = 1;
*google_rtc_audio_processing_config_present = false;
*num_capture_input_channels_present = false;
*num_capture_output_channels_present = false;
*aec_reference_delay_present = false;
*mic_gain_present = false;
}