AudioKit:如何在两个不同的AKOperationGenerator-Oscillators之间切换

时间:2018-03-18 18:07:49

标签: ios swift audiokit akoperationgenerator

我想使用AKOperation.squareWave()和AKOperation.triangleWave()在正方形和三角形波形之间建立模式切换的振荡器。当我尝试像下面这样构建它时,它不起作用。怎么了?日Thnx!

import AudioKitPlaygrounds
import AudioKit

let osc_square = AKOperationGenerator { parameters in
    return AKOperation.squareWave(
        frequency: parameters[0],
        amplitude: parameters[1]
    )
}
let osc_tri = AKOperationGenerator { parameters in
    return AKOperation.triangleWave(
        frequency: parameters[0],
        amplitude: parameters[1]
    )
}
var currentOsc: AKOperationGenerator = osc_square
var currentMode:Int = 1

AudioKit.output = currentOsc
try AudioKit.start()

setCurrentVCOParameters()
currentOsc.play()



let playgroundWidth = 500

import AudioKitUI
class LiveView: AKLiveViewController {

    override func viewDidLoad() {
        addTitle("Switch AKOperationGenerator")
        let button = AKButton(title: "Mode \(currentMode)") { _ in
            if currentMode == 1 {
                setVCOMode(2)
            }
            else if currentMode == 2 {
                setVCOMode(1)
            }
        }
        addView(button)
    }
}

func setVCOMode(_ modeIndex: Int) {
    currentMode = modeIndex
    setCurrentVCO()
}

func setCurrentVCO() {
    currentOsc.stop()
    switch currentMode {
    case 1:
        currentOsc = osc_square
    case 2:
        currentOsc = osc_tri
    default:
        currentOsc = osc_square
    }
    setCurrentVCOParameters()
    currentOsc.start()
}
func setCurrentVCOParameters() {
    currentOsc.parameters[0] = 110.0
    currentOsc.parameters[1] = 0.5
}

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = LiveView()

启动时OSC正好运行方波,但是当我触摸拨动开关时会出现静音。切换回来带回广场。

1 个答案:

答案 0 :(得分:0)

似乎不可能那样。我现在已经并行运行了不同的振荡器,当我需要它们运行/停止时启动/停止我需要的振荡器

var currentVCO1Mode: VCOMode = .sqr
let allVCO1Generators: [AKOperationGenerator]!
enum CurrentVCO1: Int {
    case sqr, tri
}
var currentVCO1:CurrentVCO1

func setCurrentVCO1() {
    vco1_square.stop()
    vco1_tri.stop()
    switch currentVCO1Mode {
        case .sqr:
            vco1_square.start()
            currentVCO1 = .sqr
        case .tri:
            vco1_tri.start()
            currentVCO1 = .tri
    }
    setCurrentVCO1Parameters()
    setCurrentVCO2()
}