/**
  * Sends audio file to API and a text representation is sent back.
  *
  * @method speechToText
  *
  * @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} subcontext Speech subcontext for translation. Please see SpeechToText API documentation for parameter values
  * @param {string} xargs X-Arg objects. Please see SpeechToText API documentation for information about this parameter
  * @param {boolean} chunked True to send the file using chunked transfer.
  * @param {string} language ISO language code (like 'en-US')
  *
  * @return {Response} Returns Response object.
  * @throws ServiceException if API request was not successful.
  *
  */
 public function speechToText($file, $context, $subcontext, $xargs, $chunked, $language)
 {
     $filecontents = $this->getFile($file);
     // throws Exception
     // Get OAuth token
     $token = $this->getCurrentClientToken();
     $speechSrvc = new SpeechService($this->base_url, $token);
     return $speechSrvc->speechToText($file, $context, $subcontext, $xargs, $chunked, $language, true);
 }
 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);
 }
 public function handleSpeechToText()
 {
     if (!isset($_REQUEST['SpeechToText'])) {
         return;
     }
     try {
         $context = $_REQUEST['SpeechContext'];
         $_SESSION['SpeechContext'] = $context;
         $filename = $_REQUEST['audio_file'];
         $_SESSION['audio_file'] = $filename;
         $chunked = isset($_REQUEST['chkChunked']) ? $_REQUEST['chkChunked'] : false;
         $_SESSION['chunked'] = $chunked;
         $flocation = $this->_audioFolder . '/' . $filename;
         $subContext = null;
         if ($context == 'Gaming') {
             $subContext = $this->_xSpeechSubContext;
         }
         $srvc = new SpeechService($this->apiFQDN, $this->getFileToken());
         $response = $srvc->speechToText($flocation, $context, $subContext, $this->_xArgs, $chunked);
         $this->results[SpeechController::RESULT_SPEECH_TO_TEXT] = $response;
     } catch (Exception $e) {
         $this->errors[SpeechController::ERROR_SPEECH_TO_TEXT] = $e->getMessage();
     }
 }
    }
    $speechSrvc = new SpeechService($FQDN, $token);
    $speechContext = $_POST['speechContext'];
    $speechFile = $_POST['speechFile'];
    $xArg = $_POST['x_arg'];
    $subContext = $_POST['x_subcontext'];
    $chunked = isset($_POST['chunked']) ? true : null;
    $allowedFiles = array('boston_celtics.wav', 'california.amr', 'coffee.amr', 'doctors.wav', 'nospeech.wav', 'samplerate_conflict_error.wav', 'this_is_a_test.spx', 'too_many_channels_error.wav', 'boston_celtics.wav');
    if (!in_array($speechFile, $allowedFiles, true)) {
        throw new Exception('Invalid speech file specified');
    }
    $flocation = $audioFolder . '/' . $speechFile;
    if ($speechContext !== 'Gaming') {
        $subContext = null;
    }
    $response = $speechSrvc->speechToText($flocation, $speechContext, $subContext, $xArg, $chunked);
    $headers = null;
    $values = null;
    $nbest = $response->getNBest();
    if ($nbest !== null) {
        $headers = array('ResponseId', 'Status', 'Hypothesis', 'LanguageId', 'Confidence', 'Grade', 'ResultText', 'Words', 'WordScores');
        $values = array($response->getResponseId(), $response->getStatus(), $nbest->getHypothesis(), $nbest->getLanguageId(), $nbest->getConfidence(), $nbest->getGrade(), $nbest->getResultText(), json_encode($nbest->getWords()), json_encode($nbest->getWordScores()));
    } else {
        $headers = array('ResponseId', 'Status');
        $values = array($response->getResponseId(), $response->getStatus());
    }
    $arr = array('success' => true, 'tables' => array(array('caption' => 'Speech Response:', 'headers' => $headers, 'values' => array($values))));
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
Esempio n. 5
0
// Get the OAuth access token.
$token = $osrvc->getToken('SPEECH,STTC,TTS');
// Create the service for interacting with the Speech API.
$speechSrvc = new SpeechService('https://api.att.com', $token);
// The Speech API requires the audio files to be certain formats. In order to
// convert speech files to the proper format, the ffmpeg program may be used.
// The ffmpeg program can be downloaded from https://ffmpeg.org/
// The following try/catch blocks can be used to test the methods of the
// Speech API. To test a specific method, comment out the other try/catch blocks.
/* This try/catch block tests the speechToText method. */
try {
    // Enter the path of the file to translate. For example: $fname = '/tmp/file.wav';
    $fname = 'ENTER VALUE!';
    $speechContext = 'Generic';
    // Send the request to convert the speech file to text.
    $response = $speechSrvc->speechToText($fname, $speechContext);
    echo 'responseId: ' . $response->getResponseId() . "\n";
} catch (ServiceException $se) {
    echo $se->getErrorResponse();
}
/* 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();