audiokit: playing two oscillators simultaneously
Asked Answered
E

1

6

Hello I'm working with AudioKit -- it is a stellar framework and I'm very happy so far just learning it. I'm working through a HelloWorld example and there is code for a UI button that engages an oscillator at a frequency...

My question is: if I want to play two oscillator tones at once, such as 432Hz and a perfect fifth above (ratio of 3:2 so 648Hz) how can I have them both play simultaneously? Is the correct design pattern to have a new node for each "tone" coming along?

class ViewController: UIViewController {

    var oscillator = AKOscillator()
    var osc2 = AKOscillator()

    override func viewDidLoad() {
        super.viewDidLoad()

        AudioKit.output = oscillator
        AudioKit.start()
    }

    @IBAction func toggleSound(sender: UIButton) {
        if oscillator.isPlaying {
            oscillator.stop()
            sender.setTitle("Play Sine Wave", forState: .Normal)
        } else {
          oscillator.amplitude = 1 //was:: random(0.5, 1)
          oscillator.frequency = 432 //was:: random(220, 880)
          osc2.amplitude = 1
          osc2.frequency = 648 //3:2 from 432Hz
            sender.setTitle("Stop Sine Wave at \(Int(oscillator.frequency))Hz", forState: .Normal)
        }
        sender.setNeedsDisplay()
    }

}

How can I chain the two oscillators together so they can sing together?

Encourage answered 15/4, 2016 at 18:8 Comment(2)
What does AudioKit.output need? I don't know the library but I would assume it would be AudioKit.output = oscillator + osc2?Fils
@yun.cloud it takes an AKNode (audioKit Node) so there must be a method that combines them or can chain them... thanks for your suggestion, Xcode is talking back :)Encourage
A
11

Try the AKMixer node:

var mixer = new AKMixer(oscillator, osc2)
AudioKit.output = mixer
try! AudioKit.start()
Acrid answered 15/4, 2016 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.