2016年6月7日 星期二

AudioKit for Swift

since: 2016/06/07
update: 2016/06/07
reference:
1. AudioKit - Powerful audio synthesis, processing, and analysis

A. 於專案中, 加入 AudioKit.framework 

    1. 接續上個專案: OSC for Swift
        到 AudioKit 下載 AudioKit-iOS-3.1.zip, 解壓縮後,  將 AudioKit.framework 拷貝到
        專案目錄裡(可以先建立 Frameworks 資料夾) 

    2. 在專案下新增 Frameworks 群組, 按下 Embedded Binaries 下的 "+"

    3. 瀏覽  AudioKit.framework 的檔案位置 > Open

    4. 點選 Create folder references > Finish

    5. 結果如下:

-----------------------------------------------------------------------------------------------

B. 專案加入 AudioKit 功能
    1. 修改 ViewController.swift 如下:
//
//  ViewController.swift
//  Hello_OSC
//
//  Created by LanliChen on 2016/6/7.
//  Copyright © 2016年 Lanli. All rights reserved.
//

import UIKit

//@add for AudioKit ############

import AudioKit

//class ViewController: UIViewController {
//@update for F53OSC ############
class ViewController: UIViewController, F53OSCPacketDestination {

    //@add for F53OSC ############
    var oscServer: F53OSCServer!
    var myAddressPattern = ""
    var myArguments = [AnyObject]()
   
    //@add for AudioKit ############

    var oscillator = AKOscillator()
   
    // @add for AudioKit ############
    func toggleSound() {
        if oscillator.isPlaying {
            oscillator.stop()
        } else {
            oscillator.amplitude = random(0.5, 1)
            oscillator.frequency = random(220, 880)
            oscillator.start()
        }
    }
   
    //@add for F53OSC ############
    func takeMessage(message: F53OSCMessage!) {
       
        print("Received '\(message)'")

       
        //@add for AudioKit ############
        toggleSound()
       
        //        let addressPattern = message.addressPattern
        //        let arguments = message.arguments
        myAddressPattern = message.addressPattern
        myArguments = message.arguments
        print("myAddressPattern = " + myAddressPattern)
        print(myArguments) // print(myArguments[0])
    }

   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        //@add for F53OSC ############
        print("Starting OSC Server")
        oscServer = F53OSCServer.init()
        oscServer.port = 3030
        oscServer.delegate = self
        if oscServer.startListening() {
            print("Listening for messages on port \(oscServer.port)")
        } else {
            print("Error: Server was unable to start listening on port \(oscServer.port)")
        }

       
        //@add for AudioKit ############
        AudioKit.output = oscillator
        AudioKit.start()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。