我在Google Cloud Client库php中使用SpeechRecoginition,流式传输吗?

发布于 2025-01-22 09:37:43 字数 3119 浏览 2 评论 0原文

我正在为Google Cloud Client库PHP使用SpeechRecoginition。

如有示例,请参阅此文档。

我们已经测试并确认了本地文件的正常操作。

但是我想通过实时发送语音文件而不是存储的文件来进行语音认可。

我想使用Mediarecorder JS将数据传递给PHP,分析来自PHP的声音,然后将其传递给客户端。

这是 document 我引用了关于Mediarecord JS。

[client.js]

var mediaRecorder;

navigator.mediaDevices.getUserMedia(constraints).then(stream=>{
        var options = {
            type: 'video',
            mimeType: 'video/webm'
        };
          
            try{
                mediaRecorder.stop()
            }catch(e){}

            mediaRecorder=null;
            let order = 0;
            mediaRecorder = new MediaRecorder(stream,options);
            mediaRecorder.ondataavailable =function(e) {

                if(e.data&&e.data.size>0) {
                    recordedChunks.push(event.data);
                    var reader = new FileReader();
                    reader.readAsArrayBuffer(e.data); 
                    reader.onloadend = async function(event) {
                        let arrayBuffer = reader.result;   
                        let uint8View = new Uint8Array(arrayBuffer);
                        let response = await fetch('server.php', {
                            method: 'POST',                 
                            body: JSON.stringify({
                                chunk: uint8View,
                                order: order
                            })                  
                        });
                        order += 1;
                    }
                }
            }
            mediaRecorder.start(250);
    })

[server.php]

<?php
require($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php');

use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;

$speechClient = new Google\Cloud\Speech\V1\SpeechClient();

try {
    $recognitionConfig = new RecognitionConfig();
    $recognitionConfig->setEncoding(AudioEncoding::FLAC);
    $recognitionConfig->setSampleRateHertz(44100);
    $recognitionConfig->setLanguageCode('ko-KR');
    $config = new StreamingRecognitionConfig();
    $config->setConfig($recognitionConfig);

    //$audioResource = fopen('localfile path', 'r'); //original
    
    //receive DATA
    $chunk = $_POST['chunk'];
    $binarydata = pack("C*", ...$chunk);
    $audioResource = $binarydata;

    $responses = $speechClient->recognizeAudioStream($config, $audioResource);

    foreach ($responses as $element) {
        //recognition result send client
    }
}  catch(Exception $e) {
    var_dump($e);
}

我想从[client.js]接收视频数据,并在[server.php]中实时分析音频。

在中国订单而不是本地文件分析中创建的数组类型数据需要SecedRecogniton。

请指教。

I am using SpeechRecoginition for google cloud client library PHP.

For examples, refer to this document.

We have tested and confirmed the normal operation with local files.

But I want to SpeechRecognition by sending a voice file in real time, not a stored file.

I want to pass data to PHP using MediaRecorder js, analyze the voice coming from PHP, and deliver it back to the client.

This is the document I referenced regarding MediaRecord js.

[client.js]

var mediaRecorder;

navigator.mediaDevices.getUserMedia(constraints).then(stream=>{
        var options = {
            type: 'video',
            mimeType: 'video/webm'
        };
          
            try{
                mediaRecorder.stop()
            }catch(e){}

            mediaRecorder=null;
            let order = 0;
            mediaRecorder = new MediaRecorder(stream,options);
            mediaRecorder.ondataavailable =function(e) {

                if(e.data&&e.data.size>0) {
                    recordedChunks.push(event.data);
                    var reader = new FileReader();
                    reader.readAsArrayBuffer(e.data); 
                    reader.onloadend = async function(event) {
                        let arrayBuffer = reader.result;   
                        let uint8View = new Uint8Array(arrayBuffer);
                        let response = await fetch('server.php', {
                            method: 'POST',                 
                            body: JSON.stringify({
                                chunk: uint8View,
                                order: order
                            })                  
                        });
                        order += 1;
                    }
                }
            }
            mediaRecorder.start(250);
    })

[server.php]

<?php
require($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php');

use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;

$speechClient = new Google\Cloud\Speech\V1\SpeechClient();

try {
    $recognitionConfig = new RecognitionConfig();
    $recognitionConfig->setEncoding(AudioEncoding::FLAC);
    $recognitionConfig->setSampleRateHertz(44100);
    $recognitionConfig->setLanguageCode('ko-KR');
    $config = new StreamingRecognitionConfig();
    $config->setConfig($recognitionConfig);

    //$audioResource = fopen('localfile path', 'r'); //original
    
    //receive DATA
    $chunk = $_POST['chunk'];
    $binarydata = pack("C*", ...$chunk);
    $audioResource = $binarydata;

    $responses = $speechClient->recognizeAudioStream($config, $audioResource);

    foreach ($responses as $element) {
        //recognition result send client
    }
}  catch(Exception $e) {
    var_dump($e);
}

I want to receive video data from [client.js] and analyze the audio in real time in [server.php].

SpeechRecogniton is needed for arrayBuffer type data created in mediaRecorder rather than local file analysis.

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文