Class: Typecast::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/typecast/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.typecast.ai".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV["TYPECAST_API_KEY"], base_url: ENV["TYPECAST_API_HOST"] || DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
# File 'lib/typecast/client.rb', line 19

def initialize(api_key: ENV["TYPECAST_API_KEY"], base_url: ENV["TYPECAST_API_HOST"] || DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30)
  @api_key = api_key.to_s.strip
  @base_url = normalize_base_url(base_url)
  validate_api_key!
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



17
18
19
# File 'lib/typecast/client.rb', line 17

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



17
18
19
# File 'lib/typecast/client.rb', line 17

def base_url
  @base_url
end

Instance Method Details

#clone_voice(audio:, filename:, name:, model:) ⇒ Object



94
95
96
97
98
# File 'lib/typecast/client.rb', line 94

def clone_voice(audio:, filename:, name:, model:)
  validate_clone_inputs(audio, name)
  response = request_multipart("/v1/voices/clone", audio: audio, filename: filename, fields: { name: name, model: model })
  Models::CustomVoice.from_h(JSON.parse(response.body))
end

#compose_speechObject



36
37
38
# File 'lib/typecast/client.rb', line 36

def compose_speech
  SpeechComposer.new(method(:text_to_speech))
end

#delete_voice(voice_id) ⇒ Object



100
101
102
103
# File 'lib/typecast/client.rb', line 100

def delete_voice(voice_id)
  request_raw(:delete, "/v1/voices/#{path_segment(voice_id)}")
  nil
end

#generate_to_file(path, text:, voice_id:, model: Models::TTS_MODEL_V30, language: nil, prompt: nil, output: nil, seed: nil) ⇒ Object

Browse available API voices at typecast.ai/developers/api/voices.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/typecast/client.rb', line 41

def generate_to_file(path, text:, voice_id:, model: Models::TTS_MODEL_V30, language: nil, prompt: nil, output: nil, seed: nil)
  request = Models::TTSRequest.new(
    voice_id: voice_id,
    text: text,
    model: model,
    language: language,
    prompt: prompt,
    output: output || inferred_output(path),
    seed: seed
  )
  response = text_to_speech(request)
  File.binwrite(path, response.audio_data)
  response
end

#get_my_subscriptionObject



74
75
76
# File 'lib/typecast/client.rb', line 74

def get_my_subscription
  Models::SubscriptionResponse.from_h(JSON.parse(request_json(:get, "/v1/users/me/subscription").body))
end

#get_voice_v2(voice_id) ⇒ Object

Raises:



85
86
87
88
89
90
91
92
# File 'lib/typecast/client.rb', line 85

def get_voice_v2(voice_id)
  voices = JSON.parse(request_json(:get, "/v2/voices/#{path_segment(voice_id)}").body).map do |item|
    Models::VoiceV2.from_h(item)
  end
  raise NotFoundError, "Voice not found: #{voice_id}" if voices.empty?

  voices.first
end

#get_voices_v2(filter = nil) ⇒ Object



78
79
80
81
82
83
# File 'lib/typecast/client.rb', line 78

def get_voices_v2(filter = nil)
  query = filter.respond_to?(:to_h) ? filter.to_h : filter
  JSON.parse(request_json(:get, "/v2/voices", nil, query).body).map do |item|
    Models::VoiceV2.from_h(item)
  end
end

#text_to_speech(request) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/typecast/client.rb', line 27

def text_to_speech(request)
  response = request_json(:post, "/v1/text-to-speech", request.to_h)
  Models::TTSResponse.new(
    audio_data: response.body,
    duration: response["X-Audio-Duration"].to_f,
    format: response["Content-Type"].to_s.include?("mp3") || response["Content-Type"].to_s.include?("mpeg") ? Models::AUDIO_MP3 : Models::AUDIO_WAV
  )
end

#text_to_speech_stream(request) {|response.body| ... } ⇒ Object

Yields:

  • (response.body)


56
57
58
59
60
61
# File 'lib/typecast/client.rb', line 56

def text_to_speech_stream(request)
  response = request_json(:post, "/v1/text-to-speech/stream", request.to_h)
  return enum_for(:text_to_speech_stream, request) unless block_given?

  yield response.body
end

#text_to_speech_with_timestamps(request, granularity: nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/typecast/client.rb', line 63

def text_to_speech_with_timestamps(request, granularity: nil)
  unless granularity.nil? || %w[word char].include?(granularity)
    raise ArgumentError, "granularity must be 'word' or 'char'"
  end

  path = "/v1/text-to-speech/with-timestamps"
  query = granularity.nil? ? nil : { granularity: granularity }
  response = request_json(:post, path, request.to_h, query)
  Models::TTSWithTimestampsResponse.from_h(JSON.parse(response.body))
end