Skip to content

Commit 10146de

Browse files
committed
upgrade quickstart.php
1 parent 1a635c2 commit 10146de

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

speech/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-speech": "^1.16",
3+
"google/cloud-speech": "^2.2",
44
"google/cloud-storage": "^1.36"
55
}
66
}

speech/quickstart.php

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016 Google Inc.
3+
* Copyright 2023 Google LLC.
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.
@@ -20,30 +20,61 @@
2020
require __DIR__ . '/vendor/autoload.php';
2121

2222
# Imports the Google Cloud client library
23-
use Google\Cloud\Speech\V1\RecognitionAudio;
24-
use Google\Cloud\Speech\V1\RecognitionConfig;
25-
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
26-
use Google\Cloud\Speech\V1\SpeechClient;
23+
24+
use Google\Cloud\Speech\V2\Client\SpeechClient;
25+
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
26+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
27+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
28+
use Google\Cloud\Speech\V2\RecognitionConfig;
29+
use Google\Cloud\Speech\V2\Recognizer;
30+
use Google\Cloud\Speech\V2\RecognizeRequest;
2731

2832
# The name of the audio file to transcribe
2933
$gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';
3034

31-
# set string as audio content
32-
$audio = (new RecognitionAudio())
33-
->setUri($gcsURI);
35+
# Your Google Cloud Project ID and location
36+
$projectId = 'YOUR_PROJECT_ID';
37+
$location = 'global';
38+
39+
# Instantiates a client
40+
$speech = new SpeechClient();
3441

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'
42+
// Create a Recognizer
43+
$createRecognizerRequest = new CreateRecognizerRequest([
44+
'parent' => SpeechClient::locationName($projectId, $location),
45+
'recognizer_id' => $recognizerId = 'quickstart-recognizer-' . uniqid(),
46+
'recognizer' => new Recognizer([
47+
'language_codes' => ['en-US'],
48+
'model' => 'latest_short'
49+
])
4050
]);
4151

42-
# Instantiates a client
43-
$client = new SpeechClient();
52+
$operation = $speech->createRecognizer($createRecognizerRequest);
53+
54+
// Wait for the operation to complete
55+
$operation->pollUntilComplete();
56+
if ($operation->operationSucceeded()) {
57+
$result = $operation->getResult();
58+
printf('Created Recognizer: %s' . PHP_EOL, $result->getName());
59+
} else {
60+
print_r($operation->getError());
61+
}
62+
63+
$recognitionConfig = (new RecognitionConfig())
64+
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
65+
'encoding' => AudioEncoding::LINEAR16,
66+
'sample_rate_hertz' => 16000,
67+
'audio_channel_count' => 1,
68+
]));
69+
70+
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
71+
$request = (new RecognizeRequest())
72+
->setRecognizer($recognizerName)
73+
->setConfig($recognitionConfig)
74+
->setUri($gcsURI);
4475

4576
# Detects speech in the audio file
46-
$response = $client->recognize($config, $audio);
77+
$response = $speech->recognize($request);
4778

4879
# Print most likely transcription
4980
foreach ($response->getResults() as $result) {
@@ -53,6 +84,4 @@
5384
printf('Transcript: %s' . PHP_EOL, $transcript);
5485
}
5586

56-
$client->close();
57-
58-
# [END speech_quickstart]
87+
$speech->close();

0 commit comments

Comments
 (0)