6

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?

Sevag
  • 191
  • 2
  • 15

1 Answers1

11

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)
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • Actually Mpm just iterates over whatever I give it, so I was accessing the bytestring with: for i in range(0, len(sound._data)): sound._data[i]. – Sevag Sep 03 '15 at 21:44
  • 2
    Your suggestion worked out perfectly btw: Pitch: 8715.26013083 Pitch: 8714.35873644 Pitch: 8713.95019086 Pitch: 8714.24068269. Thanks for making a great library and answering questions about it so actively on SO. – Sevag Sep 03 '15 at 21:45