Skip to main content

Active Noise Cancellation

Read the current ANC settings from a connected device and update its mode, gains, or fade behavior.

Prerequisites

  • The device is connected and ready.
  • The device conforms to DeviceANCAPI.
  • ancMode is not .unknown before enabling ANC controls.

Implement with AI Assistance

Build with AI

Implement this workflow with AI

Use the official Configure AIBuds Noise Control skill to adapt this workflow to your app.

Read and follow https://docs-aibuds.github.io/skills/configure-aibuds-anc. Use it to implement Configure AIBuds Noise Control in this iOS project and verify the result.
View official skill

API Reference

Framework

AIBuds.xcframework

Import

Swift
import AIBuds
import AIBudsFoundation

Protocol

The settings and commands are defined by DeviceANCAPI.

Swift
/// The protocol for device API that supports Active Noise Cancellation (ANC).
protocol DeviceANCAPI: DeviceAPI {
    /// The current Active Noise Cancellation (ANC) mode of the device.
    var ancMode: ANCMode { get }

    /// The current ANC gain value of the device.
    var ancGain: NSNumber? { get }

    /// The current transparency gain value of the device.
    var transparencyGain: NSNumber? { get }

    /// Indicates whether the ANC fade feature is currently enabled.
    var isAncFadeOn: Bool { get }

    /// Sets the Active Noise Cancellation (ANC) mode for the device.
    /// - Parameters:
    ///   - mode: The desired ANC mode to apply.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setAncMode(
        _ mode: ANCMode,
        completion: AIBudsCompletionHandler?
    )

    /// Sets the ANC gain value for the device.
    /// - Parameters:
    ///   - ancGain: The desired ANC gain value to apply.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setAncGain(
        _ ancGain: Int,
        completion: AIBudsCompletionHandler?
    )

    /// Sets the transparency gain value for the device.
    /// - Parameters:
    ///   - transparencyGain: The desired transparency gain value to apply.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setTransparencyGain(
        _ transparencyGain: Int,
        completion: AIBudsCompletionHandler?
    )

    /// Enables or disables the ANC fade feature for the device.
    /// - Parameters:
    ///   - isOn: A Boolean value indicating whether to enable (`true`) or disable (`false`) the ANC fade feature.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: Indicates whether the operation completed successfully.
    ///     - error: An optional error object that provides details if the operation failed; `nil` if the operation succeeded.
    func setAncFadeOn(
        _ isOn: Bool,
        completion: AIBudsCompletionHandler?
    )
}

Properties

PropertyTypeDescription
ancModeANCModeCurrent ANC mode.
ancGainNSNumber?Current ANC gain, or nil when unavailable.
transparencyGainNSNumber?Current transparency gain, or nil when unavailable.
isAncFadeOnBool / BOOLWhether ANC fade is enabled.

Instance Methods

MethodPurpose
setAncModeChange the ANC mode.
setAncGainChange the ANC gain.
setTransparencyGainChange the transparency gain.
setAncFadeOnEnable or disable ANC fade.

Parameters

ParameterTypeDescription
modeANCMode / AIBudsANCMode.normal, .anc, or .transparency. Do not set .unknown.
ancGainInt / NSIntegerDesired ANC gain. The current Demo uses 0...100; the public SDK does not yet guarantee a universal range.
transparencyGainInt / NSIntegerDesired transparency gain. The current Demo uses 0...100; the public SDK does not yet guarantee a universal range.
isOnBool / BOOLWhether ANC fade should be enabled.
completionAIBudsCompletionHandler?Optional completion callback.

The methods return no value directly. Their completion callback receives success and an optional NSError.

Usage Examples

Read Current Settings

Swift
import AIBuds

guard let device = device as? DeviceANCAPI,
    device.ancMode != .unknown
else {
    print("Device does not support ANC")
    return
}

print("Mode: \(device.ancMode)")
print("ANC gain: \(device.ancGain?.intValue.description ?? "Unavailable")")
print("Transparency gain: \(device.transparencyGain?.intValue.description ?? "Unavailable")")
print("ANC fade: \(device.isAncFadeOn)")

Change ANC Mode

Swift
device.setAncMode(.anc) { success, error in
    guard success else {
        print("Failed to change ANC mode: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("ANC mode updated")
}

Change Gain and Fade

The following values follow the current SDK Demo. Revisit them when the SDK publishes device-specific gain limits.

Swift
device.setAncGain(60, completion: nil)
device.setTransparencyGain(40, completion: nil)
device.setAncFadeOn(true, completion: nil)

Error Handling

If a command fails, restore the UI to the values currently exposed by the device and surface the callback error when available. The public API does not define ANC-specific error codes.