transcribe.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * transcribe class
  4. *
  5. * @method null download
  6. */
  7. if (!class_exists('transcribe')) {
  8. class transcribe {
  9. /**
  10. * declare private variables
  11. */
  12. private $api_key;
  13. /** @var string $engine */
  14. private $engine;
  15. /** @var template_engine $object */
  16. private $transcribe_object;
  17. private $settings;
  18. public $audio_path;
  19. public $audio_filename;
  20. public $audio_format;
  21. public $audio_model;
  22. public $audio_voice;
  23. public $audio_language;
  24. public $audio_message;
  25. /**
  26. * called when the object is created
  27. */
  28. public function __construct(settings $settings = null) {
  29. //make the setting object
  30. if ($settings === null) {
  31. $settings = new settings();
  32. }
  33. //add the settings object to the class
  34. $this->settings = $settings;
  35. //build the setting object and get the recording path
  36. $this->api_key = $settings->get('transcribe', 'api_key');
  37. $this->engine = $settings->get('transcribe', 'engine');
  38. }
  39. /**
  40. * transcribe - speech to text
  41. */
  42. public function transcribe() : string {
  43. if (!empty($this->engine)) {
  44. //set the class interface to use the _template suffix
  45. $classname = 'transcribe_'.$this->engine;
  46. //create the object
  47. $object = new $classname($this->settings);
  48. //ensure the class has implemented the audio_interface interface
  49. if ($object instanceof transcribe_interface) {
  50. if ($object->is_language_enabled() && !empty($this->audio_language)) {
  51. $object->set_language($this->audio_language);
  52. }
  53. $object->set_path($this->audio_path);
  54. $object->set_filename($this->audio_filename);
  55. return $object->transcribe();
  56. }
  57. else {
  58. return '';
  59. }
  60. }
  61. }
  62. }
  63. }
  64. ?>