谷歌语音API - PHP不返回任何东西 [英] Google speech API - php does not return anything

查看:145
本文介绍了谷歌语音API - PHP不返回任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码灵感来自这个用于语音到文本的全双工Google语音API的PHP版本: http://mikepultz.com/2013/07/google-speech-api-full-duplex-php-version/



我有几个flac文件可以工作,并按照Mike的文章所述给出阵列输出。但对于少数flac文件,它只是不会返回任何内容作为输出。例如: http://gavyadhar.com/video/upload/Pantry_Survey.flac ,没有输出返回。



但是相同的代码适用于这个flac文件: http://gavyadhar.com/video/upload/pantry_snack_video.flac 并返回数组输出,如下所示:



- --------回应1:你的脸是一个储藏室和一个主要的堆栈我通常是韦斯利路rasa multigrain饼干,我喜欢他们,因为他们对你来说是非常健康的,[...]
...等5个回应。

任何人都可以告诉我,为什么有些flac文件不起作用?或者有什么方法可以捕获由google API创建的异常并显示它们以排除故障?



谢谢。在这里添加两个php文件的代码:

传递API密钥和FLAC文件的PHP文件及其采样率。 <?phpinclude'GoogleSpeechToText.php'; ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); //你的API密钥在这里。$ apiKey ='XXXXXXXXXXXXXXXXXXXXXXXXXXXX'; //我的API服务器键$ speech = new GoogleSpeechToText($ apiKey);#var_dump($ speech); $ file = realpath('upload / Pantry_Survey.flac'); // file.echo的完整路径< BR>< BR> file:,$ file; #print_r(< br> ----------------- - );#print_r($ file1);#print_r(< br> -------------------); $ bitRate = 44100; ($ file,$ bitRate,'en-US'); print_r($ result); echo< BR> - -----------; $ responses = $ result [0] ['alternative']; $ i = 0; foreach($ response as $ response){$ i = $ i + 1; #echo $ response ['transcript']; echo< br>< br> --------- response $ i:,$ response ['transcript'];
$ b

lang =jsdata-hide =false> GoogleSpeechToText.php< class =snippet-code>

            p>您提供的音频文件使用2个频道。由于语音目前仅支持单声道音频,因此您需要将其转换为单声道。 
所有编码仅支持1声道(单声道)音频。
Google speech API的音频编码细节



在将音频文件转换为1个频道后,我能够成功获得提供的音频文件的响应。


My code is inspired by this php version of full duplex google speech API for speech-to-text : http://mikepultz.com/2013/07/google-speech-api-full-duplex-php-version/

I have few flac files that do work and give the array output as explained on Mike's post. But for few flac files it just doesn't return anything as output. For example : http://gavyadhar.com/video/upload/Pantry_Survey.flac, no output is returned.

But the same code works for this flac file: http://gavyadhar.com/video/upload/pantry_snack_video.flac and returns the array output as following:

---------response 1 : your face a pantry and one of the main stacks I usually Wesley Rd rasa multigrain crackers and I like them because they're pretty healthy for you and [...] ...and so on 5 responses.

Can anyone tell me, why is it that some flac files don't work? Or is there any way to catch the exceptions which are created by google API and display them to troubleshoot?

Thanks. Attaching my code here of two php files:

PHP file which passes the API key and the FLAC file with its sample rate. 

<?php
include 'GoogleSpeechToText.php';


ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1); 		

// Your API Key goes here.
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // my API server key
$speech = new GoogleSpeechToText($apiKey);

#var_dump($speech);

$file = realpath('upload/Pantry_Survey.flac'); // Full path to the file.

echo "<BR><BR>file : ", $file ;

#print_r("<br>-------------------");
#print_r($file1);
#print_r("<br>-------------------");

$bitRate = 44100; // The bit rate of the file.
$result = $speech->process($file, $bitRate, 'en-US');

print_r($result);

echo "<BR><BR>-------------";

$responses = $result[0]['alternative'];

$i = 0;
foreach ($responses as $response) {
	$i = $i + 1;
	#echo $response['transcript'];
	echo "<br><br> ---------response $i : ", $response['transcript'];
	
}

?>

GoogleSpeechToText.php

<?php
/**
 * Convert FLAC files to Text using the Google Speech API
 *
 * Credit due to Mike (mike@mikepultz.com) for his first version of this.
 *
 * @version 0.1
 * @author Roger Thomas
 * @see
 *
 */
class GoogleSpeechToText
{
    /**
     * URL of the Speech API
     * @var string
     */
    const SPEECH_BASE_URL = 'https://www.google.com/speech-api/full-duplex/v1/';

	
	
    /**
     * A 'unique' string to use for the requests
     *
     * @var string
     */
    private $requestPair;

    /**
     * The Google Auth API Key
     *
     * @var string
     */
    private $apiKey;

    /**
     * CURL Upload Handle
     *
     * @var resource
     */
    private $uploadHandle;

    /**
     * CURL Download Handle
     *
     * @var resource
     */
    private $downloadHandle;

    /**
     * Construct giving the Google Auth API Key.
     *
     * @param string $apiKey
     * @throws Exception
     */
    public function __construct($apiKey)
    {
        if (empty($apiKey)) {
            throw new Exception('$apiKey should not be empty.');
        }
        $this->apiKey = $apiKey;
        $this->requestPair = $this->getPair();
        $this->setupCurl();
    }

