Created
March 13, 2016 14:10
-
-
Save JackDesBwa/3da8056f8810f50de920 to your computer and use it in GitHub Desktop.
Script to convert midi files into C structure exploitable by (e.g.) microcontrollers
This file contains 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 python2 | |
# This script uses mido <https://pypi.python.org/pypi/mido/1.1.3> to | |
# transform a midi file into a C structure to be played by a | |
# microcontroller firmware for example | |
from mido import MidiFile | |
import sys | |
if len(sys.argv) != 2: | |
sys.stdout.write("Usage: {0} <midi_file>\n".format(sys.argv[0])) | |
exit(1) | |
sys.stdout.write(""" | |
typedef struct st_melody { | |
char note_on; ///< Is note on (0 = OFF ; 1 = ON) | |
char channel; ///< Note midi channel | |
char note; ///< Note midi tone | |
int duration; ///< Time to wait (ms) until next note action | |
} melody_t; | |
""") | |
sys.stdout.write("\nmelody_t melody[] = {\n") | |
for message in MidiFile(sys.argv[1]): | |
if message.type != 'note_on' and message.type != 'note_off': | |
continue | |
note_on = '1' | |
if message.type == 'note_off': | |
note_on = '0' | |
sys.stdout.write(' {') | |
sys.stdout.write(note_on) | |
sys.stdout.write(', ') | |
sys.stdout.write(str(message.channel)) | |
sys.stdout.write(', ') | |
sys.stdout.write(str(message.note)) | |
sys.stdout.write(', ') | |
sys.stdout.write(str(int(message.time*1000))) | |
sys.stdout.write('},\n') | |
sys.stdout.write('};\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment