cartesia 1.3.0__py3-none-any.whl → 1.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cartesia/_types.py CHANGED
@@ -36,7 +36,6 @@ class VoiceMetadata(TypedDict):
36
36
  user_id: str
37
37
  created_at: str
38
38
  language: str
39
- base_voice_id: Optional[str] = None
40
39
 
41
40
 
42
41
  class VoiceControls(TypedDict):
@@ -62,6 +61,7 @@ class OutputFormat(TypedDict):
62
61
  container: str
63
62
  encoding: str
64
63
  sample_rate: int
64
+ bit_rate: Optional[int] = None
65
65
 
66
66
 
67
67
  class EventType:
cartesia/_websocket.py CHANGED
@@ -121,7 +121,7 @@ class _TTSContext:
121
121
  raise RuntimeError(f"Error generating audio:\n{response['error']}")
122
122
  if response["done"]:
123
123
  break
124
- if response["data"]:
124
+ if "data" in response and response["data"]:
125
125
  yield self._websocket._convert_response(
126
126
  response=response, include_context_id=True
127
127
  )
@@ -138,7 +138,7 @@ class _TTSContext:
138
138
  raise RuntimeError(f"Error generating audio:\n{response['error']}")
139
139
  if response["done"]:
140
140
  break
141
- if response["data"]:
141
+ if "data" in response and response["data"]:
142
142
  yield self._websocket._convert_response(
143
143
  response=response, include_context_id=True
144
144
  )
cartesia/async_tts.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import Iterator, List, Optional
1
+ from typing import Iterator, List, Optional, Tuple
2
2
 
3
3
  import httpx
4
4
  from cartesia._async_sse import _AsyncSSE
@@ -61,3 +61,116 @@ class AsyncTTS(TTS):
61
61
  raise ValueError(f"Failed to generate audio. Error: {response.text}")
62
62
 
63
63
  return response.content
64
+
65
+ async def infill(
66
+ self,
67
+ *,
68
+ model_id: str,
69
+ language: str,
70
+ transcript: str,
71
+ voice_id: str,
72
+ output_format: OutputFormat,
73
+ left_audio_path: Optional[str] = None,
74
+ right_audio_path: Optional[str] = None,
75
+ experimental_voice_controls: Optional[VoiceControls] = None,
76
+ ) -> Tuple[bytes, bytes]:
77
+ """Generate infill audio between two existing audio segments.
78
+
79
+ Args:
80
+ model_id: The ID of the model to use for generating audio
81
+ language: The language of the transcript
82
+ transcript: The text to synthesize
83
+ voice_id: The ID of the voice to use for generating audio
84
+ output_format: The desired audio output format
85
+ left_audio_path: Path to the audio file that comes before the infill
86
+ right_audio_path: Path to the audio file that comes after the infill
87
+ experimental_voice_controls: Optional voice control parameters
88
+
89
+ Returns:
90
+ A tuple containing:
91
+ - The generated infill audio (bytes)
92
+ - The complete concatenated audio (bytes)
93
+ """
94
+ if not left_audio_path and not right_audio_path:
95
+ raise ValueError("Must specify at least one of left_audio_path or right_audio_path")
96
+
97
+ headers = self.headers.copy()
98
+ headers.pop("Content-Type", None)
99
+
100
+ left_audio_file = None
101
+ right_audio_file = None
102
+ try:
103
+ files = {}
104
+ if left_audio_path:
105
+ left_audio_file = open(left_audio_path, "rb")
106
+ files["left_audio"] = left_audio_file
107
+ if right_audio_path:
108
+ right_audio_file = open(right_audio_path, "rb")
109
+ files["right_audio"] = right_audio_file
110
+
111
+ # Construct form data with output_format fields directly
112
+ data = {
113
+ "model_id": model_id,
114
+ "language": language,
115
+ "transcript": transcript,
116
+ "voice_id": voice_id,
117
+ "output_format[container]": output_format["container"],
118
+ "output_format[encoding]": output_format["encoding"],
119
+ "output_format[sample_rate]": output_format["sample_rate"],
120
+ }
121
+
122
+ # Add bit_rate for mp3 container
123
+ if "bit_rate" in output_format:
124
+ data["output_format[bit_rate]"] = output_format["bit_rate"]
125
+
126
+ # Add voice controls if specified
127
+ if experimental_voice_controls:
128
+ if "speed" in experimental_voice_controls:
129
+ data["voice[__experimental_controls][speed]"] = experimental_voice_controls[
130
+ "speed"
131
+ ]
132
+ if "emotion" in experimental_voice_controls:
133
+ # Pass emotions as a list instead of individual values
134
+ data["voice[__experimental_controls][emotion][]"] = experimental_voice_controls[
135
+ "emotion"
136
+ ]
137
+
138
+ async with httpx.AsyncClient() as client:
139
+ response = await client.post(
140
+ f"{self._http_url()}/infill/bytes",
141
+ headers=headers,
142
+ timeout=self.timeout,
143
+ files=files,
144
+ data=data,
145
+ )
146
+
147
+ if not response.is_success:
148
+ raise ValueError(
149
+ f"Failed to infill audio. Status Code: {response.status_code}\n"
150
+ f"Error: {response.text}"
151
+ )
152
+
153
+ if left_audio_file:
154
+ left_audio_file.seek(0)
155
+ left_audio = left_audio_file.read()
156
+ else:
157
+ left_audio = None
158
+
159
+ if right_audio_file:
160
+ right_audio_file.seek(0)
161
+ right_audio = right_audio_file.read()
162
+ else:
163
+ right_audio = None
164
+
165
+ infill_audio = response.content
166
+ format = output_format["container"].lower()
167
+ total_audio = self._concat_audio_segments(
168
+ left_audio, infill_audio, right_audio, format=format
169
+ )
170
+ return infill_audio, total_audio
171
+
172
+ finally:
173
+ if left_audio_file:
174
+ left_audio_file.close()
175
+ if right_audio_file:
176
+ right_audio_file.close()
cartesia/tts.py CHANGED
@@ -1,6 +1,9 @@
1
- from typing import Iterator, List, Optional
1
+ import json
2
+ from typing import Iterator, List, Optional, Tuple
2
3
 
