Skip to content

Instantly share code, notes, and snippets.

@damonjones
Last active August 29, 2015 14:09
Show Gist options
  • Save damonjones/0fa261f1034c2f527391 to your computer and use it in GitHub Desktop.
Save damonjones/0fa261f1034c2f527391 to your computer and use it in GitHub Desktop.
Pitch Shifter test
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSError *error = nil;
// Get the URL of the file
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Audio File" withExtension:@"m4a"];
if (!url) {
NSLog(@"Invalid file URL");
abort();
}
// Create the file object for reading
self.file = [[AVAudioFile alloc] initForReading:url error:&error];
if (error) {
NSLog(@"%@", error);
abort();
}
// Create engine and player and effect nodes
self.engine = [[AVAudioEngine alloc] init];
self.player = [[AVAudioPlayerNode alloc] init];
self.pitchEffect = [[AVAudioUnitTimePitch alloc] init];
// Set the pitch-shift amount (100 cents = 1 semitone)
self.pitchEffect.pitch = 100.0; // +1 semitone
self.pitchEffect.overlap = 16.0; // more overlapping windows
// Add nodes to engine
[self.engine attachNode:self.player];
[self.engine attachNode:self.pitchEffect];
// Connect the player's output to the effect's input
[self.engine connect:self.player to:self.pitchEffect format:self.file.processingFormat];
// Connect the effect's output to the main mixer's input
[self.engine connect:self.pitchEffect to:[self.engine mainMixerNode] format:self.file.processingFormat];
// Schedule the file and start the engine
[self.player scheduleFile:self.file atTime:nil completionHandler:nil];
[self.engine startAndReturnError:&error];
if (error) {
NSLog(@"%@", error);
abort();
}
// Play the file
[self.player play];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment