Pydub raw audio data

2024/9/21 18:32:26

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 AudioSegmentsound = 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?

Answer

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_typesound = 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://en.xdnf.cn/q/72026.html

Related Q&A

Create Duplicate Rows and Change Values in Specific Columns

How to create x amount of duplicates based on a row in the dataframe and change a single or multi variables from specific columns. The rows are then added to the end of the same dataframe.A B C D E F 0…

writing and saving CSV file from scraping data using python and Beautifulsoup4

I am trying to scrape data from the PGA.com website to get a table of all of the golf courses in the United States. In my CSV table I want to include the Name of the golf course ,Address ,Ownership ,We…

Performance issue turning rows with start - end into a dataframe with TimeIndex

I have a large dataset where each line represents the value of a certain type (think a sensor) for a time interval (between start and end). It looks like this: start end type value 2015-01-01…

How can I create a key using RSA/ECB/PKCS1Padding in python?

I am struggling to find any method of using RSA in ECB mode with PKCS1 padding in python. Ive looked into pyCrypto, but they dont have PKCS1 padding in the master branch (but do in a patch). Neverthel…

Do full-outer-join with pandas.merge_asof

Hi I need to align some time series data with nearest timestamps, so I think pandas.merge_asof could be a good candidate. However, it does not have an option to set how=outer like in the standard merge…

order of calling constructors in Python

#!/usr/bin/pythonclass Parent(object): # define parent classparentAttr = 100def __init__(self):print "Calling parent constructor"def parentMethod(self):print Calling parent methoddef s…

How do I access data from a python thread

I have a very simple threading example using Python 3.4.2. In this example I am creating a five threads that just returns the character string "Result" and appends it to an array titled thre…

How to tell if a full-screen application is running?

Is it possible in python to tell if a full screen application on linux is running? I have a feeling it might be possible using Xlib but I havent found a way.EDIT: By full screen I mean the WHOLE scree…

Pretty printers for maps throwing a type error

Ive configured pretty printers using http://wiki.eclipse.org/CDT/User/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F. It successfully works for vector and other containers. However I cant get …

Return PDF generated with FPDF in Flask

I can generate a PDF with an image using the code below. How can I return the generated PDF from a Flask route?from fpdf import FPDF pdf = FPDF() img = input(enter file name) g = img + .jpg pdf.add_p…