-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathHIIRDesigner.cpp
More file actions
420 lines (378 loc) · 14.5 KB
/
HIIRDesigner.cpp
File metadata and controls
420 lines (378 loc) · 14.5 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// SPDX-License-Identifier: BSD-2-Clause
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include <hiir/PolyphaseIir2Designer.h>
#include <vector>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using FD = hiir::PolyphaseIir2Designer;
struct Stage {
int factor;
double tbw;
int nbr_coefs;
std::unique_ptr<double[]> coefs;
};
static std::vector<Stage> calculate_stages(int oversampling, double attenuation, double transition);
static void generate_cpp_prologue(int argc, char *argv[]);
static void generate_cpp_epilogue();
static void generate_cpp_coefs(const Stage *stages, int num_stages);
static void generate_cpp_upsampler(const Stage *stages, int num_stages);
static void generate_cpp_downsampler(const Stage *stages, int num_stages);
int main(int argc, char *argv[])
{
double attenuation = 0.0;
double transition = 0.0;
int oversampling = 16;
bool have_a = false;
bool have_t = false;
for (int argi = 1; argi < argc; ++argi) {
const char *arg = argv[argi];
if (!strcmp(arg, "-a")) {
if (++argi >= argc) {
fprintf(stderr, "The option %s expects a value.\n", arg);
return 1;
}
arg = argv[argi];
attenuation = atof(arg);
have_a = true;
}
else if (!strcmp(arg, "-t")) {
if (++argi >= argc) {
fprintf(stderr, "The option %s expects a value.\n", arg);
return 1;
}
arg = argv[argi];
transition = atof(arg);
have_t = true;
}
else if (!strcmp(arg, "-o")) {
if (++argi >= argc) {
fprintf(stderr, "The option %s expects a value.\n", arg);
return 1;
}
arg = argv[argi];
oversampling = atoi(arg);
}
else {
fprintf(stderr, "Unrecognized argument: %s\n", arg);
return 1;
}
}
if (!have_a) {
fprintf(stderr, "No attenuation given (-a)\n");
return 1;
}
if (!have_t) {
fprintf(stderr, "No transition bandwidth given (-t)\n");
return 1;
}
else if (attenuation < 0.0) {
fprintf(stderr, "Invalid attenuation\n");
return 1;
}
else if (transition <= 0.0 || transition >= 0.5) {
fprintf(stderr, "Invalid transition bandwidth\n");
return 1;
}
else if (oversampling < 2) {
fprintf(stderr, "Invalid oversampling\n");
return 1;
}
std::vector<Stage> stages = calculate_stages(oversampling, attenuation, transition);
int num_stages = (int)stages.size();
// generate the coeffs
generate_cpp_prologue(argc, argv);
printf("\n");
generate_cpp_coefs(stages.data(), num_stages);
printf("\n");
generate_cpp_upsampler(stages.data(), num_stages);
printf("\n");
generate_cpp_downsampler(stages.data(), num_stages);
printf("\n");
generate_cpp_epilogue();
return 0;
}
static std::vector<Stage> calculate_stages(int oversampling, double attenuation, double transition)
{
std::vector<Stage> stages;
stages.reserve(8);
bool done = false;
for (int num_stage = 0; !done; ++num_stage) {
if (num_stage > 0)
printf("\n");
Stage stage;
stage.factor = 2 << num_stage;
stage.tbw = transition *
std::pow(0.5, num_stage) + 0.5 * (1 - std::pow(0.5, num_stage));
stage.nbr_coefs = FD::compute_nbr_coefs_from_proto(attenuation, stage.tbw);
double *coefs = new double[stage.nbr_coefs]{};
stage.coefs.reset(coefs);
FD::compute_coefs(coefs, attenuation, stage.tbw);
done = stage.factor >= oversampling;
stages.push_back(std::move(stage));
}
return stages;
}
static void generate_cpp_prologue(int argc, char *argv[])
{
printf("//------------------------------------------------------------------------------\n");
printf("// This is generated by the Sfizz HIIR designer\n");
printf("// Using options:");
for (int i = 1; i < argc; ++i)
printf(" %s", argv[i]);
printf("\n");
printf("//------------------------------------------------------------------------------\n");
printf("\n");
printf(
"#pragma once\n"
"#include \"OversamplerHelpers.h\"\n"
"#include \"MathHelpers\"\n"
"\n"
"namespace sfz {\n"
);
}
static void generate_cpp_epilogue()
{
printf("} // namespace sfz\n");
}
static void generate_cpp_coefs(const Stage *stages, int num_stages)
{
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[i];
const double *coefs = stage.coefs.get();
printf("// %dx <-> %dx: TBW = %g\n", stage.factor, stage.factor / 2, stage.tbw);
printf("static constexpr double OSCoeffs%dx[%d] = {\n", stage.factor, stage.nbr_coefs);
for (int i = 0; i < stage.nbr_coefs; ++i) {
printf("\t" "%.18f,\n", coefs[i]);
}
printf("};\n");
}
}
static void generate_cpp_upsampler(const Stage *stages, int num_stages)
{
printf("class Upsampler {\n");
printf("public:\n");
printf("\t" "Upsampler()\n");
printf("\t" "{\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[i];
printf("\t\t" "up%d_.set_coefs(OSCoeffs%dx);\n", stage.factor, stage.factor);
}
printf("\t" "}\n");
printf("\t" "void clear()\n");
printf("\t" "{\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[i];
printf("\t\t" "up%d_.clear_buffers();\n", stage.factor);
}
printf("\t" "}\n");
printf("\t" "static int recommendedBuffer(int factor, int spl)\n");
printf("\t" "{\n");
printf("\t\t" "switch (factor) {\n");
printf("\t\t" "case 2:\n");
printf("\t\t\t" "return 0;\n");
printf("\t\t" "case 4:\n");
printf("\t\t\t" "return 2 * spl;\n");
printf("\t\t" "default:\n");
printf("\t\t\t" "return factor * spl;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
printf("\t" "static unsigned conversionFactor(double sourceRate, double targetRate)\n");
printf("\t" "{\n");
printf("\t\t" "int factor = static_cast<int>(std::ceil(targetRate / sourceRate));\n");
printf("\t\t" "factor = (factor > 1) ? factor : 1;\n");
printf("\t\t" "factor = (factor < 128) ? factor : 128;\n");
printf("\t\t" "return nextPow2(factor);\n");
printf("\t" "}\n");
printf("\t" "static bool canProcess(int factor)\n");
printf("\t" "{\n");
printf("\t\t" "switch (factor) {\n");
printf("\t\t" "case 1:\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[i];
printf("\t\t" "case %d:\n", stage.factor);
}
printf("\t\t\t" "return true;\n");
printf("\t\t" "default:\n");
printf("\t\t\t" "return false;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
printf("\t" "void process(int factor, const float *in, float *out, int spl, float *temp, int ntemp)\n");
printf("\t" "{\n");
printf("\t\t" "switch (factor) {\n");
printf("\t\t" "case 1:\n");
printf("\t\t\t" "if (in != out) std::memcpy(out, in, spl * sizeof(float));\n");
printf("\t\t\t" "break;\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[i];
printf("\t\t" "case %d:\n", stage.factor);
printf("\t\t\t" "process%dx(in, out, spl, temp, ntemp);\n", stage.factor);
printf("\t\t\t" "break;\n");
}
printf("\t\t" "default:\n");
printf("\t\t\t" "ASSERTFALSE;\n");
printf("\t\t\t" "break;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
for (int n = 1; n <= num_stages; ++n) {
// special case factor=2, buffer not required
if (stages[n - 1].factor == 2) {
printf("\t" "void process2x(const float *in, float *out, int spl, float * = nullptr, int = 0)\n");
printf("\t" "{\n");
printf("\t\t" "up2_.process_block(out, in, spl);\n");
printf("\t" "}\n");
continue;
}
printf("\t" "void process%dx(const float *in, float *out, int spl, float *temp, int ntemp)\n", stages[n - 1].factor);
printf("\t" "{\n");
// special case factor=4, only 1 buffer required
if (stages[n - 1].factor > 4)
printf("\t\t" "int maxspl = ntemp / %d;\n", stages[n - 1].factor);
else
printf("\t\t" "int maxspl = ntemp / %d;\n", stages[n - 1].factor / 2);
printf("\t\t" "ASSERT(maxspl > 0);\n");
printf("\t\t" "float *t1 = temp;\n");
if (stages[n - 1].factor > 4)
printf("\t\t" "float *t2 = temp + %d * maxspl;\n", stages[n - 1].factor / 2);
printf("\t\t" "while (spl > 0) {\n");
printf("\t\t\t" "int curspl = (spl < maxspl) ? spl : maxspl;\n");
for (int i = 0; i < n; ++i) {
const Stage &stage = stages[i];
const char *tempnames[] = {"t1", "t2"};
const char *outname = tempnames[i & 1];
const char *inname = tempnames[1 - (i & 1)];
if (i == 0)
inname = "in";
if (i + 1 == n)
outname = "out";
printf("\t\t\t" "up%d_.process_block(%s, %s, %d * curspl);\n", stage.factor, outname, inname, stage.factor / 2);
}
printf("\t\t\t" "in += curspl;\n");
printf("\t\t\t" "out += curspl;\n");
printf("\t\t\t" "spl -= curspl;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
}
printf("private:\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[i];
printf("\t" "hiir::Upsampler2x<%d> up%d_;\n", stage.nbr_coefs, stage.factor);
}
printf("};\n");
}
static void generate_cpp_downsampler(const Stage *stages, int num_stages)
{
printf("class Downsampler {\n");
printf("public:\n");
printf("\t" "Downsampler()\n");
printf("\t" "{\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[num_stages - 1 - i];
printf("\t\t" "down%d_.set_coefs(OSCoeffs%dx);\n", stage.factor, stage.factor);
}
printf("\t" "}\n");
printf("\t" "void clear()\n");
printf("\t" "{\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[num_stages - 1 - i];
printf("\t\t" "down%d_.clear_buffers();\n", stage.factor);
}
printf("\t" "}\n");
printf("\t" "static int recommendedBuffer(int factor, int spl)\n");
printf("\t" "{\n");
printf("\t\t" "switch (factor) {\n");
printf("\t\t" "case 2:\n");
printf("\t\t\t" "return 0;\n");
printf("\t\t" "case 4:\n");
printf("\t\t\t" "return 2 * spl;\n");
printf("\t\t" "default:\n");
printf("\t\t\t" "return factor * spl;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
printf("\t" "static unsigned conversionFactor(double sourceRate, double targetRate)\n");
printf("\t" "{\n");
printf("\t\t" "int factor = static_cast<int>(std::ceil(targetRate / sourceRate));\n");
printf("\t\t" "factor = (factor > 1) ? factor : 1;\n");
printf("\t\t" "factor = (factor < 128) ? factor : 128;\n");
printf("\t\t" "return nextPow2(factor);\n");
printf("\t" "}\n");
printf("\t" "static bool canProcess(int factor)\n");
printf("\t" "{\n");
printf("\t\t" "switch (factor) {\n");
printf("\t\t" "case 1:\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[i];
printf("\t\t" "case %d:\n", stage.factor);
}
printf("\t\t\t" "return true;\n");
printf("\t\t" "default:\n");
printf("\t\t\t" "return false;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
printf("\t" "void process(int factor, const float *in, float *out, int spl, float *temp, int ntemp)\n");
printf("\t" "{\n");
printf("\t\t" "switch (factor) {\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[num_stages - 1 - i];
printf("\t\t" "case %d:\n", stage.factor);
printf("\t\t\t" "process%dx(in, out, spl, temp, ntemp);\n", stage.factor);
printf("\t\t\t" "break;\n");
}
printf("\t\t" "case 1:\n");
printf("\t\t\t" "if (in != out) std::memcpy(out, in, spl * sizeof(float));\n");
printf("\t\t\t" "break;\n");
printf("\t\t" "default:\n");
printf("\t\t\t" "ASSERTFALSE;\n");
printf("\t\t\t" "break;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
for (int n = 1; n <= num_stages; ++n) {
// special case factor=2, buffer not required
if (stages[n - 1].factor == 2) {
printf("\t" "void process2x(const float *in, float *out, int spl, float * = nullptr, int = 0)\n");
printf("\t" "{\n");
printf("\t\t" "down2_.process_block(out, in, spl);\n");
printf("\t" "}\n");
continue;
}
printf("\t" "void process%dx(const float *in, float *out, int spl, float *temp, int ntemp)\n", stages[n - 1].factor);
printf("\t" "{\n");
// special case factor=4, only 1 buffer required
if (stages[n - 1].factor > 4)
printf("\t\t" "int maxspl = ntemp / %d;\n", stages[n - 1].factor);
else
printf("\t\t" "int maxspl = ntemp / %d;\n", stages[n - 1].factor / 2);
printf("\t\t" "ASSERT(maxspl > 0);\n");
printf("\t\t" "float *t1 = temp;\n");
if (stages[n - 1].factor > 4)
printf("\t\t" "float *t2 = temp + %d * maxspl;\n", stages[n - 1].factor / 2);
printf("\t\t" "while (spl > 0) {\n");
printf("\t\t\t" "int curspl = (spl < maxspl) ? spl : maxspl;\n");
for (int i = 0; i < n; ++i) {
const Stage &stage = stages[n - 1 - i];
const char *tempnames[] = {"t1", "t2"};
const char *outname = tempnames[i & 1];
const char *inname = tempnames[1 - (i & 1)];
if (i == 0)
inname = "in";
if (i + 1 == n)
outname = "out";
printf("\t\t\t" "down%d_.process_block(%s, %s, %d * curspl);\n", stage.factor, outname, inname, stage.factor / 2);
}
printf("\t\t\t" "in += curspl;\n");
printf("\t\t\t" "out += curspl;\n");
printf("\t\t\t" "spl -= curspl;\n");
printf("\t\t" "}\n");
printf("\t" "}\n");
}
printf("private:\n");
for (int i = 0; i < num_stages; ++i) {
const Stage &stage = stages[num_stages - 1 - i];
printf("\t" "hiir::Downsampler2x<%d> down%d_;\n", stage.nbr_coefs, stage.factor);
}
printf("};\n");
}