├── .gitignore ├── Assets ├── Scene.meta ├── Scene │ ├── TextToSpeechDemo.unity │ └── TextToSpeechDemo.unity.meta ├── Script.meta └── Script │ ├── PlayTextToSpeech.cs │ ├── PlayTextToSpeech.cs.meta │ ├── TextToSpeechManager.cs │ └── TextToSpeechManager.cs.meta ├── LICENSE ├── ProjectSettings ├── AudioManager.asset ├── ClusterInputManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset └── UnityConnectSettings.asset ├── README.md └── SpeechToText.png /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | 8 | # Visual Studio 2015 cache directory 9 | /.vs/ 10 | 11 | # Autogenerated VS/MD/Consulo solution and project files 12 | ExportedObj/ 13 | .consulo/ 14 | *.csproj 15 | *.unityproj 16 | *.sln 17 | *.suo 18 | *.tmp 19 | *.user 20 | *.userprefs 21 | *.pidb 22 | *.booproj 23 | *.svd 24 | *.pdb 25 | 26 | # Unity3D generated meta files 27 | *.pidb.meta 28 | 29 | # Unity3D Generated File On Crash Reports 30 | sysinfo.txt 31 | 32 | # Builds 33 | *.apk 34 | *.unitypackage 35 | -------------------------------------------------------------------------------- /Assets/Scene.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 072d566d0313e244abef1f6191802e17 3 | folderAsset: yes 4 | timeCreated: 1507742727 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scene/TextToSpeechDemo.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/Assets/Scene/TextToSpeechDemo.unity -------------------------------------------------------------------------------- /Assets/Scene/TextToSpeechDemo.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 304d8b96f82003b4bb89e2d4fd31616e 3 | timeCreated: 1507738012 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Script.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 09fafe616e36ce74fa2934ff1c5f6e35 3 | folderAsset: yes 4 | timeCreated: 1507742727 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Script/PlayTextToSpeech.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | using TextToSpeech; 6 | using System; 7 | 8 | [RequireComponent(typeof(AudioSource))] 9 | public class PlayTextToSpeech : MonoBehaviour 10 | { 11 | [SerializeField] TextToSpeechManager manager; 12 | [SerializeField] Button _button; 13 | [SerializeField] InputField _inputField; 14 | [SerializeField] Dropdown _languageDropDownList; 15 | [SerializeField] Dropdown _audioCodecsDropDownList; 16 | 17 | private string text; 18 | 19 | AudioSource _audioSource; 20 | 21 | private void Start() 22 | { 23 | _audioSource = GetComponent(); 24 | 25 | if (_button != null) 26 | { 27 | _button.onClick.AddListener(PlayVoice); 28 | } 29 | 30 | foreach (string name in Enum.GetNames((typeof(Language)))) 31 | { 32 | _languageDropDownList.options.Add(new Dropdown.OptionData(name)); 33 | } 34 | _languageDropDownList.value =3; 35 | 36 | foreach (string name in Enum.GetNames((typeof(AudioCodecs)))) 37 | { 38 | _audioCodecsDropDownList.options.Add(new Dropdown.OptionData(name)); 39 | } 40 | _audioCodecsDropDownList.value = 3; 41 | } 42 | 43 | public void PlayVoice() 44 | { 45 | StartCoroutine("RunPlayVoice"); 46 | } 47 | 48 | IEnumerator RunPlayVoice() 49 | { 50 | OnGetVoiceStart(); 51 | 52 | text = _inputField.text; 53 | int languageIndex = _languageDropDownList.value; 54 | int audioCodecsIndex = _audioCodecsDropDownList.value; 55 | string url = manager.GetTextToSpeechAudioWithIndex(text, languageIndex, audioCodecsIndex); 56 | WWW www = new WWW(url); 57 | yield return www; 58 | AudioClip clip = www.GetAudioClip(false, false, manager.GetCurrentAudioTypeWithIndex(audioCodecsIndex)); 59 | if (clip.length > 0 && clip !=null) 60 | { 61 | _audioSource.clip = clip; 62 | _audioSource.Play(); 63 | } 64 | else 65 | { 66 | Debug.LogError("Failed to get the voice. Please try:\n" + 67 | "1.Try it in other languages.\n" + 68 | "2.Fill in something in text field.\n" + 69 | "3.Choose the correct audio format."); 70 | } 71 | 72 | OnGetVoiceEnd(); 73 | } 74 | 75 | void OnGetVoiceStart() 76 | { 77 | if (_button != null) 78 | { 79 | _button.enabled = false; 80 | _button.targetGraphic.color = _button.colors.disabledColor; 81 | } 82 | } 83 | 84 | void OnGetVoiceEnd() 85 | { 86 | if (_button != null) 87 | { 88 | _button.enabled = true; 89 | _button.targetGraphic.color = _button.colors.normalColor; 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /Assets/Script/PlayTextToSpeech.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: faafd21fd6dd3d44ab44264394cce712 3 | timeCreated: 1507737271 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Script/TextToSpeechManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Text.RegularExpressions; 4 | using UnityEngine; 5 | 6 | namespace TextToSpeech 7 | { 8 | public enum AudioCodecs 9 | { 10 | MP3=0, 11 | WAV, 12 | AAC, 13 | OGG 14 | } 15 | 16 | public enum Language 17 | { 18 | Catalan=0, 19 | Chinese_China, 20 | Chinese_HongKong, 21 | Chinese_Taiwan, 22 | Danish, 23 | Dutch, 24 | English_Australia, 25 | English_Canada, 26 | English_GreatBritain, 27 | English_India, 28 | English_UnitedStates, 29 | Finnish, 30 | French_Canada, 31 | French_France, 32 | German, 33 | Italian, 34 | Japanese, 35 | Korean, 36 | Norwegian, 37 | Polish, 38 | Portuguese_Brazil, 39 | Portuguese_Portugal, 40 | Russian, 41 | Spanish_Mexico, 42 | Spanish_Spain, 43 | Swedish_Sweden 44 | } 45 | 46 | public class TextToSpeechOption 47 | { 48 | public static string[] language = new string[] 49 | { 50 | "ca-es", 51 | "zh-cn", 52 | "zh-hk", 53 | "zh-tw", 54 | "da-dk", 55 | "nl-nl", 56 | "en-au", 57 | "en-ca", 58 | "en-gb", 59 | "en-in", 60 | "en-us", 61 | "fi-fi", 62 | "fr-ca", 63 | "fr-fr", 64 | "de-de", 65 | "it-it", 66 | "ja-jp", 67 | "ko-kr", 68 | "nb-no", 69 | "pl-pl", 70 | "pt-br", 71 | "pt-pt", 72 | "ru-ru", 73 | "es-mx", 74 | "es-es", 75 | "sv-se" 76 | }; 77 | 78 | public static string[] audioCodecs = new string[] 79 | { 80 | "MP3", 81 | "WAV", 82 | "AAC", 83 | "OGG" 84 | }; 85 | } 86 | 87 | public class TextToSpeechManager : MonoBehaviour 88 | { 89 | public Language selectedLanguage = Language.English_UnitedStates; 90 | public AudioCodecs selectedAudioCodecs = AudioCodecs.OGG; 91 | 92 | private string _APIKey = "086fb34ec40c435ebb3b492c5742cdbb"; 93 | 94 | private string _language; 95 | private string _source; 96 | private string _audioCodecs; 97 | private string _url = "http://api.voicerss.org/?key={0}&hl={1}&src={2}&c={3}"; 98 | 99 | private string _currentLanguage 100 | { 101 | get 102 | { 103 | _language = TextToSpeechOption.language[(int)selectedLanguage]; 104 | return _language; 105 | } 106 | } 107 | 108 | private string _currentAudioCodecs 109 | { 110 | get 111 | { 112 | _audioCodecs = TextToSpeechOption.audioCodecs[(int)selectedAudioCodecs]; 113 | return _audioCodecs; 114 | } 115 | } 116 | 117 | private AudioType GetAudioType(AudioCodecs audioCodecs) 118 | { 119 | AudioType type = AudioType.OGGVORBIS; 120 | switch (audioCodecs) 121 | { 122 | case AudioCodecs.AAC: 123 | type=AudioType.ACC; 124 | break; 125 | 126 | case AudioCodecs.MP3: 127 | type=AudioType.MPEG; 128 | break; 129 | 130 | case AudioCodecs.OGG: 131 | type=AudioType.OGGVORBIS; 132 | break; 133 | 134 | case AudioCodecs.WAV: 135 | type=AudioType.WAV; 136 | break; 137 | 138 | default: 139 | type=AudioType.OGGVORBIS; 140 | break; 141 | } 142 | 143 | return type; 144 | } 145 | 146 | private AudioType GetAudioTypeWithIndex(int index) 147 | { 148 | AudioType type = AudioType.OGGVORBIS; 149 | switch (index) 150 | { 151 | case 0: 152 | type = AudioType.MPEG; 153 | break; 154 | 155 | case 1: 156 | type = AudioType.WAV; 157 | break; 158 | 159 | case 2: 160 | type = AudioType.ACC; 161 | break; 162 | 163 | case 3: 164 | type = AudioType.OGGVORBIS; 165 | break; 166 | 167 | default: 168 | type = AudioType.OGGVORBIS; 169 | break; 170 | } 171 | 172 | return type; 173 | } 174 | 175 | public AudioType GetCurrentAudioType() 176 | { 177 | return GetAudioType(selectedAudioCodecs); 178 | } 179 | 180 | public AudioType GetCurrentAudioTypeWithIndex(int index) 181 | { 182 | return GetAudioTypeWithIndex(index); 183 | } 184 | 185 | public string GetTextToSpeechAudio(string content,Language language = Language.English_UnitedStates, AudioCodecs audioCodecs = AudioCodecs.OGG) 186 | { 187 | selectedLanguage = language; 188 | selectedAudioCodecs = audioCodecs; 189 | 190 | return SetupAudioURL(content, _currentLanguage, _currentAudioCodecs); 191 | } 192 | 193 | //Language index Default = 10 (English-US) , Audio Format Index Default = 3(OGG) 194 | public string GetTextToSpeechAudioWithIndex(string content, int languageIndex =10, int audioCodecsIndex =3) 195 | { 196 | string language = TextToSpeechOption.language[languageIndex]; 197 | string audioformat= TextToSpeechOption.audioCodecs[audioCodecsIndex]; 198 | 199 | return SetupAudioURL(content, language, audioformat); 200 | } 201 | 202 | private string SetupAudioURL(string content, string language, string audioFormat) 203 | { 204 | Regex rgx = new Regex("\\s+"); 205 | _source = rgx.Replace(content, "%20"); 206 | 207 | return string.Format(_url, _APIKey, language, _source, audioFormat); 208 | } 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /Assets/Script/TextToSpeechManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0cfaf82ba67232249b04ca221832b531 3 | timeCreated: 1507734746 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Gerald-Chuah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.6.2f1 2 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity-TextToSpeech 2 | Simple text to speech demo 3 | -------------------------------------------------------------------------------- /SpeechToText.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gerald-Chuah/Unity-TextToSpeech/478794f5cdaf0bb4d791f36eca986bfde51d235e/SpeechToText.png --------------------------------------------------------------------------------