Post-Tonal Music Midi (File) Analyzer
Python script for analyzing MIDI files to determine their musical characteristics, with a special focus on identifying atonal tendencies and detecting twelve-tone rows.
The script uses the music21 library for musicological analysis and mido for MIDI file manipulation.
It also integrates with the Gemini API to generate a comprehensive, human-readable musicological report based on the raw analysis data.
- MIDI Analysis: Analyzes a given MIDI file for basic information such as the number of parts, total duration, note/chord counts, and key/time signatures.
- Atonal Tendency Detection: Provides quantitative data on atonal characteristics, including:
- Global key correlation score.
- Evenness of pitch-class distribution.
- Prevalence of dissonant melodic and harmonic intervals.
- Automated Segmentation: Splits the MIDI file into logical segments based on the completion of twelve-tone aggregates (i.e., when all 12 pitch classes have appeared).
- Twelve-Tone Row Analysis: For each segment, the script attempts to identify a "prime row" and then searches for occurrences of its 48 standard forms (Prime, Inversion, Retrograde, Retrograde-Inversion) within the music.
- Piano Reduction: Automatically generates a piano reduction of multi-part MIDI files to create a single-part score for a more focused analysis, if needed.
- AI-Powered Report Generation: Uses the Gemini API to synthesize all the raw analysis data into a cohesive, academic-style musicological report. The report discusses form, tonality, compositional techniques, and the extent of serial application.
Before running the script, you need to install the required Python libraries and set up your Gemini API key.
- Install Python Libraries:
pip install music21 mido python-dotenv google-generativeai
- Download and Install MuseScore:
The
music21library relies on a music notation software like MuseScore to render and display musical objects. You need to have it installed and configured on your system.- Download MuseScore.
- After installation,
music21should be able to find it automatically. If not, you may need to configure the path in yourmusic21settings.
- Set up Gemini API Key:
- Obtain an API key from the Google AI Studio.
- Create a file named
config.pyin the same directory as the script. - Add your API key to the
config.pyfile in the following format:
API_KEY = "YOUR_API_KEY_HERE"
- Run the script from the command line:
python main.py path/to/your/midi_file.mid
- Input Author and Title:
The script will prompt you to enter the composition's title and author, which will be included in the final report.
Enter composition title and author's name> Arnold Schoenberg - Piano Suite, Op. 25 - Review the Output:
The script will print the analysis progress and findings directly to the console. The final, comprehensive musicological report generated by Gemini will be saved to a text file in a new
analysis_reportsdirectory.
Assuming your MIDI file is named schoenberg_op25.mid:
python main.py schoenberg_op25.mid