-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
72 lines (61 loc) · 2.44 KB
/
helper.py
File metadata and controls
72 lines (61 loc) · 2.44 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
from music21 import converter, instrument, note, chord, stream
import numpy as np
def processData(data):
notes = []
for file in data:
try:
midi = converter.parse(file)
except:
print(file)
continue
notes_to_parse = None
parts = instrument.partitionByInstrument(midi)
if parts:
notes_to_parse = parts.parts[0].recurse()
else:
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element, note.Note):
notes.append(str(element.pitch))
elif isinstance(element, chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
return notes
def oneHotEncode(arr, n_labels):
return np.eye(n_labels, dtype = np.float32)[arr,:]
def getBatches(arr, batch_size, seq_length):
n_batches = len(arr) // (batch_size * seq_length)
if n_batches == 0:
print('Error: insufficient data!')
return
arr = arr[:n_batches * batch_size * seq_length] #remove elements we can't create full elements with
arr = arr.reshape((batch_size, -1)) #first dimension is batch size
for n in range(0, arr.shape[1], seq_length):
x = arr[:, n:n+seq_length] #Features
y = np.zeros_like(x)
try:
y[:, :-1], y[:, -1] = x[:, 1:], arr[:, n+seq_length] #shift targets by one
except IndexError: #Wrap it around instead
y[:, :-1], y[:, -1] = x[:, 1:], arr[:, 0]
yield x, y
def convertAndSaveMidi(predictions, file_save_to):
output_notes = []
offset = 0
for pattern in predictions:
if ('.' in pattern) or pattern.isdigit():
notes_in_chord = pattern.split('.')
notes = []
for current_note in notes_in_chord:
new_note = note.Note(int(current_note))
new_note.storedInstrument = instrument.Piano()
notes.append(new_note)
new_chord = chord.Chord(notes)
new_chord.offset = offset
output_notes.append(new_chord)
else:
new_note = note.Note(pattern)
new_note.offset = offset
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
offset = offset + 0.5
midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp = file_save_to)