|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2021 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 | +# [START speech_transcribe_with_multi_region_gcs] |
| 19 | +# Includes the autoloader for libraries installed with composer |
| 20 | +require __DIR__ . '/../vendor/autoload.php'; |
| 21 | + |
| 22 | +# Imports the Google Cloud client library |
| 23 | +use Google\Cloud\Speech\V1\SpeechClient; |
| 24 | +use Google\Cloud\Speech\V1\RecognitionAudio; |
| 25 | +use Google\Cloud\Speech\V1\RecognitionConfig; |
| 26 | +use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; |
| 27 | + |
| 28 | +# The name of the audio file to transcribe |
| 29 | +$gcsURI = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"; |
| 30 | + |
| 31 | +# set string as audio content |
| 32 | +$audio = (new RecognitionAudio()) |
| 33 | + ->setUri($gcsURI); |
| 34 | + |
| 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 | +]); |
| 41 | + |
| 42 | +# Specify a new endpoint. |
| 43 | +$options = ['apiEndpoint' => 'eu-speech.googleapis.com']; |
| 44 | + |
| 45 | +# Instantiates a client |
| 46 | +$client = new SpeechClient($options); |
| 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 | + $alternatives = $result->getAlternatives(); |
| 54 | + $mostLikely = $alternatives[0]; |
| 55 | + $transcript = $mostLikely->getTranscript(); |
| 56 | + printf('Transcript: %s' . PHP_EOL, $transcript); |
| 57 | +} |
| 58 | + |
| 59 | +$client->close(); |
| 60 | +# [END speech_transcribe_with_multi_region_gcs] |
0 commit comments