OStack程序员社区-中国程序员成长平台

标题: ios - AudioKit:如何在两个不同的 AKOperationGenerator-Oscillators 之间切换 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:53
标题: ios - AudioKit:如何在两个不同的 AKOperationGenerator-Oscillators 之间切换

我想使用 AKOperation.squareWave() 和 AKOperation.triangleWave() 构建一个在方波和三角波之间进行模式切换的振荡器。当我尝试如下构建它时,它不起作用。怎么了?谢谢!

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 的方波运行良好,但是当我触摸切换开关时会出现静音。切换回来会带回正方形。



Best Answer-推荐答案


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

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()
}

等等

关于ios - AudioKit:如何在两个不同的 AKOperationGenerator-Oscillators 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49351258/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4