3
4
  import httpx
5
+ import io
6
+ from pydub import AudioSegment
4
7
 
5
8
  from cartesia._sse import _SSE
6
9
  from cartesia._types import (
@@ -135,3 +138,155 @@ class TTS(Resource):
135
138
  ValueError: If neither or both voice_id and voice_embedding are specified.
136
139
  """
137
140
  return _validate_and_construct_voice(voice_id, voice_embedding, experimental_voice_controls)
141
+
142
+ def infill(
143
+ self,
144
+ *,
145
+ model_id: str,
146
+ language: str,
147
+ transcript: str,
148
+ voice_id: str,
149
+ output_format: OutputFormat,
150
+ left_audio_path: Optional[str] = None,
151
+ right_audio_path: Optional[str] = None,
152
+ experimental_voice_controls: Optional[VoiceControls] = None,
153
+ ) -> Tuple[bytes, bytes]:
154
+ """Generate infill audio between two existing audio segments.
155
+
156
+ Args:
157
+ model_id: The ID of the model to use for generating audio
158
+ language: The language of the transcript
159
+ transcript: The text to synthesize
160
+ voice_id: The ID of the voice to use for generating audio
161
+ output_format: The desired audio output format
162
+ left_audio_path: Path to the audio file that comes before the infill
163
+ right_audio_path: Path to the audio file that comes after the infill
164
+ experimental_voice_controls: Optional voice control parameters
165
+
166
+ Returns:
167
+ A tuple containing:
168
+ - The generated infill audio (bytes)
169
+ - The complete concatenated audio (bytes)
170
+ """
171
+ if not left_audio_path and not right_audio_path:
172
+ raise ValueError("Must specify at least one of left_audio_path or right_audio_path")
173
+
174
+ headers = self.headers.copy()
175
+ headers.pop("Content-Type", None)
176
+
177
+ left_audio_file = None
178
+ right_audio_file = None
179
+ try:
180
+ files = {}
181
+ if left_audio_path:
182
+ left_audio_file = open(left_audio_path, "rb")
183
+ files["left_audio"] = left_audio_file
184
+ if right_audio_path:
185
+ right_audio_file = open(right_audio_path, "rb")
186
+ files["right_audio"] = right_audio_file
187
+
188
+ # Construct form data with output_format fields directly
189
+ data = {
190
+ "model_id": model_id,
191
+ "language": language,
192
+ "transcript": transcript,
193
+ "voice_id": voice_id,
194
+ "output_format[container]": output_format["container"],
195
+ "output_format[encoding]": output_format["encoding"],
196
+ "output_format[sample_rate]": output_format["sample_rate"],
197
+ }
198
+
199
+ # Add bit_rate for mp3 container
200
+ if "bit_rate" in output_format:
201
+ data["output_format[bit_rate]"] = output_format["bit_rate"]
202
+
203
+ # Add voice controls if specified
204
+ if experimental_voice_controls:
205
+ if "speed" in experimental_voice_controls:
206
+ data["voice[__experimental_controls][speed]"] = experimental_voice_controls[
207
+ "speed"
208
+ ]
209
+ if "emotion" in experimental_voice_controls:
210
+ # Pass emotions as a list instead of individual values
211
+ data["voice[__experimental_controls][emotion][]"] = experimental_voice_controls[
212
+ "emotion"
213
+ ]
214
+
215
+ response = httpx.post(
216
+ f"{self._http_url()}/infill/bytes",
217
+ headers=headers,
218
+ timeout=self.timeout,
219
+ files=files,
220
+ data=data,
221
+ )
222
+
223
+ if not response.is_success:
224
+ raise ValueError(
225
+ f"Failed to infill audio. Status Code: {response.status_code}\n"
226
+ f"Error: {response.text}"
227
+ )
228
+
229
+ if left_audio_file:
230
+ left_audio_file.seek(0)
231
+ left_audio = left_audio_file.read()
232
+ else:
233
+ left_audio = None
234
+
235
+ if right_audio_file:
236
+ right_audio_file.seek(0)
237
+ right_audio = right_audio_file.read()
238
+ else:
239
+ right_audio = None
240
+
241
+ infill_audio = response.content
242
+ format = output_format["container"].lower()
243
+ total_audio = self._concat_audio_segments(
244
+ left_audio, infill_audio, right_audio, format=format
245
+ )
246
+ return infill_audio, total_audio
247
+
248
+ finally:
249
+ if left_audio_file:
250
+ left_audio_file.close()
251
+ if right_audio_file:
252
+ right_audio_file.close()
253
+
254
+ @staticmethod
255
+ def _concat_audio_segments(
256
+ left_audio: Optional[bytes],
257
+ infill_audio: bytes,
258
+ right_audio: Optional[bytes],
259
+ format: str = "wav",
260
+ ) -> bytes:
261
+ """Helper method to concatenate three audio segments while preserving audio format and headers.
262
+
263
+ Args:
264
+ left_audio: The audio segment that comes before the infill
265
+ infill_audio: The generated infill audio segment
266
+ right_audio: The audio segment that comes after the infill
267
+ format: The audio format (e.g., 'wav', 'mp3'). Defaults to 'wav'
268
+
269
+ Returns:
270
+ bytes: The concatenated audio as bytes
271
+
272
+ Raises:
273
+ ValueError: If the audio segments cannot be loaded or concatenated
274
+ """
275
+ try:
276
+ # Convert bytes to AudioSegment objects
277
+ combined = AudioSegment.empty()
278
+ if left_audio:
279
+ combined += AudioSegment.from_file(io.BytesIO(left_audio), format=format)
280
+
281
+ combined += AudioSegment.from_file(io.BytesIO(infill_audio), format=format)
282
+
283
+ if right_audio:
284
+ combined += AudioSegment.from_file(io.BytesIO(right_audio), format=format)
285
+
286
+ # Export to bytes
287
+ output = io.BytesIO()
288
+ combined.export(output, format=format)
289
+ return output.getvalue()
290
+
291
+ except Exception as e:
292
+ raise ValueError(f"Failed to concatenate audio segments: {str(e)}")
cartesia/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "1.3.0"
1
+ __version__ = "1.4.0"
cartesia/voices.py CHANGED
@@ -52,8 +52,7 @@ class Voices(Resource):
52
52
 
53
53
  if not response.is_success:
54
54
  raise ValueError(
55
- f"Failed to get voice. Status Code: {response.status_code}\n"
56
- f"Error: {response.text}"
55
+ f"Failed to get voice. Status Code: {response.status_code}\nError: {response.text}"
57
56
  )
58
57
 
59
58
  return response.json()
@@ -123,7 +122,6 @@ class Voices(Resource):
123
122
  name: str,
124
123
  description: str,
125
124
  embedding: List[float],
126
- base_voice_id: Optional[str] = None,
127
125
  language: str = "en",
128
126
  ) -> VoiceMetadata:
129
127
  """Create a new voice.
@@ -132,7 +130,6 @@ class Voices(Resource):
132
130
  name: The name of the voice.
133
131
  description: The description of the voice.
134
132
  embedding: The embedding of the voice. This should be generated with :meth:`clone`.
135
- base_voice_id: The ID of the base voice. This should be a valid voice ID if specified.
136
133
 
137
134
  Returns:
138
135
  A dictionary containing the voice metadata.
@@ -144,7 +141,6 @@ class Voices(Resource):
144
141
  "name": name,
145
142
  "description": description,
146
143
  "embedding": embedding,
147
- "base_voice_id": base_voice_id,
148
144
  "language": language,
149
145
  },
