speech_openai.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 $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_key = $settings->get('speech', 'api_key');
  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. "alloy" => "alloy",
  58. "echo" => "echo",
  59. "fable" => "fable",
  60. "nova" => "nova",
  61. "onyx" => "onyx",
  62. "shimmer" => "shimmer"
  63. );
  64. //return the languages array
  65. return $voices;
  66. }
  67. public function get_languages() : array {
  68. //create the languages array
  69. $languages = array(
  70. "af" => "Afrikaans",
  71. "ar" => "Arabic",
  72. "hy" => "Armenian",
  73. "az" => "Azerbaijani",
  74. "be" => "Belarusian",
  75. "bs" => "Bosnian",
  76. "bg" => "Bulgarian",
  77. "ca" => "Catalan",
  78. "zh" => "Chinese",
  79. "hr" => "Croatian",
  80. "cs" => "Czech",
  81. "da" => "Danish",
  82. "nl" => "Dutch",
  83. "en" => "English",
  84. "et" => "Estonian",
  85. "fi" => "Finnish",
  86. "fr" => "French",
  87. "gl" => "Galician",
  88. "de" => "German",
  89. "el" => "Greek",
  90. "he" => "Hebrew",
  91. "hi" => "Hindi",
  92. "hu" => "Hungarian",
  93. "is" => "Icelandic",
  94. "id" => "Indonesian",
  95. "it" => "Italian",
  96. "ja" => "Japanese",
  97. "kn" => "Kannada",
  98. "kk" => "Kazakh",
  99. "ko" => "Korean",
  100. "lv" => "Latvian",
  101. "lt" => "Lithuanian",
  102. "mk" => "Macedonian",
  103. "ms" => "Malay",
  104. "mr" => "Marathi",
  105. "mi" => "Maori",
  106. "ne" => "Nepali",
  107. "no" => "Norwegian",
  108. "fa" => "Persian",
  109. "pl" => "Polish",
  110. "pt" => "Portuguese",
  111. "ro" => "Romanian",
  112. "ru" => "Russian",
  113. "sr" => "Serbian",
  114. "sk" => "Slovak",
  115. "sl" => "Slovenian",
  116. "es" => "Spanish",
  117. "sw" => "Swahili",
  118. "sv" => "Swedish",
  119. "tl" => "Tagalog",
  120. "ta" => "Tamil",
  121. "th" => "Thai",
  122. "tr" => "Turkish",
  123. "uk" => "Ukrainian",
  124. "ur" => "Urdu",
  125. "vi" => "Vietnamese",
  126. "cy" => "Welsh"
  127. );
  128. //return the languages array
  129. return $languages;
  130. }
  131. /**
  132. * speech - text to speech
  133. */
  134. public function speech() : bool {
  135. // set the request URL
  136. $url = 'https://api.openai.com/v1/audio/speech';
  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($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. ?>