gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:59 25 4
gpt4 key购买 nike

我想使用 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 运行良好,但当我触摸拨动开关时,出现静音。切换回来带回正方形。

最佳答案

好像这样是不行的。我现在可以并行运行不同的振荡器,在我需要它们运行/停止时启动/停止我需要的那些

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com