Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Rock Me.mid
Binary file not shown.
Binary file added Tiger.mid
Binary file not shown.
21 changes: 15 additions & 6 deletions convert_midi.py → convert2lisp.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -76,4 +77,12 @@ def parse_to_lisp(qpiece, filename):
'G': 4,
'A': 5,
'B': 6
}
}

# 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)
2 changes: 2 additions & 0 deletions testmidi/sources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LMD_CLEAN
- ABBA
6 changes: 6 additions & 0 deletions toxml.sh
Original file line number Diff line number Diff line change
@@ -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