Class: ElevenlabsClient::TextToSpeech

Inherits:
Object
  • Object
show all
Defined in:
lib/elevenlabs_client/endpoints/text_to_speech.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ TextToSpeech

Returns a new instance of TextToSpeech.



5
6
7
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 5

def initialize(client)
  @client = client
end

Instance Method Details

#convert(voice_id, text, **options) ⇒ String Also known as: text_to_speech

POST /v1/text-to-speech/voice_id Convert text to speech and retrieve audio (binary data) Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/convert

Parameters:

  • voice_id (String)

    The ID of the voice to use

  • text (String)

    Text to synthesize

  • options (Hash)

    Optional TTS parameters

Options Hash (**options):

  • :model_id (String)

    Model to use (e.g. "eleven_monolingual_v1" or "eleven_multilingual_v1")

  • :voice_settings (Hash)

    Voice configuration (stability, similarity_boost, style, use_speaker_boost, etc.)

  • :optimize_streaming (Boolean)

    Whether to receive chunked streaming audio

Returns:

  • (String)

    The binary audio data (usually an MP3)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 20

def convert(voice_id, text, **options)
  endpoint = "/v1/text-to-speech/#{voice_id}"
  request_body = { text: text }

  # Add optional parameters
  request_body[:model_id] = options[:model_id] if options[:model_id]
  request_body[:voice_settings] = options[:voice_settings] if options[:voice_settings]

  # Handle streaming optimization
  if options[:optimize_streaming]
    @client.post_with_custom_headers(endpoint, request_body, streaming_headers)
  else
    @client.post_binary(endpoint, request_body)
  end
end

#convert_with_timestamps(voice_id, text, **options) ⇒ Hash Also known as: text_to_speech_with_timestamps

POST /v1/text-to-speech/voice_id/with-timestamps Generate speech from text with precise character-level timing information Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/with-timestamps

Parameters:

  • voice_id (String)

    Voice ID to be used

  • text (String)

    The text that will get converted into speech

  • options (Hash)

    Optional TTS parameters

Options Hash (**options):

  • :model_id (String)

    Model identifier (defaults to "eleven_multilingual_v2")

  • :language_code (String)

    ISO 639-1 language code for text normalization

  • :voice_settings (Hash)

    Voice settings overriding stored settings

  • :pronunciation_dictionary_locators (Array<Hash>)

    Pronunciation dictionary locators (max 3)

  • :seed (Integer)

    Deterministic sampling seed (0-4294967295)

  • :previous_text (String)

    Text that came before current request

  • :next_text (String)

    Text that comes after current request

  • :previous_request_ids (Array<String>)

    Request IDs of previous samples (max 3)

  • :next_request_ids (Array<String>)

    Request IDs of next samples (max 3)

  • :apply_text_normalization (String)

    Text normalization mode ("auto", "on", "off")

  • :apply_language_text_normalization (Boolean)

    Language text normalization

  • :use_pvc_as_ivc (Boolean)

    Use IVC version instead of PVC (deprecated)

  • :enable_logging (Boolean)

    Enable logging (defaults to true)

  • :optimize_streaming_latency (Integer)

    Latency optimizations (0-4, deprecated)

  • :output_format (String)

    Output format (defaults to "mp3_44100_128")

Returns:

  • (Hash)

    Response containing audio_base64, alignment, and normalized_alignment



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 59

def convert_with_timestamps(voice_id, text, **options)
  # Build query parameters
  query_params = {}
  query_params[:enable_logging] = options[:enable_logging] unless options[:enable_logging].nil?
  query_params[:optimize_streaming_latency] = options[:optimize_streaming_latency] if options[:optimize_streaming_latency]
  query_params[:output_format] = options[:output_format] if options[:output_format]
  
  # Build endpoint with query parameters
  endpoint = "/v1/text-to-speech/#{voice_id}/with-timestamps"
  if query_params.any?
    query_string = query_params.map { |k, v| "#{k}=#{v}" }.join("&")
    endpoint += "?#{query_string}"
  end
  
  # Build request body
  request_body = { text: text }
  
  # Add optional body parameters
  request_body[:model_id] = options[:model_id] if options[:model_id]
  request_body[:language_code] = options[:language_code] if options[:language_code]
  request_body[:voice_settings] = options[:voice_settings] if options[:voice_settings]
  request_body[:pronunciation_dictionary_locators] = options[:pronunciation_dictionary_locators] if options[:pronunciation_dictionary_locators]
  request_body[:seed] = options[:seed] if options[:seed]
  request_body[:previous_text] = options[:previous_text] if options[:previous_text]
  request_body[:next_text] = options[:next_text] if options[:next_text]
  request_body[:previous_request_ids] = options[:previous_request_ids] if options[:previous_request_ids]
  request_body[:next_request_ids] = options[:next_request_ids] if options[:next_request_ids]
  request_body[:apply_text_normalization] = options[:apply_text_normalization] if options[:apply_text_normalization]
  request_body[:apply_language_text_normalization] = options[:apply_language_text_normalization] unless options[:apply_language_text_normalization].nil?
  request_body[:use_pvc_as_ivc] = options[:use_pvc_as_ivc] unless options[:use_pvc_as_ivc].nil?

  @client.post(endpoint, request_body)
