同時通訳
長時間実行する音声同時通訳セッションを開始し、原文、訳文、任意の TTS 音声、イベント、最終レポートを逐次受け取ります。
SimultaneousInterpretationEventType のライフサイクルと onEvent での扱いは、AI セッションイベントを参照してください。
同時通訳セッションのライフサイクル
プロバイダーの開始、使用中の音声入力、逐次結果の並び順、中断処理、最終停止を一連の流れとして管理します。
前提条件
AIBudsAISDKが初期化済みで、登録済みプロバイダーの選択と認証が完了していること。- プロバイダーが
SimultaneousInterpretationServiceAPIに対応していること。 - 元言語と対象言語には異なる値を指定し、ハイフン区切りの言語 ID を使用すること。
- AIBuds AI SDK の内部録音を無効にする場合は、ホストアプリから外部 PCM を入力すること。接続中のデバイスを入力元にする場合、そのデバイスが
DeviceAudioRecordingAPIに準拠していること。
AI を活用して実装
AI でこのワークフローを実装
公式の「AIBuds 同時通訳の実装」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/implement-aibuds-simultaneous-interpretation を読み、その指示に従ってください。このスキルで「AIBuds 同時通訳の実装」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
フレームワーク
AIBudsAI.xcframework
インポート
- Swift
- Objective-C
import AIBuds
import AIBudsAI
import AIBudsAIFoundation#import <AIBuds/AIBuds-Swift.h>
#import <AIBudsAI/AIBudsAI-Swift.h>宣言
- Swift
- Objective-C
/// Starts a simultaneous interpretation session.
/// - Parameters:
/// - config: The session configuration.
/// - onStartSuccess: Called with the started session.
/// - onStartFailure: Called when the session cannot start.
/// - onStopByInterruption: Called when an interruption stops the session.
/// - onException: Called for a recoverable session exception. The app decides
/// whether the session should stop.
/// - streamResultHandler: Called with incremental interpretation results.
/// - onEvent: Called for session-level events.
/// - onFinish: Called with the optional final report.
public static func startSimultaneousInterpretation(_ config: SimultaneousInterpretationConfig = .default,
onStartSuccess: ((_ session: SimultaneousInterpretationSessionConvertible) -> Void)? = nil,
onStartFailure: ((_ error: NSError) -> Void)? = nil,
onStopByInterruption: ((_ error: NSError?) -> Void)? = nil,
onException: ((_ error: NSError) -> Void)? = nil,
streamResultHandler: ((
_ isFinal: Bool,
_ response: SimultaneousInterpretationDataModel?,
_ error: Error?
) -> Void)? = nil,
onEvent: ((_ event: SimultaneousInterpretationEventModel) -> Void)? = nil,
onFinish: ((_ report: SimultaneousInterpretationReportModel?) -> Void)? = nil)
/// Stops the current simultaneous interpretation session.
public static func stopSimultaneousInterpretation()/// Starts a simultaneous interpretation session.
/// - Parameters:
/// - config: The session configuration.
/// - onStartSuccess: Called with the started session.
/// - onStartFailure: Called when the session cannot start.
/// - onStopByInterruption: Called when an interruption stops the session.
/// - onException: Called for a recoverable session exception.
/// - streamResultHandler: Called with incremental interpretation results.
/// - onEvent: Called for session-level events.
/// - onFinish: Called with the optional final report.
+ (void)startSimultaneousInterpretationWithConfig:(AIBudsSimultaneousInterpretationConfig * _Nonnull)config
onStartSuccess:(void (^ _Nullable)(id <AIBudsSimultaneousInterpretationSessionConvertible> _Nonnull))onStartSuccess
onStartFailure:(void (^ _Nullable)(NSError * _Nonnull))onStartFailure
onStopByInterruption:(void (^ _Nullable)(NSError * _Nullable))onStopByInterruption
onException:(void (^ _Nullable)(NSError * _Nonnull))onException
streamResultHandler:(void (^ _Nullable)(BOOL, AIBudsSimultaneousInterpretationDataModel * _Nullable, NSError * _Nullable))streamResultHandler
onEvent:(void (^ _Nullable)(AIBudsSimultaneousInterpretationEventModel * _Nonnull))onEvent
onFinish:(void (^ _Nullable)(AIBudsSimultaneousInterpretationReportModel * _Nullable))onFinish;
/// Stops the current simultaneous interpretation session.
+ (void)stopSimultaneousInterpretation;startSimultaneousInterpretation と stopSimultaneousInterpretation を参照してください。
設定
SimultaneousInterpretationConfig では次の項目を設定できます。
| プロパティ | 既定値 | 説明 |
|---|---|---|
sourceLanguage | アプリの表示言語 | 任意の元言語。空文字列にすると自動判定が有効になります。 |
targetLanguage | en-US | 必須の対象言語。 |
enableTTS | true | 訳文の音声を合成するかどうか。 |
enableVoicePlayback | true | 合成音声を再生するかどうか。 |
usesInternalAudioRecording | true | AIBuds AI SDK の内部録音を使用するかどうか。 |
preferSpeakerOutput | false | スピーカー出力を優先するかどうか。 |
usesInternalAudioRecording が false の場合、AIBuds AI SDK はセッションの音声を録音しません。ホストアプリで返されたセッションを保持し、appendInt16PCM(_:isFinal:) または appendAudioPCMBuffer(_:isFinal:) を使って外部 PCM を入力してください。
使用例
- Swift
- Objective-C
let config = SimultaneousInterpretationConfig.default
config.sourceLanguage = "zh-CN"
config.targetLanguage = "en-US"
config.usesInternalAudioRecording = true
config.preferSpeakerOutput = false
AIBudsAISDK.startSimultaneousInterpretation(
config,
onStartSuccess: { session in
currentSession = session
},
onStartFailure: { error in
print("Unable to start: \(error.localizedDescription)")
},
onStopByInterruption: { error in
print(error?.localizedDescription ?? "Session interrupted")
currentSession = nil
},
onException: { error in
print("Session exception: \(error.localizedDescription)")
},
streamResultHandler: { isFinal, response, error in
if let error {
print(error.localizedDescription)
return
}
guard let response else { return }
if response.isSourceTextDefinite {
print("Source: \(response.sourceText ?? "")")
}
if response.isTargetTextDefinite {
print("Target: \(response.targetText ?? "")")
}
if isFinal { print("Final result") }
},
onEvent: { event in
print(event)
},
onFinish: { report in
currentSession = nil
print(report ?? "No report")
}
)AIBudsSimultaneousInterpretationConfig *config =
[AIBudsSimultaneousInterpretationConfig defaultConfig];
config.sourceLanguage = @"zh-CN";
config.targetLanguage = @"en-US";
config.usesInternalAudioRecording = YES;
config.preferSpeakerOutput = NO;
[AIBudsAISDK startSimultaneousInterpretationWithConfig:config
onStartSuccess:^(id<AIBudsSimultaneousInterpretationSessionConvertible> session) {
self.currentSession = session;
}
onStartFailure:^(NSError *error) {
NSLog(@"Unable to start: %@", error.localizedDescription);
}
onStopByInterruption:^(NSError *error) {
NSLog(@"%@", error.localizedDescription ?: @"Session interrupted");
self.currentSession = nil;
}
onException:^(NSError *error) {
NSLog(@"Session exception: %@", error.localizedDescription);
}
streamResultHandler:^(
BOOL isFinal, AIBudsSimultaneousInterpretationDataModel *response, NSError *error) {
if (error != nil) {
NSLog(@"%@", error.localizedDescription);
return;
}
if (response.isSourceTextDefinite) {
NSLog(@"Source: %@", response.sourceText ?: @"");
}
if (response.isTargetTextDefinite) {
NSLog(@"Target: %@", response.targetText ?: @"");
}
}
onEvent:^(AIBudsSimultaneousInterpretationEventModel *event) {
NSLog(@"%@", event);
}
onFinish:^(AIBudsSimultaneousInterpretationReportModel *report) {
self.currentSession = nil;
NSLog(@"%@", report);
}];session.isRecordingInternally が false の場合、外部音声の経路はホストアプリが管理します。Demo では onStartSuccess の後にデバイス側の AI 録音を開始し、デコードした PCM を順次 session.appendInt16PCM(_:isFinal:) へ渡します。停止時は先にデバイス録音を止め、その後で AIBudsAISDK.stopSimultaneousInterpretation() を呼び出します。
注意事項
- 開始時に返される
SimultaneousInterpretationSessionConvertibleを保持し、実行中のセッションと録音方式を管理してください。 onExceptionが呼ばれても、セッションが停止するとは限りません。継続するかstopSimultaneousInterpretation()を呼ぶかをアプリで判断してください。- 逐次コールバックをそのまま追加せず、
sourceTextSequenceとtargetTextSequenceを使って確定区間を並べてください。 - TTS 音声は
SimultaneousInterpretationDataModel上で、相対ファイル名、フルパス、または Base64 PCM データとして提供される場合があります。