python-3.x - 从录制的音频中获取频谱图,错误:“ParameterError:音频缓冲区并非到处都是有限的”
问题描述
我正在尝试使用“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)
推荐阅读
- javascript - 如何使用 classList 工具隐藏/显示菜单
- java - Bean 名称“mstUsermaster”的 BindingResult 和普通目标对象都不能用作请求属性
- android - 将多个值存储为 SharedPreference 是个好主意吗?
- logging - 关于 Yii2 日志的 DbTarget
- python - AttributeError:模块“Tkinter”没有属性“__file__”
- java - Kotlin-kapt 不会生成任何东西
- java - 构建我的应用程序,但它说构建失败并出现一个错误
- json - 如何忽略 Spray-json [Scala] 中的字段
- javascript - JavaScript 重定向到下一页错误
- popup - Swiper Slider 将在弹出后开始播放视频