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 6, 2023
1 parent f3eb877 commit 3bd89d2
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 2 deletions.
91 changes: 91 additions & 0 deletions main/lp/lp_add_audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,96 @@
$page .= $recordVoiceForm;
$page .= '<br>';
$page .= $form->returnForm();

$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\');
var audioSrc = \'\';
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) {
audioSrc = response;
ttsPlayer.show();
ttsPlayerAudio.prop(\'src\', audioSrc).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();
$.ajax({
type: \'post\',
url: \''.api_get_self().'?action=add_audio&tts=1&id='.$lp_item_id.'&'.api_get_cidreq().'&lp_id='.$learnpath_id.'\',
data: {
file: audioSrc,
},
success: function () {
window.location.reload();
},
});
});
});
</script>
';
}

$page .= '<h3 class="page-header">
<small>'.get_lang('Or').'</small> '.get_lang('SelectAnAudioFileFromDocuments').'</h3>';

Expand Down Expand Up @@ -243,6 +333,7 @@
$page .= '<ul class="lp_resource">'.$documentTree.'</ul>';
$page .= '</li>';
$page .= '</ul>';

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

Expand Down
19 changes: 19 additions & 0 deletions main/lp/lp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 3bd89d2

Please sign in to comment.