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(); } }
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); }
/** * 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); }
$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(); } /* 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!';
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))); } catch (Exception $e) { $arr = array('success' => false, 'text' => $e->getMessage()); } echo json_encode($arr); /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */