dari script ini improve file midi yang didapatkan agar lebih sempurna, tidak hanya 1 1 note saja, agar lebih sesuai dengan mp yang di convert , buatkan dengan lengkap dan tepat : import os
import subprocess
import shutil
import tempfile
from basic_pitch import ICASSP_2022_MODEL_PATH
from basic_pitch.inference import predict_and_save

def download_song_from_spotify(song_query):
    """Download song from Spotify using spotdl."""
    tmpdirname = tempfile.mkdtemp()
    try:
        print("Downloading song...")
        # Download the song with spotdl and save it in the temporary directory
        subprocess.run([
            'spotdl', 'download', song_query,
            '--output', os.path.join(tmpdirname, '{artist} - {title}.{output-ext}')
        ], check=True)
        
        # Fetch the downloaded song
        downloaded_files = os.listdir(tmpdirname)
        if downloaded_files:
            song_file = os.path.join(tmpdirname, downloaded_files[0])
            return song_file, downloaded_files[0], tmpdirname  # Return song path, song file name, and temp directory
        else:
            print("Failed to download song.")
            return None, None, tmpdirname
    except subprocess.CalledProcessError as e:
        print(f"An error occurred while downloading the song: {e}")
        return None, None, tmpdirname

def audio_to_midi_with_basic_pitch(audio_file, output_dir, song_name):
    """Convert audio file to MIDI using Basic Pitch."""
    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
        )

        # The actual MIDI file created will have the format: original_file_name_basic_pitch.mid
        base_name = os.path.splitext(os.path.basename(audio_file))[0]  # Extract the base name of the audio file
        midi_file = os.path.join(output_dir, f"{base_name}_basic_pitch.mid")

        # Remove any file extension (e.g., .mp3) from the song name before renaming
        clean_song_name = os.path.splitext(song_name)[0]  # Strip the .mp3 extension
        new_midi_file = os.path.join(output_dir, f"{clean_song_name}.mid")
        os.rename(midi_file, new_midi_file)

        # Delete the original mp3 file after conversion
        if os.path.exists(audio_file):
            os.remove(audio_file)
            print(f"Deleted the original audio file: {audio_file}")

        print(f"Conversion complete. MIDI file saved as {new_midi_file}.")
    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, song_name, tmpdirname = download_song_from_spotify(song_query)

    if downloaded_song:
        # Step 2: Convert audio to MIDI
        current_dir = os.path.dirname(os.path.abspath(__file__))
        output_dir = os.path.join(current_dir, 'songs')
        
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)  # Create the 'songs' directory if it doesn't exist

        print("Converting audio to MIDI...")
        audio_to_midi_with_basic_pitch(downloaded_song, output_dir, song_name)

        # Clean up: Remove the temporary directory containing the downloaded song
        if os.path.exists(tmpdirname):
            shutil.rmtree(tmpdirname)
    else:
        print("Failed to download the song.")