42d1c74663
apple / swift (push) Successful in 1m5s
docker / build-push (ci, ci/rust-ci.Dockerfile, punktfunk-rust-ci) (push) Has been cancelled
docker / build-push (docs-site, docs-site/Dockerfile, punktfunk-docs) (push) Has been cancelled
docker / build-push (--build-arg FEDORA_VERSION=44, ci, ci/fedora-rpm.Dockerfile, punktfunk-fedora44-rpm) (push) Has been cancelled
android / android (push) Has been cancelled
apple / screenshots (push) Has been cancelled
ci / web (push) Has been cancelled
ci / docs-site (push) Has been cancelled
ci / bench (push) Has been cancelled
ci / rust (push) Has been cancelled
deb / build-publish (push) Has been cancelled
decky / build-publish (push) Has been cancelled
docker / build-push (., web/Dockerfile, punktfunk-web) (push) Has been cancelled
docker / build-push (ci, ci/fedora-rpm.Dockerfile, punktfunk-fedora-rpm) (push) Has been cancelled
docker / deploy-docs (push) Has been cancelled
rpm / build-publish (fedora-44, punktfunk-fedora44-rpm) (push) Has been cancelled
rpm / build-publish (bazzite, punktfunk-fedora-rpm) (push) Has been cancelled
release / apple (push) Has been cancelled
The mic uplink handed the host pure digital silence on a multi-channel interface: AVAudioConverter's N→stereo downmix takes channels 0/1, but a pro interface puts the mic on ONE higher discrete channel. Fold the input to a mono bus ourselves instead — pick the mic's channel (or sum all) and resample that to the encoder's 48 kHz stereo, so the silent 0/1 downmix never happens. - New "Microphone channel" setting (macOS): Auto (sum every channel — a lone hot mic passes at full level) or pin 1-based channel N. Picker appears only for multi-channel devices, driven by the device's input channel count. - Diagnostics that make this class of failure self-naming next session: log the actual live capture device + format + fold mode, warn on a silent UID fallback, and a one-shot silence tripwire on the EXTRACTED signal (WARN on 10 s of zeros, else peak dBFS). - foldToMono extracted as a pure, unit-tested helper (pin / sum-clamp x interleaved / deinterleaved / mono / out-of-range). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
133 lines
5.5 KiB
Swift
133 lines
5.5 KiB
Swift
// CoreAudio HAL device enumeration for the Settings pickers. Devices are persisted by
|
|
// UID (stable across reboots/replugs — AudioDeviceIDs are not); the empty UID means
|
|
// "system default", which additionally tracks default-device changes because we then
|
|
// never pin the engine to a concrete device.
|
|
|
|
#if os(macOS)
|
|
import CoreAudio
|
|
import Foundation
|
|
|
|
public struct AudioDevice: Hashable, Identifiable, Sendable {
|
|
public let uid: String
|
|
public let name: String
|
|
public var id: String { uid }
|
|
}
|
|
|
|
public enum AudioDevices {
|
|
/// Output-capable devices (speakers, headphones, multi-output…).
|
|
public static func outputs() -> [AudioDevice] {
|
|
all().filter { hasStreams($0, scope: kAudioObjectPropertyScopeOutput) }
|
|
.compactMap(describe)
|
|
}
|
|
|
|
/// Input-capable devices (microphones, interfaces…).
|
|
public static func inputs() -> [AudioDevice] {
|
|
all().filter { hasStreams($0, scope: kAudioObjectPropertyScopeInput) }
|
|
.compactMap(describe)
|
|
}
|
|
|
|
/// Resolve a persisted UID to the current AudioDeviceID — nil when unplugged.
|
|
static func deviceID(forUID uid: String) -> AudioDeviceID? {
|
|
all().first { id in
|
|
stringProperty(id, kAudioDevicePropertyDeviceUID) == uid
|
|
}
|
|
}
|
|
|
|
/// Input channel count of the mic the picker would use — the device with this UID, or the
|
|
/// system default input when `uid` is empty. 0 when it can't be resolved. Drives the
|
|
/// "Microphone channel" picker (only shown for multi-channel interfaces).
|
|
public static func inputChannelCount(forUID uid: String) -> Int {
|
|
let id = uid.isEmpty ? defaultInputDevice() : deviceID(forUID: uid)
|
|
guard let id else { return 0 }
|
|
return channelCount(id, scope: kAudioObjectPropertyScopeInput)
|
|
}
|
|
|
|
private static func defaultInputDevice() -> AudioDeviceID? {
|
|
var address = AudioObjectPropertyAddress(
|
|
mSelector: kAudioHardwarePropertyDefaultInputDevice,
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
mElement: kAudioObjectPropertyElementMain)
|
|
var dev = AudioDeviceID(0)
|
|
var size = UInt32(MemoryLayout<AudioDeviceID>.size)
|
|
guard AudioObjectGetPropertyData(
|
|
AudioObjectID(kAudioObjectSystemObject), &address, 0, nil, &size, &dev) == noErr,
|
|
dev != 0
|
|
else { return nil }
|
|
return dev
|
|
}
|
|
|
|
/// Sum of channels across the device's streams in `scope` (its total input/output channels).
|
|
private static func channelCount(
|
|
_ id: AudioDeviceID, scope: AudioObjectPropertyScope
|
|
) -> Int {
|
|
var address = AudioObjectPropertyAddress(
|
|
mSelector: kAudioDevicePropertyStreamConfiguration,
|
|
mScope: scope,
|
|
mElement: kAudioObjectPropertyElementMain)
|
|
var size: UInt32 = 0
|
|
guard AudioObjectGetPropertyDataSize(id, &address, 0, nil, &size) == noErr, size > 0
|
|
else { return 0 }
|
|
let raw = UnsafeMutableRawPointer.allocate(
|
|
byteCount: Int(size), alignment: MemoryLayout<AudioBufferList>.alignment)
|
|
defer { raw.deallocate() }
|
|
guard AudioObjectGetPropertyData(id, &address, 0, nil, &size, raw) == noErr else { return 0 }
|
|
let abl = UnsafeMutableAudioBufferListPointer(
|
|
raw.assumingMemoryBound(to: AudioBufferList.self))
|
|
return abl.reduce(0) { $0 + Int($1.mNumberChannels) }
|
|
}
|
|
|
|
private static func all() -> [AudioDeviceID] {
|
|
var address = AudioObjectPropertyAddress(
|
|
mSelector: kAudioHardwarePropertyDevices,
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
mElement: kAudioObjectPropertyElementMain)
|
|
var size: UInt32 = 0
|
|
guard AudioObjectGetPropertyDataSize(
|
|
AudioObjectID(kAudioObjectSystemObject), &address, 0, nil, &size) == noErr,
|
|
size > 0
|
|
else { return [] }
|
|
var ids = [AudioDeviceID](
|
|
repeating: 0, count: Int(size) / MemoryLayout<AudioDeviceID>.size)
|
|
guard AudioObjectGetPropertyData(
|
|
AudioObjectID(kAudioObjectSystemObject), &address, 0, nil, &size, &ids) == noErr
|
|
else { return [] }
|
|
return ids
|
|
}
|
|
|
|
private static func hasStreams(
|
|
_ id: AudioDeviceID, scope: AudioObjectPropertyScope
|
|
) -> Bool {
|
|
var address = AudioObjectPropertyAddress(
|
|
mSelector: kAudioDevicePropertyStreams,
|
|
mScope: scope,
|
|
mElement: kAudioObjectPropertyElementMain)
|
|
var size: UInt32 = 0
|
|
return AudioObjectGetPropertyDataSize(id, &address, 0, nil, &size) == noErr && size > 0
|
|
}
|
|
|
|
/// UID + human name for a live AudioDeviceID (nil if either property is unreadable).
|
|
static func describe(_ id: AudioDeviceID) -> AudioDevice? {
|
|
guard let uid = stringProperty(id, kAudioDevicePropertyDeviceUID),
|
|
let name = stringProperty(id, kAudioObjectPropertyName)
|
|
else { return nil }
|
|
return AudioDevice(uid: uid, name: name)
|
|
}
|
|
|
|
private static func stringProperty(
|
|
_ id: AudioDeviceID, _ selector: AudioObjectPropertySelector
|
|
) -> String? {
|
|
var address = AudioObjectPropertyAddress(
|
|
mSelector: selector,
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
mElement: kAudioObjectPropertyElementMain)
|
|
var ref: CFString?
|
|
var size = UInt32(MemoryLayout<CFString?>.size)
|
|
let status = withUnsafeMutablePointer(to: &ref) { p in
|
|
AudioObjectGetPropertyData(id, &address, 0, nil, &size, p)
|
|
}
|
|
guard status == noErr, let ref else { return nil }
|
|
return ref as String
|
|
}
|
|
}
|
|
#endif
|