2

I want to build an Oscillator with mode-switch between square and triangle waveform, using AKOperation.squareWave() and AKOperation.triangleWave(). When I try to build it like the following, it does not work. Whats wrong? 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()

On startup OSC is running its square wave well, but when I touch the toggle switch silence appears. Toggling back brings back the square.

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
headkit
  • 3,307
  • 4
  • 53
  • 99
  • I can't see the problem just by looking at the code. Could you post a complete project showing this behavior? Its hard to debug without running it and there's a lot to wire up to test it properly. – Aurelius Prochazka Mar 18 '18 at 21:49
  • ok. but is it in principle possible to switch between AKOperationGenerators while running (see last code blocks)? – headkit Mar 19 '18 at 13:55
  • hey @AureliusProchazka, I edited the codeblock, made it an working PlayGround – headkit Mar 19 '18 at 18:05

1 Answers1

0

Seems like it is not possible that way. I now have running the different oscillators in parallel, starting/stopping the ones I need when I need them running/stopped like

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

etc.

headkit
  • 3,307
  • 4
  • 53
  • 99