From a58269fcdb23629955375adb8d5fd89b9ef1ae21 Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Sun, 9 Feb 2014 23:32:49 +0000 Subject: [PATCH] Got EQ working (iOS5 and OSX 10.9). Changed OSX projects to use 10.9 SDK --- ExampleApp/ExampleApp/AppDelegate.m | 4 +- .../ExampleAppMac.xcodeproj/project.pbxproj | 4 +- ExampleAppMac/ExampleAppMac/AppDelegate.m | 2 +- .../StreamingKit.xcodeproj/project.pbxproj | 4 +- StreamingKit/StreamingKit/STKAudioPlayer.h | 2 + StreamingKit/StreamingKit/STKAudioPlayer.m | 91 +++++++++++++------ 6 files changed, 72 insertions(+), 35 deletions(-) diff --git a/ExampleApp/ExampleApp/AppDelegate.m b/ExampleApp/ExampleApp/AppDelegate.m index 219e046..04f016a 100644 --- a/ExampleApp/ExampleApp/AppDelegate.m +++ b/ExampleApp/ExampleApp/AppDelegate.m @@ -31,10 +31,12 @@ self.window.backgroundColor = [UIColor whiteColor]; - audioPlayer = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = NO, .equalizerBandFrequencies = { 0 } /*{50, 100, 200, 400, 800, 1600, 2600, 16000}*/ }]; + audioPlayer = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = NO, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }]; audioPlayer.meteringEnabled = YES; audioPlayer.volume = 1.0; + [audioPlayer setGain:24 forEqualizerBand:0]; + AudioPlayerView* audioPlayerView = [[AudioPlayerView alloc] initWithFrame:self.window.bounds]; audioPlayerView.delegate = self; diff --git a/ExampleAppMac/ExampleAppMac.xcodeproj/project.pbxproj b/ExampleAppMac/ExampleAppMac.xcodeproj/project.pbxproj index d2e7fce..727c796 100644 --- a/ExampleAppMac/ExampleAppMac.xcodeproj/project.pbxproj +++ b/ExampleAppMac/ExampleAppMac.xcodeproj/project.pbxproj @@ -432,7 +432,7 @@ "$(SRCROOT)/../StreamingKit/StreamingKit", ); INFOPLIST_FILE = "ExampleAppMac/ExampleAppMac-Info.plist"; - MACOSX_DEPLOYMENT_TARGET = 10.8; + MACOSX_DEPLOYMENT_TARGET = ""; OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; @@ -452,7 +452,7 @@ "$(SRCROOT)/../StreamingKit/StreamingKit", ); INFOPLIST_FILE = "ExampleAppMac/ExampleAppMac-Info.plist"; - MACOSX_DEPLOYMENT_TARGET = 10.8; + MACOSX_DEPLOYMENT_TARGET = ""; OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; diff --git a/ExampleAppMac/ExampleAppMac/AppDelegate.m b/ExampleAppMac/ExampleAppMac/AppDelegate.m index 15e52c2..e215d62 100644 --- a/ExampleAppMac/ExampleAppMac/AppDelegate.m +++ b/ExampleAppMac/ExampleAppMac/AppDelegate.m @@ -40,7 +40,7 @@ [[self.window contentView] addSubview:playFromHTTPButton]; [[self.window contentView] addSubview:meter]; - audioPlayer = [[STKAudioPlayer alloc] init]; + audioPlayer = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} } ]; audioPlayer.delegate = self; audioPlayer.meteringEnabled = YES; audioPlayer.volume = 1.0; diff --git a/StreamingKit/StreamingKit.xcodeproj/project.pbxproj b/StreamingKit/StreamingKit.xcodeproj/project.pbxproj index 9d2c596..fc79b52 100644 --- a/StreamingKit/StreamingKit.xcodeproj/project.pbxproj +++ b/StreamingKit/StreamingKit.xcodeproj/project.pbxproj @@ -565,7 +565,7 @@ "DEBUG=1", "$(inherited)", ); - MACOSX_DEPLOYMENT_TARGET = 10.8; + MACOSX_DEPLOYMENT_TARGET = ""; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; }; @@ -582,7 +582,7 @@ GCC_ENABLE_OBJC_EXCEPTIONS = YES; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "StreamingKitMac/StreamingKitMac-Prefix.pch"; - MACOSX_DEPLOYMENT_TARGET = 10.8; + MACOSX_DEPLOYMENT_TARGET = ""; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; }; diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.h b/StreamingKit/StreamingKit/STKAudioPlayer.h index 138c736..f0c766c 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.h +++ b/StreamingKit/StreamingKit/STKAudioPlayer.h @@ -254,4 +254,6 @@ typedef void(^STKFrameFilter)(UInt32 channelsPerFrame, UInt32 bytesPerFrame, UIn /// Return values are between -60 (low) and 0 (high). -(float) averagePowerInDecibelsForChannel:(NSUInteger)channelNumber; +-(void) setGain:(float)gain forEqualizerBand:(int)bandIndex; + @end diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 1a00275..90b5071 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -87,6 +87,7 @@ static void PopulateOptionsWithDefault(STKAudioPlayerOptions* options) if ((status = (call))) \ { \ [self unexpectedError:STKAudioPlayerErrorAudioSystemError]; \ + return;\ } typedef enum @@ -1909,7 +1910,7 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl { return; } - + OSStatus status; AudioComponentDescription eqDescription = (AudioComponentDescription) @@ -1919,8 +1920,6 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl .componentManufacturer=kAudioUnitManufacturer_Apple }; - CHECK_STATUS_AND_RETURN(AudioUnitSetProperty(eqUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &canonicalAudioStreamBasicDescription, sizeof(canonicalAudioStreamBasicDescription))); - CHECK_STATUS_AND_RETURN(AUGraphAddNode(audioGraph, &eqDescription, &eqNode)); CHECK_STATUS_AND_RETURN(AUGraphNodeInfo(audioGraph, eqNode, NULL, &eqUnit)); @@ -1940,11 +1939,30 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl { CHECK_STATUS_AND_RETURN(AudioUnitSetParameter(eqUnit, kAUNBandEQParam_BypassBand + i, kAudioUnitScope_Global, 0, (AudioUnitParameterValue)0, 0)); } - - for (int i = 0; i < eqBandCount; i++) +} + +-(void) setGain:(float)gain forEqualizerBand:(int)bandIndex +{ + if (!eqUnit) { - CHECK_STATUS_AND_RETURN(AudioUnitSetParameter(eqUnit, kAUNBandEQParam_Gain + i, kAudioUnitScope_Global, -74, 0, 0)); + return; } + + AudioUnitSetParameter(eqUnit, kAUNBandEQParam_Gain + bandIndex, kAudioUnitScope_Global, 0, gain, 0); +} + +-(AUNode) createConverterNode:(AudioStreamBasicDescription)srcFormat desFormat:(AudioStreamBasicDescription)desFormat +{ + OSStatus status; + AUNode convertNode; + AudioComponentInstance convertUnit; + + status = AUGraphAddNode(audioGraph, &convertUnitDescription, &convertNode); + status = AUGraphNodeInfo(audioGraph, convertNode, &mixerDescription, &convertUnit); + status = AudioUnitSetProperty(convertUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(srcFormat)); + status = AudioUnitSetProperty(convertUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &desFormat, sizeof(desFormat)); + + return convertNode; } -(void) connectNodes:(AUNode)srcNode desNode:(AUNode)desNode srcUnit:(AudioComponentInstance)srcUnit desUnit:(AudioComponentInstance)desUnit @@ -1958,35 +1976,54 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl status = AudioUnitSetProperty(desUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(srcFormat)); - if (status == 0) + if (status) { + AUNode convertNode = [self createConverterNode:srcFormat desFormat:desFormat]; + + CHECK_STATUS_AND_RETURN(AUGraphConnectNodeInput(audioGraph, convertNode, 0, mixerNode, 0)); } + else + { + CHECK_STATUS_AND_RETURN(AUGraphConnectNodeInput(audioGraph, srcNode, 0, desNode, 0)); + } } -#define CHECK_AND_CREATE_FIRST_UNIT(x, y) \ -{ \ - if (firstNode == 0) \ - { \ - firstUnit = x; \ - firstNode = y; \ - CHECK_STATUS_AND_RETURN(AudioUnitSetProperty(firstUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &canonicalAudioStreamBasicDescription, sizeof(canonicalAudioStreamBasicDescription))); \ - } \ +-(void) setOutputCallbackForFirstNode:(AUNode)firstNode firstUnit:(AudioComponentInstance)firstUnit +{ + OSStatus status; + AURenderCallbackStruct callbackStruct; + + callbackStruct.inputProc = OutputRenderCallback; + callbackStruct.inputProcRefCon = (__bridge void*)self; + + status = AudioUnitSetProperty(firstUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &canonicalAudioStreamBasicDescription, sizeof(canonicalAudioStreamBasicDescription)); + + if (status) + { + AudioStreamBasicDescription format; + UInt32 size = sizeof(format); + + CHECK_STATUS_AND_RETURN(AudioUnitGetProperty(firstUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, &size)); + + AUNode converterNode = [self createConverterNode:canonicalAudioStreamBasicDescription desFormat:format]; + + CHECK_STATUS_AND_RETURN(AUGraphConnectNodeInput(audioGraph, converterNode, 0, firstNode, 0)); + + CHECK_STATUS_AND_RETURN(AUGraphSetNodeInputCallback(audioGraph, converterNode, 0, &callbackStruct)); + } + else + { + CHECK_STATUS_AND_RETURN(AUGraphSetNodeInputCallback(audioGraph, firstNode, 0, &callbackStruct)); + } } -(void) createAudioGraph { OSStatus status; - AUNode firstNode = 0; - AudioComponentInstance firstUnit = 0; - AUNode currentNode = 0; - AURenderCallbackStruct callbackStruct; NSMutableArray* nodes = [[NSMutableArray alloc] init]; NSMutableArray* units = [[NSMutableArray alloc] init]; - callbackStruct.inputProc = OutputRenderCallback; - callbackStruct.inputProcRefCon = (__bridge void*)self; - - CHECK_STATUS_AND_RETURN(NewAUGraph(&audioGraph)); + CHECK_STATUS_AND_RETURN(NewAUGraph(&audioGraph)); CHECK_STATUS_AND_RETURN(AUGraphOpen(audioGraph)); [self createEqUnit]; @@ -2010,12 +2047,8 @@ static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* cl [nodes addObject:@(outputNode)]; [units addObject:[NSValue valueWithPointer:outputUnit]]; } - - firstNode = (AUNode)[[nodes objectAtIndex:0] intValue]; - firstUnit = (AudioComponentInstance)[[nodes objectAtIndex:0] pointerValue]; - CHECK_STATUS_AND_RETURN(AUGraphSetNodeInputCallback(audioGraph, firstNode, 0, &callbackStruct)); - - CHECK_STATUS_AND_RETURN(AudioUnitSetProperty(firstUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &canonicalAudioStreamBasicDescription, sizeof(canonicalAudioStreamBasicDescription))); + + [self setOutputCallbackForFirstNode:(AUNode)[[nodes objectAtIndex:0] intValue] firstUnit:(AudioComponentInstance)[[units objectAtIndex:0] pointerValue]]; for (int i = 0; i < nodes.count - 1; i++) {