How to detect ambient sound level on the iPhone? - iphone

I need to create an audio loudness (decibel) detector. To clarify, I am not trying to find the volume at which the iPhone is playing, but instead the volume of its surrounding in decibels. How can I do this?

It can be done using AVAudioRecorder: http://b2cloud.com.au/tutorial/obtaining-decibels-from-the-ios-microphone

Use one of the Audio Queue or Audio Unit APIs to record low latency audio, run the samples through a DSP filter to weight the spectrum for the particular type or color of loudness you want to measure, then calibrate the mic on all the particular models of iOS devices you want to run your detector on against calibrated sound sources, perhaps in an anechoic chamber.

Related

iOS7 robotic/garbled in speaker mode on iPhone5s

We have a VOIP application, that records and plays audio. As such, we are using PlayAndRecord (kAudioSessionCategory_PlayAndRecord) audio session category.
So far, we have used it successfully with iPhone 4/4s/5 with both iOS 6 and iOS 7 where call audio and tones played clearly and were audible.
However, with iPhone 5s, we observed that both the call audio and tones sound robotic/garbled in speaker mode. When using earpiece/bluetooth/headset, sound is clear and audible.
iOS Version used with iPhone 5s: 7.0.4
We are using audiounits for recording/playing of call audio.
When setting audio properties like session category, audio route, session mode etc., we tried both the older (deprecated) AudioSessionSetProperty() and AVAudioSession APIs.
For playing tones, we are using AVAudioPlayer. Playing of tones during the VOIP call and also when pressing keypad controller within the app produces robotic sound.
When instantiating the audio component using AudioComponentInstanceNew, we set componentSubType to kAudioUnitSubType_VoiceProcessingIO.
When replacing kAudioUnitSubType_VoiceProcessingIO with kAudioUnitSubType_RemoteIO, we noticed that the sound of call audio and tones was no longer robotic, it was quite clear, but the volume level was very low when using speaker mode.
In summary, keeping all the other audio APIs the same:
kAudioUnitSubType_VoiceProcessingIO: Volume is high (desirable) but sound of tones and call audio was robotic in speaker mode.
kAudioUnitSubType_RemoteIO: Sound of tones and call audio was clear but it is not audible.
STEPS TO REPRODUCE
- Set audio session category to playAndRecord.
- Set audio route to speaker
- Set all the other audio properties like starting audio unit, activating the audio session, instantiating the audio components.
- Set the input and render callbacks
- Try both options
1. Play tones using AVAudioPlayer
2. Play call audio
Any suggestions on how to get over this issue. Raised as an issue with Apple but no response yet from them.
i have shared the code here github link
The only difference between kAudioUnitSubType_VoiceProcessingIO and kAudioUnitSubType_RemoteIO is that voiceProcessing includes code to tune out acoustic echo i.e. tunes out the noise from the speaker so the microphone doesn't pick it up. Its been a long time since I've played with the audio framework but I remember that to sound off there could be any number of things,
Are you doing any work in the audio callbacks that could be taking a long time?
The callbacks run on realtime threads. if your processing takes too long you can miss data. Would be helpful to track the data over a fixed period of time to see are you capturing it all. Use something like wireShark to sniff the network. Record the number of packets and see did the phone capture the same.
Are you modifying any of the audio?
Do you have a circular buffer that might be causing an issue?
I've had several issues doing this and one was using a third party circular buffer that was described as low latency and efficient ... it wasn't. I answered my own question here and included my circular buffer implementation that greatly improved my audio as the issue was I was skipping data.
Give this a go and let me know:
iOS UI are causing a glitch in my audio stream
Please be aware that some of this code is unique to the audio format ALaw, 0xD5 is a byte of silence in ALaw, if you are using linear PCM or any other that will probably be a noise of some kind.

Audio Output level metering in cocos2d iphone

I am developing an iPhone game with cocos2d,
I have used CDAudioManager for playing sound(this is why because i have to change the pitch of output sound). I want to measure the output sounds power. Not the gain.
I know about peakpowerForChannel: for avAudioplayer. But it can't be used here because I want to change the pitch of o/p sound. Is there any method like this for CDAudioEngine
Thank you

Rhythm (sound change) detection on iPhone

Sorry for my weak english
I've got some aif or MP3 tunes for plaing loud on the iPhone,
and I need to do some 'sound change' detections,
such I would use for some visualisations (jumping man or other)
how to do it 'easy' on iPhone or how to do it 'fine'?
should I do fft or such, or something different?
I have seen some sound visualisations back then but all they
seem not to much good (they seem not to be much clear in reactions on
music change).
Thanks
What are you currently using for playback? I would use audio queues or audio units - both give you access to buffers of audio samples as they're passed through to the hardware. At that point it's a matter of detecting peaks in the sound above a certain threshold, which you can do in a variety of different ways.
An FFT will not help you because you're interested in the time-domain waveform of the sound (its amplitude over time) not its frequency-domain characteristics (the relative strengths of different frequencies in different time windows.)

Loudness on Iphone

The scenario is that I start the recording at in my iphone app maybe by using AVAudioRecoder and when i have some input sound above certain threshold then i will do some thing. Is it possible to process the input audio while recoding??
Is there any way to know the input loudness in iPhone. Like what is the level of the loudness in numbers or if there is any other measure for that.
Look at AVFoundation, AVAudioRecorder and enable the property meteringEnabled to get threshold change callbacks.

FMOD on non-playing audio

Hey, is there any way to get the audio spectrum of a section of a song using FMOD if it is not playing?
Can I render a full song waveform using FMOD (+opengl/openframeworks/etc.) before the song is playing?
Yes.
Yes but you will have to do the your own spectrum analysis on the time domain wavedata.
You can get the wave data from the FMOD::Sound using Sound::lock. To do this you would have to create the sound as FMOD_SAMPLE which means the entire song will be decompressed into memory. You can render the waveform using this data and also conduct your spectrum analysis. FMOD's inbuilt 'getSpectrum' function will only work with snapshots of the playing data in a Channel or ChannelGroup.