speech_elevenlabs.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 = "mp3";
  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. //get the file and path details
  83. $path_array = pathinfo($this->filename);
  84. //save the file as an mp3
  85. file_put_contents($this->path.'/'.$path_array['filename'].'.mp3', $response);
  86. //find the location of sox
  87. $command = 'which sox';
  88. $sox = system($command);
  89. //use sox to convert mp3 to wav
  90. if (file_exists($this->path.'/'.$path_array['filename'].'.mp3')) {
  91. $command = $sox . " '".$this->path."/".$path_array['filename'].".mp3' -r 44100 -b 16 '".$this->path."/".$path_array['filename'].".wav'";
  92. system($command);
  93. }
  94. //delete the extra file
  95. if (file_exists($this->path.'/'.$path_array['filename'].'.wav')) {
  96. unlink($this->path.'/'.$path_array['filename'].'.mp3');
  97. }
  98. return true;
  99. }
  100. return false;
  101. //$curl = new curl('https://api.elevenlabs.io/v1/text-to-speech/' . $this->voice);
  102. //$response = $curl->set_headers($headers)->post(json_encode($data));
  103. //$error = $curl->get_error();
  104. //$http_code = $curl->get_http_code();
  105. //if ($curl->get_http_code() == 200) {
  106. //save the audio
  107. //if ($http_code == 200) {
  108. // file_put_contents($this->path . '/' . $this->filename, $response);
  109. // return true;
  110. //}
  111. //return false;
  112. }
  113. public function is_language_enabled(): bool {
  114. return false;
  115. }
  116. public function is_model_enabled(): bool {
  117. return false;
  118. }
  119. public function get_languages(): array {
  120. return ['en' => 'English'];
  121. }
  122. public function get_voices(): array {
  123. $return_value = [];
  124. $url = 'https://api.elevenlabs.io/v1/voices';
  125. $headers = [
  126. 'Content-Type: application/json',
  127. "xi-api-key: $this->api_key",
  128. ];
  129. $curl = curl_init();
  130. curl_setopt_array($curl, [
  131. CURLOPT_URL => $url,
  132. CURLOPT_RETURNTRANSFER => true,
  133. CURLOPT_ENCODING => "",
  134. CURLOPT_MAXREDIRS => 10,
  135. CURLOPT_TIMEOUT => 30,
  136. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  137. CURLOPT_CUSTOMREQUEST => "GET",
  138. ]);
  139. $response = curl_exec($curl);
  140. $error = curl_error($curl);
  141. curl_close($curl);
  142. if (!empty($response)) {
  143. $json_array = json_decode($response, true);
  144. foreach($json_array['voices'] as $row) {
  145. $voice_id = $row['voice_id'];
  146. $name = $row['name'];
  147. $gender = $row['labels']['gender'] ?? '';
  148. $accent = $row['labels']['accent'] ?? '';
  149. $use_case = $row['labels']['use case'] ?? '';
  150. $recommended_model = $row['high_quality_base_model_ids'][0] ?? '';
  151. $return_value[$voice_id] = "$name ($gender, $accent";
  152. if (!empty($use_case)) {
  153. $return_value[$voice_id] .= ", " . $use_case;
  154. }
  155. $return_value[$voice_id] .= ")";
  156. if (!empty($recommended_model)) {
  157. $return_value[$voice_id] .= " - $recommended_model";
  158. }
  159. }
  160. }
  161. return $return_value;
  162. }
  163. public function set_language(string $audio_language) {
  164. $this->languages = $audio_language;
  165. }
  166. public function set_model(string $model): void {
  167. if (array_key_exists($model, $this->get_models())) {
  168. $this->model = $model;
  169. }
  170. }
  171. public function get_model() {
  172. //if the voice is not set return the default model
  173. if (empty($this->voice)) {
  174. return 'eleven_monolingual_v1';
  175. }
  176. //get the voices and automatically find the model
  177. $url = 'https://api.elevenlabs.io/v1/voices';
  178. $headers = [
  179. 'Content-Type: application/json',
  180. "xi-api-key: $this->api_key",
  181. ];
  182. $curl = curl_init();
  183. curl_setopt_array($curl, [
  184. CURLOPT_URL => $url,
  185. CURLOPT_RETURNTRANSFER => true,
  186. CURLOPT_ENCODING => "",
  187. CURLOPT_MAXREDIRS => 10,
  188. CURLOPT_TIMEOUT => 30,
  189. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  190. CURLOPT_CUSTOMREQUEST => "GET",
  191. ]);
  192. $response = curl_exec($curl);
  193. $error = curl_error($curl);
  194. curl_close($curl);
  195. if (!empty($response)) {
  196. $json_array = json_decode($response, true);
  197. foreach($json_array['voices'] as $row) {
  198. if ($this->voice == $row['voice_id'] && !empty($row['high_quality_base_model_ids'][0])) {
  199. return $row['high_quality_base_model_ids'][0];
  200. }
  201. }
  202. return 'eleven_monolingual_v1';
  203. }
  204. }
  205. public function get_models(): array {
  206. return [
  207. 'eleven_monolingual_v1' => 'Default',
  208. 'eleven_turbo_v1' => 'Eleven Turbo v1',
  209. 'eleven_turbo_v2' => 'Eleven Turbo v2',
  210. 'eleven_multilingual_v1' => 'Eleven Multilingual v1',
  211. 'eleven_multilingual_v2' => 'Eleven Multilingual v2',
  212. ];
  213. }
  214. }
  215. ?>