Skip to content

Commit 4ace73f

Browse files
committed
update the rest of the speech samples
1 parent 3a4e080 commit 4ace73f

13 files changed

+111
-57
lines changed

speech/src/create_recognizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function create_recognizer(
3636
string $model = "latest_short"
3737
): void {
3838
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
39-
$speechClient = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
39+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
4040

4141
// Create a Recognizer
4242
$recognizer = new Recognizer([
@@ -52,7 +52,7 @@ function create_recognizer(
5252
]);
5353

5454
// Call the createRecognizer method
55-
$operation = $speechClient->createRecognizer($createRecognizerRequest);
55+
$operation = $speech->createRecognizer($createRecognizerRequest);
5656

5757
// Wait for the operation to complete
5858
$operation->pollUntilComplete();
@@ -64,7 +64,7 @@ function create_recognizer(
6464
print_r($operation->getError());
6565
}
6666

67-
$speechClient->close();
67+
$speech->close();
6868
}
6969

7070
// The following 2 lines are only needed to run the samples

speech/src/delete_recognizer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@
2929
*/
3030
function delete_recognizer(string $projectId, string $location, string $recognizerId): void
3131
{
32-
$speechClient = new SpeechClient();
32+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
33+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
3334

3435
// Create the DeleteRecognizerRequest
3536
$deleteRecognizerRequest = new DeleteRecognizerRequest([
3637
'name' => SpeechClient::recognizerName($projectId, $location, $recognizerId)
3738
]);
3839

3940
// Call the deleteRecognizer method
40-
$operation = $speechClient->deleteRecognizer($deleteRecognizerRequest);
41+
$operation = $speech->deleteRecognizer($deleteRecognizerRequest);
4142

4243
// Wait for the operation to complete
4344
$operation->pollUntilComplete();
@@ -48,7 +49,7 @@ function delete_recognizer(string $projectId, string $location, string $recogniz
4849
print_r($operation->getError());
4950
}
5051

51-
$speechClient->close();
52+
$speech->close();
5253
}
5354

5455
// The following 2 lines are only needed to run the samples

speech/src/multi_region_gcs.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ function multi_region_gcs(string $projectId, string $location, string $recognize
3838

3939
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
4040

41-
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
4241
$config = (new RecognitionConfig())
42+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
43+
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());
44+
4345
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
4446
'encoding' => AudioEncoding::LINEAR16,
4547
'sample_rate_hertz' => 16000,

speech/src/profanity_filter.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
function profanity_filter(string $projectId, string $location, string $recognizerId, string $audioFile)
3232
{
3333
// create the speech client
34-
$client = new SpeechClient();
34+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
35+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
3536

3637
// get contents of a file into a string
3738
$content = file_get_contents($audioFile);
@@ -45,6 +46,10 @@ function profanity_filter(string $projectId, string $location, string $recognize
4546

4647
$config = (new RecognitionConfig())
4748
->setFeatures($features)
49+
50+
// Can also use {@see Google\Cloud\Speech\V2\ExplicitDecodingConfig}
51+
// ->setExplicitDecodingConfig(new ExplicitDecodingConfig([...]);
52+
4853
->setAutoDecodingConfig(new AutoDetectDecodingConfig());
4954

5055
$request = (new RecognizeRequest())
@@ -53,15 +58,15 @@ function profanity_filter(string $projectId, string $location, string $recognize
5358
->setContent($content);
5459

5560
# Detects speech in the audio file
56-
$response = $client->recognize($request);
61+
$response = $speech->recognize($request);
5762

5863
# Print most likely transcription
5964
foreach ($response->getResults() as $result) {
6065
$transcript = $result->getAlternatives()[0]->getTranscript();
6166
printf('Transcript: %s' . PHP_EOL, $transcript);
6267
}
6368

64-
$client->close();
69+
$speech->close();
6570
}
6671
# [END speech_profanity_filter]
6772

speech/src/profanity_filter_gcs.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
function profanity_filter_gcs(string $projectId, string $location, string $recognizerId, string $uri)
3232
{
3333
// create the speech client
34-
$speech = new SpeechClient();
34+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
35+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
3536

3637
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
3738

@@ -40,9 +41,12 @@ function profanity_filter_gcs(string $projectId, string $location, string $recog
4041
'profanity_filter' => true
4142
]);
4243

43-
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
4444
$config = (new RecognitionConfig())
4545
->setFeatures($features)
46+
47+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
48+
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());
49+
4650
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
4751
'encoding' => AudioEncoding::LINEAR16,
4852
'sample_rate_hertz' => 16000,

speech/src/streaming_recognize.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
function streaming_recognize(string $projectId, string $location, string $recognizerId, string $audioFile)
4141
{
4242
// create the speech client
43-
$client = new SpeechClient();
43+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
44+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
4445

4546
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
4647

@@ -50,6 +51,7 @@ function streaming_recognize(string $projectId, string $location, string $recogn
5051
]);
5152
$streamingConfig = (new StreamingRecognitionConfig())
5253
->setConfig(new RecognitionConfig([
54+
// Can also use {@see Google\Cloud\Speech\V2\ExplicitDecodingConfig}
5355
'auto_decoding_config' => new AutoDetectDecodingConfig(),
5456
'features' => $features
5557
]));
@@ -58,7 +60,7 @@ function streaming_recognize(string $projectId, string $location, string $recogn
5860
->setStreamingConfig($streamingConfig);
5961

6062
// set the streaming request
61-
$stream = $client->streamingRecognize();
63+
$stream = $speech->streamingRecognize();
6264
$stream->write($streamingRequest);
6365

6466
// stream the audio file

speech/src/transcribe_async_gcs.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@
4242
*/
4343
function transcribe_async_gcs(string $projectId, string $location, string $recognizerId, string $uri)
4444
{
45-
$client = new SpeechClient();
45+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
46+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
4647
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
4748

48-
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
4949
$config = (new RecognitionConfig())
50+
// Can also use {@see Google\Cloud\Speech\V2\ExplicitDecodingConfig}
51+
// ->setExplicitDecodingConfig(new ExplicitDecodingConfig([...]);
52+
5053
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
5154
// change these variables if necessary
5255
'encoding' => AudioEncoding::LINEAR16,

speech/src/transcribe_async_words.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
*/
4747
function transcribe_async_words(string $projectId, string $location, string $recognizerId, string $uri)
4848
{
49-
$options = ['apiEndpoint' => $location == 'global' ? null : sprintf('%s-speech.googleapis.com', $location)];
50-
$speech = new SpeechClient($options);
49+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
50+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
5151
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
5252

5353
// When this is enabled, we send all the words from the beginning of the audio.
@@ -58,6 +58,13 @@ function transcribe_async_words(string $projectId, string $location, string $rec
5858
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
5959
$config = (new RecognitionConfig())
6060
->setFeatures($features)
61+
// When running outside the "global" location, you can set the model to "chirp_3" in
62+
// RecognitionConfig instead of on the recognizer.
63+
// ->setModel('chirp_3')
64+
65+
// Can also use {@see Google\Cloud\Speech\V2\ExplicitDecodingConfig}
66+
// ->setExplicitDecodingConfig(new ExplicitDecodingConfig([...]);
67+
6168
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
6269
// change these variables if necessary
6370
'encoding' => AudioEncoding::LINEAR16,

speech/src/transcribe_auto_punctuation.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
* @param string $projectId The Google Cloud project ID.
3535
* @param string $location The location of the recognizer.
3636
* @param string $recognizerId The ID of the recognizer to use.
37-
* @param string $audioFile path to an audio file.
37+
* @param string $audioFile path to an audio file (e.x. "test/data/audio32KHz.flac").
3838
*/
3939
function transcribe_auto_punctuation(string $projectId, string $location, string $recognizerId, string $audioFile)
4040
{
4141
// create the speech client
42-
$client = new SpeechClient();
42+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
43+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
4344

4445
// get contents of a file into a string
4546
$content = file_get_contents($audioFile);
@@ -53,6 +54,10 @@ function transcribe_auto_punctuation(string $projectId, string $location, string
5354

5455
$config = (new RecognitionConfig())
5556
->setFeatures($features)
57+
58+
// Can also use {@see Google\Cloud\Speech\V2\ExplicitDecodingConfig}
59+
// ->setExplicitDecodingConfig(new ExplicitDecodingConfig([...]);
60+
5661
->setAutoDecodingConfig(new AutoDetectDecodingConfig());
5762

5863
$request = (new RecognizeRequest())
@@ -61,7 +66,7 @@ function transcribe_auto_punctuation(string $projectId, string $location, string
6166
->setContent($content);
6267

6368
// make the API call
64-
$response = $client->recognize($request);
69+
$response = $speech->recognize($request);
6570
$results = $response->getResults();
6671

6772
// print results
@@ -74,7 +79,7 @@ function transcribe_auto_punctuation(string $projectId, string $location, string
7479
printf('Confidence: %s' . PHP_EOL, $confidence);
7580
}
7681

77-
$client->close();
82+
$speech->close();
7883
}
7984
# [END speech_transcribe_auto_punctuation]
8085

speech/src/transcribe_enhanced_model.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
function transcribe_enhanced_model(string $projectId, string $location, string $recognizerId, string $audioFile)
3939
{
4040
// create the speech client
41-
$client = new SpeechClient();
41+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
42+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
4243

4344
// get contents of a file into a string
4445
$content = file_get_contents($audioFile);
@@ -48,6 +49,10 @@ function transcribe_enhanced_model(string $projectId, string $location, string $
4849
// set config
4950
$config = (new RecognitionConfig())
5051
->setModel('telephony')
52+
53+
// Can also use {@see Google\Cloud\Speech\V2\ExplicitDecodingConfig}
54+
// ->setExplicitDecodingConfig(new ExplicitDecodingConfig([...]);
55+
5156
->setAutoDecodingConfig(new AutoDetectDecodingConfig());
5257

5358
$request = (new RecognizeRequest())
@@ -56,7 +61,7 @@ function transcribe_enhanced_model(string $projectId, string $location, string $
5661
->setContent($content);
5762

5863
// make the API call
59-
$response = $client->recognize($request);
64+
$response = $speech->recognize($request);
6065
$results = $response->getResults();
6166

6267
// print results
@@ -69,7 +74,7 @@ function transcribe_enhanced_model(string $projectId, string $location, string $
6974
printf('Confidence: %s' . PHP_EOL, $confidence);
7075
}
7176

72-
$client->close();
77+
$speech->close();
7378
}
7479
# [END speech_transcribe_enhanced_model]
7580

0 commit comments

Comments
 (0)