To install the Swift Package, please follow the steps below:
Add https://github.com/rnine/SimplyCoreAudio.git as a Swift Package Manager dependency to your project.
When asked to Choose Package Options, use the default settings provided by Xcode.
When asked to Add Package, add SimplyCoreAudio to your desired target(s).
Usage
Import SimplyCoreAudio
import SimplyCoreAudio
Instantiate SimplyCoreAudio
let simplyCA = SimplyCoreAudio()
Interact with SimplyCoreAudio
// Get the default output device
let device = simplyCA.defaultOutputDevice
// Get all output devices
simplyCA.allOutputDevices
// You can also filter devices by the scope needed
simplyCA.allDevices.filter { $0.channels(scope: .output) > 0 }
// For example, list all devices that aren't aggregate ones that support input
simplyCA.allNonAggregateDevices.filter { $0.channels(scope: .input) > 0 }
// After you've chosen a device you can set it as the default.
// This will switch the selected device in the system
device.isDefaultOutputDevice = true
// Set the default input device to a new device
device.isDefaultInputDevice = true
// Get preferred output channels
if let stereoPair = device.preferredChannelsForStereo(scope: .output) {
let leftChannel = stereoPair.left
let rightChannel = stereoPair.right
// Use channels...
}
// Get device samplerate
if let sampleRate = device.nominalSampleRate {
// Use samplerate...
}
// Get device virtual main volume
if let outVolume = device.virtualMainVolume(scope: .output) {
// Use output volume...
}
Subscribe to hardware-related notifications
// e.g., subscribing to `deviceListChanged` notification.
var observer = NotificationCenter.default.addObserver(forName: .deviceListChanged,
object: nil,
queue: .main) { (notification) in
// Get added devices.
guard let addedDevices = notification.userInfo?["addedDevices"] as? [AudioDevice] else { return }
// Get removed devices.
guard let removedDevices = notification.userInfo?["removedDevices"] as? [AudioDevice] else { return }
}
// Once done observing, remove observer and nil it.
NotificationCenter.default.removeObserver(observer)
observer = nil
Subscribe to notifications from a specific audio device
// Get the default output device
let device = simplyCA.defaultOutputDevice
// e.g., subscribing to `deviceNominalSampleRateDidChange` notification.
var observer = NotificationCenter.default.addObserver(forName: .deviceNominalSampleRateDidChange,
object: device,
queue: .main) { (notification) in
// Handle notification.
}
// Once done observing, remove observer and nil it.
NotificationCenter.default.removeObserver(observer)
observer = nil
Subscribe to notifications from a specific audio stream
// Get the default output device
let device = simplyCA.defaultOutputDevice
// Get the first output stream
guard let streams = device.streams(scope: .output) else { return }
guard let stream0 = streams.first else { return }
// e.g., subscribing to `streamPhysicalFormatDidChange` notification.
var observer = NotificationCenter.default.addObserver(forName: .streamPhysicalFormatDidChange,
object: stream0,
queue: .main) { (notification) in
// Handle notification.
}
// Once done observing, remove observer and nil it.
NotificationCenter.default.removeObserver(observer)
observer = nil
Supported Notifications
Audio Hardware Notifications
Name
Purpose
User Info
defaultInputDeviceChanged
Called whenever the default input device changes.
N/A
defaultOutputDeviceChanged
Called whenever the default output device changes.
N/A
defaultSystemOutputDeviceChanged
Called whenever the default system output device changes.
N/A
deviceListChanged
Called whenever the list of hardware devices and device subdevices changes.
🔊 SimplyCoreAudio
SimplyCoreAudio(formerly known asAMCoreAudio) is a Swift framework that aims to make Core Audio use less tedious in macOS.Features
Resources
Requirements
Installation
To install the Swift Package, please follow the steps below:
https://github.com/rnine/SimplyCoreAudio.gitas a Swift Package Manager dependency to your project.SimplyCoreAudioto your desired target(s).Usage
Import
SimplyCoreAudioInstantiate
SimplyCoreAudioInteract with
SimplyCoreAudioSubscribe to hardware-related notifications
Subscribe to notifications from a specific audio device
Subscribe to notifications from a specific audio stream
Supported Notifications
Audio Hardware Notifications
defaultInputDeviceChangeddefaultOutputDeviceChangeddefaultSystemOutputDeviceChangeddeviceListChangedaddedDevices: [AudioDevice],removedDevices: [AudioDevice]Audio Device Notifications
deviceNominalSampleRateDidChangedeviceAvailableNominalSampleRatesDidChangedeviceClockSourceDidChangedeviceNameDidChangedeviceOwnedObjectsDidChangedeviceVolumeDidChangechannel: UInt32,scope: ScopedeviceMuteDidChangechannel: UInt32,scope: ScopedeviceIsAliveDidChangedeviceIsRunningDidChangedeviceIsRunningSomewhereDidChangedeviceIsJackConnectedDidChangedevicePreferredChannelsForStereoDidChangedeviceHogModeDidChangeAudio Stream Notifications
streamIsActiveDidChangeisActiveflag changes state.streamPhysicalFormatDidChangeFurther Development & Patches
Do you want to contribute to the project? Please fork, patch, and then submit a pull request!
Running Tests
Please make sure to install
NullAudio.driverbefore attempting to run tests:Installing
NullAudio.driverNullAudio.driverlocated at:coreaudiodDemo Project
License
SimplyCoreAudiowas written by Ruben Nine (@rnine) in 2013-2014 (open-sourced in March 2014) and is licensed under the MIT license. See LICENSE.md.