4

Essentially, I'm trying to create MIDI's from scratch and put them online. I'm open to different languages, but prefer to use Python (one of the 2's, if that makes any difference.) and was wondering what library I should use. Thanks in advance!

1

4 Answers 4

9

Looks like this is what you are looking for:

Simple, Cross Platform MIDI Library for Python

MIDIUtil can create MIDI files in python. See:

To install, use pip install MIDIUtil

Sign up to request clarification or add additional context in comments.

2 Comments

Can it do multiple instruments? Sorry, I'm a complete noob to sound/music programming.
Literally the first paragraph of the website I posted, "MIDIUtil is a pure Python library that allows one to write muti-track Musical Instrument Digital Interface (MIDI) files from within Python programs. It is object-oriented and allows one to create and write these files with a minimum of fuss." :PP
-1
from midiutil import MIDIFile

def create_midi(chord_progression, file_name='jazz_piano_progression.mid', tempo=120):
    # Create a MIDI file
    midi = MIDIFile(1)
    midi.addTempo(0, 0, tempo)

    # Define the chords and their durations
    chords = [
        {'pitch': [60, 63, 67, 70], 'duration': 4},  # Cm7
        {'pitch': [65, 68, 72, 75], 'duration': 4},  # Fm7
        {'pitch': [70, 74, 77, 81], 'duration': 4},  # Bb7
        {'pitch': [64, 68, 71, 75], 'duration': 4}   # Ebmaj7
    ]

    time = 0
    for chord in chords:
        for pitch in chord['pitch']:
            midi.addNote(0, 0, pitch, time, chord['duration'], 100)
        time += chord['duration']

    # Save the MIDI file
    with open(file_name, 'wb') as midi_file:
        midi.writeFile(midi_file)

# Generate MIDI file with the specified chord progression
create_midi([{'Cm7': ['C', 'E♭', 'G', 'B♭']},
             {'Fm7': ['F', 'A♭', 'C', 'E♭']},
             {'Bb7': ['B♭', 'D', 'F', 'A♭']},
             {'Ebmaj7': ['E♭', 'G', 'B♭', 'D']}
             ], 'jazz_piano_progression.mid', tempo=120)

Comments

-1
from midiutil import MIDIFile

note_map = {'C': 0, 'C#': 1, 'D': 2, 'D#': 3, 'E': 4, 'F': 5, 'F#': 6, 'G': 7, 'G#': 8, 'A': 9, 'A#': 10, 'B': 11}
def note_to_midi(note):
    pitch = note[:-1]
    octave = int(note[-1])
    return 12 + octave * 12 + note_map[pitch]

midi = MIDIFile(4)
tempo = 120
for track in range(4):
    midi.addTempo(track, 0, tempo)

# Acordes
chords = [
    [('F4', 0), ('A4', 0), ('C5', 0)],
    [('G4', 4), ('B4', 4), ('D5', 4)],
    [('C4', 8), ('E4', 8), ('G4', 8)],
    [('A4', 12), ('C5', 12), ('E5', 12)]
]
for chord in chords:
    for note, time in chord:
        midi.addNote(2, 0, note_to_midi(note), time, 4, 100)

# Baixo
bassline = [('F2', 0), ('G2', 4), ('C2', 8), ('A2', 12)]
for note, time in bassline:
    midi.addNote(1, 0, note_to_midi(note), time, 4, 100)

# Melodia
melody = [
    ('C5', 0), ('D5', 1), ('E5', 2), ('E5', 3), ('G5', 4), ('G5', 5), ('F5', 6), ('E5', 7),
    ('D5', 8), ('C5', 9), ('E5', 10), ('F5', 11), ('G5', 12), ('G5', 13), ('A5', 14),
    ('A5', 15), ('G5', 16), ('E5', 17), ('F5', 18), ('G5', 19), ('E5', 20), ('D5', 21),
    ('C5', 22), ('C5', 23), ('D5', 24), ('E5', 25), ('G5', 26), ('F5', 27), ('E5', 28),
    ('D5', 29), ('E5', 30), ('F5', 31), ('G5', 32), ('G5', 33), ('F5', 34), ('D5', 35), ('C5', 36)
]
for note, time in melody:
    midi.addNote(3, 0, note_to_midi(note), time, 1, 100)

# Bateria (kick simples)
drums = [(36, i) for i in range(0, 16)]
for note, time in drums:
    midi.addNote(0, 9, note, time, 1, 100)

# Exportar
with open("around_the_world_full_arrangement.mid", "wb") as f:
    midi.writeFile(f)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

Perl has the module MIDI::Simple. I remember an article about it in the now extinct The Perl Journal:

http://www.foo.be/docs/tpj/issues/vol4_1/tpj0401-0018.html

1 Comment

They asked for a solution in Python.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.