首先,轉換sph2pipe工具所在文件夾(此工具為LDC所提供的SPHERE音頻文件轉換工具)
cd '/home/dream/Research/kaldi-master/tools/sph2pipe_v2.5'
其次:在命令行進行音頻文件轉換測試:
./sph2pipe -f wav ./wav_test/SA1.WAV ./wav_test/SA1_tr.WAV
此處需要注意的是,sph2pipe可執行文件不再PATH當中,所以需要當前路徑下的完全路徑,即:./sph2pipe才可以運行,而非sph2pipe。
1.由批處理python腳本生成轉換文件
具體的:
# encoding:utf-8
import os
import os.path
root_dir = "/home/dream/Research/Data/TIMIT/"
timit_path = "/home/dream/Research/Data/TIMIT/"
# 文件保存路徑
sph2pipe_path = "/home/dream/Research/kaldi-master/tools/sph2pipe_v2.5/sph2pipe"
# sph2pipe工具所在路徑
f = open("/home/dream/Research/Data/timit_sph2pipe/make_sph2pipe_file.txt", 'w')
for root, dirs, files in os.walk(root_dir):
for fn in files:
if fn[len(fn)-3:len(fn)]=='WAV':
sourcefile = timit_path+root[len(root_dir):]+"/"+fn
targetfile = root[len(root)-5:len(root)]+"_"+fn
s = sph2pipe_path + " -f wav " + sourcefile+" "+targetfile+"\n"
f.write(s.replace('\\','/'))
f.close()
由上述腳本,可以生成對應的txt格式的轉換文件
2.在linux下運行shell腳本文件,讀取轉換文件進行數據轉換
# 逐行執行make_sph2pipe_file.txt當中的命令
filename='/home/dream/Research/Data/timit_sph2pipe/text.txt'
while read line
do
echo $line
$line
# 此處$line進行逐行執行代碼
done < $filename