我使用voice.csv数据将其转换为音频wav表单,但它有21列,在我的代码中,它只期望两个值(列),所以我必须在代码中进行哪些更改才能创建wav表单。
数据来自https://www.kaggle.com/primaryobjects/voicegender,它还对数据进行了描述:
测量每个声音的以下声学特性,并将其包括在CSV中:
frequency
我的代码需要两列的输入。我试图将这些列转换成时间和频率,我已经尝试过跳过多个列,但我没有得到我想要的结果。
import wave
import struct
import sys
import csv
import numpy
from scipy.io import wavfile
from scipy.signal import resample
def write_wav(data, filename, framerate, amplitude):
wavfile = wave.open(filename,'w')
nchannels = 1
sampwidth = 2
framerate = framerate
nframes = len(data)
comptype = "NONE"
compname = "not compressed"
wavfile.setparams((nchannels,
sampwidth,
framerate,
nframes,
comptype,
compname))
frames = []
for s in data:
mul = int(s * amplitude)
frames.append(struct.pack('h', mul))
frames = ''.join(frames)
wavfile.writeframes(frames)
wavfile.close()
print("%s written" %(filename))
if __name__ == "__main__":
if len(sys.argv) <= 1:
print ("You must supply a filename to generate")
exit(-1)
for fname in sys.argv[1:]:
data = []
for time, value in csv.reader(open('voice.csv'), delimiter=','):
try:
data.append(float(value))#Here you can see that the time column is skipped
except ValueError:
pass # Just skip it
arr = numpy.array(data)#Just organize all your samples into an array
# Normalize data
arr /= numpy.max(numpy.abs(data)) #Divide all your samples by the max sample value
filename_head, extension = fname.rsplit(',',1)
data_resampled = resample( arr, len(data) )
wavfile.write('rec.wav', 16000, data_resampled) #resampling at 16khz
print ("File written succesfully !")
ValueError Traceback (most recent call
last)
<ipython-input-10-ad8c56a24b4d> in <module>
6
7 data = []
----> 8 for time, value in csv.reader(open('voice.csv'),
delimiter=','):
9 try:
10 data.append(float(value))#Here you can see that the
time column is skipped
ValueError: too many values to unpack (expected 2)发布于 2019-09-18 14:49:01
该文件包含统计聚合,而不是实际音频数据。没有办法,你可以反向工程一个忠实的音频信号,从这些全面的测量。
用很多话来说,这就像试图在距离和时间之间的两点之间创建一个地形轮廓。额外的测量数据,如时间上的高度差异或平均加速度,将极大地限制可能猜测的次数,但你仍在胡思乱想。
https://stackoverflow.com/questions/57995183
复制相似问题