-
Notifications
You must be signed in to change notification settings - Fork 13
/
virtual_eq.c
226 lines (189 loc) · 5.47 KB
/
virtual_eq.c
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
/*-
* Copyright (c) 2021 Hans Petter Selasky
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/queue.h>
#include "virtual_int.h"
void
vclient_tx_equalizer(struct virtual_client *pvc,
int64_t *src, size_t total)
{
double *f_data;
size_t channels;
size_t f_size;
size_t x;
f_size = pvc->profile->tx_filter_size;
if (f_size == 0 || total == 0)
return;
channels = pvc->channels;
total /= channels;
while (1) {
size_t delta;
size_t offset;
size_t y;
offset = pvc->tx_filter_offset;
delta = f_size - offset;
if (delta > total)
delta = total;
for (x = 0; x != channels; x++) {
f_data = pvc->profile->tx_filter_data[x];
if (f_data == NULL)
continue;
for (y = 0; y != delta; y++) {
pvc->tx_filter_in[x][y + offset] = src[x + y * channels];
src[x + y * channels] = pvc->tx_filter_out[x][y + offset];
}
}
pvc->tx_filter_offset += delta;
total -= delta;
src += delta * channels;
/* check if there is enough data for a new transform */
if (pvc->tx_filter_offset == f_size) {
for (x = 0; x != channels; x++) {
f_data = pvc->profile->tx_filter_data[x];
if (f_data == NULL)
continue;
/* shift down output */
for (y = 0; y != f_size; y++) {
pvc->tx_filter_out[x][y] = pvc->tx_filter_out[x][y + f_size];
pvc->tx_filter_out[x][y + f_size] = 0;
}
/* perform transform */
voss_x3_multiply_double(pvc->tx_filter_in[x],
f_data, pvc->tx_filter_out[x], f_size);
}
pvc->tx_filter_offset = 0;
}
if (total == 0)
break;
}
}
void
vclient_rx_equalizer(struct virtual_client *pvc,
int64_t *src, size_t total)
{
double *f_data;
size_t channels;
size_t f_size;
size_t x;
f_size = pvc->profile->rx_filter_size;
if (f_size == 0 || total == 0)
return;
channels = pvc->channels;
total /= channels;
while (1) {
size_t delta;
size_t offset;
size_t y;
offset = pvc->rx_filter_offset;
delta = f_size - offset;
if (delta > total)
delta = total;
for (x = 0; x != channels; x++) {
f_data = pvc->profile->rx_filter_data[x];
if (f_data == NULL)
continue;
for (y = 0; y != delta; y++) {
pvc->rx_filter_in[x][y + offset] = src[x + y * channels];
src[x + y * channels] = pvc->rx_filter_out[x][y + offset];
}
}
pvc->rx_filter_offset += delta;
total -= delta;
src += delta * channels;
/* check if there is enough data for a new transform */
if (pvc->rx_filter_offset == f_size) {
for (x = 0; x != channels; x++) {
f_data = pvc->profile->rx_filter_data[x];
if (f_data == NULL)
continue;
/* shift output down */
for (y = 0; y != f_size; y++) {
pvc->rx_filter_out[x][y] = pvc->rx_filter_out[x][y + f_size];
pvc->rx_filter_out[x][y + f_size] = 0;
}
/* perform transform */
voss_x3_multiply_double(pvc->rx_filter_in[x],
f_data, pvc->rx_filter_out[x], f_size);
}
pvc->rx_filter_offset = 0;
}
if (total == 0)
break;
}
}
int
vclient_eq_alloc(struct virtual_client *pvc)
{
uint8_t x;
pvc->tx_filter_offset = 0;
pvc->rx_filter_offset = 0;
for (x = 0; x != pvc->channels; x++) {
uint32_t size;
size = pvc->profile->tx_filter_size;
if (size != 0) {
pvc->tx_filter_in[x] =
malloc(sizeof(pvc->tx_filter_in[x][0]) * size);
pvc->tx_filter_out[x] =
calloc(2 * size, sizeof(pvc->tx_filter_out[x][0]));
if (pvc->tx_filter_in[x] == NULL ||
pvc->tx_filter_out[x] == NULL)
goto error;
}
size = pvc->profile->rx_filter_size;
if (size != 0) {
pvc->rx_filter_in[x] =
malloc(sizeof(pvc->rx_filter_in[x][0]) * size);
pvc->rx_filter_out[x] =
calloc(2 * size, sizeof(pvc->rx_filter_out[x][0]));
if (pvc->rx_filter_in[x] == NULL ||
pvc->rx_filter_out[x] == NULL)
goto error;
}
}
return (0);
error:
vclient_eq_free(pvc);
return (ENOMEM);
}
void
vclient_eq_free(struct virtual_client *pvc)
{
uint8_t x;
pvc->tx_filter_offset = 0;
pvc->rx_filter_offset = 0;
for (x = 0; x != VMAX_CHAN; x++) {
free(pvc->tx_filter_in[x]);
pvc->tx_filter_in[x] = NULL;
free(pvc->rx_filter_in[x]);
pvc->rx_filter_in[x] = NULL;
free(pvc->tx_filter_out[x]);
pvc->tx_filter_out[x] = NULL;
free(pvc->rx_filter_out[x]);
pvc->rx_filter_out[x] = NULL;
}
}