このバージョンはまだ開発中であり、まだ安定しているとは考えられていません。最新のスナップショットバージョンについては、Spring AI 1.0.3 を使用してください。

ElevenLabs テキスト読み上げ (TTS)

導入

ElevenLabs は、ディープラーニングを活用した自然な音声合成ソフトウェアを提供しています。AI 音声モデルは、32 言語に対応し、リアルで汎用性が高く、文脈を考慮した音声、音声、効果音を生成します。ElevenLabs のテキスト読み上げ API を利用すると、書籍、記事、PDF、ニュースレター、テキストなどを、超リアルな AI ナレーションで生き生きと表現できます。

前提条件

  1. ElevenLabs アカウントを作成し、API キーを取得してください。ElevenLabs サインアップページ (英語) でサインアップできます。API キーはログイン後、プロフィールページで確認できます。

  2. プロジェクトのビルドファイルに spring-ai-elevenlabs 依存関係を追加します。詳細については、依存関係管理セクションを参照してください。

自動構成

Spring AI は、ElevenLabs テキスト読み上げクライアント用の Spring Boot 自動構成を提供します。これを有効にするには、プロジェクトの Maven pom.xml ファイルに以下の依存関係を追加してください。

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-elevenlabs</artifactId>
</dependency>

または、Gradle build.gradle ビルドファイルに次の内容を追加します。

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-elevenlabs'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。

音声プロパティ

接続プロパティ

プレフィックス spring.ai.elevenlabs は、ElevenLabs 関連のすべての設定(接続と TTS 固有の設定の両方)のプロパティプレフィックスとして使用されます。これは ElevenLabsConnectionProperties で定義されています。

プロパティ

説明

デフォルト

spring.ai.elevenlabs.base-url

ElevenLabs API のベース URL。

api.elevenlabs.io (英語)

spring.ai.elevenlabs.api-key

ElevenLabs API キー。

-

プロパティの構成

プレフィックス spring.ai.elevenlabs.tts は、ElevenLabs テキスト読み上げクライアントを設定するためのプロパティプレフィックスとして使用されます。これは ElevenLabsSpeechProperties で定義されています。

プロパティ 説明 デフォルト

spring.ai.elevenlabs.tts.options.model-id

使用するモデルの ID。

11_turbo_v2_5

spring.ai.elevenlabs.tts.options.voice-id

使用する音声の ID。これは音声名ではなく、音声 ID です。

9BWtsMINqrJLrRacOk9x

spring.ai.elevenlabs.tts.options.output-format

生成されたオーディオの出力形式。下記の出力書式を参照してください。

mp3_22050_32

spring.ai.elevenlabs.tts.enabled

ElevenLabs テキスト読み上げクライアントを有効または無効にします。

true

ベース URL と API キーは、spring.ai.elevenlabs.tts.base-url と spring.ai.elevenlabs.tts.api-key を使用した TTS 専用に設定することもできます。ただし、異なる ElevenLabs サービスごとに異なる認証情報を使用する特別な理由がない限り、簡潔性の観点から、通常はグローバルプレフィックスである spring.ai.elevenlabs を使用することをお勧めします。より具体的な tts プロパティは、グローバルプロパティをオーバーライドします。
spring.ai.elevenlabs.tts.options で始まるすべてのプロパティは、実行時にオーバーライドできます。
表 1: Available Output Formats

Enum Value

説明

MP3_22050_32

MP3, 22.05 kHz, 32 kbps

MP3_44100_32

MP3, 44.1 kHz, 32 kbps

MP3_44100_64

MP3, 44.1 kHz, 64 kbps

MP3_44100_96

MP3, 44.1 kHz, 96 kbps

MP3_44100_128

MP3, 44.1 kHz, 128 kbps

MP3_44100_192

MP3, 44.1 kHz, 192 kbps

PCM_8000

PCM, 8 kHz

PCM_16000

PCM, 16 kHz

PCM_22050

PCM, 22.05 kHz

PCM_24000

PCM, 24 kHz

PCM_44100

PCM, 44.1 kHz

PCM_48000

PCM, 48 kHz

ULAW_8000

µ -law, 8 kHz

ALAW_8000

A-law, 8 kHz

OPUS_48000_32

Opus, 48 kHz, 32 kbps

OPUS_48000_64

Opus, 48 kHz, 64 kbps

OPUS_48000_96

Opus, 48 kHz, 96 kbps

OPUS_48000_128

Opus, 48 kHz, 128 kbps

OPUS_48000_192

Opus, 48 kHz, 192 kbps

ランタイムオプション

The ElevenLabsTextToSpeechOptions class provides options to use when making a text-to-speech request. On start-up, the options specified by spring.ai.elevenlabs.tts are used, but you can override these at runtime. The following options are available:

  • modelId: 使用するモデルの ID。

  • voiceId: The ID of the voice to use.

  • outputFormat: The output format of the generated audio.

  • voiceSettings: An object containing voice settings such as stabilitysimilarityBooststyleuseSpeakerBoostspeed.

  • enableLogging: A boolean to enable or disable logging.

  • languageCode: The language code of the input text (e.g., "en" for English).

  • pronunciationDictionaryLocators: A list of pronunciation dictionary locators.

  • seed: A seed for random number generation, for reproducibility.

  • previousText: Text before the main text, for context in multi-turn conversations.

  • nextText: Text after the main text, for context in multi-turn conversations.

  • previousRequestIds: Request IDs from previous turns in a conversation.

  • nextRequestIds: Request IDs for subsequent turns in a conversation.

  • applyTextNormalization: Apply text normalization ("auto", "on", or "off").

  • applyLanguageTextNormalization: Apply language text normalization.

