-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIvonaAGI.php
108 lines (88 loc) · 3.32 KB
/
IvonaAGI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
class IvonaAGI extends AGI {
private $tmp = '/tmp';
private $sox = '/usr/bin/sox';
private $mpg123 = '/usr/bin/mpg123';
private $enableDebug =1;
private $format = null;
private $codec = null;
private $samplerate = null;
private $speed = 'slow';
private $lang = 'en-GB';
private $voice = 'Amy';
public function __construct()
{
parent::__construct();
$this->debug(__METHOD__." Created instance of ".__CLASS__);
}
private function debug($message)
{
if ($this->enableDebug){
syslog(LOG_DEBUG, $message);
}
}
public function say_tts($text, $lang = null, $voice = null, $speed = null, $noanswer=null)
{
$ivona = new IvonaClient();
//$ivona->ListVoices("pl-PL", null);
if(!$lang){
$lang = $this->lang;
}
if(!$voice){
$voice = $this->voice;
}
if(!$speed){
$speed = $this->speed;
}
$filename = $ivona->getSave($text, array('Language' => $lang, 'VoiceName'=>$voice, 'VoiceRate'=>$speed));
$this->convertMP3ToWAV($filename);
$this->detectFormat();
$destFile = $this->CreateAsteriskFile($filename,$lang, $voice, $speed);
$this->debug(__METHOD__ . " Will try to play file: $destFile");
if($destFile) {
$playstring = $destFile;
if ($noanswer){
$playstring .= ",noanswer";
}
}
$this->exec("Playback", $playstring);
}
private function convertMP3ToWAV($filename){
$pathParts = pathinfo($filename);
$WAVFilename = $pathParts['dirname'].'/'. basename($filename, ".mp3").".wav";
if(file_exists($WAVFilename)){
$this->debug(__METHOD__ . " WAV Filename already exists: $filename ".$WAVFilename);
return 1;
}
$mpg123cmd = $this->mpg123." -q -w $WAVFilename $filename ";
$this->debug(__METHOD__ . " executing mpg123: $mpg123cmd");
$retMpg123 = exec($mpg123cmd);
$this->debug(__METHOD__ . " mpg123 returned $retMpg123");
}
private function CreateAsteriskFile($filename,$lang, $voice, $speed){
$pathParts = pathinfo($filename);
$destFile = $pathParts['dirname'].'/' . basename($filename, ".wav") . "_" . $this->samplerate."_".$lang . "_".$voice."_".$speed ;
if (file_exists($destFile . "." . $this->format)){
$this->debug(__METHOD__ . " Asterisk sound file already exists: ".$destFile);
return $destFile;
}
$soxcmd = $this->sox ." ". $filename . " -q -r " . $this->samplerate . " -t raw ". $destFile . "." . $this->format;
$this->debug(__METHOD__ . " executing sox: $soxcmd");
exec($soxcmd);
return $destFile;
}
private function detectFormat(){
$codec = $this->get_variable("CHANNEL(audionativeformat)"); $codec = $codec['data'];
$this->debug(__METHOD__ . " Detected codec: $codec");
$this->codec = $codec;
if (preg_match('/(silk|sln)12/', $codec)) { $this->format = "sln12" ; $this->samplerate = 12000;}
elseif (preg_match('/(speex|slin|silk)16|g722|siren7/', $codec)) { $this->format = "sln16" ; $this->samplerate = 16000;}
elseif (preg_match('/(speex|slin|celt)32|siren14/', $codec)) { $this->format = "sln32"; $this->samplerate = 32000;}
elseif (preg_match('/(celt|slin)44/', $codec)) { $this->format = "sln44"; $this->samplerate = 44100;}
elseif (preg_match('/(celt|slin)48/', $codec)) { $this->format = "sln48"; $this->samplerate = 48000;}
else { $this->format = "sln"; $this->samplerate = 8000;}
$this->debug(__METHOD__ . " Samling rate for this format is : ". $this->format.":".$this->samplerate);
return 1;
}
}
?>