How to Detect the Blow of the Mic on xCode

Save

Apple's Xcode programming application lets programmers with a knowledge of C and Objective-C programming develop applications for the iOS and OS X platforms. If you are developing an application for an iOS device and you want the application to be able to detect when a user blows into the microphone, there is a very specific way to code the application to allow it to do so. This code involves allowing the application to receive input from the microphone, sampling the audio level and telling the application to listen for the sound of someone blowing into the microphone, all of which can be implemented in the Xcode application.

  • Launch Xcode and open the view-based project that you want to detect when a user blows into the microphone.

  • Click the arrow to the left of the "Targets" option, then right-click the item that is named after the application. For example, if the application is named "GenericGame," the item will be named "GenericGame." Move your mouse cursor over the "Add" option and select the "Existing Frameworks..." option.

  • Click the "+" button, select the "AVFoundation.framework" option and click the "Add" button.

  • Click the arrow to the left of the folder named after the project. For example, if the project is named "GenericGame," click the arrow to the left of the "GenericGame" folder. Click the arrow to the left the "Classes" folder and click the "ViewController.h" file to view the file's contents.

  • Add the following two lines of code directly beneath the "#import <UIKit/UIKit.h>" line of code:

    import <AVFoundation/AVFoundation.h>
    import <CoreAudio/CoreAudioTypes.h>.
  • Add the following line of code directly beneath the "@interface MicBlowViewController : UIViewController {" line of code:
    AVAudioRecorder *recorder;.

  • Add the following code directly beneath the "[super viewDidLoad];" line of code:
    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];
    } else
    NSLog([error description]);.

  • Add the following line of code directly beneath the "- (void)dealloc {" line of code:
    [recorder release];.

  • Add the following code directly beneath the "AVAudioRecorder recorder;" line of code:
    NSTimer
    levelTimer;
    }

    • (void)levelTimerCallback:(NSTimer *)timer;
  • Add the following line of code directly beneath the "[recorder record];" line of code:
    levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];.

  • Click the project's "ViewController.m" file to view its contents. Enter the following code into the ViewController.m file:

    • (void)levelTimerCallback:(NSTimer *)timer {
      [recorder updateMeters];
      NSLog(@"Average input: %f Peak input: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]);
      }
  • Add the following code directly beneath the "- (void)dealloc {" line of code:
    [levelTimer release];.

  • Click on the project's "ViewController.h" file to view its contents. Add the following code directly beneath the "NSTimer *levelTimer;" line of code:
    double lowPassResults;.

  • Locate the section of code that begins with the line "- (void)levelTimerCallback:(NSTimer *)timer {" and ends with "}." Replace this section of code with the following code:

    • (void)listenForBlow:(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.95)
    NSLog(@"Mic blow detected");
    }

References

Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!