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 _handleTextToSpeech() { if (!isset($_REQUEST['TextToSpeechButton'])) { return; } try { $ctype = $_REQUEST['ContentType']; $txt = null; if (strcmp($ctype, 'text/plain') == 0) { $txt = file_get_contents(__DIR__ . '/../../text/PlainText.txt'); } else { $txt = file_get_contents(__DIR__ . '/../../text/SSMLWithPhoneme.txt'); } $srvc = new SpeechService($this->apiFQDN, $this->getFileToken()); $response = $srvc->textToSpeech($ctype, $txt, $this->_xArgs); $this->results[SpeechController::RESULT_TTS] = $response; } catch (Exception $e) { $this->errors[SpeechController::ERROR_TTS] = $e->getMessage(); } }
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(); } }
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(); } }
require_once __DIR__ . '/../lib/OAuth/OAuthTokenService.php'; require_once __DIR__ . '/../lib/Util/Util.php'; require_once __DIR__ . '/../lib/Speech/SpeechService.php'; use Att\Api\OAuth\OAuthToken; use Att\Api\OAuth\OAuthTokenService; use Att\Api\Util\Util; use Att\Api\Speech\SpeechService; $arr = null; try { $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); } $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;
/** * Sends the text for conversion to the codekit and returns the audio file. * * @method textToSpeech * * @param {string} data.0 Token for authentication * @param {string} ctype Content type - default 'text/plain' * @param {string} text Text to be converted to speech * @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' * @param {string} accept desired audio type - default 'audio/amr-wb' * * @return an array whose first entry is the Content-Type of the audio, * and whose second entry is the raw audio data. * @throws ServiceException if API request was not successful. * */ public function textToSpeech($ctype, $text, $xargs, $language = null, $accept = null) { // Get OAuth token $token = $this->getCurrentClientToken(); $speechSrvc = new SpeechService($this->base_url, $token); return $speechSrvc->textToSpeech($ctype, $text, $xargs, $language, $accept); }
use Att\Api\Speech\SpeechService; // Use the app account settings from developer.att.com for the following values. // Make sure that the API scope is set to SPEECH for the Speech API before // retrieving the App Key and App Secret. // Enter the value from the 'App Key' field obtained at developer.att.com // in your app account. $clientId = 'ENTER VALUE!'; // Enter the value from the 'App Secret' field obtained at developer.att.com // in your app account. $clientSecret = 'ENTER VALUE!'; // Create the service for requesting an OAuth access token. $osrvc = new OAuthTokenService('https://api.att.com', $clientId, $clientSecret); // 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();
require_once __DIR__ . '/../lib/OAuth/OAuthTokenService.php'; require_once __DIR__ . '/../lib/Util/Util.php'; require_once __DIR__ . '/../lib/Speech/SpeechService.php'; use Att\Api\OAuth\OAuthToken; use Att\Api\OAuth\OAuthTokenService; use Att\Api\Util\Util; use Att\Api\Speech\SpeechService; $arr = null; try { $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); } $speechSrvc = new SpeechService($FQDN, $token); $contentType = $_POST['contentType']; $plaintext = $_POST['plaintext']; $ssml = $_POST['ssml']; $xArg = $_POST['x_arg']; $txt = null; if ($contentType === 'text/plain') { $txt = $plaintext; if (strlen($txt) > 250) { throw new Exception("Character limit of 250 reached"); } } else { $txt = $ssml; } $response = $speechSrvc->textToSpeech($contentType, $txt, $xArg); $arr = array('success' => true, 'audio' => array('type' => 'audio/wav', 'base64' => base64_encode($response)));
try { $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)));