Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void) setUpAUGraph {
- if (self.auGraph) {
- CheckError(AUGraphClose(self.auGraph),
- "Couldn't close old AUGraph");
- CheckError (DisposeAUGraph(self.auGraph),
- "Couldn't dispose old AUGraph");
- }
- CheckError(NewAUGraph(&_auGraph),
- "Couldn't create new AUGraph");
- CheckError(AUGraphOpen(self.auGraph),
- "Couldn't open AUGraph");
- // start with file player unit
- AudioComponentDescription fileplayercd = {0};
- fileplayercd.componentType = kAudioUnitType_Generator;
- fileplayercd.componentSubType = kAudioUnitSubType_AudioFilePlayer;
- fileplayercd.componentManufacturer = kAudioUnitManufacturer_Apple;
- AUNode filePlayerNode;
- CheckError(AUGraphAddNode(self.auGraph,
- &fileplayercd,
- &filePlayerNode),
- "Couldn't add file player node");
- // get the actual unit
- CheckError(AUGraphNodeInfo(self.auGraph,
- filePlayerNode,
- NULL,
- &_filePlayerUnit),
- "couldn't get file player node");
- // remote io unit
- AudioComponentDescription outputcd = {0};
- outputcd.componentType = kAudioUnitType_Output;
- outputcd.componentSubType = kAudioUnitSubType_RemoteIO;
- outputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
- AUNode ioNode;
- CheckError(AUGraphAddNode(self.auGraph,
- &outputcd,
- &ioNode),
- "couldn't add remote io node");
- // get the remote io unit from the node
- CheckError(AUGraphNodeInfo(self.auGraph,
- ioNode,
- NULL,
- &_ioUnit),
- "couldn't get remote io unit");
- // effect unit here
- AudioComponentDescription effectcd = {0};
- effectcd.componentType = kAudioUnitType_FormatConverter;
- // effectcd.componentSubType = kAudioUnitSubType_NewTimePitch;
- effectcd.componentSubType = kAudioUnitSubType_Varispeed;
- effectcd.componentManufacturer = kAudioUnitManufacturer_Apple;
- AUNode effectNode;
- CheckError(AUGraphAddNode(self.auGraph,
- &effectcd,
- &effectNode),
- "couldn't get effect node [time/pitch]");
- // get effect unit from the node
- CheckError(AUGraphNodeInfo(self.auGraph,
- effectNode,
- NULL,
- &_effectUnit),
- "couldn't get effect unit from node");
- // enable output to the remote io unit
- UInt32 oneFlag = 1;
- UInt32 busZero = 0;
- CheckError(AudioUnitSetProperty(self.ioUnit,
- kAudioOutputUnitProperty_EnableIO,
- kAudioUnitScope_Output,
- busZero,
- &oneFlag,
- sizeof(oneFlag)),
- "Couldn't enable output on bus 0");
- // get stream format that the effect wants
- AudioStreamBasicDescription streamFormat;
- UInt32 propertySize = sizeof (streamFormat);
- CheckError(AudioUnitGetProperty(self.effectUnit,
- kAudioUnitProperty_StreamFormat,
- kAudioUnitScope_Input,
- 0,
- &streamFormat,
- &propertySize),
- "Couldn't get effect unit stream format");
- // apply effect's format elsewhere in the graph
- CheckError(AudioUnitSetProperty(self.filePlayerUnit,
- kAudioUnitProperty_StreamFormat,
- kAudioUnitScope_Output,
- busZero,
- &streamFormat,
- sizeof(streamFormat)),
- "couldn't set stream format on file player bus 0 output");
- CheckError(AudioUnitSetProperty(self.ioUnit,
- kAudioUnitProperty_StreamFormat,
- kAudioUnitScope_Input,
- busZero,
- &streamFormat,
- sizeof(streamFormat)),
- "couldn't set stream format on iounit bus 0 input");
- // make connections
- CheckError(AUGraphConnectNodeInput(self.auGraph,
- filePlayerNode,
- 0,
- effectNode,
- 0),
- "couldn't connect file player bus 0 output to effect bus 0 input");
- CheckError(AUGraphConnectNodeInput(self.auGraph,
- effectNode,
- 0,
- ioNode,
- 0),
- "couldn't connect effect bus 0 output to remoteio bus 0 input");
- CheckError(AUGraphInitialize(self.auGraph),
- "Couldn't initialize AUGraph");
- // configure file player
- CFURLRef audioFileURL = CFBridgingRetain(
- [[NSBundle mainBundle] URLForResource:@"rampAudio"
- withExtension:@"m4a"]);
- NSLog (@"found URL %@", audioFileURL);
- AudioFileID audioFile;
- CheckError(AudioFileOpenURL(audioFileURL,
- kAudioFileReadPermission,
- kAudioFileCAFType,
- &audioFile),
- "Couldn't open audio file");
- AudioStreamBasicDescription fileStreamFormat;
- UInt32 propsize = sizeof (fileStreamFormat);
- CheckError(AudioFileGetProperty(audioFile,
- kAudioFilePropertyDataFormat,
- &propertySize,
- &fileStreamFormat),
- "couldn't get input file's stream format");
- CheckError(AudioUnitSetProperty(self.filePlayerUnit,
- kAudioUnitProperty_ScheduledFileIDs,
- kAudioUnitScope_Global,
- 0,
- &audioFile,
- sizeof(audioFile)),
- "AudioUnitSetProperty[kAudioUnitProperty_ScheduledFileIDs] failed");
- UInt64 nPackets;
- propsize = sizeof(nPackets);
- CheckError(AudioFileGetProperty(audioFile,
- kAudioFilePropertyAudioDataPacketCount,
- &propsize,
- &nPackets),
- "AudioFileGetProperty[kAudioFilePropertyAudioDataPacketCount] failed");
- // tell the file player AU to play the entire file
- ScheduledAudioFileRegion rgn;
- memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
- rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
- rgn.mTimeStamp.mSampleTime = 0;
- rgn.mCompletionProc = NULL;
- rgn.mCompletionProcUserData = NULL;
- rgn.mAudioFile = audioFile;
- rgn.mLoopCount = 100;
- rgn.mStartFrame = 0;
- rgn.mFramesToPlay = nPackets * fileStreamFormat.mFramesPerPacket;
- CheckError(AudioUnitSetProperty(self.filePlayerUnit,
- kAudioUnitProperty_ScheduledFileRegion,
- kAudioUnitScope_Global,
- 0,
- &rgn,
- sizeof(rgn)),
- "AudioUnitSetProperty[kAudioUnitProperty_ScheduledFileRegion] failed");
- // prime the file player AU with default values
- UInt32 defaultVal = 0;
- CheckError(AudioUnitSetProperty(self.filePlayerUnit,
- kAudioUnitProperty_ScheduledFilePrime,
- kAudioUnitScope_Global,
- 0,
- &defaultVal,
- sizeof(defaultVal)),
- "AudioUnitSetProperty[kAudioUnitProperty_ScheduledFilePrime] failed");
- // tell the file player AU when to start playing (-1 sample time means next render cycle)
- AudioTimeStamp startTime;
- memset (&startTime, 0, sizeof(startTime));
- startTime.mFlags = kAudioTimeStampSampleTimeValid;
- startTime.mSampleTime = -1;
- CheckError(AudioUnitSetProperty(self.filePlayerUnit,
- kAudioUnitProperty_ScheduleStartTimeStamp,
- kAudioUnitScope_Global,
- 0,
- &startTime,
- sizeof(startTime)),
- "AudioUnitSetProperty[kAudioUnitProperty_ScheduleStartTimeStamp]");
- CAShow(self.auGraph);
- CheckError(AUGraphStart(self.auGraph),
- "Couldn't start AUGraph");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement