fix and improve this script : import os import subprocess from basicpitch import…
- User
fix and improve this script : import os import subprocess from basic_pitch import ICASSP_2022_MODEL_PATH from basic_pitch.inference import predict_and_save
def download_song_from_spotify(song_query): try: print("Downloading song...") subprocess.run(['spotdl', song_query], check=True)
# Cari file audio yang baru didownload downloaded_files = [f for f in os.listdir() if f.endswith('.mp3')] if len(downloaded_files) > 0: return downloaded_files[0] else: print("Failed to download song.") return None except subprocess.CalledProcessError as e: print(f"An error occurred while downloading the song: {e}") return Nonedef audio_to_midi_with_basic_pitch(audio_file, output_dir): if not os.path.exists(audio_file): print(f"Error: Audio file {audio_file} not found.") return
try: # Run inference and save MIDI file predict_and_save( [audio_file], # List of audio files output_directory=output_dir, # Where to save outputs save_midi=True, # Save the MIDI file sonify_midi=False, # Disable sonification save_model_outputs=False, # Skip saving model outputs save_notes=False, # Skip saving detected notes model_or_model_path=ICASSP_2022_MODEL_PATH # The model used for inference ) print("Conversion complete. MIDI file saved.") except Exception as e: print(f"An error occurred during audio to MIDI conversion: {e}")---- Main Program ----
if name == "main": song_query = input("Enter the song title or Spotify link: ")
# Step 1: Download the song downloaded_song = download_song_from_spotify(song_query) if downloaded_song: # Step 2: Convert audio to MIDI script_dir = os.path.dirname(os.path.abspath(__file__)) input_file = os.path.join(script_dir, downloaded_song) print("Converting audio to MIDI...") audio_to_midi_with_basic_pitch(input_file, script_dir) # Optional: Remove downloaded mp3 file after conversion if os.path.exists(input_file): os.remove(input_file) - Assistant