Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AUGraphSampleViewController.h file
- @interface AUGraphSampleViewController : UIViewController {
- AudioComponentDescription mIOUnitDescription; // Initializes it to zero to make sure there are no garbage values
- AUGraph mProcessingGraph;
- AUNode mIONode;
- AudioUnit mIOUnit;
- double mGraphSampleRate;
- }
- @end
- // AUGraphSampleViewController.m file
- #import "AUGraphSampleViewController.h"
- #import <AudioUnit/AudioUnit.h>
- #import <AudioToolbox/AudioToolbox.h>
- #import <AVFoundation/AVFoundation.h>
- @implementation AUGraphSampleViewController
- - (void) printErrorIfAny:(OSStatus)status
- {
- if (status == kAUGraphErr_NodeNotFound)
- {
- NSLog(@"Received error : kAUGraphErr_NodeNotFound");
- }
- else if(status == kAUGraphErr_InvalidConnection)
- {
- NSLog(@"Received error : kAUGraphErr_InvalidConnection");
- }
- else if(status == kAUGraphErr_OutputNodeErr)
- {
- NSLog(@"Received error : kAUGraphErr_OutputNodeErr");
- }
- else if(status == kAUGraphErr_CannotDoInCurrentContext)
- {
- NSLog(@"Received error : kAUGraphErr_CannotDoInCurrentContext");
- }
- else if(status == kAUGraphErr_InvalidAudioUnit)
- {
- NSLog(@"Received error : kAUGraphErr_InvalidAudioUnit");
- }
- NSLog(@"%s",(char *)&status);
- }
- - (void) createAudioUnitDescription
- {
- mIOUnitDescription.componentType = kAudioUnitType_Output;
- mIOUnitDescription.componentSubType = kAudioUnitSubType_RemoteIO;
- mIOUnitDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
- mIOUnitDescription.componentFlags = 0;
- mIOUnitDescription.componentFlagsMask = 0;
- }
- - (void) createInstantiateAndOpenGraph
- {
- OSStatus status;
- // Creating a Graph
- status = NewAUGraph(&mProcessingGraph);
- [self printErrorIfAny:status];
- // Intantiating a Graph
- // Add an audio unit node to the graph
- [self createAudioUnitDescription];
- status = AUGraphAddNode(mProcessingGraph, &mIOUnitDescription, &mIONode);
- [self printErrorIfAny:status];
- // Open Graph
- status = AUGraphOpen(mProcessingGraph); // Internally instantiates the audio Unit
- [self printErrorIfAny:status];
- // Obtain reference of the audio units
- AUGraphNodeInfo(mProcessingGraph, mIONode, NULL, &mIOUnit);
- // enable the input
- UInt32 enableInput = 1;
- AudioUnitElement inputBus = 1;
- AudioUnitElement outputBus = 0;
- status = AudioUnitSetProperty(mIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, inputBus, &enableInput, sizeof(enableInput));
- [self printErrorIfAny:status];
- status = AudioUnitSetProperty(mIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, outputBus, &enableInput, sizeof(enableInput));
- [self printErrorIfAny:status];
- // set the output format
- size_t bytesPerSample = sizeof (AudioUnitSampleType);
- AudioStreamBasicDescription stereoStreamFormat = {0};
- stereoStreamFormat.mFormatID = kAudioFormatLinearPCM;
- stereoStreamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
- stereoStreamFormat.mBytesPerPacket = bytesPerSample;
- stereoStreamFormat.mBytesPerFrame = bytesPerSample;
- stereoStreamFormat.mFramesPerPacket = 1;
- stereoStreamFormat.mBitsPerChannel = 8 * bytesPerSample;
- stereoStreamFormat.mChannelsPerFrame = 2; // 1 indicates mono and 2 indicates stereo
- stereoStreamFormat.mSampleRate = 44100.00;
- status = AudioUnitSetProperty(mIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, outputBus, &stereoStreamFormat, sizeof(stereoStreamFormat));
- [self printErrorIfAny:status];
- status = AudioUnitSetProperty(mIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, inputBus, &stereoStreamFormat, sizeof(stereoStreamFormat));
- [self printErrorIfAny:status];
- // Initialize the graph
- status = AUGraphInitialize(mProcessingGraph);
- if (status)
- {
- NSLog(@"Error... while initializing the graph");
- [self printErrorIfAny:status];
- }
- else
- {
- NSLog(@"Successfully initialized the graph");
- status = AUGraphStart(mProcessingGraph);
- [self printErrorIfAny:status];
- }
- }
- /*
- // The designated initializer. Override to perform setup that is required before the view is loaded.
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- */
- /*
- // Implement loadView to create a view hierarchy programmatically, without using a nib.
- - (void)loadView {
- }
- */
- - (void) printAudioSessionError:(NSError *)error withMsg:(NSString *)msg
- {
- if (error)
- NSLog(@"%@ : %@",msg,[error description]);
- }
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Define the required sample rate
- mGraphSampleRate = 44100.00; // Hertz
- // First set the required Audio Session
- NSError *audioSessionError = nil;
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- [audioSession setPreferredHardwareSampleRate:mGraphSampleRate error:&audioSessionError];
- [self printAudioSessionError:audioSessionError withMsg:@"Error while setting hardware sample rate"];
- [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&audioSessionError]; // This category supports Audio input and output.
- [self printAudioSessionError:audioSessionError withMsg:@"Error while setting category"];
- [audioSession setActive:YES error:&audioSessionError];
- [self printAudioSessionError:audioSessionError withMsg:@"Error while activating audio session"];
- mGraphSampleRate = [audioSession currentHardwareSampleRate]; // Reload from system.
- [self createInstantiateAndOpenGraph];
- }
- /*
- // Override to allow orientations other than the default portrait orientation.
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- */
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (void)dealloc {
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement