private function formatRequest($audio, array $options)
{
$analyzedFileInfo = null;
$fileFormat = null;
$recognizeOptions = ['encoding', 'sampleRate', 'languageCode', 'maxAlternatives', 'profanityFilter', 'speechContext'];
if ($audio instanceof StorageObject) {
$objIdentity = $audio->identity();
$options['audio']['uri'] = 'gs://' . $objIdentity['bucket'] . '/' . $objIdentity['object'];
$fileFormat = pathinfo($options['audio']['uri'], PATHINFO_EXTENSION);
} elseif (is_resource($audio)) {
$options['audio']['content'] = base64_encode(stream_get_contents($audio));
$fileFormat = pathinfo(stream_get_meta_data($audio)['uri'], PATHINFO_EXTENSION);
} else {
$options['audio']['content'] = base64_encode($audio);
}
if (isset($options['encoding'])) {
$options['encoding'] = strtoupper($options['encoding']);
} else {
$analyzedFileInfo = $this->analyzeAudio($audio);
$options['encoding'] = isset($analyzedFileInfo['fileformat']) ? $this->determineEncoding($analyzedFileInfo['fileformat']) : $this->determineEncoding($fileFormat);
}
if (isset($options['sampleRate'])) {
$options['sampleRate'] = (int) $options['sampleRate'];
} else {
if (!$analyzedFileInfo) {
$analyzedFileInfo = $this->analyzeAudio($audio);
}
$options['sampleRate'] = isset($analyzedFileInfo['audio']['sample_rate']) ? $analyzedFileInfo['audio']['sample_rate'] : $this->determineSampleRate($options['encoding']);
}
if (!$options['encoding']) {
throw new \InvalidArgumentException('Unable to determine encoding. Please provide the value manually.');
}
if (!$options['sampleRate']) {
throw new \InvalidArgumentException('Unable to determine sample rate. Please provide the value manually.');
}
foreach ($options as $option => $value) {
if (in_array($option, $recognizeOptions)) {
$options['config'][$option] = $value;
unset($options[$option]);
}
}
return $options;
}