音声合成
選択中の AI プロバイダーでテキストを音声に変換し、アプリで再生または処理できるローカル音声ファイルを受け取ります。
前提条件
AIBudsAISDKが初期化済みで、登録済みのプロバイダーが選択されていること。- プロバイダーが
TTSServiceAPIに対応していること。 - 必要な場合に、プロバイダー認証とネットワーク接続を利用できること。
- 公開 SDK の仕様に従い、音声合成をバックグラウンドスレッドから呼び出すこと。
AI を活用して実装
AI で実装
AI でこのワークフローを実装
公式の「AIBuds テキスト読み上げの実装」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/implement-aibuds-text-to-speech を読み、その指示に従ってください。このスキルで「AIBuds テキスト読み上げの実装」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
フレームワーク
AIBudsAI.xcframework
インポート
- Swift
- Objective-C
import AIBudsAI
import AIBudsAIFoundation#import <AIBudsAI/AIBudsAI-Swift.h>宣言
- Swift
- Objective-C
/// Synthesizes text into speech.
/// - Parameters:
/// - text: The text to synthesize.
/// - config: The synthesis configuration.
/// - completion: Called with the task identifier, success state, optional
/// result, and optional error when synthesis completes.
/// - Important: Call this method from a background thread to avoid blocking
/// the main thread.
public static func synthesizeText(_ text: String,
config: TTSConfig = .default,
completion: ((
_ taskId: String?,
_ success: Bool,
_ response: TTSResultModel?,
_ error: NSError?
) -> Void)? = nil) -> Void/// Synthesizes text into speech.
/// - Parameters:
/// - text: The text to synthesize.
/// - config: The synthesis configuration.
/// - completion: Called with the task identifier, success state, optional
/// result, and optional error when synthesis completes.
/// - Important: Call this method from a background thread to avoid blocking
/// the main thread.
+ (void)synthesizeText:(NSString * _Nonnull)text
config:(AIBudsTTSConfig * _Nonnull)config
completion:(void (^ _Nullable)(NSString * _Nullable, BOOL, AIBudsTTSResultModel * _Nullable, NSError * _Nullable))completion;synthesizeText を参照してください。
設定と結果
TTSConfig は任意の speakerId を公開します。nil の場合は、プロバイダーの既定話者が選択されます。
成功すると、TTSResultModel から次の値を取得できます。
| プロパティ | 説明 |
|---|---|
audioFile | アプリの Documents ディレクトリを基準とする音声ファイルの相対パス。 |
audioFilePath | 生成された音声ファイルのフルパス。 |
audioFormat | 生成された音声ファイルの形式。 |
使用例
- Swift
- Objective-C
let config = TTSConfig.default
config.speakerId = nil
DispatchQueue.global(qos: .userInitiated).async {
AIBudsAISDK.synthesizeText(
"Hello, how can I help you?",
config: config
) { taskId, success, response, error in
guard success, let response else {
print(error?.localizedDescription ?? "Synthesis failed")
return
}
print("Task: \(taskId ?? "Unavailable")")
print("Audio: \(response.audioFilePath)")
DispatchQueue.main.async {
playAudio(atPath: response.audioFilePath)
}
}
}AIBudsTTSConfig *config = [AIBudsTTSConfig defaultConfig];
config.speakerId = nil;
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
[AIBudsAISDK
synthesizeText:@"Hello, how can I help you?"
config:config
completion:^(
NSString *taskId, BOOL success, AIBudsTTSResultModel *response, NSError *error) {
if (!success || response == nil) {
NSLog(@"%@", error.localizedDescription ?: @"Synthesis failed");
return;
}
NSLog(@"Task: %@", taskId ?: @"Unavailable");
NSLog(@"Audio: %@", response.audioFilePath);
dispatch_async(dispatch_get_main_queue(), ^{
[self playAudioAtPath:response.audioFilePath];
});
}];
});注意事項
- この API は音声ファイルを生成するだけで、デバイスでの再生は開始しません。また、
stopSpeaking()やisSpeaking()は公開していません。 success、response、errorをまとめて判定します。成功とみなすにはsuccess == trueかつ応答値がnilではないことが必要です。- 話者 ID はプロバイダー固有です。選択中のプロバイダーが文書化している ID だけを設定してください。
- UI と音声プレーヤーの更新はメインスレッドへ戻してください。