Search code examples
iosswiftxcodeavaudioengine

Set left and right headphone volume using two different sliders


I am generating a wave sound for different frequencies and user should hear this wave sound using headphones only and he/she will set left and right headphone volumes using two different sliders. To achieve wave sound I wrote below code which works perfect. But problem is: From last 5 days I am trying to set volume for left and right headphones separately, but no luck.

class Synth {

// MARK: Properties
public static let shared = Synth()

public var volume: Float {
    set {
        audioEngine.mainMixerNode.outputVolume = newValue
    }
    get {
        audioEngine.mainMixerNode.outputVolume
    }
}

public var frequencyRampValue: Float = 0

public var frequency: Float = 440 {
    didSet {
        if oldValue != 0 {
            frequencyRampValue = frequency - oldValue
        } else {
            frequencyRampValue = 0
        }
    }
}

private var audioEngine: AVAudioEngine

private lazy var sourceNode = AVAudioSourceNode { _, _, frameCount, audioBufferList in
    let ablPointer = UnsafeMutableAudioBufferListPointer(audioBufferList)
            
    let localRampValue = self.frequencyRampValue
    let localFrequency = self.frequency - localRampValue
    
    let period = 1 / localFrequency

    for frame in 0..<Int(frameCount) {
        let percentComplete = self.time / period
        let sampleVal = self.signal(localFrequency + localRampValue * percentComplete, self.time)
        self.time += self.deltaTime
        self.time = fmod(self.time, period)
        
        for buffer in ablPointer {
            let buf: UnsafeMutableBufferPointer<Float> = UnsafeMutableBufferPointer(buffer)
            buf[frame] = sampleVal
        }
    }
    
    self.frequencyRampValue = 0
    
    return noErr
}

private var time: Float = 0
private let sampleRate: Double
private let deltaTime: Float

private var signal: Signal

// MARK: Init

init(signal: @escaping Signal = Oscillator.square) {
    
    audioEngine = AVAudioEngine()
    let mainMixer = audioEngine.mainMixerNode
    let outputNode = audioEngine.outputNode
    let format = outputNode.inputFormat(forBus: 0)
    
    sampleRate = format.sampleRate
    deltaTime = 1 / Float(sampleRate)
    
    self.signal = signal
    
    let inputFormat = AVAudioFormat(commonFormat: format.commonFormat,
                                    sampleRate: format.sampleRate,
                                    channels: 1,
                                    interleaved: format.isInterleaved)
    
    audioEngine.attach(sourceNode)
    audioEngine.connect(sourceNode, to: mainMixer, format: inputFormat)
    audioEngine.connect(mainMixer, to: outputNode, format: nil)
    mainMixer.outputVolume = 0
    audioEngine.mainMixerNode.pan = 100 // this does not work, 
    //audioEngine.mainMixerNode.pan = 1.0 // this also does not work
    do {
        try audioEngine.start()
    } catch {
        print("Could not start engine: \(error.localizedDescription)")
    }
    
}

//This function will be called in view controller to generate sound
public func setWaveformTo(_ signal: @escaping Signal) {
    self.signal = signal
}

}

With the above code I can hear the wave sound as normal in left and right headphone. I tried to use audioEngine.mainMixerNode.pan for value 100 and -100 also -1.0 and 1.0 but this did not make any change.


Solution

  • I tried to use audioEngine.mainMixerNode.pan for value 100 and -100 but this did not make any change.

    The allowable range for the pan value is {-1.0, 1.0}. The values that you say you used are outside that range, so it's not surprising that they had no effect. Try 0.75 or -0.75 instead.