Skip to content

Commit

Permalink
Plugin: Text2Speech: Allow to generate audio for LP items - refs cham…
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Mar 4, 2023
1 parent 87c808c commit 99d34c8
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 2 deletions.
102 changes: 102 additions & 0 deletions main/lp/lp_add_audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,108 @@
$page .= '<ul class="lp_resource">'.$documentTree.'</ul>';
$page .= '</li>';
$page .= '</ul>';

$text2speechPlugin = Text2SpeechPlugin::create();

if ($text2speechPlugin->isEnabled(true)) {
$page .= '<div class="clearfix"></div>
<h3 class="page-header">
<small>'.get_lang('Or').'</small>
'.$text2speechPlugin->get_title().'
</h3>
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<p>
<button id="btn-tts" class="btn btn-default" type="button">
<span id="btn-tts__spinner" class="fa fa-spinner fa-spin" aria-hidden="true" style="display: none;"></span>
'.$text2speechPlugin->get_lang('GenerateAudioFromContent').'
</button>
</p>
<p id="tts-player" id="tts-player" style="display: none;">
<audio controls class="skip"></audio>
</p>
<p id="tts-warning" class="alert alert-warning" style="display: none;">
'.get_lang('ErrorOccurred').'
</p>
<p>
<button id="btn-save-tts" class="btn btn-primary" type="button" disabled>
<span id="btn-save-tts__spinner" class="fa fa-spinner fa-spin" aria-hidden="true" style="display: none;"></span>
'.get_lang('SaveRecordedAudio').'
</button>
</p>
</div>
</div>
<script>
$(function () {
var btnTts = $(\'#btn-tts\');
var btnTssSpiner = $(\'#btn-tts__spinner\');
var ttsPlayer = $(\'#tts-player\');
var ttsPlayerAudio = $(\'#tts-player audio\');
var ttsWarning = $(\'#tts-warning\');
var btnSaveTts = $(\'#btn-save-tts\');
var btnSaveTtsSpiner = $(\'#btn-save-tts__spinner\');
btnTts.on(\'click\', function (e) {
e.preventDefault();
ttsWarning.hide();
btnTts.prop(\'disabled\', true);
btnTssSpiner.show();
$
.ajax(_p.web_plugin + \'text2speech/convert.php?item_id='.$lp_item_id.'\')
.done(function (response) {
ttsPlayer.show();
ttsPlayerAudio.prop(\'src\', response).mediaelementplayer();
btnSaveTts.prop(\'disabled\', false);
})
.fail(function () {
ttsPlayer.hide();
ttsWarning.show();
btnSaveTts.prop(\'disabled\', true);
})
.always(function () {
btnTssSpiner.hide();
btnTts.prop(\'disabled\', false);
});
});
btnSaveTts.on(\'click\', function () {
btnSaveTts.prop(\'disabled\', true);
btnSaveTtsSpiner.show();
var xhr = new XMLHttpRequest();
xhr.open(\'GET\', ttsPlayerAudio.prop(\'src\'));
xhr.responseType = \'blob\';
xhr.onload = function (recordedBlob) {
console.log(recordedBlob);
var formData = new FormData();
formData.append(\'file\', recordedBlob.target.response, \'audio-'.$lp_item_id.'\.wav\');
formData.append(\'id\', '.$lp_item_id.');
$.ajax({
url: $(\'#add_audio\').prop(\'action\'),
data: formData,
processData: false,
contentType: false,
type: \'POST\',
success: function (fileURL) {
if (!fileURL) {
return;
}
window.location.reload();
}
});
};
xhr.send();
});
});
</script>
';
}

$page .= '</div>';
$page .= '</div>';

Expand Down
70 changes: 70 additions & 0 deletions plugin/text2speech/convert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/* For license terms, see /license.txt */

use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\CourseBundle\Entity\CLpItem;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response as HttpResponse;

require_once __DIR__.'/../../main/inc/global.inc.php';

$httpRequest = HttpRequest::createFromGlobals();
$httpResponse = HttpResponse::create();

$plugin = Text2SpeechPlugin::create();

$isAllowedToEdit = api_is_allowed_to_edit(false, true);

$em = Database::getManager();

try {
if (!$plugin->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();
2 changes: 2 additions & 0 deletions plugin/text2speech/lang/english.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions plugin/text2speech/lang/french.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions plugin/text2speech/lang/spanish.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
4 changes: 2 additions & 2 deletions plugin/text2speech/src/mozillatts/MozillaTTS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -33,7 +33,7 @@ private function request(string $data): string
'Cache-Control' => 'no-cache',
'Content-Type' => 'audio/wav',
],
'sink' => $resource,
'sink' => $filePath,
]);

return $filename;
Expand Down

0 comments on commit 99d34c8

Please sign in to comment.