speech_local.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * ai class
  4. *
  5. * @method null download
  6. */
  7. class speech_local implements speech_interface {
  8. /**
  9. * declare private variables
  10. */
  11. private $api_key;
  12. private $api_url;
  13. private $path;
  14. private $filename;
  15. private $format;
  16. private $voice;
  17. private $message;
  18. private $model;
  19. /**
  20. * called when the object is created
  21. */
  22. public function __construct($settings) {
  23. //build the setting object and get the recording path
  24. $this->api_url = $settings->get('speech', 'api_url', 'https://localhost:8001/speech');
  25. }
  26. public function set_path(string $audio_path) {
  27. $this->path = $audio_path;
  28. }
  29. public function set_filename(string $audio_filename) {
  30. $this->filename = $audio_filename;
  31. }
  32. public function set_format(string $audio_format) {
  33. $this->format = $audio_format;
  34. }
  35. public function set_voice(string $audio_voice) {
  36. $this->voice = $audio_voice;
  37. }
  38. public function set_language(string $audio_language) {
  39. $this->language = $audio_language;
  40. }
  41. public function set_translate(string $audio_translate) {
  42. $this->translate = $audio_translate;
  43. }
  44. public function set_message(string $audio_message) {
  45. $this->message = $audio_message;
  46. }
  47. public function is_language_enabled() : bool {
  48. //return the whether engine is handles languages
  49. return false;
  50. }
  51. public function is_translate_enabled() : bool {
  52. //return the whether engine is able to translate
  53. return false;
  54. }
  55. public function get_voices() : array {
  56. $voices = array (
  57. "af_heart" => "Heart - American Female",
  58. "af_aoede" => "Aoede - American Female",
  59. "af_bella" => "Bella - American Female",
  60. "af_alloy" => "Alloy - American Female",
  61. "af_jessica" => "Jessica - American Female",
  62. "af_kore" => "Kore - American Female",
  63. "af_nicole" => "Nicole - American Female",
  64. "af_nova" => "Nova - American Female",
  65. "af_river" => "River - American Female",
  66. "af_sarah" => "Sarah - American Female",
  67. "af_sky" => "Sky - American Female",
  68. "am_adam" => "Adam - American Male",
  69. "am_echo" => "Echo - American Male",
  70. "am_eric" => "Eric - American Male",
  71. "am_fenrir" => "Fenrir - American Male",
  72. "am_liam" => "Liam - American Male",
  73. "am_michael" => "Michael - American Male",
  74. "am_onyx" => "Onyx - American Male",
  75. "am_puck" => "Puck - American Male",
  76. "am_santa" => "Santa - American Male",
  77. "bf_alice" => "Alice - Brittish Female",
  78. "bf_emma" => "Emma - Brittish Female",
  79. "bf_isabella" => "Isabella - Brittish Female",
  80. "bf_lily" => "Lily - Brittish Female",
  81. "bm_daniel" => "Daniel - Brittish Male",
  82. "bm_fable" => "Fable - Brittish Male",
  83. "bm_george" => "George - Brittish Male",
  84. "bm_lewis" => "Lewis - Brittish Male",
  85. "ef_dora" => "Dora - Spanish Female",
  86. "em_alex" => "Alex - Spanish Male",
  87. "em_santa" => "Santa - Spanish Male"
  88. );
  89. //return the languages array
  90. return $voices;
  91. }
  92. public function get_languages() : array {
  93. //create the languages array
  94. $languages = array (
  95. "af" => "Afrikaans",
  96. "ar" => "Arabic",
  97. "hy" => "Armenian",
  98. "az" => "Azerbaijani",
  99. "be" => "Belarusian",
  100. "bs" => "Bosnian",
  101. "bg" => "Bulgarian",
  102. "ca" => "Catalan",
  103. "zh" => "Chinese",
  104. "hr" => "Croatian",
  105. "cs" => "Czech",
  106. "da" => "Danish",
  107. "nl" => "Dutch",
  108. "en" => "English",
  109. "et" => "Estonian",
  110. "fi" => "Finnish",
  111. "fr" => "French",
  112. "gl" => "Galician",
  113. "de" => "German",
  114. "el" => "Greek",
  115. "he" => "Hebrew",
  116. "hi" => "Hindi",
  117. "hu" => "Hungarian",
  118. "is" => "Icelandic",
  119. "id" => "Indonesian",
  120. "it" => "Italian",
  121. "ja" => "Japanese",
  122. "kn" => "Kannada",
  123. "kk" => "Kazakh",
  124. "ko" => "Korean",
  125. "lv" => "Latvian",
  126. "lt" => "Lithuanian",
  127. "mk" => "Macedonian",
  128. "ms" => "Malay",
  129. "mr" => "Marathi",
  130. "mi" => "Maori",
  131. "ne" => "Nepali",
  132. "no" => "Norwegian",
  133. "fa" => "Persian",
  134. "pl" => "Polish",
  135. "pt" => "Portuguese",
  136. "ro" => "Romanian",
  137. "ru" => "Russian",
  138. "sr" => "Serbian",
  139. "sk" => "Slovak",
  140. "sl" => "Slovenian",
  141. "es" => "Spanish",
  142. "sw" => "Swahili",
  143. "sv" => "Swedish",
  144. "tl" => "Tagalog",
  145. "ta" => "Tamil",
  146. "th" => "Thai",
  147. "tr" => "Turkish",
  148. "uk" => "Ukrainian",
  149. "ur" => "Urdu",
  150. "vi" => "Vietnamese",
  151. "cy" => "Welsh"
  152. );
  153. //return the languages array
  154. return $languages;
  155. }
  156. /**
  157. * speech - text to speech
  158. */
  159. public function speech() : bool {
  160. // set the request headers
  161. $headers = [
  162. 'Content-Type: application/json'
  163. ];
  164. // set the http data
  165. $data['model'] = 'tts-1-hd';
  166. $data['input'] = $this->message;
  167. $data['voice'] = $this->voice;
  168. //$data['response_format'] = 'wav';
  169. //if (isset($this->language)) {
  170. // $data['language'] = $this->language;
  171. //}
  172. //if (isset($this->translate)) {
  173. // $data['task'] = 'translate';
  174. //}
  175. // initialize curl handle
  176. $ch = curl_init($this->api_url);
  177. // set the curl options
  178. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  179. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  180. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  181. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  182. // run the curl request and get the response
  183. $response = curl_exec($ch);
  184. // close the handle
  185. curl_close($ch);
  186. // check for errors
  187. if ($response === false) {
  188. return false;
  189. }
  190. else {
  191. // save the audio file
  192. //if (file_exists($this->path.'/'.$this->filename)) {
  193. // unlink($this->path.'/'.$this->filename);
  194. //}
  195. file_put_contents($this->path.'/'.$this->filename, $response);
  196. return true;
  197. }
  198. }
  199. public function set_model(string $model): void {
  200. if (array_key_exists($model, $this->get_models())) {
  201. $this->model = $model;
  202. }
  203. }
  204. public function get_models(): array {
  205. return [
  206. 'tts-1-hd' => 'tts-1-hd'
  207. ];
  208. }
  209. }