Advertisement
FlyFar

midi2bin.py

Mar 21st, 2023
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | Cybersecurity | 0 0
  1. import midi, sys
  2.  
  3. pattern = midi.read_midifile(sys.argv[1])
  4.  
  5. def pitchconv(pitch):
  6.     return int(round(1193180.0 / (2**((pitch-69)/12.0)*440), 0))
  7.  
  8. with open(sys.argv[2], "wb") as out:
  9.     pitches = [pitchconv(event.pitch) for event in pattern[1] if isinstance(event, midi.NoteOnEvent)]
  10.  
  11.     b = 0
  12.     d = 0
  13.    
  14.     t = 0
  15.     o = -1
  16.     for event in pattern[1]:
  17.         if isinstance(event, midi.NoteOnEvent):
  18.             if event.velocity == 0:
  19.                 d += int(round(event.tick/48.0, 0))
  20.                 p = pitchconv(event.pitch)
  21.                 out.write(chr(p & 0xff) + chr(d << 5 | p >> 8))
  22.                 b = 0
  23.             else:
  24.                 d = int(round(event.tick/48.0, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement