File size: 8,724 Bytes
ae8c95b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
55
56
57
58
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php

declare(strict_types=1);

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;

class ElevenLabsApi {
	private const BASE_URL = 'https://api.elevenlabs.io';

	private string $apiKey;
	private Client $client;

	/**
	 * ElevenLabsApi constructor.
	 *
	 * @param string $apiKey
	 * @param Client|null $client
	 */
	public function __construct(string $apiKey, Client $client = null) {
		$this->apiKey = $apiKey;
		$this->client = $client ?? new Client();
	}

	/**
	 * Returns metadata about all your generated audio.
	 *
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "items": [
	 *     {
	 *       "id": "VW7YKqPnjY4h39yTbx2L",
	 *       "title": "Generated Audio 1",
	 *       "duration": 120,
	 *       "created_at": "2023-03-16T08:00:00Z",
	 *       "url": "https://download-link.example.com/your_history_item_audio.mp3"
	 *     },
	 *     {
	 *       "id": "AbCDeFgH1I2jK3LmN4O5",
	 *       "title": "Generated Audio 2",
	 *       "duration": 90,
	 *       "created_at": "2023-03-15T08:00:00Z",
	 *       "url": "https://download-link.example.com/your_history_item_audio2.mp3"
	 *     }
	 *   ]
	 * }
	 */
	public function getHistory(): Response {
		$url = self::BASE_URL . '/v1/history';

		return $this->makeRequest('GET', $url);
	}

	/**
	 * Returns the audio of a history item.
	 *
	 * @param string $history_item_id The history item ID to get audio from.
	 *
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "url": "https://download-link.example.com/your_history_item_audio.mp3"
	 * }
	 */
	public function getAudioFromHistoryItem(string $historyItemId): Response {
		$url = self::BASE_URL . "/v1/history/{$historyItemId}";

		return $this->makeRequest('GET', $url);
	}

	/**
	 * Delete a number of history items by their IDs.
	 *
	 * @param array $history_item_ids An array of history item IDs to delete.
	 *
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "status": "success",
	 *   "message": "Selected history items deleted successfully."
	 * }
	 */
	public function deleteHistoryItems(array $historyItemIds): Response {
		$url = self::BASE_URL . '/v1/history/delete';
		$body = json_encode(['history_item_ids' => $historyItemIds]);

		return $this->makeRequest('POST', $url, $body);
	}

	/**
	 * Delete a history item by its ID
	 *
	 * @param string $history_item_id The ID of the history item to be deleted
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "message": "History item deleted successfully"
	 * }
	 */
	public function deleteHistoryItem(string $historyItemId): Response {
		$url = self::BASE_URL . "/v1/history/{$historyItemId}";

		return $this->makeRequest('DELETE', $url);
	}

	/**
	 * Download one or more history items.
	 *
	 * @param array $history_item_ids An array of history item IDs to download.
	 *
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "url": "https://download-link.example.com/your_downloaded_audio.zip"
	 * }
	 */
	public function downloadHistoryItems(array $historyItemIds): Response {
		$url = self::BASE_URL . '/v1/history/download';
		$body = json_encode(['history_item_ids' => $historyItemIds]);

		return $this->makeRequest('POST', $url, $body);
	}

	/**
	 * Convert text to speech
	 *
	 * @param string $text The text to be converted into speech
	 * @param array $options Optional parameters for the TTS conversion
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "history_item_id": "VW7YKqPnjY4h39yTbx2L",
	 *   "text": "Hello, world!",
	 *   "options": {
	 *     "voice": "Joanna",
	 *     "language": "en-US",
	 *     "output_format": "mp3"
	 *   },
	 *   "audio_url": "https://api.elevenlabs.io/v1/history/VW7YKqPnjY4h39yTbx2L/audio"
	 * }
	 */
	public function textToSpeech(string $voiceId, array $data): Response {
		$url = self::BASE_URL . "/v1/text-to-speech/{$voiceId}/stream";
		$body = json_encode($data);

		return $this->makeRequest('POST', $url, $body);
	}
	/**
	 * Convert text to speech with a specific Voice ID
	 *
	 * @param string $text The text to be converted into speech
	 * @param string $voiceId The ID of the voice to be used for TTS conversion
	 * @param array $options Optional parameters for the TTS conversion
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "history_item_id": "VW7YKqPnjY4h39yTbx2L",
	 *   "text": "Hello, world!",
	 *   "options": {
	 *     "voice_id": "Joanna",
	 *     "language": "en-US",
	 *     "output_format": "mp3"
	 *   },
	 *   "audio_url": "https://api.elevenlabs.io/v1/history/VW7YKqPnjY4h39yTbx2L/audio"
	 * }
	 */
	public function textToSpeechWithVoiceId(string $voiceId, array $data): Response {
		$url = self::BASE_URL . "/v1/text-to-speech/{$voiceId}";
		$body = json_encode($data);

		return $this->makeRequest('POST', $url, $body);
	}

