-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_pitch_onsets_for_rhythm.py
222 lines (187 loc) · 7.42 KB
/
make_pitch_onsets_for_rhythm.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
import re
import numpy, wave,matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
import librosa
import librosa.display
from PIL import Image
import re
import shutil
from create_base import *
from create_labels_files import *
from myDtw import *
from find_mismatch import *
from grade import *
from vocal_separation import *
from viterbi import *
from create_base import *
from filters import *
import crepe
score = 0
save_path = 'F:/项目/花城音乐项目/参考代码/tensorflow_models_nets-master/rhythm/test/'
src_path = 'F:/项目/花城音乐项目/样式数据/2.27MP3/旋律/'
#save_path = './onsets/test'
#src_path = './onsets/mp3/2.27节奏'
# save_path = ''
tmp = ['A','B','C','D','E']
# dis_dir = 'F:/项目/花城音乐项目/参考代码/tensorflow_models_nets-master/onsets/test'
# scr_dir = 'F:/项目/花城音乐项目/参考代码/tensorflow_models_nets-master/onsets/test'
dis_dir = ''
new_old_txt = './rhythm/new_and_old.txt'
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]'])
def load_and_trim(path):
audio, sr = librosa.load(path)
energy = librosa.feature.rmse(audio)
frames = np.nonzero(energy >= np.max(energy) / 5)
indices = librosa.core.frames_to_samples(frames)[1]
audio = audio[indices[0]:indices[-1]] if indices.size else audio[0:0]
return audio, sr
def get_total_frames_number(path):
audio, sr = librosa.load(path)
energy = librosa.feature.rmse(audio)
frames = np.nonzero(energy >= np.max(energy) / 5)
total = frames[1][-1]
return total
def list_all_files(rootdir):
import os
_files = []
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isdir(path):
_files.extend(list_all_files(path))
if os.path.isfile(path):
_files.append(path)
return _files
#将第一阶段的文件遍历出来
#_k = filter(lambda x:re.compile(r'val.txt').search(x),_fs)
def clear_dir(dis_dir,scr_dir):
for i in tmp:
d_dir = dis_dir + '/' + i
shutil.rmtree(d_dir)
os.mkdir(d_dir)
s_dir = scr_dir + '/' + i
shutil.rmtree(s_dir)
os.mkdir(s_dir)
#清空文件夹
clear_dir(save_path,save_path)
# 保存新文件名与原始文件的对应关系
files_list = []
files_list_a = []
files_list_b = []
files_list_c = []
files_list_d = []
new_old_txt = './onsets/best_dtw.txt'
files = list_all_files(src_path)
print(files)
index = 0
# 测试单个文件
#files = ['F:/项目/花城音乐项目/样式数据/2.27MP3/节奏/节奏六(5)(80).wav']
for filename in files:
print(filename)
if filename.find('wav') <= 0:
continue
elif filename.find('shift') > 0:
continue
else:
index = index + 1
type_index = get_onsets_index_by_filename(filename)
#y, sr = load_and_trim(filename)
y, sr = librosa.load(filename)
#y, sr = get_foreground(y, sr) # 分离前景音
silence_threshold = 0.2
need_vocal_separation = check_need_vocal_separation(y, silence_threshold)
if need_vocal_separation:
y, sr = get_foreground(y, sr) # 分离前景音
total_frames_number = get_total_frames_number(filename)
if len(y) > 0:
#onsets_frames = get_real_onsets_frames(y)
#_,onsets_frames = get_onset_rmse_viterbi(y,0.35)
time = librosa.get_duration(y)
print("time is {}".format(time))
time, frequency, confidence, activation = crepe.predict(y, sr, model_capacity='full', step_size=10,
viterbi=True)
step = 3
length = len(frequency)
frequency = ArithmeticAverage(frequency.copy(), step)
# y2 = ArithmeticAverage(y.copy(),step)
# y2 = MedianAverage(y.copy(),step)
frequency = expand_output(frequency, step, length)
frequency = get_nearly_note(frequency, step)
plt.plot(time, frequency)
#onsets_frames = get_onsets_by_all_v2(y, sr,len(codes[type_index])+2)
# if len(onsets_frames) < 3:
# continue
#print("onsets_frames is {}".format(onsets_frames))
onsets_frames = get_onsets_frames_by_cqt_for_rhythm(y,sr)
onstm = librosa.frames_to_time(onsets_frames, sr=sr)
plt.vlines(onstm, 0, sr, color='y', linestyle='solid')
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(4, 4)
if "." in filename:
Filename = filename.split(".")[0]
plt.axis('off')
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.ylim(200,550)
#plt.rcParams['savefig.dpi'] = 300 # 图片像素
#plt.figure(figsize=(10, 10))
#plt.rcParams['figure.dpi'] = 300 # 分辨率
dirname, filename = os.path.split(filename)
if filename.find('标准') > 0:
saveFileName = '100-A'
savepath = save_path + 'A/'
# elif filename.find('分') > 0:
# score = filename.split("分")[0].split(".")[1] # 提取分值
# elif filename.find('(') > 0:
# score = filename.split("(")[2].split(")")[0] # 提取分值
else:
#score = filename.split("(")[2].split(")")[0] # 提取分值
score = re.sub("\D", "", filename) # 筛选数字
if str(score).find("100") > 0:
score = 100
else:
score = int(score) % 100
if int(score) >=90:
grade = 'A'
savepath = save_path + 'A/'
files_list_a.append([filename.split('.wav')[0] + '-' + grade,need_vocal_separation])
elif int(score) >= 75:
grade = 'B'
savepath = save_path + 'B/'
files_list_b.append([filename.split('.wav')[0] + '-' + grade,need_vocal_separation])
elif int(score) >=60:
grade = 'C'
savepath = save_path + 'C/'
files_list_c.append([filename.split('.wav')[0] + '-' + grade,need_vocal_separation])
elif int(score) >=1:
grade = 'D'
savepath = save_path + 'D/'
files_list_d.append([filename.split('.wav')[0] + '-' + grade,need_vocal_separation])
else:
grade = 'E'
savepath = save_path + 'E/'
saveFileName = filename.split('.wav')[0] + '-' + grade
#saveFileName = str(score) + '-' + grade
file_sum = os.listdir(savepath)
#saveFileName = str(len(file_sum)+1) + '-' + filename.split(".wav")[0] + '-' + saveFileName
#saveFileName = str(len(file_sum) + 1) + '-' + saveFileName
saveFileName = saveFileName
plt.savefig(savepath + saveFileName+ '.jpg', bbox_inches='tight', pad_inches=0)
plt.clf()
saveFileName = ''
t1 = np.vstack((files_list_a,files_list_b))
t2 = np.vstack((files_list_c,files_list_d))
files_list = np.vstack((t1,t2))
write_txt(files_list, new_old_txt, mode='w')
# 先获取多唱漏唱的情况