Last active
January 25, 2017 15:50
-
-
Save damonjones/fd586dccaac972a60c0f to your computer and use it in GitHub Desktop.
Pitcher Shifter test (Swift)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// PitcherShifterSwift | |
// | |
// Created by Damon Jones on 11/9/14. | |
// Copyright (c) 2014 Damon Jones. All rights reserved. | |
// | |
import UIKit | |
import AVFoundation | |
class ViewController: UIViewController { | |
let engine: AVAudioEngine = AVAudioEngine() | |
let player: AVAudioPlayerNode = AVAudioPlayerNode() | |
let file: AVAudioFile! = nil | |
let pitchEffect: AVAudioUnitTimePitch = AVAudioUnitTimePitch() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
var error: NSError? = nil | |
// Get the URL of the file | |
let url: NSURL? = NSBundle.mainBundle().URLForResource("Audio File", withExtension: "m4a") | |
if nil == url { | |
println("Invalid file URL") | |
abort() | |
} | |
// Create the file object for reading | |
let file = AVAudioFile(forReading: url, error: &error) | |
if nil != error { | |
println(error) | |
abort() | |
} | |
// Set the pitch-shift amount (100 cents = 1 semitone) | |
pitchEffect.pitch = 100.0 // +1 semitone | |
pitchEffect.overlap = 12.0 // more overlapping windows | |
// Add nodes to engine | |
engine.attachNode(player) | |
engine.attachNode(pitchEffect) | |
// Connect the player's output to the effect's input | |
engine.connect(player, to: pitchEffect, format: file.processingFormat) | |
// Connect the effect's output to the main mixer's input | |
engine.connect(pitchEffect, to: engine.mainMixerNode, format: file.processingFormat) | |
// Schedule the file and start the engine | |
player.scheduleFile(file, atTime: nil, completionHandler: nil) | |
engine.startAndReturnError(&error) | |
if nil != error { | |
println(error) | |
abort(); | |
} | |
// Play the file | |
player.play() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment