using System; using System.Collections.Generic; using System.Linq; using System.Text; #if (!__MonoCS__) using System.Speech.Synthesis; #endif using System.Xml; using System.IO; using System.Threading; using System.Runtime.InteropServices; namespace TtsRelay { public class CerevoiceDLL { // [DllImport("msvcrt.dll", EntryPoint="puts")] [DllImport("CerevoiceDLL.dll")] public static extern bool CEREVOICE_DLL_Init([MarshalAs(UnmanagedType.LPStr)] string visemeMapping); [DllImport("CerevoiceDLL.dll")] public static extern void CEREVOICE_DLL_SetVisemeMapping([MarshalAs(UnmanagedType.LPStr)] string visemeMapping); [DllImport("CerevoiceDLL.dll")] public static extern bool CEREVOICE_DLL_GenerateAudio([MarshalAs(UnmanagedType.LPStr)] string message, [MarshalAs(UnmanagedType.LPStr)] string outputFilename, [MarshalAs(UnmanagedType.LPStr)] string messageOutputFileName, [MarshalAs(UnmanagedType.LPStr)] string voice, [Out] StringBuilder xmlReplyReturn, ref GenerateAudioReplyInterop generateAudioReplyInterop); /* [DllImport("kernel32.dll", SetLastError=true)] [PreserveSig] public static extern uint GetModuleFileName ( [In] IntPtr hModule, [Out] StringBuilder lpFilename, [In] [MarshalAs(UnmanagedType.U4)] int nSize ); StringBuilder fileName = new StringBuilder(255); DllFuntions.GetModuleFileName(IntPtr.Zero, fileName, fileName.Capacity); Console.WriteLine(fileName); */ } public class CerevoiceRelay : ITtsRelay { public bool Init(string visemeMapping) { CerevoiceDLL.CEREVOICE_DLL_Init(visemeMapping); return true; } public string [] GetVoices() { //return new string[] { "katherine", "star" }; // We enumerate the voices in the folder based on filename. string cerevoiceDir = @"../../../../../data/cereproc/voices"; List list = new List(); Console.WriteLine("cerevoiceDir: {0}", cerevoiceDir); DirectoryInfo voicedi = new DirectoryInfo(cerevoiceDir); if (voicedi.Exists) { FileInfo[] voiceNames = voicedi.GetFiles("*.lic"); foreach (FileInfo fi in voiceNames) { string voiceName = fi.Name; voiceName = voiceName.Replace(".lic", ""); voiceName = "Cerevoice_" + voiceName; list.Add(voiceName); } } return list.ToArray(); } public void SetVisemeMapping(string visemeMapping) { CerevoiceDLL.CEREVOICE_DLL_SetVisemeMapping(visemeMapping); } /* received -- op: RemoteSpeechCmd args: * RemoteSpeechCmd speak brad 6 katherine * ../../data/cache/audio/utt_20111013_204717_brad_6.aiff * * Hello world. Testing Text to Speech process_message() - * speak brad 6 katherine * ../../data/cache/audio/utt_20111013_204717_brad_6.aiff * * Hello world. Testing Text to Speech ' * * cerevoice_tts::tts() - * * Hello world. Testing Text to Speech * '../../data/cache/audio/utt_20111013_204717_brad_6.wav' * 'd:\edwork\saso\data\cache\audio\utt_20111013_204717_brad_6.wav' * 'katherine' * * from cerevoicerelay * RemoteSpeechReply * brad 6 OK: * * * * * vrAgentBML brad sbm_test_bml_14 start * PlaySound "d:\edwork\saso\data\cache\audio\utt_20111013_204717_brad_6.wav" brad * * from ttsspeechrelay * RemoteSpeechReply * brad 9 OK: * unused 1 OK: * * * * */ public bool GenerateAudio(string message, string outputFileName, string messageOutputFileName, string voice, ref string xmlReplyReturn, ref GenerateAudioReply generateAudioReplyReturn) { StringBuilder reply = new StringBuilder(65536); // The voice names have "Festival_" prepended to identify them, but the real festival names don't have this string cerevoiceVoice = voice.Remove(0, "Cerevoice_".Length); GenerateAudioReplyInterop generateAudioReplyInterop = new GenerateAudioReplyInterop(); CerevoiceDLL.CEREVOICE_DLL_GenerateAudio(message, outputFileName, messageOutputFileName, cerevoiceVoice, reply, ref generateAudioReplyInterop); xmlReplyReturn = reply.ToString(); generateAudioReplyReturn.used = true; generateAudioReplyReturn.soundFile = Marshal.PtrToStringAnsi(generateAudioReplyInterop.soundFile); generateAudioReplyReturn.WordBreakList = new List>(); generateAudioReplyReturn.MarkList = new List>(); generateAudioReplyReturn.VisemeList = new List(); for (int i = 0; i < generateAudioReplyInterop.workBreakListNum; i++) { double wordBreakListStart = generateAudioReplyInterop.wordBreakListStart.ElementAt(i); double wordBreakListEnd = generateAudioReplyInterop.wordBreakListEnd.ElementAt(i); generateAudioReplyReturn.WordBreakList.Add(new KeyValuePairS(wordBreakListStart, wordBreakListEnd)); } for (int i = 0; i < generateAudioReplyInterop.markListNum; i++) { string markListName = Marshal.PtrToStringAnsi(generateAudioReplyInterop.markListName.ElementAt(i)); double markListTime = generateAudioReplyInterop.markListTime.ElementAt(i); generateAudioReplyReturn.MarkList.Add(new KeyValuePairS(markListName, markListTime)); } for (int i = 0; i < generateAudioReplyInterop.visemeListNum; i++) { string visemeListType = Marshal.PtrToStringAnsi(generateAudioReplyInterop.visemeListType.ElementAt(i)); double visemeListStart = generateAudioReplyInterop.visemeListStart.ElementAt(i); double visemeListArticulation = generateAudioReplyInterop.visemeListArticulation.ElementAt(i); generateAudioReplyReturn.VisemeList.Add(new GenerateAudioReplyViseme(visemeListType, visemeListStart, visemeListArticulation)); } return true; } } }