From 689aa46e7c4d2f3c88f3bb8dfcb2596aebdffed7 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Fri, 3 Mar 2023 18:55:40 -0500 Subject: [PATCH] Plugin: Text2Speech: Allow to generate audio for LP items - refs #4622 --- main/lp/lp_add_audio.php | 91 +++++++++++++++++++ main/lp/lp_controller.php | 19 ++++ plugin/text2speech/convert.php | 70 ++++++++++++++ plugin/text2speech/lang/english.php | 2 + plugin/text2speech/lang/french.php | 2 + plugin/text2speech/lang/spanish.php | 2 + .../text2speech/src/mozillatts/MozillaTTS.php | 4 +- 7 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 plugin/text2speech/convert.php diff --git a/main/lp/lp_add_audio.php b/main/lp/lp_add_audio.php index 08228600d76..ba58d7f73b5 100755 --- a/main/lp/lp_add_audio.php +++ b/main/lp/lp_add_audio.php @@ -208,6 +208,96 @@ $page .= $recordVoiceForm; $page .= '
'; $page .= $form->returnForm(); + +$text2speechPlugin = Text2SpeechPlugin::create(); + +if ($text2speechPlugin->isEnabled(true)) { + $page .= '
+ +
+
+

+ +

+ + +

+ +

+
+
+ + '; +} + $page .= ''; @@ -243,6 +333,7 @@ $page .= ''; $page .= ''; $page .= ''; + $page .= ''; $page .= ''; diff --git a/main/lp/lp_controller.php b/main/lp/lp_controller.php index 7281a08ab04..824845e40aa 100755 --- a/main/lp/lp_controller.php +++ b/main/lp/lp_controller.php @@ -711,6 +711,25 @@ function(reponse) { exit; } + if (Text2SpeechPlugin::create()->isEnabled(true) + && isset($_GET['tts']) && 1 === (int) $_GET['tts'] + ) { + $audioPath = api_get_path(SYS_UPLOAD_PATH).'plugins/text2speech/'.basename($_POST['file']); + + $fileInfo = new SplFileInfo($audioPath); + + if ($fileInfo->isReadable()) { + $_FILES['file'] = [ + 'name' => $fileInfo->getFilename(), + 'type' => 'audio/'.$fileInfo->getExtension(), + 'tmp_name' => $fileInfo->getRealPath(), + 'error' => UPLOAD_ERR_OK, + 'size' => $fileInfo->getSize(), + 'copy_file' => true, + ]; + } + } + // Upload audio if (isset($_FILES['file']) && !empty($_FILES['file'])) { // Updating the lp.modified_on diff --git a/plugin/text2speech/convert.php b/plugin/text2speech/convert.php new file mode 100644 index 00000000000..44179d67934 --- /dev/null +++ b/plugin/text2speech/convert.php @@ -0,0 +1,70 @@ +isEnabled(true) + || !$isAllowedToEdit + ) { + throw new Exception(); + } + + $textToConvert = ''; + + if ($httpRequest->query->has('text')) { + $textToConvert = $httpRequest->query->get('text'); + } elseif ($httpRequest->query->has('item_id')) { + $itemId = $httpRequest->query->getInt('item_id'); + + $item = $em->find(CLpItem::class, $itemId); + + if (!$item) { + throw new Exception(); + } + + $course = api_get_course_entity($item->getCId()); + $documentRepo = $em->getRepository(CDocument::class); + + $document = $documentRepo->findOneBy([ + 'cId' => $course->getId(), + 'iid' => $item->getPath(), + ]); + + if (!$document) { + throw new Exception(); + } + + $textToConvert = file_get_contents( + api_get_path(SYS_COURSE_PATH).$course->getDirectory().'/document/'.$document->getPath() + ); + $textToConvert = strip_tags($textToConvert); + } + + if (empty($textToConvert)) { + throw new Exception(); + } + + $path = $plugin->convert($textToConvert); + + $httpResponse->setContent($path); +} catch (Exception $exception) { + $httpResponse->setStatusCode(HttpResponse::HTTP_BAD_REQUEST); +} + +$httpResponse->send(); diff --git a/plugin/text2speech/lang/english.php b/plugin/text2speech/lang/english.php index 0e726ced1e9..dcf24eeeb45 100755 --- a/plugin/text2speech/lang/english.php +++ b/plugin/text2speech/lang/english.php @@ -4,3 +4,5 @@ $strings['plugin_title'] = 'Text to Speech (Text2Speech)'; $strings['plugin_comment'] = 'Plugin to convert text to speech using a 3rd-party service'; $strings['tool_enable'] = 'Enable plugin'; + +$strings['GenerateAudioFromContent'] = 'Generate audio from content'; diff --git a/plugin/text2speech/lang/french.php b/plugin/text2speech/lang/french.php index e1e3de0eb36..ee1bcad2f49 100755 --- a/plugin/text2speech/lang/french.php +++ b/plugin/text2speech/lang/french.php @@ -4,3 +4,5 @@ $strings['plugin_title'] = 'Texte pour parler (Text2Speech)'; $strings['plugin_comment'] = "Ce plugin permet de convertir du texte en parole à l'aide d'un service tiers"; $strings['tool_enable'] = 'Activer le plug-in'; + +$strings['GenerateAudioFromContent'] = 'Generate audio from content'; diff --git a/plugin/text2speech/lang/spanish.php b/plugin/text2speech/lang/spanish.php index 57c34c470f6..b7afb63bfdf 100755 --- a/plugin/text2speech/lang/spanish.php +++ b/plugin/text2speech/lang/spanish.php @@ -3,3 +3,5 @@ $strings['plugin_title'] = 'Texto a Voz (Text2Speech)'; $strings['plugin_comment'] = 'Este plugin es para convertir texto a voz usando un servicio de terceros'; $strings['tool_enable'] = 'Enable plugin'; + +$strings['GenerateAudioFromContent'] = 'Generar audio desde el contenido'; diff --git a/plugin/text2speech/src/mozillatts/MozillaTTS.php b/plugin/text2speech/src/mozillatts/MozillaTTS.php index 41d33e2efff..c15c26041cc 100644 --- a/plugin/text2speech/src/mozillatts/MozillaTTS.php +++ b/plugin/text2speech/src/mozillatts/MozillaTTS.php @@ -24,7 +24,7 @@ private function request(string $data): string { $filename = uniqid().'.wav'; $filePath = $this->filePath.$filename; - $resource = fopen($filePath, 'w'); +// $resource = fopen(realpath($filePath), 'w'); $client = new GuzzleHttp\Client(); $client->get($this->url.'?api_key='.urlencode($this->apiKey). @@ -33,7 +33,7 @@ private function request(string $data): string 'Cache-Control' => 'no-cache', 'Content-Type' => 'audio/wav', ], - 'sink' => $resource, + 'sink' => $filePath, ]); return $filename;