Skip to content

Commit 38a9cf2

Browse files
authored
Add Speech Streaming (GoogleCloudPlatform#342)
* add speech streaming * changes the dist to trusty and adds sudo for grpc installation.
1 parent b11f89d commit 38a9cf2

File tree

6 files changed

+294
-1
lines changed

6 files changed

+294
-1
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ branches:
1717
- master
1818

1919
language: php
20+
sudo: required
21+
dist: trusty
2022

2123
matrix:
2224
include:
@@ -40,6 +42,9 @@ before_install:
4042
- popd
4143
- mkdir -p build/logs
4244

45+
before_script:
46+
- pecl install grpc
47+
4348
script:
4449
- testing/run_test_suite.sh
4550

speech/api/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
"require": {
33
"google/cloud-speech": "^0.2",
44
"google/cloud-storage": "^1.0",
5+
"google/gax": "^0.8.1",
6+
"google/proto-client-php": "^0.10.0",
57
"symfony/console": "^3.0"
68
},
79
"autoload": {
810
"psr-4": {
911
"Google\\Cloud\\Samples\\Speech\\": "src/"
1012
},
1113
"files": [
14+
"src/functions/streaming_recognize.php",
1215
"src/functions/transcribe_async_gcs.php",
1316
"src/functions/transcribe_sync.php",
1417
"src/functions/transcribe_sync_gcs.php"

speech/api/composer.lock

Lines changed: 175 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

speech/api/src/TranscribeCommand.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ protected function configure()
7575
InputOption::VALUE_NONE,
7676
'Run the transcription asynchronously. '
7777
)
78+
->addOption(
79+
'stream',
80+
null,
81+
InputOption::VALUE_OPTIONAL,
82+
'Stream the audio file. Supply an argument to stream from a mic.'
83+
)
7884
;
7985
}
8086

@@ -93,6 +99,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
9399
list($bucketName, $objectName) = array_slice($matches, 1);
94100
}
95101
if ($isGcs) {
102+
if ($input->getOption('stream')) {
103+
throw new LogicException('Cannot stream from a bucket!');
104+
}
96105
if ($input->getOption('async')) {
97106
transcribe_async_gcs($bucketName, $objectName, $languageCode, $options);
98107
} else {
@@ -101,6 +110,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
101110
} else {
102111
if ($input->getOption('async')) {
103112
throw new LogicException('Async requests require a GCS URI');
113+
}
114+
if ($input->getOption('stream')) {
115+
$encodingInt = constant("google\cloud\speech\\v1\RecognitionConfig\AudioEncoding::$encoding");
116+
streaming_recognize($audioFile, $languageCode, $encodingInt, $sampleRate);
104117
} else {
105118
transcribe_sync($audioFile, $languageCode, $options);
106119
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Speech;
25+
26+
# [START streaming_recognize]
27+
use Google\Cloud\Speech\V1\SpeechClient;
28+
use google\cloud\speech\v1\RecognitionConfig;
29+
use google\cloud\speech\v1\StreamingRecognitionConfig;
30+
use google\cloud\speech\v1\StreamingRecognizeRequest;
31+
32+
/**
33+
* Transcribe an audio file using Google Cloud Speech API
34+
* Example:
35+
* ```
36+
* $audoEncoding = google\cloud\speech\v1\RecognitionConfig\AudioEncoding::WAV
37+
* streaming_recognize('/path/to/audiofile.wav', 'en-US');
38+
* ```.
39+
*
40+
* @param string $audioFile path to an audio file.
41+
* @param string $languageCode The language of the content to
42+
* be recognized. Accepts BCP-47 (e.g., `"en-US"`, `"es-ES"`).
43+
* @param string $encoding
44+
* @param string $sampleRateHertz
45+
*
46+
* @return string the text transcription
47+
*/
48+
function streaming_recognize($audioFile, $languageCode, $encoding, $sampleRateHertz)
49+
{
50+
if (!defined('Grpc\STATUS_OK')) {
51+
throw new \Exception('Install the grpc extension ' .
52+
'(pecl install grpc)');
53+
}
54+
if (!class_exists('google\cloud\speech\v1\SpeechGrpcClient')) {
55+
throw new \Exception('Install the proto client PHP library ' .
56+
'(composer require google/proto-client-php)');
57+
}
58+
if (!class_exists('Google\GAX\GrpcConstants')) {
59+
throw new \Exception('Install the GAX library ' .
60+
'(composer require google/gax)');
61+
}
62+
63+
$speechClient = new SpeechClient();
64+
try {
65+
$config = new RecognitionConfig();
66+
$config->setLanguageCode($languageCode);
67+
$config->setEncoding($encoding);
68+
$config->setSampleRateHertz($sampleRateHertz);
69+
70+
$strmConfig = new StreamingRecognitionConfig();
71+
$strmConfig->setConfig($config);
72+
73+
$strmReq = new StreamingRecognizeRequest();
74+
$strmReq->setStreamingConfig($strmConfig);
75+
76+
$strm = $speechClient->streamingRecognize();
77+
$strm->write($strmReq);
78+
79+
$strmReq = new StreamingRecognizeRequest();
80+
$f = fopen($audioFile, "rb");
81+
$fsize = filesize($audioFile);
82+
$bytes = fread($f, $fsize);
83+
$strmReq->setAudioContent($bytes);
84+
$strm->write($strmReq);
85+
86+
foreach ($strm->closeWriteAndReadAll() as $response) {
87+
foreach ($response->getResults() as $result) {
88+
foreach ($result->getAlternatives() as $alt) {
89+
printf("Transcription: %s\n", $alt->getTranscript());
90+
}
91+
}
92+
}
93+
} finally {
94+
$speechClient->close();
95+
}
96+
}
97+
# [END streaming_recognize]

speech/api/test/TranscribeCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function provideTranscribe()
8686
return [
8787
[__DIR__ . '/data/audio32KHz.raw', 'LINEAR16', '32000'],
8888
[__DIR__ . '/data/audio32KHz.flac', 'FLAC', '32000'],
89+
[__DIR__ . '/data/audio32KHz.raw', 'LINEAR16', '32000', ['--stream' => true]],
8990
['gs://' . self::$bucketName . '/audio32KHz.raw', 'LINEAR16', '32000'],
9091
['gs://' . self::$bucketName . '/audio32KHz.raw', 'LINEAR16', '32000', ['--async' => true]],
9192
];

0 commit comments

Comments
 (0)