-
Notifications
You must be signed in to change notification settings - Fork 0
/
midiexport.py
883 lines (665 loc) · 31.3 KB
/
midiexport.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
import mido
from scipy.io.wavfile import read
import scipy
import time
import librosa
import numpy as np
from numpy import diff
import pandas as pd
import xlsxwriter
import matplotlib.pyplot as plt
from scipy import signal
import madmom
"""
Here, we define several methods to take an audio file and a midi file
and turn them into a set of spectrograms (X), and a set of labels for each frame (y)
At the end, for the generation of the dataset of the models,
only "get_onset_data_th_pt_conv_cqt", and "get_onset_data_th_pt_conv_fft_filtered" were used
"""
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
def normalized(a):
normalized_array = (a - np.min(a))/(np.max(a)-np.min(a))
return normalized_array
def get_data(midi_file,audio_file):
mid = mido.MidiFile(midi_file)
window_size=2048
number_of_midi_notes = 128
notes = np.zeros((1,number_of_midi_notes),dtype=int)
midi = np.zeros((1,number_of_midi_notes),dtype=int)
print("Shape of midi is: "+ str(notes.shape))
print("Starting STFT")
start_time = time.time()
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
print("The STFT done: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
print("Starting to decode the MIDI file")
start_time = time.time()
window_on_hold=0
for msg in mid:
windows_since_last_msg = msg.time * samplerate / window_size
window_on_hold += windows_since_last_msg-int(windows_since_last_msg)
if(window_on_hold>=1):
windows_since_last_msg += window_on_hold
window_on_hold=0
notes_copy = notes.copy()
notes_copy = np.tile(notes_copy,(int(windows_since_last_msg),1))
midi = np.concatenate((midi,notes_copy.copy()),axis=0)
if(msg.type == 'note_on'):
#notes[msg.note] = msg.velocity
notes[0][msg.note] = 1 #note is either on or off
elif(msg.type == 'note_off'):
notes[0][msg.note] = 0
midi = midi[:-(len(midi)-len(S[0]))]
print("Shape of midi is: " + str(midi.shape))
print("The MIDI file has been decoded: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
S = np.transpose(S)
return S,midi
def get_onset_data_th_pt(midi_file,audio_file,window_size):
#returns: X as the STFT frames
#y as the [therapist][patient]
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
print("Starting STFT")
start_time = time.time()
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
S = np.transpose(S)
print("The STFT done: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
S_with_extra_features = np.zeros((len(S),1026))
print("Starting to decode the MIDI file")
start_time = time.time()
print("Midi length is: "+ str(mid.length))
midi = np.zeros((int(round(mid.length*samplerate)),2),dtype=int)
sample_no = 0
for msg in mid:
sample_no = sample_no+int(round(msg.time*samplerate))
if(msg.type == 'note_on'):
#print(msg)
if(msg.note > 67):
midi[sample_no][1] = 1 #patient note
patient_onset_sample_numbers.append(sample_no)
#print('patient note at: '+ str(sample_no/44100)+'s\n')
elif(msg.note <= 67 and msg.note >= 0):
midi[sample_no][0] = 1 #therapist note
therapist_onset_sample_numbers.append(sample_no)
#print('therapist note at: '+ str(sample_no/44100)+'s\n')
framed_midi = np.zeros((len(S),2),dtype=int)
for sample_no in therapist_onset_sample_numbers:
#print("therapist note in frame: "+str(int(sample_no/window_size)))
if(int(sample_no/window_size)+1 < len(S)):
framed_midi[int(sample_no/window_size)+1][0] = 1
for sample_no in patient_onset_sample_numbers:
#print("patient note in frame: "+str(int(sample_no/window_size)))
if(int(sample_no/window_size)+1 < len(S)):
framed_midi[int(sample_no/window_size)+1][1] = 1
for i in range(len(S)):
if(i!=0):
S_with_extra_features[i] = np.concatenate((S[i],[np.average(S[i])-np.average(S[i-1])]),axis=0)
elif(i==0):
S_with_extra_features[i] = np.concatenate((S[i],[0]),axis=0)
print("The MIDI file has been decoded: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
print("Shape of midi is: " + str(framed_midi.shape))
print("Shape of signal is: " + str(S_with_extra_features.shape))
return S_with_extra_features,framed_midi
def get_onset_data_th_pt_exp(midi_file,audio_file,window_size):
#returns: X as the difference between the next and the current STFT frames and the average increase for the frame
#y as the [therapist][patient]
#the window size can be changed
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
print("Starting STFT")
start_time = time.time()
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
S = np.transpose(S)
print("The STFT done: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
S_with_extra_features = np.zeros((len(S),(int(window_size/2)+2)))
print("Starting to decode the MIDI file")
start_time = time.time()
print("Midi length is: "+ str(mid.length))
midi = np.zeros((int(round(mid.length*samplerate)),2),dtype=int)
sample_no = 0
for msg in mid:
sample_no = sample_no+int(round(msg.time*samplerate))
if(msg.type == 'note_on'):
#print(msg)
if(msg.note > 67):
midi[sample_no][1] = 1 #patient note
patient_onset_sample_numbers.append(sample_no)
#print('patient note at: '+ str(sample_no/44100)+'s\n')
elif(msg.note <= 67 and msg.note >= 0):
midi[sample_no][0] = 1 #therapist note
therapist_onset_sample_numbers.append(sample_no)
#print('therapist note at: '+ str(sample_no/44100)+'s\n')
framed_midi = np.zeros((len(S),2),dtype=int)
for sample_no in therapist_onset_sample_numbers:
#print("therapist note in frame: "+str(int(sample_no/window_size)))
framed_midi[int(sample_no/window_size)][0] = 1
for sample_no in patient_onset_sample_numbers:
#print("patient note in frame: "+str(int(sample_no/window_size)))
framed_midi[int(sample_no/window_size)][1] = 1
for i in range(len(S)):
if(i<(len(S)-1)):
S_with_extra_features[i] = np.concatenate((S[i+1]-S[i],[np.average(S[i+1])-np.average(S[i])]),axis=0)
elif(i == len(S)-1):
S_with_extra_features[i] = np.zeros((S_with_extra_features[i].shape))
print("The MIDI file has been decoded: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
print("Shape of midi is: " + str(framed_midi.shape))
print("Shape of signal is: " + str(S_with_extra_features.shape))
return S_with_extra_features,framed_midi
def get_onset_data_th_pt_exp2(midi_file,audio_file,window_size):
#returns: X as the STFT frames and the average increase for the frame
#y as the [therapist][patient]
#window_size can be changed
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
print("Starting STFT")
start_time = time.time()
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
S = np.transpose(S)
print("The STFT done: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
S_with_extra_features = np.zeros((len(S),(int(window_size/2)+2)))
print("Starting to decode the MIDI file")
start_time = time.time()
print("Midi length is: "+ str(mid.length))
midi = np.zeros((int(round(mid.length*samplerate)),2),dtype=int)
sample_no = 0
for msg in mid:
sample_no = sample_no+int(round(msg.time*samplerate))
if(msg.type == 'note_on'):
#print(msg)
if(msg.note > 67):
midi[sample_no][1] = 1 #patient note
patient_onset_sample_numbers.append(sample_no)
#print('patient note at: '+ str(sample_no/44100)+'s\n')
elif(msg.note <= 67 and msg.note >= 0):
midi[sample_no][0] = 1 #therapist note
therapist_onset_sample_numbers.append(sample_no)
#print('therapist note at: '+ str(sample_no/44100)+'s\n')
framed_midi = np.zeros((len(S),2),dtype=int)
for sample_no in therapist_onset_sample_numbers:
#print("therapist note in frame: "+str(int(sample_no/window_size)))
framed_midi[int(sample_no/window_size)][0] = 1
for sample_no in patient_onset_sample_numbers:
#print("patient note in frame: "+str(int(sample_no/window_size)))
framed_midi[int(sample_no/window_size)][1] = 1
for i in range(len(S)):
if(i<(len(S)-1)):
S_with_extra_features[i] = np.concatenate((S[i],[np.average(S[i+1])-np.average(S[i])]),axis=0)
elif(i == len(S)-1):
S_with_extra_features[i] = np.concatenate((S[i],[0]),axis=0)
print("The MIDI file has been decoded: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
print("Shape of midi is: " + str(framed_midi.shape))
print("Shape of signal is: " + str(S_with_extra_features.shape))
return S_with_extra_features,framed_midi
def get_onset_data_th_pt_exp3(midi_file,audio_file,window_size):
#returns: X as the next STFT frame and the average increase for the frame
#y as the [therapist][patient]
#window_size can be changed
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
print("Starting STFT")
start_time = time.time()
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
S = np.transpose(S)
print("The STFT done: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
S_with_extra_features = np.zeros((len(S),(int(window_size/2)+2)))
print("Starting to decode the MIDI file")
start_time = time.time()
print("Midi length is: "+ str(mid.length))
midi = np.zeros((int(round(mid.length*samplerate)),2),dtype=int)
sample_no = 0
for msg in mid:
sample_no = sample_no+int(round(msg.time*samplerate))
if(msg.type == 'note_on'):
#print(msg)
if(msg.note > 67):
midi[sample_no][1] = 1 #patient note
patient_onset_sample_numbers.append(sample_no)
#print('patient note at: '+ str(sample_no/44100)+'s\n')
elif(msg.note <= 67 and msg.note >= 0):
midi[sample_no][0] = 1 #therapist note
therapist_onset_sample_numbers.append(sample_no)
#print('therapist note at: '+ str(sample_no/44100)+'s\n')
framed_midi = np.zeros((len(S),2),dtype=int)
for sample_no in therapist_onset_sample_numbers:
#print("therapist note in frame: "+str(int(sample_no/window_size)))
framed_midi[int(sample_no/window_size)][0] = 1
for sample_no in patient_onset_sample_numbers:
#print("patient note in frame: "+str(int(sample_no/window_size)))
framed_midi[int(sample_no/window_size)][1] = 1
for i in range(len(S)):
if(i<(len(S)-1)):
S_with_extra_features[i] = np.concatenate((S[i+1],[np.average(S[i+1])-np.average(S[i])]),axis=0)
elif(i == len(S)-1):
S_with_extra_features[i] = np.zeros(S_with_extra_features[0].shape)
print("The MIDI file has been decoded: ")
print("--- %s seconds ---" % (time.time() - start_time))
print("******************")
print("Shape of midi is: " + str(framed_midi.shape))
print("Shape of signal is: " + str(S_with_extra_features.shape))
return S_with_extra_features,framed_midi
def get_onset_data_th_pt_exp4(midi_file,audio_file,window_size,next_window = False):
#print('Decoding file: '+audio_file)
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
S = np.transpose(S)
sample_no = 0
last_sample_no = 0
number_of_onsets = 0
for msg in mid:
sample_no = sample_no+int(round(msg.time*samplerate))
if(msg.type == 'note_on'):
if(last_sample_no != sample_no):
number_of_onsets += 1
last_sample_no = sample_no
window_number = int(sample_no/window_size)
if(msg.note > 79):
if(window_number not in patient_onset_sample_numbers):
patient_onset_sample_numbers.append(window_number)
elif(msg.note <= 79):
if(window_number not in therapist_onset_sample_numbers):
therapist_onset_sample_numbers.append(window_number)
output_Signal = []
output_Onsets = []
for sample_no in therapist_onset_sample_numbers:
if(next_window == True and sample_no !=len(S)-1):
output_Signal.append(S[sample_no+1])
else:
output_Signal.append(S[sample_no])
if(sample_no in patient_onset_sample_numbers):
output_Onsets.append([1,1]) #means there is both therapist and patient onset
patient_onset_sample_numbers.remove(sample_no)
else:
output_Onsets.append([1,0]) #means there is only therapist onset
for sample_no in patient_onset_sample_numbers:
if(next_window == True and sample_no !=len(S)-1):
output_Signal.append(S[sample_no+1])
else:
output_Signal.append(S[sample_no])
output_Onsets.append([0,1]) #means there is only patient onset
return output_Signal,output_Onsets
def get_onset_data_th_pt_exp5(midi_file,audio_file,window_size,next_window = False):
print('Decoding file: '+audio_file)
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
S = np.transpose(S)
last_meaningful_frequency_bin = int(16000/((samplerate)/window_size))
S = S[:,0:last_meaningful_frequency_bin]
sample_no = 0
last_sample_no = 0
number_of_onsets = 0
for msg in mid:
sample_no = sample_no+int(round(msg.time*samplerate))
if(msg.type == 'note_on'):
if(last_sample_no != sample_no):
number_of_onsets += 1
last_sample_no = sample_no
if(msg.note > 79):
patient_onset_sample_numbers.append(int(sample_no/window_size))
elif(msg.note <= 79 and msg.note >= 0):
therapist_onset_sample_numbers.append(int(sample_no/window_size))
if(next_window):
for i in range(len(S)):
if(i<(len(S)-1)):
S[i] = S[i+1]
elif(i == len(S)-1):
S[i] = np.zeros(S[0].shape)
output_Signal = []
output_Onsets = []
for sample_no in therapist_onset_sample_numbers:
output_Signal.append(S[sample_no])
if(sample_no in patient_onset_sample_numbers):
output_Onsets.append([1,1]) #means there is both therapist and patient onset
patient_onset_sample_numbers.remove(sample_no)
else:
output_Onsets.append([1,0]) #means there is only therapist onset
for sample_no in patient_onset_sample_numbers:
output_Signal.append(S[sample_no])
output_Onsets.append([0,1]) #means there is only patient onset
return output_Signal,output_Onsets
def get_onset_data_zero(midi_file,audio_file,window_size,next_window = False):
print('Decoding file: '+audio_file)
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
(sig, samplerate) = librosa.load(audio_file, sr=None)
S = np.abs(librosa.stft(sig, center=True, n_fft=window_size, hop_length=window_size))
S = librosa.power_to_db(S)
S = np.transpose(S)
sample_no = 0
last_sample_no = 0
onset_window_numbers = []
for msg in mid:
sample_no = sample_no+int(round(msg.time*samplerate))
if(msg.type == 'note_on'):
if(last_sample_no != sample_no):
last_sample_no = sample_no
onset_window_numbers.append(int(sample_no/window_size))
if(next_window):
for i in range(len(S)):
if(i<(len(S)-1)):
S[i] = S[i+1]
elif(i == len(S)-1):
S[i] = np.zeros(S[0].shape)
output_Signal = []
output_Onsets = []
frames_to_skip_after_onset = 3
frames_skipped = 4
for i in range(len(S)):
if (i in onset_window_numbers):
frames_skipped = 0
elif (frames_skipped > frames_to_skip_after_onset):
output_Signal.append(S[i])
output_Onsets.append([0,0])
#if(len(output_Onsets)>200):
#break
else:
frames_skipped += 1
return output_Signal,output_Onsets
def get_onset_data_th_pt_exp6(midi_file,audio_file,window_size,overlap_size,next_window = False):
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
inp = read(audio_file)
data = np.array(inp[1][:,0], dtype=float)
fs = np.array(inp[0])
f, t, Zxx = signal.stft(data, fs, window='hann', nperseg=window_size, noverlap=overlap_size)
S = np.log(np.abs(Zxx) + 1)
S = np.transpose(S)
S = normalized(S)
last_onset_time = -1
number_of_onsets = 0
onset_time = 0
hop_size = window_size - overlap_size
frame_calculation_constant = fs/hop_size
for msg in mid:
onset_time +=msg.time
if(msg.type == 'note_on'):
if(last_onset_time != onset_time):
number_of_onsets += 1
last_onset_time = onset_time
window_number = int(onset_time*frame_calculation_constant)
if(msg.note > 79):
if(window_number not in patient_onset_sample_numbers):
patient_onset_sample_numbers.append(window_number)
elif(msg.note <= 79):
if(window_number not in therapist_onset_sample_numbers):
therapist_onset_sample_numbers.append(window_number)
output_Signal = []
output_Onsets = []
for sample_no in therapist_onset_sample_numbers:
if(next_window == True and sample_no !=len(S)-1):
output_Signal.append(S[sample_no+1])
else:
output_Signal.append(S[sample_no])
if(sample_no in patient_onset_sample_numbers):
output_Onsets.append([1,1]) #means there is both therapist and patient onset
patient_onset_sample_numbers.remove(sample_no)
else:
output_Onsets.append([1,0]) #means there is only therapist onset
for sample_no in patient_onset_sample_numbers:
if(next_window == True and sample_no !=len(S)-1):
output_Signal.append(S[sample_no+1])
else:
output_Signal.append(S[sample_no])
output_Onsets.append([0,1]) #means there is only patient onset
return output_Signal,output_Onsets
def get_onset_data_th_pt_conv(midi_file,audio_file,window_size,overlap_size,output_frame_size,normalize=False,time_frames=5,frame_offset=0,return_y_with_timestamps=False):
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
data, fs = librosa.load(audio_file)
f, t, S = signal.stft(data, fs, window='hann', nperseg=window_size, noverlap=overlap_size)
S = np.log(np.abs(S) + 1)
S = np.transpose(S)
S = S.astype(np.float16)
if(normalize): S = normalized(S)
last_onset_time = -1
onset_time = 0
hop_size = window_size - overlap_size
frame_calculation_constant = float(fs/hop_size)
for msg in mid:
onset_time +=msg.time
if(msg.type == 'note_on'):
print('onset at: ',onset_time)
if(last_onset_time != onset_time):
last_onset_time = onset_time
window_number = find_nearest(t,onset_time)
if(window_number <= len(S)-time_frames and window_number>(-frame_offset)):
if(msg.note > 79):
if(window_number not in patient_onset_sample_numbers):
patient_onset_sample_numbers.append(window_number)
elif(msg.note <= 79):
if(window_number not in therapist_onset_sample_numbers):
therapist_onset_sample_numbers.append(window_number)
output_Signal = []
output_Onsets = []
for sample_no in therapist_onset_sample_numbers:
#print('from the dataset, window_no: '+str(sample_no))
output_Signal.append(S[(sample_no+frame_offset):(sample_no+frame_offset+time_frames)][:])
if(sample_no in patient_onset_sample_numbers):
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,2])
else:
output_Onsets.append([1,1]) #means there is both therapist and patient onset
patient_onset_sample_numbers.remove(sample_no)
else:
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,0])
else:
output_Onsets.append([1,0]) #means there is only therapist onset
for sample_no in patient_onset_sample_numbers:
#print('from the dataset, window_no: '+str(sample_no))
output_Signal.append(S[(sample_no+frame_offset):(sample_no+frame_offset+time_frames)][:])
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,1])
else:
output_Onsets.append([0,1]) #means there is only patient onset
return output_Signal,output_Onsets
def get_onset_data_th_pt_diff_dnn(midi_file,audio_file,window_size,overlap_size,next_window = False):
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
inp = read(audio_file)
data = np.array(inp[1][:,0], dtype=float)
fs = np.array(inp[0])
f, t, Zxx = signal.stft(data, fs, window='hann', nperseg=window_size, noverlap=overlap_size)
S = np.log(np.abs(Zxx) + 1)
S = np.transpose(S)
last_onset_time = -1
onset_time = 0
hop_size = window_size - overlap_size
frame_calculation_constant = fs/hop_size
for msg in mid:
onset_time +=msg.time
if(msg.type == 'note_on'):
if(last_onset_time != onset_time):
last_onset_time = onset_time
window_number = int(onset_time*frame_calculation_constant)
if(window_number >= 1):
if(msg.note > 79):
if(window_number not in patient_onset_sample_numbers):
patient_onset_sample_numbers.append(window_number)
elif(msg.note <= 79):
if(window_number not in therapist_onset_sample_numbers):
therapist_onset_sample_numbers.append(window_number)
output_Signal = []
output_Onsets = []
for sample_no in therapist_onset_sample_numbers:
if(next_window == True and sample_no !=len(S)-1):
output_Signal.append(normalized(S[sample_no+1]-S[sample_no]))
else:
output_Signal.append(normalized(np.mean(S[sample_no:sample_no+3],axis=0)-np.mean(S[sample_no-4:sample_no-1],axis=0)))
if(sample_no in patient_onset_sample_numbers):
output_Onsets.append([1,1]) #means there is both therapist and patient onset
patient_onset_sample_numbers.remove(sample_no)
else:
output_Onsets.append([1,0]) #means there is only therapist onset
for sample_no in patient_onset_sample_numbers:
if(next_window == True and sample_no !=len(S)-1):
output_Signal.append(normalized(S[sample_no+1]-S[sample_no]))
else:
output_Signal.append(normalized(np.mean(S[sample_no:sample_no+3],axis=0)-np.mean(S[sample_no-4:sample_no-1],axis=0)))
output_Onsets.append([0,1]) #means there is only patient onset
return output_Signal,output_Onsets
def get_onset_data_th_pt_conv_cqt(midi_file,audio_file,hop_length,normalize=False,time_frames=5,frame_offset=0,log=False,return_y_with_timestamps=False):
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
y, sr = librosa.load(audio_file)
S = np.abs(librosa.cqt(y, sr=sr,hop_length=hop_length,filter_scale=1,n_bins=96,bins_per_octave=12))
S = S.astype(np.float16)
S = np.transpose(S)
if(normalize): S = normalized(S)
last_onset_time = -1
onset_time = 0
frame_calculation_constant = float(sr/hop_length)
for msg in mid:
onset_time +=msg.time
if(msg.type == 'note_on'):
if(last_onset_time != onset_time):
last_onset_time = onset_time
window_number = int(onset_time*frame_calculation_constant)
if(window_number <= len(S)-time_frames and window_number>(-frame_offset)):
if(msg.note > 79):
if(window_number not in patient_onset_sample_numbers):
patient_onset_sample_numbers.append(window_number)
elif(msg.note <= 79):
if(window_number not in therapist_onset_sample_numbers):
therapist_onset_sample_numbers.append(window_number)
output_Signal = []
output_Onsets = []
for sample_no in therapist_onset_sample_numbers:
#print('from the dataset, window_no: '+str(sample_no))
output_Signal.append(S[(sample_no+frame_offset):(sample_no+frame_offset+time_frames)][:])
if(sample_no in patient_onset_sample_numbers):
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,2])
else:
output_Onsets.append([1,1]) #means there is both therapist and patient onset
patient_onset_sample_numbers.remove(sample_no)
else:
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,0])
else:
output_Onsets.append([1,0]) #means there is only therapist onset
for sample_no in patient_onset_sample_numbers:
#print('from the dataset, window_no: '+str(sample_no))
output_Signal.append(S[(sample_no+frame_offset):(sample_no+frame_offset+time_frames)][:])
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,1])
else:
output_Onsets.append([0,1]) #means there is only patient onset
return output_Signal,output_Onsets
def get_onset_data_th_pt_conv_fft_filtered(midi_file,audio_file,window_size,overlap_size,filter_frequencies,normalize=False,time_frames=5,frame_offset=0,return_y_with_timestamps=False):
mid = mido.MidiFile(midi_file)
therapist_onset_sample_numbers = []
patient_onset_sample_numbers = []
data, fs = librosa.load(audio_file)
f, t, S = signal.stft(data, fs, window='hann', nperseg=window_size, noverlap=overlap_size)
filter_indexes = []
for lg in filter_frequencies:
filter_indexes.append(find_nearest(f, lg))
filter_indexes = np.array(list(set(filter_indexes)))
S = S[filter_indexes]
S = np.log(np.abs(S) + 1)
S = np.transpose(S)
S = S.astype(np.float16)
if(normalize): S = normalized(S)
last_onset_time = -1
onset_time = 0
hop_size = window_size - overlap_size
for msg in mid:
onset_time +=msg.time
if(msg.type == 'note_on'):
if(last_onset_time != onset_time):
last_onset_time = onset_time
window_number = find_nearest(t,onset_time)
if(window_number <= len(S)-time_frames and window_number>(-frame_offset)):
if(msg.note > 79):
if(window_number not in patient_onset_sample_numbers):
patient_onset_sample_numbers.append(window_number)
elif(msg.note <= 79):
if(window_number not in therapist_onset_sample_numbers):
therapist_onset_sample_numbers.append(window_number)
output_Signal = []
output_Onsets = []
for sample_no in therapist_onset_sample_numbers:
#print('from the dataset, window_no: '+str(sample_no))
output_Signal.append(S[(sample_no+frame_offset):(sample_no+frame_offset+time_frames)][:])
if(sample_no in patient_onset_sample_numbers):
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,2])
else:
output_Onsets.append([1,1]) #means there is both therapist and patient onset
patient_onset_sample_numbers.remove(sample_no)
else:
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,0])
else:
output_Onsets.append([1,0]) #means there is only therapist onset
for sample_no in patient_onset_sample_numbers:
#print('from the dataset, window_no: '+str(sample_no))
output_Signal.append(S[(sample_no+frame_offset):(sample_no+frame_offset+time_frames)][:])
if(return_y_with_timestamps==True):
output_Onsets.append([sample_no,1])
else:
output_Onsets.append([0,1]) #means there is only patient onset
return np.array(output_Signal),np.array(output_Onsets)
def get_conv_inputs(audio_file,onset_frame_numbers,window_size,overlap_size,next_window = False):
inp = read(audio_file)
data = np.array(inp[1][:,0], dtype=float)
fs = np.array(inp[0])
f, t, Zxx = signal.stft(data, fs, window='hann', nperseg=window_size, noverlap=overlap_size)
S = np.log(np.abs(Zxx) + 1)
S = np.transpose(S)
S_norm = normalized(S)
output_Signal = []
for sample_no in onset_frame_numbers:
#print('from the new one, window_no: '+str(sample_no))
if(next_window == True and sample_no !=len(S)-1):
output_Signal.append(S_norm[sample_no+1:sample_no+6,:744])
else:
output_Signal.append(S_norm[sample_no:sample_no+5,:744])
return output_Signal