speech_openai.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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', 'https://api.openai.com/v1/audio/speech');
  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. "ash" => "ash",
  61. "coral" => "coral",
  62. "echo" => "echo",
  63. "fable" => "fable",
  64. "nova" => "nova",
  65. "onyx" => "onyx",
  66. "sage" => "sage",
  67. "shimmer" => "shimmer"
  68. );
  69. //return the languages array
  70. return $voices;
  71. }
  72. public function get_languages() : array {
  73. //create the languages array
  74. $languages = array(
  75. "af" => "Afrikaans",
  76. "ar" => "Arabic",
  77. "hy" => "Armenian",
  78. "az" => "Azerbaijani",
  79. "be" => "Belarusian",
  80. "bs" => "Bosnian",
  81. "bg" => "Bulgarian",
  82. "ca" => "Catalan",
  83. "zh" => "Chinese",
  84. "hr" => "Croatian",
  85. "cs" => "Czech",
  86. "da" => "Danish",
  87. "nl" => "Dutch",
  88. "en" => "English",
  89. "et" => "Estonian",
  90. "fi" => "Finnish",
  91. "fr" => "French",
  92. "gl" => "Galician",
  93. "de" => "German",
  94. "el" => "Greek",
  95. "he" => "Hebrew",
  96. "hi" => "Hindi",
  97. "hu" => "Hungarian",
  98. "is" => "Icelandic",
  99. "id" => "Indonesian",
  100. "it" => "Italian",
  101. "ja" => "Japanese",
  102. "kn" => "Kannada",
  103. "kk" => "Kazakh",
  104. "ko" => "Korean",
  105. "lv" => "Latvian",
  106. "lt" => "Lithuanian",
  107. "mk" => "Macedonian",
  108. "ms" => "Malay",
  109. "mr" => "Marathi",
  110. "mi" => "Maori",
  111. "ne" => "Nepali",
  112. "no" => "Norwegian",
  113. "fa" => "Persian",
  114. "pl" => "Polish",
  115. "pt" => "Portuguese",
  116. "ro" => "Romanian",
  117. "ru" => "Russian",
  118. "sr" => "Serbian",
  119. "sk" => "Slovak",
  120. "sl" => "Slovenian",
  121. "es" => "Spanish",
  122. "sw" => "Swahili",
  123. "sv" => "Swedish",
  124. "tl" => "Tagalog",
  125. "ta" => "Tamil",
  126. "th" => "Thai",
  127. "tr" => "Turkish",
  128. "uk" => "Ukrainian",
  129. "ur" => "Urdu",
  130. "vi" => "Vietnamese",
  131. "cy" => "Welsh"
  132. );
  133. //return the languages array
  134. return $languages;
  135. }
  136. /**
  137. * speech - text to speech
  138. */
  139. public function speech() : bool {
  140. // set the request headers
  141. $headers = [
  142. 'Authorization: Bearer ' . $this->api_key,
  143. 'Content-Type: application/json'
  144. ];
  145. // set the http data
  146. $data['model'] = 'tts-1-hd';
  147. $data['input'] = $this->message;
  148. $data['voice'] = $this->voice;
  149. $data['response_format'] = 'wav';
  150. if (isset($this->language)) {
  151. $data['language'] = $this->language;
  152. }
  153. if (isset($this->translate)) {
  154. $data['task'] = 'translate';
  155. }
  156. // initialize curl handle
  157. $ch = curl_init($this->api_url);
  158. // set the curl options
  159. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  160. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  161. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  162. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  163. // run the curl request and get the response
  164. $response = curl_exec($ch);
  165. // close the handle
  166. curl_close($ch);
  167. // check for errors
  168. if ($response === false) {
  169. return false;
  170. }
  171. else {
  172. // save the audio file
  173. file_put_contents($this->path.'/'.$this->filename, $response);
  174. return true;
  175. }
  176. }
  177. public function set_model(string $model): void {
  178. if (array_key_exists($model, $this->get_models())) {
  179. $this->model = $model;
  180. }
  181. }
  182. public function get_models(): array {
  183. return [
  184. 'tts-1-hd' => 'tts-1-hd'
  185. ];
  186. }
  187. }
  188. }
  189. ?>