Last active
April 6, 2016 19:23
-
-
Save Artoria2e5/23555b476e72dc5aaa5f to your computer and use it in GitHub Desktop.
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 python | |
from __future__ import division, print_function, unicode_literals | |
from textwrap import dedent | |
import itertools | |
from mido2create import * | |
import operator | |
def mido_c(it, slot=15): | |
_br = True | |
yield '/** BEGIN GENERATED SONG */' | |
while _br: | |
T = list(itertools.islice(it, 0, 16)) | |
L = len(T) | |
if L is 0: | |
break | |
yield '/* SONG SLOT=%d LEN=%d NOTES... */' % (slot, L) | |
yield 'create_write_bytes(%d, "%s");' % (2 * L + 3, | |
reduce(operator.add, ("\\x%02x" % byte for byte in ([140,slot, L] + | |
reduce(operator.add, (list(a) for a in T)))))) | |
yield 'create_play_song(%d);' % (slot) | |
yield 'msleep(%dL);' % (sum(a[1] for a in T) * 64000) | |
yield '/** END OF GENERATED SONG */' | |
def mido_cfunc(it, slot=15, funcname='mid', indent=' '): | |
yield '/** Song on slot %d, try thread on it! */' % (slot) | |
yield 'void %s(void) {' % funcname | |
for i in mido_c(it, slot = slot): | |
yield indent + i | |
yield indent + 'return;' | |
yield '}' | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) == 1: | |
print('%s [MIDS...]' % sys.argv[0]) | |
else: | |
print(dedent('''\ | |
/** Create 'set-song' helpers */ | |
static void create_write_bytes(int len, char* bytes); | |
inline static void create_write_bytes(int len, char* bytes){ int i; for (i=0; i<len; i++) create_write_byte(bytes[i]); } | |
#define create_play_song(x) CreatePlaySongEx(x) | |
int CreatePlaySongEx(char songID){ | |
create_write_byte(141); | |
create_write_byte(songID); | |
} | |
''')) | |
for i in sys.argv[1:]: | |
for j in mido_cfunc(mido2create(MidiFile(i)), funcname=('mid_' + i[:-4])): | |
print(j) |
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 python | |
from __future__ import division, print_function, unicode_literals | |
from mido import MidiFile | |
def mido2create(midobj): | |
tick = 0 | |
note = 0 | |
tempo = 500 * 1000 | |
for msg in midobj: | |
tick += msg.time | |
if msg.type == 'set_tempo': | |
tempo = msg.tempo | |
elif msg.type == 'note_on': | |
# We can only accept one note at a time... | |
if note == 0: | |
tick = 0 | |
note = msg.note | |
elif msg.type == 'note_off': | |
if note == msg.note: | |
yield (note, int(tick * tempo * 64 / 1000000)) | |
note = 0 | |
tick = 0 | |
else: | |
pass | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) == 1: | |
print('%s [FILES...]' % sys.argv[0]) | |
else: | |
for i in sys.argv[1:]: | |
print('%s = %s' % (i[:-4], repr(list(mido2create(MidiFile(i)))))) |
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 python | |
import pycreate # https://github.com/mgobryan/pycreate | |
import itertools | |
def play_trunk(it, cr, ports): | |
# not a good idea.... | |
# why don't they provide hasnext...... | |
while it.__length_hint__() > 0: | |
for i in ports: | |
cr.setSong(i, list(itertools.islice(it, 0, 16))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment