예제 #1
0
 public function testRequest()
 {
     require __DIR__ . '/cfgs/speech_config.php';
     if (isset($proxyHost) && isset($proxyPort)) {
         RestfulEnvironment::setProxy($proxyHost, $proxyPort);
     }
     if (isset($allowAllCerts)) {
         RestfulEnvironment::setAcceptAllCerts($allowAllCerts);
     }
     $osrvc = new OAuthTokenService($FQDN, $api_key, $secret_key);
     $token = $osrvc->getToken('Speech,TTS,STTC');
     $srvc = new SpeechService($FQDN, $token);
     $fname = __DIR__ . '/files/BostonCeltics.wav';
     $response = $srvc->speechToText($fname, 'Generic');
     $this->assertTrue($response != null);
     $response = $srvc->textToSpeech('text/plain', 'testing ok');
     $this->assertTrue($response != null);
     $response = $srvc->speechToTextCustom('GenericHints', $fname);
     $this->assertTrue($response != null);
 }
 private function _handleSpeechToText()
 {
     if (!isset($_REQUEST['SpeechToText'])) {
         return;
     }
     try {
         $context = $_REQUEST['SpeechContext'];
         $_SESSION['SpeechContext'] = $context;
         $filename = $_REQUEST['audio_file'];
         $_SESSION['audio_file'] = $filename;
         $nameParam = $_REQUEST['nameParam'];
         $_SESSION['nameParam'] = $nameParam;
         $flocation = $this->_audioFolder . '/' . $filename;
         $path = __DIR__ . '/../../template/';
         $xGrammar = $path . 'x-grammar.txt';
         $xDictionary = $path . 'x-dictionary.txt';
         $srvc = new SpeechService($this->apiFQDN, $this->getFileToken());
         $response = $srvc->speechToTextCustom($context, $flocation, $xGrammar, $xDictionary, $this->_xArgs);
         $this->results[SpeechController::RESULT_STT] = $response;
     } catch (Exception $e) {
         $this->errors[SpeechController::ERROR_STT] = $e->getMessage();
     }
 }
 /**
  * Sends audio file to API and a text representation is sent back. Supports custom dictionary and/or grammar file.
  *
  * @method speechToTextCustom
  *
  * @param {string} file Name and path of local audio file to send to codekit
  * @param {string} context Speech context for translation. Please see SpeechToText API documentation for parameter values
  * @param {string} grammar_file Name and path of the grammar file.
  * @param {string} dictionary_file Name and path of the dictionary file.
  * @param {string} xargs X-Arg objects. Please see SpeechToText API documentation for information about this parameter
  * @param {string} language ISO language code - default 'en-US'
  *
  * @return {Response} Returns Response object.
  * @throws ServiceException if API request was not successful.
  *
  */
 public function speechToTextCustom($file, $context, $grammar_file, $dictionary_file, $xargs, $language)
 {
     $filecontents = $this->getFile($file);
     // throws Exception
     // Get OAuth token
     $token = $this->getCurrentClientToken();
     $speechSrvc = new SpeechService($this->base_url, $token);
     return $speechSrvc->speechToTextCustom($context, $file, $language, $grammar_file, $dictionary_file, $xargs, true);
 }
예제 #4
0
/* This try/catch block tests the textToSpeech method. */
try {
    // Specify the content type.
    $ctype = 'ENTER VALUE!';
    // Specify text to convert to speech.
    $txt = 'ENTER VALUE!';
    // Send the request to convert the specified text to audio.
    $response = $speechSrvc->textToSpeech($ctype, $txt);
    echo 'audio length: ' . strlen($response) . "\n";
} catch (ServiceException $se) {
    echo $se->getErrorResponse();
}
/* This try/catch block tests the speechToTextCustom method. */
try {
    // Enter the path of the file to translate.
    // For example: $fname = '/tmp/file.wav';
    $fname = 'ENTER VALUE!';
    // Enter the path of the grammar file.
    // For example: $gfname = '/tmp/x-grammar.txt'
    $gfname = 'ENTER VALUE!';
    // Enter the path of the dictionary file.
    // For example: $dfname = '/tmp/x-dictionary.txt'
    $dfname = 'ENTER VALUE!';
    $speechContext = 'GenericHints';
    // Send the request to convert the speech file to text.
    $response = $speechSrvc->speechToTextCustom($speechContext, $fname, $gfname, $dfname);
    $recognition = $response['Recognition'];
    echo 'responseId: ' . $recognition['ResponseId'] . "\n";
} catch (ServiceException $se) {
    echo $se->getErrorResponse();
}
    $token = OAuthToken::loadToken($oauth_file);
    if ($token == null || $token->isAccessTokenExpired()) {
        $tokenSrvc = new OAuthTokenService($FQDN, $api_key, $secret_key);
        $token = $tokenSrvc->getTokenUsingScope($scope);
        $token->saveToken($oauth_file);
    }
    $context = $_REQUEST['speechContext'];
    $nameParam = $_REQUEST['nameParam'];
    $filename = $_REQUEST['audioFile'];
    $xArgs = $_REQUEST['x_arg'];
    $flocation = $audioFolder . '/' . $filename;
    $path = __DIR__ . '/../template/';
    $xGrammar = $path . 'x-grammar.txt';
    $xDictionary = $path . 'x-dictionary.txt';
    $srvc = new SpeechService($FQDN, $token);
    $response = $srvc->speechToTextCustom($context, $flocation, $xGrammar, $xDictionary, $xArgs);
    $headers = null;
    $values = array();
    $recognition = $response['Recognition'];
    if (isset($recognition['NBest'])) {
        $nbests = $recognition['NBest'];
        foreach ($nbests as $nbest) {
            $headers = array('ResponseId', 'Status', 'Hypothesis', 'LanguageId', 'Confidence', 'Grade', 'ResultText', 'Words', 'WordScores');
            $values[] = array($recognition['ResponseId'], $recognition['Status'], $nbest['Hypothesis'], $nbest['LanguageId'], $nbest['Confidence'], $nbest['Grade'], $nbest['ResultText'], json_encode($nbest['Words']), json_encode($nbest['WordScores']));
        }
    } else {
        $headers = array('ResponseId', 'Status');
        $values = array($response->getResponseId(), $response->getStatus());
    }
    $arr = array('success' => true, 'tables' => array(array('caption' => 'Speech Response:', 'headers' => $headers, 'values' => $values)));
} catch (Exception $e) {