Skip to content

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

Add commandline flag to specify preferred model serialization

David Rubinstein767e818d5def
diff --git a/basic_pitch/__init__.py b/basic_pitch/__init__.py
index b72ba2e..0d86dc8 100644
--- a/basic_pitch/__init__.py
+++ b/basic_pitch/__init__.py
@@ -15,6 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import enum
import logging
import pathlib
@@ -75,13 +76,26 @@ except ImportError:
"reinstall basic-pitch with `pip install 'basic-pitch[onnx]'`"
)
+
+class FilenameSuffix(enum.Enum):
+ tf = "nmp"
+ coreml = "nmp.mlpackage"
+ tflite = "nmp.tflite"
+ onnx = "nmp.onnx"
+
+
if TF_PRESENT:
- _filename = "nmp"
+ _default_model_type = FilenameSuffix.tf
elif CT_PRESENT:
- _filename = "nmp.mlpackage"
+ _default_model_type = FilenameSuffix.coreml
elif TFLITE_PRESENT:
- _filename = "nmp.tflite"
+ _default_model_type = FilenameSuffix.tflite
elif ONNX_PRESENT:
- _filename = "nmp.onnx"
+ _default_model_type = FilenameSuffix.onnx
+
+
+def build_icassp_2022_model_path(suffix: FilenameSuffix) -> pathlib.Path:
+ return pathlib.Path(__file__).parent / "saved_models/icassp_2022" / suffix.value
+
-ICASSP_2022_MODEL_PATH = pathlib.Path(__file__).parent / "saved_models/icassp_2022" / _filename
+ICASSP_2022_MODEL_PATH = build_icassp_2022_model_path(_default_model_type)
diff --git a/basic_pitch/predict.py b/basic_pitch/predict.py
index becaf51..725af8b 100644
--- a/basic_pitch/predict.py
+++ b/basic_pitch/predict.py
@@ -20,7 +20,11 @@ import os
import pathlib
import traceback
-from basic_pitch import ICASSP_2022_MODEL_PATH
+from basic_pitch import (
+ ICASSP_2022_MODEL_PATH,
+ FilenameSuffix,
+ build_icassp_2022_model_path,
+)
from basic_pitch.inference import Model
@@ -31,15 +35,26 @@ def main() -> None:
"""Handle command line arguments. Entrypoint for this script."""
parser = argparse.ArgumentParser(description="Predict midi from audio.")
parser.add_argument("output_dir", type=str, help="directory to save outputs")
- parser.add_argument("audio_paths", type=str, nargs="+", help="Space separated paths to the input audio files.")
parser.add_argument(
- "--model_path",
+ "audio_paths",
+ type=str,
+ nargs="+",
+ help="Space separated paths to the input audio files.",
+ )
+ parser.add_argument(
+ "--model-path",
type=str,
default=ICASSP_2022_MODEL_PATH,
help="path to the saved model directory. Defaults to a ICASSP 2022 model. "
"The preferred model is determined by the first library available in "
"[tensorflow, coreml, tensorflow-lite, onnx]",
)
+ parser.add_argument(
+ "--model-type",
+ type=str,
+ choices=["tf", "coreml", "tflite", "onnx"],
+ help="If used, --model-path is ignored and instead the model serialization type" "specified is used.",
+ )
parser.add_argument(
"--save-midi",
action="store_true",
@@ -109,8 +124,17 @@ def main() -> None:
default=120,
help="The tempo for the midi file.",
)
- 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.")
+ 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()
print("")
@@ -122,7 +146,11 @@ def main() -> None:
# 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_path
+ from basic_pitch.inference import (
+ predict_and_save,
+ verify_output_dir,
+ verify_input_path,
+ )
output_dir = pathlib.Path(args.output_dir)
verify_output_dir(output_dir)
@@ -131,6 +159,11 @@ def main() -> None:
for audio_path in audio_path_list:
verify_input_path(audio_path)
+ if args.model_type:
+ model = Model(build_icassp_2022_model_path(FilenameSuffix[args.model_type]))
+ else:
+ model = Model(args.model_path)
+
try:
predict_and_save(
audio_path_list,
@@ -139,7 +172,7 @@ def main() -> None:
args.sonify_midi,
args.save_model_outputs,
args.save_note_events,
- Model(pathlib.Path(args.model_path)),
+ model,
args.onset_threshold,
args.frame_threshold,
args.minimum_note_length,