Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SAG3194

macrumors 6502
Original poster
Jan 4, 2009
399
0
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!
 
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.
 
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.
 
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.
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.