从录制的音频中获取频谱图,错误:“ ParameterError:音频缓冲区并非在任何地方都是有限的”

时间:2019-04-06 02:19:59

标签: python-3.x pyaudio spectrogram librosa

我正在尝试使用'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
    '''

1 个答案:

答案 0 :(得分:0)

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

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