forked from DBraun/DawDreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_faust_processor.py
398 lines (276 loc) · 12 KB
/
test_faust_processor.py
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
from dawdreamer_utils import *
BUFFER_SIZE = 1
from itertools import product
@pytest.mark.parametrize("duration,buffer_size", product(
[(44099.5/44100), (44100.5/44100), 1., (4./44100)],
[1, 2, 4, 8, 16, 128, 2048]))
def test_faust_passthrough(duration, buffer_size):
engine = daw.RenderEngine(SAMPLE_RATE, buffer_size)
data = load_audio_file(ASSETS / "575854__yellowtree__d-b-funk-loop.wav", duration=duration)
playback_processor = engine.make_playback_processor("playback", data)
faust_processor = engine.make_faust_processor("faust")
faust_processor.set_dsp_string('process = _, _;')
faust_processor.compile()
# print(faust_processor.get_parameters_description())
graph = [
(playback_processor, []),
(faust_processor, ["playback"])
]
engine.load_graph(graph)
render(engine, duration=duration)
audio = engine.get_audio()
assert data.shape[1] == audio.shape[1]
# todo: shouldn't need to trim one sample like this
min_length = min(audio.shape[1], data.shape[1]) - 1
data = data[:,:min_length]
audio = audio[:,:min_length]
assert np.allclose(data, audio, atol=1e-07)
# do the same for noise
data = np.random.rand(2, int(SAMPLE_RATE*duration))
playback_processor.set_data(data)
render(engine, duration=duration)
audio = engine.get_audio()
assert data.shape[1] == audio.shape[1]
# todo: shouldn't need to trim one sample like this
min_length = min(audio.shape[1], data.shape[1]) - 1
data = data[:,:min_length]
audio = audio[:,:min_length]
assert np.allclose(data, audio, atol=1e-07)
def test_faust_automation_tempo():
duration = 10.
engine = daw.RenderEngine(SAMPLE_RATE, 128)
faust_processor = engine.make_faust_processor("faust")
faust_processor.set_dsp_string(
"""
declare name "MyInstrument";
process = hslider("freq", 440., 0., 15000., 0.) : os.osc : _*.4 <: _, _;
""")
ppqn = 960
automation = make_sine(4, duration, sr=ppqn)
automation = 220.+220.*(automation > 0).astype(np.float32)
faust_processor.set_automation("/MyInstrument/freq", automation, ppqn=ppqn)
# print(faust_processor.get_parameters_description())
graph = [
(faust_processor, [])
]
engine.load_graph(graph)
render(engine, file_path=OUTPUT / 'test_faust_automation_tempo.wav', duration=duration)
@pytest.mark.parametrize("duration,buffer_size", product(
[(44099.5/44100), (44100.5/44100), 1., (4./44100)],
[1, 2, 4, 8, 16, 128, 2048]))
def test_faust_multichannel_in_out(duration, buffer_size):
engine = daw.RenderEngine(SAMPLE_RATE, buffer_size)
numChannels = 9
data = np.sin(np.linspace(0, 4000, num=int(SAMPLE_RATE*duration)))
data = np.stack([data for _ in range(numChannels)])
playback_processor = engine.make_playback_processor("playback", data)
faust_processor = engine.make_faust_processor("faust")
faust_processor.set_dsp_string(f'process = si.bus({numChannels});')
faust_processor.compile()
# print(faust_processor.get_parameters_description())
graph = [
(playback_processor, []),
(faust_processor, ["playback"])
]
engine.load_graph(graph)
render(engine, duration=duration)
audio = engine.get_audio()
# todo: shouldn't need to trim one sample like this
min_length = min(audio.shape[1], data.shape[1]) -1
data = data[:,:min_length]
audio = audio[:,:min_length]
assert(np.allclose(data, audio, atol=1e-07))
def test_faust_sidechain():
"""Have the volume of the drums attenuate the volume of the bass."""
DURATION = 5.1
engine = daw.RenderEngine(SAMPLE_RATE, BUFFER_SIZE)
drums = engine.make_playback_processor("drums",
load_audio_file(ASSETS / "Music Delta - Disco" / "drums.wav", duration=DURATION))
bass = engine.make_playback_processor("bass",
load_audio_file(ASSETS / "Music Delta - Disco" / "bass.wav", duration=DURATION))
dsp_path = abspath(FAUST_DSP / "sidechain.dsp")
faust_processor = engine.make_faust_processor("faust")
faust_processor.set_dsp(dsp_path)
faust_processor.compile()
# print(faust_processor.get_parameters_description())
graph = [
(drums, []),
(bass, []),
(faust_processor, [bass.get_name(), drums.get_name()]),
(engine.make_add_processor("add", [1., 1.]), ["faust", "drums"])
]
engine.load_graph(graph)
render(engine, file_path=OUTPUT / 'test_sidechain_on.wav')
graph = [
(drums, []),
(bass, []),
(engine.make_add_processor("add", [1., 1.]), ["bass", "drums"])
]
engine.load_graph(graph)
render(engine, file_path=OUTPUT / 'test_sidechain_off.wav')
def test_faust_zita_rev1(set_data=False):
engine = daw.RenderEngine(SAMPLE_RATE, BUFFER_SIZE)
data = load_audio_file(ASSETS / "575854__yellowtree__d-b-funk-loop.wav")
playback_processor = engine.make_playback_processor("playback", data)
if set_data:
playback_processor.set_data(data)
dsp_path = abspath(FAUST_DSP / "dm.zita_rev1.dsp")
faust_processor = engine.make_faust_processor("faust")
faust_processor.set_dsp(dsp_path)
faust_processor.set_dsp(dsp_path)
faust_processor.compile()
# print(faust_processor.get_parameters_description())
graph = [
(playback_processor, []),
(faust_processor, ["playback"])
]
engine.load_graph(graph)
render(engine, file_path=OUTPUT / 'test_faust_dm.zita_rev1.wav')
def test_faust_automation():
DURATION = 5.1
engine = daw.RenderEngine(SAMPLE_RATE, BUFFER_SIZE)
drums = engine.make_playback_processor("drums",
load_audio_file(ASSETS / "Music Delta - Disco" / "drums.wav", duration=DURATION))
other = engine.make_playback_processor("other",
load_audio_file(ASSETS / "Music Delta - Disco" / "other.wav", duration=DURATION))
dsp_path = abspath(FAUST_DSP / "two_stereo_inputs_filter.dsp")
faust_processor = engine.make_faust_processor("faust")
faust_processor.set_dsp(dsp_path)
faust_processor.compile()
# print(faust_processor.get_parameters_description())
faust_processor.set_parameter("/MyEffect/cutoff", 7000.0) # Change the cutoff frequency.
assert(faust_processor.get_parameter("/MyEffect/cutoff") == 7000.)
# or set automation like this
faust_processor.set_automation("/MyEffect/cutoff", 10000+9000*make_sine(2, DURATION))
graph = [
(drums, []),
(other, []),
(faust_processor, [drums.get_name(), other.get_name()])
]
engine.load_graph(graph)
render(engine, file_path=OUTPUT / 'test_faust_automation.wav')
def test_faust_add():
"""
This example isn't meant to sound meaningful. It just demonstrates taking a single
input ("drums"), passing it to multiple other processors, and then mixing those
processors into a single stereo out. It's meaningful to test a system
that has more internal channels (8) than inputs (2) or outputs (2).
"""
DURATION = 5.1
engine = daw.RenderEngine(SAMPLE_RATE, BUFFER_SIZE)
drums = engine.make_playback_processor("drums",
load_audio_file(ASSETS / "Music Delta - Disco" / "drums.wav", duration=DURATION))
def get_filter(i):
processor = engine.make_faust_processor(f"filter{i}")
assert processor.set_dsp_string("""
declare name "MyFilter";
process = sp.stereoize(fi.lowpass(2, 10000.));
""")
assert processor.compile()
assert processor.get_num_input_channels() == 2
assert processor.get_num_output_channels() == 2
return processor
N_INPUTS = 4
faust_processor = engine.make_faust_processor("faust")
assert(faust_processor.set_dsp_string(
f"""
declare name "MyEQ";
process = par(i, {N_INPUTS},
sp.stereoize(hslider("gain%i", 1., 0., 1., 0.01)*_)
) :> _, _;
"""))
assert(faust_processor.compile())
print(faust_processor.code)
assert(faust_processor.get_num_input_channels() == N_INPUTS*2)
assert(faust_processor.get_num_output_channels() == 2)
print(faust_processor.get_parameters_description())
faust_processor.set_parameter("/MyEQ/gain0", .1)
faust_processor.set_parameter("/MyEQ/gain1", .1)
faust_processor.set_parameter("/MyEQ/gain2", .1)
faust_processor.set_parameter("/MyEQ/gain3", .1)
assert(np.isclose(faust_processor.get_parameter("/MyEQ/gain0"), .1))
assert(np.isclose(faust_processor.get_parameter("/MyEQ/gain1"), .1))
assert(np.isclose(faust_processor.get_parameter("/MyEQ/gain2"), .1))
assert(np.isclose(faust_processor.get_parameter("/MyEQ/gain3"), .1))
# faust_processor.set_automation("/MyEQ/gain0", .5+.5*make_sine(2, DURATION))
graph = [
(drums, []),
(get_filter(0), ["drums"]),
(get_filter(1), ["drums"]),
(get_filter(2), ["drums"]),
(get_filter(3), ["drums"]),
(faust_processor, ["filter0", "filter1", "filter2", "filter3"])
]
assert(engine.load_graph(graph))
assert(engine.render(5.))
output = engine.get_audio()
wavfile.write(OUTPUT / 'test_faust_add.wav', SAMPLE_RATE, output.transpose())
def test_faust_ambisonics_encoding(ambisonics_order=2, set_data=False):
engine = daw.RenderEngine(SAMPLE_RATE, BUFFER_SIZE)
data = load_audio_file(ASSETS / "575854__yellowtree__d-b-funk-loop.wav")
data = data.mean(axis=0, keepdims=True)
assert(data.ndim == 2)
assert(data.shape[0] == 1)
playback_processor = engine.make_playback_processor("playback", data)
if set_data:
playback_processor.set_data(data)
faust_processor = engine.make_faust_processor("faust")
dsp_code = f"""
import("stdfaust.lib");
L = {ambisonics_order};
encoder3Dxyz(n, x, tx, ty, tz) = ho.encoder3D(n, signal, angle, elevation)
with {{
max_gain = 10.; // todo: user can pick this.
// 10 indicates don't multiply the signal by more than 10 even if it's very close in 3D
xz_square = tx*tx+tz*tz;
signal = x / max(1./max_gain, sqrt(xz_square+ty*ty));
angle = atan2(tx, -tz);
elevation = atan2(ty, sqrt(xz_square));
}};
// todo: ma.EPSILON doesn't work on Linux?
eps = .00001;
//eps = ma.EPSILON;
process(sig) = encoder3Dxyz(L, sig,
hslider("tx", 1., -10000., 10000., eps),
hslider("ty", 1., -10000., 10000., eps),
hslider("tz", 1., -10000., 10000., eps)
);
"""
faust_processor.set_dsp_string(dsp_code)
assert(faust_processor.compile())
# print(faust_processor.get_parameters_description())
assert(faust_processor.get_num_input_channels() == 1)
assert(faust_processor.get_num_output_channels() == (ambisonics_order+1)**2)
graph = [
(playback_processor, []),
(faust_processor, ["playback"])
]
assert(engine.load_graph(graph))
render(engine, file_path=OUTPUT / 'test_faust_ambisonics_encoding.wav')
def test_faust_library_extra():
DURATION = 5.1
engine = daw.RenderEngine(SAMPLE_RATE, BUFFER_SIZE)
data = load_audio_file(ASSETS / "575854__yellowtree__d-b-funk-loop.wav", duration=DURATION)
playback_processor = engine.make_playback_processor("playback", data)
faust_processor = engine.make_faust_processor("faust")
# The point of this test is that if we don't specify the faust_libraries_path,
# then my_hidden_library.lib won't be found and it will fail to compile
faust_processor.faust_libraries_path = abspath(FAUST_DSP)
assert(faust_processor.set_dsp_string(
"""
import("my_hidden_library.lib");
process = _, _ : my_hidden_stereo_effect;
"""))
assert(faust_processor.compile())
# print(faust_processor.get_parameters_description())
graph = [
(playback_processor, []),
(faust_processor, ["playback"])
]
assert(engine.load_graph(graph))
render(engine, file_path=OUTPUT / 'test_faust_library_extra.wav')
# check that it's non-silent
audio = engine.get_audio()
assert(np.mean(np.abs(audio)) > .05)
# if __name__ == '__main__':
# test_faust_library_extra()