Speech/src/SpeechClient.php
public function recognize($audio, array $options = [])
    {
        $results = [];
        $response = $this->connection->recognize(
            $this->formatRequest($audio, $options)
        );

        if (!isset($response['results'])) {
            return $results;
        }

        foreach ($response['results'] as $result) {
            $results[] = new Result($result);
        }

        return $results;
    }
public function recognize($audio, array $options = [])
    {
        $results = [];
        $response = $this->connection->recognize(
            $this->formatRequest($audio, $options)
        );

        if (!isset($response['results'])) {
            return $results;
        }

        foreach ($response['results'] as $result) {
            $results[] = new Result($result);
        }

        return $results;
    }
public function recognize ( $ audio , array $ options = [ ] ) { $ results = [ ] ; $ response = $ this -> connection -> recognize ( $ this -> formatRequest ( $ audio , $ options ) ) ; if ( ! isset ( $ response [ 'results' ] ) ) { return $ results ; } foreach ( $ response [ 'results' ] as $ result ) { $ results [ ] = new Result ( $ result ) ; } return $ results ; }
Runs a recognize request and returns the results immediately. Ideal when
working with audio up to approximately one minute in length.

Example:
```
$results = $speech->recognize(
fopen(__DIR__  . '/audio.flac', 'r')
);

foreach ($results as $result) {
echo $result->topAlternative()['transcript'] . PHP_EOL;
}
```

```
// Run with speech contexts, sample rate, and encoding provided
$results = $speech->recognize(
fopen(__DIR__  . '/audio.flac', 'r'), [
'encoding' => 'FLAC',
'sampleRateHertz' => 16000,
'speechContexts' => [
[
'phrases' => [
'The Google Cloud Platform',
'Speech API'
]
]
]
]);

foreach ($results as $result) {
echo $result->topAlternative()['transcript'] . PHP_EOL;
}
```

@codingStandardsIgnoreStart
@see https://cloud.google.com/speech/reference/rest/v1/speech/recognize#SpeechRecognitionResult SpeechRecognitionResult
@see https://cloud.google.com/speech/reference/rest/v1/speech/recognize Recognize API documentation
@see https://cloud.google.com/speech/reference/rest/v1/RecognitionConfig#AudioEncoding AudioEncoding types
@see https://cloud.google.com/speech/docs/best-practices Speech API best practices
@codingStandardsIgnoreEnd

@param resource|string|StorageObject $audio The audio to recognize. May
be a resource, string of bytes, a URI pointing to a
Google Cloud Storage object in the format of
`gs://{bucket-name}/{object-name}` or a
{@see Google\Cloud\Storage\StorageObject}.
@param array $options [optional] {
Configuration options.

@type bool $detectGcsUri When providing $audio as a string, this flag
determines whether or not to attempt to detect if the string
represents a Google Cloud Storage URI in the format of
`gs://{bucket-name}/{object-name}`. **Defaults to** `true`.
@type string $languageCode The language of the content. BCP-47
(e.g., `"en-US"`, `"es-ES"`) language codes are accepted. See
[Language Support](https://cloud.google.com/speech/docs/languages)
for a list of the currently supported language codes. This
value will be required if a default has not been set on the
client. **Defaults to** the value set on the client.
@type int $sampleRateHertz Sample rate in Hertz of the provided
audio. Valid values are: 8000-48000. 16000 is optimal. For best
results, set the sampling rate of the audio source to 16000 Hz.
If that's not possible, use the native sample rate of the audio
source (instead of re-sampling). For .flac and .wav files the
Speech API will make a best effort to read the sample rate from
the file's headers.
@type string $encoding Encoding of the provided audio. May be one of
`"LINEAR16"`, `"FLAC"`, `"MULAW"`, `"AMR"`, `"AMR_WB"`. For
.flac and .wav files the Speech API will make a best effort to
determine the encoding type from the file's headers.
@type int $maxAlternatives Maximum number of alternatives to be
returned. Valid values are 1-30. **Defaults to** `1`.
@type bool $profanityFilter If set to `true`, the server will attempt
to filter out profanities, replacing all but the initial
character in each filtered word with asterisks, e.g. \"f***\".
**Defaults to** `false`.
@type array $speechContexts A list of arrays where each element must
contain a key `phrases`. Each key `phrases` should contain an
array of strings which provide "hints" to the speech recognizer
to favor specific words and phrases in the results. Please see
[SpeechContext](https://cloud.google.com/speech/reference/rest/v1/RecognitionConfig#SpeechContext)
for more information.
@type bool $enableWordTimeOffsets If set to `true`, a list of
`wordTimeOffsets` are returned with the top alternative. If
`false` or omitted, no `wordTimeOffsets` are returned.
**Defaults to** `false`.
}
@return array Result[]
@throws \InvalidArgumentException
Runs a recognize request and returns the results immediately . Ideal when working with audio up to approximately one minute in length .