Advertisement
meetakshay99

RemoteIO code

Jun 9th, 2011
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // AUGraphSampleViewController.h file
  2.  
  3. @interface AUGraphSampleViewController : UIViewController {
  4.  
  5.     AudioComponentDescription mIOUnitDescription; // Initializes it to zero to make sure there are no garbage values
  6.     AUGraph mProcessingGraph;
  7.     AUNode mIONode;
  8.     AudioUnit mIOUnit;
  9.    
  10.     double mGraphSampleRate;
  11. }
  12.  
  13. @end
  14.  
  15.  
  16. // AUGraphSampleViewController.m file
  17. #import "AUGraphSampleViewController.h"
  18. #import <AudioUnit/AudioUnit.h>
  19. #import <AudioToolbox/AudioToolbox.h>
  20. #import <AVFoundation/AVFoundation.h>
  21.  
  22. @implementation AUGraphSampleViewController
  23.  
  24. - (void) printErrorIfAny:(OSStatus)status
  25. {
  26.     if (status == kAUGraphErr_NodeNotFound)
  27.     {
  28.         NSLog(@"Received error : kAUGraphErr_NodeNotFound");
  29.     }
  30.     else if(status == kAUGraphErr_InvalidConnection)
  31.     {
  32.         NSLog(@"Received error : kAUGraphErr_InvalidConnection");
  33.     }
  34.     else if(status == kAUGraphErr_OutputNodeErr)
  35.     {
  36.         NSLog(@"Received error : kAUGraphErr_OutputNodeErr");
  37.     }
  38.     else if(status == kAUGraphErr_CannotDoInCurrentContext)
  39.     {
  40.         NSLog(@"Received error : kAUGraphErr_CannotDoInCurrentContext");
  41.     }
  42.     else if(status == kAUGraphErr_InvalidAudioUnit)
  43.     {
  44.         NSLog(@"Received error : kAUGraphErr_InvalidAudioUnit");
  45.     }
  46.     NSLog(@"%s",(char *)&status);
  47. }
  48.  
  49. - (void) createAudioUnitDescription
  50. {
  51.     mIOUnitDescription.componentType = kAudioUnitType_Output;
  52.     mIOUnitDescription.componentSubType = kAudioUnitSubType_RemoteIO;
  53.     mIOUnitDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
  54.     mIOUnitDescription.componentFlags = 0;
  55.     mIOUnitDescription.componentFlagsMask = 0;
  56. }
  57.  
  58. - (void) createInstantiateAndOpenGraph
  59. {
  60.    
  61.     OSStatus status;
  62.     // Creating a Graph
  63.     status = NewAUGraph(&mProcessingGraph);
  64.     [self printErrorIfAny:status];
  65.    
  66.     // Intantiating a Graph
  67.     // Add an audio unit node to the graph
  68.     [self createAudioUnitDescription];
  69.     status = AUGraphAddNode(mProcessingGraph, &mIOUnitDescription, &mIONode);
  70.     [self printErrorIfAny:status];
  71.    
  72.     // Open Graph
  73.     status = AUGraphOpen(mProcessingGraph); // Internally instantiates the audio Unit
  74.     [self printErrorIfAny:status];
  75.    
  76.     // Obtain reference of the audio units
  77.     AUGraphNodeInfo(mProcessingGraph, mIONode, NULL, &mIOUnit);
  78.    
  79.     // enable the input
  80.     UInt32 enableInput = 1;
  81.     AudioUnitElement inputBus = 1;
  82.     AudioUnitElement outputBus = 0;
  83.     status = AudioUnitSetProperty(mIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, inputBus, &enableInput, sizeof(enableInput));
  84.     [self printErrorIfAny:status];
  85.     status = AudioUnitSetProperty(mIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, outputBus, &enableInput, sizeof(enableInput));
  86.     [self printErrorIfAny:status];
  87.    
  88.     // set the output format
  89.     size_t bytesPerSample = sizeof (AudioUnitSampleType);
  90.     AudioStreamBasicDescription stereoStreamFormat = {0};
  91.     stereoStreamFormat.mFormatID = kAudioFormatLinearPCM;
  92.     stereoStreamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
  93.     stereoStreamFormat.mBytesPerPacket  = bytesPerSample;
  94.     stereoStreamFormat.mBytesPerFrame = bytesPerSample;
  95.     stereoStreamFormat.mFramesPerPacket = 1;
  96.     stereoStreamFormat.mBitsPerChannel = 8 * bytesPerSample;
  97.     stereoStreamFormat.mChannelsPerFrame  = 2// 1 indicates mono and 2 indicates stereo
  98.     stereoStreamFormat.mSampleRate  = 44100.00;
  99.      
  100.     status = AudioUnitSetProperty(mIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, outputBus, &stereoStreamFormat, sizeof(stereoStreamFormat));
  101.     [self printErrorIfAny:status];
  102.     status = AudioUnitSetProperty(mIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, inputBus, &stereoStreamFormat, sizeof(stereoStreamFormat));
  103.     [self printErrorIfAny:status];
  104.    
  105.     // Initialize the graph
  106.     status = AUGraphInitialize(mProcessingGraph);
  107.     if (status)
  108.     {
  109.         NSLog(@"Error... while initializing the graph");
  110.         [self printErrorIfAny:status];
  111.     }
  112.     else
  113.     {
  114.         NSLog(@"Successfully initialized the graph");
  115.         status = AUGraphStart(mProcessingGraph);
  116.         [self printErrorIfAny:status];
  117.     }
  118. }
  119.  
  120.  
  121. /*
  122. // The designated initializer. Override to perform setup that is required before the view is loaded.
  123. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  124.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  125.     if (self) {
  126.         // Custom initialization
  127.     }
  128.     return self;
  129. }
  130. */
  131.  
  132. /*
  133. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  134. - (void)loadView {
  135. }
  136. */
  137.  
  138. - (void) printAudioSessionError:(NSError *)error withMsg:(NSString *)msg
  139. {
  140.     if (error)
  141.         NSLog(@"%@ : %@",msg,[error description]);
  142. }
  143.  
  144. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  145. - (void)viewDidLoad {
  146.     [super viewDidLoad];
  147.    
  148.     // Define the required sample rate
  149.     mGraphSampleRate = 44100.00; // Hertz
  150.    
  151.     // First set the required Audio Session
  152.     NSError *audioSessionError = nil;
  153.     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  154.     [audioSession setPreferredHardwareSampleRate:mGraphSampleRate error:&audioSessionError];
  155.     [self printAudioSessionError:audioSessionError withMsg:@"Error while setting hardware sample rate"];
  156.     [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&audioSessionError];    // This category supports Audio input and output.
  157.     [self printAudioSessionError:audioSessionError withMsg:@"Error while setting category"];
  158.     [audioSession setActive:YES error:&audioSessionError];
  159.     [self printAudioSessionError:audioSessionError withMsg:@"Error while activating audio session"];
  160.     mGraphSampleRate = [audioSession currentHardwareSampleRate];    // Reload from system.
  161.    
  162.     [self createInstantiateAndOpenGraph];
  163. }
  164.  
  165.  
  166.  
  167. /*
  168. // Override to allow orientations other than the default portrait orientation.
  169. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  170.     // Return YES for supported orientations
  171.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  172. }
  173. */
  174.  
  175. - (void)didReceiveMemoryWarning {
  176.     // Releases the view if it doesn't have a superview.
  177.     [super didReceiveMemoryWarning];
  178.    
  179.     // Release any cached data, images, etc that aren't in use.
  180. }
  181.  
  182. - (void)viewDidUnload {
  183.     // Release any retained subviews of the main view.
  184.     // e.g. self.myOutlet = nil;
  185. }
  186.  
  187.  
  188. - (void)dealloc {
  189.     [super dealloc];
  190. }
  191.  
  192. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement