This Python script generates a MIDI file based on musical notes generated according to a specified musical genre. It utilizes the music21
library for music theory and MIDI file handling, along with the random
and os
modules.
The script consists of two main functions:
generate_midi(notes, filename)
: Creates a MIDI file from a list of note names.generate_notes_by_genre(genre, length)
: Generates a list of note names based on the specified musical genre.At the end of the script, there is an example usage where the user is prompted to enter a genre, and the script generates a MIDI file named genre_output.mid
based on that genre.
music21
: A Python library for computer-aided musicology, which provides tools to analyze, create, and manipulate music.random
: Used to generate random choices for notes and scales.os
: Provides functions for interacting with the operating system, such as file path manipulation.generate_midi(notes, filename="output.mid")
Generates a MIDI file from a list of note names.
notes
: A list of note names as strings (e.g., ['C4', 'D4', 'E4']
).filename
: The name of the MIDI file to create. Defaults to "output.mid"
.Create a Stream: Initializes a Stream
object from music21
to hold musical elements.
2. Add Notes to the Stream: Iterates over the list of note names, creates a Note
object for each, and appends it to the stream.
3. Translate Stream to MIDI: Converts the stream into a MIDI file object.
4. Determine File Path: Gets the current working directory and constructs the full file path for the MIDI file.
5. Write MIDI File: Opens the file in write-binary mode, writes the MIDI data, and closes the file.
6. Confirmation Message: Prints a message indicating the MIDI file has been created. The message is in Czech:
Translation: „The MIDI file was created at: [file_path]“
generate_notes_by_genre(genre, length=10)
Generates a list of note names based on the specified musical genre.
genre
: A string indicating the musical genre (e.g., „rock“, „pop“).length
: The number of notes to generate. Defaults to 10
.Define Scales and Rhythms Based on Genre:
Rock:
Pop:
Other Genres (Default):
2. Initialize Notes List and Select a Scale:
3. Generate Notes:
length
times to generate the specified number of notes.current_scale
.rhythms
.notes
list.Note: In this implementation, the note durations are not utilized when generating the MIDI file; only the note pitches are used.
4. Return the List of Notes:
Example Usage
User Input:
The script prompts the user to enter a genre. The prompt is in Czech:
Translation: „Enter a genre (rock, pop, or other and press Enter): „
2. Generate Notes:
generate_notes_by_genre
with the user’s input and generates a list of 20 notes.3. Generate MIDI File:
generate_midi
with the generated notes and creates a MIDI file named "genre_output.mid"
.Install Dependencies:
Ensure you have music21
installed:
2. Run the Script:
Execute the script in a Python environment.
3. Enter a Genre:
"rock"
, "pop"
, or any other text for the default option.4. Output:
genre_output.mid
in the current working directory.Genre Customization:
Rhythm Handling:
rhythms
lists, the current implementation does not apply note durations when adding notes to the MIDI file. To incorporate rhythms, additional code is needed to set the duration of each note.Internationalization:
Some prompts and messages are in Czech. You may translate them to English or any other language as needed.
Input Prompt:
Confirmation Message:
Enhancements:
Incorporate Note Durations:
generate_midi
function to handle note durations based on the rhythm patterns.Expand Genre Support:
Error Handling:
User Interface:
To make the script utilize note durations from the rhythm patterns, you can modify the generate_notes_by_genre
function to return a list of tuples containing note names and durations.
generate_notes_by_genre
FunctionModified generate_midi
Function
Adjusted Example Usage
This script provides a basic framework for generating MIDI files based on musical genres. By leveraging the music21
library, it allows for music theory concepts like scales and rhythms to be incorporated into the generated music. With further development and customization, it can be expanded into a more sophisticated music generation tool.
Hit 'Generate & Copy' button to generate embed code. It will be copied to your Clipboard. You can now paste this embed code inside your website's HTML where you want to show the List.