From d69ab01104529d3eb25a47432903454029dd67e2 Mon Sep 17 00:00:00 2001 From: "manish.r" Date: Fri, 26 May 2017 11:35:19 +0530 Subject: [PATCH] Base Code Change-Id: I39c220cd7ffe6c84a78b64643d65d446aa5047a4 Signed-off-by: manish.r --- LICENSE | 202 +++++++ packaging/csapi-uix-tts-engine.manifest | 5 + packaging/csapi-uix-tts-engine.spec | 44 ++ .../Interop/Interop.Libraries.cs | 30 + .../Interop/Interop.TtsEngine.cs | 180 ++++++ src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.csproj | 16 + src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.snk | Bin 0 -> 596 bytes .../Tizen.Uix.TtsEngine/ExceptionFactory.cs | 101 ++++ .../Tizen.Uix.TtsEngine/TtsEngine.cs | 626 +++++++++++++++++++++ 9 files changed, 1204 insertions(+) create mode 100755 LICENSE create mode 100755 packaging/csapi-uix-tts-engine.manifest create mode 100755 packaging/csapi-uix-tts-engine.spec create mode 100755 src/Tizen.Uix.TtsEngine/Interop/Interop.Libraries.cs create mode 100755 src/Tizen.Uix.TtsEngine/Interop/Interop.TtsEngine.cs create mode 100755 src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.csproj create mode 100755 src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.snk create mode 100755 src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine/ExceptionFactory.cs create mode 100755 src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine/TtsEngine.cs diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packaging/csapi-uix-tts-engine.manifest b/packaging/csapi-uix-tts-engine.manifest new file mode 100755 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/csapi-uix-tts-engine.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/csapi-uix-tts-engine.spec b/packaging/csapi-uix-tts-engine.spec new file mode 100755 index 0000000..ce03709 --- /dev/null +++ b/packaging/csapi-uix-tts-engine.spec @@ -0,0 +1,44 @@ +Name: csapi-uix-tts-engine +Summary: Tizen Uix tts engine API for C# +Version: 1.0.2 +Release: 1 +Group: Development/Libraries +License: Apache-2.0 +URL: https://www.tizen.org +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest + +AutoReqProv: no +ExcludeArch: aarch64 + +BuildRequires: dotnet-build-tools + +# C# API Requires +BuildRequires: csapi-tizen-nuget + +%define Assemblies Tizen.Uix.TtsEngine + +%description +%{summary} + +%dotnet_import_sub_packages + +%prep +%setup -q +cp %{SOURCE1} . + +%build +for ASM in %{Assemblies}; do +%dotnet_build $ASM +%dotnet_pack $ASM/$ASM.nuspec %{version} +done + +%install +for ASM in %{Assemblies}; do +%dotnet_install $ASM +done + +%files +%manifest %{name}.manifest +%license LICENSE +%attr(644,root,root) %{dotnet_assembly_files} diff --git a/src/Tizen.Uix.TtsEngine/Interop/Interop.Libraries.cs b/src/Tizen.Uix.TtsEngine/Interop/Interop.Libraries.cs new file mode 100755 index 0000000..178808e --- /dev/null +++ b/src/Tizen.Uix.TtsEngine/Interop/Interop.Libraries.cs @@ -0,0 +1,30 @@ +/* +* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + + +/// +/// Partial Interop Class +/// +internal static partial class Interop +{ + /// + /// Partial Libraries Class + /// + internal static partial class Libraries + { + internal const string TtsEngine = "libtts_engine.so"; + } +} diff --git a/src/Tizen.Uix.TtsEngine/Interop/Interop.TtsEngine.cs b/src/Tizen.Uix.TtsEngine/Interop/Interop.TtsEngine.cs new file mode 100755 index 0000000..0a2a962 --- /dev/null +++ b/src/Tizen.Uix.TtsEngine/Interop/Interop.TtsEngine.cs @@ -0,0 +1,180 @@ +/* +* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + + +using System; +using System.Runtime.InteropServices; +using Tizen.Uix.TtsEngine; +using static Tizen.Uix.TtsEngine.Engine; + +/// +/// Partial Interop Class +/// +internal static partial class Interop +{ + /// + /// TtsEngine Interop Class + /// + internal static class TtsEngine + { + internal static string LogTag = "Tizen.Uix.TtsEngine"; + + private const int ErrorTts = -0x02F10000; + + public enum ErrorCode + { + None = Tizen.Internals.Errors.ErrorCode.None, /**< Successful */ + OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory, /**< Out of Memory */ + IoError = Tizen.Internals.Errors.ErrorCode.IoError, /**< I/O error */ + InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter, /**< Invalid parameter */ + NetworkDown = Tizen.Internals.Errors.ErrorCode.Networkdown, /**< Network down(Out of network) */ + PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied, /**< Permission denied */ + InvalidState = ErrorTts | 0x01, /**< Invalid state */ + InvalidVoice = ErrorTts | 0x02, /**< Invalid voice */ + OperationFailed = ErrorTts | 0x04, /**< Operation failed */ + NotSupportedFeature = ErrorTts | 0x06, /**< Not supported feature of current engine */ + NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported, /**< Device or resource busy */ + }; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error InitializeCb(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error DeinitializeCb(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error ForEachSupportedVoicesCb(SupportedVoice cb, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error IsValidVoiceCb(string language, int type, out bool isValid); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error SetPitchCb(int pitch); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error LoadVoiceCb(string langauge, int type); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error UnloadVoiceCb(string language, int type); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error CheckAppAgreedCb(string appid, out bool isAgreed); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool NeedAppCredentialCb(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error StartSynthesisCb(IntPtr language, int type, IntPtr text, int speed, IntPtr appid, IntPtr credential, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error CancelSynthesisCb(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error GetInfoCb(out IntPtr engineUuid, out IntPtr engineName, out IntPtr engineSetting, out int useNetwork); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error PrivateDataSetCb(string key, string data); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Error PrivateDataRequestedCb(string key, out string data); + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + internal struct RequestCallbackStruct + { + internal int version; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal GetInfoCb getInfo; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal InitializeCb initialize; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal DeinitializeCb deinitialize; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal ForEachSupportedVoicesCb supportedVoice; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal IsValidVoiceCb validVoice; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal SetPitchCb pitch; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal LoadVoiceCb loadVoice; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal UnloadVoiceCb unloadVoice; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal StartSynthesisCb startSynthesis; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal CancelSynthesisCb cancelSynthesis; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal CheckAppAgreedCb checkAppAgreed; + [MarshalAs(UnmanagedType.FunctionPtr)] + internal NeedAppCredentialCb needAppCredential; + }; + + internal sealed class CallbackStructGCHandle : IDisposable + { + internal RequestCallbackStruct CallbackStruct; + internal GCHandle CallbackStructHandle; + public CallbackStructGCHandle() + { + CallbackStruct = new RequestCallbackStruct(); + CallbackStructHandle = GCHandle.Alloc(CallbackStruct); + } + + #region IDisposable Support + private bool disposedValue = false; + + void Dispose(bool disposing) + { + Tizen.Log.Info(LogTag, "In Dispose"); + if (!disposedValue) + { + if (disposing) + { + Tizen.Log.Info(LogTag, "In Dispose free called"); + CallbackStructHandle.Free(); + } + + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(true); + } + #endregion + } + + [DllImport(Libraries.TtsEngine, EntryPoint = "ttse_main")] + internal static extern Error TTSEMain(int argc, string[] argv, IntPtr callback); + + [DllImport(Libraries.TtsEngine, EntryPoint = "ttse_get_speed_range")] + internal static extern Error TTSEGetSpeedRange(out int min, out int normal, out int max); + + [DllImport(Libraries.TtsEngine, EntryPoint = "ttse_get_pitch_range")] + internal static extern Error TTSEGetPitchRange(out int min, out int normal, out int max); + + [DllImport(Libraries.TtsEngine, EntryPoint = "ttse_send_result")] + internal static extern Error TTSESendResult(ResultEvent resultEvent, IntPtr data, int dataSize, AudioType audioType, int rate, IntPtr userData); + + [DllImport(Libraries.TtsEngine, EntryPoint = "ttse_send_error")] + internal static extern Error TTSESendError(Error error, string msg); + + [DllImport(Libraries.TtsEngine, EntryPoint = "ttse_set_private_data_set_cb")] + internal static extern Error TTSESetPrivateDataSetCb(PrivateDataSetCb callbackFunc); + + [DllImport(Libraries.TtsEngine, EntryPoint = "ttse_set_private_data_requested_cb")] + internal static extern Error TTSESetPrivateDataRequestedCb(PrivateDataRequestedCb callbackFunc); + } +} diff --git a/src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.csproj b/src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.csproj new file mode 100755 index 0000000..38f8346 --- /dev/null +++ b/src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.csproj @@ -0,0 +1,16 @@ + + + + netstandard1.3 + True + True + Tizen.Uix.TtsEngine.snk + true + True + + + + + + + \ No newline at end of file diff --git a/src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.snk b/src/Tizen.Uix.TtsEngine/Tizen.Uix.TtsEngine.snk new file mode 100755 index 0000000000000000000000000000000000000000..fc12d26f0425a492808b21d0ccef12e051b99570 GIT binary patch literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50097fsI@Ef_F62h?AB1zbWnYT|I_*7LnMG9 zEkh|9ju0PeuoE3pn*woeAG=Q`Gc9&3Y8ed&p@f9lbX)2-74M}a^_9zOhBfDz%yIM3i~f+7<0Pw}-E0jN~!!@C9m7Wt62@bJfF^rvKqe4d;NTE4Z$=d2mX0i; zjD;RWn|4cig1iOmjr1~k$^cm3M!}_DY0A~EwAciLOKkX2+G=URh}bWA=F0&^qm=bC zfqWjS@Ujk6THAKRnMbXHCdGN^Y?07N18IYgo?FFrPnDK8@7eKlqDC`tp=JvTE$_Yh i + /// Enumeration for audio type. + /// + public enum AudioType + { + /// + /// Signed 16-bit audio type + /// + RawS16 = 0, + /// + /// Unsigned 8-bit audio type + /// + RawU8, + /// + /// Maximum Value + /// + Max + }; + + /// + /// Enumeration for result. + /// + public enum ResultEvent + { + /// + /// Event when the voice synthesis is failed + /// + Fail = -1, + /// + /// Event when the sound data is first data by callback function + /// + Start = 1, + /// + /// Event when the next sound data exist, not first and not last + /// + Continue = 2, + /// + /// Event when the sound data is last data or sound data is only one result + /// + Finish = 3 + }; + + /// + /// Enumeration for Voice Type + /// + public enum VoiceType + { + /// + /// male voice type. + /// + Male = 1, + /// + /// female voice type. + /// + Female = 2, + /// + /// child's voice type. + /// + Child = 3 + } + + /// + /// Enum for Error values that can occur + /// + public enum Error + { + /// + /// Successful, No error + /// + None = ErrorCode.None, + /// + /// Out of Memory + /// + OutOfMemory = ErrorCode.OutOfMemory, + /// + /// I/O error + /// + IoError = ErrorCode.IoError, + /// + /// Invalid parameter + /// + InvalidParameter = ErrorCode.InvalidParameter, + /// + /// Network down(Out of network) + /// + NetworkDown = ErrorCode.NetworkDown, + /// + /// Invalid state + /// + InvalidState = ErrorCode.InvalidState, + /// + /// Invalid voice + /// + InvalidVoice = ErrorCode.InvalidVoice, + /// + /// Operation failed + /// + OperationFailed = ErrorCode.OperationFailed, + /// + /// Not supported feature of current engine + /// + NotSupportedFeature = ErrorCode.NotSupportedFeature, + /// + /// NOT supported + /// + NotSupported = ErrorCode.NotSupported, + /// + /// Permission denied + /// + PermissionDenied = ErrorCode.PermissionDenied + }; + + /// + /// This Class represents the Tts Engine which has to be inherited to make the engine. + /// + public abstract class Engine + { + private CallbackStructGCHandle _callbackStructGCHandle = new CallbackStructGCHandle(); + private PrivateDataSetCb _privateDataSetCb; + private Action _privateDatacallback; + private PrivateDataRequestedCb _privateDataRequestedCb; + private OutAction _privateDataRequestedCallback; + private static Engine _engine; + private IntPtr _structIntPtrHandle; + + /// + /// An Action with 2 Input Parameter returning a Error + /// + /// Generic Type for Parameter 1 + /// The Input Parameter 1 + /// The Input Parameter 2 + /// Error Value + public delegate Error Action(T a, T b); + + /// + /// An Action with 2 Out Parameter returning a Error + /// + /// Generic Type for Parameter 1 + /// The Input Parameter 1 + /// The Input Parameter 2 + /// Error Value + public delegate Error OutAction(T a, out T b); + + /// + /// Called when TTS engine informs the engine service user about whole supported language and voice type list. + /// This callback function is implemented by the engine service user.Therefore, the engine developer does NOT have to implement this callback function. + /// + /// + /// This callback function is called by ForEachSupportedVoices() to inform the whole supported voice list. userData must be transferred from ForEachSupportedVoices(). + /// + /// The language is specified as an ISO 3166 alpha-2 two-letter country code followed by ISO 639-1 for the two-letter language code. + /// For example, "ko_KR" for Korean, "en_US" for American English + /// The voice type + /// The user data passed from ForEachSupportedVoices() + /// true to continue with the next iteration of the loop false to break out of the loop + /// ForEachSupportedVoices() will invoke this callback function. + public delegate bool SupportedVoice(string language, VoiceType type, IntPtr userData); + + /// + /// Called when the engine service user starts to synthesize a voice, asynchronously. + /// + /// + /// In this callback function, TTS engine must transfer the synthesized result to the engine service user using SendResult(). + /// Also, if TTS engine needs the application's credential, it can set the credential granted to the application. + /// + /// The language is specified as an ISO 3166 alpha-2 two-letter country code followed by ISO 639-1 for the two-letter language code. + /// For example, "ko_KR" for Korean, "en_US" for American English + /// The voice type + /// Texts + /// The speed of speaking + /// The Application ID + /// The credential granted to the application + /// The user data which must be passed to SendResult() function + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// 3. InvalidParameter + /// 4. InvalidVoice + /// 5. OperationFailed + /// 6. NetworkDown + /// 7. PermissionDenied + /// + /// This function invokes SendResult() + public abstract Error StartSynthesis(string language, int type, string text, int speed, string appid, string credential, IntPtr userData); + + /// + /// Called when the engine service user requests the basic information of TTS engine. + /// + /// + /// The allocated engineUuid, engineName, and engineSetting will be released internally. + /// In order to upload the engine at Tizen Appstore, both a service app and a ui app are necessary. + /// Therefore, engineSetting must be transferred to the engine service user. + /// + /// UUID of engine + /// Name of engine + /// The engine setting application(ui app)'s app ID + /// The status for using network + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// + public abstract Error GetInformation(out string engineUuid, out string engineName, out string engineSetting, out bool useNetwork); + + /// + /// Called when the engine service user initializes TTS engine. + /// + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// 3. NotSupportedFeature + /// 4. PermissionDenied + /// + public abstract Error Initialize(); + + /// + /// Called when the engine service user deinitializes TTS engine. + /// + /// + /// NOTE that the engine may be terminated automatically. When this callback function is invoked, the release of resources is necessary. + /// + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// + public abstract Error Deinitialize(); + + /// + /// Called when the engine service user gets the whole supported voice list. + /// + /// + /// In this function, the engine service user's callback function 'SupportedVoice()' is invoked repeatedly for getting all supported voices, + /// and userData must be transferred to 'SupportedVoice()'. If 'SupportedVoice()' returns false, it should be stopped to call 'SupportedVoice()'. + /// The callback function + /// The user data which must be passed to SupportedVoice() + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. OperationFailed + /// + /// This callback function invokes SupportedVoice() repeatedly for getting all supported voices. + public abstract Error ForEachSupportedVoices(SupportedVoice callback, IntPtr userData); + + /// + /// Called when the engine service user checks whether the voice is valid or not in TTS engine. + /// + /// The language is specified as an ISO 3166 alpha-2 two-letter country code followed by ISO 639-1 for the two-letter language code. + /// For example, "ko_KR" for Korean, "en_US" for American English + /// The voice type + /// A variable for checking whether the corresponding voice is valid or not. true to be valid, false to be invalid + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidParameter + /// + public abstract Error IsValidVoice(string language, int type, out bool isValid); + + /// + /// Called when the engine service user sets the default pitch of TTS engine. + /// + /// The default pitch + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// 3. OperationFailed + /// 4. InvalidParameter + /// + public abstract Error SetPitch(int pitch); + + /// + /// Called when the engine service user requests to load the corresponding voice type for the first time. + /// + /// The language is specified as an ISO 3166 alpha-2 two-letter country code followed by ISO 639-1 for the two-letter language code. + /// For example, "ko_KR" for Korean, "en_US" for American English + /// The voice type + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// 3. OperationFailed + /// 4. InvalidVoice + /// 5. InvalidParameter + /// 6. OutOfMemory + /// + public abstract Error LoadVoice(string language, int type); + + /// + /// Called when the engine service user requests to unload the corresponding voice type or to stop using voice. + /// + /// The language is specified as an ISO 3166 alpha-2 two-letter country code followed by ISO 639-1 for the two-letter language code. + /// For example, "ko_KR" for Korean, "en_US" for American English + /// The voice type + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// 3. OperationFailed + /// 4. InvalidVoice + /// 5. InvalidParameter + /// + public abstract Error UnloadVoice(string language, int type); + + /// + /// Called when the engine service user requests for TTS engine to check whether the application agreed the usage of TTS engine. + /// This callback function is called when the engine service user requests for TTS engine to check the application's agreement about using the engine. + /// According to the need, the engine developer can provide some user interfaces to check the agreement. + /// + /// + /// If the TTS engine developer wants not to check the agreement, the developer has need to return proper values as isAgreed in accordance with the intention. + /// true if the developer regards that every application agreed the usage of the engine, false if the developer regards that every application disagreed. + /// NOTE that, however, there may be any legal issue unless the developer checks the agreement. + /// Therefore, we suggest that the engine developers should provide a function to check the agreement. + /// + /// The Application ID + /// A variable for checking whether the application agreed to use TTS engine or not. true to agree, false to disagree + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// 3. NotSupportedFeature + /// 4. InvalidParameter + /// + public abstract Error CheckAppAgreed(string appid, out bool isAgreed); + + /// + /// Called when the engine service user checks whether TTS engine needs the application's credential. + /// + /// true if TTS engine needs the application's credential, otherwise false + public abstract bool NeedAppCredential(); + + /// + /// Called when the engine service user cancels to synthesize a voice. + /// + /// + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidState + /// + /// + /// StartSynthesis should be performed + /// + public abstract Error CancelSynthesis(); + + /// + /// Public Constructor + /// + public Engine() + { + _engine = this; + } + + /// + /// Main function for Text-To-Speech (TTS) engine. + /// This function is the main function for operating TTS engine. + /// + /// + /// ServiceAppMain should be used for working the engine after this function. + /// + /// The argument count(original) + /// The argument(original) + /// Thrown in case of Invalid Parameter + /// Thrown in case of Not supported + /// thrown in case of Operation failure + public void EngineMain(int argc, string[] argv) + { + _callbackStructGCHandle.CallbackStruct.version = 1; + _callbackStructGCHandle.CallbackStruct.getInfo = _getInfoCb; + _callbackStructGCHandle.CallbackStruct.initialize = Initialize; + _callbackStructGCHandle.CallbackStruct.deinitialize = _deinitializeCb; + _callbackStructGCHandle.CallbackStruct.supportedVoice = ForEachSupportedVoices; + _callbackStructGCHandle.CallbackStruct.validVoice = IsValidVoice; + _callbackStructGCHandle.CallbackStruct.pitch = SetPitch; + _callbackStructGCHandle.CallbackStruct.loadVoice = LoadVoice; + _callbackStructGCHandle.CallbackStruct.unloadVoice = UnloadVoice; + _callbackStructGCHandle.CallbackStruct.startSynthesis = _startSynthesisCb; + _callbackStructGCHandle.CallbackStruct.cancelSynthesis = CancelSynthesis; + _callbackStructGCHandle.CallbackStruct.checkAppAgreed = CheckAppAgreed; + _callbackStructGCHandle.CallbackStruct.needAppCredential = NeedAppCredential; + _structIntPtrHandle = Marshal.AllocHGlobal(Marshal.SizeOf(_callbackStructGCHandle.CallbackStruct)); + Marshal.StructureToPtr(_callbackStructGCHandle.CallbackStruct, _structIntPtrHandle, false); + Error error = TTSEMain(argc, argv, _structIntPtrHandle); + if (error != Error.None) + { + Log.Error(LogTag, "TTSEMain Failed with error " + error); + throw ExceptionFactory.CreateException((ErrorCode)error); + } + + Log.Info(LogTag, "After TTSEMain"); + } + + /// + /// Gets the speed range from Tizen platform + /// + /// + /// This API is used when TTS engine wants to get the speed range from Tizen platform + /// + /// The minimum speed value + /// The normal speed value + /// The maximum speed value + /// Thrown in case of Not supported + /// thrown in case of Operation failure + public void GetSpeedRange(out int min, out int normal, out int max) + { + Error error = TTSEGetSpeedRange(out min, out normal, out max); + if (error != Error.None) + { + Log.Error(LogTag, "TTSEGetSpeedRange Failed with error " + error); + throw ExceptionFactory.CreateException((ErrorCode)error); + } + + } + + /// + /// Gets the pitch range from Tizen platform. + /// + /// + /// This API is used when TTS engine wants to get the pitch range from Tizen platform. + /// + /// The minimum pitch value + /// The normal pitch value + /// The maximum pitch value + /// Thrown in case of Not supported + /// thrown in case of Operation failure + public void GetPitchRange(out int min, out int normal, out int max) + { + Error error = TTSEGetPitchRange(out min, out normal, out max); + if (error != Error.None) + { + Log.Error(LogTag, "TTSEGetPitchRange Failed with error " + error); + throw ExceptionFactory.CreateException((ErrorCode)error); + } + + } + + /// + /// Sends the synthesized result to the engine service user. + /// + /// + /// This API is used in StartSynthesis(), when TTS engine sends the synthesized result to the engine service user. + /// The synthesized result must be transferred to the engine service user through this function. + /// + /// The result event + /// Result data + /// Result data size + /// The audio type + /// The sample rate + /// Thrown in case of Invalid Parameter + /// Thrown in case of Not supported + /// thrown in case of Operation failure + /// + /// EngineMain function should be invoked before this function is called. StartSynthesis() will invoke this function. + /// + public void SendResult(ResultEvent resultEvent, IntPtr data, int dataSize, AudioType audioType, int rate) + { + Error error = TTSESendResult(resultEvent, data, dataSize, audioType, rate, IntPtr.Zero); + if (error != Error.None) + { + Log.Error(LogTag, "TTSESendResult Failed with error " + error); + throw ExceptionFactory.CreateException((ErrorCode)error); + } + } + + /// + /// Sends the error to the engine service user. + /// + /// The error reason + /// The error message + /// + /// EngineMain function should be invoked before this function is called. + /// + public void SendError(Error error, string msg) + { + Error err = TTSESendError(error, msg); + if (err != Error.None) + { + Log.Error(LogTag, "SendError Failed with error " + err); + throw ExceptionFactory.CreateException((ErrorCode)error); + } + + } + + /// + /// Sets a callback function for setting the private data. + /// + /// + /// Called when the engine service user gets the private data from Tts engine. + /// In Parameters: + /// a = Key -- The key field of private data + /// b = data -- The data field of private data + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidParameter + /// 3. OperationFailed + /// 4. NotSupported + /// + /// Thrown in case of Invalid Parameter + /// Thrown in case of Permission denied + /// Thrown in case of Not supported + /// thrown in case of Operation failure + /// + /// Main function should be invoked before this function is called. + /// + public void SetPrivateDataSetDelegate(Action callback) + { + _privateDatacallback = callback; + _privateDataSetCb = (string key, string data) => + { + return _privateDatacallback.Invoke(key, data); + }; + Error error = TTSESetPrivateDataSetCb(_privateDataSetCb); + if (error != Error.None) + { + Log.Error(LogTag, "SetPrivateDataSetDelegate Failed with error " + error); + throw ExceptionFactory.CreateException((ErrorCode)error); + } + + } + + /// + /// Sets a callback function for setting the private data. + /// + /// callback function + /// Called when TTS engine receives the private data from the engine service user. + /// This callback function is called when the engine service user sends the private data to TTS engine. + /// Out Parameters: + /// a = Key -- The key field of private data + /// b = data -- The data field of private data + /// Following Error Codes can be returned + /// 1. None + /// 2. InvalidParameter + /// 3. OperationFailed + /// 4. NotSupported + /// + /// Thrown in case of Invalid Parameter + /// Thrown in case of Permission denied + /// Thrown in case of Not supported + /// thrown in case of Operation failure + /// + /// Main function should be invoked before this function is called. + /// + public void SetPrivateDataRequestedDelegate(OutAction callback) + { + _privateDataRequestedCallback = callback; + _privateDataRequestedCb = (string key, out string data) => + { + return _privateDataRequestedCallback.Invoke(key, out data); + }; + Error error = TTSESetPrivateDataRequestedCb(_privateDataRequestedCb); + if (error != Error.None) + { + Log.Error(LogTag, "SetPrivateDataRequestedDelegate Failed with error " + error); + throw ExceptionFactory.CreateException((ErrorCode)error); + } + + } + private StartSynthesisCb _startSynthesisCb = (IntPtr language, int type, IntPtr text, int speed, IntPtr appid, IntPtr credential, IntPtr userData) => + { + string lan = null; + string txt = null; + string apid = null; + string cre = null; + if (language != null) + lan = Marshal.PtrToStringAnsi(language); + if (text != null) + txt = Marshal.PtrToStringAnsi(text); + if (appid != null) + apid = Marshal.PtrToStringAnsi(appid); + if (credential != null) + cre = Marshal.PtrToStringAnsi(credential); + return _engine.StartSynthesis(lan, type, txt, speed, apid, cre, IntPtr.Zero); + }; + + private GetInfoCb _getInfoCb = (out IntPtr engineUuid, out IntPtr engineName, out IntPtr engineSetting, out int useNetwork) => + { + string uuid; + string name; + string setting; + bool network; + Error err = _engine.GetInformation(out uuid, out name, out setting, out network); + if (network == true) + { + useNetwork = 1; + } + else + { + useNetwork = 0; + } + engineUuid = Marshal.StringToHGlobalAnsi(uuid); + engineName = Marshal.StringToHGlobalAnsi(name); + engineSetting = Marshal.StringToHGlobalAnsi(setting); + return err; + }; + + private DeinitializeCb _deinitializeCb = () => + { + Marshal.FreeHGlobal(_engine._structIntPtrHandle); + return _engine.Deinitialize(); + }; + } +} \ No newline at end of file -- 2.7.4