Browse Source

Remove class_exists and interface_exists conditions

FusionPBX 1 week ago
parent
commit
4b44df8b0a

+ 71 - 76
resources/classes/transcribe.php

@@ -1,92 +1,87 @@
 <?php
 
 /**
- * transcribe class
- *
- * @method null download
- */
-if (!class_exists('transcribe')) {
-	class transcribe {
-
-		/**
-		 * declare private variables
-		 */
-		private $api_key;
-
-		/** @var string $engine */
-		private $engine;
-
-		/** @var template_engine $object */
-		private $transcribe_object;
-
-		private $settings;
-
-		public $audio_path;
-		public $audio_filename;
-		public $audio_string;
-		public $audio_mime_type;
-		public $audio_format;
-		public $audio_model;
-		public $audio_voice;
-		public $audio_language;
-		public $audio_message;
-
-		/**
-		 * called when the object is created
-		 */
-		public function __construct(settings $settings = null) {
-			//make the setting object
-			if ($settings === null) {
-				$settings = new settings();
-			}
+* transcribe class
+*
+*/
+class transcribe {
+
+	/**
+	 * declare private variables
+	 */
+	private $api_key;
+
+	/** @var string $engine */
+	private $engine;
+
+	/** @var template_engine $object */
+	private $transcribe_object;
+
+	private $settings;
+
+	public $audio_path;
+	public $audio_filename;
+	public $audio_string;
+	public $audio_mime_type;
+	public $audio_format;
+	public $audio_model;
+	public $audio_voice;
+	public $audio_language;
+	public $audio_message;
+
+	/**
+	 * called when the object is created
+	 */
+	public function __construct(settings $settings = null) {
+		//make the setting object
+		if ($settings === null) {
+			$settings = new settings();
+		}
 
-			//add the settings object to the class
-			$this->settings = $settings;
+		//add the settings object to the class
+		$this->settings = $settings;
 
-			//build the setting object and get the recording path
-			$this->api_key = $settings->get('transcribe', 'api_key');
-			$this->engine = $settings->get('transcribe', 'engine');
-		}
+		//build the setting object and get the recording path
+		$this->api_key = $settings->get('transcribe', 'api_key');
+		$this->engine = $settings->get('transcribe', 'engine');
+	}
 
-		/**
-		 * transcribe - speech to text
-		 */
-		public function transcribe() : string {
+	/**
+	 * transcribe - speech to text
+	 */
+	public function transcribe() : string {
 
-			if (!empty($this->engine)) {
-				//set the class interface to use the _template suffix
-				$classname = 'transcribe_'.$this->engine;
+		if (!empty($this->engine)) {
+			//set the class interface to use the _template suffix
+			$classname = 'transcribe_'.$this->engine;
 
-				if (empty($this->audio_path)) {
-					$this->audio_path = null;
-				}
+			if (empty($this->audio_path)) {
+				$this->audio_path = null;
+			}
 
-				//create the object
-				$object = new $classname($this->settings);
-
-				//ensure the class has implemented the audio_interface interface
-				if ($object instanceof transcribe_interface) {
-					if ($object->is_language_enabled() && !empty($this->audio_language)) {
-						$object->set_language($this->audio_language);
-					}
-					if (!empty($this->audio_string)) {
-						$object->set_audio_string($this->audio_string);
-					}
-					if (!empty($this->audio_mime_type)) {
-						$object->set_audio_mime_type($this->audio_mime_type);
-					}
-					$object->set_path($this->audio_path);
-					$object->set_filename($this->audio_filename);
-					return $object->transcribe();
+			//create the object
+			$object = new $classname($this->settings);
+
+			//ensure the class has implemented the audio_interface interface
+			if ($object instanceof transcribe_interface) {
+				if ($object->is_language_enabled() && !empty($this->audio_language)) {
+					$object->set_language($this->audio_language);
+				}
+				if (!empty($this->audio_string)) {
+					$object->set_audio_string($this->audio_string);
 				}
-				else {
-					return '';
+				if (!empty($this->audio_mime_type)) {
+					$object->set_audio_mime_type($this->audio_mime_type);
 				}
+				$object->set_path($this->audio_path);
+				$object->set_filename($this->audio_filename);
+				return $object->transcribe();
+			}
+			else {
+				return '';
 			}
-
 		}
 
 	}
-}
 
-?>
+}

+ 226 - 232
resources/classes/transcribe_azure.php

@@ -1,186 +1,213 @@
 <?php
 
+/**
+* transcribe_azure class
+*
+*/
+class transcribe_azure implements transcribe_interface {
+
+	/**
+	 * declare private variables
+	 */
+	private $api_key;
+	private $api_url;
+	private $language;
+	private $path;
+	private $filename;
+	private $audio_string;
+	private $audio_mime_type;
+	private $format;
+	private $voice;
+	private $message;
+	private $model;
+
+	/**
+	 * called when the object is created
+	 */
+	public function __construct($settings) {
+
+		//build the setting object and get the recording path
+		$this->api_key = $settings->get('transcribe', 'api_key');
+		$this->api_url = $settings->get('transcribe', 'api_url');
+		$this->language = $settings->get('transcribe', 'language');
 
- /**
- * transcribe_azure class
- *
- * @method null download
- */
-if (!class_exists('transcribe_azure')) {
-	class transcribe_azure implements transcribe_interface {
-
-		/**
-		 * declare private variables
-		 */
-		private $api_key;
-		private $api_url;
-		private $language;
-		private $path;
-		private $filename;
-		private $audio_string;
-		private $audio_mime_type;
-		private $format;
-		private $voice;
-		private $message;
-		private $model;
-
-		/**
-		 * called when the object is created
-		 */
-		public function __construct($settings) {
-
-			//build the setting object and get the recording path
-			$this->api_key = $settings->get('transcribe', 'api_key');
-			$this->api_url = $settings->get('transcribe', 'api_url');
-			$this->language = $settings->get('transcribe', 'language');
-
-		}
+	}
 
-		public function set_path(string $audio_path) {
-			$this->path = $audio_path;
-		}
+	public function set_path(string $audio_path) {
+		$this->path = $audio_path;
+	}
 
-		public function set_filename(string $audio_filename) {
-			$this->filename = $audio_filename;
-		}
+	public function set_filename(string $audio_filename) {
+		$this->filename = $audio_filename;
+	}
 
-		public function set_audio_string(string $audio_string) {
-			$this->audio_string = $audio_string;
-		}
+	public function set_audio_string(string $audio_string) {
+		$this->audio_string = $audio_string;
+	}
 
-		public function set_audio_mime_type(string $audio_mime_type) {
-			$this->audio_mime_type = $audio_mime_type;
-		}
+	public function set_audio_mime_type(string $audio_mime_type) {
+		$this->audio_mime_type = $audio_mime_type;
+	}
 
-		public function set_format(string $audio_format) {
-			$this->format = $audio_format;
-		}
+	public function set_format(string $audio_format) {
+		$this->format = $audio_format;
+	}
 
-		public function set_voice(string $audio_voice) {
-			$this->voice = $audio_voice;
-		}
+	public function set_voice(string $audio_voice) {
+		$this->voice = $audio_voice;
+	}
 
-		public function set_language(string $audio_language) {
-			$this->language = $audio_language;
-		}
+	public function set_language(string $audio_language) {
+		$this->language = $audio_language;
+	}
 
-		public function set_translate(string $audio_translate) {
-			$this->translate = $audio_translate;
-		}
+	public function set_translate(string $audio_translate) {
+		$this->translate = $audio_translate;
+	}
 
-		public function set_message(string $audio_message) {
-			$this->message = $audio_message;
-		}
+	public function set_message(string $audio_message) {
+		$this->message = $audio_message;
+	}
 
-		public function is_language_enabled() : bool {
-			//return the whether engine is handles languages
-			return false;
-		}
+	public function is_language_enabled() : bool {
+		//return the whether engine is handles languages
+		return false;
+	}
 
-		public function is_translate_enabled() : bool {
-			//return the whether engine is able to translate
-			return false;
-		}
+	public function is_translate_enabled() : bool {
+		//return the whether engine is able to translate
+		return false;
+	}
 
-		public function get_languages() : array {
-			//create the languages array
-			$languages = array(
-				"af" => "Afrikaans",
-				"ar" => "Arabic",
-				"hy" => "Armenian",
-				"az" => "Azerbaijani",
-				"be" => "Belarusian",
-				"bs" => "Bosnian",
-				"bg" => "Bulgarian",
-				"ca" => "Catalan",
-				"zh" => "Chinese",
-				"hr" => "Croatian",
-				"cs" => "Czech",
-				"da" => "Danish",
-				"nl" => "Dutch",
-				"en" => "English",
-				"et" => "Estonian",
-				"fi" => "Finnish",
-				"fr" => "French",
-				"gl" => "Galician",
-				"de" => "German",
-				"el" => "Greek",
-				"he" => "Hebrew",
-				"hi" => "Hindi",
-				"hu" => "Hungarian",
-				"is" => "Icelandic",
-				"id" => "Indonesian",
-				"it" => "Italian",
-				"ja" => "Japanese",
-				"kn" => "Kannada",
-				"kk" => "Kazakh",
-				"ko" => "Korean",
-				"lv" => "Latvian",
-				"lt" => "Lithuanian",
-				"mk" => "Macedonian",
-				"ms" => "Malay",
-				"mr" => "Marathi",
-				"mi" => "Maori",
-				"ne" => "Nepali",
-				"no" => "Norwegian",
-				"fa" => "Persian",
-				"pl" => "Polish",
-				"pt" => "Portuguese",
-				"ro" => "Romanian",
-				"ru" => "Russian",
-				"sr" => "Serbian",
-				"sk" => "Slovak",
-				"sl" => "Slovenian",
-				"es" => "Spanish",
-				"sw" => "Swahili",
-				"sv" => "Swedish",
-				"tl" => "Tagalog",
-				"ta" => "Tamil",
-				"th" => "Thai",
-				"tr" => "Turkish",
-				"uk" => "Ukrainian",
-				"ur" => "Urdu",
-				"vi" => "Vietnamese",
-				"cy" => "Welsh"
-			);
-
-			//return the languages array
-			return $languages;
-		}
+	public function get_languages() : array {
+		//create the languages array
+		$languages = array(
+			"af" => "Afrikaans",
+			"ar" => "Arabic",
+			"hy" => "Armenian",
+			"az" => "Azerbaijani",
+			"be" => "Belarusian",
+			"bs" => "Bosnian",
+			"bg" => "Bulgarian",
+			"ca" => "Catalan",
+			"zh" => "Chinese",
+			"hr" => "Croatian",
+			"cs" => "Czech",
+			"da" => "Danish",
+			"nl" => "Dutch",
+			"en" => "English",
+			"et" => "Estonian",
+			"fi" => "Finnish",
+			"fr" => "French",
+			"gl" => "Galician",
+			"de" => "German",
+			"el" => "Greek",
+			"he" => "Hebrew",
+			"hi" => "Hindi",
+			"hu" => "Hungarian",
+			"is" => "Icelandic",
+			"id" => "Indonesian",
+			"it" => "Italian",
+			"ja" => "Japanese",
+			"kn" => "Kannada",
+			"kk" => "Kazakh",
+			"ko" => "Korean",
+			"lv" => "Latvian",
+			"lt" => "Lithuanian",
+			"mk" => "Macedonian",
+			"ms" => "Malay",
+			"mr" => "Marathi",
+			"mi" => "Maori",
+			"ne" => "Nepali",
+			"no" => "Norwegian",
+			"fa" => "Persian",
+			"pl" => "Polish",
+			"pt" => "Portuguese",
+			"ro" => "Romanian",
+			"ru" => "Russian",
+			"sr" => "Serbian",
+			"sk" => "Slovak",
+			"sl" => "Slovenian",
+			"es" => "Spanish",
+			"sw" => "Swahili",
+			"sv" => "Swedish",
+			"tl" => "Tagalog",
+			"ta" => "Tamil",
+			"th" => "Thai",
+			"tr" => "Turkish",
+			"uk" => "Ukrainian",
+			"ur" => "Urdu",
+			"vi" => "Vietnamese",
+			"cy" => "Welsh"
+		);
+
+		//return the languages array
+		return $languages;
+	}
 
-		/**
-		 * transcribe - speech to text
-		 */
-		public function transcribe() : string {
+	/**
+	 * transcribe - speech to text
+	 */
+	public function transcribe() : string {
 
-			//get the content type
-			if (file_exists($this->path.'/'.$this->filename)) {
-				$path_array = pathinfo($this->path.'/'.$this->filename);
-				if ($path_array['extension'] == "mp3") {
-					$content_type = 'audio/mp3';
-				}
-				if ($path_array['extension'] == "wav") {
-					$content_type = 'audio/wav';
-				}
+		//get the content type
+		if (file_exists($this->path.'/'.$this->filename)) {
+			$path_array = pathinfo($this->path.'/'.$this->filename);
+			if ($path_array['extension'] == "mp3") {
+				$content_type = 'audio/mp3';
 			}
-			elseif (!empty($this->audio_string)) {
-				$content_type = $this->audio_mime_type;
+			if ($path_array['extension'] == "wav") {
+				$content_type = 'audio/wav';
 			}
+		}
+		elseif (!empty($this->audio_string)) {
+			$content_type = $this->audio_mime_type;
+		}
+
+		//start output buffer
+		ob_start();
+		$out = fopen('php://output', 'w');
+
+		if (isset($this->api_key) && $this->api_key != '') {
+
+			$url = "https://" . $this->api_url . ".api.cognitive.microsoft.com/sts/v1.0/issueToken";
+			$headers = [
+				"Content-type: application/x-www-form-urlencoded",
+				"Content-Length: 0",
+				"Ocp-Apim-Subscription-Key: " . $this->api_key
+			];
+
+			// initialize a curl handle
+			$ch = curl_init($url);
+
+			//set the request method to POST
+			curl_setopt($ch, CURLOPT_POST, true);
+
+			//send the http headers
+			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+
+			//return the response as a string instead of outputting it directly
+			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
-			//start output buffer
-			ob_start();
-			$out = fopen('php://output', 'w');
+			//run the curl request to get the access token
+			$access_token = curl_exec($ch);
 
-			if (isset($this->api_key) && $this->api_key != '') {
+			//close the handle
+			curl_close($ch);
 
-				$url = "https://" . $this->api_url . ".api.cognitive.microsoft.com/sts/v1.0/issueToken";
+			//if a token was returned then use it to make the transcribe request
+			if (empty($access_token)) {
+				return false;
+			}
+			else {
+				$url = "https://" . $this->api_url . ".stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=" . $this->language . "&format=detailed";
+				$file_path = $this->path . '/' . $this->filename;
 				$headers = [
-					"Content-type: application/x-www-form-urlencoded",
-					"Content-Length: 0",
-					"Ocp-Apim-Subscription-Key: " . $this->api_key
+					"Authorization: Bearer " . $access_token,
+					"Content-type: audio/wav; codec=\"audio/pcm\"; samplerate=8000; trustsourcerate=false"
 				];
 
-				// initialize a curl handle
+				//initialize a curl handle
 				$ch = curl_init($url);
 
 				//set the request method to POST
@@ -189,94 +216,61 @@ if (!class_exists('transcribe_azure')) {
 				//send the http headers
 				curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
+				//prepare to send the file or audio
+				if (file_exists($this->path.'/'.$this->filename)) {
+					//send the file using
+					curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_path));
+				}
+				elseif (!empty($this->audio_string)) {
+					//send the audio from as a string
+					curl_setopt($ch, CURLOPT_POSTFIELDS, $this->audio_string);
+				}
+				else {
+					//audio file or string not found
+					return false;
+				}
+
 				//return the response as a string instead of outputting it directly
 				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
-				//run the curl request to get the access token
-				$access_token = curl_exec($ch);
+				//add verbose for debugging
+				curl_setopt($ch, CURLOPT_VERBOSE, true);
+				curl_setopt($ch, CURLOPT_STDERR, $out);
+
+				//run the curl request to transcribe the message
+				$json_response = curl_exec($ch);
+
+				//check for errors
+				if (curl_errno($ch)) {
+					echo 'Error: ' . curl_error($ch);
+					exit;
+				}
 
 				//close the handle
 				curl_close($ch);
 
-				//if a token was returned then use it to make the transcribe request
-				if (empty($access_token)) {
-					return false;
+				//convert the json to an a
+				$array = json_decode($json_response, true);
+
+				//validate the json
+				if ($array === null) {
+					return 'invalid json';
 				}
 				else {
-					$url = "https://" . $this->api_url . ".stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=" . $this->language . "&format=detailed";
-					$file_path = $this->path . '/' . $this->filename;
-					$headers = [
-						"Authorization: Bearer " . $access_token,
-						"Content-type: audio/wav; codec=\"audio/pcm\"; samplerate=8000; trustsourcerate=false"
-					];
-
-					//initialize a curl handle
-					$ch = curl_init($url);
-
-					//set the request method to POST
-					curl_setopt($ch, CURLOPT_POST, true);
-
-					//send the http headers
-					curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-
-					//prepare to send the file or audio
-					if (file_exists($this->path.'/'.$this->filename)) {
-						//send the file using
-						curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_path));
-					}
-					elseif (!empty($this->audio_string)) {
-						//send the audio from as a string
-						curl_setopt($ch, CURLOPT_POSTFIELDS, $this->audio_string);
-					}
-					else {
-						//audio file or string not found
-						return false;
-					}
-
-					//return the response as a string instead of outputting it directly
-					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
-					//add verbose for debugging
-					curl_setopt($ch, CURLOPT_VERBOSE, true);
-					curl_setopt($ch, CURLOPT_STDERR, $out);
-
-					//run the curl request to transcribe the message
-					$json_response = curl_exec($ch);
-
-					//check for errors
-					if (curl_errno($ch)) {
-						echo 'Error: ' . curl_error($ch);
-						exit;
-					}
-
-					//close the handle
-					curl_close($ch);
-
-					//convert the json to an a
-					$array = json_decode($json_response, true);
-
-					//validate the json
-					if ($array === null) {
-						return 'invalid json';
-					}
-					else {
-						$this->message = $array['NBest'][0]['Display'];
-					}
+					$this->message = $array['NBest'][0]['Display'];
 				}
-
 			}
 
-			//return the transcription
-			if (empty($this->message)) {
-				return '';
-			}
-			else {
-				return $this->message;
-			}
+		}
 
+		//return the transcription
+		if (empty($this->message)) {
+			return '';
+		}
+		else {
+			return $this->message;
 		}
 
 	}
-}
 
-?>
+}

+ 280 - 286
resources/classes/transcribe_google.php

@@ -1,296 +1,228 @@
 <?php
 
+/**
+* transcribe_google class
+*
+*/
+class transcribe_google implements transcribe_interface {
+
+	/**
+	 * declare private variables
+	 */
+	private $api_key;
+	private $api_url;
+	private $language;
+	private $alternate_language;
+	private $application_credentials;
+	private $path;
+	private $filename;
+	private $audio_string;
+	private $audio_mime_type;
+	private $format;
+	private $voice;
+	private $message;
+	private $model;
+
+	/**
+	 * called when the object is created
+	 */
+	public function __construct($settings) {
+
+		// build the setting object and get the recording path
+		$this->api_key = $settings->get('transcribe', 'api_key');
+		$this->api_url = $settings->get('transcribe', 'api_url');
+		$this->language = $settings->get('transcribe', 'language');
+		$this->alternate_language = $settings->get('transcribe', 'alternate_language');
+		$this->application_credentials = $settings->get('transcribe', 'application_credentials');
 
- /**
- * transcribe_google class
- *
- * @method null download
- */
-if (!class_exists('transcribe_google')) {
-	class transcribe_google implements transcribe_interface {
-
-		/**
-		 * declare private variables
-		 */
-		private $api_key;
-		private $api_url;
-		private $language;
-		private $alternate_language;
-		private $application_credentials;
-		private $path;
-		private $filename;
-		private $audio_string;
-		private $audio_mime_type;
-		private $format;
-		private $voice;
-		private $message;
-		private $model;
-
-		/**
-		 * called when the object is created
-		 */
-		public function __construct($settings) {
-
-			// build the setting object and get the recording path
-			$this->api_key = $settings->get('transcribe', 'api_key');
-			$this->api_url = $settings->get('transcribe', 'api_url');
-			$this->language = $settings->get('transcribe', 'language');
-			$this->alternate_language = $settings->get('transcribe', 'alternate_language');
-			$this->application_credentials = $settings->get('transcribe', 'application_credentials');
-
-		}
+	}
 
-		public function set_path(string $audio_path) {
-			$this->path = $audio_path;
-		}
+	public function set_path(string $audio_path) {
+		$this->path = $audio_path;
+	}
 
-		public function set_filename(string $audio_filename) {
-			$this->filename = $audio_filename;
-		}
+	public function set_filename(string $audio_filename) {
+		$this->filename = $audio_filename;
+	}
 
-		public function set_audio_string(string $audio_string) {
-			$this->audio_string = $audio_string;
-		}
+	public function set_audio_string(string $audio_string) {
+		$this->audio_string = $audio_string;
+	}
 
-		public function set_audio_mime_type(string $audio_mime_type) {
-			$this->audio_mime_type = $audio_mime_type;
-		}
+	public function set_audio_mime_type(string $audio_mime_type) {
+		$this->audio_mime_type = $audio_mime_type;
+	}
 
-		public function set_format(string $audio_format) {
-			$this->format = $audio_format;
-		}
+	public function set_format(string $audio_format) {
+		$this->format = $audio_format;
+	}
 
-		public function set_voice(string $audio_voice) {
-			$this->voice = $audio_voice;
-		}
+	public function set_voice(string $audio_voice) {
+		$this->voice = $audio_voice;
+	}
 
-		public function set_language(string $audio_language) {
-			$this->language = $audio_language;
-		}
+	public function set_language(string $audio_language) {
+		$this->language = $audio_language;
+	}
 
-		public function set_translate(string $audio_translate) {
-			$this->translate = $audio_translate;
-		}
+	public function set_translate(string $audio_translate) {
+		$this->translate = $audio_translate;
+	}
 
-		public function set_message(string $audio_message) {
-			$this->message = $audio_message;
-		}
+	public function set_message(string $audio_message) {
+		$this->message = $audio_message;
+	}
 
-		public function is_language_enabled() : bool {
-			// return the whether engine is handles languages
-			return false;
-		}
+	public function is_language_enabled() : bool {
+		// return the whether engine is handles languages
+		return false;
+	}
 
-		public function is_translate_enabled() : bool {
-			// return the whether engine is able to translate
-			return false;
-		}
+	public function is_translate_enabled() : bool {
+		// return the whether engine is able to translate
+		return false;
+	}
 
-		public function get_languages() : array {
-			// create the languages array
-			$languages = array(
-				"af" => "Afrikaans",
-				"ar" => "Arabic",
-				"hy" => "Armenian",
-				"az" => "Azerbaijani",
-				"be" => "Belarusian",
-				"bs" => "Bosnian",
-				"bg" => "Bulgarian",
-				"ca" => "Catalan",
-				"zh" => "Chinese",
-				"hr" => "Croatian",
-				"cs" => "Czech",
-				"da" => "Danish",
-				"nl" => "Dutch",
-				"en" => "English",
-				"et" => "Estonian",
-				"fi" => "Finnish",
-				"fr" => "French",
-				"gl" => "Galician",
-				"de" => "German",
-				"el" => "Greek",
-				"he" => "Hebrew",
-				"hi" => "Hindi",
-				"hu" => "Hungarian",
-				"is" => "Icelandic",
-				"id" => "Indonesian",
-				"it" => "Italian",
-				"ja" => "Japanese",
-				"kn" => "Kannada",
-				"kk" => "Kazakh",
-				"ko" => "Korean",
-				"lv" => "Latvian",
-				"lt" => "Lithuanian",
-				"mk" => "Macedonian",
-				"ms" => "Malay",
-				"mr" => "Marathi",
-				"mi" => "Maori",
-				"ne" => "Nepali",
-				"no" => "Norwegian",
-				"fa" => "Persian",
-				"pl" => "Polish",
-				"pt" => "Portuguese",
-				"ro" => "Romanian",
-				"ru" => "Russian",
-				"sr" => "Serbian",
-				"sk" => "Slovak",
-				"sl" => "Slovenian",
-				"es" => "Spanish",
-				"sw" => "Swahili",
-				"sv" => "Swedish",
-				"tl" => "Tagalog",
-				"ta" => "Tamil",
-				"th" => "Thai",
-				"tr" => "Turkish",
-				"uk" => "Ukrainian",
-				"ur" => "Urdu",
-				"vi" => "Vietnamese",
-				"cy" => "Welsh"
-			);
-
-			// return the languages array
-			return $languages;
-		}
+	public function get_languages() : array {
+		// create the languages array
+		$languages = array(
+			"af" => "Afrikaans",
+			"ar" => "Arabic",
+			"hy" => "Armenian",
+			"az" => "Azerbaijani",
+			"be" => "Belarusian",
+			"bs" => "Bosnian",
+			"bg" => "Bulgarian",
+			"ca" => "Catalan",
+			"zh" => "Chinese",
+			"hr" => "Croatian",
+			"cs" => "Czech",
+			"da" => "Danish",
+			"nl" => "Dutch",
+			"en" => "English",
+			"et" => "Estonian",
+			"fi" => "Finnish",
+			"fr" => "French",
+			"gl" => "Galician",
+			"de" => "German",
+			"el" => "Greek",
+			"he" => "Hebrew",
+			"hi" => "Hindi",
+			"hu" => "Hungarian",
+			"is" => "Icelandic",
+			"id" => "Indonesian",
+			"it" => "Italian",
+			"ja" => "Japanese",
+			"kn" => "Kannada",
+			"kk" => "Kazakh",
+			"ko" => "Korean",
+			"lv" => "Latvian",
+			"lt" => "Lithuanian",
+			"mk" => "Macedonian",
+			"ms" => "Malay",
+			"mr" => "Marathi",
+			"mi" => "Maori",
+			"ne" => "Nepali",
+			"no" => "Norwegian",
+			"fa" => "Persian",
+			"pl" => "Polish",
+			"pt" => "Portuguese",
+			"ro" => "Romanian",
+			"ru" => "Russian",
+			"sr" => "Serbian",
+			"sk" => "Slovak",
+			"sl" => "Slovenian",
+			"es" => "Spanish",
+			"sw" => "Swahili",
+			"sv" => "Swedish",
+			"tl" => "Tagalog",
+			"ta" => "Tamil",
+			"th" => "Thai",
+			"tr" => "Turkish",
+			"uk" => "Ukrainian",
+			"ur" => "Urdu",
+			"vi" => "Vietnamese",
+			"cy" => "Welsh"
+		);
+
+		// return the languages array
+		return $languages;
+	}
 
-		/**
-		 * transcribe - speech to text
-		 */
-		public function transcribe() : string {
+	/**
+	 * transcribe - speech to text
+	 */
+	public function transcribe() : string {
 
-			if (!isset($this->language) && empty($this->language)) {
-				$this->language = 'en-US';
-			}
-			if (!isset($this->alternate_language) && empty($this->alternate_language)) {
-				$this->alternate_language = 'es-US';
-			}
+		if (!isset($this->language) && empty($this->language)) {
+			$this->language = 'en-US';
+		}
+		if (!isset($this->alternate_language) && empty($this->alternate_language)) {
+			$this->alternate_language = 'es-US';
+		}
 
-			// get the content type
-			if (file_exists($this->path.'/'.$this->filename)) {
-				$path_array = pathinfo($this->path.'/'.$this->filename);
-				if ($path_array['extension'] == "mp3") {
-					$content_type = 'audio/mp3';
-				}
-				if ($path_array['extension'] == "wav") {
-					$content_type = 'audio/wav';
-				}
+		// get the content type
+		if (file_exists($this->path.'/'.$this->filename)) {
+			$path_array = pathinfo($this->path.'/'.$this->filename);
+			if ($path_array['extension'] == "mp3") {
+				$content_type = 'audio/mp3';
 			}
-			elseif (!empty($this->audio_string)) {
-				$content_type = $this->audio_mime_type;
+			if ($path_array['extension'] == "wav") {
+				$content_type = 'audio/wav';
 			}
+		}
+		elseif (!empty($this->audio_string)) {
+			$content_type = $this->audio_mime_type;
+		}
 
-			// start output buffer
-			ob_start();
-			$out = fopen('php://output', 'w');
-
-			// version 1
-			if (trim($this->api_url) == 'https://speech.googleapis.com/v1p1beta1/speech') {
-				if (isset($this->api_key) && $this->api_key != '') {
-					
-					if (file_exists($this->path.'/'.$this->filename)) {
-						//file has been found
-					}
-					elseif (!empty($this->audio_string)) {
-						//if this is empty then use the temp directory
-						if (empty($this->path)) {
-							$this->path = sys_get_temp_dir();
-						}
-
-						//save the audio string on the file system
-						file_put_contents($this->path.'/'.$this->filename, $this->audio_string);
-					}
-					else {
-						//audio file or string not found
-						return false;
-					}
-
-					//get the length of the audio file
-					$audio_length = (float)system("soxi -D ".$this->path."/".$this->filename);
-
-					// Convert audio file to FLAC format
-					$flac_file = $this->path . '/' . $this->filename . '.flac';
-					$command = "sox ".$this->path."/".$this->filename." ".$flac_file;
-					if ($audio_length > 59) { $command .= " trim 0 00:59"; }
-					exec($command);
-
-					// Base64 encode FLAC file
-					$flac_base64 = base64_encode(file_get_contents($flac_file));
-
-					// Prepare JSON data
-					$data = [
-						'config' => [
-							'languageCode' => $this->language,
-							'enableWordTimeOffsets' => false,
-							'enableAutomaticPunctuation' => true,
-							'alternativeLanguageCodes' => $this->alternate_language
-						],
-						'audio' => [
-							'content' => $flac_base64
-						]
-					];
-					$json_data = json_encode($data);
-
-					// initialize a curl handle
-					$ch = curl_init();
-
-					// set the URL for the request
-					curl_setopt($ch, CURLOPT_URL, $this->api_url . ':recognize?key=' . $this->api_key);
-
-					// set the request method to POST
-					curl_setopt($ch, CURLOPT_POST, true);
-
-					// send the HTTP headers
-					curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
-
-					// send the HTTP post
-					curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
-
-					// return the response as a string instead of outputting it directly
-					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
-					// run the curl request and transcription message
-					$response = curl_exec($ch);
-
-					// check for errors
-					if (curl_errno($ch)) {
-						echo 'Error: ' . curl_error($ch);
-						exit;
-					}
-
-					// close the handle
-					curl_close($ch);
-
-					// Remove temporary FLAC file
-					unlink($flac_file);
-				}
-			}
+		// start output buffer
+		ob_start();
+		$out = fopen('php://output', 'w');
 
-			// version 2
-			if (substr($this->api_url, 0, 32) == 'https://speech.googleapis.com/v2') {
-				if (!empty($this->application_credentials)) {
-					putenv("GOOGLE_APPLICATION_CREDENTIALS=".$this->application_credentials);
-				}
+		// version 1
+		if (trim($this->api_url) == 'https://speech.googleapis.com/v1p1beta1/speech') {
+			if (isset($this->api_key) && $this->api_key != '') {
 
-				// Base64 encode the audio
 				if (file_exists($this->path.'/'.$this->filename)) {
 					//file has been found
-					$audio_base64 = base64_encode(file_get_contents($this->file_path . '/' . $this->file_name));
 				}
 				elseif (!empty($this->audio_string)) {
-					$audio_base64 = base64_encode($this->audio_string);
+					//if this is empty then use the temp directory
+					if (empty($this->path)) {
+						$this->path = sys_get_temp_dir();
+					}
+
+					//save the audio string on the file system
+					file_put_contents($this->path.'/'.$this->filename, $this->audio_string);
 				}
 				else {
 					//audio file or string not found
 					return false;
 				}
 
+				//get the length of the audio file
+				$audio_length = (float)system("soxi -D ".$this->path."/".$this->filename);
+
+				// Convert audio file to FLAC format
+				$flac_file = $this->path . '/' . $this->filename . '.flac';
+				$command = "sox ".$this->path."/".$this->filename." ".$flac_file;
+				if ($audio_length > 59) { $command .= " trim 0 00:59"; }
+				exec($command);
+
+				// Base64 encode FLAC file
+				$flac_base64 = base64_encode(file_get_contents($flac_file));
+
 				// Prepare JSON data
 				$data = [
 					'config' => [
-						'auto_decoding_config' => [],
-						'language_codes' => [$this->language],
-						'model' => 'long'
+						'languageCode' => $this->language,
+						'enableWordTimeOffsets' => false,
+						'enableAutomaticPunctuation' => true,
+						'alternativeLanguageCodes' => $this->alternate_language
 					],
-					'content' => $audio_base64
+					'audio' => [
+						'content' => $flac_base64
+					]
 				];
 				$json_data = json_encode($data);
 
@@ -298,13 +230,13 @@ if (!class_exists('transcribe_google')) {
 				$ch = curl_init();
 
 				// set the URL for the request
-				curl_setopt($ch, CURLOPT_URL, $this->api_url);
+				curl_setopt($ch, CURLOPT_URL, $this->api_url . ':recognize?key=' . $this->api_key);
 
 				// set the request method to POST
-				curl_setopt($ch, CURLOPT_POST, 1);
+				curl_setopt($ch, CURLOPT_POST, true);
 
 				// send the HTTP headers
-				curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Authorization: Bearer ' . shell_exec('gcloud auth application-default print-access-token')]);
+				curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
 
 				// send the HTTP post
 				curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
@@ -312,10 +244,6 @@ if (!class_exists('transcribe_google')) {
 				// return the response as a string instead of outputting it directly
 				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
-				//add verbose for debugging
-				curl_setopt($ch, CURLOPT_VERBOSE, true);
-				curl_setopt($ch, CURLOPT_STDERR, $out);
-
 				// run the curl request and transcription message
 				$response = curl_exec($ch);
 
@@ -327,39 +255,105 @@ if (!class_exists('transcribe_google')) {
 
 				// close the handle
 				curl_close($ch);
+
+				// Remove temporary FLAC file
+				unlink($flac_file);
 			}
+		}
 
-			// validate the json
-			if (!empty($response)) {
-				$ob = json_decode($response);
-				if($ob === null) {
-					echo "invalid json\n";
-					return false;
-				}
+		// version 2
+		if (substr($this->api_url, 0, 32) == 'https://speech.googleapis.com/v2') {
+			if (!empty($this->application_credentials)) {
+				putenv("GOOGLE_APPLICATION_CREDENTIALS=".$this->application_credentials);
+			}
 
-				$json = json_decode($response, true);
-				// echo "json; ".$json."\n";
-				$message = '';
-				foreach($json['results'] as $row) {
-					$this->message .= $row['alternatives'][0]['transcript'];
-				}
+			// Base64 encode the audio
+			if (file_exists($this->path.'/'.$this->filename)) {
+				//file has been found
+				$audio_base64 = base64_encode(file_get_contents($this->file_path . '/' . $this->file_name));
+			}
+			elseif (!empty($this->audio_string)) {
+				$audio_base64 = base64_encode($this->audio_string);
 			}
+			else {
+				//audio file or string not found
+				return false;
+			}
+
+			// Prepare JSON data
+			$data = [
+				'config' => [
+					'auto_decoding_config' => [],
+					'language_codes' => [$this->language],
+					'model' => 'long'
+				],
+				'content' => $audio_base64
+			];
+			$json_data = json_encode($data);
+
+			// initialize a curl handle
+			$ch = curl_init();
+
+			// set the URL for the request
+			curl_setopt($ch, CURLOPT_URL, $this->api_url);
+
+			// set the request method to POST
+			curl_setopt($ch, CURLOPT_POST, 1);
 
-			// show the debug information
-			fclose($out);
-			// $this->debug = ob_get_clean();
+			// send the HTTP headers
+			curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Authorization: Bearer ' . shell_exec('gcloud auth application-default print-access-token')]);
 
-			// return the transcription
-			if (empty($this->message)) {
-				return '';
+			// send the HTTP post
+			curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
+
+			// return the response as a string instead of outputting it directly
+			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+			//add verbose for debugging
+			curl_setopt($ch, CURLOPT_VERBOSE, true);
+			curl_setopt($ch, CURLOPT_STDERR, $out);
+
+			// run the curl request and transcription message
+			$response = curl_exec($ch);
+
+			// check for errors
+			if (curl_errno($ch)) {
+				echo 'Error: ' . curl_error($ch);
+				exit;
 			}
-			else {
-				return $this->message;
+
+			// close the handle
+			curl_close($ch);
+		}
+
+		// validate the json
+		if (!empty($response)) {
+			$ob = json_decode($response);
+			if($ob === null) {
+				echo "invalid json\n";
+				return false;
+			}
+
+			$json = json_decode($response, true);
+			// echo "json; ".$json."\n";
+			$message = '';
+			foreach($json['results'] as $row) {
+				$this->message .= $row['alternatives'][0]['transcript'];
 			}
+		}
+
+		// show the debug information
+		fclose($out);
+		// $this->debug = ob_get_clean();
 
+		// return the transcription
+		if (empty($this->message)) {
+			return '';
+		}
+		else {
+			return $this->message;
 		}
 
 	}
-}
 
-?>
+}

+ 222 - 228
resources/classes/transcribe_local.php

@@ -1,253 +1,247 @@
 <?php
 
+/**
+* transcribe_local class
+*
+*/
+class transcribe_local implements transcribe_interface {
+
+	/**
+	 * declare private variables
+	 */
+	private $api_key;
+	private $api_url;
+	public  $api_model;
+	private $path;
+	private $filename;
+	private $audio_string;
+	private $audio_mime_type;
+	private $format;
+	private $message;
+	private $language;
+
+	/**
+	 * called when the object is created
+	 */
+	public function __construct($settings) {
+
+		//build the setting object and get the recording path
+		$this->api_key = $settings->get('transcribe', 'api_key', '');
+		$this->api_url = $settings->get('transcribe', 'api_url', '');
+		$this->api_model = $settings->get('transcribe', 'api_model', 'whisper-1');
 
- /**
- * transcribe_local class
- *
- * @method null download
- */
-if (!class_exists('transcribe_local')) {
-	class transcribe_local implements transcribe_interface {
-
-		/**
-		 * declare private variables
-		 */
-		private $api_key;
-		private $api_url;
-		public  $api_model;
-		private $path;
-		private $filename;
-		private $audio_string;
-		private $audio_mime_type;
-		private $format;
-		private $message;
-		private $language;
-
-		/**
-		 * called when the object is created
-		 */
-		public function __construct($settings) {
-
-			//build the setting object and get the recording path
-			$this->api_key = $settings->get('transcribe', 'api_key', '');
-			$this->api_url = $settings->get('transcribe', 'api_url', '');
-			$this->api_model = $settings->get('transcribe', 'api_model', 'whisper-1');
+	}
 
-		}
+	public function set_path(string $audio_path) {
+		$this->path = $audio_path;
+	}
 
-		public function set_path(string $audio_path) {
-			$this->path = $audio_path;
-		}
+	public function set_filename(string $audio_filename) {
+		$this->filename = $audio_filename;
+	}
 
-		public function set_filename(string $audio_filename) {
-			$this->filename = $audio_filename;
-		}
+	public function set_audio_string(string $audio_string) {
+		$this->audio_string = $audio_string;
+	}
 
-		public function set_audio_string(string $audio_string) {
-			$this->audio_string = $audio_string;
-		}
+	public function set_audio_mime_type(string $audio_mime_type) {
+		$this->audio_mime_type = $audio_mime_type;
+	}
 
-		public function set_audio_mime_type(string $audio_mime_type) {
-			$this->audio_mime_type = $audio_mime_type;
-		}
+	public function set_format(string $audio_format) {
+		$this->format = $audio_format;
+	}
 
-		public function set_format(string $audio_format) {
-			$this->format = $audio_format;
-		}
+	public function set_message(string $audio_message) {
+		$this->message = $audio_message;
+	}
+
+	public function is_language_enabled() : bool {
+		//return the whether engine is handles languages
+		return false;
+	}
 
-		public function set_message(string $audio_message) {
-			$this->message = $audio_message;
+	public function set_language(string $audio_language) {
+		$this->language = $audio_language;
+	}
+
+	public function get_languages() : array {
+		//create the languages array
+		$languages = array(
+			"af" => "Afrikaans",
+			"ar" => "Arabic",
+			"hy" => "Armenian",
+			"az" => "Azerbaijani",
+			"be" => "Belarusian",
+			"bs" => "Bosnian",
+			"bg" => "Bulgarian",
+			"ca" => "Catalan",
+			"zh" => "Chinese",
+			"hr" => "Croatian",
+			"cs" => "Czech",
+			"da" => "Danish",
+			"nl" => "Dutch",
+			"en" => "English",
+			"et" => "Estonian",
+			"fi" => "Finnish",
+			"fr" => "French",
+			"gl" => "Galician",
+			"de" => "German",
+			"el" => "Greek",
+			"he" => "Hebrew",
+			"hi" => "Hindi",
+			"hu" => "Hungarian",
+			"is" => "Icelandic",
+			"id" => "Indonesian",
+			"it" => "Italian",
+			"ja" => "Japanese",
+			"kn" => "Kannada",
+			"kk" => "Kazakh",
+			"ko" => "Korean",
+			"lv" => "Latvian",
+			"lt" => "Lithuanian",
+			"mk" => "Macedonian",
+			"ms" => "Malay",
+			"mr" => "Marathi",
+			"mi" => "Maori",
+			"ne" => "Nepali",
+			"no" => "Norwegian",
+			"fa" => "Persian",
+			"pl" => "Polish",
+			"pt" => "Portuguese",
+			"ro" => "Romanian",
+			"ru" => "Russian",
+			"sr" => "Serbian",
+			"sk" => "Slovak",
+			"sl" => "Slovenian",
+			"es" => "Spanish",
+			"sw" => "Swahili",
+			"sv" => "Swedish",
+			"tl" => "Tagalog",
+			"ta" => "Tamil",
+			"th" => "Thai",
+			"tr" => "Turkish",
+			"uk" => "Ukrainian",
+			"ur" => "Urdu",
+			"vi" => "Vietnamese",
+			"cy" => "Welsh"
+		);
+
+		//return the languages array
+		return $languages;
+	}
+
+	/**
+	 * transcribe - speech to text
+	 */
+	public function transcribe() : string {
+
+		// Use the curl command line for debuging
+		//echo "/usr/bin/curl --request POST ";
+		//echo " --url 'http://127.0.0.1:8000/transcribe' ";
+		//echo " --header 'Authorization: Bearer ".$this->api_key."' ";
+		//echo " --header 'Content-Type: multipart/form-data' ";
+		//echo " --form 'file=@".$this->path.'/'.$this->filename."' ";
+		//echo " --form 'model=whisper-1' ";
+		//echo " --form 'response_format=text' ";
+		//echo "\n";
+
+		//start output buffer
+		ob_start();
+		$out = fopen('php://output', 'w');
+
+		// initialize a curl handle
+		$ch = curl_init();
+
+		// set the api_url if not already set
+		if (empty($this->api_url)) {
+			$this->api_url = 'http://127.0.0.1:8000/transcribe';
 		}
 
-		public function is_language_enabled() : bool {
-			//return the whether engine is handles languages
+		// set the URL for the request
+		curl_setopt($ch, CURLOPT_URL, $this->api_url);
+
+		// set the request method to POST
+		curl_setopt($ch, CURLOPT_POST, true);
+
+		// set the request headers
+		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+			'Authorization: Bearer '.$this->api_key,
+			'Content-Type: multipart/form-data'
+		));
+
+		// prepare the HTTP POST data
+		if (file_exists($this->path.'/'.$this->filename)) {
+			//send the audio from the file system
+			$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
+		}
+		elseif (!empty($this->audio_string)) {
+			//send the audio from as a string
+			$post_data['file'] = new CURLStringFile($this->audio_string, $this->filename, $this->audio_mime_type);
+		}
+		else {
+			//audio file or string not found
 			return false;
 		}
 
-		public function set_language(string $audio_language) {
-			$this->language = $audio_language;
-		}
+		$post_data['model'] = $this->api_model;
+		$post_data['response_format'] = 'text';
+		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 
-		public function get_languages() : array {
-			//create the languages array
-			$languages = array(
-				"af" => "Afrikaans",
-				"ar" => "Arabic",
-				"hy" => "Armenian",
-				"az" => "Azerbaijani",
-				"be" => "Belarusian",
-				"bs" => "Bosnian",
-				"bg" => "Bulgarian",
-				"ca" => "Catalan",
-				"zh" => "Chinese",
-				"hr" => "Croatian",
-				"cs" => "Czech",
-				"da" => "Danish",
-				"nl" => "Dutch",
-				"en" => "English",
-				"et" => "Estonian",
-				"fi" => "Finnish",
-				"fr" => "French",
-				"gl" => "Galician",
-				"de" => "German",
-				"el" => "Greek",
-				"he" => "Hebrew",
-				"hi" => "Hindi",
-				"hu" => "Hungarian",
-				"is" => "Icelandic",
-				"id" => "Indonesian",
-				"it" => "Italian",
-				"ja" => "Japanese",
-				"kn" => "Kannada",
-				"kk" => "Kazakh",
-				"ko" => "Korean",
-				"lv" => "Latvian",
-				"lt" => "Lithuanian",
-				"mk" => "Macedonian",
-				"ms" => "Malay",
-				"mr" => "Marathi",
-				"mi" => "Maori",
-				"ne" => "Nepali",
-				"no" => "Norwegian",
-				"fa" => "Persian",
-				"pl" => "Polish",
-				"pt" => "Portuguese",
-				"ro" => "Romanian",
-				"ru" => "Russian",
-				"sr" => "Serbian",
-				"sk" => "Slovak",
-				"sl" => "Slovenian",
-				"es" => "Spanish",
-				"sw" => "Swahili",
-				"sv" => "Swedish",
-				"tl" => "Tagalog",
-				"ta" => "Tamil",
-				"th" => "Thai",
-				"tr" => "Turkish",
-				"uk" => "Ukrainian",
-				"ur" => "Urdu",
-				"vi" => "Vietnamese",
-				"cy" => "Welsh"
-			);
-
-			//return the languages array
-			return $languages;
-		}
+		// return the response as a string instead of outputting it directly
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+		// set the connection timeout and the overall maximum curl run time
+		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
+		curl_setopt($ch, CURLOPT_TIMEOUT, 300);
+
+		// follow any "Location: " header the server sends as part of the HTTP header.
+		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
+
+		// automatically set the Referer: field in requests where it follows a Location: redirect.
+		curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
+
+		// set whether to verify SSL peer
+		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
 
-		/**
-		 * transcribe - speech to text
-		 */
-		public function transcribe() : string {
-
-			// Use the curl command line for debuging
-			//echo "/usr/bin/curl --request POST ";
-			//echo " --url 'http://127.0.0.1:8000/transcribe' ";
-			//echo " --header 'Authorization: Bearer ".$this->api_key."' ";
-			//echo " --header 'Content-Type: multipart/form-data' ";
-			//echo " --form 'file=@".$this->path.'/'.$this->filename."' ";
-			//echo " --form 'model=whisper-1' ";
-			//echo " --form 'response_format=text' ";
-			//echo "\n";
-
-			//start output buffer
-			ob_start();
-			$out = fopen('php://output', 'w');
-
-			// initialize a curl handle
-			$ch = curl_init();
-
-			// set the api_url if not already set
-			if (empty($this->api_url)) {
-				$this->api_url = 'http://127.0.0.1:8000/transcribe';
-			}
-
-			// set the URL for the request
-			curl_setopt($ch, CURLOPT_URL, $this->api_url);
-
-			// set the request method to POST
-			curl_setopt($ch, CURLOPT_POST, true);
-
-			// set the request headers
-			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
-				'Authorization: Bearer '.$this->api_key,
-				'Content-Type: multipart/form-data'
-			));
-
-			// prepare the HTTP POST data
-			if (file_exists($this->path.'/'.$this->filename)) {
-				//send the audio from the file system
-				$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
-			}
-			elseif (!empty($this->audio_string)) {
-				//send the audio from as a string
-				$post_data['file'] = new CURLStringFile($this->audio_string, $this->filename, $this->audio_mime_type);
-			}
-			else {
-				//audio file or string not found
-				return false;
-			}
-
-			$post_data['model'] = $this->api_model;
-			$post_data['response_format'] = 'text';
-			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
-
-			// return the response as a string instead of outputting it directly
-			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
-			// set the connection timeout and the overall maximum curl run time
-			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
-			curl_setopt($ch, CURLOPT_TIMEOUT, 300);
-
-			// follow any "Location: " header the server sends as part of the HTTP header.
-			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
-
-			// automatically set the Referer: field in requests where it follows a Location: redirect.
-			curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
-
-			// set whether to verify SSL peer
-			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
-
-			// add verbose for debugging
-			curl_setopt($ch, CURLOPT_VERBOSE, true);
-			curl_setopt($ch, CURLOPT_STDERR, $out);
-
-			// run the curl request and transcription message
-			$this->message = curl_exec($ch);
-
-			// show the debug information
-			fclose($out);
-
-			// check for errors
-			if (curl_errno($ch)) {
-				echo 'Error: ' . curl_error($ch);
-				exit;
-			}
-
-			// close the handle
-			curl_close($ch);
-
-			// return the transcription
-			if (empty($this->message)) {
-				return '';
-			}
-			else {
-				return trim($this->message);
-			}
+		// add verbose for debugging
+		curl_setopt($ch, CURLOPT_VERBOSE, true);
+		curl_setopt($ch, CURLOPT_STDERR, $out);
+
+		// run the curl request and transcription message
+		$this->message = curl_exec($ch);
+
+		// show the debug information
+		fclose($out);
+
+		// check for errors
+		if (curl_errno($ch)) {
+			echo 'Error: ' . curl_error($ch);
+			exit;
 		}
 
-		public function set_model(string $model): void {
-			if (array_key_exists($model, $this->get_models())) {
-				$this->api_model = $model;
-			}
+		// close the handle
+		curl_close($ch);
+
+		// return the transcription
+		if (empty($this->message)) {
+			return '';
+		}
+		else {
+			return trim($this->message);
 		}
+	}
 
-		public function get_models(): array {
-			return [
-				'tts-1-hd' => 'tts-1-hd'
-			];
+	public function set_model(string $model): void {
+		if (array_key_exists($model, $this->get_models())) {
+			$this->api_model = $model;
 		}
+	}
 
+	public function get_models(): array {
+		return [
+			'tts-1-hd' => 'tts-1-hd'
+		];
 	}
-}
 
-?>
+}

+ 248 - 253
resources/classes/transcribe_openai.php

@@ -1,282 +1,277 @@
 <?php
 
+/**
+* transcribe_openai class
+*
+*/
+class transcribe_openai implements transcribe_interface {
+
+	/**
+	 * declare private variables
+	 */
+	private $api_key;
+	private $api_url;
+	public  $api_model;
+	private $path;
+	private $filename;
+	private $audio_string;
+	private $audio_mime_type;
+	private $format;
+	private $voice;
+	private $message;
+	private $model;
+	private $language;
+
+	/**
+	 * called when the object is created
+	 */
+	public function __construct($settings) {
+
+		//build the setting object and get the recording path
+		$this->api_key = $settings->get('transcribe', 'api_key', '');
+		$this->api_url = $settings->get('transcribe', 'api_url', '');
+		$this->api_model = $settings->get('transcribe', 'api_model', 'whisper-1');
 
- /**
- * transcribe_openai class
- *
- * @method null download
- */
-if (!class_exists('transcribe_openai')) {
-	class transcribe_openai implements transcribe_interface {
-
-		/**
-		 * declare private variables
-		 */
-		private $api_key;
-		private $api_url;
-		public  $api_model;
-		private $path;
-		private $filename;
-		private $audio_string;
-		private $audio_mime_type;
-		private $format;
-		private $voice;
-		private $message;
-		private $model;
-		private $language;
-
-		/**
-		 * called when the object is created
-		 */
-		public function __construct($settings) {
-
-			//build the setting object and get the recording path
-			$this->api_key = $settings->get('transcribe', 'api_key', '');
-			$this->api_url = $settings->get('transcribe', 'api_url', '');
-			$this->api_model = $settings->get('transcribe', 'api_model', 'whisper-1');
+	}
 
-		}
+	public function set_path(string $audio_path) {
+		$this->path = $audio_path;
+	}
 
-		public function set_path(string $audio_path) {
-			$this->path = $audio_path;
-		}
+	public function set_filename(string $audio_filename) {
+		$this->filename = $audio_filename;
+	}
 
-		public function set_filename(string $audio_filename) {
-			$this->filename = $audio_filename;
-		}
+	public function set_audio_string(string $audio_string) {
+		$this->audio_string = $audio_string;
+	}
 
-		public function set_audio_string(string $audio_string) {
-			$this->audio_string = $audio_string;
-		}
+	public function set_audio_mime_type(string $audio_mime_type) {
+		$this->audio_mime_type = $audio_mime_type;
+	}
 
-		public function set_audio_mime_type(string $audio_mime_type) {
-			$this->audio_mime_type = $audio_mime_type;
-		}
+	public function set_format(string $audio_format) {
+		$this->format = $audio_format;
+	}
 
-		public function set_format(string $audio_format) {
-			$this->format = $audio_format;
-		}
+	public function set_voice(string $audio_voice) {
+		$this->voice = $audio_voice;
+	}
 
-		public function set_voice(string $audio_voice) {
-			$this->voice = $audio_voice;
-		}
+	public function set_language(string $audio_language) {
+		$this->language = $audio_language;
+	}
 
-		public function set_language(string $audio_language) {
-			$this->language = $audio_language;
-		}
+	public function set_translate(string $audio_translate) {
+		$this->translate = $audio_translate;
+	}
 
-		public function set_translate(string $audio_translate) {
-			$this->translate = $audio_translate;
-		}
+	public function set_message(string $audio_message) {
+		$this->message = $audio_message;
+	}
 
-		public function set_message(string $audio_message) {
-			$this->message = $audio_message;
-		}
+	public function is_language_enabled() : bool {
+		//return the whether engine is handles languages
+		return false;
+	}
 
-		public function is_language_enabled() : bool {
-			//return the whether engine is handles languages
-			return false;
+	public function is_translate_enabled() : bool {
+		//return the whether engine is able to translate
+		return false;
+	}
+
+	public function get_voices() : array {
+		$voices = array(
+			"alloy" => "alloy",
+			"echo" => "echo",
+			"fable" => "fable",
+			"nova" => "nova",
+			"onyx" => "onyx",
+			"shimmer" => "shimmer"
+		);
+
+		//return the languages array
+		return $voices;
+	}
+
+	public function get_languages() : array {
+		//create the languages array
+		$languages = array(
+			"af" => "Afrikaans",
+			"ar" => "Arabic",
+			"hy" => "Armenian",
+			"az" => "Azerbaijani",
+			"be" => "Belarusian",
+			"bs" => "Bosnian",
+			"bg" => "Bulgarian",
+			"ca" => "Catalan",
+			"zh" => "Chinese",
+			"hr" => "Croatian",
+			"cs" => "Czech",
+			"da" => "Danish",
+			"nl" => "Dutch",
+			"en" => "English",
+			"et" => "Estonian",
+			"fi" => "Finnish",
+			"fr" => "French",
+			"gl" => "Galician",
+			"de" => "German",
+			"el" => "Greek",
+			"he" => "Hebrew",
+			"hi" => "Hindi",
+			"hu" => "Hungarian",
+			"is" => "Icelandic",
+			"id" => "Indonesian",
+			"it" => "Italian",
+			"ja" => "Japanese",
+			"kn" => "Kannada",
+			"kk" => "Kazakh",
+			"ko" => "Korean",
+			"lv" => "Latvian",
+			"lt" => "Lithuanian",
+			"mk" => "Macedonian",
+			"ms" => "Malay",
+			"mr" => "Marathi",
+			"mi" => "Maori",
+			"ne" => "Nepali",
+			"no" => "Norwegian",
+			"fa" => "Persian",
+			"pl" => "Polish",
+			"pt" => "Portuguese",
+			"ro" => "Romanian",
+			"ru" => "Russian",
+			"sr" => "Serbian",
+			"sk" => "Slovak",
+			"sl" => "Slovenian",
+			"es" => "Spanish",
+			"sw" => "Swahili",
+			"sv" => "Swedish",
+			"tl" => "Tagalog",
+			"ta" => "Tamil",
+			"th" => "Thai",
+			"tr" => "Turkish",
+			"uk" => "Ukrainian",
+			"ur" => "Urdu",
+			"vi" => "Vietnamese",
+			"cy" => "Welsh"
+		);
+
+		//return the languages array
+		return $languages;
+	}
+
+	/**
+	 * transcribe - speech to text
+	 * @return string transcibed messages returned or empty for failure
+	 */
+	public function transcribe() : string {
+
+		// Use the curl command line for debuging
+		//echo "/usr/bin/curl --request POST ";
+		//echo " --url 'https://api.openai.com/v1/audio/transcriptions' ";
+		//echo " --header 'Authorization: Bearer ".$this->api_key."' ";
+		//echo " --header 'Content-Type: multipart/form-data' ";
+		//echo " --form 'file=@".$this->path.'/'.$this->filename."' ";
+		//echo " --form 'model=whisper-1' ";
+		//echo " --form 'response_format=text' ";
+		//echo "\n";
+
+		//start output buffer
+		ob_start();
+		$out = fopen('php://output', 'w');
+
+		// initialize a curl handle
+		$ch = curl_init();
+
+		// set the api_url if not already set
+		if (empty($this->api_url)) {
+			$this->api_url = 'https://api.openai.com/v1/audio/transcriptions';
 		}
 
-		public function is_translate_enabled() : bool {
-			//return the whether engine is able to translate
+		// set the URL for the request
+		curl_setopt($ch, CURLOPT_URL, $this->api_url);
+
+		// set the request method to POST
+		curl_setopt($ch, CURLOPT_POST, true);
+
+		// set the request headers
+		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+			'Authorization: Bearer '.$this->api_key,
+			'Content-Type: multipart/form-data'
+		));
+
+		// prepare the HTTP POST data
+		if (file_exists($this->path.'/'.$this->filename)) {
+			//send the audio from the file system
+			$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
+		}
+		elseif (!empty($this->audio_string)) {
+			//send the audio from as a string
+			$post_data['file'] = new CURLStringFile($this->audio_string, $this->filename, $this->audio_mime_type);
+		}
+		else {
+			//audio file or string not found
 			return false;
 		}
 
-		public function get_voices() : array {
-			$voices = array(
-				"alloy" => "alloy",
-				"echo" => "echo",
-				"fable" => "fable",
-				"nova" => "nova",
-				"onyx" => "onyx",
-				"shimmer" => "shimmer"
-			);
-
-			//return the languages array
-			return $voices;
-		}
+		$post_data['model'] = $this->api_model;
+		$post_data['response_format'] = 'text';
+		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 
-		public function get_languages() : array {
-			//create the languages array
-			$languages = array(
-				"af" => "Afrikaans",
-				"ar" => "Arabic",
-				"hy" => "Armenian",
-				"az" => "Azerbaijani",
-				"be" => "Belarusian",
-				"bs" => "Bosnian",
-				"bg" => "Bulgarian",
-				"ca" => "Catalan",
-				"zh" => "Chinese",
-				"hr" => "Croatian",
-				"cs" => "Czech",
-				"da" => "Danish",
-				"nl" => "Dutch",
-				"en" => "English",
-				"et" => "Estonian",
-				"fi" => "Finnish",
-				"fr" => "French",
-				"gl" => "Galician",
-				"de" => "German",
-				"el" => "Greek",
-				"he" => "Hebrew",
-				"hi" => "Hindi",
-				"hu" => "Hungarian",
-				"is" => "Icelandic",
-				"id" => "Indonesian",
-				"it" => "Italian",
-				"ja" => "Japanese",
-				"kn" => "Kannada",
-				"kk" => "Kazakh",
-				"ko" => "Korean",
-				"lv" => "Latvian",
-				"lt" => "Lithuanian",
-				"mk" => "Macedonian",
-				"ms" => "Malay",
-				"mr" => "Marathi",
-				"mi" => "Maori",
-				"ne" => "Nepali",
-				"no" => "Norwegian",
-				"fa" => "Persian",
-				"pl" => "Polish",
-				"pt" => "Portuguese",
-				"ro" => "Romanian",
-				"ru" => "Russian",
-				"sr" => "Serbian",
-				"sk" => "Slovak",
-				"sl" => "Slovenian",
-				"es" => "Spanish",
-				"sw" => "Swahili",
-				"sv" => "Swedish",
-				"tl" => "Tagalog",
-				"ta" => "Tamil",
-				"th" => "Thai",
-				"tr" => "Turkish",
-				"uk" => "Ukrainian",
-				"ur" => "Urdu",
-				"vi" => "Vietnamese",
-				"cy" => "Welsh"
-			);
-
-			//return the languages array
-			return $languages;
-		}
+		// return the response as a string instead of outputting it directly
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+		// set the connection timeout and the overall maximum curl run time
+		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
+		curl_setopt($ch, CURLOPT_TIMEOUT, 300);
 
-		/**
-		 * transcribe - speech to text
-		 */
-		public function transcribe() : string {
-
-			// Use the curl command line for debuging
-			//echo "/usr/bin/curl --request POST ";
-			//echo " --url 'https://api.openai.com/v1/audio/transcriptions' ";
-			//echo " --header 'Authorization: Bearer ".$this->api_key."' ";
-			//echo " --header 'Content-Type: multipart/form-data' ";
-			//echo " --form 'file=@".$this->path.'/'.$this->filename."' ";
-			//echo " --form 'model=whisper-1' ";
-			//echo " --form 'response_format=text' ";
-			//echo "\n";
-
-			//start output buffer
-			ob_start();
-			$out = fopen('php://output', 'w');
-
-			// initialize a curl handle
-			$ch = curl_init();
-
-			// set the api_url if not already set
-			if (empty($this->api_url)) {
-				$this->api_url = 'https://api.openai.com/v1/audio/transcriptions';
-			}
-
-			// set the URL for the request
-			curl_setopt($ch, CURLOPT_URL, $this->api_url);
-
-			// set the request method to POST
-			curl_setopt($ch, CURLOPT_POST, true);
-
-			// set the request headers
-			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
-				'Authorization: Bearer '.$this->api_key,
-				'Content-Type: multipart/form-data'
-			));
-
-			// prepare the HTTP POST data
-			if (file_exists($this->path.'/'.$this->filename)) {
-				//send the audio from the file system
-				$post_data['file'] = new CURLFile($this->path.'/'.$this->filename);
-			}
-			elseif (!empty($this->audio_string)) {
-				//send the audio from as a string
-				$post_data['file'] = new CURLStringFile($this->audio_string, $this->filename, $this->audio_mime_type);
-			}
-			else {
-				//audio file or string not found
-				return false;
-			}
-
-			$post_data['model'] = $this->api_model;
-			$post_data['response_format'] = 'text';
-			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
-
-			// return the response as a string instead of outputting it directly
-			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
-			// set the connection timeout and the overall maximum curl run time
-			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
-			curl_setopt($ch, CURLOPT_TIMEOUT, 300);
-
-			// follow any "Location: " header the server sends as part of the HTTP header.
-			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
-
-			// automatically set the Referer: field in requests where it follows a Location: redirect.
-			curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
-
-			// set whether to verify SSL peer
-			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
-
-			// add verbose for debugging
-			curl_setopt($ch, CURLOPT_VERBOSE, true);
-			curl_setopt($ch, CURLOPT_STDERR, $out);
-
-			// run the curl request and transcription message
-			$this->message = curl_exec($ch);
-
-			// show the debug information
-			fclose($out);
-
-			// check for errors
-			if (curl_errno($ch)) {
-				echo 'Error: ' . curl_error($ch);
-				exit;
-			}
-
-			// close the handle
-			curl_close($ch);
-
-			// return the transcription
-			if (empty($this->message)) {
-				return '';
-			}
-			else {
-				return trim($this->message);
-			}
+		// follow any "Location: " header the server sends as part of the HTTP header.
+		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
+
+		// automatically set the Referer: field in requests where it follows a Location: redirect.
+		curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
+
+		// set whether to verify SSL peer
+		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
+
+		// add verbose for debugging
+		curl_setopt($ch, CURLOPT_VERBOSE, true);
+		curl_setopt($ch, CURLOPT_STDERR, $out);
+
+		// run the curl request and transcription message
+		$this->message = curl_exec($ch);
+
+		// show the debug information
+		fclose($out);
+
+		// check for errors
+		if (curl_errno($ch)) {
+			echo 'Error: ' . curl_error($ch);
+			exit;
 		}
 
-		public function set_model(string $model): void {
-			if (array_key_exists($model, $this->get_models())) {
-				$this->api_model = $model;
-			}
+		// close the handle
+		curl_close($ch);
+
+		// return the transcription
+		if (empty($this->message)) {
+			return '';
+		}
+		else {
+			return trim($this->message);
 		}
+	}
 
-		public function get_models(): array {
-			return [
-				'tts-1-hd' => 'tts-1-hd'
-			];
+	public function set_model(string $model): void {
+		if (array_key_exists($model, $this->get_models())) {
+			$this->api_model = $model;
 		}
+	}
 
+	public function get_models(): array {
+		return [
+			'tts-1-hd' => 'tts-1-hd'
+		];
 	}
-}
 
-?>
+}

+ 226 - 232
resources/classes/transcribe_watson.php

@@ -1,282 +1,276 @@
 <?php
 
+/**
+* transcribe_watson class
+*
+*/
+class transcribe_watson implements transcribe_interface {
+
+	/**
+	 * declare private variables
+	 */
+	private $api_key;
+	private $api_url;
+	private $language;
+	private $path;
+	private $filename;
+	private $audio_string;
+	private $audio_mime_type;
+	private $format;
+	private $voice;
+	private $message;
+	private $model;
+
+	/**
+	 * called when the object is created
+	 */
+	public function __construct($settings) {
+
+		//build the setting object and get the recording path
+		$this->api_key = $settings->get('transcribe', 'api_key');
+		$this->api_url = $settings->get('transcribe', 'api_url');
+		$this->language = $settings->get('transcribe', 'language');
 
- /**
- * transcribe_watson class
- *
- * @method null download
- */
-if (!class_exists('transcribe_watson')) {
-	class transcribe_watson implements transcribe_interface {
-
-		/**
-		 * declare private variables
-		 */
-		private $api_key;
-		private $api_url;
-		private $language;
-		private $path;
-		private $filename;
-		private $audio_string;
-		private $audio_mime_type;
-		private $format;
-		private $voice;
-		private $message;
-		private $model;
-
-		/**
-		 * called when the object is created
-		 */
-		public function __construct($settings) {
-
-			//build the setting object and get the recording path
-			$this->api_key = $settings->get('transcribe', 'api_key');
-			$this->api_url = $settings->get('transcribe', 'api_url');
-			$this->language = $settings->get('transcribe', 'language');
+	}
 
-		}
+	public function set_path(string $audio_path) {
+		$this->path = $audio_path;
+	}
 
-		public function set_path(string $audio_path) {
-			$this->path = $audio_path;
-		}
+	public function set_filename(string $audio_filename) {
+		$this->filename = $audio_filename;
+	}
 
-		public function set_filename(string $audio_filename) {
-			$this->filename = $audio_filename;
-		}
+	public function set_audio_string(string $audio_string) {
+		$this->audio_string = $audio_string;
+	}
 
-		public function set_audio_string(string $audio_string) {
-			$this->audio_string = $audio_string;
-		}
+	public function set_audio_mime_type(string $audio_mime_type) {
+		$this->audio_mime_type = $audio_mime_type;
+	}
 
-		public function set_audio_mime_type(string $audio_mime_type) {
-			$this->audio_mime_type = $audio_mime_type;
-		}
+	public function set_format(string $audio_format) {
+		$this->format = $audio_format;
+	}
 
-		public function set_format(string $audio_format) {
-			$this->format = $audio_format;
-		}
+	public function set_voice(string $audio_voice) {
+		$this->voice = $audio_voice;
+	}
 
-		public function set_voice(string $audio_voice) {
-			$this->voice = $audio_voice;
-		}
+	public function set_language(string $audio_language) {
+		$this->language = $audio_language;
+	}
 
-		public function set_language(string $audio_language) {
-			$this->language = $audio_language;
-		}
+	public function set_translate(string $audio_translate) {
+		$this->translate = $audio_translate;
+	}
 
-		public function set_translate(string $audio_translate) {
-			$this->translate = $audio_translate;
-		}
+	public function set_message(string $audio_message) {
+		$this->message = $audio_message;
+	}
 
-		public function set_message(string $audio_message) {
-			$this->message = $audio_message;
-		}
+	public function is_language_enabled() : bool {
+		//return the whether engine is handles languages
+		return false;
+	}
 
-		public function is_language_enabled() : bool {
-			//return the whether engine is handles languages
-			return false;
-		}
+	public function is_translate_enabled() : bool {
+		//return the whether engine is able to translate
+		return false;
+	}
 
-		public function is_translate_enabled() : bool {
-			//return the whether engine is able to translate
-			return false;
-		}
+	public function get_languages() : array {
+		//create the languages array
+		$languages = array(
+			"af" => "Afrikaans",
+			"ar" => "Arabic",
+			"hy" => "Armenian",
+			"az" => "Azerbaijani",
+			"be" => "Belarusian",
+			"bs" => "Bosnian",
+			"bg" => "Bulgarian",
+			"ca" => "Catalan",
+			"zh" => "Chinese",
+			"hr" => "Croatian",
+			"cs" => "Czech",
+			"da" => "Danish",
+			"nl" => "Dutch",
+			"en" => "English",
+			"et" => "Estonian",
+			"fi" => "Finnish",
+			"fr" => "French",
+			"gl" => "Galician",
+			"de" => "German",
+			"el" => "Greek",
+			"he" => "Hebrew",
+			"hi" => "Hindi",
+			"hu" => "Hungarian",
+			"is" => "Icelandic",
+			"id" => "Indonesian",
+			"it" => "Italian",
+			"ja" => "Japanese",
+			"kn" => "Kannada",
+			"kk" => "Kazakh",
+			"ko" => "Korean",
+			"lv" => "Latvian",
+			"lt" => "Lithuanian",
+			"mk" => "Macedonian",
+			"ms" => "Malay",
+			"mr" => "Marathi",
+			"mi" => "Maori",
+			"ne" => "Nepali",
+			"no" => "Norwegian",
+			"fa" => "Persian",
+			"pl" => "Polish",
+			"pt" => "Portuguese",
+			"ro" => "Romanian",
+			"ru" => "Russian",
+			"sr" => "Serbian",
+			"sk" => "Slovak",
+			"sl" => "Slovenian",
+			"es" => "Spanish",
+			"sw" => "Swahili",
+			"sv" => "Swedish",
+			"tl" => "Tagalog",
+			"ta" => "Tamil",
+			"th" => "Thai",
+			"tr" => "Turkish",
+			"uk" => "Ukrainian",
+			"ur" => "Urdu",
+			"vi" => "Vietnamese",
+			"cy" => "Welsh"
+		);
+
+		//return the languages array
+		return $languages;
+	}
 
-		public function get_languages() : array {
-			//create the languages array
-			$languages = array(
-				"af" => "Afrikaans",
-				"ar" => "Arabic",
-				"hy" => "Armenian",
-				"az" => "Azerbaijani",
-				"be" => "Belarusian",
-				"bs" => "Bosnian",
-				"bg" => "Bulgarian",
-				"ca" => "Catalan",
-				"zh" => "Chinese",
-				"hr" => "Croatian",
-				"cs" => "Czech",
-				"da" => "Danish",
-				"nl" => "Dutch",
-				"en" => "English",
-				"et" => "Estonian",
-				"fi" => "Finnish",
-				"fr" => "French",
-				"gl" => "Galician",
-				"de" => "German",
-				"el" => "Greek",
-				"he" => "Hebrew",
-				"hi" => "Hindi",
-				"hu" => "Hungarian",
-				"is" => "Icelandic",
-				"id" => "Indonesian",
-				"it" => "Italian",
-				"ja" => "Japanese",
-				"kn" => "Kannada",
-				"kk" => "Kazakh",
-				"ko" => "Korean",
-				"lv" => "Latvian",
-				"lt" => "Lithuanian",
-				"mk" => "Macedonian",
-				"ms" => "Malay",
-				"mr" => "Marathi",
-				"mi" => "Maori",
-				"ne" => "Nepali",
-				"no" => "Norwegian",
-				"fa" => "Persian",
-				"pl" => "Polish",
-				"pt" => "Portuguese",
-				"ro" => "Romanian",
-				"ru" => "Russian",
-				"sr" => "Serbian",
-				"sk" => "Slovak",
-				"sl" => "Slovenian",
-				"es" => "Spanish",
-				"sw" => "Swahili",
-				"sv" => "Swedish",
-				"tl" => "Tagalog",
-				"ta" => "Tamil",
-				"th" => "Thai",
-				"tr" => "Turkish",
-				"uk" => "Ukrainian",
-				"ur" => "Urdu",
-				"vi" => "Vietnamese",
-				"cy" => "Welsh"
-			);
-
-			//return the languages array
-			return $languages;
-		}
+	/**
+	 * transcribe - speech to text
+	 */
+	public function transcribe() : string {
 
-		/**
-		 * transcribe - speech to text
-		 */
-		public function transcribe() : string {
-
-			//get the content type
-			if (file_exists($this->path.'/'.$this->filename)) {
-				$path_array = pathinfo($this->path.'/'.$this->filename);
-				if ($path_array['extension'] == "mp3") {
-					$content_type = 'audio/mp3';
-				}
-				if ($path_array['extension'] == "wav") {
-					$content_type = 'audio/wav';
-				}
+		//get the content type
+		if (file_exists($this->path.'/'.$this->filename)) {
+			$path_array = pathinfo($this->path.'/'.$this->filename);
+			if ($path_array['extension'] == "mp3") {
+				$content_type = 'audio/mp3';
 			}
-			elseif (!empty($this->audio_string)) {
-				$content_type = $this->audio_mime_type;
+			if ($path_array['extension'] == "wav") {
+				$content_type = 'audio/wav';
 			}
+		}
+		elseif (!empty($this->audio_string)) {
+			$content_type = $this->audio_mime_type;
+		}
 
-			//start output buffer
-			//ob_start();
-
-			//open standard output
-			$out = fopen('php://output', 'w');
+		//start output buffer
+		//ob_start();
 
-			// initialize a curl handle
-			$ch = curl_init();
+		//open standard output
+		$out = fopen('php://output', 'w');
 
-			// set the URL for the request
-			curl_setopt($ch, CURLOPT_URL, $this->api_url);
+		// initialize a curl handle
+		$ch = curl_init();
 
-			// set the request method to POST
-			curl_setopt($ch, CURLOPT_POST, 1);
+		// set the URL for the request
+		curl_setopt($ch, CURLOPT_URL, $this->api_url);
 
-			//return the response as a string instead of outputting it directly
-			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+		// set the request method to POST
+		curl_setopt($ch, CURLOPT_POST, 1);
 
-			//send the autentication
-			curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . $this->api_key);
+		//return the response as a string instead of outputting it directly
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
-			//set the authentication type
-			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+		//send the autentication
+		curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . $this->api_key);
 
-			//send the content type
-			curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: '.$content_type]);
+		//set the authentication type
+		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 
-			//send the file as binary
-			curl_setopt($ch, CURLOPT_BINARYTRANSFER,TRUE);
+		//send the content type
+		curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: '.$content_type]);
 
-			//prepare to send the file or audio
-			if (file_exists($this->path.'/'.$this->filename)) {
-				//send the audio from the file system
-				curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($this->path.'/'.$this->filename));
-			}
-			elseif (!empty($this->audio_string)) {
-				//send the audio from as a string
-				curl_setopt($ch, CURLOPT_POSTFIELDS, $this->audio_string);
-			}
-			else {
-				//audio file or string not found
-				return false;
-			}
+		//send the file as binary
+		curl_setopt($ch, CURLOPT_BINARYTRANSFER,TRUE);
 
-			//set the connection timeout and the overall maximum curl run time
-			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
-			curl_setopt($ch, CURLOPT_TIMEOUT, 300);
+		//prepare to send the file or audio
+		if (file_exists($this->path.'/'.$this->filename)) {
+			//send the audio from the file system
+			curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($this->path.'/'.$this->filename));
+		}
+		elseif (!empty($this->audio_string)) {
+			//send the audio from as a string
+			curl_setopt($ch, CURLOPT_POSTFIELDS, $this->audio_string);
+		}
+		else {
+			//audio file or string not found
+			return false;
+		}
 
-			//To follow any "Location: " header that the server sends as part of the HTTP header.
-			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
+		//set the connection timeout and the overall maximum curl run time
+		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
+		curl_setopt($ch, CURLOPT_TIMEOUT, 300);
 
-			//To automatically set the Referer: field in requests where it follows a Location: redirect.
-			curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
+		//To follow any "Location: " header that the server sends as part of the HTTP header.
+		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
 
-			//Set whether to verify SSL peer
-			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
+		//To automatically set the Referer: field in requests where it follows a Location: redirect.
+		curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
 
-			//hide the headers when set to 0
-			curl_setopt($ch, CURLOPT_HEADER, 0);
+		//Set whether to verify SSL peer
+		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
 
-			//add verbose for debugging
-			curl_setopt($ch, CURLOPT_VERBOSE, true);
-			curl_setopt($ch, CURLOPT_STDERR, $out);
+		//hide the headers when set to 0
+		curl_setopt($ch, CURLOPT_HEADER, 0);
 
-			//show the debug information
-			fclose($out);
+		//add verbose for debugging
+		curl_setopt($ch, CURLOPT_VERBOSE, true);
+		curl_setopt($ch, CURLOPT_STDERR, $out);
 
-			//save the buffer
-			//$this->debug = ob_get_clean();
+		//show the debug information
+		fclose($out);
 
-			// run the curl request and transcription message
-			$json_response = curl_exec($ch);
+		//save the buffer
+		//$this->debug = ob_get_clean();
 
-			// check for errors
-			if (curl_errno($ch)) {
-				echo 'Error: ' . curl_error($ch);
-				exit;
-			}
+		// run the curl request and transcription message
+		$json_response = curl_exec($ch);
 
-			// close the handle
-			curl_close($ch);
+		// check for errors
+		if (curl_errno($ch)) {
+			echo 'Error: ' . curl_error($ch);
+			exit;
+		}
 
-			//get the content of the message
-			$json = json_decode($json_response, true);
+		// close the handle
+		curl_close($ch);
 
-			//validate the json
-			if ($json === null) {
-				return 'invalid json';
-			}
+		//get the content of the message
+		$json = json_decode($json_response, true);
 
-			//find the data
-			foreach($json['results'] as $row) {
-				$this->message .= $row['alternatives'][0]['transcript'];
-			}
+		//validate the json
+		if ($json === null) {
+			return 'invalid json';
+		}
 
-			//clean the data
-			$this->message = str_replace("%HESITATION", " ", trim($this->message));
-			$this->message = ucfirst($this->message);
+		//find the data
+		foreach($json['results'] as $row) {
+			$this->message .= $row['alternatives'][0]['transcript'];
+		}
 
-			// return the transcription
-			if (empty($this->message)) {
-				return '';
-			}
-			else {
-				return $this->message;
-			}
+		//clean the data
+		$this->message = str_replace("%HESITATION", " ", trim($this->message));
+		$this->message = ucfirst($this->message);
 
+		// return the transcription
+		if (empty($this->message)) {
+			return '';
+		}
+		else {
+			return $this->message;
 		}
 
 	}
-}
 
-?>
+}

+ 10 - 11
resources/interfaces/transcribe_interface.php

@@ -1,14 +1,13 @@
 <?php
 
-//define the template class
-if (!interface_exists('transcribe_interface')) {
-	interface transcribe_interface {
-		public function set_path(string $audio_path);
-		public function set_filename(string $audio_filename);
-		public function transcribe() : string;
-		public function set_language(string $audio_language);
-		public function get_languages() : array;
-	}
+/**
+* transcribe_interface template class
+*
+*/
+interface transcribe_interface {
+	public function set_path(string $audio_path);
+	public function set_filename(string $audio_filename);
+	public function transcribe() : string;
+	public function set_language(string $audio_language);
+	public function get_languages() : array;
 }
-
-?>