Permalink
Browse files

Flag to override output to iphone speaker

  • Loading branch information...
1 parent 5d46854 commit 465b7b222057237edbe647215cf51e90f8185109 @ndonald2 ndonald2 committed Jun 12, 2013
Showing with 33 additions and 89 deletions.
  1. +6 −2 Novocaine/Novocaine.h
  2. +27 −87 Novocaine/Novocaine.m
View
@@ -101,8 +101,12 @@ typedef void (^NovocaineInputBlock)(float *data, UInt32 numFrames, UInt32 numCha
// ------ These properties/methods are used for configuration -------
-@property (nonatomic, copy) NSString *inputRoute;
-@property (nonatomic, assign) BOOL inputEnabled;
+@property (nonatomic, copy) NSString *inputRoute;
+@property (nonatomic, assign) BOOL inputEnabled;
+
+#ifdef USING_IOS
+@property (nonatomic, assign) BOOL forceOutputToSpeaker;
+#endif
// ND: Exposing the block setters this way will create the correct block signature for auto-complete.
// These will map to "copy" property setters in class continuation in source file
View
@@ -137,6 +137,9 @@ - (id)init
// Fire up the audio session ( with steady error checking ... )
[self setupAudioSession];
+
+ // start audio units
+ [self setupAudioUnits];
return self;
@@ -179,111 +182,48 @@ - (void)freeBuffers
- (void)setInputEnabled:(BOOL)inputEnabled
{
- if (inputEnabled != _inputEnabled){
-
- if (inputEnabled){
-
- if (self.inputAvailable) {
-
- // Enable input
- UInt32 one = 1;
- OSStatus err = AudioUnitSetProperty(_inputUnit,
- kAudioOutputUnitProperty_EnableIO,
- kAudioUnitScope_Input,
- kInputBus,
- &one,
- sizeof(one));
-
- if (err){
-
- }
- else{
- _inputEnabled = YES;
- }
- }
-
- // If we don't have input, then ask the user to provide some
- else
- {
-
- // TODO: Not sure a
-#if defined ( USING_IOS )
- UIAlertView *noInputAlert =
- [[UIAlertView alloc] initWithTitle:@"No Audio Input"
- message:@"Couldn't find any audio input. Plug in your Apple headphones or another microphone."
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
-
- [noInputAlert show];
-#endif
-
- }
- }
- else
- {
- // Disable input
- UInt32 zero = 0;
- OSStatus err = AudioUnitSetProperty(_inputUnit,
- kAudioOutputUnitProperty_EnableIO,
- kAudioUnitScope_Input,
- kInputBus,
- &zero,
- sizeof(zero));
-
- if (err){
-
- }
- else{
- _inputEnabled = NO;
- }
- }
+
+ _inputEnabled = inputEnabled;
+}
+#ifdef USING_IOS
+- (void)setForceOutputToSpeaker:(BOOL)forceOutputToSpeaker
+{
+ UInt32 value = forceOutputToSpeaker ? 1 : 0;
+
+#if !TARGET_IPHONE_SIMULATOR
+ // should not be fatal error
+ OSStatus err = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(UInt32), &value);
+ if (err != noErr){
+ NSLog(@"Could not override audio output route to speaker");
+ }
+ else{
+ _forceOutputToSpeaker = forceOutputToSpeaker;
}
+#else
+ _forceOutputToSpeaker = forceOutputToSpeaker;
+#endif
}
-
+#endif
#pragma mark - Audio Methods
- (void)setupAudioSession
{
-
// Initialize and configure the audio session, and add an interuption listener
#if defined ( USING_IOS )
- CheckError( AudioSessionInitialize(NULL, NULL, sessionInterruptionListener, (__bridge void *)(self)), "Couldn't initialize audio session");
+ NSError *err = nil;
+ if (![[AVAudioSession sharedInstance] setActive:YES error:&err]){
+ NSLog(@"Could not activate audio session: %@", err);
+ }
[self checkAudioSource];
#elif defined ( USING_OSX )
// TODO: grab the audio device
[self enumerateAudioDevices];
self.inputAvailable = YES;
#endif
-
-
- // Check the session properties (available input routes, number of channels, etc)
-
- // If we do have input, then let's rock 'n roll.
-// if (self.inputAvailable) {
-// [self setupAudio];
-// [self play];
-// }
-//
-// // If we don't have input, then ask the user to provide some
-// else
-// {
-//#if defined ( USING_IOS )
-// UIAlertView *noInputAlert =
-// [[UIAlertView alloc] initWithTitle:@"No Audio Input"
-// message:@"Couldn't find any audio input. Plug in your Apple headphones or another microphone."
-// delegate:self
-// cancelButtonTitle:@"OK"
-// otherButtonTitles:nil];
-//
-// [noInputAlert show];
-//#endif
-//
-// }
}

0 comments on commit 465b7b2

Please sign in to comment.