Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.
rninePublic archive

About

🔊 A Swift framework that aims to make Core Audio use less tedious in macOS

Topics

Resources

Stars

455 stars

Watchers

1 watching

Forks

Latest commit

 

History

477 Commits

Folders and files

Repository files navigation

🔊 SimplyCoreAudio

Platform Swift support Swift Package Manager compatible GitHub tag License

SimplyCoreAudio (formerly known as AMCoreAudio) is a Swift framework that aims to make Core Audio use less tedious in macOS.

Features

  • Enumerating audio devices (see SimplyCoreAudio)
  • Getting/setting default input, output and system device (see SimplyCoreAudio)
  • Creating and destroying aggregate devices (see SimplyCoreAudio)
  • Querying audio device properties such as: name, manufacturer, UID, volume, mute, sample rate, clock source, etc. (see AudioDevice)
  • Managing (physical and virtual) audio streams associated to an audio device (see AudioStream)
  • Subscribing to audio hardware, audio device, and audio stream notifications (see Notification Extensions)

Resources

Requirements

  • Xcode 12+
  • Swift 4.0+
  • macOS 10.12+

Installation

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

  1. Import SimplyCoreAudio

    import SimplyCoreAudio
  2. Instantiate SimplyCoreAudio

    let simplyCA = SimplyCoreAudio()
  3. 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...
    }
  4. 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