Advertisement
Guest User

AUGraph

a guest
Oct 16th, 2013
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void) setUpAUGraph {
  2.     if (self.auGraph) {
  3.         CheckError(AUGraphClose(self.auGraph),
  4.                    "Couldn't close old AUGraph");
  5.         CheckError (DisposeAUGraph(self.auGraph),
  6.                     "Couldn't dispose old AUGraph");
  7.     }
  8.    
  9.     CheckError(NewAUGraph(&_auGraph),
  10.                "Couldn't create new AUGraph");
  11.    
  12.     CheckError(AUGraphOpen(self.auGraph),
  13.                "Couldn't open AUGraph");
  14.  
  15.     // start with file player unit
  16.     AudioComponentDescription fileplayercd = {0};
  17.     fileplayercd.componentType = kAudioUnitType_Generator;
  18.     fileplayercd.componentSubType = kAudioUnitSubType_AudioFilePlayer;
  19.     fileplayercd.componentManufacturer = kAudioUnitManufacturer_Apple;
  20.    
  21.     AUNode filePlayerNode;
  22.     CheckError(AUGraphAddNode(self.auGraph,
  23.                               &fileplayercd,
  24.                               &filePlayerNode),
  25.                "Couldn't add file player node");
  26.     // get the actual unit
  27.     CheckError(AUGraphNodeInfo(self.auGraph,
  28.                                filePlayerNode,
  29.                                NULL,
  30.                                &_filePlayerUnit),
  31.                "couldn't get file player node");
  32.    
  33.     // remote io unit
  34.     AudioComponentDescription outputcd = {0};
  35.     outputcd.componentType = kAudioUnitType_Output;
  36.     outputcd.componentSubType = kAudioUnitSubType_RemoteIO;
  37.     outputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
  38.    
  39.     AUNode ioNode;
  40.     CheckError(AUGraphAddNode(self.auGraph,
  41.                               &outputcd,
  42.                               &ioNode),
  43.                "couldn't add remote io node");
  44.    
  45.     // get the remote io unit from the node
  46.     CheckError(AUGraphNodeInfo(self.auGraph,
  47.                                ioNode,
  48.                                NULL,
  49.                                &_ioUnit),
  50.                "couldn't get remote io unit");
  51.    
  52.    
  53.     // effect unit here
  54.     AudioComponentDescription effectcd = {0};
  55.     effectcd.componentType = kAudioUnitType_FormatConverter;
  56. //  effectcd.componentSubType = kAudioUnitSubType_NewTimePitch;
  57.  
  58.     effectcd.componentSubType = kAudioUnitSubType_Varispeed;
  59.  
  60.     effectcd.componentManufacturer = kAudioUnitManufacturer_Apple;
  61.    
  62.     AUNode effectNode;
  63.     CheckError(AUGraphAddNode(self.auGraph,
  64.                               &effectcd,
  65.                               &effectNode),
  66.                "couldn't get effect node [time/pitch]");
  67.    
  68.     // get effect unit from the node
  69.     CheckError(AUGraphNodeInfo(self.auGraph,
  70.                                effectNode,
  71.                                NULL,
  72.                                &_effectUnit),
  73.                "couldn't get effect unit from node");
  74.    
  75.     // enable output to the remote io unit
  76.     UInt32 oneFlag = 1;
  77.     UInt32 busZero = 0;
  78.     CheckError(AudioUnitSetProperty(self.ioUnit,
  79.                                     kAudioOutputUnitProperty_EnableIO,
  80.                                     kAudioUnitScope_Output,
  81.                                     busZero,
  82.                                     &oneFlag,
  83.                                     sizeof(oneFlag)),
  84.                "Couldn't enable output on bus 0");
  85.    
  86.     // get stream format that the effect wants
  87.     AudioStreamBasicDescription streamFormat;
  88.     UInt32 propertySize = sizeof (streamFormat);
  89.     CheckError(AudioUnitGetProperty(self.effectUnit,
  90.                                     kAudioUnitProperty_StreamFormat,
  91.                                     kAudioUnitScope_Input,
  92.                                     0,
  93.                                     &streamFormat,
  94.                                     &propertySize),
  95.                "Couldn't get effect unit stream format");
  96.    
  97.  
  98.     // apply effect's format elsewhere in the graph
  99.     CheckError(AudioUnitSetProperty(self.filePlayerUnit,
  100.                                     kAudioUnitProperty_StreamFormat,
  101.                                     kAudioUnitScope_Output,
  102.                                     busZero,
  103.                                     &streamFormat,
  104.                                     sizeof(streamFormat)),
  105.                "couldn't set stream format on file player bus 0 output");
  106.    
  107.     CheckError(AudioUnitSetProperty(self.ioUnit,
  108.                                     kAudioUnitProperty_StreamFormat,
  109.                                     kAudioUnitScope_Input,
  110.                                     busZero,
  111.                                     &streamFormat,
  112.                                     sizeof(streamFormat)),
  113.                "couldn't set stream format on iounit bus 0 input");
  114.  
  115.  
  116.     // make connections
  117.     CheckError(AUGraphConnectNodeInput(self.auGraph,
  118.                                        filePlayerNode,
  119.                                        0,
  120.                                        effectNode,
  121.                                        0),
  122.                "couldn't connect file player bus 0 output to effect bus 0 input");
  123.    
  124.     CheckError(AUGraphConnectNodeInput(self.auGraph,
  125.                                        effectNode,
  126.                                        0,
  127.                                        ioNode,
  128.                                        0),
  129.                "couldn't connect effect bus 0 output to remoteio bus 0 input");
  130.    
  131.    
  132.     CheckError(AUGraphInitialize(self.auGraph),
  133.                "Couldn't initialize AUGraph");
  134.  
  135.     // configure file player
  136.     CFURLRef audioFileURL = CFBridgingRetain(
  137.                                              [[NSBundle mainBundle] URLForResource:@"rampAudio"
  138.                                                                      withExtension:@"m4a"]);
  139.     NSLog (@"found URL %@", audioFileURL);
  140.     AudioFileID audioFile;
  141.     CheckError(AudioFileOpenURL(audioFileURL,
  142.                                 kAudioFileReadPermission,
  143.                                 kAudioFileCAFType,
  144.                                 &audioFile),
  145.                "Couldn't open audio file");
  146.    
  147.     AudioStreamBasicDescription fileStreamFormat;
  148.     UInt32 propsize = sizeof (fileStreamFormat);
  149.     CheckError(AudioFileGetProperty(audioFile,
  150.                                     kAudioFilePropertyDataFormat,
  151.                                     &propertySize,
  152.                                     &fileStreamFormat),
  153.                "couldn't get input file's stream format");
  154.    
  155.     CheckError(AudioUnitSetProperty(self.filePlayerUnit,
  156.                                     kAudioUnitProperty_ScheduledFileIDs,
  157.                                     kAudioUnitScope_Global,
  158.                                     0,
  159.                                     &audioFile,
  160.                                     sizeof(audioFile)),
  161.                "AudioUnitSetProperty[kAudioUnitProperty_ScheduledFileIDs] failed");
  162.    
  163.     UInt64 nPackets;
  164.     propsize = sizeof(nPackets);
  165.     CheckError(AudioFileGetProperty(audioFile,
  166.                                     kAudioFilePropertyAudioDataPacketCount,
  167.                                     &propsize,
  168.                                     &nPackets),
  169.                "AudioFileGetProperty[kAudioFilePropertyAudioDataPacketCount] failed");
  170.    
  171.     // tell the file player AU to play the entire file
  172.     ScheduledAudioFileRegion rgn;
  173.     memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
  174.     rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
  175.     rgn.mTimeStamp.mSampleTime = 0;
  176.     rgn.mCompletionProc = NULL;
  177.     rgn.mCompletionProcUserData = NULL;
  178.     rgn.mAudioFile = audioFile;
  179.     rgn.mLoopCount = 100;
  180.     rgn.mStartFrame = 0;
  181.     rgn.mFramesToPlay = nPackets * fileStreamFormat.mFramesPerPacket;
  182.    
  183.     CheckError(AudioUnitSetProperty(self.filePlayerUnit,
  184.                                     kAudioUnitProperty_ScheduledFileRegion,
  185.                                     kAudioUnitScope_Global,
  186.                                     0,
  187.                                     &rgn,
  188.                                     sizeof(rgn)),
  189.                "AudioUnitSetProperty[kAudioUnitProperty_ScheduledFileRegion] failed");
  190.    
  191.     // prime the file player AU with default values
  192.     UInt32 defaultVal = 0;
  193.     CheckError(AudioUnitSetProperty(self.filePlayerUnit,
  194.                                     kAudioUnitProperty_ScheduledFilePrime,
  195.                                     kAudioUnitScope_Global,
  196.                                     0,
  197.                                     &defaultVal,
  198.                                     sizeof(defaultVal)),
  199.                "AudioUnitSetProperty[kAudioUnitProperty_ScheduledFilePrime] failed");
  200.    
  201.     // tell the file player AU when to start playing (-1 sample time means next render cycle)
  202.     AudioTimeStamp startTime;
  203.     memset (&startTime, 0, sizeof(startTime));
  204.     startTime.mFlags = kAudioTimeStampSampleTimeValid;
  205.     startTime.mSampleTime = -1;
  206.     CheckError(AudioUnitSetProperty(self.filePlayerUnit,
  207.                                     kAudioUnitProperty_ScheduleStartTimeStamp,
  208.                                     kAudioUnitScope_Global,
  209.                                     0,
  210.                                     &startTime,
  211.                                     sizeof(startTime)),
  212.                "AudioUnitSetProperty[kAudioUnitProperty_ScheduleStartTimeStamp]");
  213.  
  214.     CAShow(self.auGraph);
  215.    
  216.     CheckError(AUGraphStart(self.auGraph),
  217.                "Couldn't start AUGraph");
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement