Files
pi-midi-zone/examples/04-single-note.py
T

32 lines
915 B
Python
Raw Normal View History

#!/usr/bin/env python3
"""
04-single-note.py - Single note example (from MIDIUtil docs, adapted)
Creates a minimal single-track MIDI file with one note.
Reference: examples/single-note-example.py from MIDIUtil repo
"""
from midiutil import MIDIFile
# Create the MIDIFile Object
my_midi = MIDIFile(1)
# Add track name and tempo
track = 0
time = 0
my_midi.addTrackName(track, time, "Sample Track")
my_midi.addTempo(track, time, 120)
# Add a single note
channel = 0 # MIDI channel 1
pitch = 60 # MIDI note number (60 = middle C / C4)
duration = 1 # Duration in beats (1 = quarter note)
volume = 100 # MIDI velocity 0-127
my_midi.addNote(track, channel, pitch, time, duration, volume)
# Write to disk
with open("single-note.mid", 'wb') as binfile:
my_midi.writeFile(binfile)
print("Written: single-note.mid")
print(f" Note: C4(60) on channel {channel}, {duration} beat(s) at 120 BPM")