メインコンテンツまでスキップ

アクティブノイズキャンセリング

接続中のデバイスから現在の ANC 設定を取得し、モード、ゲイン、フェード動作を更新します。

前提条件

  • デバイスが接続済みで、操作可能な状態であること。
  • デバイスが DeviceANCAPI に準拠していること。
  • ANC 操作を有効にする前に、ancMode.unknown ではないこと。

AI を活用して実装

AI で実装

AI でこのワークフローを実装

公式の「AIBuds ノイズコントロールの設定」スキルを使い、アプリに合わせて実装します。

https://docs-aibuds.github.io/ja/skills/configure-aibuds-anc を読み、その指示に従ってください。このスキルで「AIBuds ノイズコントロールの設定」をこの iOS プロジェクトに実装し、検証してください。
公式スキルを見る

API リファレンス

フレームワーク

AIBuds.xcframework

インポート

Swift
import AIBuds
import AIBudsFoundation

プロトコル

設定とコマンドは 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?
    )
}

プロパティ

プロパティ説明
ancModeANCMode現在の ANC モード。
ancGainNSNumber?現在の ANC ゲイン。取得できない場合は nil
transparencyGainNSNumber?現在の外音取り込みゲイン。取得できない場合は nil
isAncFadeOnBool / BOOLANC フェードが有効かどうか。

インスタンスメソッド

メソッド用途
setAncModeANC モードを変更します。
setAncGainANC ゲインを変更します。
setTransparencyGain外音取り込みゲインを変更します。
setAncFadeOnANC フェードを有効または無効にします。

パラメータ

パラメータ説明
modeANCMode / AIBudsANCMode.normal.anc.transparency のいずれか。.unknown は設定しないでください。
ancGainInt / NSInteger設定する ANC ゲイン。現行 Demo は 0...100 を使用しますが、公開 SDK は共通範囲をまだ保証していません。
transparencyGainInt / NSInteger設定する外音取り込みゲイン。現行 Demo は 0...100 を使用しますが、公開 SDK は共通範囲をまだ保証していません。
isOnBool / BOOLANC フェードを有効にするかどうか。
completionAIBudsCompletionHandler?任意の完了コールバック。

これらのメソッドは値を直接返しません。完了コールバックは success と任意の NSError を受け取ります。

使用例

現在の設定を確認

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)")

ANC モードを変更

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")
}

ゲインとフェードを変更

次の値は現行 SDK Demo に合わせています。SDK がデバイス固有のゲイン範囲を公開した時点で見直してください。

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

エラー処理

コマンドに失敗した場合は、デバイスが現在公開している値へ UI を戻し、コールバックエラーがある場合は内容を提示します。公開 API では ANC 固有のエラーコードは定義されていません。