Files
fusionpbx/core/ai/resources/classes/ai.php
T

190 lines
4.5 KiB
PHP
Raw Normal View History

2024-03-22 23:14:12 -06:00
<?php
/**
* audio class
*
* @method null download
*/
if (!class_exists('ai')) {
class ai {
/**
* declare private variables
*/
private $transcribe_key;
private $speech_key;
/** @var string $engine */
private $transcribe_engine;
private $speech_engine;
/** @var template_engine $object */
private $transcribe_object;
private $speech_object;
2024-03-23 17:17:21 -06:00
private $settings;
2024-03-22 23:14:12 -06:00
public $audio_path;
public $audio_filename;
public $audio_format;
2024-03-30 17:16:16 -03:00
public $audio_model;
2024-03-22 23:14:12 -06:00
public $audio_voice;
2024-03-23 17:17:21 -06:00
public $audio_language;
2024-03-22 23:14:12 -06:00
public $audio_message;
/**
* called when the object is created
*/
2024-03-23 17:17:21 -06:00
public function __construct(settings $settings = null) {
2024-03-22 23:14:12 -06:00
//make the setting object
2024-03-23 17:17:21 -06:00
if ($settings === null) {
$settings = new settings();
2024-03-22 23:14:12 -06:00
}
2024-03-23 17:17:21 -06:00
//add the settings object to the class
$this->settings = $settings;
2024-03-22 23:14:12 -06:00
//build the setting object and get the recording path
2024-03-23 17:17:21 -06:00
$this->transcribe_key = $settings->get('ai', 'transcribe_key');
$this->transcribe_engine = $settings->get('ai', 'transcribe_engine');
$this->speech_key = $settings->get('ai', 'speech_key');
$this->speech_engine = $settings->get('ai', 'speech_engine');
}
/**
* get_voices - get the list voices
*/
public function get_voices() : array {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the voices array
return $object->get_voices();
}
2024-03-30 17:16:16 -03:00
/**
* get_voices - get the list voices
*/
public function get_models() : array {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the voices array
return $object->get_models();
}
2024-03-23 17:17:21 -06:00
/**
2024-03-25 12:10:16 -06:00
* is_translate_enabled - get whether the engine can do translations
2024-03-23 17:17:21 -06:00
*/
2024-03-25 12:10:16 -06:00
public function is_translate_enabled() : bool {
2024-03-23 17:17:21 -06:00
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the translate_enabled
2024-03-25 12:10:16 -06:00
return $object->is_translate_enabled();
2024-03-23 17:17:21 -06:00
}
/**
2024-03-25 12:10:16 -06:00
* is_language_enabled - get whether the engine allows to set the language
2024-03-23 17:17:21 -06:00
*/
2024-03-25 12:10:16 -06:00
public function is_language_enabled() : bool {
2024-03-23 17:17:21 -06:00
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the language_enabled
2024-03-25 12:10:16 -06:00
return $object->is_language_enabled();
2024-03-23 17:17:21 -06:00
}
/**
* get_languages - get the list languages
*/
public function get_languages() : array {
//set the class interface to use the _template suffix
$classname = 'ai_'.$this->speech_engine;
//create the object
$object = new $classname($this->settings);
//return the languages array
return $object->get_languages();
2024-03-22 23:14:12 -06:00
}
/**
* speech - text to speech
*/
public function speech() {
if (!empty($this->speech_engine)) {
//set the class interface to use the _template suffix
2024-03-23 17:17:21 -06:00
$classname = 'ai_'.$this->speech_engine;
2024-03-22 23:14:12 -06:00
//create the object
2024-03-23 17:17:21 -06:00
$object = new $classname($this->settings);
2024-03-22 23:14:12 -06:00
//ensure the class has implemented the audio_interface interface
2024-03-23 17:17:21 -06:00
if ($object instanceof ai_speech) {
2024-03-22 23:14:12 -06:00
$object->set_path($this->audio_path);
$object->set_filename($this->audio_filename);
$object->set_format($this->audio_format);
$object->set_voice($this->audio_voice);
2024-03-30 19:42:23 -06:00
//$object->set_model($this->audio_model);
2024-03-23 17:17:21 -06:00
//$object->set_language($this->audio_language);
//$object->set_translate($this->audio_translate);
2024-03-22 23:14:12 -06:00
$object->set_message($this->audio_message);
$object->speech();
}
else {
return false;
}
}
}
/**
* transcribe - speech to text
*/
public function transcribe() : string {
if (!empty($this->transcribe_engine)) {
//set the class interface to use the _template suffix
2024-03-23 17:17:21 -06:00
$classname = 'ai_'.$this->transcribe_engine;
2024-03-22 23:14:12 -06:00
//create the object
2024-03-23 17:17:21 -06:00
$object = new $classname($this->settings);
2024-03-25 12:10:16 -06:00
2024-03-22 23:14:12 -06:00
//ensure the class has implemented the audio_interface interface
2024-03-23 17:17:21 -06:00
if ($object instanceof ai_transcribe) {
2024-03-25 12:10:16 -06:00
if ($object->is_language_enabled() && !empty($this->audio_language)) {
$object->set_language($this->audio_language);
}
2024-03-22 23:14:12 -06:00
$object->set_path($this->audio_path);
$object->set_filename($this->audio_filename);
return $object->transcribe();
}
else {
return '';
}
}
}
}
}
2024-03-30 19:42:23 -06:00
?>