forked from spotify/pedalboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_external_plugins.py
950 lines (772 loc) Β· 34.2 KB
/
test_external_plugins.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#! /usr/bin/env python
#
# Copyright 2021 Spotify AB
#
# Licensed under the GNU Public License, Version 3.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.gnu.org/licenses/gpl-3.0.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import time
import math
import psutil
import atexit
import random
import shutil
import platform
import subprocess
from glob import glob
from pathlib import Path
import mido
from pedalboard._pedalboard import (
WrappedBool,
strip_common_float_suffixes,
normalize_python_parameter_name,
)
import pytest
import pedalboard
import numpy as np
from typing import Optional
TEST_EFFECT_PLUGIN_BASE_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "plugins", "effect"
)
TEST_INSTRUMENT_PLUGIN_BASE_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "plugins", "instrument"
)
TEST_PRESET_BASE_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "presets")
TEST_MIDI_BASE_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "midi")
AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT = [
os.path.basename(filename)
for filename in glob(os.path.join(TEST_EFFECT_PLUGIN_BASE_PATH, platform.system(), "*"))
]
AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT = [
os.path.basename(filename)
for filename in glob(os.path.join(TEST_INSTRUMENT_PLUGIN_BASE_PATH, platform.system(), "*"))
]
# Disable Audio Unit tests on GitHub Actions, as the
# action container fails to load Audio Units:
if os.getenv("CIBW_TEST_REQUIRES") or os.getenv("CI"):
AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT = [
f for f in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT if "component" not in f
]
AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT = [
f for f in AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT if "component" not in f
]
AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT = (
AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT + AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT
)
def is_container_plugin(filename: str):
for klass in pedalboard._AVAILABLE_PLUGIN_CLASSES:
try:
return len(klass.get_plugin_names_for_file(filename)) > 1
except ImportError:
pass
return False
AVAILABLE_CONTAINER_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT = [
filename
for filename in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
if is_container_plugin(filename)
]
def get_parameters(plugin_filename: str):
return load_test_plugin(plugin_filename).parameters
TEST_PLUGIN_CACHE = {}
TEST_PLUGIN_ORIGINAL_PARAMETER_CACHE = {}
PLUGIN_FILES_TO_DELETE = set()
MACOS_PLUGIN_INSTALL_PATH = Path.home() / "Library" / "Audio" / "Plug-Ins" / "Components"
def find_plugin_path(plugin_filename: str) -> str:
plugin_path = os.path.join(TEST_EFFECT_PLUGIN_BASE_PATH, platform.system(), plugin_filename)
if not os.path.exists(plugin_path):
plugin_path = os.path.join(
TEST_INSTRUMENT_PLUGIN_BASE_PATH, platform.system(), plugin_filename
)
if not os.path.exists(plugin_path):
raise ValueError(f"Failed to find plugin named {plugin_path}!")
return plugin_path
def load_test_plugin(plugin_filename: str, disable_caching: bool = False, *args, **kwargs):
"""
Load a plugin file from disk, or use an existing instance to save
on test runtime if we already have one in memory.
The plugin filename is used here rather than the fully-qualified path
to reduce line length in the pytest output.
"""
key = repr((plugin_filename, args, tuple(kwargs.items())))
if key not in TEST_PLUGIN_CACHE or disable_caching:
plugin_path = find_plugin_path(plugin_filename)
if platform.system() == "Darwin" and plugin_filename.endswith(".component"):
# On macOS, AudioUnit components must be installed in
# ~/Library/Audio/Plug-Ins/Components in order to be loaded.
installed_plugin_path = os.path.join(MACOS_PLUGIN_INSTALL_PATH, plugin_filename)
plugin_already_installed = os.path.exists(installed_plugin_path)
if not plugin_already_installed:
shutil.copytree(plugin_path, installed_plugin_path)
PLUGIN_FILES_TO_DELETE.add(installed_plugin_path)
plugin_path = installed_plugin_path
# Try to load a given plugin multiple times if necessary.
# Unsure why this is necessary, but it seems this only happens in test.
exception = None
for attempt in range(5):
try:
plugin = pedalboard.load_plugin(plugin_path, *args, **kwargs)
break
except ImportError as e:
exception = e
time.sleep(attempt)
else:
raise exception
if disable_caching:
return plugin
TEST_PLUGIN_CACHE[key] = plugin
TEST_PLUGIN_ORIGINAL_PARAMETER_CACHE[key] = {
key: getattr(plugin, key) for key in plugin.parameters.keys()
}
# Try to reset default parameters when loading:
plugin = TEST_PLUGIN_CACHE[key]
for name in plugin.parameters.keys():
try:
setattr(plugin, name, TEST_PLUGIN_ORIGINAL_PARAMETER_CACHE[key][name])
except ValueError:
pass
# Force a reset:
plugin.reset()
return plugin
def delete_installed_plugins():
for plugin_file in PLUGIN_FILES_TO_DELETE:
shutil.rmtree(plugin_file)
atexit.register(delete_installed_plugins)
# Allow testing with all of the plugins on the local machine:
if os.environ.get("ENABLE_TESTING_WITH_LOCAL_PLUGINS", False):
for plugin_class in pedalboard._AVAILABLE_PLUGIN_CLASSES:
for plugin_path in plugin_class.installed_plugins:
try:
load_test_plugin(plugin_path)
AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT.append(plugin_path)
except Exception as e:
print(f"Tried to load {plugin_path} for local testing, but failed with: {e}")
# Even if the plugin failed to load, add it to
# the list of known container plugins if necessary:
if is_container_plugin(plugin_path):
AVAILABLE_CONTAINER_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT.append(plugin_path)
def plugin_named(*substrings: str) -> Optional[str]:
"""
Return the first plugin filename that contains all of the
provided substrings from the list of available test plugins.
"""
for plugin_filename in AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT:
if all([s.lower() in plugin_filename.lower() for s in substrings]):
return plugin_filename
def max_volume_of(x: np.ndarray) -> float:
return np.amax(np.abs(x))
def test_at_least_one_plugin_is_available_for_testing():
assert AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
@pytest.mark.parametrize(
"value,expected",
[
("nope", "nope"),
("10.5x", "10.5"),
("12%", "12"),
("123 Hz", "123"),
("123.45 Hz", "123.45"),
("123.45 kHz", "123450.0"),
("kHz", "kHz"),
(1.23, 1.23),
("one thousand kHz", "one thousand kHz"),
],
)
def test_strip_common_float_suffixes(value, expected):
actual = strip_common_float_suffixes(value)
assert actual == expected
@pytest.mark.parametrize("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_at_least_one_parameter(plugin_filename: str):
"""
Many tests below are parametrized on the parameters of the plugin;
if our parameter parsing code fails, those tests won't fail as
there will just be no code to run. This works around that problem.
"""
assert get_parameters(plugin_filename)
@pytest.mark.parametrize(
"plugin_filename,plugin_preset",
[
(plugin, os.path.join(TEST_PRESET_BASE_PATH, plugin + ".vstpreset"))
for plugin in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
if os.path.isfile(os.path.join(TEST_PRESET_BASE_PATH, plugin + ".vstpreset"))
],
)
def test_preset_parameters(plugin_filename: str, plugin_preset: str):
# plugin with default params.
plugin = load_test_plugin(plugin_filename)
default_params = {k: v.raw_value for k, v in plugin.parameters.items() if v.type == float}
# load preset file
plugin.load_preset(plugin_preset)
for name, default in default_params.items():
actual = getattr(plugin, name)
if math.isnan(actual):
continue
assert (
actual != default
), f"Expected attribute {name} to be different from default ({default}), but was {actual}"
@pytest.mark.parametrize("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_initial_parameters(plugin_filename: str):
parameters = {
k: v.min_value for k, v in get_parameters(plugin_filename).items() if v.type == float
}
# Reload the plugin, but set the initial parameters in the load call.
plugin = load_test_plugin(plugin_filename, parameter_values=parameters)
for name, expected in parameters.items():
actual = getattr(plugin, name)
if math.isnan(actual):
continue
assert actual == expected, f"Expected attribute {name} to be {expected}, but was {actual}"
@pytest.mark.parametrize(
"plugin_filename,parameter_name",
[
(path, parameter)
for path in AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT
for parameter in [k for k, v in get_parameters(path).items() if v.type == float]
],
)
def test_initial_parameter_validation(plugin_filename: str, parameter_name: str):
plugin = load_test_plugin(plugin_filename)
with pytest.raises(ValueError):
load_test_plugin(
plugin_filename,
parameter_values={parameter_name: getattr(plugin, parameter_name).max_value + 100},
)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_initial_parameter_validation_missing(plugin_filename: str):
with pytest.raises(AttributeError):
load_test_plugin(plugin_filename, parameter_values={"missing_parameter": 123})
@pytest.mark.parametrize("loader", [pedalboard.load_plugin] + pedalboard._AVAILABLE_PLUGIN_CLASSES)
def test_import_error_on_missing_path(loader):
with pytest.raises(ImportError):
loader("./")
@pytest.mark.parametrize("plugin_filename", AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT)
@pytest.mark.parametrize("num_channels,sample_rate", [(2, 48000), (2, 44100), (2, 22050)])
@pytest.mark.parametrize(
"notes",
[
# Accept note-like objects via duck typing:
[
mido.Message("note_on", note=100, velocity=3, time=0),
mido.Message("note_off", note=100, time=5.0),
],
# Accept tuples of MIDI bytes and their timestamps:
[
(mido.Message("note_on", note=100, velocity=3).bytes(), 0),
(mido.Message("note_off", note=100).bytes(), 5),
],
# Accept bytes objects representing MIDI messages, plus their timestamps:
[
(bytes(mido.Message("note_on", note=100, velocity=3).bytes()), 0),
(bytes(mido.Message("note_off", note=100).bytes()), 5),
],
],
)
def test_instrument_plugin_accepts_notes(
plugin_filename: str, num_channels: int, sample_rate: float, notes
):
plugin = load_test_plugin(plugin_filename)
assert plugin.is_instrument
assert not plugin.is_effect
output = plugin(notes, 6.0, sample_rate, num_channels=num_channels)
assert np.amax(np.abs(output)) > 0
@pytest.mark.parametrize("plugin_filename", AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_instrument_plugin_rejects_switched_duration_and_sample_rate(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)
assert plugin.is_instrument
assert not plugin.is_effect
with pytest.raises(ValueError) as e:
plugin([], 44100, 6.0)
assert "reversing the order" in str(e)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_instrument_plugin_rejects_notes_naively_read_from_midi_file(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)
assert plugin.is_instrument
assert not plugin.is_effect
midifile = mido.MidiFile(os.path.join(TEST_MIDI_BASE_PATH, "ascending_chromatic.mid"))
with pytest.raises(ValueError) as e:
plugin(midifile.tracks[0], 6.0, 44100)
assert "timestamp" in str(e)
assert "seconds" in str(e)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
@pytest.mark.parametrize("num_channels,sample_rate", [(2, 48000), (2, 44100), (2, 22050)])
def test_effect_plugin_does_not_accept_notes(
plugin_filename: str, num_channels: int, sample_rate: float
):
notes = [
mido.Message("note_on", note=100, velocity=3, time=0),
mido.Message("note_off", note=100, time=5.0),
]
plugin = load_test_plugin(plugin_filename)
assert plugin.is_effect
assert not plugin.is_instrument
with pytest.raises(ValueError):
plugin(notes, 6.0, sample_rate, num_channels=num_channels)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT)
@pytest.mark.parametrize("num_channels,sample_rate", [(2, 48000), (2, 44100), (2, 22050)])
def test_instrument_plugin_accepts_buffer_size(
plugin_filename: str, num_channels: int, sample_rate: float
):
notes = [
mido.Message("note_on", note=100, velocity=3, time=0),
mido.Message("note_off", note=100, time=5.0),
]
plugin = load_test_plugin(plugin_filename)
assert plugin.is_instrument
assert not plugin.is_effect
outputs = [
plugin(notes, 6.0, sample_rate, num_channels=num_channels, buffer_size=buffer_size)
for buffer_size in [1, 32, 1024]
]
for a, b in zip(outputs, outputs[1:]):
np.testing.assert_allclose(a, b, atol=0.02)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT)
@pytest.mark.parametrize("num_channels,sample_rate", [(2, 48000), (2, 44100), (2, 22050)])
def test_instrument_plugin_does_not_accept_audio(
plugin_filename: str, num_channels: int, sample_rate: float
):
plugin = load_test_plugin(plugin_filename)
assert plugin.is_instrument
assert not plugin.is_effect
with pytest.raises(ValueError):
plugin(np.random.rand(num_channels, sample_rate), sample_rate)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_INSTRUMENT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_pedalboard_does_not_accept_instruments(plugin_filename: str):
with pytest.raises(ValueError):
pedalboard.Pedalboard([load_test_plugin(plugin_filename)])
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
@pytest.mark.parametrize(
"num_channels,sample_rate",
[(1, 48000), (2, 48000), (1, 44100), (2, 44100), (1, 22050), (2, 22050)],
)
def test_effect_plugin_accepts_variable_channel_count(
plugin_filename: str, num_channels: int, sample_rate: float
):
plugin = load_test_plugin(plugin_filename)
assert plugin.is_effect
noise = np.random.rand(num_channels, sample_rate)
try:
effected = plugin(noise, sample_rate)
assert effected.shape == noise.shape
except ValueError as e:
if "does not support" not in str(e):
raise
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_effect_plugin_accepts_variable_channel_count_without_reloading(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)
for num_channels, sample_rate in [
(1, 48000),
(2, 48000),
(1, 44100),
(2, 44100),
(1, 22050),
(2, 22050),
]:
noise = np.random.rand(num_channels, sample_rate)
try:
effected = plugin(noise, sample_rate)
assert effected.shape == noise.shape
except ValueError as e:
if "does not support" not in str(e):
raise
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_all_parameters_are_accessible_as_properties(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)
assert plugin.parameters
for parameter_name in plugin.parameters.keys():
assert hasattr(plugin, parameter_name)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_parameters_cant_be_assigned_to_directly(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)
assert plugin.parameters
for parameter_name in plugin.parameters.keys():
current_value = getattr(plugin, parameter_name)
with pytest.raises(TypeError):
plugin.parameters[parameter_name] = current_value
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_all_parameters_have_accessors(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)
assert plugin.parameters
for parameter_name, parameter in plugin.parameters.items():
assert parameter_name in dir(plugin)
parameter_value = getattr(plugin, parameter_name)
assert hasattr(parameter_value, "raw_value")
assert repr(parameter)
assert isinstance(parameter_value, (float, WrappedBool, str))
assert parameter_value.raw_value == parameter.raw_value
assert parameter_value.range is not None
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_attributes_proxy(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)
# Should be disallowed:
with pytest.raises(AttributeError):
plugin.parameters = "123"
# Should be allowed:
plugin.new_parameter = "123"
@pytest.mark.parametrize(
"plugin_filename,parameter_name",
[
(path, parameter)
for path in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
for parameter in [k for k, v in get_parameters(path).items() if v.type == bool]
],
)
def test_bool_parameters(plugin_filename: str, parameter_name: str):
plugin = load_test_plugin(plugin_filename)
parameter_value = getattr(plugin, parameter_name)
assert repr(parameter_value) in ("True", "False")
assert parameter_value in (True, False)
# Flip the parameter and ensure that it does change:
setattr(plugin, parameter_name, not parameter_value)
assert getattr(plugin, parameter_name) != parameter_value
# Ensure that if we access an attribute that we're not adding to the value,
# we fall back to the underlying type (bool) or we raise an exception if not:
assert hasattr(parameter_value, "bit_length")
with pytest.raises(AttributeError):
parameter_value.something_that_doesnt_exist
@pytest.mark.parametrize(
"plugin_filename,parameter_name",
[
(path, parameter)
for path in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
for parameter in [k for k, v in get_parameters(path).items() if v.type == bool]
],
)
def test_bool_parameter_valdation(plugin_filename: str, parameter_name: str):
plugin = load_test_plugin(plugin_filename)
with pytest.raises(ValueError):
setattr(plugin, parameter_name, 123.4)
@pytest.mark.parametrize(
"plugin_filename,parameter_name",
[
(path, parameter)
for path in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
for parameter in [k for k, v in get_parameters(path).items() if v.type == float]
],
)
def test_float_parameters(plugin_filename: str, parameter_name: str):
plugin = load_test_plugin(plugin_filename)
parameter_value = getattr(plugin, parameter_name)
assert repr(parameter_value) == repr(float(parameter_value))
assert isinstance(parameter_value, float)
# Change the parameter and ensure that it does change:
new_values = parameter_value.valid_values
if parameter_value.step_size is not None:
_min, _max, _ = parameter_value.range
step_size = parameter_value.step_size
new_values = np.arange(_min, _max, step_size)
# Because sometimes np.arange(_min, _max) gives values _slightly outside_
# of [_min, _max] thanks to floating point:
new_values = [max(_min, min(_max, new_value)) for new_value in new_values]
epsilon = parameter_value.step_size or parameter_value.approximate_step_size or 0.001
for new_value in new_values:
setattr(plugin, parameter_name, new_value)
if math.isnan(getattr(plugin, parameter_name)):
continue
assert math.isclose(new_value, getattr(plugin, parameter_name), abs_tol=epsilon * 2.0)
# Ensure that if we access an attribute that we're not adding to the value,
# we fall back to the underlying type (float) or we raise an exception if not:
assert parameter_value.real == float(parameter_value)
with pytest.raises(AttributeError):
parameter_value.something_that_doesnt_exist
@pytest.mark.parametrize(
"plugin_filename,parameter_name",
[
(path, parameter)
for path in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
for parameter in [k for k, v in get_parameters(path).items() if v.type == float]
],
)
def test_float_parameter_valdation(plugin_filename: str, parameter_name: str):
plugin = load_test_plugin(plugin_filename)
parameter = getattr(plugin, parameter_name)
with pytest.raises(ValueError):
setattr(plugin, parameter_name, "not a float")
# Should be allowed to set a float parameter followed by its label:
if parameter.label:
setattr(plugin, parameter_name, f"{parameter} {parameter.label}")
# ...but not by a different label:
if parameter.label != "dB":
with pytest.raises(ValueError):
setattr(plugin, parameter_name, f"{parameter} dB")
max_range = parameter.max_value
with pytest.raises(ValueError):
setattr(plugin, parameter_name, max_range + 100)
min_range = parameter.min_value
with pytest.raises(ValueError):
setattr(plugin, parameter_name, min_range - 100)
@pytest.mark.parametrize(
"plugin_filename,parameter_name",
[
(path, parameter)
for path in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
for parameter in [k for k, v in get_parameters(path).items() if v.type == str]
],
)
def test_str_parameters(plugin_filename: str, parameter_name: str):
plugin = load_test_plugin(plugin_filename)
parameter_value = getattr(plugin, parameter_name)
assert repr(parameter_value) == repr(parameter_value)
assert isinstance(parameter_value, str)
# Change the parameter and ensure that it does change:
for new_value in parameter_value.valid_values:
setattr(plugin, parameter_name, new_value)
assert getattr(plugin, parameter_name) == new_value
# Ensure that if we access an attribute that we're not adding to the value,
# we fall back to the underlying type (str) or we raise an exception if not:
assert parameter_value.lower()
with pytest.raises(AttributeError):
parameter_value.something_that_doesnt_exist
@pytest.mark.parametrize(
"plugin_filename,parameter_name",
[
(path, parameter)
for path in AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT
for parameter in [k for k, v in get_parameters(path).items() if v.type == str]
],
)
def test_string_parameter_valdation(plugin_filename: str, parameter_name: str):
plugin = load_test_plugin(plugin_filename)
some_other_object = object()
with pytest.raises(ValueError):
setattr(plugin, parameter_name, some_other_object)
with pytest.raises(ValueError):
setattr(plugin, parameter_name, "some value not present")
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_plugin_parameters_persist_between_calls(plugin_filename: str):
if plugin_filename == "UnityGain32Channel.vst3":
pytest.skip("No parameters in this plugin.")
plugin = load_test_plugin(plugin_filename)
sr = 44100
noise = np.random.rand(2, sr)
# Set random parameter values on the plugin:
for name, parameter in plugin.parameters.items():
if name == "program":
continue
if parameter.type == float:
low, high, step = parameter.range
if not step:
step = 0.1
if low is None:
low = 0.0
if high is None:
high = 1.0
values = [
x * step for x in list(range(int(low / step), int(high / step), 1)) + [high / step]
]
random_value = random.choice(values)
elif parameter.type == bool:
random_value = bool(random.random())
elif parameter.type == str:
if parameter.valid_values:
random_value = random.choice(parameter.valid_values)
else:
random_value = None
if (
random_value is not None
and "bypass" not in name.lower()
and "preset" not in name.lower()
):
setattr(plugin, name, random_value)
expected_values = {}
for name, parameter in plugin.parameters.items():
expected_values[name] = getattr(plugin, name)
plugin.process(noise, sr)
for name, parameter in plugin.parameters.items():
assert (
getattr(plugin, name) == expected_values[name]
), f"Expected {name} to match saved value"
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_plugin_state_cleared_between_invocations_by_default(plugin_filename: str):
plugin = load_test_plugin(plugin_filename, disable_caching=True)
sr = 44100
noise = np.random.rand(sr, 2)
assert max_volume_of(noise) > 0.95
silence = np.zeros_like(noise)
assert max_volume_of(silence) == 0.0
assert max_volume_of(plugin(silence, sr)) < 0.00001
assert max_volume_of(plugin(noise, sr)) > 0.00001
for _ in range(10):
assert max_volume_of(silence) == 0.0
assert max_volume_of(plugin(silence, sr)) < 0.00001
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_plugin_state_not_cleared_between_invocations_if_reset_is_false(plugin_filename: str):
if plugin_filename == "UnityGain32Channel.vst3":
pytest.skip("No internal state stored in this plugin.")
plugin = load_test_plugin(plugin_filename, disable_caching=True)
sr = 44100
noise = np.random.rand(sr, 2)
assert max_volume_of(noise) > 0.95
silence = np.zeros_like(noise)
assert max_volume_of(silence) == 0.0
assert max_volume_of(plugin(silence, sr, reset=False)) < 0.00001
assert max_volume_of(plugin(noise, sr, reset=False)) > 0.00001
assert max_volume_of(plugin(silence, sr, reset=False)) > 0.00001
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_explicit_effect_reset(plugin_filename: str):
plugin = load_test_plugin(plugin_filename, disable_caching=True)
sr = 44100
noise = np.random.rand(sr, 2)
assert max_volume_of(noise) > 0.95
silence = np.zeros_like(noise)
assert max_volume_of(silence) == 0.0
assert max_volume_of(plugin(silence, sr, reset=False)) < 0.00001
assert max_volume_of(plugin(noise, sr, reset=False)) > 0.00001
plugin.reset()
assert max_volume_of(plugin(silence, sr, reset=False)) < 0.00001
@pytest.mark.skipif(not plugin_named("Magical8BitPlug"), reason="Missing Magical8BitPlug 2 plugin.")
def test_explicit_instrument_reset():
plugin = load_test_plugin(plugin_named("Magical8BitPlug"), disable_caching=True)
sr = 44100
notes = [mido.Message("note_on", note=100, velocity=127, time=0)]
assert max_volume_of(plugin(notes, 5.0, sr, reset=False)) >= 0.5
plugin.reset()
assert max_volume_of(plugin([], 5.0, sr, reset=False)) < 0.00001
@pytest.mark.parametrize("plugin_filename", AVAILABLE_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_explicit_reset_in_pedalboard(plugin_filename: str):
sr = 44100
board = pedalboard.Pedalboard([load_test_plugin(plugin_filename, disable_caching=True)])
noise = np.random.rand(sr, 2)
assert max_volume_of(noise) > 0.95
silence = np.zeros_like(noise)
assert max_volume_of(silence) == 0.0
assert max_volume_of(board(silence, reset=False, sample_rate=sr)) < 0.00001
assert max_volume_of(board(noise, reset=False, sample_rate=sr)) > 0.00001
board.reset()
assert max_volume_of(board(silence, reset=False, sample_rate=sr)) < 0.00001
@pytest.mark.parametrize("value", (True, False))
def test_wrapped_bool(value: bool):
wrapped = WrappedBool(value)
assert wrapped == value
assert repr(wrapped) == repr(value)
assert hash(wrapped) == hash(value)
assert bool(wrapped) == bool(value)
assert str(wrapped) == str(value)
for attr in dir(value):
assert hasattr(wrapped, attr)
def test_wrapped_bool_requires_bool():
with pytest.raises(TypeError):
assert WrappedBool(None)
@pytest.mark.parametrize(
"_input,expected",
[
("C#", "c_sharp"),
("Bβ", "b_flat"),
("Azimuth (ΒΊ)", "azimuth"),
("Normal String", "normal_string"),
],
)
def test_parameter_name_normalization(_input: str, expected: str):
assert normalize_python_parameter_name(_input) == expected
@pytest.mark.skipif(not plugin_named("CHOWTapeModel"), reason="Missing CHOWTapeModel plugin.")
@pytest.mark.parametrize("buffer_size", [16, 128, 8192, 65536])
@pytest.mark.parametrize("oversampling", [1, 2, 4, 8, 16])
def test_external_plugin_latency_compensation(buffer_size: int, oversampling: int):
"""
This test loads CHOWTapeModel (which has non-zero latency due
to an internal oversampler), puts it into Bypass mode, then
ensures that the input matches the output exactly.
"""
num_seconds = 10.0
sample_rate = 48000
noise = np.random.rand(int(num_seconds * sample_rate))
plugin = load_test_plugin(plugin_named("CHOWTapeModel"), disable_caching=True)
plugin.bypass = True
plugin.oversampling = oversampling
output = plugin.process(noise, sample_rate, buffer_size=buffer_size)
np.testing.assert_allclose(output, noise, atol=0.05)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_show_editor(plugin_filename: str):
# Run this test in a subprocess, as otherwise we'd block this thread:
full_plugin_filename = find_plugin_path(plugin_filename)
try:
subprocess.check_output(
[
psutil.Process(os.getpid()).exe(),
"-c",
"import pedalboard;"
f'pedalboard.load_plugin(r"{full_plugin_filename}").show_editor();',
],
timeout=5,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as e:
if (
b"no visual display devices available" in e.output
# Unsure why, but in some test environments, we
# can't load Pedalboard in a subprocess.
# TODO(psobot): Ensure we can load Pedalboard properly
# in all environments for this test.
or b"No module named 'pedalboard'" in e.output
):
pass
else:
raise
except subprocess.TimeoutExpired:
# This is good: the UI was shown, no issues.
pass
@pytest.mark.skipif(
not AVAILABLE_CONTAINER_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT,
reason="No plugin containers installed in test environment!",
)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_CONTAINER_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_plugin_container_handling(plugin_filename: str):
"""
Some plugins can have multiple sub-plugins within them.
As of v0.4.5, Pedalboard requires indicating the specific plugin to open within the container.
These plugins will fail by default.
"""
with pytest.raises(ValueError) as e:
load_test_plugin(plugin_filename, disable_caching=True)
assert plugin_filename in str(e)
assert "plugin_name" in str(e)
# Hackily parse out the plugin names:
message = e.value.args[0]
plugin_names = [line.strip().strip('"') for line in message.split("\n")[1:]]
assert f"{len(plugin_names)} plugins" in message
# Try to re-load each of the component plugins here:
for plugin_name in plugin_names:
plugin = load_test_plugin(plugin_filename, disable_caching=True, plugin_name=plugin_name)
assert plugin_name == plugin.name
@pytest.mark.parametrize("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_get_plugin_name_from_regular_plugin(plugin_filename: str):
plugin_path = find_plugin_path(plugin_filename)
if ".vst3" in plugin_filename:
names = pedalboard.VST3Plugin.get_plugin_names_for_file(plugin_path)
elif ".component" in plugin_filename:
names = pedalboard.AudioUnitPlugin.get_plugin_names_for_file(plugin_path)
else:
raise ValueError("Plugin does not seem to be a .vst3 or .component.")
assert len(names) == 1
assert load_test_plugin(plugin_filename).name == names[0]
@pytest.mark.skipif(
not AVAILABLE_CONTAINER_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT,
reason="No plugin containers installed in test environment!",
)
@pytest.mark.parametrize("plugin_filename", AVAILABLE_CONTAINER_EFFECT_PLUGINS_IN_TEST_ENVIRONMENT)
def test_get_plugin_names_from_container(plugin_filename: str):
plugin_path = find_plugin_path(plugin_filename)
if ".vst3" in plugin_filename:
names = pedalboard.VST3Plugin.get_plugin_names_for_file(plugin_path)
elif ".component" in plugin_filename:
names = pedalboard.AudioUnitPlugin.get_plugin_names_for_file(plugin_path)
else:
raise ValueError("Plugin does not seem to be a .vst3 or .component.")
assert len(names) > 1
@pytest.mark.parametrize("plugin_filename", ["UnityGain32Channel.vst3"])
def test_poly_channel_plugins(plugin_filename):
plugin_path = find_plugin_path(plugin_filename)
plugin = pedalboard.load_plugin(plugin_path)
sr = 44100
noise = np.random.rand(sr, 32)
outputs = plugin(
input_array=noise,
sample_rate=sr)
assert outputs.shape == noise.shape
assert np.all(np.isclose(noise, outputs))