	/**
	 * Make a request to the ElevenLabs API.
	 *
	 * @param string $method
	 * @param string $url
	 * @param string|null $body
	 * @return Response
	 */
	private function makeRequest(string $method, string $url, string $body = null): Response {
		$options = [
			'headers' => [
				'Xi-Api-Key' => $this->apiKey,
				'Content-Type' => 'application/json',
			],
		];

		if ($body !== null) {
			$options['body'] = $body;
		}

		return $this->client->request($method, $url, $options);
	}

	/**
	 * Delete a history item by its ID.
	 *
	 * @param string $history_item_id The history item ID to delete.
	 *
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "status": "success",
	 *   "message": "History item deleted successfully."
	 * }
	 */
	public function deleteSample(string $voiceId, string $sampleId): Response {
		$url = self::BASE_URL . "/v1/voices/{$voiceId}/samples/{$sampleId}";

		return $this->makeRequest('DELETE', $url);
	}

	/**
	 * Returns the audio corresponding to a sample attached to a voice.
	 *
	 * @param string $voiceId
	 * @param string $sampleId
	 * @return Response
	 * Example of a successful response (200 OK):
	 * Content-Type: audio/mpeg
	 * (Binary audio content)
	 */
	public function getAudioFromSample(string $voiceId, string $sampleId): Response {
		$url = self::BASE_URL . "/v1/voices/{$voiceId}/samples/{$sampleId}/audio";

		return $this->makeRequest('GET', $url);
	}

	/**
	 * Delete a history item by its ID.
	 *
	 * @param string $historyItemId
	 * @return Response
	 */
	public function deleteHistoryItemById(string $historyItemId): Response {
		$url = self::BASE_URL . "/v1/history/{$historyItemId}";

		return $this->makeRequest('DELETE', $url);
	}

	/**
	 * Download one or more history items.
	 *
	 * @param array $historyItemIds
	 * @return Response
	 */
	public function downloadHistoryItemsByIds(array $historyItemIds): Response {
		$url = self::BASE_URL . '/v1/history/download';
		$body = json_encode(['history_item_ids' => $historyItemIds]);

		return $this->makeRequest('POST', $url, $body);
	}

	/**
	 * Get metadata about all your generated audio.
	 *
	 * @return Response
	 *
	 * Example of a successful response (200 OK):
	 * {
	 *   "items": [
	 *     {
	 *       "id": "VW7YKqPnjY4h39yTbx2L",
	 *       "created_at": "2023-03-16T12:30:00Z",
	 *       "request": {
	 *         "text": "Hello, world!",
	 *         "voice": "en-US-Wavenet-A",
	 *         "language_code": "en-US",
	 *         "speed": 1,
	 *         "pitch": 0,
	 *         "volume_gain_db": 0
	 *       },
	 *       "duration_seconds": 2.16
	 *     },
	 *     {
	 *       "id": "yv9dA7SxQx2zG8f4Zv1m",
	 *       "created_at": "2023-03-15T14:45:00Z",
	 *       "request": {
	 *         "text": "Good morning!",
	 *         "voice": "en-US-Wavenet-B",
	 *         "language_code": "en-US",
	 *         "speed": 1,
	 *         "pitch": 0,
	 *         "volume_gain_db": 0
	 *       },
	 *       "duration_seconds": 1.8
	 *     }
	 *   ]
	 * }
	 */
	public function getGeneratedItems(): Response {
		$url = self::BASE_URL . '/v1/history';

		return $this->makeRequest('GET', $url);
	}

	/**
	 * Get the audio of a history item by its ID.
	 *
	 * @param string $historyItemId
	 * @return Response
	 */
	public function getAudioFromHistoryItemById(string $historyItemId): Response {
		$url = self::BASE_URL . "/v1/history/{$historyItemId}/audio";

		return $this->makeRequest('GET', $url);
	}

	/**
	 * Delete a number of history items by their IDs.
	 *
	 * @param array $historyItemIds
	 * @return Response
	 */
	public function deleteHistoryItemsByIds(array $historyItemIds): Response {
		$url = self::BASE_URL . '/v1/history/delete';
		$body = json_encode(['history_item_ids' => $historyItemIds]);

		return $this->makeRequest('POST', $url, $body);
	}
}