Saturday, January 18, 2025

Convert MP3 to MIDI Using Spotify’s BasicPitch and TensorFlow

Have you ever wanted to convert an MP3 file into a MIDI format for music production, transcription, or remixing?

BasicPitch by Spotify is an open-source AI-powered tool that makes this process simple and accurate.

With just a few lines of Python code, you can extract notes and melodies from an audio file and use them in a Digital Audio Workstation (DAW) or for further analysis. Let’s dive in.


What is BasicPitch?

BasicPitch is an AI-powered polyphonic pitch detection model developed by Spotify. Unlike traditional MIDI conversion tools, BasicPitch:

  • Detects multiple notes at once (polyphonic transcription)
  • Understands pitch bends and vibrato
  • Works with various instruments, not just piano
  • Is lightweight and open-source

More about BasicPitch:


Steps to Convert MP3 to MIDI

1. Install Dependencies

First, install the required Python packages:


pip install basic-pitch ffmpeg-python


2. Use This Python Script

This script will process the MP3 file, run BasicPitch’s AI model, and save the MIDI file.

import os
from basic_pitch.inference import predict_and_save from basic_pitch import ICASSP_2022_MODEL_PATH # -------------------------------------- # MP3 to MIDI Conversion using BasicPitch by Spotify # This script processes an MP3 file using BasicPitch # and saves the resulting MIDI file in the output directory. # -------------------------------------- # Define the input MP3 file path (Replace "song.mp3" with your actual file) input_mp3 = r"song.mp3" # Ensure the file is in the same directory or provide a full path # Define the output directory where MIDI files will be saved output_dir = r"output" # Check if the input MP3 file exists before proceeding if not os.path.exists(input_mp3): raise FileNotFoundError(f"Error: The file '{input_mp3}' does not exist. Please check the path.") # Load the BasicPitch model basic_pitch_model = str(ICASSP_2022_MODEL_PATH) # -------------------------------------- # Running the BasicPitch Inference Model # The model will analyze the MP3 file and generate a MIDI file. # -------------------------------------- print("Running BasicPitch inference on the MP3 file...") predict_and_save( [input_mp3], # Use MP3 directly, no need to convert to WAV manually output_dir, # Save MIDI files in the output directory save_midi=True, # Enable saving the MIDI output sonify_midi=False, # Disable MIDI sonification (audio rendering) save_model_outputs=False, # Disable saving raw model outputs save_notes=False, # Disable saving individual note files model_or_model_path=basic_pitch_model, # Load BasicPitch model ) # MIDI conversion complete. Check the output folder. print(f"Conversion complete. Check the '{output_dir}' folder for the MIDI files.")

Setup Instructions

Why Use BasicPitch Instead of Other MIDI Conversion Tools?

Feature      BasicPitch (Spotify)Other Tools
AI-powered      Yes               Mostly rule-based
Polyphonic (Multiple Notes)      Yes               Mostly monophonic
Pitch Bends and Vibrato      Yes               No or limited
Open Source      Yes               Often paid
Lightweight and Fast      Yes               Some require heavy processing

Real-World Use Cases

  • Convert guitar recordings to MIDI for editing in DAWs like Ableton and FL Studio
  • Transcribe piano melodies into MIDI for remixing or re-orchestrating
  • Turn vocal hums into music notes to create melodies from scratch
  • Use for music education and research to analyze complex musical pieces

What’s Next?

Once your MP3 is converted to MIDI, you can:

  • Import it into DAWs like Ableton, FL Studio, Logic Pro, or GarageBand
  • Assign virtual instruments to the MIDI notes and create new sounds
  • Use it for sheet music transcription and study compositions

Have you tried BasicPitch yet? What are your thoughts? Let me know in the comments.


Useful Resources

No comments:

Post a Comment