gpt4 book ai didi

python-3.x - 从录制的音频中获取频谱图,错误 : "ParameterError: Audio buffer is not finite everywhere"

转载 作者:行者123 更新时间:2023-12-04 17:38:43 28 4
gpt4 key购买 nike

我正在尝试使用“pyaudio”录制声音并获取音频的频谱图,但出现上述错误:“音频缓冲区并非处处有限”。它可能是重复的,但我没有找到解决错误的方法。这是我的代码:

CHUNK = 96000 # number of data points to read at a time
RATE = 16000 # time resolution of the recording device (Hz)
p=pyaudio.PyAudio() # start the PyAudio class
stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,
frames_per_buffer=CHUNK) #uses default input device

# create a numpy array holding a single read of audio data
stop=0
while not stop: #to it a few times just to see
print('Recording')
audio=np.frombuffer(stream.read(CHUNK))
print(type(audio[0]))
print("max value: ",np.max(audio))
print("min value: ",np.min(audio))
sd.play(audio,RATE)
S = librosa.feature.melspectrogram(audio, sr=RATE)
S = 10 * np.log(S + 1e-15)
#em=get_emotion_audio(audio,RATE)
#print("[DETECTED] ",em)
stop=1
# close the stream gracefully
stream.stop_stream()
stream.close()
p.terminate()

这是我遇到的错误:

'''
Recording
<class 'numpy.float64'>
max value: nan
min value: nan
---------------------------------------------------------------------------
ParameterError Traceback (most recent call last)
<ipython-input-3-33fa263f625d> in <module>
19 print("min value: ",np.min(audio))
20 sd.play(audio,RATE)
---> 21 S = librosa.feature.melspectrogram(audio, sr=RATE)
22 S = 10 * np.log(S + 1e-15)
23 #em=get_emotion_audio(audio,RATE)

~\Anaconda3\lib\site-packages\librosa\feature\spectral.py in melspectrogram(y, sr, S, n_fft, hop_length, power, **kwargs)
1529
1530 S, n_fft = _spectrogram(y=y, S=S, n_fft=n_fft, hop_length=hop_length,
-> 1531 power=power)
1532
1533 # Build a Mel filter

~\Anaconda3\lib\site-packages\librosa\core\spectrum.py in _spectrogram(y, S, n_fft, hop_length, power)
1555 else:
1556 # Otherwise, compute a magnitude spectrogram from input
-> 1557 S = np.abs(stft(y, n_fft=n_fft, hop_length=hop_length))**power
1558
1559 return S, n_fft

~\Anaconda3\lib\site-packages\librosa\core\spectrum.py in stft(y, n_fft, hop_length, win_length, window, center, dtype, pad_mode)
159
160 # Check audio is valid
--> 161 util.valid_audio(y)
162
163 # Pad the time series so that frames are centered

~\Anaconda3\lib\site-packages\librosa\util\utils.py in valid_audio(y, mono)
168
169 if not np.isfinite(y).all():
--> 170 raise ParameterError('Audio buffer is not finite everywhere')
171
172 return True

ParameterError: Audio buffer is not finite everywhere
'''

最佳答案

解决方案是将这两行更改为:

audio=np.frombuffer(stream.read(CHUNK),dtype=np.int16)
S = librosa.feature.melspectrogram(audio.astype('float32'), sr=RATE)

关于python-3.x - 从录制的音频中获取频谱图,错误 : "ParameterError: Audio buffer is not finite everywhere",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55545290/

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