Hey guys I'm new here! Currently I am developing an application for the iPhone and iPad that plays a simple sound when you blow in the devices microphone.
Here are my issues:
1) Sound is kinda wacky sounding. Doesn't sound like the original sound when i blow in the mic.
2) How can I get the sound to play again (instead of just once) without having to restart the simulator? My repeat code doesn't work
The code i provided below works. Just needs some tuning. Any help appreciated!!

To test it, create a new Window-Based Application and name the project as "MicBlow". You will have to upload your own sound clip and edit the code so it plays. Add the FrameWorks to. You can blow or tap on your computers microphone to test it in the simulator.
MicBlowViewController.h
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import <AudioToolbox/AudioToolbox.h>
@interface MicBlowViewController : UIViewController {
AVAudioRecorder *recorder;
NSTimer *levelTimer;
double lowPassResults;
AVAudioPlayer *audioPlayer;
}
- (void)levelTimerCallback:(NSTimer *)timer;
@end
MicBlowViewController.m
Code:
#import "MicBlowViewController.h"
@implementation MicBlowViewController
- (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])); // BLOW SENSITIVITY
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.60){
NSLog(@"Mic blow detected");
// START OF AUDIO CODE ------------------------
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"Wind", CFSTR ("wav"), NULL); // SOUND FILE HERE
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
audioPlayer.numberOfLoops = -1;
// END OF AUDIO CODE------------------------------
[self awakeFromNib];
[audioPlayer release];
}
}
- (void)dealloc {
[levelTimer release];
[recorder release];
[super dealloc];
}
@end
Thank you,
Any help very much appreciated!!