Last active
May 21, 2024 20:00
-
-
Save nst/bccc3dc2d2318cf637e65e1a03c1c9f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# midascii.py - Listen to ASCII strings as MIDI notes | |
# Nicolas Seriot - 2024-05-14 | |
# https://gist.github.com/nst/bccc3dc2d2318cf637e65e1a03c1c9f7 | |
# Minimal midi file thanks to @corkami | |
# https://github.com/corkami/pics/blob/master/binary/midi.png | |
def ascii_to_midi(s, filename): | |
header = list(b'MThd') + [0,0,0,6,0,1,0,1,0,60] # ticks per quarter note | |
notes = [] | |
for c in s: | |
print(c, ord(c)) | |
notes += [0, 0x90, ord(c), 127, # note on | |
30, 0x90, ord(c), 0] # note off | |
prog_change = [0, 0xC0, 0x22] # Electric Picked Bass | |
end_of_track = [0, 0xFF, 0x2F, 0] | |
length = len(prog_change) + len(notes) + len(end_of_track) | |
track = list(b'MTrk') + [0, 0, length >> 8, length & 0xFF] | |
midi = header + track + prog_change + notes + end_of_track | |
with open(filename, "wb") as f: | |
bytes = bytearray(midi) | |
f.write(bytes) | |
if __name__ == "__main__": | |
# https://seriot.ch/midi/pi.mid | |
import math | |
s = str(math.pi) | |
#s = "01234" | |
#s = "Nicolas Seriot http://seriot.ch" | |
ascii_to_midi(s, "x.mid") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment