Advertisement
Guest User

ISpeechTTS - Unity 3D

a guest
Dec 15th, 2015
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Text.RegularExpressions;
  4.  
  5.  
  6. /*
  7.  *  Internet required to operate.
  8.  *
  9.  *  Heads up! ISpeech is NOT free, but you can use as a developer. More information here:
  10.  *  https://www.ispeech.org/developer/purchase/plans
  11.  *
  12.  *  Documentation:  http://www.ispeech.org/api
  13.  *
  14.  */
  15.  
  16.  
  17. public class ISpeechTTS : MonoBehaviour {
  18.  
  19.     public string words = "Hello";
  20.     //============== TTS settings ==============
  21.     public enum VOICES {
  22.         //US English Female (default)
  23.         usenglishfemale,
  24.         //US English Male
  25.         usenglishmale,
  26.         //UK English Female
  27.         ukenglishfemale,
  28.         //UK English Male
  29.         ukenglishmale,
  30.         //Australian English Female
  31.         auenglishfemale,
  32.         //US Spanish Female
  33.         usspanishfemale,
  34.         //US Spanish Male
  35.         usspanishmale,
  36.         //Chinese Female
  37.         chchinesefemale,
  38.         //Chinese Male
  39.         chchinesemale,
  40.         //Hong Kong Cantonese Female
  41.         hkchinesefemale,
  42.         //Taiwan Chinese Female
  43.         twchinesefemale,
  44.         //Japanese Female
  45.         jpjapanesefemale,
  46.         //Japanese Male
  47.         jpjapanesemale,
  48.         //Korean Female
  49.         krkoreanfemale,
  50.         //Korean Male
  51.         krkoreanmale,
  52.         //Canadian English Female
  53.         caenglishfemale,
  54.         //Hungarian Female
  55.         huhungarianfemale,
  56.         //Brazilian Portuguese Female
  57.         brportuguesefemale,
  58.         //European Portuguese Female
  59.         eurportuguesefemale,
  60.         //European Portuguese Male
  61.         eurportuguesemale,
  62.         //European Spanish Female
  63.         eurspanishfemale,
  64.         //European Spanish Male
  65.         eurspanishmale,
  66.         //European Catalan Female
  67.         eurcatalanfemale,
  68.         //European Czech Female
  69.         eurczechfemale,
  70.         //European Danish Female
  71.         eurdanishfemale,
  72.         //European Finnish Female
  73.         eurfinnishfemale,
  74.         //European French Female
  75.         eurfrenchfemale,
  76.         //European French Male
  77.         eurfrenchmale,
  78.         //European Norwegian Female
  79.         eurnorwegianfemale,
  80.         //European Dutch Female
  81.         eurdutchfemale,
  82.         //European Polish Female
  83.         eurpolishfemale,
  84.         //European Italian Female
  85.         euritalianfemale,
  86.         //European Italian Male
  87.         euritalianmale,
  88.         //European Turkish Female
  89.         eurturkishfemale,
  90.         //European Turkish Male
  91.         eurturkishmale,
  92.         //European German Female
  93.         eurgermanfemale,
  94.         //European German Male
  95.         eurgermanmale,
  96.         //Russian Female
  97.         rurussianfemale,
  98.         //Russian Male
  99.         rurussianmale,
  100.         //Swedish Female
  101.         swswedishfemale,
  102.         //Canadian French Female
  103.         cafrenchfemale,
  104.         //Canadian French Male
  105.         cafrenchmale
  106.     };
  107.     public VOICES voiceStyle;
  108.     //-----------------------------
  109.     private AudioType audioType;
  110.     public enum AUDIOFORMAT{
  111.         mp3,
  112.         ogg,
  113.         mp4,
  114.         wav,
  115.         wma
  116.     }
  117.     private AUDIOFORMAT audioFormat;
  118.     //--------------------------------
  119.     //put YOUR key to iSpeech here.
  120.     public string ApiKey = "developerdemokeydeveloperdemokey";//developerdemokeydeveloperdemokey
  121.     //==============================================
  122.  
  123.  
  124.     public AudioSource audio = null;
  125.     private WWW www;
  126.  
  127.  
  128.  
  129.     void Start ()
  130.     {
  131.         if(audio == null)
  132.         {
  133.             audio = GetComponent<AudioSource>();
  134.         }
  135.  
  136.          
  137.         #if UNITY_ANDROID
  138.         audioFormat = AUDIOFORMAT.mp3;
  139.         audioType = AudioType.MPEG;
  140.         #else//Windows
  141.         audioFormat = AUDIOFORMAT.ogg;
  142.         audioType = AudioType.OGGVORBIS;
  143.         #endif
  144.     }
  145.  
  146.  
  147.  
  148.     IEnumerator GetTTS ()
  149.     {
  150.         // Remove the "spaces" in excess
  151.         Regex rgx = new Regex ("\\s+");
  152.         // Replace the "spaces" with "% 20" for the link Can be interpreted
  153.         string result = rgx.Replace (words, "%20");
  154.         string url = "http://api.ispeech.org/api/rest?"+"apikey="+ApiKey+"&action=convert"+"&voice="+voiceStyle+"&format="+audioFormat+"&frequency=44100"+"&text="+result;
  155.  
  156.         www = new WWW (url);
  157.         yield return www;
  158.         audio.clip = www.GetAudioClip (false,false,audioType);
  159.         audio.Play ();
  160.     }
  161.  
  162.  
  163.  
  164.     void OnGUI ()
  165.     {
  166.         words = GUI.TextField (new Rect (0, 10, Screen.width, 50), words);
  167.         if (GUI.Button (new Rect (0, Screen.height-50-10, Screen.width, 50), "Speak")) {
  168.             StartCoroutine (GetTTS ());
  169.         }
  170.     }
  171.        
  172.  
  173.  
  174. }//fecha classe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement