i'm trying to implement google speech to text for live audio streams. I have been able to implement for location audio file correctly. However, live audio content is not successful.
I am using V1 of the speech-to-text api. I have gRPC installed already.
This is my PHP script
require 'vendor/autoload.php';
$conn= new mysqli('localhost',"", "", "");
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionAudio;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognizeRequest;
use Google\Cloud\Speech\V1\StreamingRecognizeResponse;
use Google\Cloud\Speech\V1\StreamingRecognitionResult;
use Google\Cloud\Speech\V1\SpeechContext;
use Google\Cloud\Speech\V1\SpeakerDiarizationConfig;
use Google\Cloud\Speech\V1\WordInfo;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/xxx-455121-be3eb805f1d7.json');
$ffmpegCommand = "ffmpeg -re -i -ac 1 -ar 16000 -f s16le pipe:1";
$handle = popen($ffmpegCommand, "r");
try {
$client = new SpeechClient(['transport' => 'grpc', 'credentials' => json_decode(file_get_contents(getenv('GOOGLE_APPLICATION_CREDENTIALS')), true)]);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
$encoding = AudioEncoding::MP3;
try {
$config = (new RecognitionConfig())
->setEncoding($encoding)
->setSampleRateHertz(24000)
->setLanguageCode('en-US')
->setEnableAutomaticPunctuation(true);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
try {
$streamConfig = new StreamingRecognitionConfig([
'config' => $config,
'interim_results' => true,
]);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
try {
$stream = $client->streamingRecognize();
$stream->write(new StreamingRecognizeRequest([
'streaming_config' => $streamConfig
]));
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
exit;
}
while (!feof($handle)) {
usleep(5000);
$chunk = fread($handle, 4096);
printf('chunk: ' . $chunk);
if ($chunk !== false) {
try {
$stream->write(new StreamingRecognizeRequest([
'audio_content' => $chunk
]));
} catch (Exception $e) {
printf('Errorc: ' . $e->getMessage());
}
}
}
print_r($stream->responses());
foreach ($stream->responses() as $response) {
mysqli_query($conn, "INSERT INTO transcriptions (transcript) VALUES ('loop1')");
foreach ($response->getResults() as $result) {
mysqli_query($conn, "INSERT INTO transcriptions (transcript) VALUES ('loop2')");
foreach ($result->getAlternatives() as $alternative) {
$trans = $alternative->getTranscript();
mysqli_query($conn, "INSERT INTO transcriptions (transcript) VALUES ('$trans')");
printf('Transcript: %s' . PHP_EOL, $alternative->getTranscript());
printf('loop');
}
}
}
pclose($handle);
$stream->close();
$client->close();
?>`
The script breaks at $chunk = fread($handle, 4096)
This is the error message
Please assist