Sevag
Sevag

Reputation: 301

Pydub raw audio data

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?

Upvotes: 6

Views: 8797

Answers (1)

Jiaaro
Jiaaro

Reputation: 76958

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)

Upvotes: 11

Related Questions