Skip to content

Commit 3a4e080

Browse files
committed
WIP
1 parent 10146de commit 3a4e080

17 files changed

+546
-418
lines changed

speech/quickstart.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
print_r($operation->getError());
6161
}
6262

63-
$recognitionConfig = (new RecognitionConfig())
63+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
64+
$config = (new RecognitionConfig())
6465
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
6566
'encoding' => AudioEncoding::LINEAR16,
6667
'sample_rate_hertz' => 16000,
@@ -70,7 +71,7 @@
7071
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
7172
$request = (new RecognizeRequest())
7273
->setRecognizer($recognizerName)
73-
->setConfig($recognitionConfig)
74+
->setConfig($config)
7475
->setUri($gcsURI);
7576

7677
# Detects speech in the audio file

speech/src/create_recognizer.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google LLC.
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+
namespace Google\Cloud\Samples\Speech;
19+
20+
use Google\Cloud\Speech\V2\Client\SpeechClient;
21+
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
22+
use Google\Cloud\Speech\V2\Recognizer;
23+
24+
/**
25+
* Create a new recognizer.
26+
*
27+
* @param string $projectId The Google Cloud project ID.
28+
* @param string $location The location of the recognizer.
29+
* @param string $recognizerId The ID of the recognizer to create.
30+
* @param string $model The recognizer model. Use "chirp_3" for diarization.
31+
*/
32+
function create_recognizer(
33+
string $projectId,
34+
string $location,
35+
string $recognizerId,
36+
string $model = "latest_short"
37+
): void {
38+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
39+
$speechClient = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
40+
41+
// Create a Recognizer
42+
$recognizer = new Recognizer([
43+
'language_codes' => ['en-US'],
44+
'model' => $model,
45+
]);
46+
47+
// Create the CreateRecognizerRequest
48+
$createRecognizerRequest = new CreateRecognizerRequest([
49+
'parent' => SpeechClient::locationName($projectId, $location),
50+
'recognizer_id' => $recognizerId,
51+
'recognizer' => $recognizer
52+
]);
53+
54+
// Call the createRecognizer method
55+
$operation = $speechClient->createRecognizer($createRecognizerRequest);
56+
57+
// Wait for the operation to complete
58+
$operation->pollUntilComplete();
59+
60+
if ($operation->operationSucceeded()) {
61+
$result = $operation->getResult();
62+
printf('Created Recognizer: %s' . PHP_EOL, $result->getName());
63+
} else {
64+
print_r($operation->getError());
65+
}
66+
67+
$speechClient->close();
68+
}
69+
70+
// The following 2 lines are only needed to run the samples
71+
require_once __DIR__ . '/../../testing/sample_helpers.php';
72+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/delete_recognizer.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google LLC.
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+
namespace Google\Cloud\Samples\Speech;
19+
20+
use Google\Cloud\Speech\V2\Client\SpeechClient;
21+
use Google\Cloud\Speech\V2\DeleteRecognizerRequest;
22+
23+
/**
24+
* Delete a recognizer.
25+
*
26+
* @param string $projectId The Google Cloud project ID.
27+
* @param string $location The location of the recognizer.
28+
* @param string $recognizerId The ID of the recognizer to delete.
29+
*/
30+
function delete_recognizer(string $projectId, string $location, string $recognizerId): void
31+
{
32+
$speechClient = new SpeechClient();
33+
34+
// Create the DeleteRecognizerRequest
35+
$deleteRecognizerRequest = new DeleteRecognizerRequest([
36+
'name' => SpeechClient::recognizerName($projectId, $location, $recognizerId)
37+
]);
38+
39+
// Call the deleteRecognizer method
40+
$operation = $speechClient->deleteRecognizer($deleteRecognizerRequest);
41+
42+
// Wait for the operation to complete
43+
$operation->pollUntilComplete();
44+
45+
if ($operation->operationSucceeded()) {
46+
printf('Deleted Recognizer: %s' . PHP_EOL, $deleteRecognizerRequest->getName());
47+
} else {
48+
print_r($operation->getError());
49+
}
50+
51+
$speechClient->close();
52+
}
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/multi_region_gcs.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2021 Google Inc.
3+
* Copyright 2023 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -18,37 +18,41 @@
1818
namespace Google\Cloud\Samples\Speech;
1919

2020
# [START speech_transcribe_with_multi_region_gcs]
21-
# Imports the Google Cloud client library
22-
use Google\Cloud\Speech\V1\RecognitionAudio;
23-
use Google\Cloud\Speech\V1\RecognitionConfig;
24-
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
25-
use Google\Cloud\Speech\V1\SpeechClient;
21+
use Google\Cloud\Speech\V2\Client\SpeechClient;
22+
use Google\Cloud\Speech\V2\RecognitionConfig;
23+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
24+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
25+
use Google\Cloud\Speech\V2\RecognizeRequest;
2626

2727
/**
28+
* @param string $projectId The Google Cloud project ID.
29+
* @param string $location The location of the recognizer.
30+
* @param string $recognizerId The ID of the recognizer to use (other than global).
2831
* @param string $uri The Cloud Storage object to transcribe
2932
* e.x. gs://cloud-samples-data/speech/brooklyn_bridge.raw
3033
*/
31-
function multi_region_gcs(string $uri)
34+
function multi_region_gcs(string $projectId, string $location, string $recognizerId, string $uri)
3235
{
33-
# set string as audio content
34-
$audio = (new RecognitionAudio())
35-
->setUri($uri);
36+
$options = ['apiEndpoint' => sprintf('%s-speech.googleapis.com', $location)];
37+
$speech = new SpeechClient($options);
3638

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-
]);
39+
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
4340

44-
# Specify a new endpoint.
45-
$options = ['apiEndpoint' => 'eu-speech.googleapis.com'];
41+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
42+
$config = (new RecognitionConfig())
43+
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
44+
'encoding' => AudioEncoding::LINEAR16,
45+
'sample_rate_hertz' => 16000,
46+
'audio_channel_count' => 1,
47+
]));
4648

47-
# Instantiates a client
48-
$client = new SpeechClient($options);
49+
$request = (new RecognizeRequest())
50+
->setRecognizer($recognizerName)
51+
->setConfig($config)
52+
->setUri($uri);
4953

5054
# Detects speech in the audio file
51-
$response = $client->recognize($config, $audio);
55+
$response = $speech->recognize($request);
5256

5357
# Print most likely transcription
5458
foreach ($response->getResults() as $result) {
@@ -57,8 +61,6 @@ function multi_region_gcs(string $uri)
5761
$transcript = $mostLikely->getTranscript();
5862
printf('Transcript: %s' . PHP_EOL, $transcript);
5963
}
60-
61-
$client->close();
6264
}
6365
# [END speech_transcribe_with_multi_region_gcs]
6466

speech/src/profanity_filter.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
# Copyright 2020 Google LLC
2+
# Copyright 2023 Google LLC
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
@@ -15,41 +15,45 @@
1515
namespace Google\Cloud\Samples\Speech;
1616

1717
# [START speech_profanity_filter]
18-
use Google\Cloud\Speech\V1\RecognitionAudio;
19-
use Google\Cloud\Speech\V1\RecognitionConfig;
20-
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
21-
use Google\Cloud\Speech\V1\SpeechClient;
18+
use Google\Cloud\Speech\V2\Client\SpeechClient;
19+
use Google\Cloud\Speech\V2\RecognizeRequest;
20+
use Google\Cloud\Speech\V2\RecognitionConfig;
21+
use Google\Cloud\Speech\V2\RecognitionFeatures;
22+
use Google\Cloud\Speech\V2\AutoDetectDecodingConfig;
2223

