1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* PHP version 5.5 |
5
|
|
|
* |
6
|
|
|
* @package TTS |
7
|
|
|
* @author Sergey V.Kuzin <[email protected]> |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace TTS; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use TTS\Helper\SplitterText; |
16
|
|
|
use TTS\Traits\OnlineTrait; |
17
|
|
|
|
18
|
|
|
class Yandex extends TextToSpeechAbstract |
19
|
|
|
{ |
20
|
|
|
use OnlineTrait; |
21
|
|
|
|
22
|
|
|
const SPEAKER_JANE = 'jane'; |
23
|
|
|
const SPEAKER_OKSANA = 'oksana'; |
24
|
|
|
const SPEAKER_ALYSS = 'alyss'; |
25
|
|
|
const SPEAKER_OMAZH = 'omazh'; |
26
|
|
|
const SPEAKER_ZAHAR = 'zahar'; |
27
|
|
|
const SPEAKER_ERMIL = 'ermil'; |
28
|
|
|
|
29
|
|
|
protected $driver = 'Yandex'; |
30
|
|
|
|
31
|
|
|
protected $vote = self::VOTE_FEMALE; |
32
|
|
|
|
33
|
|
|
protected $speaker = 'oksana'; |
34
|
|
|
|
35
|
|
|
protected $apiKey; |
36
|
|
|
/** |
37
|
|
|
* @param string $apiKey |
38
|
|
|
* @param LoggerInterface $logger |
39
|
|
|
* @param array $options |
40
|
|
|
*/ |
41
|
|
|
public function __construct($apiKey, LoggerInterface $logger = null, array $options = []) |
42
|
|
|
{ |
43
|
|
|
if (!is_string($apiKey)) { |
44
|
|
|
throw new \InvalidArgumentException('$apiKey must string'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->apiKey = $apiKey; |
48
|
|
|
parent::__construct($logger, $options); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function synthesize($text, $outFile) |
52
|
|
|
{ |
53
|
|
|
$sp = new SplitterText(); |
54
|
|
|
$text = iconv('UTF-8', 'UTF-8', $text); |
55
|
|
|
|
56
|
|
|
$lines = $sp->split($text, 512); |
57
|
|
|
$content = ''; |
58
|
|
View Code Duplication |
foreach ($lines as $line) { |
59
|
|
|
$query = [ |
60
|
|
|
'format' => 'mp3', |
61
|
|
|
'quality' => 'hi', |
62
|
|
|
'text' => $line, |
63
|
|
|
'speaker' => $this->speaker, // jane, omazh, zahar, ermil |
64
|
|
|
'emotion' => 'good', // neutral (доброжелательный), neutral(нейтральный), evil (злой). |
|
|
|
|
65
|
|
|
'lang' => 'ru‑RU', |
66
|
|
|
'speed' => 1.2, |
67
|
|
|
//'drunk' => 'true', |
|
|
|
|
68
|
|
|
//'robot' => 'true', |
|
|
|
|
69
|
|
|
//'ill' => 'true', |
|
|
|
|
70
|
|
|
'key' => $this->apiKey |
71
|
|
|
]; |
72
|
|
|
$content .= $this->reguestGet($query); |
73
|
|
|
} |
74
|
|
|
file_put_contents($outFile, $content); |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getUri() |
79
|
|
|
{ |
80
|
|
|
//return 'http://tts.voicetech.yandex.net/tts'; |
81
|
|
|
return 'https://tts.voicetech.yandex.net/generate'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getSpeaker() |
88
|
|
|
{ |
89
|
|
|
return $this->speaker; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setOksanaSpeaker() |
93
|
|
|
{ |
94
|
|
|
$this->speaker = self::SPEAKER_OKSANA; |
95
|
|
|
$this->vote = self::VOTE_FEMALE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setAlyssSpeaker() |
99
|
|
|
{ |
100
|
|
|
$this->speaker = self::SPEAKER_ALYSS; |
101
|
|
|
$this->vote = self::VOTE_FEMALE; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setJaneSpeaker() |
105
|
|
|
{ |
106
|
|
|
$this->speaker = self::SPEAKER_JANE; |
107
|
|
|
$this->vote = self::VOTE_FEMALE; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setOmazhSpeaker() |
111
|
|
|
{ |
112
|
|
|
$this->speaker = self::SPEAKER_OMAZH; |
113
|
|
|
$this->vote = self::VOTE_FEMALE; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function setZaharSpeaker() |
117
|
|
|
{ |
118
|
|
|
$this->speaker = self::SPEAKER_ZAHAR; |
119
|
|
|
$this->vote = self::VOTE_MALE; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setErmilSpeaker() |
123
|
|
|
{ |
124
|
|
|
$this->speaker = self::SPEAKER_ERMIL; |
125
|
|
|
$this->vote = self::VOTE_MALE; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $speaker |
130
|
|
|
*/ |
131
|
|
|
public function setSpeaker($speaker) |
132
|
|
|
{ |
133
|
|
|
$this->speaker = $speaker; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.