-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.php
46 lines (39 loc) · 1.07 KB
/
voice.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
<?php
class Voice
{
private static $voices = array(
'alex', 'bruce', 'fred', 'junior', 'agnes', 'kathy', 'princess', 'vicki', 'victoria', 'albert',
'bad news', 'bahh', 'bells', 'boing', 'bubbles', 'cellos', 'deranged',
'good news', 'hysterical', 'pipe organ', 'trinoids', 'whisper', 'zarvox',
);
private $voice;
/**
* Create new voice for named voice
*/
public function __construct($voice)
{
if (!in_array($voice, self::$voices)) {
throw new Exception('No such voice, ' . $voice);
}
$this->voice = $voice;
}
/**
* Say the given phrase using system "say" command
* @param string $phrase Phrase to say
*/
public function say($phrase)
{
$safePhrase = escapeshellarg($phrase);
passthru("say -v {$this->voice} $safePhrase");
}
/**
* Say the phrase with a randomly chosen voice
* @param string $phrase Phrase to say
*/
public static function sayWithRandomVoice($phrase)
{
$index = mt_rand(0, count(self::$voices) - 1);
$v = new Voice(self::$voices[$index]);
$v->say($phrase);
}
}