2324
/**
24-
* @param string $audioFile path to an audio file
25+
* @param string $projectId The Google Cloud project ID.
26+
* @param string $location The location of the recognizer.
27+
* @param string $recognizerId The ID of the recognizer to use.
28+
* @param string $audioFile path to an audio file.
29+
* ex "test/data/audio32KHz.flac"
2530
*/
26-
function profanity_filter(string $audioFile)
31+
function profanity_filter(string $projectId, string $location, string $recognizerId, string $audioFile)
2732
{
28-
// change these variables if necessary
29-
$encoding = AudioEncoding::LINEAR16;
30-
$sampleRateHertz = 32000;
31-
$languageCode = 'en-US';
32-
$profanityFilter = true;
33+
// create the speech client
34+
$client = new SpeechClient();
3335

3436
// get contents of a file into a string
3537
$content = file_get_contents($audioFile);
3638

37-
// set string as audio content
38-
$audio = (new RecognitionAudio())
39-
->setContent($content);
39+
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
40+
41+
// When true, the profanity filter will be enabled.
42+
$features = new RecognitionFeatures([
43+
'profanity_filter' => true
44+
]);
4045

41-
// set config
4246
$config = (new RecognitionConfig())
43-
->setEncoding($encoding)
44-
->setSampleRateHertz($sampleRateHertz)
45-
->setLanguageCode($languageCode)
46-
->setProfanityFilter($profanityFilter);
47+
->setFeatures($features)
48+
->setAutoDecodingConfig(new AutoDetectDecodingConfig());
4749

48-
// create the speech client
49-
$client = new SpeechClient();
50+
$request = (new RecognizeRequest())
51+
->setRecognizer($recognizerName)
52+
->setConfig($config)
53+
->setContent($content);
5054

5155
# Detects speech in the audio file
52-
$response = $client->recognize($config, $audio);
56+
$response = $client->recognize($request);
5357

5458
# Print most likely transcription
5559
foreach ($response->getResults() as $result) {

speech/src/profanity_filter_gcs.php

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
# Copyright 2020 Google LLC
2+
# Copyright 2023 Google LLC
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
@@ -15,46 +15,57 @@
1515
namespace Google\Cloud\Samples\Speech;
1616

1717
# [START speech_profanity_filter_gcs]
18-
use Google\Cloud\Speech\V1\RecognitionAudio;
19-
use Google\Cloud\Speech\V1\RecognitionConfig;
20-
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
21-
use Google\Cloud\Speech\V1\SpeechClient;
18+
use Google\Cloud\Speech\V2\Client\SpeechClient;
19+
use Google\Cloud\Speech\V2\RecognitionConfig;
20+
use Google\Cloud\Speech\V2\RecognitionFeatures;
21+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
22+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
23+
use Google\Cloud\Speech\V2\RecognizeRequest;
2224

2325
/**
26+
* @param string $projectId The Google Cloud project ID.
27+
* @param string $location The location of the recognizer.
28+
* @param string $recognizerId The ID of the recognizer to use.
2429
* @param string $uri The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name)
2530
*/
26-
function profanity_filter_gcs(string $uri)
31+
function profanity_filter_gcs(string $projectId, string $location, string $recognizerId, string $uri)
2732
{
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);
33+
// create the speech client
34+
$speech = new SpeechClient();
35+
36+
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
3737

38-
// set config
38+
// When true, the profanity filter will be enabled.
39+
$features = new RecognitionFeatures([
40+
'profanity_filter' => true
41+
]);
42+
43+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
3944
$config = (new RecognitionConfig())
40-
->setEncoding($encoding)
41-
->setSampleRateHertz($sampleRateHertz)
42-
->setLanguageCode($languageCode)
43-
->setProfanityFilter($profanityFilter);
45+
->setFeatures($features)
46+
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
47+
'encoding' => AudioEncoding::LINEAR16,
48+
'sample_rate_hertz' => 16000,
49+
'audio_channel_count' => 1,
50+
]));
4451

45-
// create the speech client
46-
$client = new SpeechClient();
52+
$request = (new RecognizeRequest())
53+
->setRecognizer($recognizerName)
54+
->setConfig($config)
55+
->setUri($uri);
4756

4857
# Detects speech in the audio file
49-
$response = $client->recognize($config, $audio);
58+
$response = $speech->recognize($request);
5059

5160
# Print most likely transcription
5261
foreach ($response->getResults() as $result) {
53-
$transcript = $result->getAlternatives()[0]->getTranscript();
62+
$alternatives = $result->getAlternatives();
63+
$mostLikely = $alternatives[0];
64+
$transcript = $mostLikely->getTranscript();
5465
printf('Transcript: %s' . PHP_EOL, $transcript);
5566
}
5667

57-
$client->close();
68+
$speech->close();
5869
}
5970
# [END speech_profanity_filter_gcs]
6071

0 commit comments

Comments
 (0)