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?

mahal tertin
  • 3,204
  • 21
  • 40
sova
  • 4,792
  • 10
  • 38
  • 47
  • What does `AudioKit.output` need? I don't know the library but I would assume it would be `AudioKit.output = oscillator + osc2`? – yun Apr 15 '16 at 18:25
  • @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 :) – sova Apr 15 '16 at 23:21

1 Answers1

12

Try the AKMixer node:

var mixer = new AKMixer(oscillator, osc2)
AudioKit.output = mixer
try! AudioKit.start()
Aurelius Prochazka
  • 4,100
  • 1
  • 9
  • 34
jaket
  • 8,472
  • 2
  • 20
  • 41