Signal processing/python question?

Status
Not open for further replies.
NOTE: Title was changed from a python question to a more general question, see a few posts down...

I want to take audio files (probably will transcode them to aiff/wav if it makes things easier) and find "dead air" and measure the length of this dead air.

I know how to do this in Cocoa or Java, but I'm curious if this is possible to do with something like perl or python from stock OS X/linux command line tools.

Can anyone think of a way?

I did some googling, and it seems there are python libraries for audio analysis but they are more interested in frequency representations it seems...
 

Jonathon

Ars Legatus Legionis
17,645
Subscriptor
omniron":1cs09eq3 said:
Why is this reporting false:


>>> wavedata[2]
'\x00\x89'
>>> wavedata[2]==137
False


x89 is 137 in decimal, shouldn't those be equal?
No; '\x00\x89' is a string, not a number. There's also a good chance that it's a little-endian value, which means it's stored in memory in reverse order if you're looking at it byte by byte (that number specifically is 35072).

If you need a number (or whatever) out of the string, you can unpack binary data with struct.unpack(). Something like struct.unpack("<H", wavedata[2]), assuming your data's composed of unsigned 16-bit integers in little-endian byte order (which WAV files are).

Or, if you'd rather access the data as an array of 16-bit integers, you can do array.array("H", wavedata). That avoids having to do any funkiness with slicing. This one uses the system's endianness, so if your data's big-endian or you're working with little-endian data on a big-endian machine (i.e. PPC... but who uses that anymore? :p), you'll have to call array.byteswap() on your array after you create it.
 
omniron":1o747731 said:
it seems to take a few minutes to process on a 150MB file

Do the math. You've a file with thousands of samples per second. I'm assuming about 10 megs per minute uncompressed with ~15 minutes of data. Multiply 15 minutes * 60 seconds * 44,000 samples for the number of points in the file. That's only 40 million calculations using what I'm assuming is a scripting language, right?
 
PastorOfMuppets":2vpsnoiz said:
DSP with ... Python? Interesting choice :D

You could speed it up by reading the entire file into memory and then delaying the output until you are done processing. Right now you are doing 88,000 IO calls a second in a scripting language.

I was thinking the IOs were the problem. How would I tell py to read the file into memory first? Shouldn't it do this on it's own...
 
Jackass JoeJoe":2cwzi7d6 said:
omniron":2cwzi7d6 said:
it seems to take a few minutes to process on a 150MB file

Do the math. You've a file with thousands of samples per second. I'm assuming about 10 megs per minute uncompressed with ~15 minutes of data. Multiply 15 minutes * 60 seconds * 44,000 samples for the number of points in the file. That's only 40 million calculations using what I'm assuming is a scripting language, right?

I know but even the most basic music player can do eq and visualization without breaking a sweat.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I know but even the most basic music player can do eq and visualization without breaking a sweat

Because we've had many *years* of effort in decoding it fast (and for the vis most likely FFT) which will be a combination of some hairy C, assembly and specific floating point instructions (on x86 SSE, on the various embedded platform dedicated decode blocks which are basically very special purpose DSPs)

Scripting languages could in theory try to optimise the sort of code you're writing, but most will assume, pretty rationally, that if you want to do serious DSP stuff you'll use an optimised library with most of the heavy lifting done in compiled native code.
 
Code:
wavedata=a.readframes(1)

This is the section that is reading the data from the the file into memory.
You need to increase the number of frames that are read at a time to say 10k - 100k. Reading a single frame at a time is costly in IO time.
You then need another loop to iterate over the chunk of frame data. I would not output the result to the console for every frame, store the results in a buffer for output after you processed the chunk of frames.
 
PastorOfMuppets":1bce7ken said:
Code:
wavedata=a.readframes(1)

This is the section that is reading the data from the the file into memory.
You need to increase the number of frames that are read at a time to say 10k - 100k. Reading a single frame at a time is costly in IO time.
You then need another loop to iterate over the chunk of frame data. I would not output the result to the console for every frame, store the results in a buffer for output after you processed the chunk of frames.

I feel like a goof for not noticing this sooner...


but changing that results in a 6x speedup. :)
 
sryan2k1":22blhmu6 said:
You also never said if you did what I said, of getting rid of the console output. Write the output to a file.

Yeah the console output was pretty much never there. I’m aware that always runs lots slower. That was just for debugging more or less.

an hour audio file went from taking 4-5 minutes to do what I want, to about 40 seconds. Not too bad. This definitely will speed up my task.

I could thread it and probably make that time a lot faster.
 

tuffy

Ars Scholae Palatinae
972
omniron":1ouk1pyh said:
I could thread it and probably make that time a lot faster.
Not necessarily. If speed is an issue, use one of Python's profilers to find out where all your time is being spent, then try to make those parts faster. In an extreme case, you could replace a slow Python-based number crunching function with one written in C, which typically speeds things up dramatically. I've done a bit of audio processing in Python, and C extensions for critical sections are ultimately what it took to get the level of performance that I wanted.
 
tuffy":2b3877iz said:
omniron":2b3877iz said:
I could thread it and probably make that time a lot faster.
Not necessarily. If speed is an issue, use one of Python's profilers to find out where all your time is being spent, then try to make those parts faster. In an extreme case, you could replace a slow Python-based number crunching function with one written in C, which typically speeds things up dramatically. I've done a bit of audio processing in Python, and C extensions for critical sections are ultimately what it took to get the level of performance that I wanted.

Speed isn't my concern in this case, minimal coding time is (i could do this analysis by hand, but if the time spend code < time spend by hand, then it's worth it to dig into python).

My overall task is really simple actually, i have maybe a dozen hours of audio, and i'm trying to get a breakdown of how much "silence" is in each one. It seemed worth it to learn a little about python to get this done, and considering i've pretty much got it working in about 4-5 hours of googling and experimenting, i've definitely saved myself lots of time. I initially (just for fun) was going to try to do some filtering but this seems like maybe Python isn't the best tool? There are heuristic ways to mimic a filtering process.

Maybe i'll try it out in C (or mostly likely Objective-C) for a future issue ;)

I'll definitely look into this profiling stuff though. Right now, my wav files are up to 500MBs large, and it seems Python takes much longer than it needs to read this into RAM (if i try to read getnframes() in at once). If I had to guess, i'd imagine it's doing a lot of re-allocating on the back-end slowing the whole process down?
 
Status
Not open for further replies.