アクティブノイズキャンセリング
接続中のデバイスから現在の 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
- Objective-C
import AIBuds
import AIBudsFoundation#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>プロトコル
設定とコマンドは DeviceANCAPI で定義されています。
- Swift
- Objective-C
/// 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?
)
}/// The protocol for device API that supports Active Noise Cancellation (ANC).
@protocol AIBudsDeviceANCAPI <AIBudsDeviceAPI>
/// The current Active Noise Cancellation (ANC) mode of the device.
@property(nonatomic, readonly) enum AIBudsANCMode ancMode;
/// The current ANC gain value of the device.
@property(nonatomic, readonly, strong) NSNumber *_Nullable ancGain;
/// The current transparency gain value of the device.
@property(nonatomic, readonly, strong) NSNumber *_Nullable transparencyGain;
/// Indicates whether the ANC fade feature is currently enabled.
@property(nonatomic, readonly) BOOL isAncFadeOn;
/// 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.
- (void)setAncMode:(enum AIBudsANCMode)mode
completion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)setAncGain:(NSInteger)ancGain completion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)setTransparencyGain:(NSInteger)transparencyGain
completion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)setAncFadeOn:(BOOL)isOn completion:(AIBudsCompletionHandler _Nullable)completion;
@endプロパティ
| プロパティ | 型 | 説明 |
|---|---|---|
ancMode | ANCMode | 現在の ANC モード。 |
ancGain | NSNumber? | 現在の ANC ゲイン。取得できない場合は nil。 |
transparencyGain | NSNumber? | 現在の外音取り込みゲイン。取得できない場合は nil。 |
isAncFadeOn | Bool / BOOL | ANC フェードが有効かどうか。 |
インスタンスメソッド
| メソッド | 用途 |
|---|---|
setAncMode | ANC モードを変更します。 |
setAncGain | ANC ゲインを変更します。 |
setTransparencyGain | 外音取り込みゲインを変更します。 |
setAncFadeOn | ANC フェードを有効または無効にします。 |
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
mode | ANCMode / AIBudsANCMode | .normal、.anc、.transparency のいずれか。.unknown は設定しないでください。 |
ancGain | Int / NSInteger | 設定する ANC ゲイン。現行 Demo は 0...100 を使用しますが、公開 SDK は共通範囲をまだ保証していません。 |
transparencyGain | Int / NSInteger | 設定する外音取り込みゲイン。現行 Demo は 0...100 を使用しますが、公開 SDK は共通範囲をまだ保証していません。 |
isOn | Bool / BOOL | ANC フェードを有効にするかどうか。 |
completion | AIBudsCompletionHandler? | 任意の完了コールバック。 |
これらのメソッドは値を直接返しません。完了コールバックは success と任意の NSError を受け取ります。
使用例
現在の設定を確認
- Swift
- Objective-C
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)")#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceANCAPI> device = (id<AIBudsDeviceANCAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceANCAPI)] ||
device.ancMode == AIBudsANCModeUnknown) {
NSLog(@"Device does not support ANC");
return;
}
NSLog(@"Mode: %ld", (long)device.ancMode);
NSLog(@"ANC gain: %@", device.ancGain ?: @"Unavailable");
NSLog(@"Transparency gain: %@", device.transparencyGain ?: @"Unavailable");
NSLog(@"ANC fade: %@", device.isAncFadeOn ? @"On" : @"Off");ANC モードを変更
- Swift
- Objective-C
device.setAncMode(.anc) { success, error in
guard success else {
print("Failed to change ANC mode: \(error?.localizedDescription ?? "Unknown error")")
return
}
print("ANC mode updated")
}[device setAncMode:AIBudsANCModeAnc
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Failed to change ANC mode: %@", error.localizedDescription);
return;
}
NSLog(@"ANC mode updated");
}];ゲインとフェードを変更
次の値は現行 SDK Demo に合わせています。SDK がデバイス固有のゲイン範囲を公開した時点で見直してください。
- Swift
- Objective-C
device.setAncGain(60, completion: nil)
device.setTransparencyGain(40, completion: nil)
device.setAncFadeOn(true, completion: nil)[device setAncGain:60 completion:nil];
[device setTransparencyGain:40 completion:nil];
[device setAncFadeOn:YES completion:nil];エラー処理
コマンドに失敗した場合は、デバイスが現在公開している値へ UI を戻し、コールバックエラーがある場合は内容を提示します。公開 API では ANC 固有のエラーコードは定義されていません。