Pydub raw audio data

橙三吉。 提交于 2020-01-01 19:25:52

问题


I'm using Pydub in Python 3.4 to try to detect the pitch of some audio files.

I have a working pitch detection algorithm (McLeod Pitch Method), which is robust for real-time applications (I even made an Android pitch detection app with it: https://github.com/sevagh/Pitcha).

My issue is that I'm not getting any meaningful output from the algorithm when I apply it to AudioSegment._data.

Code:

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))

Output:

Pitch: 150.000002396

If I play the same wav file from my speakers, record it from my microphone and apply the algorithm on the raw microphone capture (signed 16-bit little endian PCM, 44100Hz, mono), I get the correct pitch.

Does AudioSegment._data not return what I'm expecting?


回答1:


sound._data is a bytestring. I'm not sure what input Mpm expects, but you may need to convert the bytestring to an array like so:

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)


来源:https://stackoverflow.com/questions/32373996/pydub-raw-audio-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!