Search code examples
swiftxcodeswift4avaudioplayeraudiokit

Playing a tone with specific frequency in either left or right headphone


I would like to know if there is any way that one can generate an audio tone of a specific frequency and play it through plugged in headphones on either the right or left side of the headphones. Is there any possible was to do this with code or a module such as AudioKit.


Solution

  • Yes, just use the AKOscillator node:

    https://audiokit.io/docs/Classes/AKOscillator.html

    to create the frequency:

    var osc = AKOscillator()
    osc.frequency = 440 // or whatever
    

    and then use an AKPanner node:

    https://audiokit.io/docs/Classes/AKPanner.html

    and

    https://audiokit.io/playgrounds/Basics/Stereo%20Panning/

    And set the pan to -1 for left and 1 for right:

    var panner = AKPanner(osc)
    panner.pan = -1 // or 1