-
Notifications
You must be signed in to change notification settings - Fork 0
/
cqt_helper.py
965 lines (861 loc) · 54.9 KB
/
cqt_helper.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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# -*- coding: UTF-8 -*-
import librosa
import matplotlib.pyplot as plt
import librosa.display
import numpy as np
import scipy.signal as signal
from dtw import dtw
from create_base import *
from rms_helper import *
import os
# 1. Get the file path to the included audio example
# Sonify detected beat events
# 定义加载语音文件并去掉两端静音的函数
test_codes = np.array(['[1000,1000;2000;1000,500,500;2000]',
'[2000;1000,1000;500,500,1000;2000]',
'[1000,1000;500,500,1000;1000,1000;2000]',
'[1000,--(1000);1000,--(1000);500,250,250,1000;--(1000),1000]',
'[500;1000,500,1000,500;500,500,500,250,250,500,500;250,250,500,500,1000]',
'[1000,--(1000);1000,--(1000);1000,-(500),500;1000,1000]',
'[750,250,500,500,500,-(500);500,1000,500,500,-(500);750,250,500,500,500,-(500)]',
'[500,1000,500,500,250,250;1000,500,750,250,500;3000]',
'[500,500,500;1000,500;500,500,500;1500;500,500,500;1000,500;500;1000;1500]',
'[500,500,1000;500,500;1000;375,125,250,250,375,125,250,250;500,500,1000]'])
test_note_codes = np.array(['[3,3,3,3,3,3,3,5,1,2,3]',
'[5,5,3,2,1,2,5,3,2]',
'[5,5,3,2,1,2,2,3,2,6-,5-]',
'[5,1+,7,1+,2+,1+,7,6,5,2,4,3,6,5]',
'[3,6,7,1+,2+,1+,7,6,3]',
'[1+,7,1+,2+,3+,2+,1+,7,6,7,1+,2+,7,1+,7,1+,2+,1+]',
'[5,6,1+,6,2,3,1,6-,5-]',
'[5,5,6,5,6,5,1,3,0,2,2,5-,2,1]',
'[3,2,1,2,1,1,2,3,4,5,3,6,5,5,3]',
'[3,4,5,1+,7,6,5]'])
test_rhythm_codes = np.array(['[500,500,1000;500,500,1000;500,500,750,250;2000]',
'[1000,1000;500,500,1000;1000,500,500; 2000]',
'[1000,1000;500,500,1000;500,250,250,500,500;2000]',
'[500,1000,500;250,250,250,250,500,500;500,500,500,500;2000]',
'[1000;500,500,1000;500,500,500,500;2000]',
'[500;500,500,500,500;500,500,500,500;500,500,500,500;250,250,250,250,500]',
'[1000,750,250,2000;500,500,500,500,2000]',
'[1000,1000,1000,500,500;1000,1000,1000,--(1000);1000,1000,1000;1000,4000]',
'[1500,500,500,500;2500,500;1000,500,500,500,500;2500,500]',
'[500,500;1500,500,500,500;2000]'])
def get_code(index, type):
if type == 1:
code = test_codes[index]
if type == 2:
code = test_rhythm_codes[index]
if type == 3:
code = test_note_codes[index]
# code = code.replace(";", ',')
# code = code.replace("[", '')
# code = code.replace("]", '')
# code = [x for x in code.split(',')]
return code
def get_onsets_index_by_filename(filename):
if filename.find("节奏10") >= 0 or filename.find("节奏十") >= 0 or filename.find("节奏题十") >= 0 or filename.find(
"节奏题10") >= 0 or filename.find("节10") >= 0:
return 9
elif filename.find("节奏1") >= 0 or filename.find("节奏一") >= 0 or filename.find("节奏题一") >= 0 or filename.find(
"节奏题1") >= 0 or filename.find("节1") >= 0:
return 0
elif filename.find("节奏2") >= 0 or filename.find("节奏二") >= 0 or filename.find("节奏题二") >= 0 or filename.find(
"节奏题2") >= 0 or filename.find("节2") >= 0:
return 1
elif filename.find("节奏3") >= 0 or filename.find("节奏三") >= 0 or filename.find("节奏题三") >= 0 or filename.find(
"节奏题3") >= 0 or filename.find("节3") >= 0:
return 2
elif filename.find("节奏4") >= 0 or filename.find("节奏四") >= 0 or filename.find("节奏题四") >= 0 or filename.find(
"节奏题4") >= 0 or filename.find("节4") >= 0:
return 3
elif filename.find("节奏5") >= 0 or filename.find("节奏五") >= 0 or filename.find("节奏题五") >= 0 or filename.find(
"节奏题5") >= 0 or filename.find("节5") >= 0:
return 4
elif filename.find("节奏6") >= 0 or filename.find("节奏六") >= 0 or filename.find("节奏题六") >= 0 or filename.find(
"节奏题6") >= 0 or filename.find("节6") >= 0:
return 5
elif filename.find("节奏7") >= 0 or filename.find("节奏七") >= 0 or filename.find("节奏题七") >= 0 or filename.find(
"节奏题7") >= 0 or filename.find("节7") >= 0:
return 6
elif filename.find("节奏8") >= 0 or filename.find("节奏八") >= 0 or filename.find("节奏题八") >= 0 or filename.find(
"节奏题8") >= 0 or filename.find("节8") >= 0:
return 7
elif filename.find("节奏9") >= 0 or filename.find("节奏九") >= 0 or filename.find("节奏题九") >= 0 or filename.find(
"节奏题9") >= 0 or filename.find("节9") >= 0:
return 8
else:
return -1
def get_onsets_index_by_filename_rhythm(filename):
if filename.find("旋律10") >= 0 or filename.find("旋律十") >= 0 or filename.find("视唱十") >= 0 or filename.find(
"视唱10") >= 0 or filename.find("旋10") >= 0:
return 9
elif filename.find("旋律1") >= 0 or filename.find("旋律一") >= 0 or filename.find("视唱一") >= 0 or filename.find(
"视唱1") >= 0 or filename.find("旋1") >= 0:
return 0
elif filename.find("旋律2") >= 0 or filename.find("旋律二") >= 0 or filename.find("视唱二") >= 0 or filename.find(
"视唱2") >= 0 or filename.find("旋2") >= 0:
return 1
elif filename.find("旋律3") >= 0 or filename.find("旋律三") >= 0 or filename.find("视唱三") >= 0 or filename.find(
"视唱3") >= 0 or filename.find("旋3") >= 0:
return 2
elif filename.find("旋律4") >= 0 or filename.find("旋律四") >= 0 or filename.find("视唱四") >= 0 or filename.find(
"视唱4") >= 0 or filename.find("旋4") >= 0:
return 3
elif filename.find("旋律5") >= 0 or filename.find("旋律五") >= 0 or filename.find("视唱五") >= 0 or filename.find(
"视唱5") >= 0 or filename.find("旋5") >= 0:
return 4
elif filename.find("旋律6") >= 0 or filename.find("旋律六") >= 0 or filename.find("视唱六") >= 0 or filename.find(
"视唱6") >= 0 or filename.find("旋6") >= 0:
return 5
elif filename.find("旋律7") >= 0 or filename.find("旋律七") >= 0 or filename.find("视唱七") >= 0 or filename.find(
"视唱7") >= 0 or filename.find("旋7") >= 0:
return 6
elif filename.find("旋律8") >= 0 or filename.find("旋律八") >= 0 or filename.find("视唱八") >= 0 or filename.find(
"视唱8") >= 0 or filename.find("旋8") >= 0:
return 7
elif filename.find("旋律9") >= 0 or filename.find("旋律九") >= 0 or filename.find("视唱九") >= 0 or filename.find(
"视唱9") >= 0 or filename.find("旋9") >= 0:
return 8
else:
return -1
def write_txt(content, filename, mode='w'):
"""保存txt数据
:param content:需要保存的数据,type->list
:param filename:文件名
:param mode:读写模式:'w' or 'a'
:return: void
"""
with open(filename, mode) as f:
f.write(content)
def get_cqt_diff(filename):
y, sr = librosa.load(filename)
rms = librosa.feature.rmse(y=y)[0]
rms = [x / np.std(rms) for x in rms]
CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=16000), ref=np.max)
w, h = CQT.shape
CQT = np.where(CQT > -22, np.max(CQT), np.min(CQT))
min_cqt = np.min(CQT)
max_cqt = np.max(CQT)
result = [0,0,0,0,0,0,0,0,0,0]
for i in range(10,h):
col_cqt = CQT[:,i]
before_col_cqt = CQT[:,i-1]
diff_sum = np.sum([1 if col_cqt[i] == max_cqt and before_col_cqt[i] == min_cqt else 0 for i in range(len(col_cqt))])
result.append(diff_sum)
# b, a = signal.butter(4, 0.3, analog=False)
#
# sig_ff = signal.filtfilt(b, a, result)
from scipy.signal import savgol_filter
sig_ff = savgol_filter(rms, 11, 1) # window size 51, polynomial order 3
sig_ff = [x / np.max(sig_ff) for x in sig_ff]
max_indexs = [i for i in range(1,len(sig_ff)-1) if sig_ff[i]>sig_ff[i-1] and sig_ff[i]>sig_ff[i+1] and sig_ff[i] > 0.2]
return result,sig_ff,max_indexs
def get_cqt_start_indexs(filename,filter_p1 = 7,filter_p2 = 1,row_level=30,sum_cols_threshold=1):
#print("filename is {}".format(filename))
# y, sr = librosa.load(filename)
# CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=16000), ref=np.max)
# w, h = CQT.shape
# CQT = np.where(CQT > -22, np.max(CQT), np.min(CQT))
# min_cqt = np.min(CQT)
# max_cqt = np.max(CQT)
#
# CQT = signal.medfilt(CQT, (15, 15)) # 二维中值滤波
#
#
# result = []
# last_i = 0
# for i in range(10,h):
# col_cqt = CQT[30:,i]
# before_col_cqt_1 = CQT[30:,i-1]
# before_col_cqt_2 = CQT[30:, i - 2]
# before_col_cqt_3 = CQT[30:, i - 3]
# before_col_cqt_4 = CQT[30:, i - 4]
# before_col_cqt_5 = CQT[30:, i - 5]
# sum_col_cqt = np.sum([1 for i in range(len(col_cqt)) if col_cqt[i] == max_cqt])
# sum_before_col_cqt_1 = np.sum([1 for i in range(len(before_col_cqt_1)) if before_col_cqt_1[i] == max_cqt])
# sum_before_col_cqt_2 = np.sum([1 for i in range(len(before_col_cqt_2)) if before_col_cqt_2[i] == max_cqt])
# sum_before_col_cqt_3 = np.sum([1 for i in range(len(before_col_cqt_3)) if before_col_cqt_3[i] == max_cqt])
# sum_before_col_cqt_4 = np.sum([1 for i in range(len(before_col_cqt_4)) if before_col_cqt_4[i] == max_cqt])
# sum_before_col_cqt_5 = np.sum([1 for i in range(len(before_col_cqt_5)) if before_col_cqt_5[i] == max_cqt])
# sum_min = np.min([sum_before_col_cqt_1,sum_before_col_cqt_2,sum_before_col_cqt_3,sum_before_col_cqt_4,sum_before_col_cqt_5])
# #if sum_col_cqt >=5 + sum_min and sum_min <3 and i - last_i > 5:
# if sum_col_cqt >= 1 + sum_min and i - last_i > 5:
# result.append(i)
# last_i = i
#
# return result
sum_cols, sig_ff = get_sum_max_for_cols(filename,filter_p1,filter_p2,row_level)
sig_ff = [x / np.std(sig_ff) for x in sig_ff]
starts = [i for i in range(1,len(sig_ff)-1) if sig_ff[i] > sig_ff[i-1] and sig_ff[i] >= sig_ff[i+1] and sig_ff[i] >np.mean(sig_ff)*0.5]
# starts = [i for i in range(10,len(sum_cols)-1) if sum_cols[i] > sum_cols[i-1] and np.max(sum_cols[i-5:i-1]) == 0]
if len(starts) == 0:
return []
selected_starts = [starts[0]]
for i in range(1,len(starts)):
s = selected_starts[-1]
e = starts[i]
if np.min(sum_cols[s:e]) <= sum_cols_threshold and (sig_ff[e] - np.min(sig_ff[s:e]) > np.mean(sig_ff)*0.5 and sig_ff[s] - np.min(sig_ff[s:e]) > np.mean(sig_ff)*0.5):
selected_starts.append(e)
selected_starts = [x -2 for x in selected_starts]
return selected_starts
def get_cqt_start_indexs_v2(filename,filter_p1 = 7,filter_p2 = 1,row_level=30,sum_cols_threshold=1):
sum_cols, sig_ff = get_sum_max_for_cols(filename,filter_p1,filter_p2,row_level)
tmp = np.zeros(len(sum_cols))
for i in range(1, len(sum_cols) - 1):
tmp[i] = np.max(sum_cols[i - 1:i + 2])
sig_ff = signal.medfilt(tmp, 1) # 二维中值滤波
sig_ff = [x / np.std(sig_ff) for x in sig_ff]
#starts = [i for i in range(1,len(sig_ff)-1) if sig_ff[i] > sig_ff[i-1] and sig_ff[i] >= sig_ff[i+1] and sig_ff[i] >np.mean(sig_ff)*0.5]
# starts = [i for i in range(1, len(sig_ff) - 6) if sig_ff[i] > sig_ff[i - 1] and sig_ff[i-1] == 0 or(sig_ff[i-1] > sig_ff[i] and sig_ff[i+1] > sig_ff[i] and np.max(sig_ff[i-1:i+5]) - np.min(sig_ff[i-1:i+5]) > np.max(sig_ff)*0.5)]
starts = [i for i in range(1, len(sig_ff) - 6) if sig_ff[i] > sig_ff[i - 1] and sig_ff[i-1] == 0 and sig_ff[i-1] == 0 or(sig_ff[i-1] > sig_ff[i] and sig_ff[i+1] > sig_ff[i] and np.max(sig_ff[i-1:i+5]) - np.min(sig_ff[i-1:i+5]) > np.max(sig_ff)*0.5)]
selected_starts = starts
# sig_ff_on_starts = [sig_ff[i] for i in starts]
# starts = [x for x in starts if sig_ff[x] > np.mean(sig_ff_on_starts)* 0.80]
# starts = [i for i in range(10,len(sig_ff)-1) if sig_ff[i] > sig_ff[i-1] and np.max(sig_ff[i-4:i]) == 0]
# if len(starts) == 0:
# return []
# selected_starts = [starts[0]]
# for i in range(1,len(starts)):
# s = selected_starts[-1]
# e = starts[i]
# if np.min(sum_cols[s:e]) <= sum_cols_threshold and (sig_ff[e] - np.min(sig_ff[s:e]) > np.mean(sig_ff)*0.5 and sig_ff[s] - np.min(sig_ff[s:e]) > np.mean(sig_ff)*0.5):
# selected_starts.append(e)
selected_starts = [x for x in selected_starts]
return selected_starts
def get_best_cqt_start_indexs_by_diff_level(filename,start, end,base_frames):
base_total = len(base_frames)
# print("start, end,base_total is {},{},{}".format(start, end,base_total))
best_gap_number = 1000
best_start_indexs = []
rms, rms_diff, sig_ff, max_indexs = get_rms_max_indexs_for_onset(filename)
for row_level in range(30,60,3):
start_indexs = get_cqt_start_indexs_v2(filename, filter_p1=7, filter_p2=1, row_level = row_level, sum_cols_threshold=1)
start_indexs = [x for x in start_indexs if x > start-6 and x < end]
# if len(start_indexs) == len(max_indexs):
# gaps = [np.abs(max_indexs[i] - start_indexs[i]) for i in range(len(start_indexs))]
# if np.max(gaps) < 6:
# return start_indexs
# print("row_level is {}".format(row_level))
# print("base_frames is {} ,size {}".format(base_frames, len(base_frames)))
# print("start_indexs is {} ,size {}".format(start_indexs, len(start_indexs)))
xc, yc, path1, path2,loss_indexs = get_matched_frames(base_frames, start_indexs)
if len(xc) == len(base_frames) and len(yc) != 0:
return yc
tmp = []
for x in start_indexs:
offset = [np.abs(x - m) for m in max_indexs]
if np.min(offset) < 10:
tmp.append(x)
start_indexs = tmp
# if len(start_indexs) >= base_total:
# return start_indexs
if np.abs(len(start_indexs) - base_total) < best_gap_number:
best_gap_number = np.abs(len(start_indexs) - base_total)
best_start_indexs = start_indexs
if len(best_start_indexs) == base_total:
return best_start_indexs
else:
xc, yc, path1, path2,loss_indexs = get_matched_frames(base_frames, best_start_indexs)
return yc
def get_cqt_col_diff(filename):
y, sr = librosa.load(filename)
CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=16000), ref=np.max)
w, h = CQT.shape
CQT = np.where(CQT > -22, np.max(CQT), np.min(CQT))
CQT = signal.medfilt(CQT, (15, 15)) # 二维中值滤波
result = []
for i in range(1,h-4):
col_cqt = CQT[10:,i]
before_col_cqt = CQT[10:,i-1]
sum = np.sum([1 if before_col_cqt[i] != col_cqt[i] else 0 for i in range(len(col_cqt))])
result.append(sum)
return result
def get_onset_frame_length(filename,onset_code):
code = parse_onset_code(onset_code)
code = [int(x) for x in code]
sum_cols, sig_ff = get_sum_max_for_cols(filename)
#cqt_col_diff = get_cqt_col_diff(filename)
cqt_col_diff = np.array(sum_cols)
cqt_col_diff[-10:] = 0
#cqt_col_diff = [x if x > 2 else 0 for x in cqt_col_diff]
end = len(cqt_col_diff)
starts = get_cqt_start_indexs(filename)
if len(starts) == 0:
return 0,0,0
start = starts[0]
# for i in range(2,len(cqt_col_diff)):
# if np.max(cqt_col_diff[:i-1]) <= 2 and cqt_col_diff[i] >2:
# start = i
for i in range(len(cqt_col_diff)-6,0,-1):
if np.max(cqt_col_diff[i+1:]) <= 1 and cqt_col_diff[i] >=1:
end = i
start_indexs = get_cqt_start_indexs(filename)
if code[-1] >= 2000:
end = end + (end - start - (end - start_indexs[-1])) * code[-1]/(np.sum(code[:-1])) - (end - start_indexs[-1])
if end > len(sum_cols):
end = len(sum_cols) - 10
return start,end,end-start
def get_sum_max_for_cols(filename,filter_p1 = 7,filter_p2 = 1,row_level=30):
y, sr = librosa.load(filename)
CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=16000), ref=np.max)
w, h = CQT.shape
CQT = np.where(CQT > -22, np.max(CQT), np.min(CQT))
min_cqt = np.min(CQT)
max_cqt = np.max(CQT)
CQT = signal.medfilt(CQT, (5, 5)) #二维中值滤波
# 滤波去噪声
for i in range(10, h):
col_cqt = CQT[:, i]
sum_col = np.sum([1 if x == max_cqt else 0 for x in col_cqt])
# if sum_col <= 5:
# CQT[:, i] = min_cqt
# continue
for j in range(10, w - 5):
if col_cqt[j + 1] == min_cqt and col_cqt[j - 1] == min_cqt and col_cqt[j] == max_cqt:
CQT[j, i] = min_cqt
result = [0,0,0,0,0,0,0,0,0,0]
for i in range(10, h):
col_cqt = CQT[row_level:, i]
sum_col_cqt = np.sum([1 for i in range(len(col_cqt)) if col_cqt[i] == max_cqt])
result.append(sum_col_cqt)
from scipy.signal import savgol_filter
sig_ff = savgol_filter(result, filter_p1, filter_p2) # window size 51, polynomial order 3
# b, a = signal.butter(4, 0.2, analog=False)
# sig_ff = signal.filtfilt(b, a, result)
#sig_ff = [x / np.max(sig_ff) for x in sig_ff]
return result,sig_ff
def parse_onset_code(onset_code):
code = onset_code
indexs = []
code = code.replace(";", ',')
code = code.replace("[", '')
code = code.replace("]", '')
tmp = [x for x in code.split(',')]
for i in range(len(tmp)):
if tmp[i].find("(") >= 0:
indexs.append(i)
while code.find("(") >= 0:
code = code.replace("(", '')
code = code.replace(")", '')
code = code.replace("-", '')
code = code.replace("--", '')
code = [x for x in code.split(',')]
result = []
for i in range(len(code)):
if i in indexs:
continue
elif i+1 not in indexs:
result.append(code[i])
else:
t = int(code[i]) + int(code[i+1])
result.append(t)
return result
def del_code_less_500(onset_code):
code = parse_onset_code(onset_code)
result = []
less_end = 0
for i in range(len(code)):
x = int(code[i])
if i >= less_end:
if x < 500:
for n in range(i,len(code)):
if int(code[n]) >= 500:
less_end = n
break
tmp = np.sum([int(code[m]) for m in range(i,less_end)])
result.append(tmp)
else:
result.append(x)
return result
def add_loss_small_onset(start_indexs,onset_code):
if len(start_indexs) == 0:
return []
#print("start_index is {},size is {}".format(start_indexs,len(start_indexs)))
code = parse_onset_code(onset_code)
code = [int(x) for x in code]
#print("code is {},size is {}".format(code, len(code)))
total_length_no_last = np.sum(code[:-1])
real_total_length_no_last = start_indexs[-1] - start_indexs[0]
rate = real_total_length_no_last/total_length_no_last
code_dict = {}
for x in range(125, 4000, 125):
code_dict[x] = int(x * rate)
# width_2000 = int(2000 * rate)
# width_1500 = int(1500 * rate)
# width_1000 = int(1000 * rate)
# width_750 = int(750 * rate)
# width_500 = int(500 * rate)
# width_375 = int(375 * rate)
# width_250 = int(250 * rate)
# width_125 = int(125 * rate)
# code_dict = {2000: width_2000, 1500: width_1500, 1000: width_1000, 750: width_750, 500: width_500, 375: width_375, 250: width_250, 125: width_125}
less_500 = [i for i in range(len(code)) if int(code[i]) < 500 and int(code[i]) == np.min(code)]
if len(less_500) == len(code) - len(start_indexs):
for i in less_500:
start_indexs.append(start_indexs[i] + code_dict.get(int(code[i])))
start_indexs.sort()
else:
for i in range(len(code) -1):
if i+1 >= len(start_indexs) -1:
return start_indexs
onset_width = start_indexs[i+1] - start_indexs[i]
base_width = code_dict.get(code[i])
next_base_width = code_dict.get(code[i+1])
offset = np.abs((base_width - onset_width)/base_width) # 宽度误差
offset_with_next = np.abs((base_width + next_base_width - onset_width) / (base_width + next_base_width)) # 宽度误差
next_code = int(code[i+1])
if next_code < 500:
if offset_with_next < 0.35 and len(code) > len(start_indexs):
start_indexs.append(start_indexs[i] + int(onset_width*base_width/(base_width + next_base_width)))
start_indexs.sort()
#print("add")
else:
#print("ok")
pass
return start_indexs
def get_250_mayby_indexs(filename,onset_code):
sum_cols, sig_ff = get_sum_max_for_cols(filename, filter_p1=31, filter_p2=12)
starts = [i for i in range(1, len(sig_ff) - 1) if sig_ff[i] > sig_ff[i - 1] and sig_ff[i] >= sig_ff[i + 1] and sig_ff[i] > 3]
starts_diff = np.diff(starts)
#print("starts_diff is {},size is {}".format(starts_diff, len(starts_diff)))
code = parse_onset_code(onset_code)
code = [int(x) for x in code]
#print("code is {},size is {}".format(code, len(code)))
total_length_no_last = np.sum(code[:-1])
if len(start_indexs) == 0:
return
real_total_length_no_last = start_indexs[-1] - start_indexs[0]
rate = real_total_length_no_last / total_length_no_last
width_375 = int(375 * rate)
#print("width_375 is {}".format(width_375))
width_250 = int(250 * rate)
#print("width_250 is {}".format(width_250))
width_125 = int(125 * rate)
#print("width_125 is {}".format(width_125))
def get_dtw(onset_frames,base_frames):
euclidean_norm = lambda x, y: np.abs(x - y)
d, cost_matrix, acc_cost_matrix, path = dtw(onset_frames,base_frames, dist=euclidean_norm)
dis = d * np.sum(acc_cost_matrix.shape)
return dis
def get_base_frames_for_onset(filename,onset_code):
start, end, total_frames_number = get_onset_frame_length(filename,onset_code)
base_frames = onsets_base_frames(onset_code, total_frames_number)
return base_frames
def modify_row_level(filename,onset_code):
base_frames = get_base_frames_for_onset(filename,onset_code)
base_frames_diff = np.diff(base_frames)
best_dis = 0
best_start_indexs = get_cqt_start_indexs(filename)
best_row_level = 31
for row_level in range(31,55):
start_indexs = get_cqt_start_indexs(filename, filter_p1=31, filter_p2=12, row_level=row_level)
start_indexs_diff = np.diff(start_indexs)
base_frames = [x - (base_frames[0]-start_indexs[0]) for x in base_frames]
dis = get_dtw(start_indexs_diff,base_frames_diff)
print("row_level,dis is {},{}".format(row_level, dis))
if dis > best_dis and len(start_indexs) == len(base_frames):
best_dis = dis
best_start_indexs = start_indexs
best_row_level = row_level
print("best_row_level,best_start_indexs is {},{}".format(best_row_level,best_start_indexs))
return best_row_level,best_start_indexs
def check_each_onset(start_indexs,onset_code):
if len(start_indexs) == 0:
return []
#print("start_index is {},size is {}".format(start_indexs,len(start_indexs)))
code = parse_onset_code(onset_code)
code = [int(x) for x in code]
#print("code is {},size is {}".format(code, len(code)))
total_length_no_last = np.sum(code[:-1])
real_total_length_no_last = start_indexs[-1] - start_indexs[0]
rate = real_total_length_no_last/total_length_no_last
code_dict = {}
for x in range(125,4000,125):
code_dict[x] = int(x * rate)
for i in range(len(code)):
#print("code {} is {}".format(i,code[i]))
if i + 1 > len(start_indexs) - 1:
return start_indexs
onset_width = start_indexs[i+1] - start_indexs[i]
if i+2 <= len(start_indexs) - 1:
next_onset_width = start_indexs[i+2] - start_indexs[i+1]
else:
next_onset_width = 1
base_width = code_dict.get(code[i])
if i+1 <= len(code) -1:
next_base_width = code_dict.get(code[i+1])
else:
next_base_width = 0
offset = np.abs((base_width - onset_width)/base_width) # 宽度误差
offset_with_next = np.abs((base_width + next_base_width - onset_width) / (base_width + next_base_width)) # 宽度误差
offset_with_next_real = np.abs((onset_width + next_onset_width - base_width) / base_width) # 宽度误差
#如果是2000,则要判断是否拖带尾声
if int(code[i]) == 2000:
if i == len(code)-1 and start_indexs[i + 1] is not None: #最后一个拖带尾声
start_indexs.remove(start_indexs[i + 1])
#print("remove")
elif onset_width < base_width * 0.4 and offset_with_next_real < 0.48: #拖带尾声
start_indexs.remove(start_indexs[i+1])
#print("remove")
#如果是375或250,则要判断是否漏检
elif int(code[i]) == 375 or int(code[i]) == 250:
if offset_with_next < 0.3: #漏检
start_indexs.append(start_indexs[i] + int(onset_width * base_width / (base_width + next_base_width)))
start_indexs.sort()
#print("add")
else:
#print("ok")
pass
return start_indexs
def check_rms_max_by_dtw(max_indexs,base_frames,start_indexs):
base_frames_diff = np.diff(base_frames)
max_indexs_diff = np.diff(max_indexs)
raw_dis = get_dtw(max_indexs_diff,base_frames_diff)
del_maxs = []
last_del_i = 0
last_del_dis = 100000
selected_max_indexs = max_indexs.copy()
for i in range(1,len(max_indexs)):
x = max_indexs[i]
offset = [np.abs(x - s) for s in start_indexs]
if np.min(offset)<=2: #即在cqt识别出的节拍中,则不作删除处理,相信cqt识别结果
continue
tmp = [m for m in max_indexs if m != x]
dis = get_dtw(np.diff(tmp), base_frames_diff)
dis_selected = get_dtw(np.diff(selected_max_indexs), base_frames_diff)
if dis < raw_dis and dis < dis_selected:
if i - last_del_i == 1:
if dis < last_del_dis:
del_maxs.append(x)
if max_indexs[last_del_i] in del_maxs:
del_maxs.remove(max_indexs[last_del_i])
# else:
# del_maxs.append(max_indexs[last_del_i])
else:
del_maxs.append(x)
last_del_i = i
last_del_dis = dis
selected_max_indexs = [x for x in max_indexs if x not in del_maxs]
return selected_max_indexs
def get_best_onset_types(start_indexs,onset_frames,onset_code):
#通过与cqt起始点的距离判断可能的伪节拍
fake_onset_frames = []
for x in onset_frames:
offset = [np.abs(x-s) for s in start_indexs]
if np.min(offset) > 5:
fake_onset_frames.append(x)
code = parse_onset_code(onset_code)
code = [int(x) for x in code]
if len(onset_frames) - len(fake_onset_frames) == len(code):
tmp = [ x for x in onset_frames if x not in fake_onset_frames]
return tmp,fake_onset_frames
best_dis = 1000000
best_onset_frames = onset_frames
if len(fake_onset_frames)>0:
while len(best_onset_frames) > len(code):
onset_frames_tmp = best_onset_frames.copy()
flag = True
for f in fake_onset_frames:
tmp = [o for o in onset_frames_tmp if o != f]
types = get_onset_type(tmp, onset_code)
dis = get_dtw(types, code[:-1])
if dis < best_dis:
best_onset_frames = tmp
best_dis = dis
flag = False
if flag == True:
return best_onset_frames, fake_onset_frames
else:
return onset_frames,fake_onset_frames
return best_onset_frames,fake_onset_frames
if __name__ == "__main__":
# y, sr = load_and_trim('F:/项目/花城音乐项目/样式数据/ALL/旋律/1.31MP3/旋律1.100分.wav')
filename = 'F:/项目/花城音乐项目/样式数据/2.27MP3/旋律/旋律2.1(80).wav'
filename = 'F:/项目/花城音乐项目/样式数据/ALL/旋律/1.31MP3/旋律3.100分.wav'
# filename = 'F:/项目/花城音乐项目/样式数据/2.27MP3/旋律/旋律一(9)(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/2.27MP3/旋律/旋律一(14)(95).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋律五(3)(63).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/2.27MP3/节奏/节奏一(4)(96).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋3王(80).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋4谭(95).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋4文(75).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋8录音1(80).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1.3(93).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋3罗(80).wav'
filename = 'F:/项目/花城音乐项目/样式数据/2.27MP3/旋律/旋律1_40312(95).wav'
# filename = 'e:/test_image/m1/A/旋律1_40312(95).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋3罗(80).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋律十(2)(80).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/2.27MP3/旋律/旋律8录音3(95).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1王(98).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/Archive/dada1.wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1王(98).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律mp3/旋律8.100分.mp3'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律mp3/旋律7.100分.mp3'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律mp3/旋律5.100分.mp3'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律mp3/旋律六.5(100).mp3'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律mp3/旋律6.75分.mp3'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律mp3/旋律1.40分.mp3'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1.2(92).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1谭(98).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1王(98).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋3.3(96).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋4谭(95).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋律8录音3(95).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1谭(98).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋1王(98).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋8文(58).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋律四(1)(20).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋4王(56).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/旋律/旋4欧(25).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律八(9)(90).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律二(2)(90分).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律九(4)(95).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律三(2)(90分).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律四.1(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律四.3(100).wav'
# # filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律十(5)(50).wav'
# # filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律七(5)(55).wav'
# # filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律1.90分.wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律四.10(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律三(3)(80分).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律三(8)(80).wav'
# # filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律二(2)(90分).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律三.10(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律一.6(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/1-2/旋律/旋律九(6)(50).wav'
filename = 'F:/项目/花城音乐项目/样式数据/6.24MP3/旋律/小学8题20190624-3898-2.wav'
filename = 'F:/项目/花城音乐项目/样式数据/2.27MP3/节奏/节奏一(4)(96).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节10.4(60).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节1文(95).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节奏一(1)(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节奏一录音一(82).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节奏一(3)(90).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节奏十(1)(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节1谭(96).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节2录音1(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节2录音3(100).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节4谭(95).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节4.1(95).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节4.1(95).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节10.4(60).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节10.1(97).wav'
# filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节4熙(95).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节奏三(1)(100).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节7.1(80).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节8.1(78).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节8文(5).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节10桢(80).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节8文(5).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节10文(85).wav'
filename = 'F:/项目/花城音乐项目/样式数据/3.06MP3/节奏/节1.2(100).wav'
filename = 'F:/项目/花城音乐项目/样式数据/6.18MP3/节奏/1;100.wav'
# filename = 'F:/项目/花城音乐项目/样式数据/6.18MP3/节奏/12;98.wav'
filename = 'F:/项目/花城音乐项目/样式数据/6.18MP3/节奏/01,100.wav'
filename = 'F:/项目/花城音乐项目/样式数据/6.18MP3/节奏/节奏3,90.wav'
# filename = 'F:/项目/花城音乐项目/样式数据/6.18MP3/节奏/节奏3,78.wav'
filename = 'F:/项目/花城音乐项目/样式数据/6.18MP3/节奏/节奏3,80.wav'
filename = 'F:/项目/花城音乐项目/样式数据/6.18MP3/节奏/2,88(声音偏小).wav'
result_path = 'e:/test_image/n/'
plt.close()
type_index = get_onsets_index_by_filename(filename)
onset_code = get_code(type_index, 1)
rhythm_code = get_code(type_index, 2)
pitch_code = get_code(type_index, 3)
onset_code = '[500,500,250,250,250,250;500,250,250,1000;250,250,250,250,750,250;250,250,500,1000]'
# filename = 'F:/项目/花城音乐项目/样式数据/6.24MP3/旋律/两只老虎20190624-2939.wav'
# filename = 'F:/项目/花城音乐项目/样式数据/6.24MP3/旋律/小学8题20190624-3898-1.wav'
filename, onset_code = 'F:/项目/花城音乐项目/样式数据/6.24MP3/旋律/小学8题20190624-3898-1.wav', '[1000,1000;500,250,250,500;1000,500,500;2000]' # 第1条 这个可以给满分 95/90
filename, onset_code = 'F:/项目/花城音乐项目/样式数据/6.24MP3/旋律/小学8题20190624-3898-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 第2条 基本上可以是满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/6.24MP3/旋律/小学8题20190624-3898-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 第3条 故意错一个,扣一分即可 89?86
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/6.24MP3/旋律/小学8题20190624-3898-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 第4条 故意错了两处,应该扣两分左右即可 94?87
filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/小学8题20190625-2251 节拍题一.wav', '[1000,1000;500,250,250,500;1000,500,500;2000]' # 应该有七分左右 78
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/小学8题20190625-2251 节拍题三.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 应该接近满分 98
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/中学8题20190701-4154 节拍题二.wav', '[1000,1000;1500,500;500,250,250,500,500;2000]' # 可给满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/中学8题20190701-4154 节拍题三.wav', '[500,1000,500;2000;500,250,250,500,500;2000]' # 可给接近满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/录音题E20190701-9528 第一题.wav', '[1000,1000;500,250,250,1000;500,500,500,500;2000]' # 可给满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/录音题E20190701-9528 第二题.wav', '[1000,500,500;500,250,250,500;500,500,1000;2000]' # 可给接近满分 90
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/中学8题20190701-1547 节奏一.wav', '[500,250,250,500,500;1500,500;1000,1000;2000]' # 可给接近满分 94
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/中学8题20190701-1547 节奏二.wav', '[1000,1000;1500,500;500,250,250,500,500;2000]' # 可给接近满分 97
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/中学8题20190701-1547 节奏三.wav', '[500,1000,500;2000;500,250,250,500,500;2000]' # 可给接近满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.01MP3/旋律/中学8题20190701-1547 节奏四.wav', '[500,1000,500;2000;500,500,500,250,250;2000]' # 应该给接近九分 93 ????
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.12MP3/旋律/小学8题20190702-2647-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 可给满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.12MP3/旋律/小学8题20190702-2647-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 可给满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.12MP3/旋律/小学8题20190702-2647-3.wav', '[2000,250,250,250,250,1000;2000;500,500,1000]' # 可给满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.12MP3/旋律/小学8题20190702-2647-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 可给满分 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-2776-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-2776-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-2776-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-2776-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-5668-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 68
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-5668-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 65
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-5668-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 87
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-5668-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 100 ?????
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6249-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6249-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6249-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6249-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6285-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6285-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6285-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.17MP3/旋律/小学8题20190717-6285-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 100
filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4634-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 54
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4634-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 47
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4634-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 42
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4634-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 30
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4856-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 92
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4856-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 65
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4856-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 81
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190717-4856-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 85
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-4074-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 60
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-4074-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 67
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-4074-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 49
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-4074-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 86
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-7649-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 66
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-7649-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 89
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-7649-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-7649-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-9728-1.wav', '[1000,1000;500,250,250,1000;1000,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-9728-2.wav', '[1000,500,500;2000;250,250,500,500,500;2000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-9728-3.wav', '[2000;250,250,250,250,1000;2000;500,500,1000]' # 100
# filename, onset_code = 'F:/项目/花城音乐项目/样式数据/7.18MP3/旋律/小学8题20190718-9728-4.wav', '[1000,250,250,250,250;2000;1000,500,500;2000]' # 100
# rhythm_code = '[1000,1000;500,500,1000;500,250,250,500,500;2000]'
# melody_code = '[5,5,3,2,1,2,2,3,2,6-,5-]'
print("rhythm_code is {}".format(rhythm_code))
print("pitch_code is {}".format(pitch_code))
# plt, total_score, onset_score, note_scroe, detail_content = draw_plt(filename, rhythm_code, pitch_code)
# plt.show()
# plt.clf()
y, sr = librosa.load(filename)
CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=16000), ref=np.max)
CQT = np.where(CQT > -22, np.max(CQT), np.min(CQT))
plt.subplot(2,1,1)
rms, sig_ff, max_indexs = get_cqt_diff(filename)
times = librosa.frames_to_time(np.arange(len(rms)))
librosa.display.specshow(CQT, x_axis='time')
#plt.plot(times, rms)
#plt.plot(times, sig_ff)
plt.xlim(0, np.max(times))
max_index_times = librosa.frames_to_time(max_indexs)
#plt.vlines(max_index_times, 0, np.max(rms), color='r', linestyle='dashed')
start, end, length = get_onset_frame_length(filename,onset_code)
base_frames = onsets_base_frames(onset_code, length)
base_frames_diff =np.diff(base_frames)
start_indexs = get_cqt_start_indexs(filename)
print("start_indexs is {} ,size {}".format(start_indexs, len(start_indexs)))
# best_start_indexs = get_best_cqt_start_indexs_by_diff_level(filename,start, end,base_frames)
# start_indexs = best_start_indexs
# print("best_start_indexs is {} ,size {}".format(best_start_indexs, len(best_start_indexs)))
raw_start_indexs = start_indexs.copy()
start_indexs_diff = np.diff(start_indexs)
rms, rms_diff, sig_ff, max_indexs = get_rms_max_indexs_for_onset(filename)
code = parse_onset_code(onset_code)
code = [int(x) for x in code]
if code[-1] >= 2000:
width_2000 = length * 2000 /np.sum(code)
max_indexs = [x for x in max_indexs if x >= start - 5 and x < end - int(width_2000*0.4)]
else:
max_indexs = [x for x in max_indexs if x >= start - 5 and x < end]
print("base_frames is {} ,size {}".format(base_frames, len(base_frames)))
print("max_indexs is {} ,size {}".format(max_indexs, len(max_indexs)))
max_indexs_diff = np.diff(max_indexs)
xc, yc, path1, path2,loss_indexs = get_matched_frames(base_frames, max_indexs)
if len(xc) == len(base_frames):
start_indexs = yc
else:
raw_start_indexs = start_indexs.copy()
if len(start_indexs) > 1 and len(max_indexs) > 1 and len(start_indexs) != len(base_frames):
dis_with_starts = get_dtw(start_indexs_diff, base_frames_diff)
print("dis_with_starts is {}".format(dis_with_starts))
dis_with_starts_no_first = get_dtw(start_indexs_diff[1:], base_frames_diff)
print("dis_with_starts_no_first is {}".format(dis_with_starts_no_first))
dis_with_maxs = get_dtw(max_indexs_diff, base_frames_diff)
print("dis_with_maxs is {}".format(dis_with_maxs))
dis_with_maxs_on_first = get_dtw(max_indexs_diff[1:], base_frames_diff)
print("dis_with_maxs_on_first is {}".format(dis_with_maxs_on_first))
all_dis = [dis_with_starts,dis_with_starts_no_first,dis_with_maxs,dis_with_maxs_on_first]
dis_min = np.min(all_dis)
min_index = all_dis.index(dis_min)
if 0 == min_index:
start_indexs = start_indexs
elif 1 == min_index:
sum_cols, sig_ff = get_sum_max_for_cols(filename)
first_range = np.sum([1 if i > start and i < start + start_indexs_diff[0] and sum_cols[i] > sum_cols[start+3]*0.2 else 0 for i in range(start,start + start_indexs_diff[0])]) #根据节拍长度判断是否为真实节拍
if len(start_indexs) == len(base_frames) + 1:
start_indexs = start_indexs[1:]
elif first_range > base_frames_diff[0]*0.3:
start_indexs = start_indexs
else:
start_indexs = start_indexs[1:]
elif 2 == min_index:
start_indexs = max_indexs
elif 3 == min_index:
sum_cols, sig_ff = get_sum_max_for_cols(filename)
first_range = np.sum([1 if i > start and i < start + start_indexs_diff[0] and sum_cols[i] > sum_cols[start+3]*0.2 else 0 for i in range(start,start + start_indexs_diff[0])]) #根据节拍长度判断是否为真实节拍
if first_range > base_frames_diff[0]*0.3:
start_indexs = max_indexs
else:
start_indexs = max_indexs[1:]
print("start_indexs is {},size is {}".format(start_indexs, len(start_indexs)))
start_indexs =check_rms_max_by_dtw(start_indexs, base_frames,raw_start_indexs)
print("start_indexs is {},size is {}".format(start_indexs, len(start_indexs)))
# if dis_with_starts < dis_with_maxs:
# onsets_frames = start_indexs
# else:
# onsets_frames = max_index
else:
start_indexs = start_indexs
if len(start_indexs) != len(base_frames):
start_indexs = check_each_onset(start_indexs, onset_code)
print("start_indexs is {},size is {}".format(start_indexs, len(start_indexs)))
start_indexs = add_loss_small_onset(start_indexs, onset_code)
print("start_indexs is {},size is {}".format(start_indexs,len(start_indexs)))
# types = get_onset_type(start_indexs, onset_code)
# types[7] = types[6] + types[7]
# types.remove(125)
# code = parse_onset_code(onset_code)
# code = [int(x) for x in code]
# dis = get_dtw(types, code[:-1])
# print("dis is {}".format(dis))
start_indexs,fake_onset_frames = get_best_onset_types(raw_start_indexs, start_indexs, onset_code)
print("start_indexs is {},size is {}".format(start_indexs, len(start_indexs)))
print("raw_start_indexs is {},size is {}".format(raw_start_indexs, len(raw_start_indexs)))
raw_start_indexs_time = librosa.frames_to_time(raw_start_indexs)
start_indexs_time = librosa.frames_to_time(start_indexs)
max_indexs_time = librosa.frames_to_time(max_indexs)
fake_onset_frames_time = librosa.frames_to_time(fake_onset_frames)
plt.vlines(raw_start_indexs_time, 0, 84, color='w', linestyle='solid')
# plt.vlines(start_indexs_time, 0,84, color='b', linestyle='solid')
#plt.vlines(max_indexs_time, 0, 84, color='r', linestyle='dashed')
#plt.vlines(fake_onset_frames_time, 0, 84, color='black', linestyle='dashed')
start_time = librosa.frames_to_time(start)
end_time = librosa.frames_to_time(end)
plt.vlines(start_time, 0, 84, color='r', linestyle='dashed')
plt.vlines(end_time, 0, 84, color='r', linestyle='dashed')
plt.subplot(2,1,2)
sum_cols, sig_ff = get_sum_max_for_cols(filename,filter_p1 = 7,filter_p2 = 1,row_level=30)
#sum_cols = [10 if s > 0 else 0 for s in sum_cols]
#sum_cols = [np.max(sum_cols[i-1:i+2]) for i in range(1,len(sum_cols)-1)]
tmp = np.zeros(len(sum_cols))
for i in range(1,len(sum_cols)-1):
tmp[i] = np.max(sum_cols[i-1:i+2])
sig_ff = signal.medfilt(tmp,1) #二维中值滤波
#sum_cols,sig_ff = get_sum_max_for_cols(filename,filter_p1 = 31,filter_p2 = 12,row_level = 50)
#sig_ff = [x/np.std(sig_ff) for x in sig_ff]
starts = [i for i in range(1, len(sig_ff) - 6) if sig_ff[i] > sig_ff[i - 1] and sig_ff[i-1] == 0 or(sig_ff[i-1] > sig_ff[i] and sig_ff[i+1] > sig_ff[i] and np.max(sig_ff[i-1:i+5]) - np.min(sig_ff[i-1:i+5]) > np.max(sig_ff)*0.5)]
# best_row_level, best_start_indexs = modify_row_level(filename)
starts_times = librosa.frames_to_time(starts)
plt.vlines(starts_times, 0, 15, color='r', linestyle='solid')
times = librosa.frames_to_time(np.arange(len(rms)))
sum_cols_diff = list(np.diff(sum_cols))
sum_cols_diff.insert(0,0)
plt.plot(times,sum_cols)
#plt.plot(times, sum_cols_diff)
plt.plot(times, sig_ff)
plt.xlim(0, np.max(times))
plt.show()
start_indexs = add_loss_small_onset(start_indexs,onset_code)
get_250_mayby_indexs(filename, onset_code)
code = del_code_less_500(onset_code)
print("code without less 500 is {}".format(code))