    /**
     * Setup CURL requests, both up and down.
     */
    private function setupCurl()
    {
        $this->uploadHandle = curl_init();
        $this->downloadHandle = curl_init();
        
		curl_setopt($this->downloadHandle, CURLOPT_URL, self::SPEECH_BASE_URL . 'down?pair=' . $this->requestPair );
		
		#echo "<br>downloadHandle :: ", self::SPEECH_BASE_URL . 'down?pair=' . $this->requestPair;

        curl_setopt($this->downloadHandle, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($this->uploadHandle,CURLOPT_RETURNTRANSFER,true);

        curl_setopt($this->uploadHandle,CURLOPT_POST,true);
		
		//----added by asmi shah - 7 jan 2015
		curl_setopt($this->uploadHandle, CURLOPT_MAX_SEND_SPEED_LARGE, 30000);
		curl_setopt($this->uploadHandle, CURLOPT_LOW_SPEED_TIME, 9999);
		curl_setopt($this->downloadHandle, CURLOPT_LOW_SPEED_TIME, 9999);
		//----

    }

    /**
     * Generate a Pair for the request. This identifies the requests later.
     *
     * @return string
     */
    private function getPair()
    {
        $c = '0123456789';
        $s = '';
        for ($i=0; $i<16; $i++) {
            $s .= $c[rand(0, strlen($c) - 1)];
        }
		echo "pair : ",$s;
        return $s;
		
    }

    /**
     * Make the request, returning either an array, or boolean false on
     * failure.
     *
     * @param string $file the file name to process
     * @param integer $rate the bitrate of the flac content (example: 44100)
     * @param string $language the ISO language code
     *      (en-US has been confirmed as working)
     * @throws Exception
     * @return array|boolean false for failure.
     */
    public function process($file, $rate, $language = 'en-US')
    {
        if (!$file || !file_exists($file) || !is_readable($file)) {
            throw new Exception(
                '$file must be specified and be a valid location.'
            );
        }
		else { echo "<br>file exists"; }

        $data = file_get_contents($file);
		
		#var_dump($rate);
        if (!$data) {
            throw new Exception('Unable to read ' . $file);
        }
		else { echo "<br>file is readable"; }
		
		
		$upload_data = array(
					"Content_Type"  =>  "audio/x-flac; rate=". $rate,
					"Content"       =>  $data,
		);
		

        if (empty($rate) || !is_integer($rate)) {
            throw new Exception('$rate must be specified and be an integer');
        }
		else { echo "<br>sample rate is received :" . $rate ; }

        curl_setopt($this->uploadHandle,CURLOPT_URL,self::SPEECH_BASE_URL . 'up?lang=' .$language . '&lm=dictation&timeout=20&client=chromium&pair=' .$this->requestPair . '&key=' . $this->apiKey);
        
		curl_setopt($this->uploadHandle,CURLOPT_HTTPHEADER,array('Transfer-Encoding: chunked','Content-Type: audio/x-flac; rate=' . $rate));
        
		curl_setopt($this->uploadHandle,CURLOPT_POSTFIELDS,$upload_data);
		
		#echo "<br><br>------ data : ", $data;
		#echo "<br><br>------";
		#echo "<BR><BR> URL made up : ", self::SPEECH_BASE_URL . 'up?lang=' .$language . '&lm=dictation&client=chromium&pair=' .$this->requestPair . '&key=' . $this->apiKey;
		

        $curlMulti = curl_multi_init();

        curl_multi_add_handle($curlMulti, $this->downloadHandle);
        curl_multi_add_handle($curlMulti, $this->uploadHandle);
		
        $active = null;
        do {
            curl_multi_exec($curlMulti, $active);
        } while ($active > 0);

        $res = curl_multi_getcontent($this->downloadHandle);

		#var_dump($this->downloadHandle);
		#echo "resource type ::".get_resource_type($this->downloadHandle);
        
		$output = array();
        $results = explode("\n", $res);
		
		
		#var_dump($results);
		#$i = 0;
        foreach ($results as $result) {
			#$i = $i + 1;
			#echo "<BR><BR><BR>--------------- string ||$i|| : ", $result;
			
            $object = json_decode($result, true);
            if (
                (isset($object['result']) == true) &&
                (count($object['result']) > 0)
            ) {
                foreach ($object['result'] as $obj) {
                    $output[] = $obj;
                }
            }
        }

        curl_multi_remove_handle($curlMulti, $this->downloadHandle);
        curl_multi_remove_handle($curlMulti, $this->uploadHandle);
        curl_multi_close($curlMulti);

        if (empty($output)) {
			echo "<BR><br>output is empty<BR>";
            return false;
        }
		echo "<BR><BR>";
        return  $output;
    }

    /**
     * Close any outstanding connections in the destruct
     */
    public function __destruct()
    {
        curl_close($this->uploadHandle);
        curl_close($this->downloadHandle);
    }
}

?>

解决方案

The audio file you provided uses 2 channels. As speech currently only supports single channel audio, you will need to convert it down to a single channel. All encodings support only 1 channel (mono) audio. Audio encoding detail of Google speech API

After converting the audio file down to 1 channel I was able to successfully get a response with the provided audio file.

这篇关于谷歌语音API - PHP不返回任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