Skip to content

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

Add tests for type hinting.

Peter Sobot1fb92a48e563
diff --git a/tests/mypy_fixtures/audio_file.py b/tests/mypy_fixtures/audio_file.py
new file mode 100644
index 0000000..263264f
--- /dev/null
+++ b/tests/mypy_fixtures/audio_file.py
@@ -0,0 +1,10 @@
+# This file is not intended to be executed - it's intended to be used to ensure MyPy compatibility!
+
+from pedalboard.io import AudioFile
+
+with AudioFile("some_path") as f:
+ f.read(f.frames)
+
+with open("some_path", "rb") as f:
+ with AudioFile(f) as r:
+ r.read(r.frames)
diff --git a/tests/mypy_fixtures/fail_audio_file.py b/tests/mypy_fixtures/fail_audio_file.py
new file mode 100644
index 0000000..d886fcb
--- /dev/null
+++ b/tests/mypy_fixtures/fail_audio_file.py
@@ -0,0 +1,10 @@
+# This file is not intended to be executed - it's intended to be used to ensure MyPy compatibility!
+
+from pedalboard.io import AudioFile
+
+with AudioFile(b"some_binary_string") as f:
+ f.read(f.frames)
+
+with open("some_path", "r") as f:
+ with AudioFile(f) as r:
+ r.read(r.frames)
diff --git a/tests/mypy_fixtures/readme_guitar.py b/tests/mypy_fixtures/readme_guitar.py
new file mode 100644
index 0000000..3f41550
--- /dev/null
+++ b/tests/mypy_fixtures/readme_guitar.py
@@ -0,0 +1,38 @@
+# This file is not intended to be executed - it's intended to be used to ensure MyPy compatibility!
+
+# Don't do import *! (It just makes this example smaller)
+from pedalboard import *
+from pedalboard.io import AudioFile
+
+# Read in a whole file, resampling to our desired sample rate:
+samplerate = 44100.0
+with AudioFile("guitar-input.wav").resampled_to(samplerate) as f:
+ audio = f.read(f.frames)
+
+# Make a pretty interesting sounding guitar pedalboard:
+board = Pedalboard(
+ [
+ Compressor(threshold_db=-50, ratio=25),
+ Gain(gain_db=30),
+ Chorus(),
+ LadderFilter(mode=LadderFilter.Mode.HPF12, cutoff_hz=900),
+ Phaser(),
+ Convolution("./guitar_amp.wav", 1.0),
+ Reverb(room_size=0.25),
+ ]
+)
+
+# Pedalboard objects behave like lists, so you can add plugins:
+board.append(Compressor(threshold_db=-25, ratio=10))
+board.append(Gain(gain_db=10))
+board.append(Limiter())
+
+# ... or change parameters easily:
+board[0].threshold_db = -40
+
+# Run the audio through this pedalboard!
+effected = board(audio, samplerate)
+
+# Write the audio back as a wav file:
+with AudioFile("processed-output.wav", "w", samplerate, effected.shape[0]) as f:
+ f.write(effected)
diff --git a/tests/mypy_fixtures/readme_parallel.py b/tests/mypy_fixtures/readme_parallel.py
new file mode 100644
index 0000000..03f6803
--- /dev/null
+++ b/tests/mypy_fixtures/readme_parallel.py
@@ -0,0 +1,38 @@
+# This file is not intended to be executed - it's intended to be used to ensure MyPy compatibility!
+
+from pedalboard import Pedalboard, Compressor, Delay, Distortion, Gain, PitchShift, Reverb, Mix
+
+passthrough = Gain(gain_db=0)
+
+delay_and_pitch_shift = Pedalboard(
+ [
+ Delay(delay_seconds=0.25, mix=1.0),
+ PitchShift(semitones=7),
+ Gain(gain_db=-3),
+ ]
+)
+
+delay_longer_and_more_pitch_shift = Pedalboard(
+ [
+ Delay(delay_seconds=0.5, mix=1.0),
+ PitchShift(semitones=12),
+ Gain(gain_db=-6),
+ ]
+)
+
+board = Pedalboard(
+ [
+ # Put a compressor at the front of the chain:
+ Compressor(),
+ # Run all of these pedalboards simultaneously with the Mix plugin:
+ Mix(
+ [
+ passthrough,
+ delay_and_pitch_shift,
+ delay_longer_and_more_pitch_shift,
+ ]
+ ),
+ # Add a reverb on the final mix:
+ Reverb(),
+ ]
+)
diff --git a/tests/mypy_fixtures/readme_quickstart.py b/tests/mypy_fixtures/readme_quickstart.py
new file mode 100644
index 0000000..212698e
--- /dev/null
+++ b/tests/mypy_fixtures/readme_quickstart.py
@@ -0,0 +1,23 @@
+# This file is not intended to be executed - it's intended to be used to ensure MyPy compatibility!
+
+from pedalboard import Pedalboard, Chorus, Reverb
+from pedalboard.io import AudioFile
+
+# Make a Pedalboard object, containing multiple audio plugins:
+board = Pedalboard([Chorus(), Reverb(room_size=0.25)])
+
+# Open an audio file for reading, just like a regular file:
+with AudioFile("some-file.wav") as f:
+
+ # Open an audio file to write to:
+ with AudioFile("output.wav", "w", f.samplerate, f.num_channels) as o:
+
+ # Read one second of audio at a time, until the file is empty:
+ while f.tell() < f.frames:
+ chunk = f.read(int(f.samplerate))
+
+ # Run the audio through our pedalboard:
+ effected = board(chunk, f.samplerate, reset=False)
+
+ # Write the output to our output file:
+ o.write(effected)
diff --git a/tests/mypy_fixtures/readme_vst.py b/tests/mypy_fixtures/readme_vst.py
new file mode 100644
index 0000000..df8da07
--- /dev/null
+++ b/tests/mypy_fixtures/readme_vst.py
@@ -0,0 +1,29 @@
+# This file is not intended to be executed - it's intended to be used to ensure MyPy compatibility!
+
+from pedalboard import Pedalboard, Reverb, load_plugin
+from pedalboard.io import AudioFile
+
+# Load a VST3 or Audio Unit plugin from a known path on disk:
+vst = load_plugin("./VSTs/RoughRider3.vst3")
+
+print(vst.parameters.keys())
+# dict_keys([
+# 'sc_hpf_hz', 'input_lvl_db', 'sensitivity_db',
+# 'ratio', 'attack_ms', 'release_ms', 'makeup_db',
+# 'mix', 'output_lvl_db', 'sc_active',
+# 'full_bandwidth', 'bypass', 'program',
+# ])
+
+# Set the "ratio" parameter to 15
+vst.ratio = 15
+
+# Use this VST to process some audio:
+with AudioFile("some-file.wav", "r") as f:
+ audio = f.read(f.frames)
+ samplerate = f.samplerate
+effected = vst(audio, samplerate)
+
+# ...or put this VST into a chain with other plugins:
+board = Pedalboard([vst, Reverb()])
+# ...and run that pedalboard with the same VST instance!
+effected = board(audio, samplerate)
diff --git a/tests/test_type_hints.py b/tests/test_type_hints.py
new file mode 100644
index 0000000..467f343
--- /dev/null
+++ b/tests/test_type_hints.py
@@ -0,0 +1,46 @@
+#! /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.
+
+
+from psutil import Process
+import os
+from glob import glob
+import pytest
+import subprocess
+from subprocess import STDOUT
+
+FIXTURE_DIR = os.path.join(os.path.dirname(__file__), "mypy_fixtures")
+ALL_FIXTURES = set(glob(f"{FIXTURE_DIR}/*.py"))
+FAILING_FIXTURES = set([f for f in ALL_FIXTURES if "fail" in f])
+PASSING_FIXTURES = ALL_FIXTURES - FAILING_FIXTURES
+
+
[email protected]("filename", PASSING_FIXTURES)
+def test_mypy_passes(filename):
+ # Run this test in a subprocess, as MyPy forcibly exits, killing PyTest:
+ try:
+ subprocess.check_output([Process(os.getpid()).exe(), "-m", "mypy", filename], stderr=STDOUT)
+ except subprocess.CalledProcessError as e:
+ raise AssertionError(
+ f"Expected MyPy to pass, but failed with:\n\n{e.stdout.decode('utf-8')}"
+ ) from e
+
+
[email protected]("filename", FAILING_FIXTURES)
+def test_mypy_fails(filename):
+ # Run this test in a subprocess, as MyPy forcibly exits, killing PyTest:
+ with pytest.raises(subprocess.CalledProcessError):
+ subprocess.check_output([Process(os.getpid()).exe(), "-m", "mypy", filename], stderr=STDOUT)