Skip to content

Instantly share code, notes, and snippets.

@arielelkin
Last active December 17, 2015 11:18
Show Gist options
  • Save arielelkin/5600812 to your computer and use it in GitHub Desktop.
Save arielelkin/5600812 to your computer and use it in GitHub Desktop.
AAE+STK Gists
//NOTE:
//THIS GIST IS PART OF A LARGER iOS MUSIC APP DEVELOPMENT TUTORIAL, WHICH I'LL POST SHORTLY.
//Follow http://twitter.com/arivocals for updates
/***************************
DOWNLOAD LINKS FOR LIBRARIES:
STK:
http://momu.stanford.edu/stk/release/momu-stk-1.0.0.tgz
AAE:
git clone --depth=1 https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine.git
https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine/archive/master.zip
AAE Setup Instructions:
http://theamazingaudioengine.com/doc/_getting-_started.html
***************************/
//LISTING 1
//Setting up AEAudioController
self.audioController = [[AEAudioController alloc]
initWithAudioDescription:[AEAudioController nonInterleavedFloatStereoAudioDescription]
inputEnabled:NO
];
NSError *errorAudioSetup = NULL;
BOOL result = [_audioController start:&errorAudioSetup];
if ( !result ) {
NSLog(@"Error starting audio engine: %@", errorAudioSetup.localizedDescription);
}
//LISTING 2
//Setting up the STK's Mandolin within an AEBlockChannel
self.myMandolin = new stk::Mandolin(400);
self.myMandolin->setFrequency(400);
self.myMandolinChannel = [AEBlockChannel channelWithBlock:^(const AudioTimeStamp *time,
UInt32 frames,
AudioBufferList *audio) {
for ( int i=0; i<frames; i++ ) {
((float*)audio->mBuffers[0].mData)[i] =
((float*)audio->mBuffers[1].mData)[i] = self.myMandolin->tick();
}
}];
[self.audioController addChannels:@[self.myMandolinChannel]];
//LISTING 3
//Adding an STK Effect within an AEBlockFilter
self.myChorus = new stk::Chorus(500);
self.myChorus->setModFrequency(500);
self.myChorus->setEffectMix(1);
self.myFilter = [AEBlockFilter filterWithBlock:^(AEAudioControllerFilterProducer producer, void *producerToken, const AudioTimeStamp *time, UInt32 frames, AudioBufferList *audio) {
OSStatus status = producer(producerToken, audio, &frames);
if ( status != noErr ) return;
for (int i = 0; i<frames; i++) {
((float*)audio->mBuffers[0].mData)[i] = self.myChorus->tick(((float*)audio->mBuffers[0].mData)[i]);
((float*)audio->mBuffers[1].mData)[i] = self.myChorus->tick(((float*)audio->mBuffers[1].mData)[i]);
}
}];
[self.audioController addFilter:self.myFilter toChannel:self.myMandolinChannel];
//LISTING 4
//Adding a reverb Audio Unit
NSError *errorReverbSetup = NULL;
self.myReverb = [[AEAudioUnitFilter alloc] initWithComponentDescription:AEAudioComponentDescriptionMake(kAudioUnitManufacturer_Apple, kAudioUnitType_Effect, kAudioUnitSubType_Reverb2) audioController:self.audioController error:&errorReverbSetup];
if (errorReverbSetup) {
NSLog(@"Error setting up reverb: %@", errorReverbSetup.localizedDescription);
}
[self.audioController addFilter:self.myReverb toChannel:self.myMandolinChannel];
//LISTING 5
//Change one of the parameters of the reverb:
-(IBAction)sliderMoved:(UISlider *)sender{
AudioUnitSetParameter(self.myReverb.audioUnit, kReverb2Param_DryWetMix, kAudioUnitScope_Global, 0, sender.value, 0);
NSLog(@"dry wet set to %f", sender.value);
}
//LISTING 6
//checkResult code for functions that return OSStatus
#define checkResult(result,operation) (_checkResult((result),(operation),strrchr(__FILE__, '/')+1,__LINE__))
static inline BOOL _checkResult(OSStatus result, const char *operation, const char* file, int line) {
if ( result != noErr ) {
NSLog(@"%s:%d: %s result %d %08X %4.4s\n", file, line, operation, (int)result, (int)result, (char*)&result);
return NO;
}
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment