Skip to content

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

Change Pitch Shift plugin interface to use semitones.

Peter Sobot78b98b1beeec
diff --git a/pedalboard/plugins/PitchShift.h b/pedalboard/plugins/PitchShift.h
index 49f0863..75fed09 100644
--- a/pedalboard/plugins/PitchShift.h
+++ b/pedalboard/plugins/PitchShift.h
@@ -30,48 +30,45 @@ class PitchShift : public RubberbandPlugin
{
private:
- double _scaleFactor = 1.0;
+ double _semitones = 0.0;
// Allow pitch shifting by up to 6 octaves up or down:
- static constexpr float MIN_SCALE_FACTOR = 0.015625;
- static constexpr float MAX_SCALE_FACTOR = 64.0;
+ static constexpr int MIN_SEMITONES = -6 * 12;
+ static constexpr int MAX_SEMITONES = 6 * 12;
public:
- void setScaleFactor(double scale) {
- if (scale < MIN_SCALE_FACTOR || scale > MAX_SCALE_FACTOR) {
- throw std::range_error("Scale factor must be a value between " +
- std::to_string(MIN_SCALE_FACTOR) + " and " +
- std::to_string(MAX_SCALE_FACTOR) + ".");
+ void setSemitones(double semitones) {
+ if (semitones < MIN_SEMITONES || semitones > MAX_SEMITONES) {
+ throw std::range_error("Semitones of pitch must be a value between " +
+ std::to_string(MIN_SEMITONES) + "st and " +
+ std::to_string(MAX_SEMITONES) + "st.");
}
- _scaleFactor = scale;
+ _semitones = semitones;
if (rbPtr)
- rbPtr->setPitchScale(_scaleFactor);
+ rbPtr->setPitchScale(getScaleFactor());
}
- double getScaleFactor() { return _scaleFactor; }
+ double getSemitones() { return _semitones; }
+ double getScaleFactor() { return pow(2, (getSemitones() / 12)); }
void prepare(const juce::dsp::ProcessSpec &spec) override final {
RubberbandPlugin::prepare(spec);
- rbPtr->setPitchScale(_scaleFactor);
+ rbPtr->setPitchScale(getScaleFactor());
}
};
inline void init_pitch_shift(py::module &m) {
py::class_<PitchShift, Plugin>(
- m, "PitchShift",
- "Shift pitch without affecting audio duration. Shifted audio may start "
- "with a short duration of silence, depending on the scale factor used. "
- "The rate of the audio will be multiplied by scale_factor (i.e.: 2x "
- "means one octave up, 0.5x means one octave down).")
+ m, "PitchShift", "Shift pitch without affecting audio duration.")
.def(py::init([](double scale) {
auto plugin = new PitchShift();
- plugin->setScaleFactor(scale);
+ plugin->setSemitones(scale);
return plugin;
}),
- py::arg("scale_factor") = 1.0)
- .def_property("scale_factor", &PitchShift::getScaleFactor,
- &PitchShift::setScaleFactor);
+ py::arg("semitones") = 0.0)
+ .def_property("semitones", &PitchShift::getSemitones,
+ &PitchShift::setSemitones);
}
}; // namespace Pedalboard
diff --git a/tests/test_pitch_shift.py b/tests/test_pitch_shift.py
index e91e477..964804f 100644
--- a/tests/test_pitch_shift.py
+++ b/tests/test_pitch_shift.py
@@ -20,44 +20,44 @@ import numpy as np
from pedalboard import Pedalboard, PitchShift
[email protected]("scale", [1 / 2, 1.0, 2.0])
[email protected]("semitones", [-12, 0, 12])
@pytest.mark.parametrize("fundamental_hz", [440, 880])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
-def test_pitch_shift(scale, fundamental_hz, sample_rate):
+def test_pitch_shift(semitones, fundamental_hz, sample_rate):
num_seconds = 10.0
samples = np.arange(num_seconds * sample_rate)
sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
- plugin = PitchShift(scale)
+ plugin = PitchShift(semitones)
output = plugin.process(sine_wave, sample_rate)
assert np.all(np.isfinite(output))
[email protected]("scale", [0.01, 65.0])
-def test_pitch_shift_extremes_throws_errors(scale):
[email protected]("semitones", [-73, 73])
+def test_pitch_shift_extremes_throws_errors(semitones):
with pytest.raises(ValueError):
- PitchShift(scale)
+ PitchShift(semitones)
[email protected]("scale", [1 / 64, 1 / 8, 1 / 2, 2, 8, 64])
[email protected]("semitones", [-72, -36, -12, 12, 36, 72])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
@pytest.mark.parametrize("buffer_size", [32, 512, 8192])
-def test_pitch_shift_extremes(scale, sample_rate, buffer_size):
+def test_pitch_shift_extremes(semitones, sample_rate, buffer_size):
noise = np.random.rand(int(5.0 * sample_rate))
- plugin = PitchShift(scale)
+ plugin = PitchShift(semitones)
output = plugin.process(noise, sample_rate, buffer_size=buffer_size)
assert np.all(np.isfinite(output))
[email protected]("scale", [1.0])
[email protected]("semitones", [0])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
@pytest.mark.parametrize("buffer_size", [512, 8192])
-def test_pitch_shift_latency_compensation(scale, sample_rate, buffer_size):
+def test_pitch_shift_latency_compensation(semitones, sample_rate, buffer_size):
num_seconds = 10.0
fundamental_hz = 440.0
samples = np.arange(num_seconds * sample_rate)
sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
- plugin = Pedalboard([PitchShift(scale), PitchShift(1 / scale)])
+ plugin = Pedalboard([PitchShift(semitones), PitchShift(-semitones)])
output = plugin.process(sine_wave, sample_rate, buffer_size=buffer_size)
np.testing.assert_allclose(
sine_wave[sample_rate:-sample_rate], output[sample_rate:-sample_rate], rtol=0.01, atol=0.01