Pydub 原始音频数据

Pydub raw audio data

我在 Python 3.4 中使用 Pydub 来尝试检测一些音频文件的音调。

我有一个有效的音高检测算法(McLeod Pitch Method),它对实时应用程序很稳健(我什至用它制作了一个 Android 音高检测应用程序:https://github.com/sevagh/Pitcha)。

我的问题是,当我将算法应用于 AudioSegment._data 时,我没有从算法中获得任何有意义的输出。

代码:

from pydub import AudioSegment

sound = AudioSegment.from_wav(file="./8700hz.wav")

#sampling rate = sound.frame_rate = 44100hz
mpm = Mpm(sound.frame_rate, len(sound._data))
print(mpm.get_pitch(sound._data))

输出:

Pitch: 150.000002396

如果我从我的扬声器播放相同的 wav 文件,从我的麦克风录制它并将算法应用于原始麦克风捕获(带符号的 16 位小端 PCM,44100Hz,单声道),我得到正确的音高。

AudioSegment._data 是否return 我所期待的?

sound._data 是一个 bytestring。我不确定 Mpm 需要什么输入,但您可能需要像这样将 bytestring 转换为 array

import array
from pydub import AudioSegment
from pydub.utils import get_array_type

sound = AudioSegment.from_wav(file="./8700hz.wav")

bit_depth = sound.sample_width * 8
array_type = get_array_type(bit_depth)

numeric_array = array.array(array_type, sound._data)