transcribe_azure.com 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * transcribe_azure class
  4. *
  5. * @method null download
  6. */
  7. if (!class_exists('transcribe_azure')) {
  8. class transcribe_azure implements transcribe_interface {
  9. /**
  10. * declare private variables
  11. */
  12. private $api_key;
  13. private $api_url;
  14. private $language;
  15. private $path;
  16. private $filename;
  17. private $format;
  18. private $voice;
  19. private $message;
  20. private $model;
  21. /**
  22. * called when the object is created
  23. */
  24. public function __construct($settings) {
  25. //build the setting object and get the recording path
  26. $this->api_key = $settings->get('transcribe', 'api_key');
  27. $this->api_url = $settings->get('transcribe', 'api_url');
  28. $this->language = $settings->get('transcribe', 'language');
  29. }
  30. public function set_path(string $audio_path) {
  31. $this->path = $audio_path;
  32. }
  33. public function set_filename(string $audio_filename) {
  34. $this->filename = $audio_filename;
  35. }
  36. public function set_format(string $audio_format) {
  37. $this->format = $audio_format;
  38. }
  39. public function set_voice(string $audio_voice) {
  40. $this->voice = $audio_voice;
  41. }
  42. public function set_language(string $audio_language) {
  43. $this->language = $audio_language;
  44. }
  45. public function set_translate(string $audio_translate) {
  46. $this->translate = $audio_translate;
  47. }
  48. public function set_message(string $audio_message) {
  49. $this->message = $audio_message;
  50. }
  51. public function is_language_enabled() : bool {
  52. //return the whether engine is handles languages
  53. return false;
  54. }
  55. public function is_translate_enabled() : bool {
  56. //return the whether engine is able to translate
  57. return false;
  58. }
  59. public function get_languages() : array {
  60. //create the languages array
  61. $languages = array(
  62. "af" => "Afrikaans",
  63. "ar" => "Arabic",
  64. "hy" => "Armenian",
  65. "az" => "Azerbaijani",
  66. "be" => "Belarusian",
  67. "bs" => "Bosnian",
  68. "bg" => "Bulgarian",
  69. "ca" => "Catalan",
  70. "zh" => "Chinese",
  71. "hr" => "Croatian",
  72. "cs" => "Czech",
  73. "da" => "Danish",
  74. "nl" => "Dutch",
  75. "en" => "English",
  76. "et" => "Estonian",
  77. "fi" => "Finnish",
  78. "fr" => "French",
  79. "gl" => "Galician",
  80. "de" => "German",
  81. "el" => "Greek",
  82. "he" => "Hebrew",
  83. "hi" => "Hindi",
  84. "hu" => "Hungarian",
  85. "is" => "Icelandic",
  86. "id" => "Indonesian",
  87. "it" => "Italian",
  88. "ja" => "Japanese",
  89. "kn" => "Kannada",
  90. "kk" => "Kazakh",
  91. "ko" => "Korean",
  92. "lv" => "Latvian",
  93. "lt" => "Lithuanian",
  94. "mk" => "Macedonian",
  95. "ms" => "Malay",
  96. "mr" => "Marathi",
  97. "mi" => "Maori",
  98. "ne" => "Nepali",
  99. "no" => "Norwegian",
  100. "fa" => "Persian",
  101. "pl" => "Polish",
  102. "pt" => "Portuguese",
  103. "ro" => "Romanian",
  104. "ru" => "Russian",
  105. "sr" => "Serbian",
  106. "sk" => "Slovak",
  107. "sl" => "Slovenian",
  108. "es" => "Spanish",
  109. "sw" => "Swahili",
  110. "sv" => "Swedish",
  111. "tl" => "Tagalog",
  112. "ta" => "Tamil",
  113. "th" => "Thai",
  114. "tr" => "Turkish",
  115. "uk" => "Ukrainian",
  116. "ur" => "Urdu",
  117. "vi" => "Vietnamese",
  118. "cy" => "Welsh"
  119. );
  120. //return the languages array
  121. return $languages;
  122. }
  123. /**
  124. * transcribe - speech to text
  125. */
  126. public function transcribe() : string {
  127. //get the content type
  128. $path_array = pathinfo($this->path.'/'.$this->filename);
  129. if ($path_array['extension'] == "mp3") {
  130. $content_type = 'audio/mp3';
  131. }
  132. if ($path_array['extension'] == "wav") {
  133. $content_type = 'audio/wav';
  134. }
  135. //start output buffer
  136. ob_start();
  137. $out = fopen('php://output', 'w');
  138. if (isset($api_key) && $api_key != '') {
  139. $url = "https://" . $this->api_url . ".api.cognitive.microsoft.com/sts/v1.0/issueToken";
  140. $headers = [
  141. "Content-type: application/x-www-form-urlencoded",
  142. "Content-Length: 0",
  143. "Ocp-Apim-Subscription-Key: " . $this->api_key
  144. ];
  145. // initialize a curl handle
  146. $ch = curl_init($url);
  147. //set the request method to POST
  148. curl_setopt($ch, CURLOPT_POST, true);
  149. //send the http headers
  150. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  151. //return the response as a string instead of outputting it directly
  152. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  153. //run the curl request to get the access token
  154. $access_token = curl_exec($ch);
  155. //close the handle
  156. curl_close($ch);
  157. //if a token was returned then use it to make the transcribe request
  158. if (empty($access_token)) {
  159. return false;
  160. }
  161. else {
  162. $url = "https://" . $this->api_url . ".stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=" . $this->language . "&format=detailed";
  163. $file_path = $file_path . '/' . $file_name;
  164. $headers = [
  165. "Authorization: Bearer " . $access_token,
  166. "Content-type: audio/wav; codec=\"audio/pcm\"; samplerate=8000; trustsourcerate=false"
  167. ];
  168. //initialize a curl handle
  169. $ch = curl_init($url);
  170. //set the request method to POST
  171. curl_setopt($ch, CURLOPT_POST, true);
  172. //send the http headers
  173. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  174. //send the file using
  175. curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_path));
  176. //return the response as a string instead of outputting it directly
  177. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  178. //add verbose for debugging
  179. curl_setopt($ch, CURLOPT_VERBOSE, true);
  180. curl_setopt($ch, CURLOPT_STDERR, $out);
  181. //run the curl request to transcribe the message
  182. $json_response = curl_exec($ch);
  183. //check for errors
  184. if (curl_errno($ch)) {
  185. echo 'Error: ' . curl_error($ch);
  186. exit;
  187. }
  188. //close the handle
  189. curl_close($ch);
  190. //convert the json to an a
  191. $array = json_decode($json_response, true);
  192. //validate the json
  193. if ($array === null) {
  194. return 'invalid json';
  195. }
  196. else {
  197. $this->message = $array['NBest'][0]['Display'];
  198. }
  199. }
  200. }
  201. //return the transcription
  202. if (empty($this->message)) {
  203. return '';
  204. }
  205. else {
  206. return $this->message;
  207. }
  208. }
  209. }
  210. }
  211. ?>