From c93033471c67434a884f8aae1826560b95273a3c 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 generate audio for LP items - refs #4622 --- main/lp/lp_add_audio.php | 100 ++++++++++++++++++ plugin/text2speech/convert.php | 71 +++++++++++++ .../text2speech/src/mozillatts/MozillaTTS.php | 4 +- 3 files changed, 173 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..b245ed01939 100755 --- a/main/lp/lp_add_audio.php +++ b/main/lp/lp_add_audio.php @@ -243,6 +243,106 @@ $page .= ''; $page .= ''; $page .= ''; + +if (Text2SpeechPlugin::create()->isEnabled(true)) { + $page .= '
+ +
+
+

+ +

+ + +

+ +

+
+
+ + '; +} + $page .= ''; $page .= ''; diff --git a/plugin/text2speech/convert.php b/plugin/text2speech/convert.php new file mode 100644 index 00000000000..e8625a7aa4f --- /dev/null +++ b/plugin/text2speech/convert.php @@ -0,0 +1,71 @@ +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/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;