2020. 10. 19. 15:05
Unity
void DownloadTTS(string folderPath, string text, string fileName)
{
if (System.IO.Directory.Exists(folderPath) == false)
{
Debug.Log($"System.IO.Directory.CreateDirectory(folderPath), folderPath: {folderPath}");
System.IO.Directory.CreateDirectory(folderPath);
}
var filePath = $"{folderPath}/{fileName}.mp3";
if (CheckFile(filePath) == false)
{
StartCoroutine(DownloadFile("http://" + $@"translate.google.com/translate_tts?ie=UTF-8&textlen=32&client=tw-ob&q={text.Substring(0, Mathf.Min(200, text.Length))}&tl=en-gb", filePath));
}
}
Web에서 긁어오는 타입
200자 제한
일본어는 ja다
원샷타입
IEnumerator TextToSpeech(string text)
{
Debug.Log($"TextToSpeech({text})");
if (string.IsNullOrWhiteSpace(text))
{
Debug.Log($"빈문자열이라 정지");
yield break;
}
var co = LoadAudioClipMP3($@"http://translate.google.com/translate_tts?ie=UTF-8&textlen=32&client=tw-ob&q={text.Substring(0, Mathf.Min(200, text.Length))}&tl=en-gb");
StartCoroutine(co);
yield return new WaitUntil(() => co?.Current as AudioClip != null);
var audio = (AudioClip)co.Current;
var obj = new GameObject("WebTTS-" + text);
var audioSource = obj.AddComponent<AudioSource>();
audioSource.PlayOneShot(audio);
Debug.Log($"오디오 길이 {audio.length}초");
Destroy(obj, audio.length + 1); //오디오 시간에 1초를 추가(여유분)
yield return null;
}
static IEnumerator LoadAudioClipMP3(string downloadLink)
{
Debug.Log($"LoadAudioClipMP3({downloadLink})");
UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(downloadLink, AudioType.MPEG);
var operation = unityWebRequest.SendWebRequest();
yield return new WaitUntil(() => operation.isDone);
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
}
else
{
Debug.Log("LoadAudioClipSuccess");
}
var clip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
clip.name = downloadLink;
yield return clip;
}
위에서 딕셔너리 추가버전
Dictionary<string, AudioClip> ttsDict = new Dictionary<string, AudioClip>();
AudioSource lastTTS;
IEnumerator TextToSpeech(string text)
{
Debug.Log($"TextToSpeech({text})");
if (string.IsNullOrWhiteSpace(text))
{
Debug.Log($"빈문자열이라 정지");
yield break;
}
AudioClip audio;
if (ttsDict.ContainsKey(text))
{
Debug.Log($"딕셔너리에서 가져옴({text})");
audio = ttsDict[text];
}
else
{
var co = LoadAudioClipMP3($@"http://translate.google.com/translate_tts?ie=UTF-8&textlen=32&client=tw-ob&q={text.Substring(0, Mathf.Min(200, text.Length))}&tl=ja");
StartCoroutine(co);
yield return new WaitUntil(() => co?.Current as AudioClip != null);
audio = (AudioClip)co.Current;
ttsDict.Add(text, audio);
}
var obj = new GameObject("WebTTS-" + text);
var audioSource = obj.AddComponent<AudioSource>();
if (lastTTS!=null)
{
Destroy(lastTTS);
}
lastTTS = audioSource;
audioSource.PlayOneShot(audio);
//audioSource.pitch = 1.2f;
Debug.Log($"오디오 길이 {audio.length}초");
Destroy(obj, audio.length + 1); //오디오 시간에 1초를 추가(여유분)
yield return null;
}
static IEnumerator LoadAudioClipMP3(string downloadLink)
{
Debug.Log($"LoadAudioClipMP3({downloadLink})");
UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(downloadLink, AudioType.MPEG);
var operation = unityWebRequest.SendWebRequest();
yield return new WaitUntil(() => operation.isDone);
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
}
else
{
Debug.Log("LoadAudioClipSuccess");
}
var clip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
clip.name = downloadLink;
yield return clip;
}
'Unity' 카테고리의 다른 글
유니티 내비게이션 navigation (0) | 2020.10.25 |
---|---|
유니티 Mask (0) | 2020.09.24 |
유니티 IDE(비주얼스튜디오) 바꾸기 (0) | 2020.08.19 |