【问题标题】:Pydub raw audio dataPydub 原始音频数据
【发布时间】:2015-09-03 11:10:44
【问题描述】:

我在 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 没有返回我所期望的吗?

【问题讨论】:

    标签: python pydub pitch-detection


    【解决方案1】:

    sound._databytestring。我不确定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)
    

    【讨论】:

    • 实际上 Mpm 只是迭代我给它的任何东西,所以我正在访问字节串:for i in range(0, len(sound._data)): sound._data[i]。跨度>
    • 顺便说一句,您的建议得到了完美的解决:间距:8715.26013083 间距:8714.35873644 间距:8713.95019086 间距:8714.24068269。感谢您制作了一个很棒的库并在 SO 上如此积极地回答有关它的问题。
    猜你喜欢
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多