using System; using System.Collections.Generic; using System.Reflection; using System.Diagnostics; namespace SeikaCenter { class Cevio : ProductControlBase { CevioProxy cevioTalker = null; string Version { get; } AppDomain appDomain = null; private bool disposed = false; protected override void Dispose(bool disposing) { if (this.disposed) return; if (disposing) { //if (appDomain != null) //{ // try // { // AppDomain.Unload(appDomain); // } // finally // { // appDomain = null; // } //} } this.disposed = true; base.Dispose(disposing); } public Cevio(string databaseFilename) { avatorParams = new Dictionary(); try { appDomain = AppDomain.CreateDomain("SeikaCenterCevioPlugin"); Type t2 = typeof(CevioProxy); cevioTalker = (CevioProxy)appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, t2.FullName); dynamic talkers = cevioTalker.AvailableCasts; if (talkers.Length !=0) { for (int i = 0; i < talkers.Length; i++) { avatorParam item = new avatorParam(); cevioTalker.Cast = talkers[i]; item.avatorIndex = i; item.avatorName = talkers[i]; item.voiceEffects_default = new Dictionary { {VoiceEffect.volume, new EffectValueInfo( (decimal)cevioTalker.Volume, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.speed, new EffectValueInfo( (decimal)cevioTalker.Speed, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.pitch, new EffectValueInfo( (decimal)cevioTalker.Tone, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.alpha, new EffectValueInfo( (decimal)cevioTalker.Alpha, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.intonation, new EffectValueInfo( (decimal)cevioTalker.ToneScale, 0.0m, 100.0m, 1.00m)} }; item.voiceEffects = new Dictionary { {VoiceEffect.volume, new EffectValueInfo( (decimal)cevioTalker.Volume, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.speed, new EffectValueInfo( (decimal)cevioTalker.Speed, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.pitch, new EffectValueInfo( (decimal)cevioTalker.Tone, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.alpha, new EffectValueInfo( (decimal)cevioTalker.Alpha, 0.0m, 100.0m, 1.00m)}, {VoiceEffect.intonation, new EffectValueInfo( (decimal)cevioTalker.ToneScale, 0.0m, 100.0m, 1.00m)} }; item.voiceEmotions_default = new Dictionary(); item.voiceEmotions = new Dictionary(); Dictionary emoparams = cevioTalker.Components; if (0 < emoparams.Count) { foreach (KeyValuePair emo in emoparams) { item.voiceEmotions_default.Add(emo.Key, new EffectValueInfo((decimal)(emo.Value), 0.00m, 100.0m, 1.00m)); item.voiceEmotions.Add(emo.Key, new EffectValueInfo((decimal)(emo.Value), 0.00m, 100.0m, 1.00m)); } } avatorParams.Add(i, item); } aliveInstance = true; } } catch(Exception e) { Console.WriteLine("{0},{1}", e.Message,e.StackTrace); aliveInstance = false; } } public override bool SelectAvator(int avatorIndex) { if ((avatorIndex < 0) || (avatorIndex > avatorParams.Count)) return false; selectedAvator = avatorIndex; cevioTalker.Cast = avatorParams[avatorIndex].avatorName; return true; } public override double Play(bool async = false) { Stopwatch sw = new Stopwatch(); ApplyEffectParameters(); ApplyEmotionParameters(); sw.Start(); try { cevioTalker.Speak(TalkText, async); } catch (Exception) { // } sw.Stop(); return sw.ElapsedMilliseconds; } public override double SaveFromProduct(string saveFilename) { ApplyEffectParameters(); ApplyEmotionParameters(); try { cevioTalker.Save(TalkText, saveFilename); } catch (Exception) { // } return 0.0; } private void ApplyEmotionParameters() { if (avatorParams.Count != 0) { foreach (KeyValuePair item in avatorParams[selectedAvator].voiceEmotions) { cevioTalker.SetComponent(item.Key, (uint)(item.Value.value)); } } } private void ApplyEffectParameters() { if (avatorParams.Count != 0) { if (!KeepEmotionSetting) { foreach (KeyValuePair item in avatorParams[selectedAvator].voiceEffects_default) { switch (item.Key) { case VoiceEffect.volume: cevioTalker.Volume = (uint)(item.Value.value); break; case VoiceEffect.speed: cevioTalker.Speed = (uint)(item.Value.value); break; case VoiceEffect.pitch: cevioTalker.Tone = (uint)(item.Value.value); break; case VoiceEffect.alpha: cevioTalker.Alpha = (uint)(item.Value.value); break; case VoiceEffect.intonation: cevioTalker.ToneScale = (uint)(item.Value.value); break; } } } foreach (KeyValuePair item in avatorParams[selectedAvator].voiceEffects) { switch (item.Key) { case VoiceEffect.volume: cevioTalker.Volume = (uint)(item.Value.value); break; case VoiceEffect.speed: cevioTalker.Speed = (uint)(item.Value.value); break; case VoiceEffect.pitch: cevioTalker.Tone = (uint)(item.Value.value); break; case VoiceEffect.alpha: cevioTalker.Alpha = (uint)(item.Value.value); break; case VoiceEffect.intonation: cevioTalker.ToneScale = (uint)(item.Value.value); break; } } } } } // このクラス内で遅延バインドさせる public class CevioProxy : MarshalByRefObject { dynamic cevioTalker; double timeout = 3 * 60 * 1000; public CevioProxy() { Type t2 = Type.GetTypeFromProgID("CeVIO.Talk.RemoteService.Talker"); // 遅延バインドで使用します cevioTalker = Activator.CreateInstance(t2); } public override object InitializeLifetimeService() { return null; } public string[] AvailableCasts { get { List x = new List(); dynamic ac = cevioTalker.AvailableCasts; for (int i = 0; i < ac.Length; i++) { x.Add(ac[i]); } return x.ToArray(); } } public string Cast { get { return cevioTalker.Cast; } set { cevioTalker.Cast = value; } } public uint Volume { get { return cevioTalker.Volume; } set { cevioTalker.Volume = value; } } public uint Speed { get { return cevioTalker.Speed; } set { cevioTalker.Speed = value; } } public uint Tone { get { return cevioTalker.Tone; } set { cevioTalker.Tone = value; } } public uint Alpha { get { return cevioTalker.Alpha; } set { cevioTalker.Alpha = value; } } public uint ToneScale { get { return cevioTalker.ToneScale; } set { cevioTalker.ToneScale = value; } } public Dictionary Components { get { var emo = cevioTalker.Components; Dictionary param = new Dictionary(); for (int i = 0; i < emo.Length; i++) { param.Add(emo[i].Name, emo[i].Value); } return param; } } public void SetComponent(string emoName, uint emoValue) { cevioTalker.Components[emoName].Value = emoValue; } public string GetComponent(string emoName) { return cevioTalker.Components[emoName].Value; } public void Speak(string text, bool async = false) { dynamic status = cevioTalker.Speak(text); //SpeakingState status = cevioTalker.Speak(TalkText); if (!async) { status.Wait(timeout); } cevioTalker.Stop(); } public void Save(string text, string WavFilePath) { cevioTalker.OutputWaveToFile(text, WavFilePath); } } }