150
146
  timeout=self.timeout,
@@ -1,12 +1,14 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: cartesia
3
- Version: 1.3.0
3
+ Version: 1.4.0
4
4
  Summary: The official Python library for the Cartesia API.
5
+ License-File: LICENSE.md
5
6
  Requires-Python: >=3.9
6
7
  Requires-Dist: aiohttp>=3.10.10
7
- Requires-Dist: httpx>=0.27.2
8
+ Requires-Dist: httpx>=0.27.0
8
9
  Requires-Dist: iterators>=0.2.0
9
- Requires-Dist: requests>=2.32.3
10
+ Requires-Dist: pydub>=0.25.1
11
+ Requires-Dist: requests>=2.31.0
10
12
  Requires-Dist: websockets>=10.4
11
13
  Description-Content-Type: text/markdown
12
14
 
@@ -4,20 +4,20 @@ cartesia/_async_websocket.py,sha256=y9YL9fU8eLENZZECJUwRBVTfEx4ZMl96Y5zHaRY2BiI,
4
4
  cartesia/_constants.py,sha256=khGNVpiQVDmv1oZU7pKTd9C1AHjiaM8zQ2He9d5zI_c,435
5
5
  cartesia/_logger.py,sha256=vU7QiGSy_AJuJFmClUocqIJ-Ltku_8C24ZU8L6fLJR0,53
