Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Fix tests when running on Apple Silicon. (#56)

Peter Sobotf37c06b59536
diff --git a/pedalboard/ExternalPlugin.h b/pedalboard/ExternalPlugin.h
index 7999c6f..ea2762d 100644
--- a/pedalboard/ExternalPlugin.h
+++ b/pedalboard/ExternalPlugin.h
@@ -193,6 +193,19 @@ public:
pathToSharedObjectFile.getFullPathName().toStdString() + "\"` to " +
"see which dependencies might be missing.).");
#else
+
+// On certain Macs, plugins will only load if installed in the appropriate path.
+#if JUCE_MAC
+ bool pluginIsInstalled =
+ pluginFileStripped.contains("/Library/Audio/Plug-Ins/Components/");
+ if (!pluginIsInstalled) {
+ throw pybind11::import_error(
+ "Unable to load plugin " + pathToPluginFile.toStdString() +
+ ": unsupported plugin format or load failure. Plugin file may need "
+ "to be moved to /Library/Audio/Plug-Ins/Components/ or "
+ "~/Library/Audio/Plug-Ins/Components/.");
+ }
+#endif
throw pybind11::import_error(
"Unable to load plugin " + pathToPluginFile.toStdString() +
": unsupported plugin format or load failure.");
diff --git a/test-requirements.txt b/test-requirements.txt
index 146ae0a..42548b9 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -11,6 +11,6 @@ interrogate
numpy
sox
-tensorflow==2.5.0; python_version < '3.10'
+tensorflow==2.5.0; python_version < '3.10' and platform.machine != 'arm64'
google-cloud-storage
tqdm
diff --git a/tests/test_external_plugins.py b/tests/test_external_plugins.py
index 1b3fdeb..97f34b6 100644
--- a/tests/test_external_plugins.py
+++ b/tests/test_external_plugins.py
@@ -16,10 +16,14 @@
import os
+import time
import math
+import atexit
import random
+import shutil
import platform
from glob import glob
+from pathlib import Path
from pedalboard.pedalboard import WrappedBool, strip_common_float_suffixes
import pytest
@@ -43,15 +47,19 @@ if os.getenv("CIBW_TEST_REQUIRES") or os.getenv("CI"):
def get_parameters(plugin_filename: str):
- try:
- return load_test_plugin(plugin_filename).parameters
- except ImportError:
- return {}
+ return load_test_plugin(plugin_filename).parameters
TEST_PLUGIN_CACHE = {}
TEST_PLUGIN_ORIGINAL_PARAMETER_CACHE = {}
+# If true, copy all test Audio Units into the appropriate install path
+# before running tests. On some versions of macOS, this is necessary or
+# else Audio Units won't load.
+TEMPORARILY_INSTALL_AUDIO_UNITS = True
+PLUGIN_FILES_TO_DELETE = set()
+MACOS_PLUGIN_INSTALL_PATH = Path.home() / "Library" / "Audio" / "Plug-Ins" / "Components"
+
def load_test_plugin(plugin_filename: str, disable_caching: bool = False, *args, **kwargs):
"""
@@ -64,9 +72,35 @@ def load_test_plugin(plugin_filename: str, disable_caching: bool = False, *args,
key = repr((plugin_filename, args, tuple(kwargs.items())))
if key not in TEST_PLUGIN_CACHE or disable_caching:
- plugin = pedalboard.load_plugin(
- os.path.join(TEST_PLUGIN_BASE_PATH, platform.system(), plugin_filename), *args, **kwargs
- )
+ plugin_path = os.path.join(TEST_PLUGIN_BASE_PATH, platform.system(), plugin_filename)
+
+ if (
+ platform.system() == "Darwin"
+ and plugin_filename.endswith(".component")
+ and TEMPORARILY_INSTALL_AUDIO_UNITS
+ ):
+ # On certain macOS machines, it seems 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
@@ -87,6 +121,14 @@ def load_test_plugin(plugin_filename: str, disable_caching: bool = False, *args,
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:
diff --git a/tests/test_memory_leaks.py b/tests/test_memory_leaks.py
index 184a2a0..d2508fa 100644
--- a/tests/test_memory_leaks.py
+++ b/tests/test_memory_leaks.py
@@ -15,22 +15,17 @@
# limitations under the License.
-import os
import gc
import weakref
import pytest
-import platform
-import pedalboard
-from .test_external_plugins import TEST_PLUGIN_BASE_PATH, AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT
+from .test_external_plugins import load_test_plugin, AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT
@pytest.mark.parametrize("plugin_path", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_plugin_can_be_garbage_collected(plugin_path: str):
- # Load a VST3 plugin from disk:
- plugin = pedalboard.load_plugin(
- os.path.join(TEST_PLUGIN_BASE_PATH, platform.system(), plugin_path)
- )
+ # Load a VST3 or Audio Unit plugin from disk:
+ plugin = load_test_plugin(plugin_path, disable_caching=True)
_plugin_ref = weakref.ref(plugin)
param_name, param = next(
diff --git a/tests/test_tensorflow.py b/tests/test_tensorflow.py
index 9ad8bf5..3e8e09b 100644
--- a/tests/test_tensorflow.py
+++ b/tests/test_tensorflow.py
@@ -15,24 +15,27 @@
# limitations under the License.
import sys
+import pytest
+import platform
import numpy as np
import pedalboard
+ sys.version_info.minor >= 10, reason="TensorFlow not supported on Python 3.10+ yet."
+)
+ (platform.processor() == "arm" and platform.system() == "Darwin"),
+ reason="TensorFlow not supported on M1 Macs yet.",
+)
def test_can_be_called_in_tensorflow_data_pipeline():
+ import tensorflow as tf
+
sr = 48000
plugins = pedalboard.Pedalboard([pedalboard.Gain(), pedalboard.Reverb()], sample_rate=sr)
noise = np.random.rand(sr)
- try:
- import tensorflow as tf
- except ModuleNotFoundError:
- # TensorFlow is not yet supported on Python 3.10 - don't bother testing.
- if sys.version_info.minor >= 10:
- return
- raise
-
ds = tf.data.Dataset.from_tensor_slices([noise]).map(
lambda audio: tf.numpy_function(plugins.process, [audio], tf.float32)
)