-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
77 lines (59 loc) · 2.7 KB
/
Copy pathpreprocessing.py
File metadata and controls
77 lines (59 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- encoding: utf-8 -*-
from sys import argv
import midi
import math
from util import *
from boxes import *
def main(argv):
basename = argv[0]
box = eval(argv[1])
midi_file = "midi/%s.mid" % basename
print("Reading MIDI file: %s" % basename)
tracks = midi.read_midifile(midi_file)
print("Found %d tracks:" % len(tracks))
all_notes_on = []
all_notes_off = []
for i, track in enumerate(tracks):
notes_on = list(filter(lambda note: note.is_event(midi.events.NoteOnEvent.statusmsg), track))
notes_off = list(filter(lambda note: note.is_event(midi.events.NoteOffEvent.statusmsg), track))
print("- Track #%d: %d notes" % (i, len(list(notes_on))))
all_notes_on.extend(notes_on)
all_notes_off.extend(notes_off)
print("Total of %d notes" % len(all_notes_on))
print("Song is %d beats long" % beats(tracks))
all_distances = []
for key in range(-48, 49):
pitches = list(map(lambda note: note.pitch + key, all_notes_on))
distances = map(box.distance, pitches)
try:
average_distance = sum(distances) / len(pitches)
except ZeroDivisionError:
average_distance = 0
print("Error! Zero Division Error!")
all_distances.append((average_distance, key))
print("Size of all_distances: %d" % len(all_distances))
best_distance, transpose = min(all_distances, key=lambda t: t[0])
print("Best distance %f with transpose key %d, transposing" % (best_distance, transpose))
for note in all_notes_on:
closest = box.closest(note.pitch + transpose)
note.set_pitch(closest)
# write to file
sweetspot = 2.15 # use this for songs without semiquaver
# sweetspot = 4.6 # use this for songs with semiquaver
notes_perbeat = dict((beat,[]) for beat in range(beats(tracks)*math.floor(sweetspot)+5))
for i in range(len(tracks)):
# if i == 1: # uncomment this if you want to only output certain tracks
tickbeat = 0
notes_on = list(filter(lambda note: note.is_event(midi.events.NoteOnEvent.statusmsg), tracks[i]))
for note in notes_on:
tickbeat += int(sweetspot*((note.tick + 1)/tracks.resolution))
# print(note, start)
if note.velocity > 0:
notes_perbeat[tickbeat].append(note.pitch)
file = open("txt/%s_%s.txt" % (basename, box.symbol), "w")
for notes_list in notes_perbeat.values():
# print(notes_list)
newlist = list(dict.fromkeys(notes_list)) # remove duplicate notes
file.write(" ".join(str(note) for note in newlist) + "\n")
if __name__ == '__main__':
main(argv[1:])