Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-22-2011, 09:43 PM   #1 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default Blow in iPhone mic & activate sound (help please)

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!!
aceiswild is offline   Reply With Quote
Reply

Bookmarks

Tags
blow, iphone, mic, sensitivity, sound

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
14 members and 376 guests
13dario13, 7twenty7, buggen, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, n00b, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:27 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0