Detect Blowing into Mic

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

  1. SAG3194 macrumors 6502

    SAG3194

    Joined:
    Jan 4, 2009
    #1
    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.

    Thanks!
     
  2. evana macrumors newbie

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

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

    Code:
    - (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,
    							  nil];
    		
    	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

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
    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.

    ymmv.
     
  5. Ken11110000 macrumors newbie

    Joined:
    Feb 26, 2010
    #5
    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

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    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

    Joined:
    Feb 21, 2010
    #7
    cool

    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