transcribe.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_string;
  21. public $audio_mime_type;
  22. public $audio_format;
  23. public $audio_model;
  24. public $audio_voice;
  25. public $audio_language;
  26. public $audio_message;
  27. /**
  28. * called when the object is created
  29. */
  30. public function __construct(settings $settings = null) {
  31. //make the setting object
  32. if ($settings === null) {
  33. $settings = new settings();
  34. }
  35. //add the settings object to the class
  36. $this->settings = $settings;
  37. //build the setting object and get the recording path
  38. $this->api_key = $settings->get('transcribe', 'api_key');
  39. $this->engine = $settings->get('transcribe', 'engine');
  40. }
  41. /**
  42. * transcribe - speech to text
  43. */
  44. public function transcribe() : string {
  45. if (!empty($this->engine)) {
  46. //set the class interface to use the _template suffix
  47. $classname = 'transcribe_'.$this->engine;
  48. if (empty($this->audio_path)) {
  49. $this->audio_path = null;
  50. }
  51. //create the object
  52. $object = new $classname($this->settings);
  53. //ensure the class has implemented the audio_interface interface
  54. if ($object instanceof transcribe_interface) {
  55. if ($object->is_language_enabled() && !empty($this->audio_language)) {
  56. $object->set_language($this->audio_language);
  57. }
  58. if (!empty($this->audio_string)) {
  59. $object->set_audio_string($this->audio_string);
  60. }
  61. if (!empty($this->audio_mime_type)) {
  62. $object->set_audio_mime_type($this->audio_mime_type);
  63. }
  64. $object->set_path($this->audio_path);
  65. $object->set_filename($this->audio_filename);
  66. return $object->transcribe();
  67. }
  68. else {
  69. return '';
  70. }
  71. }
  72. }
  73. }
  74. }
  75. ?>