// Refresh phrase data public static void RefreshVoiceData(TTSService service, TTSPreloadVoiceData voiceData, TTSDiskCacheSettings cacheSettings, ref string log) { // Get voice settings if (service == null) { log += "\n-No TTS service found"; return; } // No voice data if (voiceData == null) { log += "\n-No voice data provided"; return; } // Get voice TTSVoiceSettings voiceSettings = service.GetPresetVoiceSettings(voiceData.presetVoiceID); if (voiceSettings == null) { log += "\n-Missing Voice Setting: " + voiceData.presetVoiceID; return; } // Generate if (cacheSettings == null) { cacheSettings = new TTSDiskCacheSettings() { DiskCacheLocation = TTSDiskCacheLocation.Preload }; } // Iterate phrases for (int p = 0; p < voiceData.phrases.Length; p++) { RefreshPhraseData(service, cacheSettings, voiceSettings, voiceData.phrases[p]); } }
/// <summary> /// Imported dictionary data into an existing TTSPreloadSettings asset /// </summary> public static bool ImportData(TTSPreloadSettings preloadSettings, Dictionary <string, List <string> > textsByVoice) { // Import if (preloadSettings == null) { Debug.LogError("TTS Preload Utility - Import Failed - Null Preload Settings"); return(false); } // Whether or not changed bool changed = false; // Generate if needed if (preloadSettings.data == null) { preloadSettings.data = new TTSPreloadData(); changed = true; } // Begin voice list List <TTSPreloadVoiceData> voices = new List <TTSPreloadVoiceData>(); if (preloadSettings.data.voices != null) { voices.AddRange(preloadSettings.data.voices); } // Iterate voice names foreach (var voiceName in textsByVoice.Keys) { // Get voice index if possible int voiceIndex = voices.FindIndex((v) => string.Equals(v.presetVoiceID, voiceName)); // Generate voice TTSPreloadVoiceData voice; if (voiceIndex == -1) { voice = new TTSPreloadVoiceData(); voice.presetVoiceID = voiceName; voiceIndex = voices.Count; voices.Add(voice); } // Use existing else { voice = voices[voiceIndex]; } // Get texts & phrases for current voice List <string> texts = new List <string>(); List <TTSPreloadPhraseData> phrases = new List <TTSPreloadPhraseData>(); if (voice.phrases != null) { foreach (var phrase in voice.phrases) { if (!string.IsNullOrEmpty(phrase.textToSpeak) && !texts.Contains(phrase.textToSpeak)) { texts.Add(phrase.textToSpeak); phrases.Add(phrase); } } } // Get data List <string> newTexts = textsByVoice[voiceName]; if (newTexts != null) { foreach (var newText in newTexts) { if (!string.IsNullOrEmpty(newText) && !texts.Contains(newText)) { changed = true; texts.Add(newText); phrases.Add(new TTSPreloadPhraseData() { textToSpeak = newText }); } } } // Apply voice voice.phrases = phrases.ToArray(); voices[voiceIndex] = voice; } // Apply data if (changed) { preloadSettings.data.voices = voices.ToArray(); EditorUtility.SetDirty(preloadSettings); } // Return changed return(changed); }
// Perform iterate private static IEnumerator PerformIteratePhrases(TTSService service, TTSPreloadData preloadData, TTSPreloadIterateDelegate onIterate, Action <float> onProgress, Action <string> onComplete) { // Get cache settings TTSDiskCacheSettings cacheSettings = new TTSDiskCacheSettings() { DiskCacheLocation = TTSDiskCacheLocation.Preload }; // Get total phrases int phraseTotal = 0; foreach (var voice in preloadData.voices) { if (voice.phrases == null) { continue; } foreach (var phrase in voice.phrases) { phraseTotal++; } } // Begin onProgress?.Invoke(0f); // Iterate int phraseCount = 0; float phraseInc = 1f / (float)phraseTotal; string log = string.Empty; for (int v = 0; v < preloadData.voices.Length; v++) { // Get voice data TTSPreloadVoiceData voiceData = preloadData.voices[v]; // Get voice TTSVoiceSettings voiceSettings = service.GetPresetVoiceSettings(voiceData.presetVoiceID); if (voiceSettings == null) { log += "\n-Missing Voice Setting: " + voiceData.presetVoiceID; phraseCount += voiceData.phrases.Length; continue; } // Iterate phrases for (int p = 0; p < voiceData.phrases.Length; p++) { // Iterate progress float progress = (float)phraseCount / (float)phraseTotal; onProgress?.Invoke(progress); phraseCount++; // Iterate yield return(onIterate(service, cacheSettings, voiceSettings, voiceData.phrases[p], (p2) => onProgress?.Invoke(progress + p2 * phraseInc), (l) => log += l)); } } // Complete onProgress?.Invoke(1f); onComplete?.Invoke(log); }