2023年7月13日发(作者:)
c#调⽤开源espeak语⾳引擎实现中英⽂混读(原创)c#调⽤开源espeak语⾳引擎实现中英⽂混读需求实际.net项⽬中需要⽤到TTS(⽂本转语⾳技术),实现语⾳播服务,需要中英⽂混读⽅案c#引⽤espeak_中的函数[DllImport("espeak_", CharSet = , CallingConvention = )]static extern Error espeak_SetVoiceByName([MarshalAs(8Str)] string name);[DllImport("espeak_", CharSet = , CallingConvention = )]static extern Error espeak_SetParameter(Parameter parameter, int value, ParameterType type);[DllImport("espeak_", CharSet = , CallingConvention = )]static extern Error espeak_Synchronize();[DllImport("espeak_", CharSet = , CallingConvention = )]static extern Error espeak_Terminate();[DllImport("espeak_", CharSet = , CallingConvention = )]static extern IntPtr espeak_GetCurrentVoice();[DllImport("espeak_", CharSet = , CallingConvention = )]static extern Error espeak_Synth([MarshalAs(8Str)] string text, int size, uint startPosition = 0, PositionType positionType = ter, uint endPosition = 0, SpeechFlags flags = tf8, UIntPtr uniqueIdentifier = default(UIntPtr), IntPtr userData = default(IntPtr));[DllImport("espeak_", CharSet = , CallingConvention = )]static extern int espeak_Initialize(AudioOutput output, int bufferLength, IntPtr path, int options);[DllImport("espeak_", CharSet = , CallingConvention = )]static extern Error espeak_Cancel();[DllImport("espeak_", CharSet = , CallingConvention = )]static extern void espeak_SetSynthCallback(allback callback);c#对espeak_函数进⾏封装 public static void Initialize(string path) { IntPtr ptr = ToHGlobalAnsi(path); var result = espeak_Initialize(onous,100, ptr, 0); //
释放 ptr
的内存 lobal(ptr); if (result == (int)_INTERNAL_ERROR) { throw new Exception(("Could not initialize ESpeak. Maybe there is no espeak data at {0}?", path)); } espeak_SetSynthCallback(); Initialized = true; } public static bool SetRate(int rate) { if (rate < 80 && rate > 450) if (rate < 80 && rate > 450) { throw new Exception("The rate must be between 80 and 450."); } var result = espeak_SetParameter(, rate, te); return CheckResult(result); } static bool CheckResult(Error result) { if (result == _OK) { return true; } else if (result == _BUFFER_FULL) { return false; } else if (result == _INTERNAL_ERROR) { throw new Exception("Internal error in ESpeak."); } else { return false; } } public static bool Speak(string text) { var result = espeak_Synth(text, * DefaultCharSize); return CheckResult(result); } public static bool SpeakSSML(string text) { var result = espeak_Synth(text, * DefaultCharSize, 0, ter, 0, tf8 | ); return CheckResult(result); } public static bool Stop() { var result = espeak_Cancel(); return CheckResult(result); } public static bool Espeak_Synchronize() { var result = espeak_Synchronize(); return CheckResult(result); } public static bool Espeak_Terminate() { var result = espeak_Terminate(); return CheckResult(result); } public static bool SetVoiceByName(string name) { var result = espeak_SetVoiceByName(name); return CheckResult(result); } public static Voice GetCurrentVoice() { IntPtr espeakVoicePtr = espeak_GetCurrentVoice(); ESpeakVoice espeakVoice = (ESpeakVoice)tructure(espeakVoicePtr, typeof(ESpeakVoice)); if ((default(ESpeakVoice))) { throw new Exception("eSpeak returned an empty voice object. Did you call one of the ce*() functions?"); } return new Voice() { Name = , Languages = ing(1), Priority = (int)ges[0], Identifier = fier, }; }c#控制台程序调⽤发声 class Program { static void Main(string[] args) { lize("." ); ceByName("zh+m10"); ("Helloworld!"); //
等待发⾳结束 _Synchronize(); //_Terminate(); //(5000); (); ("你好。Hello world"); _Synchronize(); _Terminate(); (); }
}源码Github地址因该模块开发主要得益于开源,故将源码开源:
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689249922a225759.html
评论列表(0条)