例:

ElevenLabsTextToSpeechOptions speechOptions = ElevenLabsTextToSpeechOptions.builder()
    .model("eleven_multilingual_v2")
    .voiceId("your_voice_id")
    .outputFormat(ElevenLabsApi.OutputFormat.MP3_44100_128.getValue())
    .build();

TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("Hello, this is a text-to-speech example.", speechOptions);
TextToSpeechResponse response = elevenLabsTextToSpeechModel.call(speechPrompt);

Using Voice Settings

You can customize the voice output by providing VoiceSettings in the options. This allows you to control properties like stability and similarity.

var voiceSettings = new ElevenLabsApi.SpeechRequest.VoiceSettings(0.75f, 0.75f, 0.0f, true);

ElevenLabsTextToSpeechOptions speechOptions = ElevenLabsTextToSpeechOptions.builder()
    .model("eleven_multilingual_v2")
    .voiceId("your_voice_id")
    .voiceSettings(voiceSettings)
    .build();

TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("This is a test with custom voice settings!", speechOptions);
TextToSpeechResponse response = elevenLabsTextToSpeechModel.call(speechPrompt);

手動構成

spring-ai-elevenlabs 依存関係をプロジェクトの Maven pom.xml ファイルに追加します。

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-elevenlabs</artifactId>
</dependency>

または、Gradle build.gradle ビルドファイルに次の内容を追加します。

dependencies {
    implementation 'org.springframework.ai:spring-ai-elevenlabs'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。

次に、ElevenLabsTextToSpeechModel を作成します。

ElevenLabsApi elevenLabsApi = ElevenLabsApi.builder()
		.apiKey(System.getenv("ELEVEN_LABS_API_KEY"))
		.build();

ElevenLabsTextToSpeechModel elevenLabsTextToSpeechModel = ElevenLabsTextToSpeechModel.builder()
	.elevenLabsApi(elevenLabsApi)
	.defaultOptions(ElevenLabsTextToSpeechOptions.builder()
		.model("eleven_turbo_v2_5")
		.voiceId("your_voice_id") // e.g. "9BWtsMINqrJLrRacOk9x"
		.outputFormat("mp3_44100_128")
		.build())
	.build();

// The call will use the default options configured above.
TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("Hello, this is a text-to-speech example.");
TextToSpeechResponse response = elevenLabsTextToSpeechModel.call(speechPrompt);

byte[] responseAsBytes = response.getResult().getOutput();

リアルタイムオーディオのストリーミング

The ElevenLabs Speech API supports real-time audio streaming using chunk transfer encoding. This allows audio playback to begin before the entire audio file is generated.

ElevenLabsApi elevenLabsApi = ElevenLabsApi.builder()
		.apiKey(System.getenv("ELEVEN_LABS_API_KEY"))
		.build();

ElevenLabsTextToSpeechModel elevenLabsTextToSpeechModel = ElevenLabsTextToSpeechModel.builder()
	.elevenLabsApi(elevenLabsApi)
	.build();

ElevenLabsTextToSpeechOptions streamingOptions = ElevenLabsTextToSpeechOptions.builder()
    .model("eleven_turbo_v2_5")
    .voiceId("your_voice_id")
    .outputFormat("mp3_44100_128")
    .build();

TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("Today is a wonderful day to build something people love!", streamingOptions);

Flux<TextToSpeechResponse> responseStream = elevenLabsTextToSpeechModel.stream(speechPrompt);

// Process the stream, e.g., play the audio chunks
responseStream.subscribe(speechResponse -> {
    byte[] audioChunk = speechResponse.getResult().getOutput();
    // Play the audioChunk
});

Voices API

The ElevenLabs Voices API allows you to retrieve information about available voices, their settings, and default voice settings. You can use this API to discover the `voiceId` s to use in your speech requests.

To use the Voices API, you’ll need to create an instance of ElevenLabsVoicesApi:

ElevenLabsVoicesApi voicesApi = ElevenLabsVoicesApi.builder()
        .apiKey(System.getenv("ELEVEN_LABS_API_KEY"))
        .build();

You can then use the following methods:

  • getVoices(): Retrieves a list of all available voices.

  • getDefaultVoiceSettings(): Gets the default settings for voices.

  • getVoiceSettings(String voiceId): Returns the settings for a specific voice.

  • getVoice(String voiceId): Returns metadata about a specific voice.

例:

// Get all voices
ResponseEntity<ElevenLabsVoicesApi.Voices> voicesResponse = voicesApi.getVoices();
List<ElevenLabsVoicesApi.Voice> voices = voicesResponse.getBody().voices();

// Get default voice settings
ResponseEntity<ElevenLabsVoicesApi.VoiceSettings> defaultSettingsResponse = voicesApi.getDefaultVoiceSettings();
ElevenLabsVoicesApi.VoiceSettings defaultSettings = defaultSettingsResponse.getBody();

// Get settings for a specific voice
ResponseEntity<ElevenLabsVoicesApi.VoiceSettings> voiceSettingsResponse = voicesApi.getVoiceSettings(voiceId);
ElevenLabsVoicesApi.VoiceSettings voiceSettings = voiceSettingsResponse.getBody();

// Get details for a specific voice
ResponseEntity<ElevenLabsVoicesApi.Voice> voiceDetailsResponse = voicesApi.getVoice(voiceId);
ElevenLabsVoicesApi.Voice voiceDetails = voiceDetailsResponse.getBody();

サンプルコード