gpt4 book ai didi

python - 值错误 ("Incomplete wav chunk.") 值错误 : Incomplete wav chunk

转载 作者:行者123 更新时间:2023-11-28 19:08:44 31 4
gpt4 key购买 nike

import numpy as np
from matplotlib import pyplot as plt
import scipy.io.wavfile as wav
from numpy.lib import stride_tricks


def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
win = window(frameSize)
hopSize = int(frameSize - np.floor(overlapFac * frameSize))


samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)

cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1

samples = np.append(samples, np.zeros(frameSize))

frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
frames *= win

return np.fft.rfft(frames)


def logscale_spec(spec, sr=44100, factor=20.):
timebins, freqbins = np.shape(spec)

scale = np.linspace(0, 1, freqbins) ** factor
scale *= (freqbins-1)/max(scale)
scale = np.unique(np.round(scale))


newspec = np.complex128(np.zeros([timebins, len(scale)]))
for i in range(0, len(scale)):
if i == len(scale)-1:
newspec[:,i] = np.sum(spec[:,scale[i]:], axis=1)
else:
newspec[:,i] = np.sum(spec[:,scale[i]:scale[i+1]], axis=1)


allfreqs = np.abs(np.fft.fftfreq(freqbins*2, 1./sr)[:freqbins+1])
freqs = []
for i in range(0, len(scale)):
if i == len(scale)-1:
freqs += [np.mean(allfreqs[scale[i]:])]
else:
freqs += [np.mean(allfreqs[scale[i]:scale[i+1]])]

return newspec, freqs


def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="jet"):
samplerate, samples = wav.read(audiopath)
s = stft(samples, binsize)

sshow, freq = logscale_spec(s, factor=1.0, sr=samplerate)
ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel

timebins, freqbins = np.shape(ims)

plt.figure(figsize=(15, 7.5))
plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none")
plt.colorbar()

plt.xlabel("time (s)")
plt.ylabel("frequency (hz)")
plt.xlim([0, timebins-1])
plt.ylim([0, freqbins])

xlocs = np.float32(np.linspace(0, timebins-1, 5))
plt.xticks(xlocs, ["%.02f" % l for l in ((xlocs*len(samples)/timebins)+(0.5*binsize))/samplerate])
ylocs = np.int16(np.round(np.linspace(0, freqbins-1, 10)))
plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs])

if plotpath:
plt.savefig(plotpath, bbox_inches="tight")
else:
plt.show()

plt.clf()

plotstft("wav1.wav")

有没有人可以帮我解决这个问题....真的很困惑...,当我尝试读取波形文件时发生了这个错误。是什么原因造成的,wave 文件还是?我只是音频分析的初学者......真的需要一些帮助

最佳答案

WAV 和 PCM 不完全相同,都是无损的,但 wav 是 Windows 友好文件,PCM 是纯原始音频数据。尝试使用 PCM 文件。

顺便说一句,我遇到了同样的问题。我从 wav 切换到 pcm,它起作用了。

关于python - 值错误 ("Incomplete wav chunk.") 值错误 : Incomplete wav chunk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43226589/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com