SimplyCoreAudio Documentation

repository·develop·Indexed 19 days ago

https://github.com/rnine/simplycoreaudio

A Swift framework for macOS that provides high-level abstractions for Apple's Core Audio API. It simplifies managing audio devices, streams, and hardware notifications, allowing developers to query device properties, modify system defaults, and subscribe to audio hardware changes.

Tokens
2.8K
Snippets
9
Records
9
Agent score
16%

What's inside SimplyCoreAudio

  1. Install SimplyCoreAudio via Swift Package Manager

    develop

    To integrate SimplyCoreAudio into your macOS project, use Swift Package Manager (SPM):

    1. Add https://github.com/rnine/SimplyCoreAudio.git as a dependency in Xcode.
    2. Use the default package options when prompted.
    3. Add the SimplyCoreAudio package to your specific target(s).

    Requirements:

    • Xcode 12+
    • Swift 4.0+
    • macOS 10.12+
    https://github.com/rnine/SimplyCoreAudio.git
  2. Install NullAudio.driver for testing

    develop

    To run tests for SimplyCoreAudio, you must install the NullAudio.driver plugin:

    1. Download and build NullAudio.driver (refer to Apple's documentation on creating an audio server driver plug-in).
    2. Install it into the system HAL Plug-Ins folder: /Library/Audio/Plug-Ins/HAL.
    3. Reload coreaudiod to apply changes.
    # Install to HAL folder
    # (Move your built driver here)
    
    # Reload coreaudiod
    sudo launchctl kill KILL system/com.apple.audio.coreaudiod
  3. Basic usage of SimplyCoreAudio

    develop

    To use the framework, import the module, instantiate the SimplyCoreAudio class, and use its properties to interact with audio hardware. You can query default devices, list all devices, filter by scope (input/output), and modify system defaults like the default output device.

    import SimplyCoreAudio
    
    let simplyCA = SimplyCoreAudio()
    
    // Get the default output device
    let device = simplyCA.defaultOutputDevice
    
    // Get all output devices
    let allOutputs = simplyCA.allOutputDevices
    
    // Filter devices by scope
    let inputDevices = simplyCA.allDevices.filter { $0.channels(scope: .output) > 0 }
    
    // Set a device as the system default
    device.isDefaultOutputDevice = true
  4. Query AudioDevice properties

    develop

    Once you have an AudioDevice instance, you can query various hardware properties including sample rate, volume, and preferred stereo channels.

    // Get preferred output channels
    if let stereoPair = device.preferredChannelsForStereo(scope: .output) {
        let leftChannel = stereoPair.left
        let rightChannel = stereoPair.right
    }
    
    // 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...
    }
  5. Subscribe to Audio Hardware notifications

    develop

    You can observe system-wide audio changes using NotificationCenter. Common hardware notifications include changes to default devices and the device list.

    // 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 }
    }
    
    // Cleanup
    NotificationCenter.default.removeObserver(observer)
    observer = nil
  6. Subscribe to Audio Device and Audio Stream notifications

    develop

    Notifications can be scoped to a specific AudioDevice or a specific AudioStream to monitor granular changes like sample rate shifts or physical format changes.

    // Subscribe to a specific device
    let device = simplyCA.defaultOutputDevice
    var deviceObserver = NotificationCenter.default.addObserver(forName: .deviceNominalSampleRateDidChange,
                                                                   object: device,
                                                                   queue: .main) { (notification) in
        // Handle device-specific change
    }
    
    // Subscribe to a specific stream
    if let streams = device.streams(scope: .output), let stream0 = streams.first {
        var streamObserver = NotificationCenter.default.addObserver(forName: .streamPhysicalFormatDidChange,
                                                                    object: stream0,
                                                                    queue: .main) { (notification) in
            // Handle stream-specific change
        }
    }
  7. Reference: Audio Stream Notifications

    develop

    These notifications are sent when properties of a specific AudioStream change.

    | Name | Purpose | User Info |
    |:---|:---|:---|
    | `streamIsActiveDidChange` | Called whenever the audio stream `isActive` flag changes state. | N/A |
    | `streamPhysicalFormatDidChange` | Called whenever the audio stream physical format changes. | N/A |
  8. Reference: Audio Device Notifications

    develop

    These notifications are sent when properties of a specific AudioDevice change.

    | Name | Purpose | User Info |
    |:---|:---|:---|
    | `deviceNominalSampleRateDidChange` | Called whenever the audio device's sample rate changes. | N/A |
    | `deviceAvailableNominalSampleRatesDidChange` | Called whenever the audio device's list of nominal sample rates changes. | N/A |
    | `deviceClockSourceDidChange` | Called whenever the audio device's clock source changes. | N/A |
    | `deviceNameDidChange` | Called whenever the audio device's name changes. | |
    | `deviceOwnedObjectsDidChange` | Called whenever the list of owned audio devices on this audio device changes. | N/A |
    | `deviceVolumeDidChange` | Called whenever the audio device's volume for a given channel and scope changes. | `channel: UInt32`, `scope: Scope` |
    | `deviceMuteDidChange` | Called whenever the audio device's mute state for a given channel and scope changes. | `channel: UInt32`, `scope: Scope` |
    | `deviceIsAliveDidChange` | Called whenever the audio device's list of nominal sample rates changes. | N/A |
    | `deviceIsRunningDidChange` | Called whenever the audio device's *is running* property changes. | N/A |
    | `deviceIsRunningSomewhereDidChange` | Called whenever the audio device's *is running somewhere* property changes. | N/A |
    | `deviceIsJackConnectedDidChange` | Called whenever the audio device's *is jack connected* property changes. | N/A |
    | `devicePreferredChannelsForStereoDidChange` | Called whenever the audio device's *preferred channels for stereo* property changes. | N/A |
    | `deviceHogModeDidChange` | Called whenever the audio device's *hog mode* property changes. | N/A |
  9. Reference: Audio Hardware Notifications

    develop

    These notifications are broadcast globally when the system audio state changes.

    | 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. | `addedDevices: [AudioDevice]`, `removedDevices: [AudioDevice]` |