Skip to content

Commit b2a3e07

Browse files
authored
chore: upgrade speech to new sample format (#1642)
1 parent 01ca49c commit b2a3e07

14 files changed

+510
-494
lines changed

speech/src/base64_encode_audio.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

24-
if (count($argv) != 2) {
25-
return print("Usage: php base64_encode_audio.php AUDIO_FILE\n");
26-
}
27-
list($_, $audioFile) = $argv;
24+
namespace Google\Cloud\Samples\Speech;
2825

2926
# [START base64_audio]
30-
/** Uncomment and populate these variables in your code */
31-
// $audioFile = 'path to an audio file';
27+
/**
28+
* @param string $audioFile path to an audio file
29+
*/
30+
function base64_encode_audio(string $audioFile)
31+
{
32+
$audioFileResource = fopen($audioFile, 'r');
33+
$base64Audio = base64_encode(stream_get_contents($audioFileResource));
34+
print($base64Audio);
35+
}
36+
# [END base64_audio]
3237

33-
$audioFileResource = fopen($audioFile, 'r');
34-
$base64Audio = base64_encode(stream_get_contents($audioFileResource));
35-
print($base64Audio);
36-
# [end base64_audio]
38+
// The following 2 lines are only needed to run the samples
39+
require_once __DIR__ . '/../../testing/sample_helpers.php';
40+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/multi_region_gcs.php

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,53 @@
1515
* limitations under the License.
1616
*/
1717

18-
# [START speech_transcribe_with_multi_region_gcs]
19-
# Includes the autoloader for libraries installed with composer
20-
require __DIR__ . '/../vendor/autoload.php';
18+
namespace Google\Cloud\Samples\Speech;
2119

20+
# [START speech_transcribe_with_multi_region_gcs]
2221
# Imports the Google Cloud client library
2322
use Google\Cloud\Speech\V1\SpeechClient;
2423
use Google\Cloud\Speech\V1\RecognitionAudio;
2524
use Google\Cloud\Speech\V1\RecognitionConfig;
2625
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
2726

28-
# The name of the audio file to transcribe
29-
$gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';
27+
/**
28+
* @param string $uri The Cloud Storage object to transcribe
29+
* e.x. gs://cloud-samples-data/speech/brooklyn_bridge.raw
30+
*/
31+
function multi_region_gcs(string $uri)
32+
{
33+
# set string as audio content
34+
$audio = (new RecognitionAudio())
35+
->setUri($uri);
3036

31-
# set string as audio content
32-
$audio = (new RecognitionAudio())
33-
->setUri($gcsURI);
37+
# The audio file's encoding, sample rate and language
38+
$config = new RecognitionConfig([
39+
'encoding' => AudioEncoding::LINEAR16,
40+
'sample_rate_hertz' => 16000,
41+
'language_code' => 'en-US'
42+
]);
3443

35-
# The audio file's encoding, sample rate and language
36-
$config = new RecognitionConfig([
37-
'encoding' => AudioEncoding::LINEAR16,
38-
'sample_rate_hertz' => 16000,
39-
'language_code' => 'en-US'
40-
]);
44+
# Specify a new endpoint.
45+
$options = ['apiEndpoint' => 'eu-speech.googleapis.com'];
4146

42-
# Specify a new endpoint.
43-
$options = ['apiEndpoint' => 'eu-speech.googleapis.com'];
47+
# Instantiates a client
48+
$client = new SpeechClient($options);
4449

45-
# Instantiates a client
46-
$client = new SpeechClient($options);
50+
# Detects speech in the audio file
51+
$response = $client->recognize($config, $audio);
4752

48-
# Detects speech in the audio file
49-
$response = $client->recognize($config, $audio);
53+
# Print most likely transcription
54+
foreach ($response->getResults() as $result) {
55+
$alternatives = $result->getAlternatives();
56+
$mostLikely = $alternatives[0];
57+
$transcript = $mostLikely->getTranscript();
58+
printf('Transcript: %s' . PHP_EOL, $transcript);
59+
}
5060

51-
# Print most likely transcription
52-
foreach ($response->getResults() as $result) {
53-
$alternatives = $result->getAlternatives();
54-
$mostLikely = $alternatives[0];
55-
$transcript = $mostLikely->getTranscript();
56-
printf('Transcript: %s' . PHP_EOL, $transcript);
61+
$client->close();
5762
}
58-
59-
$client->close();
6063
# [END speech_transcribe_with_multi_region_gcs]
64+
65+
// The following 2 lines are only needed to run the samples
66+
require_once __DIR__ . '/../../testing/sample_helpers.php';
67+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/profanity_filter.php

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,55 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START speech_profanity_filter]
16-
17-
# Includes the autoloader for libraries installed with composer
18-
require __DIR__ . '/../vendor/autoload.php';
19-
20-
if (count($argv) != 2) {
21-
return print("Usage: php profanity_filter.php AUDIO_FILE\n");
22-
}
23-
list($_, $audioFile) = $argv;
15+
namespace Google\Cloud\Samples\Speech;
2416

17+
# [START speech_profanity_filter]
2518
use Google\Cloud\Speech\V1\SpeechClient;
2619
use Google\Cloud\Speech\V1\RecognitionAudio;
2720
use Google\Cloud\Speech\V1\RecognitionConfig;
2821
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
2922

30-
/** Uncomment and populate these variables in your code */
31-
// $audioFile = 'path to an audio file';
23+
/**
24+
* @param string $audioFile path to an audio file
25+
*/
26+
function profanity_filter(string $audioFile)
27+
{
28+
// change these variables if necessary
29+
$encoding = AudioEncoding::LINEAR16;
30+
$sampleRateHertz = 32000;
31+
$languageCode = 'en-US';
32+
$profanityFilter = true;
3233

33-
// change these variables if necessary
34-
$encoding = AudioEncoding::LINEAR16;
35-
$sampleRateHertz = 32000;
36-
$languageCode = 'en-US';
37-
$profanityFilter = true;
34+
// get contents of a file into a string
35+
$content = file_get_contents($audioFile);
3836

39-
// get contents of a file into a string
40-
$content = file_get_contents($audioFile);
37+
// set string as audio content
38+
$audio = (new RecognitionAudio())
39+
->setContent($content);
4140

42-
// set string as audio content
43-
$audio = (new RecognitionAudio())
44-
->setContent($content);
41+
// set config
42+
$config = (new RecognitionConfig())
43+
->setEncoding($encoding)
44+
->setSampleRateHertz($sampleRateHertz)
45+
->setLanguageCode($languageCode)
46+
->setProfanityFilter($profanityFilter);
4547

46-
// set config
47-
$config = (new RecognitionConfig())
48-
->setEncoding($encoding)
49-
->setSampleRateHertz($sampleRateHertz)
50-
->setLanguageCode($languageCode)
51-
->setProfanityFilter($profanityFilter);
48+
// create the speech client
49+
$client = new SpeechClient();
5250

53-
// create the speech client
54-
$client = new SpeechClient();
51+
# Detects speech in the audio file
52+
$response = $client->recognize($config, $audio);
5553

56-
# Detects speech in the audio file
57-
$response = $client->recognize($config, $audio);
54+
# Print most likely transcription
55+
foreach ($response->getResults() as $result) {
56+
$transcript = $result->getAlternatives()[0]->getTranscript();
57+
printf('Transcript: %s' . PHP_EOL, $transcript);
58+
}
5859

59-
# Print most likely transcription
60-
foreach ($response->getResults() as $result) {
61-
$transcript = $result->getAlternatives()[0]->getTranscript();
62-
printf('Transcript: %s' . PHP_EOL, $transcript);
60+
$client->close();
6361
}
64-
65-
$client->close();
66-
6762
# [END speech_profanity_filter]
63+
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/profanity_filter_gcs.php

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,52 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START speech_profanity_filter_gcs]
16-
# Includes the autoloader for libraries installed with composer
17-
require __DIR__ . '/../vendor/autoload.php';
18-
19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if (count($argv) != 2) {
23-
return print("Usage: php profanity_filter_gcs.php AUDIO_FILE\n");
24-
}
25-
list($_, $audioFile) = $argv;
15+
namespace Google\Cloud\Samples\Speech;
2616

17+
# [START speech_profanity_filter_gcs]
2718
use Google\Cloud\Speech\V1\SpeechClient;
2819
use Google\Cloud\Speech\V1\RecognitionAudio;
2920
use Google\Cloud\Speech\V1\RecognitionConfig;
3021
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
3122

32-
/** The Cloud Storage object to transcribe */
33-
// $uri = 'The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name)';
34-
35-
// change these variables if necessary
36-
$encoding = AudioEncoding::LINEAR16;
37-
$sampleRateHertz = 32000;
38-
$languageCode = 'en-US';
39-
$profanityFilter = true;
40-
41-
// set string as audio content
42-
$audio = (new RecognitionAudio())
43-
->setUri($audioFile);
44-
45-
// set config
46-
$config = (new RecognitionConfig())
47-
->setEncoding($encoding)
48-
->setSampleRateHertz($sampleRateHertz)
49-
->setLanguageCode($languageCode)
50-
->setProfanityFilter($profanityFilter);
51-
52-
// create the speech client
53-
$client = new SpeechClient();
54-
55-
# Detects speech in the audio file
56-
$response = $client->recognize($config, $audio);
57-
58-
# Print most likely transcription
59-
foreach ($response->getResults() as $result) {
60-
$transcript = $result->getAlternatives()[0]->getTranscript();
61-
printf('Transcript: %s' . PHP_EOL, $transcript);
23+
/**
24+
* @param string $uri The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name)
25+
*/
26+
function profanity_filter_gcs(string $uri)
27+
{
28+
// change these variables if necessary
29+
$encoding = AudioEncoding::LINEAR16;
30+
$sampleRateHertz = 32000;
31+
$languageCode = 'en-US';
32+
$profanityFilter = true;
33+
34+
// set string as audio content
35+
$audio = (new RecognitionAudio())
36+
->setUri($uri);
37+
38+
// set config
39+
$config = (new RecognitionConfig())
40+
->setEncoding($encoding)
41+
->setSampleRateHertz($sampleRateHertz)
42+
->setLanguageCode($languageCode)
43+
->setProfanityFilter($profanityFilter);
44+
45+
// create the speech client
46+
$client = new SpeechClient();
47+
48+
# Detects speech in the audio file
49+
$response = $client->recognize($config, $audio);
50+
51+
# Print most likely transcription
52+
foreach ($response->getResults() as $result) {
53+
$transcript = $result->getAlternatives()[0]->getTranscript();
54+
printf('Transcript: %s' . PHP_EOL, $transcript);
55+
}
56+
57+
$client->close();
6258
}
63-
64-
$client->close();
65-
6659
# [END speech_profanity_filter_gcs]
60+
61+
// The following 2 lines are only needed to run the samples
62+
require_once __DIR__ . '/../../testing/sample_helpers.php';
63+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/streaming_recognize.php

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return print("Usage: php streaming_recognize.php AUDIO_FILE\n");
29-
}
30-
list($_, $audioFile) = $argv;
24+
namespace Google\Cloud\Samples\Speech;
3125

3226
# [START speech_transcribe_streaming]
3327
use Google\Cloud\Speech\V1\SpeechClient;
@@ -36,43 +30,50 @@
3630
use Google\Cloud\Speech\V1\StreamingRecognizeRequest;
3731
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
3832

39-
/** Uncomment and populate these variables in your code */
40-
// $audioFile = 'path to an audio file';
41-
42-
// change these variables if necessary
43-
$encoding = AudioEncoding::LINEAR16;
44-
$sampleRateHertz = 32000;
45-
$languageCode = 'en-US';
33+
/**
34+
* @param string $audioFile path to an audio file
35+
*/
36+
function streaming_recognize(string $audioFile)
37+
{
38+
// change these variables if necessary
39+
$encoding = AudioEncoding::LINEAR16;
40+
$sampleRateHertz = 32000;
41+
$languageCode = 'en-US';
4642

47-
$speechClient = new SpeechClient();
48-
try {
49-
$config = (new RecognitionConfig())
50-
->setEncoding($encoding)
51-
->setSampleRateHertz($sampleRateHertz)
52-
->setLanguageCode($languageCode);
43+
$speechClient = new SpeechClient();
44+
try {
45+
$config = (new RecognitionConfig())
46+
->setEncoding($encoding)
47+
->setSampleRateHertz($sampleRateHertz)
48+
->setLanguageCode($languageCode);
5349

54-
$strmConfig = new StreamingRecognitionConfig();
55-
$strmConfig->setConfig($config);
50+
$strmConfig = new StreamingRecognitionConfig();
51+
$strmConfig->setConfig($config);
5652

57-
$strmReq = new StreamingRecognizeRequest();
58-
$strmReq->setStreamingConfig($strmConfig);
53+
$strmReq = new StreamingRecognizeRequest();
54+
$strmReq->setStreamingConfig($strmConfig);
5955

60-
$strm = $speechClient->streamingRecognize();
61-
$strm->write($strmReq);
56+
$strm = $speechClient->streamingRecognize();
57+
$strm->write($strmReq);
6258

63-
$strmReq = new StreamingRecognizeRequest();
64-
$content = file_get_contents($audioFile);
65-
$strmReq->setAudioContent($content);
66-
$strm->write($strmReq);
59+
$strmReq = new StreamingRecognizeRequest();
60+
$content = file_get_contents($audioFile);
61+
$strmReq->setAudioContent($content);
62+
$strm->write($strmReq);
6763

68-
foreach ($strm->closeWriteAndReadAll() as $response) {
69-
foreach ($response->getResults() as $result) {
70-
foreach ($result->getAlternatives() as $alt) {
71-
printf("Transcription: %s\n", $alt->getTranscript());
64+
foreach ($strm->closeWriteAndReadAll() as $response) {
65+
foreach ($response->getResults() as $result) {
66+
foreach ($result->getAlternatives() as $alt) {
67+
printf("Transcription: %s\n", $alt->getTranscript());
68+
}
7269
}
7370
}
71+
} finally {
72+
$speechClient->close();
7473
}
75-
} finally {
76-
$speechClient->close();
7774
}
7875
# [END speech_transcribe_streaming]
76+
77+
// The following 2 lines are only needed to run the samples
78+
require_once __DIR__ . '/../../testing/sample_helpers.php';
79+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)