speech_elevenlabs.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * ai_elevenlabs class
  4. *
  5. */
  6. class speech_elevenlabs implements speech_interface {
  7. private $voice;
  8. private $path;
  9. private $message;
  10. private $format;
  11. private $filename;
  12. private $languages;
  13. private $api_key;
  14. private $model;
  15. public function __construct($settings) {
  16. $this->voice = "";
  17. $this->path = "";
  18. $this->message = "";
  19. $this->format = "";
  20. $this->filename = "";
  21. //build the setting object and get the recording path
  22. $this->api_key = $settings->get('speech', 'api_key');
  23. }
  24. public function set_filename(string $audio_filename) {
  25. $this->filename = $audio_filename;
  26. }
  27. public function set_format(string $audio_format) {
  28. $this->format = $audio_format;
  29. }
  30. public function set_message(string $audio_message) {
  31. $this->message = $audio_message;
  32. }
  33. public function set_path(string $audio_path) {
  34. $this->path = $audio_path;
  35. }
  36. public function set_voice(string $audio_voice) {
  37. $this->voice = $audio_voice;
  38. }
  39. public function speech(): bool {
  40. //get the model automatically
  41. $model_id = $this->get_model();
  42. // set the request URL
  43. $url = 'https://api.elevenlabs.io/v1/text-to-speech/' . $this->voice;
  44. // set the request headers
  45. $headers[] = 'Content-Type: application/json';
  46. $headers[] = 'xi-api-key: '.$this->api_key;
  47. // set the http data
  48. $data['model_id'] = $model_id;
  49. $data['text'] = $this->message;
  50. //$data['pronunciation_dictionary_locators'][0]['pronunciation_dictionary_id'];
  51. //$data['pronunciation_dictionary_locators'][0]['version_id'];
  52. $data['voice_settings']['similarity_boost'] = 1;
  53. $data['voice_settings']['stability'] = 1;
  54. $data['voice_settings']['style'] = 0;
  55. $data['voice_settings']['use_speaker_boost'] = 'true';
  56. // initialize curl handle
  57. $ch = curl_init($url);
  58. // set the curl options
  59. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  60. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  61. curl_setopt($ch, CURLOPT_POST, true);
  62. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  63. // run the curl request and get the response
  64. $response = curl_exec($ch);
  65. // get the errors
  66. $error = curl_error($ch);
  67. // get the http code
  68. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  69. // close the handle
  70. curl_close($ch);
  71. // show the result when there is an error
  72. if ($http_code != 200) {
  73. echo "error ".$error."\n";
  74. echo "http_code ".$http_code."\n";
  75. if (strlen($response) < 500) {
  76. view_array(json_decode($response, true));
  77. }
  78. exit;
  79. }
  80. // save the audio file
  81. if ($http_code == 200) {
  82. file_put_contents($this->path.'/'.$this->filename, $response);
  83. return true;
  84. }
  85. return false;
  86. //$curl = new curl('https://api.elevenlabs.io/v1/text-to-speech/' . $this->voice);
  87. //$response = $curl->set_headers($headers)->post(json_encode($data));
  88. //$error = $curl->get_error();
  89. //$http_code = $curl->get_http_code();
  90. //if ($curl->get_http_code() == 200) {
  91. //save the audio
  92. //if ($http_code == 200) {
  93. // file_put_contents($this->path . '/' . $this->filename, $response);
  94. // return true;
  95. //}
  96. //return false;
  97. }
  98. public function is_language_enabled(): bool {
  99. return false;
  100. }
  101. public function is_model_enabled(): bool {
  102. return false;
  103. }
  104. public function get_languages(): array {
  105. return ['en' => 'English'];
  106. }
  107. public function get_voices(): array {
  108. $return_value = [];
  109. $url = 'https://api.elevenlabs.io/v1/voices';
  110. $headers = [
  111. 'Content-Type: application/json',
  112. "xi-api-key: $this->api_key",
  113. ];
  114. $curl = curl_init();
  115. curl_setopt_array($curl, [
  116. CURLOPT_URL => $url,
  117. CURLOPT_RETURNTRANSFER => true,
  118. CURLOPT_ENCODING => "",
  119. CURLOPT_MAXREDIRS => 10,
  120. CURLOPT_TIMEOUT => 30,
  121. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  122. CURLOPT_CUSTOMREQUEST => "GET",
  123. ]);
  124. $response = curl_exec($curl);
  125. $error = curl_error($curl);
  126. curl_close($curl);
  127. if (!empty($response)) {
  128. $json_array = json_decode($response, true);
  129. foreach($json_array['voices'] as $row) {
  130. $voice_id = $row['voice_id'];
  131. $name = $row['name'];
  132. $gender = $row['labels']['gender'] ?? '';
  133. $accent = $row['labels']['accent'] ?? '';
  134. $use_case = $row['labels']['use case'] ?? '';
  135. $recommended_model = $row['high_quality_base_model_ids'][0] ?? '';
  136. $return_value[$voice_id] = "$name ($gender, $accent";
  137. if (!empty($use_case)) {
  138. $return_value[$voice_id] .= ", " . $use_case;
  139. }
  140. $return_value[$voice_id] .= ")";
  141. if (!empty($recommended_model)) {
  142. $return_value[$voice_id] .= " - $recommended_model";
  143. }
  144. }
  145. }
  146. return $return_value;
  147. }
  148. public function set_language(string $audio_language) {
  149. $this->languages = $audio_language;
  150. }
  151. public function set_model(string $model): void {
  152. if (array_key_exists($model, $this->get_models())) {
  153. $this->model = $model;
  154. }
  155. }
  156. public function get_model() {
  157. //if the voice is not set return the default model
  158. if (empty($this->voice)) {
  159. return 'eleven_monolingual_v1';
  160. }
  161. //get the voices and automatically find the model
  162. $url = 'https://api.elevenlabs.io/v1/voices';
  163. $headers = [
  164. 'Content-Type: application/json',
  165. "xi-api-key: $this->api_key",
  166. ];
  167. $curl = curl_init();
  168. curl_setopt_array($curl, [
  169. CURLOPT_URL => $url,
  170. CURLOPT_RETURNTRANSFER => true,
  171. CURLOPT_ENCODING => "",
  172. CURLOPT_MAXREDIRS => 10,
  173. CURLOPT_TIMEOUT => 30,
  174. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  175. CURLOPT_CUSTOMREQUEST => "GET",
  176. ]);
  177. $response = curl_exec($curl);
  178. $error = curl_error($curl);
  179. curl_close($curl);
  180. if (!empty($response)) {
  181. $json_array = json_decode($response, true);
  182. foreach($json_array['voices'] as $row) {
  183. if ($this->voice == $row['voice_id'] && !empty($row['high_quality_base_model_ids'][0])) {
  184. return $row['high_quality_base_model_ids'][0];
  185. }
  186. }
  187. return 'eleven_monolingual_v1';
  188. }
  189. }
  190. public function get_models(): array {
  191. return [
  192. 'eleven_monolingual_v1' => 'Default',
  193. 'eleven_turbo_v1' => 'Eleven Turbo v1',
  194. 'eleven_turbo_v2' => 'Eleven Turbo v2',
  195. 'eleven_multilingual_v1' => 'Eleven Multilingual v1',
  196. 'eleven_multilingual_v2' => 'Eleven Multilingual v2',
  197. ];
  198. }
  199. }
  200. ?>