6
6
  cartesia/_sse.py,sha256=CugabGUAUM-N2BruxNFxDB20HyxDlRdbN-J_yAzvBMY,5667
7
- cartesia/_types.py,sha256=gixQbKbX-H8xbD7jxHmc02KXLyjEaup19lh_57_YBl8,2570
8
- cartesia/_websocket.py,sha256=nRCq9xB0T9yYHoLqtn0GsJmcap-OAlJdSIrzTl40qMI,14875
7
+ cartesia/_types.py,sha256=p0OzSzH174WrG8LRyu_MvNXZPhhTfLArSSDpcYY4xa0,2565
8
+ cartesia/_websocket.py,sha256=7gDLcfMoIwmKj07iLk5UZ4ypxlv-3UmMd3VFjVn1QaE,14921
9
9
  cartesia/async_client.py,sha256=y_K_Yuv0weA4k9ZYD0M9bNM3x3frsq07tqkg7R9h0-o,2714
10
- cartesia/async_tts.py,sha256=IbWVRKklNClXASR6ylHaukcMRR304LUguqc4yMopbDU,2076
10
+ cartesia/async_tts.py,sha256=CgbrLk7tc0NKSBC8zZH5I4CpWHpOgkypo0D2hyg5LLE,6466
11
11
  cartesia/client.py,sha256=OS1ORUSlR8Jg-em1imeTAFfwkC85AQFnw8PYtTdUuC8,2364
12
12
  cartesia/resource.py,sha256=wpnB3IPcTdxYSp0vxSkpntp4NSvqvnwUWF-0ZpgWV9o,1585
13
- cartesia/tts.py,sha256=kWvqce9K3gZ4QrWD-ciYdK29n49SNkxhd2A7ueTOwMY,4878
14
- cartesia/version.py,sha256=F5mW07pSyGrqDNY2Ehr-UpDzpBtN-FsYU0QGZWf6PJE,22
15
- cartesia/voices.py,sha256=bDYbs0KoikAROJlmbnLdo4TrW0YwzjMvp70uKG6Alp0,7180
13
+ cartesia/tts.py,sha256=WV8OduM87ciM1ht60Fi9Fh4gunX2Xew3K96ELCzpP-8,10702
14
+ cartesia/version.py,sha256=8UhoYEXHs1Oai7BW_ExBmuwWnRI-yMG_u1fQAXMizHQ,22
15
+ cartesia/voices.py,sha256=DLO_GJYDRhzFbqVIqzGOP1m1Ylzq7tVm6VHrknekFCk,6968
16
16
  cartesia/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
17
  cartesia/utils/deprecated.py,sha256=2cXvGtrxhPeUZA5LWy2n_U5OFLDv7SHeFtzqhjSJGyk,1674
18
18
  cartesia/utils/retry.py,sha256=O6fyVWpH9Su8c0Fwupl57xMt6JrwJ52txBwP3faUL7k,3339
19
19
  cartesia/utils/tts.py,sha256=TbvBZqHR6LxPim6s5RyGiURi4hIfqWt3KUk5QYOOhfc,2177
20
- cartesia-1.3.0.dist-info/METADATA,sha256=eedG5B4V6MxvDuPUMdYwp6UHrX6yQ6dJTMRRZxq1-UA,20976
21
- cartesia-1.3.0.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
22
- cartesia-1.3.0.dist-info/licenses/LICENSE.md,sha256=PT2YG5wEtEX1TNDn5sXkUXqbn-neyr7cZenTxd40ql4,1074
23
- cartesia-1.3.0.dist-info/RECORD,,
20
+ cartesia-1.4.0.dist-info/METADATA,sha256=LLv3iE6dKcAeZuSb6bDAc0GSVICUOS8szaM4n1F-Mww,21030
21
+ cartesia-1.4.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
22
+ cartesia-1.4.0.dist-info/licenses/LICENSE.md,sha256=PT2YG5wEtEX1TNDn5sXkUXqbn-neyr7cZenTxd40ql4,1074
23
+ cartesia-1.4.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.26.3
2
+ Generator: hatchling 1.27.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any