Skip to content

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

Add checks for common pitfalls when rendering MIDI.

Peter Sobotb5a83e6b2a30
diff --git a/pedalboard/ExternalPlugin.h b/pedalboard/ExternalPlugin.h
index e8fc831..7dad61c 100644
--- a/pedalboard/ExternalPlugin.h
+++ b/pedalboard/ExternalPlugin.h
@@ -983,6 +983,22 @@ public:
float sampleRate,
unsigned int numChannels,
unsigned long bufferSize, bool reset) {
+
+ // Tiny quality-of-life improvement to try to detect if people have swapped
+ // the duration and sample_rate arguments:
+ if ((duration == 48000 || duration == 44100 || duration == 22050 ||
+ duration == 11025) &&
+ sampleRate < 8000) {
+ throw std::invalid_argument(
+ "Plugin '" + pluginInstance->getName().toStdString() +
+ "' was called with a duration argument of " +
+ std::to_string(duration) + " and a sample_rate argument of " +
+ std::to_string(sampleRate) +
+ ". These arguments appear to be flipped, and may cause distorted "
+ "audio to be rendered. Try reversing the order of the sample_rate "
+ "and duration arguments provided to this method.");
+ }
+
std::scoped_lock<std::mutex>(this->mutex);
py::array_t<float> outputArray;
diff --git a/pedalboard/midi_utils.py b/pedalboard/midi_utils.py
index fd9da66..552ba2f 100644
--- a/pedalboard/midi_utils.py
+++ b/pedalboard/midi_utils.py
@@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+from collections import Counter
from typing import List, Tuple, Union
@@ -56,4 +57,24 @@ def normalize_midi_messages(_input) -> List[Tuple[bytes, float]]:
elif not isinstance(message, bytes):
message = bytes(message)
output.append((message, time))
+
+ # Detect the case in which the provided timestamps
+ # are likely delta values rather than absolute values:
+ all_timestamps = [t for _, t in output]
+ if len(all_timestamps) > 100 and len(set(all_timestamps)) > 1:
+ all_timestamps_histogram = Counter(all_timestamps)
+ (
+ most_common_timestamp,
+ num_instances_of_most_common,
+ ) = all_timestamps_histogram.most_common()[0]
+ if num_instances_of_most_common > 100:
+ raise ValueError(
+ "Pedalboard requires MIDI input timestamps to be absolute values, specified "
+ "as the number of seconds from the start of the returned audio buffer. The "
+ f"provided MIDI data contains {num_instances_of_most_common:,} events at timestamp "
+ f"{most_common_timestamp}, which suggests that the timestamps may be delta values "
+ "rather than absolute values. Try converting your MIDI message timestamps to "
+ "absolute values first."
+ )
+
return output
diff --git a/tests/midi/ascending_chromatic.mid b/tests/midi/ascending_chromatic.mid
new file mode 100644
index 0000000..906f373
Binary files /dev/null and b/tests/midi/ascending_chromatic.mid differ
diff --git a/tests/test_external_plugins.py b/tests/test_external_plugins.py
index 5e35055..88ae643 100644
--- a/tests/test_external_plugins.py
+++ b/tests/test_external_plugins.py
@@ -47,6 +47,7 @@ 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)
@@ -343,6 +344,28 @@ def test_instrument_plugin_accepts_notes(
assert np.amax(np.abs(output)) > 0
[email protected]("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)
+
+
[email protected]("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(