IVONA TTS SaaS to usługa umożliwiająca automatyczne generowanie mowy. Poniżej krótki przykład demonstrujący podstawowe możliwości generatora .
Przydatne adresy:
Aplikacja zbudowana w modelu MVC umożliwia wprowadzenie tekstu oraz wybranie głosu, w którym ma zostać odczytany.
Treść tekstu, wybrany głos oraz parametru dźwięku są wysyłane do Ivony z użyciem protokołu SOAP. Jako rezultat funkcji tworzącej plik z odczytanym tekstem otrzymujemy kod embed flashowego komponentu odtwarzającego odczytany tekst oraz odnośnik do pliku mp3 z tym nagraniem:
Korzystanie z API wymaga założenia konta na stronie Ivony (formularz rejestracji), dostępne są dwa rodzaje kont – płatne oraz darmowa wersja testowa (szczegóły).
W testowej aplikacji wykorzystano Zend Framework 1.10.1, PHP 5.3 oraz Smarty 3 beta 8.
Wywołanie dowolnej metody z API wymaga autoryzacji. W tym celu należy pobrać token przekazywany następnie jako parametr wywołania metody.
W klasie modelu następuje automatyczne pobranie tokenu (wraz ze sprawdzeniem wyniku pobierania) oraz po wywołaniu metody SOAP sprawdzany jest jej rezultat. W przypadku wystąpienia błędu pobierany jest jego opis.
Klasa kontrolera:
class Ivona_IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->formText = $formText = $this->_getParam('text', ''); $this->view->formVoice = $formVoice = $this->_getParam('voice', ''); $this->view->errorMsg = ''; // Utworzenie obiektu Ivony $ivona = new Ivona(IVONA_LOGIN, IVONA_PASSWORD); // Jeżeli przesłano dane z formularza tworzymy plik z dźwiękiem if ($formText != '' && $formVoice != '') { if (($result = $ivona->createSpeechFile($formText, $formVoice)) === false) { $this->view->errorMsg = $ivona->getErrorMsg(); return; } $this->view->embedCode = $result['embedCode']; $this->view->charactersPrice = $result['charactersPrice']; $this->view->soundUrl = $result['soundUrl']; } else { $this->view->embedCode = ''; $this->view->charactersPrice = ''; $this->view->soundUrl = ''; } // Pobieramy listę dostępnych głosów if (($result = $ivona->getListVoices()) === false) { $this->view->errorMsg = $ivona->getErrorMsg(); return; } $this->view->voices = $result; } } |
Klasa modelu:
class Ivona { /** * Adres pliku wsdl * * @var string */ static $wsdl = 'http://www.ivona.com/saasapiwsdl.php'; /** * Opcje wsdl * * @var array */ static $wsdlOptions = array( 'exceptions' => 0 ); /** * Kodek dźwięku * * @var string */ static $speechCodecId = 'mp3/22050'; /** * Typ uploadowanego tekstu * * @var string */ static $speechContentType = 'text/plain'; /** * Klient Soap * * @var SoapClient|null */ protected $_soapClient = null; /** * Adres email * * @var string|null */ protected $_mail = null; /** * Hasło * * @var string|null */ protected $_password = null; /** * Treść błędu * * @var string */ protected $_errorMsg = ''; /** * Konstruktor * * @param string $email email * @param string $password hasło */ public function __construct($email, $password) { $this->_soapClient = new SoapClient(self::$wsdl, self::$wsdlOptions); $this->_email = $email; $this->_password = $password; } /** * Utworzenie pliku z mową * * @param string $text tekst do odczytania * @param string $voice głos, w którym ma zostać odczytany tekst * @return mixed wynik funkcji */ public function createSpeechFile($text, $voice) { $input = array( 'text' => $text, 'contentType' => self::$speechContentType, 'voiceId' => $voice, 'codecId' => self::$speechCodecId ); return $this->_soapCall('createSpeechFile', $input); } /** * Pobranie listy dostępnych głosów * * @return mixed wynik funkcji */ public function getListVoices() { return $this->_soapCall('listVoices'); } /** * Pobranie tokena * * @return array|bool token lub false w przypadku błędu */ protected function _getToken() { if (($token = $this->_checkResult($this->_soapClient->__soapCall('getToken', array('email' => $this->_email)))) === false) { return false; } return array( 'token' => $token, 'md5' => md5(md5($this->_password) . $token), ); } /** * Sprawdzenie poprawności wyniku wywołania SOAP * * @param mixed $result wynik wywołania * @return mixed */ protected function _checkResult($result) { if (is_soap_fault($result)) { $this->_errorMsg = "SOAP Fault:\n faultcode:[{$result->faultcode}]\n faultstring:[{$result->faultstring}]\n faultactor:[{$result->faultactor}]\n"; return false; } return $result; } /** * Wywołanie SOAP * * @param string $name nazwa funkcji * @param array $input parametry funkcji * @return mixed wynik funkcji */ protected function _soapCall($name, $input = array()) { if (($token = $this->_getToken()) === false) { return false; } return $this->_checkResult($this->_soapClient->__soapCall($name, array_merge($token, $input))); } /** * Pobranie komunikatu o błędach * * @return string */ public function getErrorMsg() { return $this->_errorMsg; } } |
Szablon widoku (Smarty 3):
{if $errorMsg != ''} <h3>Wystapił błąd</h3> {$errorMsg|nl2br} {else} {$embedCode} {if $soundUrl!=''}<a href="{$soundUrl}">pobierz plik</a>{/if} <hr /> <form method="post"> <label for="text">Tekst:</label> <textarea id="text" style="width: 200px; height: 100px;" name="text">{$formText}</textarea> <fieldset> <legend>Głos</legend> {foreach $voices as $voice} <input id="{$voice->voiceId}" name="voice" type="radio" value="{$voice->voiceId}" />voiceId} checked="checked"{/if}/> <label for="{$voice->voiceId}">{$voice->voiceName} ({$voice->langId})</label> {/foreach} </fieldset> <input name="submit" type="submit" value="Test" /> </form> {/if} |