2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2005 by Greg Gay */
8 /* This program is free software. You can redistribute it and/or*/
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /****************************************************************/
12 // $Id: atalkerlib.inc.php 5123 2005-07-12 14:59:03Z greg
14 // Common functions etc used thoughout ATalker
17 // Setup the speech directories if they don't yet exist
18 if($_SESSION['privileges'] == AT_ADMIN_PRIV_ADMIN){
20 // where admin audio files are saved
21 define('AT_SPEECH_TEMPLATE_ROOT', AT_CONTENT_DIR.'template/');
22 define('AT_SPEECH_TEMPLATE_DIR', AT_CONTENT_DIR.'template/'.$_SESSION['lang'].'/');
23 define('AT_SPEECH_TEMPLATE_URL', $_base_href.'content/template/'.$_SESSION['lang'].'/');
24 define('AT_SPEECH_FILES_DIR', AT_CONTENT_DIR.'template/temp/');
25 define('AT_SPEECH_URL', $_base_href.'content/template/temp/');
26 define('AT_SPEECH_DIR', AT_CONTENT_DIR.'template/temp/');
28 // See if the speech directories exists yet, and create them if they don't
29 if(!opendir(AT_SPEECH_TEMPLATE_ROOT)){
30 mkdir(AT_SPEECH_TEMPLATE_ROOT, 0755);
32 if(!opendir(AT_SPEECH_DIR)){
33 mkdir(AT_SPEECH_DIR, 0755);
36 if(!opendir(AT_SPEECH_FILES_DIR)){
37 mkdir(AT_SPEECH_FILES_DIR, 0755);
40 if(!opendir(AT_SPEECH_TEMPLATE_DIR)){
41 mkdir(AT_SPEECH_TEMPLATE_DIR, 0755);
45 define('AT_SPEECH_DIR', AT_CONTENT_DIR.'speech/');
46 define('AT_SPEECH_FILES_DIR', AT_CONTENT_DIR.$_SESSION['course_id'].'/speech/');
47 define('AT_SPEECH_URL', $_base_href.'content/speech/');
48 // See if the speech directories exists yet, and create them if they don't
49 if(@!opendir(AT_SPEECH_DIR)){
50 mkdir(AT_SPEECH_DIR, 0755);
52 if($_SESSION['course_id'] != "0"){
53 if(@!opendir(AT_SPEECH_FILES_DIR)){
54 mkdir(AT_SPEECH_FILES_DIR, 0755);
60 //Validate filename form field
61 if($_POST['filename']){
62 if (!(eregi("^[a-zA-Z0-9_]([a-zA-Z0-9_\.])*$", $_POST['filename']))) {
63 $error = "TTS_FILE_CHARS";
64 $msg->addError($error);
68 if($_GET['postdata']){
69 $postdata = stripslashes($_GET['postdata']);
70 $_POST = unserialize($postdata);
74 $postdata = serialize($_POST);
77 // Garbage collector: delete tempfiles after $filelife seconds
78 function clean_tts_files(){
79 //global AT_SPEECH_DIR;
80 $filelife = "1200"; //1200 seconds = 20 minutes
81 if ($handle = opendir(AT_SPEECH_DIR)){
82 while (false !== ($file = readdir($handle))) {
83 $rawfile = split("\.", $file);
84 if($rawfile[0] != ''){
85 $dir_files[$i] .= "$rawfile[0]\n";
87 if($this_now - $rawfile[0] > $filelife){
88 unlink(AT_SPEECH_DIR.$file);
97 // Manage ATalker tabs and popup window
98 if ((isset($_REQUEST['popup'])) && ($_REQUEST['popup'] == TRUE)) {
100 $popup_win = "popup=1";
104 // Read feedback messages out load
107 * Create scm file to pass to text2wave to define the voice, the volumn and the speed of the wave to build
109 @author Eura Ercolani
110 @return The complete path of the generated file
113 function build_scm_file()
117 //get voice info from the table AT_languages and AT_voices
118 $sql_command = "SELECT voice_name, voice_volumn, voice_speed FROM ".TABLE_PREFIX."languages, ".TABLE_PREFIX."voices WHERE language_code='";
119 $sql_command .= $_SESSION[lang]."' AND ".TABLE_PREFIX."languages.voice_id = ".TABLE_PREFIX."voices.voice_id";
120 $result = mysql_query($sql_command, $db);
121 $row = mysql_fetch_row($result);
122 $voice_name = $row[0]; //name of the voice to use
123 $volumn = $row[1]; //range value between 1(low) and 10 (high)
124 $speed = $row[2]; //range value between 0.6 (very fast) and 2.0 (very slow)
125 $scheme_file_txt .= "(";
126 $scheme_file_txt .= $voice_name;
127 $scheme_file_txt .= ")\n";
128 $scheme_file_txt .= "(";
129 $scheme_file_txt .= "Parameter.set 'Duration_Stretch ".$speed;
130 $scheme_file_txt .= ")";
132 //Define the scm file name
134 $scheme_file_name = AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$now.'.scm';
135 //Open the file for output
136 $fp = fopen($scheme_file_name,'w');
139 echo _AT(AT_ERROR_TTS__NOT_CREATE_SCHEME);
142 //Write into the file
143 fputs($fp, $scheme_file_txt);
146 $voice_info[0] = $scheme_file_name;
147 $voice_info[1] = $volumn;
154 * Reads aloud error and feedback messages
156 * @param array $messages a list of messages sent to the $msg->printAll() function;
157 * @param array $vals a list of subistute (i.e.for %s within language) values for dynamic messages
162 function read_messages($messages, $vals){
163 global $_base_href, $course_base_href, $msg, $play, $val, $db;
164 /* Modified by Eura Ercolani: mimetype support - BEGIN */
166 /* Modified by Eura Ercolani: mimetype support - END */
167 foreach ($messages as $item){
168 $sql = "SELECT * from ".TABLE_PREFIX."language_text WHERE language_code = '$_SESSION[lang]' AND term = '$item'";
169 $result = mysql_query($sql, $db);
171 while($row = mysql_fetch_row($result)){
173 /* Modified by Eura Ercolani: messages localization - BEGIN */
174 //check to see if the folder exists....
175 if(!is_dir(AT_MSGS_DIR.DIRECTORY_SEPARATOR.$_SESSION[lang]))//folder does not exists, I make it
176 mkdir(AT_MSGS_DIR.$_SESSION[lang]);
177 //$file_in = AT_MSGS_DIR.$row[2].'.txt';
178 $file_in = AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.txt';
179 //$file_out = AT_MSGS_DIR.$row[2].'.wav';
180 $file_out = AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.wav';
181 //$file_out_mp3 = AT_MSGS_DIR.$row[2].'.mp3';
182 $file_out_mp3 = AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.mp3';
183 //$file_recieve = AT_MSGS_URL.$row[2].'.mp3';
184 $file_recieve = AT_MSGS_URL.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.mp3';
185 /* Modified by Eura Ercolani: messages localization - END */
187 if(file_exists($file_out_mp3)){
189 /* Modified by Eura Ercolani: mime type support - BEGIN */
190 //echo '<embed src="'.$file_recieve.'" autostart="true" hidden="true" volumn="10" ></embed>';
191 echo '<embed src="'.$file_recieve.'" autostart="true" height="0" width="0" volumn="10" type="'.$_SESSION['mp3HiddenMimeType'].'"></embed>';
192 /* Modified by Eura Ercolani: mime type support - END */
196 $fp = fopen($file_in,'w');
199 echo AT_ERROR_TTS_NOT_CREATE_TEXT;
203 $message = strip_tags($row[3]);
204 $message = str_replace("%s", $vals[$row[2]], $message);
205 fputs($fp, $message);
207 /* Modified by Eura Ercolani: voice setting - BEGIN */
208 $voice_info = build_scm_file();
209 $command = "text2wave ".$file_in." -o ".$file_out;
210 $command .= " -F 48000 -scale ".$voice_info[1]." -eval ".$voice_info[0];
211 //$command = "text2wave $file_in -o $file_out -F 48000";
212 /* Modified by Eura Ercolani: voice setting - BEGIN */
214 if(shell_exec('lame --longhelp')){
216 $command2 .= ' lame --quiet '.$file_out.' '. $file_out_mp3;
218 }else if (shell_exec('bladeenc -h')) {
220 $command2 .= ' bladeenc -quiet '.$file_out.' '. $file_out_mp3;
223 escapeshellcmd($command);
224 escapeshellcmd($command2);
228 /* Modified by Eura Ercolani: mimetype support - BEGIN */
229 //echo '<embed src="'.$file_recieve.'" autostart="true" hidden="true" volumn="10" ></embed>';
230 echo '<embed src="'.$file_recieve.'" autostart="true" height="0" width="0" volumn="10" type="'.$_SESSION['mp3HiddenMimeType'].'"></embed>';
231 /* Modified by Eura Ercolani: mimetype support - END */
234 /* Modified by Eura Ercolani: delete scm file - BEGIN */
235 unlink($voice_info[0]);
236 /* Modified by Eura Ercolani: delete scm file - END */
243 // List tabs for the ATalker
244 function get_atalker_tabs() {
245 //these are the _AT(x) variable names and their include file
246 $tabs[0] = array('text_reader', 'index.php', '');
247 $tabs[1] = array('sable_reader', 'index.php', '');
248 if($_SESSION['privileges'] == AT_ADMIN_PRIV_ADMIN){
249 $tabs[2] = array('voice_files', 'index.php', '');
256 // Check to see what encoders are available: currently supported encoders include: lame. bladeenc, oggenc
258 function get_encoders(){
259 global $select, $_POST;
261 $command2 = 'bladeenc -h';
262 $command2 = escapeshellcmd($command2);
263 $command3 = 'lame --version';
264 $command3 = escapeshellcmd($command3);
265 if(shell_exec($command2) != '' || shell_exec($command3) != ''){
266 echo '<option value="mp3"';
267 if($_POST['file_type'] == 'mp3'){
270 echo '>MP3</option>';
273 $command = 'oggenc --version';
274 $command = escapeshellcmd($command);
275 if(shell_exec($command) != ''){
276 echo '<option value="ogg"';
277 if($_POST['file_type'] == 'ogg'){
280 echo '>OGG</option>';
285 // Send the user to the right place after each TTS action
287 global $_POST, $now, $file_recieve, $error, $postdata, $msg, $file_save, $filename, $tab, $voice_file, $lang_var, $messages;
290 if(!$error && !$_GET['page']){
291 if($_POST['download']){
292 //unset($_POST['export'])
293 header('Content-type: audio/x-'.$_POST['file_type']);
294 header('Content-Disposition: attachment; filename="'.$now.'.'.$_POST['file_type']);
295 readfile($file_recieve);
297 }else if($_POST['save']){
298 $file_save = str_replace(" ", "_", $file_save);
301 if(@fopen($file_save, r)){
302 $error= array(TTS_FILE_EXISTS, $filename);
303 $msg->addError($error);
305 if(copy($file_recieve, $file_save)){
306 $feedback = array(TTS_FILE_SAVED, $filename);
307 $msg->addFeedback($feedback);
309 $error = array(TTS_FILE_SAVE_FAILED, $filename);
310 $msg->addError($error);
313 unset($_POST['save']);
314 }else if($_POST['export']){
315 unset($_POST['export']);
316 header('Content-type: text/plain');
317 header("Location:".AT_SPEECH_URL.$now.".sable");
321 //header('Content-type: audio/x-'.$_POST['file_type']);
322 header('Content-type: audio/x-'.$_POST['file_type']);
323 header("Location:".$file_recieve);