#!/usr/bin/env python3 """ 01-basic-chord.py - MIDIUtil basic example Creates a simple chord progression in C major using MIDIUtil. """ from midiutil import MIDIFile # === Setup === degrees = [ [60, 64, 67], # C3 (C4, E4, G4) [62, 65, 69], # D3 (D4, F4, A4) [64, 67, 71], # E3 (E4, G4, B4) [65, 69, 72], # F3 (F4, A4, C5) ] # Chords: C, Dm, Em, F track = 0 # Track index channel = 0 # MIDI channel 1 time = 0 # Start at beat 0 duration = 1 # Each chord lasts 1 beat (quarter note) bpm = 120 volume = 100 # MIDI velocity 0-127 # === Create MIDI File === my_midi = MIDIFile(1) # 1 user track + auto tempo track = format 1 my_midi.addTempo(track, 0, bpm) my_midi.addTrackName(track, 0, "Basic Chord Progression") # === Add Notes === for chord in degrees: for pitch in chord: my_midi.addNote(track, channel, pitch, time, duration, volume) time += duration # Advance one beat per chord # === Write to Disk === with open("basic-chord.mid", "wb") as output_file: my_midi.writeFile(output_file) print("Written: basic-chord.mid")