rnine/SimplyCoreAudio

Cannot get output balance of MacBook Pro M1 Speakers

jamsinclair opened this issue · 3 comments

Problem

I am unable to retrieve the output balance for the builtin speakers on my M1 MacBook Pro.

Other devices, like external headphones, are able to retrieve the balance with no issues.

Specs:

  • MacOS 12.0.1 (Monterey)
  • MacBook Pro (14-inch, 2021), Apple M1 Pro chip
  • Xcode 13.2.1

Code in Use:

import SimplyCoreAudio

let simplyCA = SimplyCoreAudio()
let device = simplyCA.defaultOutputDevice

// This works fine for both builtin and external audio devices
if let outVolume = device?.virtualMainVolume(scope: .output) {
    os_log("Current default device volume of: %F", outVolume)
}

// This works only for external audio devices, builtin speakers do not work
if let outBalance = device?.virtualMainBalance(scope: .output) {
    os_log("Current default device balance of: %F", outBalance)
}

Notes

Feels like this might be an underlying apple issue or I am just doing something stupid 😅

Also so much ❤️ for this library. The Audio APIs are a real developer pain to work with. This will help simplify a bunch of code in my app.

I've copied some of the source code and trying it out in my app, looks like maybe it's throwing kCMIOHardwareUnknownPropertyError.

Does this mean newer macbooks don't expose the balance property for their speakers?

This is likely terrible code, let me know if there's a better approach to debug!

var masterBalanceAddress = AudioObjectPropertyAddress(mSelector: kAudioHardwareServiceDeviceProperty_VirtualMainBalance,
                                                      mScope: kAudioDevicePropertyScopeOutput,
                                                      mElement: kAudioObjectPropertyElementMain)

var deviceId: AudioObjectID = <put device id here>        
var balanceValue: Float32 = 0
var size: UInt32 = UInt32(MemoryLayout<Float32>.size)

let result: OSStatus = AudioObjectGetPropertyData(deviceID, &masterBalanceAddress, UInt32(0), nil, &size, &balanceValue)

// Outputs status of 2003332927 which is `kCMIOHardwareUnknownPropertyError`
rnine commented

@jamsinclair I don't have access to an M1 Mac right now, but it sounds like the built-in speakers output on your M1 Mac may not support setting this property. One way to confirm this is by opening Audio MIDI Setup (included with macOS) and check if the UI control for Balance is enabled for this device:

Screenshot 2022-01-16 at 12 57 35

@rnine Thanks! There's definitely enabled options to configure the balance for the system speakers in both the "Sound Preferences" and "Audio Midi Setup" GUIs.

I would be happy to help debug further from my machine. Any ideas of CoreAudio properties or options I can play around with to get/set the balance?