Detect Blowing into Mic

Discussion in 'iOS Programming' started by SAG3194, May 27, 2009.

  1. SAG3194 macrumors 6502


    Jan 4, 2009
    How would I make an app so that when someone blows into the Microphone (like smule) an image comes up? If this is already posted I apologize please point me into the right direction.

  2. evana macrumors newbie

    Sep 2, 2009
    Did you solve this problem can u pls help me out i m stack here.
  3. sspbond007 macrumors member

    Aug 19, 2008
    Try This one.
    It worked for me. Dont forget to import AVFoundation framework.

    - (void)viewDidLoad {
        [super viewDidLoad];
    	NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
    	NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
    							  [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
    							  [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
    							  [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
    							  [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
    	NSError *error;
    	recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
    	if (recorder) {
    		[recorder prepareToRecord];
    		recorder.meteringEnabled = YES;
    		[recorder record];
    		levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    	} else
    		NSLog([error description]);	
    - (void)levelTimerCallback:(NSTimer *)timer {
    	[recorder updateMeters];
    	const double ALPHA = 0.05;
    	double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
    	lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;	
    	if (lowPassResults > 0.65)
    		NSLog(@"Mic blow detected");
    set ALPHA to change sensitivity.
  4. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    DSP code

    If you want to properly detect only blowing and with some precision, you will have to delve into low latency Audio Unit RemoteIO sound input, and advanced music signal processing. Something like audio DSP granular analysis/resynthesis for pitch/time stretching or vocoder analysis, with the filtered input granules sent on as input to your physics or graphics animation engine. Anything else will have higher latency and/or pick up a lot of sounds other than blowing.

    Not doing this is probably why Smule apps works, and the cheap copies don't.

  5. Ken11110000 macrumors newbie

    Feb 26, 2010
    low freq filter

    Also...I don't know what the lowest sample rate that is available, but lowering that value would cut out a few higher pitched sounds. You need at least two samples to define a signal's period, so with 44100, you'd barely get 22050Hz as the highest sound to be recorded. If you could lower it to say... 4000 samples per second, then you'd barely be getting 2000Hz as the highest sound. Right around the mid section of human speech. I'm not sure what frequency range a blowing sound falls under, but that would be a start. So, in conclusion, try lowering the sample rate.
  6. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    Sounds, of the type usually considered to be blowing, can be very wide band indeed, including a fair amount of high frequency (for audio) content.
  7. HarryWorksInc macrumors regular

    Feb 21, 2010

    Everything here looks good to me i tried running this, but what kind of range would the kAudioFormatAppleLossless integer be?

    Sorry, i'm new to using the microphone

Share This Page