Advertisement
Guest User

Untitled

a guest
May 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #!/usr/bin/php -q
  2. <?php
  3. // License for all code of this FreePBX module can be found in the license file inside the module directory
  4. // Copyright 2013 Schmooze Com Inc.
  5. // Xavier Ourciere xourciere[at]propolys[dot]com
  6. //
  7.  
  8. $config = parse_amportal_conf( "/etc/amportal.conf" );
  9.  
  10. require_once "phpagi.php";
  11. require_once "phpagi-asmanager.php";
  12.  
  13. $AGI = new AGI();
  14. debug("TTS AGI Started", 1);
  15.  
  16. $text = $argv[1];
  17. $hash = md5($text);
  18. $engine = $argv[2];
  19. $enginebin = $argv[3];
  20.  
  21. $f = $AGI->get_full_variable('${CHANNEL(audionativeformat)}');
  22. $nformat = $f['data'];
  23. $format = array(
  24. "ext" => "wav",
  25. "rate" => "8000"
  26. );
  27.  
  28. //amazing work my friend: https://github.com/stevenmirabito/asterisk-picotts/blob/master/picotts.agi#L251
  29. switch(true) {
  30. case preg_match('/(silk|sln)12/',$nformat):
  31. $format = array(
  32. "ext" => "sln12",
  33. "rate" => "12000"
  34. );
  35. break;
  36. case preg_match('/(speex|slin|silk)16|g722|siren7/',$nformat):
  37. $format = array(
  38. "ext" => "sln16",
  39. "rate" => "16000"
  40. );
  41. break;
  42. case preg_match('/(speex|slin|celt)32|siren14/',$nformat):
  43. $format = array(
  44. "ext" => "sln32",
  45. "rate" => "32000"
  46. );
  47. break;
  48. case preg_match('/(celt|slin)44/',$nformat):
  49. $format = array(
  50. "ext" => "sln44",
  51. "rate" => "44000"
  52. );
  53. break;
  54. case preg_match('/(celt|slin)48/',$nformat):
  55. $format = array(
  56. "ext" => "sln48",
  57. "rate" => "48000"
  58. );
  59. break;
  60. default;
  61. $format = array(
  62. "ext" => "wav",
  63. "rate" => "8000"
  64. );
  65. break;
  66. }
  67.  
  68. if (!isset($text))
  69. {
  70. return 0;
  71. }
  72.  
  73. if ( $retval != 0 ) {
  74. debug("ERROR: TTS engine binary not found.", 1);
  75. return $retval;
  76. }
  77.  
  78. $soundsdir = $config["ASTVARLIBDIR"]."/sounds/tts";
  79. if( !is_dir($soundsdir) ) mkdir($soundsdir, 0775);
  80.  
  81. $wavefile = $soundsdir."/$engine-tts-$hash.".$format['ext'];
  82.  
  83. $tmpwavefile = $soundsdir."/$engine-tts-temp-$hash.wav";
  84. debug("Generated WAV file: $wavefile", 3);
  85. $textfile = $soundsdir."/$engine-tts-$hash.txt";
  86. debug("TXT file: $textfile", 3);
  87.  
  88. if ( !file_exists($wavefile) ) {
  89. debug("Text to speech wave file doesnt exist, lets create it.", 1);
  90. if ( false === ($fh = fopen($textfile, "w")) ) {
  91. debug("ERROR: Cannot open the file: $textfile", 1);
  92. return 1;
  93. }
  94. if ( false === fwrite($fh, $text) ) {
  95. debug("ERROR: Cannot write to file: $textfile", 1);
  96. return 1;
  97. }
  98. fclose($fh);
  99. debug("Executing $engine", 1);
  100. switch ($engine) {
  101. case 'text2wave':
  102. exec($enginebin." -f ".$format['rate']." -o $tmpwavefile $textfile");
  103. break;
  104. case 'flite':
  105. exec($enginebin." -f $textfile -o $tmpwavefile");
  106. break;
  107. case 'swift':
  108. exec($enginebin." -p audio/channels=1,audio/sampling-rate=".$format['rate']." -o $tmpwavefile -f $textfile");
  109. break;
  110. case 'pico':
  111. exec($enginebin." -o $tmpwavefile ".escapeshellarg(file_get_contents($textfile)));
  112. break;
  113. case 'aws-polly':
  114. $mp3 = "--mp3=/var/lib/asterisk/sounds/tts/$engine-tts-$hash.mp3";
  115. $txt = "--text=\"$text\"";
  116. $wav = "--wav=/var/lib/asterisk/sounds/tts/$engine-tts-$hash";
  117. $exec = "$enginebin /opt/aws-nodejs-sample/script.js $mp3 $txt $wav";
  118. debug($mp3);
  119. debug($txt);
  120. debug($wav);
  121. debug($exec);
  122. $result = exec($exec);
  123. debug($result);
  124. break;
  125.  
  126. default:
  127. debug("$engine is not a valid engine!", 1);
  128. break;
  129. }
  130. }
  131. if(file_exists($tmpwavefile)) {
  132. exec("sox $tmpwavefile -q -r ".$format['rate']." -t raw $wavefile");
  133. unlink($tmpwavefile);
  134. }
  135.  
  136. if(file_exists($wavefile)) {
  137. // Adding a wait because the first time the wave file is generated, it was not played
  138. $AGI->wait_for_digit(1000);
  139. debug("Streaming the generated wave.", 1);
  140. $AGI->stream_file("tts/".basename($wavefile,".".$format['ext']),'#');
  141. } else {
  142. debug("File was not created!", 1);
  143. }
  144. debug("TTS AGI end", 1);
  145.  
  146. function parse_amportal_conf($filename) {
  147. $file = file($filename);
  148. $matches = array();
  149. $matchpattern = '/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/';
  150. foreach ($file as $line) {
  151. if (preg_match($matchpattern, $line, $matches)) {
  152. $conf[ $matches[1] ] = $matches[2];
  153. }
  154. }
  155. return $conf;
  156. }
  157.  
  158. function debug($string, $level=3) {
  159. global $AGI;
  160. $AGI->verbose($string, $level);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement