-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathGoogleSpeechToText.php
208 lines (184 loc) · 4.97 KB
/
GoogleSpeechToText.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Convert FLAC files to Text using the Google Speech API
*
* Credit due to Mike (mike@mikepultz.com) for his first version of this.
*
* @version 0.1
* @author Roger Thomas
* @see
*
*/
class GoogleSpeechToText
{
/**
* URL of the Speech API
* @var string
*/
const SPEECH_BASE_URL = 'https://www.google.com/speech-api/full-duplex/v1/';
/**
* A 'unique' string to use for the requests
*
* @var string
*/
private $requestPair;
/**
* The Google Auth API Key
*
* @var string
*/
private $apiKey;
/**
* CURL Upload Handle
*
* @var resource
*/
private $uploadHandle;
/**
* CURL Download Handle
*
* @var resource
*/
private $downloadHandle;
/**
* Construct giving the Google Auth API Key.
*
* @param string $apiKey
* @throws Exception
*/
public function __construct($apiKey)
{
if (empty($apiKey)) {
throw new Exception('$apiKey should not be empty.');
}
$this->apiKey = $apiKey;
$this->requestPair = $this->getPair();
$this->setupCurl();
}
/**
* Setup CURL requests, both up and down.
*/
private function setupCurl()
{
$this->uploadHandle = curl_init();
$this->downloadHandle = curl_init();
curl_setopt(
$this->downloadHandle,
CURLOPT_URL,
self::SPEECH_BASE_URL . 'down?pair=' . $this->requestPair
);
curl_setopt(
$this->downloadHandle,
CURLOPT_RETURNTRANSFER,
true
);
curl_setopt(
$this->uploadHandle,
CURLOPT_RETURNTRANSFER,
true
);
curl_setopt(
$this->uploadHandle,
CURLOPT_POST,
true
);
}
/**
* Generate a Pair for the request. This identifies the requests later.
*
* @return string
*/
private function getPair()
{
$c = '0123456789';
$s = '';
for ($i=0; $i<16; $i++) {
$s .= $c[rand(0, strlen($c) - 1)];
}
return $s;
}
/**
* Make the request, returning either an array, or boolean false on
* failure.
*
* @param string $file the file name to process
* @param integer $rate the bitrate of the flac content (example: 44100)
* @param string $language the ISO language code
* (en-US has been confirmed as working)
* @throws Exception
* @return array|boolean false for failure.
*/
public function process($file, $rate, $language = 'en-US')
{
if (!$file || !file_exists($file) || !is_readable($file)) {
throw new Exception(
'$file must be specified and be a valid location.'
);
}
$data = file_get_contents($file);
if (!$data) {
throw new Exception('Unable to read ' . $file);
}
if (empty($rate) || !is_integer($rate)) {
throw new Exception('$rate must be specified and be an integer');
}
curl_setopt(
$this->uploadHandle,
CURLOPT_URL,
self::SPEECH_BASE_URL . 'up?lang=' .
$language . '&lm=dictation&client=chromium&pair=' .
$this->requestPair . '&key=' . $this->apiKey
);
curl_setopt(
$this->uploadHandle,
CURLOPT_HTTPHEADER,
array(
'Transfer-Encoding: chunked',
'Content-Type: audio/x-flac; rate=' . $rate
)
);
curl_setopt(
$this->uploadHandle,
CURLOPT_POSTFIELDS,
array(
'file' => $data
)
);
$curlMulti = curl_multi_init();
curl_multi_add_handle($curlMulti, $this->downloadHandle);
curl_multi_add_handle($curlMulti, $this->uploadHandle);
$active = null;
do {
curl_multi_exec($curlMulti, $active);
} while ($active > 0);
$res = curl_multi_getcontent($this->downloadHandle);
$output = array();
$results = explode("\n", $res);
foreach ($results as $result) {
$object = json_decode($result, true);
if (
(isset($object['result']) == true) &&
(count($object['result']) > 0)
) {
foreach ($object['result'] as $obj) {
$output[] = $obj;
}
}
}
curl_multi_remove_handle($curlMulti, $this->downloadHandle);
curl_multi_remove_handle($curlMulti, $this->uploadHandle);
curl_multi_close($curlMulti);
if (empty($output)) {
return false;
}
return $output;
}
/**
* Close any outstanding connections in the destruct
*/
public function __destruct()
{
curl_close($this->uploadHandle);
curl_close($this->downloadHandle);
}
}