tags:

views:

136

answers:

3

I'm trying to make an audio-sensitive animation, and for that purpose, I'm looking for a way to look up the current audio level. I'm looking for the peak within a set amount of time. (Think the red bar that stays on for a second or so, on an audio meter.)

I've searched around for for something like this, and the only thing I could find was how to read a movie's audio levels, and how Quartz Compositions have access to this thru their iTunes Visualizer protocol.

I'm looking for a way to read this from the microphone, although I'm also interested if you know how to read this from an audio file.

Thanks!

A: 

You can use AudioQueue services to record buffers and then use a for loop over each 16-bit sample to find the peak. You can do the same for output.

lucius
+1  A: 

Take a look here. You want one of the 'envelope detector' methods, or the 'simple peak follower'.

If you're doing this, sooner or later you're going to need some other algorithm from that site too.

Andrew McGregor
While that link seems to have very useful information, I'm not quite sure how to practically put this to use in Cocoa.
Kenneth Ballenegger
The other answer says how to get hold of the sample data, then you use these algorithms to calculate meter readings. There may be some type conversion required.
Andrew McGregor
A: 

I was looking for the same thing and came across your post. I found the answer. You need a QTCaptureConnection to an audio device then you can monitor the QTCaptureConnectionAudioAveragePowerLevelsAttribute attribute of the connection. See this example project to show the details: here

regulus6633