def classifi(src_file): _fix_len = 22050 * 2 * 180 flen = os.path.getsize(os.path.join(TEMP_DIR, "temp.wav")) _my_dict = {} for _name in GENRE_LIST: _my_dict[_name] = 0 if flen > _fix_len: os.system('sox "%s/temp.wav" "%s/output.wav" trim 0 60 : newfile : restart' % (TEMP_DIR, TEMP_DIR)) for _i in range(1, (flen / _fix_len + 1)): wav_fn = "%s/output%03d.wav" % (TEMP_DIR, _i) ceps_fn = "%s/output%03d.ceps.npy" % (TEMP_DIR, _i) ceps.create_ceps(wav_fn) _my_dict[train_model(create_model, X, y, ceps_fn)] += 1 _max = -1 _dst = "" for _key in _my_dict: if _my_dict[_key] > _max: _max = _my_dict[_key] _dst = _key else: wav_fn = "%s/temp.wav" % TEMP_DIR ceps_fn = "%s/temp.ceps.npy" % TEMP_DIR ceps.create_ceps(wav_fn) _dst = train_model(create_model, X, y, ceps_fn) _cmd = 'mv "%s" "%s/%s"' % (src_file, DEST_DIR, _dst) print(">>>[%s]" % _cmd) os.system(_cmd)
def do_file(p_path,path): print(">>> [%s/%s] :" % (p_path,path)) f = os.popen("ls %s/%s"%(p_path,path)) fs = f.read() f.close() f_list = fs.split('\n') print(">>> [%s]" % f_list) for _f in f_list[:-1]: if _f.find('.wav')>0: print('>>> wav file: [%s]' % _f) fn = '%s/%s/%s' % (p_path,path,_f) print('--- [%s]' % fn) ceps.create_ceps(fn)
def test_model_on_single_file(file_path): clf = joblib.load('data/model_ceps.pkl') create_ceps(test_file) fnpy = file_path[:-3]+"ceps.npy" print fnpy X, y = read_ceps_test(fnpy) probs = clf.predict_proba(X) print str(probs) #print "\t".join(str(x) for x in traverse) #print "\t".join(str("%.3f" % x) for x in probs[0]) probs=probs[0] max_prob = max(probs) for i,j in enumerate(probs): if probs[i] == max_prob: max_prob_index=i print max_prob_index predicted_genre = genre_list[max_prob_index] print "\n\npredicted genre = ",predicted_genre return predicted_genre
def classifi(src_file): ''' 获取音频文件长度 ''' flen = os.path.getsize(os.path.join(TEMP_DIR,'temp.wav')) print(">>> flen=%d" % flen) _my_dict = {} for _name in GENRE_LIST: _my_dict[_name] = 0 ''' 将大音频文件(大于3分钟的)分割成3分钟采样率22050的单声道音频文件 ''' _fix_len = 22050 * 3 * 60 n = flen/_fix_len print(">>> n=%d" % n) if flen>_fix_len: ''' 将大音频文件拆分成小样本 ''' os.system(r'sox "%s"/temp.wav "%s"/output.wav trim 0 180 : newfile : restart' % (TEMP_DIR,TEMP_DIR)) wav_fn = '%s/output001.wav' % TEMP_DIR flen = os.path.getsize(wav_fn) for _i in range(1,n): '''对拆分音频进行分类 ''' wav_fn = '%s/output%03d.wav' % (TEMP_DIR,_i) if flen==os.path.getsize(wav_fn): ceps_fn = '%s/output%03d.ceps.npy' % (TEMP_DIR,_i) #print(">>> ...[%s]" % ceps_fn) '''创建测试样本 ''' ceps.create_ceps(wav_fn) '''样本分类,并分类积分 ''' _my_dict[train_model(create_model, X, y, ceps_fn)]+=1 '''大数判决 ''' _max = -1 _dst= '' for _key in _my_dict: if _my_dict[_key]>_max: _max = _my_dict[_key] _dst = _key else: wav_fn = '%s/temp.wav' % TEMP_DIR ceps_fn = '%s/temp.ceps.npy' % TEMP_DIR '''创建测试样本 ''' ceps.create_ceps(wav_fn) '''样本分类 ''' _dst = train_model(create_model, X, y, ceps_fn) ''' 将原始的音频文件 放置 到归类的目录下 2014.12.14:问题,若在目录下有同名文件,结果? -增加时间戳 ''' _t = str(time.time()) _cmd = 'mv "%s" "%s"/%s/"%s"' % (src_file,DEST_DIR,_dst,(_t+'_'+os.path.split(src_file)[1])) print(_cmd) os.system(_cmd)
import sys from save import make_wav from ceps import create_ceps if __name__ == "__main__": DIR = "/home/viewtiful/2018-cap1-7/src/mfcc/" # DIR = "C:\Users\lynn\PycharmProjects\\2018-cap1-7\src\mfcc" f = sys.argv[1] while 1: num = int(input()) sec = int(input()) make_wav(f, num, sec) fn = DIR + f + str(num) + ".wav" X = [] create_ceps(fn)