Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 340
Sevag Avatar asked Sep 03 '15 11:09

Sevag


People also ask

What is PyDub?

pydub is a Python library to work with only . wav files. By using this library we can play, split, merge, edit our . wav audio files.

Does PyDub need ffmpeg?

You need to install ffmpeg executable, not python-ffmpeg package.

What is Audioop?

The audioop module contains some useful operations on sound fragments. It operates on sound fragments consisting of signed integer samples 8, 16, 24 or 32 bits wide, stored in bytes-like objects. All scalar items are integers, unless specified otherwise.


1 Answers

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)
like image 163
Jiaaro Avatar answered Sep 21 '22 15:09

Jiaaro