Repository navigation
Expand file tree
/
Copy pathcrossover_generic.c
More file actions
296 lines (260 loc) · 7.99 KB
/
Copy pathcrossover_generic.c
File metadata and controls
296 lines (260 loc) · 7.99 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2020 Google LLC. All rights reserved.
//
// Author: Sebastiano Carlucci <scarlucci@google.com>
#include <ipc/stream.h>
#include <sof/audio/module_adapter/module/module_interface.h>
#include <sof/audio/component.h>
#include <sof/audio/crossover/crossover.h>
#include <sof/audio/format.h>
#include <sof/math/iir_df2t.h>
#include <stdint.h>
/*
* \brief Splits x into two based on the coefficients set in the lp
* and hp filters. The output of the lp is in y1, the output of
* the hp is in y2.
*
* As a side effect, this function mutates the delay values of both
* filters.
*/
static inline void crossover_generic_lr4_split(struct iir_state_df2t *lp,
struct iir_state_df2t *hp,
int32_t x, int32_t *y1,
int32_t *y2)
{
*y1 = crossover_generic_process_lr4(x, lp);
*y2 = crossover_generic_process_lr4(x, hp);
}
/*
* \brief Splits input signal into two and merges it back to it's
* original form.
*
* With 3-way crossovers, one output goes through only one LR4 filter,
* whereas the other two go through two LR4 filters. This causes the signals
* to be out of phase. We need to pass the signal through another set of LR4
* filters to align back the phase.
*/
static inline void crossover_generic_lr4_merge(struct iir_state_df2t *lp,
struct iir_state_df2t *hp,
int32_t x, int32_t *y)
{
int32_t z1, z2;
z1 = crossover_generic_process_lr4(x, lp);
z2 = crossover_generic_process_lr4(x, hp);
*y = sat_int32(((int64_t)z1) + z2);
}
static void crossover_generic_split_2way(int32_t in,
int32_t out[],
struct crossover_state *state)
{
crossover_generic_lr4_split(&state->lowpass[0], &state->highpass[0],
in, &out[0], &out[1]);
}
static void crossover_generic_split_3way(int32_t in,
int32_t out[],
struct crossover_state *state)
{
int32_t z1, z2;
crossover_generic_lr4_split(&state->lowpass[0], &state->highpass[0],
in, &z1, &z2);
/* Realign the phase of z1 */
crossover_generic_lr4_merge(&state->lowpass[1], &state->highpass[1],
z1, &out[0]);
crossover_generic_lr4_split(&state->lowpass[2], &state->highpass[2],
z2, &out[1], &out[2]);
}
static void crossover_generic_split_4way(int32_t in,
int32_t out[],
struct crossover_state *state)
{
int32_t z1, z2;
crossover_generic_lr4_split(&state->lowpass[1], &state->highpass[1],
in, &z1, &z2);
crossover_generic_lr4_split(&state->lowpass[0], &state->highpass[0],
z1, &out[0], &out[1]);
crossover_generic_lr4_split(&state->lowpass[2], &state->highpass[2],
z2, &out[2], &out[3]);
}
#if CONFIG_FORMAT_S16LE
static void crossover_s16_default_pass(struct comp_data *cd,
struct input_stream_buffer *bsource,
struct output_stream_buffer **bsinks,
int32_t num_sinks,
uint32_t frames)
{
const struct audio_stream *sink_stream;
const struct audio_stream *source_stream = bsource->data;
int16_t *x;
int32_t *y;
int i, j;
int n = audio_stream_get_channels(source_stream) * frames;
for (i = 0; i < n; i++) {
x = audio_stream_read_frag_s16(source_stream, i);
for (j = 0; j < num_sinks; j++) {
if (!bsinks[j])
continue;
sink_stream = bsinks[j]->data;
y = audio_stream_write_frag_s16(sink_stream, i);
*y = *x;
}
}
}
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE
static void crossover_s32_default_pass(struct comp_data *cd,
struct input_stream_buffer *bsource,
struct output_stream_buffer **bsinks,
int32_t num_sinks,
uint32_t frames)
{
const struct audio_stream *sink_stream;
const struct audio_stream *source_stream = bsource->data;
int32_t *x, *y;
int i, j;
int n = audio_stream_get_channels(source_stream) * frames;
for (i = 0; i < n; i++) {
x = audio_stream_read_frag_s32(source_stream, i);
for (j = 0; j < num_sinks; j++) {
if (!bsinks[j])
continue;
sink_stream = bsinks[j]->data;
y = audio_stream_write_frag_s32(sink_stream, i);
*y = *x;
}
}
}
#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */
#if CONFIG_FORMAT_S16LE
static void crossover_s16_default(struct comp_data *cd,
struct input_stream_buffer *bsource,
struct output_stream_buffer **bsinks,
int32_t num_sinks,
uint32_t frames)
{
struct crossover_state *state;
const struct audio_stream *source_stream = bsource->data;
struct audio_stream *sink_stream;
int16_t *x, *y;
int ch, i, j;
int idx;
int nch = audio_stream_get_channels(source_stream);
int32_t out[num_sinks];
for (ch = 0; ch < nch; ch++) {
idx = ch;
state = &cd->state[ch];
for (i = 0; i < frames; i++) {
x = audio_stream_read_frag_s16(source_stream, idx);
cd->crossover_split(*x << 16, out, state);
for (j = 0; j < num_sinks; j++) {
if (!bsinks[j])
continue;
sink_stream = bsinks[j]->data;
y = audio_stream_write_frag_s16(sink_stream,
idx);
*y = sat_int16(Q_SHIFT_RND(out[j], 31, 15));
}
idx += nch;
}
}
}
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE
static void crossover_s24_default(struct comp_data *cd,
struct input_stream_buffer *bsource,
struct output_stream_buffer **bsinks,
int32_t num_sinks,
uint32_t frames)
{
struct crossover_state *state;
const struct audio_stream *source_stream = bsource->data;
struct audio_stream *sink_stream;
int32_t *x, *y;
int ch, i, j;
int idx;
int nch = audio_stream_get_channels(source_stream);
int32_t out[num_sinks];
for (ch = 0; ch < nch; ch++) {
idx = ch;
state = &cd->state[ch];
for (i = 0; i < frames; i++) {
x = audio_stream_read_frag_s32(source_stream, idx);
cd->crossover_split(*x << 8, out, state);
for (j = 0; j < num_sinks; j++) {
if (!bsinks[j])
continue;
sink_stream = bsinks[j]->data;
y = audio_stream_write_frag_s32(sink_stream,
idx);
*y = sat_int24(Q_SHIFT_RND(out[j], 31, 23));
}
idx += nch;
}
}
}
#endif /* CONFIG_FORMAT_S24LE */
#if CONFIG_FORMAT_S32LE
static void crossover_s32_default(struct comp_data *cd,
struct input_stream_buffer *bsource,
struct output_stream_buffer **bsinks,
int32_t num_sinks,
uint32_t frames)
{
struct crossover_state *state;
const struct audio_stream *source_stream = bsource->data;
struct audio_stream *sink_stream;
int32_t *x, *y;
int ch, i, j;
int idx;
int nch = audio_stream_get_channels(source_stream);
int32_t out[num_sinks];
for (ch = 0; ch < nch; ch++) {
idx = ch;
state = &cd->state[ch];
for (i = 0; i < frames; i++) {
x = audio_stream_read_frag_s32(source_stream, idx);
cd->crossover_split(*x, out, state);
for (j = 0; j < num_sinks; j++) {
if (!bsinks[j])
continue;
sink_stream = bsinks[j]->data;
y = audio_stream_write_frag_s32(sink_stream,
idx);
*y = out[j];
}
idx += nch;
}
}
}
#endif /* CONFIG_FORMAT_S32LE */
const struct crossover_proc_fnmap crossover_proc_fnmap[] = {
/* { SOURCE_FORMAT , PROCESSING FUNCTION } */
#if CONFIG_FORMAT_S16LE
{ SOF_IPC_FRAME_S16_LE, crossover_s16_default },
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE
{ SOF_IPC_FRAME_S24_4LE, crossover_s24_default },
#endif /* CONFIG_FORMAT_S24LE */
#if CONFIG_FORMAT_S32LE
{ SOF_IPC_FRAME_S32_LE, crossover_s32_default },
#endif /* CONFIG_FORMAT_S32LE */
};
const struct crossover_proc_fnmap crossover_proc_fnmap_pass[] = {
/* { SOURCE_FORMAT , PROCESSING FUNCTION } */
#if CONFIG_FORMAT_S16LE
{ SOF_IPC_FRAME_S16_LE, crossover_s16_default_pass },
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE
{ SOF_IPC_FRAME_S24_4LE, crossover_s32_default_pass },
#endif /* CONFIG_FORMAT_S24LE */
#if CONFIG_FORMAT_S32LE
{ SOF_IPC_FRAME_S32_LE, crossover_s32_default_pass },
#endif /* CONFIG_FORMAT_S32LE */
};
const size_t crossover_proc_fncount = ARRAY_SIZE(crossover_proc_fnmap);
const crossover_split crossover_split_fnmap[] = {
crossover_generic_split_2way,
crossover_generic_split_3way,
crossover_generic_split_4way,
};
const size_t crossover_split_fncount = ARRAY_SIZE(crossover_split_fnmap);