Class: ElevenlabsClient::SpeechToSpeech

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SpeechToSpeech

Returns a new instance of SpeechToSpeech.



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

def initialize(client)
  @client = client
end

Instance Method Details

#convert(voice_id, audio_file, filename, **options) ⇒ String Also known as: voice_changer

POST /v1/speech-to-speech/:voice_id Transform audio from one voice to another. Maintain full control over emotion, timing and delivery. Documentation: https://elevenlabs.io/docs/api-reference/speech-to-speech

Parameters:

  • voice_id (String)

    ID of the voice to be used

  • audio_file (IO, File)

    The audio file which holds the content and emotion

  • filename (String)

    Original filename for the audio file

  • options (Hash)

    Optional parameters

Options Hash (**options):

  • :enable_logging (Boolean)

    Enable logging (default: true)

  • :optimize_streaming_latency (Integer)

    Latency optimization level (0-4, deprecated)

  • :output_format (String)

    Output format (default: "mp3_44100_128")

  • :model_id (String)

    Model identifier (default: "eleven_english_sts_v2")

  • :voice_settings (String)

    JSON encoded voice settings

  • :seed (Integer)

    Deterministic sampling seed (0-4294967295)

  • :remove_background_noise (Boolean)

    Remove background noise (default: false)

  • :file_format (String)

    Input file format ("pcm_s16le_16" or "other")

Returns:

  • (String)

    Binary audio data



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/elevenlabs_client/endpoints/speech_to_speech.rb', line 26

def convert(voice_id, audio_file, filename, **options)
  endpoint = "/v1/speech-to-speech/#{voice_id}"
  
  # 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]
  
  # Add query parameters to endpoint if any exist
  if query_params.any?
    query_string = query_params.map { |k, v| "#{k}=#{v}" }.join("&")
    endpoint += "?#{query_string}"
  end

  # Build multipart payload
  payload = {
    audio: @client.file_part(audio_file, filename)
  }
  
  # Add optional form parameters
  payload[:model_id] = options[:model_id] if options[:model_id]
  payload[:voice_settings] = options[:voice_settings] if options[:voice_settings]
  payload[:seed] = options[:seed] if options[:seed]
  payload[:remove_background_noise] = options[:remove_background_noise] unless options[:remove_background_noise].nil?
  payload[:file_format] = options[:file_format] if options[:file_format]

  @client.post_multipart(endpoint, payload)
end

#convert_stream(voice_id, audio_file, filename, **options, &block) ⇒ Faraday::Response Also known as: voice_changer_stream

POST /v1/speech-to-speech/:voice_id/stream Stream audio from one voice to another. Maintain full control over emotion, timing and delivery. Documentation: https://elevenlabs.io/docs/api-reference/speech-to-speech/stream

Parameters:

  • voice_id (String)

    ID of the voice to be used

  • audio_file (IO, File)

    The audio file which holds the content and emotion

  • filename (String)

    Original filename for the audio file

  • options (Hash)

    Optional parameters

  • block (Proc)

    Block to handle each chunk of streaming audio data

Options Hash (**options):

  • :enable_logging (Boolean)

    Enable logging (default: true)

  • :optimize_streaming_latency (Integer)

    Latency optimization level (0-4, deprecated)

  • :output_format (String)

    Output format (default: "mp3_44100_128")

  • :model_id (String)

    Model identifier (default: "eleven_english_sts_v2")

  • :voice_settings (String)

    JSON encoded voice settings

  • :seed (Integer)

    Deterministic sampling seed (0-4294967295)

  • :remove_background_noise (Boolean)

    Remove background noise (default: false)

  • :file_format (String)

    Input file format ("pcm_s16le_16" or "other")

Returns:

  • (Faraday::Response)

    Response object for streaming



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/elevenlabs_client/endpoints/speech_to_speech.rb', line 74

def convert_stream(voice_id, audio_file, filename, **options, &block)
  endpoint = "/v1/speech-to-speech/#{voice_id}/stream"
  
  # 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]
  
  # Add query parameters to endpoint if any exist
  if query_params.any?
    query_string = query_params.map { |k, v| "#{k}=#{v}" }.join("&")
    endpoint += "?#{query_string}"
  end

  # Build multipart payload
  payload = {
    audio: @client.file_part(audio_file, filename)
  }
  
  # Add optional form parameters
  payload[:model_id] = options[:model_id] if options[:model_id]
  payload[:voice_settings] = options[:voice_settings] if options[:voice_settings]
  payload[:seed] = options[:seed] if options[:seed]
  payload[:remove_background_noise] = options[:remove_background_noise] unless options[:remove_background_noise].nil?
  payload[:file_format] = options[:file_format] if options[:file_format]

  # Use streaming multipart request
  response = @client.instance_variable_get(:@conn).post(endpoint) do |req|
    req.headers["xi-api-key"] = @client.api_key
    req.body = payload
    
    # Set up streaming callback if block provided
    if block_given?
      req.options.on_data = proc do |chunk, _|
        block.call(chunk)
      end
    end
  end

  @client.send(:handle_response, response)
end