| from basic_pitch.inference import predict_and_save |
| from basic_pitch import ICASSP_2022_MODEL_PATH |
| import os |
|
|
| class MidiConverter: |
| def __init__(self): |
| print("Initializing Basic Pitch for MIDI conversion...") |
| self.output_dir = "midi_output" |
|
|
| def convert(self, audio_path: str, output_path: str): |
| """ |
| Convert audio file to MIDI using Basic Pitch. |
| """ |
| try: |
| |
| |
| |
| |
| |
| |
| output_dir = os.path.dirname(output_path) |
| file_name_no_ext = os.path.splitext(os.path.basename(output_path))[0] |
| |
| print(f"Converting {audio_path} to MIDI...") |
| |
| |
| predict_and_save( |
| [audio_path], |
| output_dir, |
| True, |
| False, |
| False, |
| False, |
| ICASSP_2022_MODEL_PATH, |
| onset_threshold=0.6, |
| frame_threshold=0.4, |
| minimum_note_length=100.0, |
| minimum_frequency=None, |
| maximum_frequency=None |
| ) |
| |
| |
| |
| original_basename = os.path.splitext(os.path.basename(audio_path))[0] |
| generated_midi = os.path.join(output_dir, f"{original_basename}_basic_pitch.mid") |
| |
| if os.path.exists(generated_midi): |
| if os.path.exists(output_path): |
| os.remove(output_path) |
| os.rename(generated_midi, output_path) |
| print(f"MIDI saved to {output_path}") |
| return output_path |
| else: |
| print("Warning: Expected MIDI file not found.") |
| return None |
| |
| except Exception as e: |
| print(f"Error converting to MIDI: {e}") |
| import traceback |
| traceback.print_exc() |
| return None |
|
|