| import os |
| import warnings |
| import tempfile |
| from basic_pitch.inference import predict_and_save |
| from basic_pitch import ICASSP_2022_MODEL_PATH |
| import tensorflow as tf |
|
|
| |
| os.environ['CUDA_VISIBLE_DEVICES'] = '-1' |
| tf.config.set_visible_devices([], 'GPU') |
|
|
|
|
| |
| warnings.filterwarnings("ignore", category=RuntimeWarning) |
|
|
| |
| BASE_MIDI_DIR = "data/midi" |
|
|
|
|
| def inferir_basic_pitch(input_file: str) -> str: |
| """ |
| Procesa un archivo de audio y genera un archivo MIDI usando Basic Pitch. |
| |
| Args: |
| input_file: Ruta al archivo de audio de entrada |
| |
| Returns: |
| Ruta al archivo MIDI generado |
| """ |
| |
| os.makedirs(BASE_MIDI_DIR, exist_ok=True) |
| |
| try: |
| print(f"Procesando archivo: {input_file}") |
| print(f"Directorio de salida: {BASE_MIDI_DIR}") |
| |
| |
| if not os.path.exists(input_file): |
| print(f"Error: El archivo de entrada no existe: {input_file}") |
| return None |
| |
| |
| for f in os.listdir(BASE_MIDI_DIR): |
| if f.endswith('.mid'): |
| os.remove(os.path.join(BASE_MIDI_DIR, f)) |
| |
| |
| try: |
| predict_and_save( |
| model_or_model_path=ICASSP_2022_MODEL_PATH, |
| audio_path_list=[input_file], |
| output_directory=BASE_MIDI_DIR, |
| save_midi=True, |
| sonify_midi=False, |
| save_model_outputs=False, |
| save_notes=False, |
| ) |
| print("Predicci贸n completada") |
| except Exception as model_error: |
| print(f"Error espec铆fico del modelo: {model_error}") |
| |
| try: |
| print("Intentando configuraci贸n alternativa...") |
| predict_and_save( |
| audio_path_list=[input_file], |
| output_directory=BASE_MIDI_DIR, |
| save_midi=True, |
| sonify_midi=False, |
| save_model_outputs=False, |
| save_notes=False, |
| ) |
| print("Predicci贸n alternativa completada") |
| except Exception as alt_error: |
| print(f"Error en configuraci贸n alternativa: {alt_error}") |
| return None |
| |
| |
| generated_files = [f for f in os.listdir(BASE_MIDI_DIR) if f.endswith('.mid')] |
| print(f"Archivos generados: {generated_files}") |
| |
| if generated_files: |
| |
| latest_file = max([os.path.join(BASE_MIDI_DIR, f) for f in generated_files], |
| key=os.path.getctime) |
| print(f"Archivo MIDI encontrado: {latest_file}") |
| return latest_file |
| else: |
| print("No se gener贸 ning煤n archivo MIDI") |
| return None |
| |
| except Exception as e: |
| print(f"Error general durante la inferencia: {e}") |
| print(f"Tipo de error: {type(e).__name__}") |
| return None |
| |