1

有没有办法可以将所有值转换为每块数据的 float32?我尝试过这种方式,它不会给我一个错误,但随后奥比奥无法识别任何音高。此外,我不能直接转换为 float32,否则它对于我在程序的不同部分中使用的 FFT 变得过于敏感。

CHUNK = 1024 
#need it to be float for pitch, but int for frequencies
#FORMAT = pyaudio.paFloat32
FORMAT = pyaudio.paInt32

CHANNELS = 1
RATE = 44100

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)


audioData = stream.read(CHUNK)
pDetection = aubio.pitch("default", CHUNK,
         CHUNK, RATE)
    #     
# Set unit.
pDetection.set_unit("midi")
pDetection.set_silence(-40)
pDetection.set_tolerance(.1)
 #trying to change data into float         
 for i in audioData:
     i = np.float32(i)

  pitchSamples = np.fromstring(audioData, dtype = aubio.float_type)
    # 
    pitch = pDetection(pitchSamples)[0]
4

1 回答 1

1

aubio 需要浮点音频样本,而不是整数。要将短整数 ( )转换[-32768, 32767]为浮点数 ( [-1., 1.]),您只需将它们乘以1./32768 ~= 3.0517578125e-05.

def short_to_float(x):
    return x * 3.0517578125e-05

作为参考,demo_pyaudio.py中提供了一个使用浮点数的完整示例。

于 2018-04-25T12:12:08.540 回答