diff --git a/Documentation/docfx.json b/Documentation/docfx.json index 2bcea65..fbd557b 100644 --- a/Documentation/docfx.json +++ b/Documentation/docfx.json @@ -11,7 +11,7 @@ ], "dest": "api", "filter": "filterConfig.yml", - "includePrivateMembers": true, + "includePrivateMembers": false, "allowCompilationErrors": true } ], diff --git a/Documentation/index.md b/Documentation/index.md index 2b30a41..77d4a87 100644 --- a/Documentation/index.md +++ b/Documentation/index.md @@ -11,7 +11,7 @@ A Unity C# wrapper for the Google Cloud Text-To-Speech API. ## Installation -This *should* work on any reasonably modern Unity version. Built and tested in Unity 6.0. +Requires Unity 6.0 because of the plugin's usage of [*Awaitable*](https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Awaitable.html). Built and tested in Unity 6.0. # [OpenUPM](#tab/openupm) diff --git a/README.md b/README.md index 1cafe10..730c95e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A Unity C# wrapper for the Google Cloud Text-To-Speech API. ## Installation -This *should* work on any reasonably modern Unity version. Built and tested in Unity 6.0. +Requires Unity 6.0 because of the plugin's usage of [*Awaitable*](https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Awaitable.html). Built and tested in Unity 6.0. ### OpenUPM diff --git a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/APIReferenceManual.pdf b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/APIReferenceManual.pdf index 2698f9a..69e14ef 100644 Binary files a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/APIReferenceManual.pdf and b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/APIReferenceManual.pdf differ diff --git a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/Documentation.pdf b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/Documentation.pdf index c5ec121..126c89a 100644 Binary files a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/Documentation.pdf and b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Documentation~/Documentation.pdf differ diff --git a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Synthesis/Response/TextToSpeechSynthesisResponse.cs b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Synthesis/Response/TextToSpeechSynthesisResponse.cs index 1b7d1ee..3626575 100644 --- a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Synthesis/Response/TextToSpeechSynthesisResponse.cs +++ b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Synthesis/Response/TextToSpeechSynthesisResponse.cs @@ -1,7 +1,6 @@ using Newtonsoft.Json; using System; using System.IO; -using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; @@ -39,8 +38,9 @@ public class TextToSpeechSynthesisResponse /// The encoding of the audio. /// The audio converted to an . /// Thrown if is unsupported. - public async Task ToAudioClip(TextToSpeechSynthesisAudioEncoding encoding) + public async Awaitable ToAudioClip(TextToSpeechSynthesisAudioEncoding encoding) { + await Awaitable.MainThreadAsync(); AudioType audioType = encoding switch { TextToSpeechSynthesisAudioEncoding.WavLinear16 => AudioType.WAV, @@ -50,7 +50,7 @@ public async Task ToAudioClip(TextToSpeechSynthesisAudioEncoding enco string path = Path.Combine(Application.temporaryCachePath, $"{nameof(TextToSpeechSynthesisResponse)}.bin"); byte[] buffer = Convert.FromBase64String(Audio); - File.WriteAllBytes(path, buffer); + await File.WriteAllBytesAsync(path, buffer); using UnityWebRequest audioClipRequest = UnityWebRequestMultimedia.GetAudioClip($"file://{path}", audioType); await audioClipRequest.SendWebRequest(); @@ -65,7 +65,7 @@ public async Task ToAudioClip(TextToSpeechSynthesisAudioEncoding enco /// /// The audio converted to an . /// Thrown if is unsupported. - public async Task ToAudioClip() + public async Awaitable ToAudioClip() { return await ToAudioClip(AudioMetadata?.Encoding ?? TextToSpeechSynthesisAudioEncoding.Default); } diff --git a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/TextToSpeechVoiceType.cs b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/TextToSpeechVoiceType.cs index d9491ae..61028f3 100644 --- a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/TextToSpeechVoiceType.cs +++ b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/TextToSpeechVoiceType.cs @@ -5,6 +5,11 @@ /// public enum TextToSpeechVoiceType { + /// + /// Default value, ignore. + /// + None, + /// /// Standard /// diff --git a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Voices/IEnumerableExtensions.cs b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Voices/IEnumerableExtensions.cs index 8f2f77c..8276349 100644 --- a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Voices/IEnumerableExtensions.cs +++ b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Data/Voices/IEnumerableExtensions.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; @@ -16,7 +17,7 @@ public static class IEnumerableExtensions /// Voices with the type and gender corresponding to and . public static IEnumerable FilterByAttributes(this IEnumerable thiz, TextToSpeechVoiceGender gender, TextToSpeechVoiceType type) { - return from voice in thiz where voice.Gender == gender && voice.Name.Type == type select voice; + return from voice in thiz where voice?.Gender == gender && voice?.Name?.Type == type select voice; } /// @@ -26,7 +27,7 @@ public static IEnumerable FilterByAttributes(this IEnumerable /// Voices with the gender corresponding to . public static IEnumerable FilterByGender(this IEnumerable thiz, TextToSpeechVoiceGender gender) { - return from voice in thiz where voice.Gender == gender select voice; + return from voice in thiz where voice?.Gender == gender select voice; } /// @@ -36,7 +37,7 @@ public static IEnumerable FilterByGender(this IEnumerableVoices with the type corresponding to . public static IEnumerable FilterByType(this IEnumerable thiz, TextToSpeechVoiceType type) { - return from voice in thiz where voice.Name.Type == type select voice; + return from voice in thiz where voice?.Name?.Type == type select voice; } /// @@ -47,7 +48,42 @@ public static IEnumerable FilterByType(this IEnumerableA voice with the type and gender corresponding to and , if not found. public static TextToSpeechVoice FindByAttributes(this IEnumerable thiz, TextToSpeechVoiceGender gender, TextToSpeechVoiceType type) { - return thiz.FirstOrDefault(voice => voice.Gender == gender && voice.Name.Type == type); + return thiz.FirstOrDefault(voice => voice?.Gender == gender && voice?.Name?.Type == type); + } + + /// + /// Finds the first voice with the given and one of the specified , + /// where the order of types in specifies preference. + /// + /// + /// The method searches the collection and returns the first voice that matches the gender and any of the provided types, + /// preferring the order in which the types are listed. + /// + /// The collection of voices to search within. + /// The gender to filter by. + /// The types to filter by. Order indicates preference. + /// The first voice matching the gender and one of the specified types, or null if none is found. + public static TextToSpeechVoice FindByAttributes(this IEnumerable thiz, TextToSpeechVoiceGender gender, params TextToSpeechVoiceType[] types) + { + if (types.Length < 2) + return types.Length == 0 ? FindByGender(thiz, gender) : FindByAttributes(thiz, gender, types[0]); + + Dictionary foundVoices = new(types.Length); + foreach (TextToSpeechVoice voice in thiz) + { + if (voice?.Gender != gender) + continue; + + TextToSpeechVoiceType type = voice.Name.Type; + int index = Array.IndexOf(types, type); + + if (index == 0) + return voice; + else if (index >= 0 && !foundVoices.ContainsKey(type)) + foundVoices[type] = voice; + } + + return (from type in types where foundVoices.ContainsKey(type) select foundVoices[type]).FirstOrDefault(); } /// @@ -57,7 +93,7 @@ public static TextToSpeechVoice FindByAttributes(this IEnumerableA voice with the gender corresponding to , if not found. public static TextToSpeechVoice FindByGender(this IEnumerable thiz, TextToSpeechVoiceGender gender) { - return thiz.FirstOrDefault(voice => voice.Gender == gender); + return thiz.FirstOrDefault(voice => voice?.Gender == gender); } /// @@ -67,7 +103,7 @@ public static TextToSpeechVoice FindByGender(this IEnumerable /// A voice with the type corresponding to , if not found. public static TextToSpeechVoice FindByType(this IEnumerable thiz, TextToSpeechVoiceType type) { - return thiz.FirstOrDefault(voice => voice.Name.Type == type); + return thiz.FirstOrDefault(voice => voice?.Name?.Type == type); } /// @@ -77,8 +113,8 @@ public static TextToSpeechVoice FindByType(this IEnumerable t /// A voice with the name corresponding to , if not found. public static TextToSpeechVoice FindByName(this IEnumerable thiz, TextToSpeechVoiceName name) { - string nameStr = name.FullName; - return thiz.FirstOrDefault(voice => voice.Name.FullName == nameStr); + string nameStr = name?.FullName; + return thiz.FirstOrDefault(voice => voice?.Name?.FullName == nameStr); } } } \ No newline at end of file diff --git a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Managers/TextToSpeechManager.cs b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Managers/TextToSpeechManager.cs index 0bc72ac..49622c0 100644 --- a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Managers/TextToSpeechManager.cs +++ b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/Runtime/Scripts/Managers/TextToSpeechManager.cs @@ -1,6 +1,5 @@ using Newtonsoft.Json; using System; -using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; using Uralstech.UCloud.TextToSpeech.Exceptions; @@ -58,8 +57,9 @@ public void SetApiKey(string apiKey) /// The computed response. /// Thrown if the API request fails. /// Thrown if the response could not be parsed. - public async Task Request(ITextToSpeechPostRequest request) + public async Awaitable Request(ITextToSpeechPostRequest request) { + await Awaitable.MainThreadAsync(); string utf8RequestData = request.GetUtf8EncodedData(); string requestEndpoint = request.GetEndpointUri(); @@ -85,8 +85,9 @@ public async Task Request(ITextToSpeechPostRequest request /// The computed response. /// Thrown if the API request fails. /// Thrown if the response could not be parsed. - public async Task Request(ITextToSpeechGetRequest request) + public async Awaitable Request(ITextToSpeechGetRequest request) { + await Awaitable.MainThreadAsync(); string requestEndpoint = request.GetEndpointUri(); using UnityWebRequest webRequest = UnityWebRequest.Get(requestEndpoint); diff --git a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/package.json b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/package.json index 6a4eb3d..44318b5 100644 --- a/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/package.json +++ b/UCloud.TextToSpeech/Packages/com.uralstech.ucloud.texttospeech/package.json @@ -3,7 +3,7 @@ "displayName": "UCloud.TextToSpeech", "description": "A Unity C# wrapper for the Google Cloud Text-To-Speech API.", "keywords": [], - "version": "1.1.0", + "version": "1.2.0", "unity": "6000.0", "hideInEditor": false, "documentationUrl": "https://uralstech.github.io/UCloud.TextToSpeech/",