transcribe_google.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * transcribe_google class
  4. *
  5. * @method null download
  6. */
  7. if (!class_exists('transcribe_google')) {
  8. class transcribe_google implements transcribe_interface {
  9. /**
  10. * declare private variables
  11. */
  12. private $api_key;
  13. private $api_url;
  14. private $language;
  15. private $alternate_language;
  16. private $application_credentials;
  17. private $path;
  18. private $filename;
  19. private $format;
  20. private $voice;
  21. private $message;
  22. private $model;
  23. /**
  24. * called when the object is created
  25. */
  26. public function __construct($settings) {
  27. // build the setting object and get the recording path
  28. $this->api_key = $settings->get('transcribe', 'api_key');
  29. $this->api_url = $settings->get('transcribe', 'api_url');
  30. $this->language = $settings->get('transcribe', 'language');
  31. $this->alternate_language = $settings->get('transcribe', 'alternate_language');
  32. $this->application_credentials = $settings->get('transcribe', 'application_credentials');
  33. }
  34. public function set_path(string $audio_path) {
  35. $this->path = $audio_path;
  36. }
  37. public function set_filename(string $audio_filename) {
  38. $this->filename = $audio_filename;
  39. }
  40. public function set_format(string $audio_format) {
  41. $this->format = $audio_format;
  42. }
  43. public function set_voice(string $audio_voice) {
  44. $this->voice = $audio_voice;
  45. }
  46. public function set_language(string $audio_language) {
  47. $this->language = $audio_language;
  48. }
  49. public function set_translate(string $audio_translate) {
  50. $this->translate = $audio_translate;
  51. }
  52. public function set_message(string $audio_message) {
  53. $this->message = $audio_message;
  54. }
  55. public function is_language_enabled() : bool {
  56. // return the whether engine is handles languages
  57. return false;
  58. }
  59. public function is_translate_enabled() : bool {
  60. // return the whether engine is able to translate
  61. return false;
  62. }
  63. public function get_languages() : array {
  64. // create the languages array
  65. $languages = array(
  66. "af" => "Afrikaans",
  67. "ar" => "Arabic",
  68. "hy" => "Armenian",
  69. "az" => "Azerbaijani",
  70. "be" => "Belarusian",
  71. "bs" => "Bosnian",
  72. "bg" => "Bulgarian",
  73. "ca" => "Catalan",
  74. "zh" => "Chinese",
  75. "hr" => "Croatian",
  76. "cs" => "Czech",
  77. "da" => "Danish",
  78. "nl" => "Dutch",
  79. "en" => "English",
  80. "et" => "Estonian",
  81. "fi" => "Finnish",
  82. "fr" => "French",
  83. "gl" => "Galician",
  84. "de" => "German",
  85. "el" => "Greek",
  86. "he" => "Hebrew",
  87. "hi" => "Hindi",
  88. "hu" => "Hungarian",
  89. "is" => "Icelandic",
  90. "id" => "Indonesian",
  91. "it" => "Italian",
  92. "ja" => "Japanese",
  93. "kn" => "Kannada",
  94. "kk" => "Kazakh",
  95. "ko" => "Korean",
  96. "lv" => "Latvian",
  97. "lt" => "Lithuanian",
  98. "mk" => "Macedonian",
  99. "ms" => "Malay",
  100. "mr" => "Marathi",
  101. "mi" => "Maori",
  102. "ne" => "Nepali",
  103. "no" => "Norwegian",
  104. "fa" => "Persian",
  105. "pl" => "Polish",
  106. "pt" => "Portuguese",
  107. "ro" => "Romanian",
  108. "ru" => "Russian",
  109. "sr" => "Serbian",
  110. "sk" => "Slovak",
  111. "sl" => "Slovenian",
  112. "es" => "Spanish",
  113. "sw" => "Swahili",
  114. "sv" => "Swedish",
  115. "tl" => "Tagalog",
  116. "ta" => "Tamil",
  117. "th" => "Thai",
  118. "tr" => "Turkish",
  119. "uk" => "Ukrainian",
  120. "ur" => "Urdu",
  121. "vi" => "Vietnamese",
  122. "cy" => "Welsh"
  123. );
  124. // return the languages array
  125. return $languages;
  126. }
  127. /**
  128. * transcribe - speech to text
  129. */
  130. public function transcribe() : string {
  131. if (!isset($this->language) && empty($this->language)) {
  132. $this->language = 'en-US';
  133. }
  134. if (!isset($this->alternate_language) && empty($this->alternate_language)) {
  135. $this->alternate_language = 'es-US';
  136. }
  137. // get the content type
  138. $path_array = pathinfo($this->path.'/'.$this->filename);
  139. if ($path_array['extension'] == "mp3") {
  140. $content_type = 'audio/mp3';
  141. }
  142. if ($path_array['extension'] == "wav") {
  143. $content_type = 'audio/wav';
  144. }
  145. // start output buffer
  146. ob_start();
  147. $out = fopen('php://output', 'w');
  148. // version 1
  149. if (trim($this->api_url) == 'https://speech.googleapis.com/v1p1beta1/speech') {
  150. if (isset($this->api_key) && $this->api_key != '') {
  151. //get the length of the audio file
  152. $audio_length = (float)system("soxi -D ".$this->path."/".$this->filename);
  153. // Convert audio file to FLAC format
  154. $flac_file = $this->path . '/' . $this->filename . '.flac';
  155. $command = "sox ".$this->path."/".$this->filename." ".$flac_file;
  156. if ($audio_length > 59) { $command .= " trim 0 00:59"; }
  157. exec($command);
  158. // Base64 encode FLAC file
  159. $flac_base64 = base64_encode(file_get_contents($flac_file));
  160. // Prepare JSON data
  161. $data = [
  162. 'config' => [
  163. 'languageCode' => $this->language,
  164. 'enableWordTimeOffsets' => false,
  165. 'enableAutomaticPunctuation' => true,
  166. 'alternativeLanguageCodes' => $this->alternate_language
  167. ],
  168. 'audio' => [
  169. 'content' => $flac_base64
  170. ]
  171. ];
  172. $json_data = json_encode($data);
  173. // initialize a curl handle
  174. $ch = curl_init();
  175. // set the URL for the request
  176. curl_setopt($ch, CURLOPT_URL, $this->api_url . ':recognize?key=' . $this->api_key);
  177. // set the request method to POST
  178. curl_setopt($ch, CURLOPT_POST, true);
  179. // send the HTTP headers
  180. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  181. // send the HTTP post
  182. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  183. // return the response as a string instead of outputting it directly
  184. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  185. // run the curl request and transcription message
  186. $response = curl_exec($ch);
  187. // check for errors
  188. if (curl_errno($ch)) {
  189. echo 'Error: ' . curl_error($ch);
  190. exit;
  191. }
  192. // close the handle
  193. curl_close($ch);
  194. // Remove temporary FLAC file
  195. unlink($flac_file);
  196. }
  197. }
  198. // version 2
  199. if (substr($this->api_url, 0, 32) == 'https://speech.googleapis.com/v2') {
  200. if (!empty($this->application_credentials)) {
  201. putenv("GOOGLE_APPLICATION_CREDENTIALS=".$this->application_credentials);
  202. }
  203. // Base64 encode audio file
  204. $audio_base64 = base64_encode(file_get_contents($this->file_path . '/' . $this->file_name));
  205. // Prepare JSON data
  206. $data = [
  207. 'config' => [
  208. 'auto_decoding_config' => [],
  209. 'language_codes' => [$this->language],
  210. 'model' => 'long'
  211. ],
  212. 'content' => $audio_base64
  213. ];
  214. $json_data = json_encode($data);
  215. // initialize a curl handle
  216. $ch = curl_init();
  217. // set the URL for the request
  218. curl_setopt($ch, CURLOPT_URL, $this->api_url);
  219. // set the request method to POST
  220. curl_setopt($ch, CURLOPT_POST, 1);
  221. // send the HTTP headers
  222. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Authorization: Bearer ' . shell_exec('gcloud auth application-default print-access-token')]);
  223. // send the HTTP post
  224. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  225. // return the response as a string instead of outputting it directly
  226. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  227. //add verbose for debugging
  228. curl_setopt($ch, CURLOPT_VERBOSE, true);
  229. curl_setopt($ch, CURLOPT_STDERR, $out);
  230. // run the curl request and transcription message
  231. $response = curl_exec($ch);
  232. // check for errors
  233. if (curl_errno($ch)) {
  234. echo 'Error: ' . curl_error($ch);
  235. exit;
  236. }
  237. // close the handle
  238. curl_close($ch);
  239. }
  240. // validate the json
  241. if (!empty($response)) {
  242. $ob = json_decode($response);
  243. if($ob === null) {
  244. echo "invalid json\n";
  245. return false;
  246. }
  247. $json = json_decode($response, true);
  248. // echo "json; ".$json."\n";
  249. $message = '';
  250. foreach($json['results'] as $row) {
  251. $this->message .= $row['alternatives'][0]['transcript'];
  252. }
  253. }
  254. // show the debug information
  255. fclose($out);
  256. // $this->debug = ob_get_clean();
  257. // return the transcription
  258. if (empty($this->message)) {
  259. return '';
  260. }
  261. else {
  262. return $this->message;
  263. }
  264. }
  265. }
  266. }
  267. ?>