diff --git a/Rock Me.mid b/Rock Me.mid new file mode 100644 index 0000000..390217a Binary files /dev/null and b/Rock Me.mid differ diff --git a/Tiger.mid b/Tiger.mid new file mode 100644 index 0000000..4ce73e5 Binary files /dev/null and b/Tiger.mid differ diff --git a/convert_midi.py b/convert2lisp.py similarity index 77% rename from convert_midi.py rename to convert2lisp.py index 07dc0c4..4c51e5f 100644 --- a/convert_midi.py +++ b/convert2lisp.py @@ -1,14 +1,14 @@ import music21 as m21 import csv import math - +from fractions import Fraction def parse_midi(midipiece): + ```Parse a .mid file to quantised format``` piece = m21.converter.parse(midipiece) qpiece = piece.quantize() filename = midipiece[:-4] - parse_to_lisp(qpiece, filename) - return parse_to_csv(qpiece, filename) + return qpiece, filename def parse_to_csv(qpiece, filename): @@ -39,6 +39,7 @@ def parse_to_csv(qpiece, filename): def parse_to_lisp(qpiece, filename): + '''Take a quantised piece and output a tuple of monophonic, polyphonic lisp format data''' monophonic_lisp = [] collect_lisp = [] monophonic = next((part for part in qpiece if not [p for p in part.recurse().notes if len(p.pitches) > 1]), None) @@ -54,13 +55,13 @@ def parse_to_lisp(qpiece, filename): for event in part.recurse().notes: try: diatonic_pitch = diatonic_pitch_lookup[event.pitch.step] + math.floor(event.pitch.midi/12) * 7 - 12 - collect_lisp.append((event.offset, event.pitch.midi-21, diatonic_pitch, event.quarterLength, voice)) + collect_lisp.append((str(event.offset), event.pitch.midi-21, diatonic_pitch, str(event.quarterLength), voice)) except: # this event is a chord pitches = event.pitches for p in pitches: diatonic_pitch = diatonic_pitch_lookup[p.step] + math.floor(p.midi/12) * 7 - 12 - collect_lisp.append((event.offset, p.midi - 21, diatonic_pitch, event.quarterLength, voice)) + collect_lisp.append((str(event.offset), p.midi-21, diatonic_pitch, str(event.quarterLength), voice)) polyphonic_lisp = sorted(collect_lisp, key=lambda k: k[0]) with open(filename+'_poly.txt', "w+") as f: for p_tuple in polyphonic_lisp: @@ -76,4 +77,12 @@ def parse_to_lisp(qpiece, filename): 'G': 4, 'A': 5, 'B': 6 -} \ No newline at end of file +} + +# Example usage +# address = "/home/irisren/Dropbox/111Projects/MIREX/2018Prediction/testmidi/Rock Me.mid" (Work with str Fraction) +# address = "/home/irisren/Dropbox/111Projects/MIREX/2018Prediction/testmidi/Tiger.mid" (Gives float directly without the Fraction) +# o = parse_midi(address)[0] +# oname = parse_midi(address)[1] +# olisp = parse_to_lisp(o, oname) +# print(olisp) diff --git a/testmidi/sources.txt b/testmidi/sources.txt new file mode 100644 index 0000000..4c1de9c --- /dev/null +++ b/testmidi/sources.txt @@ -0,0 +1,2 @@ +LMD_CLEAN + - ABBA diff --git a/toxml.sh b/toxml.sh new file mode 100644 index 0000000..be9a345 --- /dev/null +++ b/toxml.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +find ./clean_midi -type f -name "*.mid" -print0 | while IFS= read -r -d '' file; do + printf '%s\n' "$file" + mscore "$file" -o "$file.musicxml" +done