speech_openai.php 4.6 KB

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