対話翻訳
話者ごとに短い同時通訳セッションを開始し、相手の番では元の言語と対象言語を入れ替えることで、2 人が交互に話す翻訳体験を実装します。
これは ConversationTranslationDemoController で示す製品ワークフローであり、独立した ConversationTranslationAPI を追加するものではありません。
One conversation turn
Each speaker turn owns one interpretation session. Reverse the language direction for the partner's turn and never run both turns concurrently.
前提条件
- 同時通訳のすべての前提条件を満たすこと。
- 元の言語と対象言語に、異なるハイフン区切りの言語識別子を使用すること。
- ホストアプリで、現在の話者、セッション、開始/停止状態、表示済みの各ターンのメッセージを管理すること。
- 両方の話者が同時にセッションを開始できないようにすること。
AI を活用して実装
AI でこのワークフローを実装
公式の「AIBuds 会話翻訳の実装」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/implement-aibuds-conversation-translation を読み、その指示に従ってください。このスキルで「AIBuds 会話翻訳の実装」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
対話翻訳では startSimultaneousInterpretation、stopSimultaneousInterpretation、SimultaneousInterpretationConfig を使用します。
公開宣言とコールバック仕様の全体は、同時通訳を参照してください。
使用例
話者のターンを開始
Demo では「自分」を myLanguage → partnerLanguage に、「相手」を逆方向に対応付けます。次のヘルパーは決定済みの言語方向を受け取り、この状態を明確に表します。
- Swift
- Objective-C
func startConversationTurn(source: String, target: String) {
guard currentSession == nil, source != target else { return }
let config = SimultaneousInterpretationConfig.default
config.sourceLanguage = source
config.targetLanguage = target
config.usesInternalAudioRecording = true
config.preferSpeakerOutput = true
config.enableVoicePlayback = true
AIBudsAISDK.startSimultaneousInterpretation(
config,
onStartSuccess: { session in
currentSession = session
setTurnActive(true)
},
onStartFailure: { error in
currentSession = nil
setTurnActive(false)
show(error)
},
onStopByInterruption: { error in
currentSession = nil
setTurnActive(false)
if let error { show(error) }
},
onException: { error in
showRecoverable(error)
},
streamResultHandler: { _, response, error in
if let error {
show(error)
return
}
guard let response else { return }
if response.isSourceTextDefinite { renderSource(response) }
if response.isTargetTextDefinite { renderTarget(response) }
},
onEvent: { event in
handle(event)
},
onFinish: { report in
currentSession = nil
setTurnActive(false)
save(report)
}
)
}- (void)startConversationTurnFrom:(NSString *)source to:(NSString *)target {
if (self.currentSession != nil || [source isEqualToString:target])
return;
AIBudsSimultaneousInterpretationConfig *config =
[AIBudsSimultaneousInterpretationConfig defaultConfig];
config.sourceLanguage = source;
config.targetLanguage = target;
config.usesInternalAudioRecording = YES;
config.preferSpeakerOutput = YES;
config.enableVoicePlayback = YES;
[AIBudsAISDK startSimultaneousInterpretationWithConfig:config
onStartSuccess:^(id<AIBudsSimultaneousInterpretationSessionConvertible> session) {
self.currentSession = session;
[self setTurnActive:YES];
}
onStartFailure:^(NSError *error) {
self.currentSession = nil;
[self setTurnActive:NO];
[self showError:error];
}
onStopByInterruption:^(NSError *error) {
self.currentSession = nil;
[self setTurnActive:NO];
if (error != nil)
[self showError:error];
}
onException:^(NSError *error) {
[self showRecoverableError:error];
}
streamResultHandler:^(
BOOL isFinal, AIBudsSimultaneousInterpretationDataModel *response, NSError *error) {
if (error != nil) {
[self showError:error];
return;
}
if (response.isSourceTextDefinite)
[self renderSource:response];
if (response.isTargetTextDefinite)
[self renderTarget:response];
}
onEvent:^(AIBudsSimultaneousInterpretationEventModel *event) {
[self handleEvent:event];
}
onFinish:^(AIBudsSimultaneousInterpretationReportModel *report) {
self.currentSession = nil;
[self setTurnActive:NO];
[self saveReport:report];
}];
}現在のターンを停止
currentSession.isRecordingInternally が false の場合は、最後の音声が正しい順序で送られるよう、サービス停止前にデバイス側の AI 録音を停止します。
- Swift
- Objective-C
func stopConversationTurn() {
guard let session = currentSession else { return }
if !session.isRecordingInternally,
let recordingDevice = device as? DeviceAudioRecordingAPI
{
recordingDevice.stopAIAudioRecording(.onSite) { _, _ in
AIBudsAISDK.stopSimultaneousInterpretation()
}
} else {
AIBudsAISDK.stopSimultaneousInterpretation()
}
}- (void)stopConversationTurn {
if (self.currentSession == nil)
return;
if (!self.currentSession.isRecordingInternally) {
id<AIBudsDeviceAudioRecordingAPI> recordingDevice =
(id<AIBudsDeviceAudioRecordingAPI>)self.device;
if ([recordingDevice conformsToProtocol:@protocol(AIBudsDeviceAudioRecordingAPI)]) {
[recordingDevice
stopAIAudioRecordingWithScene:AIBudsRecordingSceneOnSite
completion:^(__unused BOOL success, __unused NSError *error) {
[AIBudsAISDK stopSimultaneousInterpretation];
}];
return;
}
}
[AIBudsAISDK stopSimultaneousInterpretation];
}エラー処理
開始失敗、中断、回復可能な例外、ストリームエラー、デバイス録音の失敗、最終完了を個別に処理します。デバイス録音の開始中に停止が要求された場合は、開始コールバックが完了するまでサービス停止を延期してください。Demo ではこの競合を明示的に管理しています。
注意事項
- 1 つの対話に複数のターンを含められますが、同時に有効にする通訳セッションは 1 つだけです。
- 言語の入れ替えは、どのターンも実行されていないときだけ行います。
- すべてのコールバックをそのまま追加せず、原文と訳文のシーケンスフィールドを使って確定区間を並べます。
- 外部デバイス録音を使用する場合は、最初のパケットを失わないよう、デバイス音声の開始前に保持した通訳セッションをアプリの PCM 転送経路へ登録します。