Skip to content

A lightweight yet powerful audio-to-MIDI converter with pitch bend detection

Windowing is now iterator based

David Rubinstein2227005267cd
diff --git a/basic_pitch/inference.py b/basic_pitch/inference.py
index 5168099..54ae5ea 100644
--- a/basic_pitch/inference.py
+++ b/basic_pitch/inference.py
@@ -127,7 +127,7 @@ class Model:
def window_audio_file(
audio_original: npt.NDArray[np.float32], hop_size: int
-) -> Tuple[npt.NDArray[np.float32], List[Dict[str, int]]]:
+) -> Iterable[Tuple[npt.NDArray[np.float32], Dict[str, float]]]:
"""
Pad appropriately an audio file, and return as
windowed signal, with window length = AUDIO_N_SAMPLES
@@ -138,28 +138,24 @@ def window_audio_file(
window_times: list of {'start':.., 'end':...} objects (times in seconds)
"""
- audio_windowed = np.expand_dims(
- librosa.util.frame(
- librosa.util.fix_length(audio_original, size=(audio_original.shape[0] // hop_size + 2) * hop_size),
- frame_length=AUDIO_N_SAMPLES,
- hop_length=hop_size,
- axis=0,
- ),
- axis=-1,
- )
- window_times = [
- {
+ for i in range(0, audio_original.shape[0], hop_size):
+ window = audio_original[i : i + AUDIO_N_SAMPLES]
+ if len(window) < AUDIO_N_SAMPLES:
+ window = np.pad(
+ window,
+ pad_width=[[0, AUDIO_N_SAMPLES - len(window)]],
+ )
+ t_start = float(i) / AUDIO_SAMPLE_RATE
+ window_time = {
"start": t_start,
"end": t_start + (AUDIO_N_SAMPLES / AUDIO_SAMPLE_RATE),
}
- for t_start in np.arange(audio_windowed.shape[0]) * hop_size / AUDIO_SAMPLE_RATE
- ]
- return audio_windowed, window_times
+ yield np.expand_dims(window, axis=-1), window_time
def get_audio_input(
audio_path: Union[pathlib.Path, str], overlap_len: int, hop_size: int
-) -> Iterable[Tuple[npt.NDArray[np.float32], Dict[str, int], int]]:
+) -> Iterable[Tuple[npt.NDArray[np.float32], Dict[str, float], int]]:
"""
Read wave file (as mono), pad appropriately, and return as
windowed signal, with window length = AUDIO_N_SAMPLES
@@ -178,12 +174,15 @@ def get_audio_input(
original_length = audio_original.shape[0]
audio_original = np.concatenate([np.zeros((int(overlap_len / 2),), dtype=np.float32), audio_original])
- audio_windowed, window_times = window_audio_file(audio_original, hop_size)
- for window, window_time in zip(audio_windowed, window_times):
+ for window, window_time in window_audio_file(audio_original, hop_size):
yield np.expand_dims(window, axis=0), window_time, original_length
-def unwrap_output(output: npt.NDArray[np.float32], audio_original_length: int, n_overlapping_frames: int) -> np.array:
+def unwrap_output(
+ output: npt.NDArray[np.float32],
+ audio_original_length: int,
+ n_overlapping_frames: int,
+) -> np.array:
"""Unwrap batched model predictions to a single matrix.
Args:
diff --git a/tests/test_inference.py b/tests/test_inference.py
index 60feb60..893707c 100644
--- a/tests/test_inference.py
+++ b/tests/test_inference.py
@@ -70,7 +70,13 @@ class TestPredict(unittest.TestCase):
test_audio_path = RESOURCES_PATH / "vocadito_10.wav"
with tempfile.TemporaryDirectory() as tmpdir:
inference.predict_and_save(
- [test_audio_path], tmpdir, True, True, True, True, model=inference.Model(ICASSP_2022_MODEL_PATH)
+ [test_audio_path],
+ tmpdir,
+ True,
+ True,
+ True,
+ True,
+ model=inference.Model(ICASSP_2022_MODEL_PATH),
)
expected_midi_path = tmpdir / pathlib.Path("vocadito_10_basic_pitch.mid")
expected_csv_path = tmpdir / pathlib.Path("vocadito_10_basic_pitch.csv")
@@ -142,7 +148,7 @@ class TestPredict(unittest.TestCase):
def test_window_audio_file(self) -> None:
test_audio_path = RESOURCES_PATH / "vocadito_10.wav"
audio, _ = librosa.load(str(test_audio_path), sr=AUDIO_SAMPLE_RATE, mono=True)
- audio_windowed, window_times = inference.window_audio_file(audio, AUDIO_N_SAMPLES - 30 * FFT_HOP)
+ audio_windowed, window_times = zip(*inference.window_audio_file(audio, AUDIO_N_SAMPLES - 30 * FFT_HOP))
assert len(audio_windowed) == 6
assert len(window_times) == 6
for time in window_times:
@@ -155,7 +161,7 @@ class TestPredict(unittest.TestCase):
overlap_len = 30 * FFT_HOP
audio = np.concatenate([np.zeros((overlap_len // 2,), dtype=np.float32), audio])
audio_windowed: List[npt.NDArray[np.float32]] = []
- window_times: List[Dict[str, int]] = []
+ window_times: List[Dict[str, float]] = []
for audio_window, window_time, original_length in inference.get_audio_input(
test_audio_path, overlap_len, AUDIO_N_SAMPLES - overlap_len
):