-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicting.py
73 lines (64 loc) · 2.94 KB
/
predicting.py
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
#IMPORT RELEVANT MODULES
import pandas as pd
import numpy as np
import preprocessing
from midiutil import MIDIFile
#-------------------------------------------------------------------------------------------------------
#FUNCTIONS
def createAnnotation(yPred, frameduration, audiopath, audiofilename, noteMin):
track = 0
channel = 0
time = 0
tempo = 60
MyMIDI = MIDIFile(1)
MyMIDI.addTempo(track, time, tempo)
lengthAudioFrames = len(yPred)
times = np.arange(0.00, lengthAudioFrames, frameduration)
times = ["%.2f" % (round(t, 2)) for t in times]
offset = (preprocessing.getMIDIfromNote(noteMin)) - 1
notesPred = preprocessing.getNotesfromF0Labels(yPred, offset)
startTimesList = []
endTimesList = []
notesList = []
for index, note in enumerate(notesPred):
if(note == 'None'):
continue
else:
if(note == notesPred[index-1] and note == notesPred[index+1]):
continue
else:
if(note != notesPred[index-1] and note != notesPred[index+1]):
startTimesList.append(times[index])
notesList.append(note)
endTimesList.append(times[index])
MIDIpitch = preprocessing.getMIDIfromNote(note)
MIDItime = float(times[index])
MIDIduration = 0.01
MyMIDI.addNote(track, channel, MIDIpitch, MIDItime, MIDIduration, volume=100)
elif(note != notesPred[index-1] and note == notesPred[index+1]):
startTimesList.append(times[index])
notesList.append(note)
elif(note == notesPred[index-1] and note != notesPred[index+1]):
endTimesList.append(times[index])
MIDIpitch = preprocessing.getMIDIfromNote(note)
MIDItime = float(startTimesList[-1])
MIDIduration = float(times[index]) - MIDItime + 0.01
MyMIDI.addNote(track, channel, MIDIpitch, MIDItime, MIDIduration, volume=100)
startTimes = pd.DataFrame(startTimesList)
endTimes = pd.DataFrame(endTimesList)
notes = pd.DataFrame(notesList)
groundTruthClean = pd.concat([startTimes, endTimes, notes], axis=1)
groundTruthClean.columns = ["Start Time (s)", "End Time (s)", "Note"]
fileIsol = preprocessing.extractFileName(audiofilename)
groundTruthClean.to_csv(audiopath+'/'+fileIsol+'.csv', sep=',')
with open(audiopath+'/'+fileIsol+".mid", "wb") as outputFile:
MyMIDI.writeFile(outputFile)
print("Created groundtruth .csv and MIDI under filename in project filepath")
def predictOutputSingle(model, X):
probabilities = model.predict(X)
predictions = probabilities.argmax(axis=-1)
return predictions
def predictOutputMT(model, X):
probabilities = model.predict([X, X])[0]
predictions = probabilities.argmax(axis=-1)
return predictions