end

#stream(voice_id, text, **options, &block) ⇒ Faraday::Response Also known as: text_to_speech_stream

POST /v1/text-to-speech/voice_id/stream Stream text-to-speech audio in real-time chunks Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/stream

Parameters:

  • voice_id (String)

    The ID of the voice to use

  • text (String)

    Text to synthesize

  • options (Hash)

    Optional TTS parameters

  • block (Proc)

    Block to handle each audio chunk

Options Hash (**options):

  • :model_id (String)

    Model to use (defaults to "eleven_multilingual_v2")

  • :output_format (String)

    Output format (defaults to "mp3_44100_128")

  • :voice_settings (Hash)

    Voice configuration

Returns:

  • (Faraday::Response)

    The response object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 107

def stream(voice_id, text, **options, &block)
  output_format = options[:output_format] || "mp3_44100_128"
  endpoint = "/v1/text-to-speech/#{voice_id}/stream?output_format=#{output_format}"
  
  request_body = {
    text: text,
    model_id: options[:model_id] || "eleven_multilingual_v2"
  }
  
  # Add voice_settings if provided
  request_body[:voice_settings] = options[:voice_settings] if options[:voice_settings]

  @client.post_streaming(endpoint, request_body, &block)
end

#stream_with_timestamps(voice_id, text, **options, &block) ⇒ Faraday::Response Also known as: text_to_speech_stream_with_timestamps

POST /v1/text-to-speech/voice_id/stream/with-timestamps Stream text-to-speech audio with character-level timing information Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/stream-with-timestamps

Parameters:

  • voice_id (String)

    Voice ID to be used

  • text (String)

    The text that will get converted into speech

  • options (Hash)

    Optional TTS parameters

  • block (Proc)

    Block to handle each streaming chunk containing audio and timing data

Options Hash (**options):

  • :model_id (String)

    Model identifier (defaults to "eleven_multilingual_v2")

  • :language_code (String)

    ISO 639-1 language code for text normalization

  • :voice_settings (Hash)

    Voice settings overriding stored settings

  • :pronunciation_dictionary_locators (Array<Hash>)

    Pronunciation dictionary locators (max 3)

  • :seed (Integer)

    Deterministic sampling seed (0-4294967295)

  • :previous_text (String)

    Text that came before current request

  • :next_text (String)

    Text that comes after current request

  • :previous_request_ids (Array<String>)

    Request IDs of previous samples (max 3)

  • :next_request_ids (Array<String>)

    Request IDs of next samples (max 3)

  • :apply_text_normalization (String)

    Text normalization mode ("auto", "on", "off")

  • :apply_language_text_normalization (Boolean)

    Language text normalization

  • :use_pvc_as_ivc (Boolean)

    Use IVC version instead of PVC (deprecated)

  • :enable_logging (Boolean)

    Enable logging (defaults to true)

  • :optimize_streaming_latency (Integer)

    Latency optimizations (0-4, deprecated)

  • :output_format (String)

    Output format (defaults to "mp3_44100_128")

Returns:

  • (Faraday::Response)

    The response object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 146

def stream_with_timestamps(voice_id, text, **options, &block)
  # Build query parameters
  query_params = {}
  query_params[:enable_logging] = options[:enable_logging] unless options[:enable_logging].nil?
  query_params[:optimize_streaming_latency] = options[:optimize_streaming_latency] if options[:optimize_streaming_latency]
  query_params[:output_format] = options[:output_format] if options[:output_format]
  
  # Build endpoint with query parameters
  endpoint = "/v1/text-to-speech/#{voice_id}/stream/with-timestamps"
  if query_params.any?
    query_string = query_params.map { |k, v| "#{k}=#{v}" }.join("&")
    endpoint += "?#{query_string}"
  end
  
  # Build request body
  request_body = { text: text }
  
  # Add optional body parameters
  request_body[:model_id] = options[:model_id] if options[:model_id]
  request_body[:language_code] = options[:language_code] if options[:language_code]
  request_body[:voice_settings] = options[:voice_settings] if options[:voice_settings]
  request_body[:pronunciation_dictionary_locators] = options[:pronunciation_dictionary_locators] if options[:pronunciation_dictionary_locators]
  request_body[:seed] = options[:seed] if options[:seed]
  request_body[:previous_text] = options[:previous_text] if options[:previous_text]
  request_body[:next_text] = options[:next_text] if options[:next_text]
  request_body[:previous_request_ids] = options[:previous_request_ids] if options[:previous_request_ids]
  request_body[:next_request_ids] = options[:next_request_ids] if options[:next_request_ids]
  request_body[:apply_text_normalization] = options[:apply_text_normalization] if options[:apply_text_normalization]
  request_body[:apply_language_text_normalization] = options[:apply_language_text_normalization] unless options[:apply_language_text_normalization].nil?
  request_body[:use_pvc_as_ivc] = options[:use_pvc_as_ivc] unless options[:use_pvc_as_ivc].nil?

  # Use streaming method with JSON parsing for timestamp data
  @client.post_streaming_with_timestamps(endpoint, request_body, &block)
end