A lightweight yet powerful audio-to-MIDI converter with pitch bend detection
Python audiolightweightmachine-learningmidimusicpitch-detectionpolyphonicpythontranscriptiontypescript
expose full tracebacks on error; patch sonification-samplerate bug
Rachel Bittner7afdc0097046
diff --git a/basic_pitch/commandline_printing.py b/basic_pitch/commandline_printing.pyindex f51d1e5..6cf1248 100644--- a/basic_pitch/commandline_printing.py+++ b/basic_pitch/commandline_printing.py@@ -15,14 +15,11 @@# See the License for the specific language governing permissions and# limitations under the License.-import inspectimport osimport pathlib-import sysimport threading-import timefrom contextlib import contextmanager-from typing import Iterator, Optional, Union+from typing import Iterator, UnionTF_LOG_LEVEL_KEY = "TF_CPP_MIN_LOG_LEVEL"TF_LOG_LEVEL_NO_WARNINGS_VALUE = "3"@@ -76,59 +73,3 @@ def no_tf_warnings() -> Iterator[None]:os.environ[TF_LOG_LEVEL_KEY] = TF_LOG_LEVEL_NO_WARNINGS_VALUEyieldos.environ[TF_LOG_LEVEL_KEY] = tf_logging_level---@contextmanager-def entertaining_waiting(operation_name: Optional[str] = None) -> Iterator[None]:- """- Display a jazzy loading animation in the commandline for the duration of this context-- Args:- operation_name: the name of the operation contained in this context to be displayed in- the commandline- """- if operation_name is None:- operation_name = f"Running Command: '{inspect.stack()[2].function}'"-- loading = True- loading_states = ["π₯", "π·", "πΈ", "π»", "πΉ", "πͺ", "πͺ", "πΊ", "πͺ"]- blank_message = " " * (len(loading_states) * 2 + len(operation_name))-- def animate() -> None:- emoji_index = 0- while loading:- if emoji_index >= len(loading_states):- emoji_index = 0- loading_message = blank_message- else:- loading_message = "".join(loading_states[0:emoji_index])-- s_flush()- s_print(f"\r{operation_name}... " + loading_message)- emoji_index += 1- time.sleep(0.3)-- t = threading.Thread(target=animate)- t.start()-- yield- loading = False---def s_print(msg: str) -> None:- """- Thread safe print function-- Args:- msg: the text to be printed to stdout- """- with s_print_lock:- sys.stdout.write(msg)---def s_flush() -> None:- """- Thread safe flush function- """- with s_print_lock:- sys.stdout.flush()diff --git a/basic_pitch/inference.py b/basic_pitch/inference.pyindex 8c50d16..4e31c23 100644--- a/basic_pitch/inference.py+++ b/basic_pitch/inference.py@@ -20,6 +20,7 @@ import enumimport jsonimport osimport pathlib+import tracebackfrom typing import Dict, List, Optional, Sequence, Tuple, Unionfrom tensorflow import Tensor, signal, keras, saved_model@@ -35,7 +36,6 @@ from basic_pitch.constants import ()from basic_pitch import ICASSP_2022_MODEL_PATH, note_creation as inferfrom basic_pitch.commandline_printing import (- entertaining_waiting,generating_file_message,no_tf_warnings,file_saved_confirmation,@@ -122,7 +122,9 @@ def unwrap_output(output: Tensor, audio_original_length: int, n_overlapping_framdef run_inference(- audio_path: Union[pathlib.Path, str], model: keras.Model, debug_file: Optional[pathlib.Path] = None+ audio_path: Union[pathlib.Path, str],+ model: keras.Model,+ debug_file: Optional[pathlib.Path] = None,) -> Dict[str, np.array]:"""Run the model on the input audio path.@@ -200,7 +202,9 @@ def verify_output_dir(output_dir: Union[pathlib.Path, str]) -> None:def build_output_path(- audio_path: Union[pathlib.Path, str], output_directory: Union[pathlib.Path, str], output_type: OutputExtensions+ audio_path: Union[pathlib.Path, str],+ output_directory: Union[pathlib.Path, str],+ output_type: OutputExtensions,) -> pathlib.Path:"""Create an output path and make sure it doesn't already exist.@@ -235,7 +239,8 @@ def build_output_path(def save_note_events(- note_events: List[Tuple[float, float, int, float, Optional[List[int]]]], save_path: Union[pathlib.Path, str]+ note_events: List[Tuple[float, float, int, float, Optional[List[int]]]],+ save_path: Union[pathlib.Path, str],) -> None:"""Save note events to file@@ -266,7 +271,7 @@ def predict(multiple_pitch_bends: bool = False,melodia_trick: bool = True,debug_file: Optional[pathlib.Path] = None,-) -> Tuple[Dict[str, np.array], pretty_midi.PrettyMIDI, List[Tuple[float, float, int, float, Optional[List[int]]]]]:+) -> Tuple[Dict[str, np.array], pretty_midi.PrettyMIDI, List[Tuple[float, float, int, float, Optional[List[int]]]],]:"""Run a single prediction.Args:@@ -284,7 +289,7 @@ def predict(The model output, midi data and note events from a single prediction"""- with entertaining_waiting(f"Predicting MIDI for {audio_path}..."), no_tf_warnings():+ with no_tf_warnings():# It's convenient to be able to pass in a keras saved model so if# someone wants to place this function in a loop,# the model doesn't have to be reloaded every function call@@ -293,8 +298,9 @@ def predict(else:model = model_or_model_path- model_output = run_inference(audio_path, model, debug_file)+ print(f"Predicting MIDI for {audio_path}...")+ model_output = run_inference(audio_path, model, debug_file)min_note_len = int(np.round(minimum_note_length / 1000 * (AUDIO_SAMPLE_RATE / FFT_HOP)))midi_data, note_events = infer.model_output_to_notes(model_output,@@ -344,13 +350,13 @@ def predict_and_save(model_path: Union[pathlib.Path, str] = ICASSP_2022_MODEL_PATH,onset_threshold: float = 0.5,frame_threshold: float = 0.3,- sonification_samplerate: int = 44100,minimum_note_length: float = 58,minimum_frequency: Optional[float] = None,maximum_frequency: Optional[float] = None,multiple_pitch_bends: bool = False,melodia_trick: bool = True,debug_file: Optional[pathlib.Path] = None,+ sonification_samplerate: int = 44100,) -> None:"""Make a prediction and save the results to file.@@ -364,13 +370,13 @@ def predict_and_save(model_path: Path to load the Keras saved model from. Can be local or on GCS.onset_threshold: Minimum energy required for an onset to be considered present.frame_threshold: Minimum energy requirement for a frame to be considered present.- sonification_samplerate: Sample rate for rendering audio from MIDI.minimum_note_length: The minimum allowed note length in frames.minimum_freq: Minimum allowed output frequency, in Hz. If None, all frequencies are used.maximum_freq: Maximum allowed output frequency, in Hz. If None, all frequencies are used.multiple_pitch_bends: If True, allow overlapping notes in midi file to have pitch bends.melodia_trick: Use the melodia post-processing step.debug_file: An optional path to output debug data to. Useful for testing/verification.+ sonification_samplerate: Sample rate for rendering audio from MIDI."""model = saved_model.load(str(model_path))@@ -421,5 +427,7 @@ def predict_and_save(file_saved_confirmation(OutputExtensions.NOTE_EVENTS.name, note_events_path)except Exception:failed_to_save(OutputExtensions.NOTE_EVENTS.name, note_events_path)- except Exception as e:- print(e)+ except Exception:+ print("π¨ Something went wrong π - see the traceback below for details.")+ print("")+ print(traceback.format_exc())diff --git a/basic_pitch/note_creation.py b/basic_pitch/note_creation.pyindex 23c3d23..2f77e36 100644--- a/basic_pitch/note_creation.py+++ b/basic_pitch/note_creation.py@@ -106,11 +106,7 @@ def model_output_to_notes(return note_events_to_midi(estimated_notes_time_seconds, multiple_pitch_bends), estimated_notes_time_seconds-def sonify_midi(- midi: pretty_midi.PrettyMIDI,- save_path: Union[pathlib.Path, str],- sr: Optional[int] = 44100-) -> None:+def sonify_midi(midi: pretty_midi.PrettyMIDI, save_path: Union[pathlib.Path, str], sr: Optional[int] = 44100) -> None:"""Sonify a pretty_midi midi object and save to a file.Args:diff --git a/basic_pitch/predict.py b/basic_pitch/predict.pyindex aad28c3..c6f9fb2 100644--- a/basic_pitch/predict.py+++ b/basic_pitch/predict.py@@ -93,6 +93,12 @@ def main() -> None:help="Allow overlapping notes in midi file to have pitch bends. Note: this will map each ""pitch to its own instrument",)+ parser.add_argument(+ "--sonification-samplerate",+ type=int,+ default=44100,+ help="The samplerate for sonified audio files.",+ )parser.add_argument("--debug-file", default=None, help="Optional file for debug output for inference.")parser.add_argument("--no-melodia", default=False, action="store_true", help="Skip the melodia trick.")args = parser.parse_args()@@ -101,9 +107,11 @@ def main() -> None:print("β¨β¨β¨β¨β¨β¨β¨β¨β¨")print("β¨ Basic Pitch β¨")print("β¨β¨β¨β¨β¨β¨β¨β¨β¨")+ print("")# tensorflow is very slow to import# this import is here so that the help messages print faster+ print("Importing Tensorflow (this may take a few seconds)...")from basic_pitch.inference import predict_and_save, verify_output_dir, verify_input_pathoutput_dir = pathlib.Path(args.output_dir)@@ -129,6 +137,7 @@ def main() -> None:args.multiple_pitch_bends,not args.no_melodia,pathlib.Path(args.debug_file) if args.debug_file else None,+ args.sonification_samplerate,)print("\nβ¨ Done β¨\n")