예제 #1
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Title = titleStr + " " + versionStr;

            try
            {
                // AssistantSeikaとの接続
                // シンプルな例は
                // https://hgotoh.jp/wiki/doku.php/documents/voiceroid/assistantseika/interface/wcf/wcf-004
                // を見てください。
                WcfClient = new WCFClient();

                if (WcfClient.AvatorList().Count == 0)
                {
                    throw new Exception("No Avators detected from AssistantSeika");
                }

                // 設定色々
                Config = new Configs(ref WcfClient);
            }
            catch (Exception e0)
            {
                MessageBox.Show("前提ソフトウエアであるAssistantSeikaを起動していないか、AssistantSeikaが音声合成製品を認識していない可能性があります。" + "\n" + e0.Message, "AssistantSeikaの状態");
                Application.Current.Shutdown();
                return;
            }

            try
            {
                // 古いバージョンの設定値のバージョンアップを試みる
                if (Properties.Settings.Default.UpgradeRequired)
                {
                    Properties.Settings.Default.Upgrade();
                    Properties.Settings.Default.UpgradeRequired = false;
                    //Properties.Settings.Default.Save();
                }
            }
            catch (Exception e0)
            {
                MessageBox.Show(e0.Message, "設定値読み込みの問題1");
                Application.Current.Shutdown();
                return;
            }

            try
            {
                // 設定値を取り込むよ!
                UserData = new UserDefData();
                if (Properties.Settings.Default.UserSettings != "")
                {
                    DataContractJsonSerializer uds = new DataContractJsonSerializer(typeof(UserDefData));
                    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Properties.Settings.Default.UserSettings));
                    UserData = (UserDefData)uds.ReadObject(ms);
                    ms.Close();
                }
            }
            catch (Exception e0)
            {
                MessageBox.Show(e0.Message, "設定値読み込みの問題2");
                Application.Current.Shutdown();
                return;
            }

            try
            {
                // 設定値を取り込むよ!
                UserData.QuietMessages = Config.MessageLoader();
            }
            catch (Exception e0)
            {
                MessageBox.Show(e0.Message, "設定値読み込みの問題3");
                Application.Current.Shutdown();
                return;
            }

            try
            {
                // 古い版のデータだったら補正
                // 設定値が取り込めない環境がある模様だ。対策する

                if (UserData is null)
                {
                    UserData = new UserDefData();
                }

                if (UserData.VoiceParams is null)
                {
                    UserData.VoiceParams        = new Dictionary <int, Dictionary <int, Dictionary <int, Dictionary <string, Dictionary <string, Dictionary <string, decimal> > > > > >();
                    UserData.SelectedCid        = new Dictionary <int, Dictionary <int, int> >();
                    UserData.SelectedCallMethod = new Dictionary <int, int>();

                    foreach (ListenInterface InterfaceIdx in Enum.GetValues(typeof(ListenInterface)))
                    {
                        UserData.VoiceParams[(int)InterfaceIdx]        = new Dictionary <int, Dictionary <int, Dictionary <string, Dictionary <string, Dictionary <string, decimal> > > > >();
                        UserData.SelectedCid[(int)InterfaceIdx]        = new Dictionary <int, int>();
                        UserData.SelectedCallMethod[(int)InterfaceIdx] = (int)Methods.sync;

                        foreach (BouyomiVoice BouIdx in Enum.GetValues(typeof(BouyomiVoice)))
                        {
                            UserData.VoiceParams[(int)InterfaceIdx][(int)BouIdx] = new Dictionary <int, Dictionary <string, Dictionary <string, Dictionary <string, decimal> > > >();
                            UserData.SelectedCid[(int)InterfaceIdx][(int)BouIdx] = Config.AvatorNames.First().Key;

                            foreach (int cid in Config.AvatorNames.Keys)
                            {
                                UserData.VoiceParams[(int)InterfaceIdx][(int)BouIdx][cid] = Config.AvatorParams(cid);
                            }
                        }
                    }
                }

                if (UserData.InterfaceSwitch is null)
                {
                    UserData.InterfaceSwitch = new Dictionary <int, bool>()
                    {
                        { 0, true },
                        { 1, true },
                        { 2, true },
                        { 3, false },
                        { 4, false }
                    };
                }

                if (UserData.RandomVoiceMethod is null)
                {
                    UserData.RandomVoiceMethod = new Dictionary <int, int>()
                    {
                        { 0, 0 },
                        { 1, 0 },
                        { 2, 0 },
                        { 3, 0 },
                        { 4, 0 }
                    };
                }

                if (UserData.QuietMessages is null)
                {
                    UserData.QuietMessages = new Dictionary <int, List <string> >();
                }

                if (UserData.AddSuffixStr is null)
                {
                    UserData.AddSuffix    = false;
                    UserData.AddSuffixStr = "(以下略";
                    UserData.TextLength   = 96;
                }

                if (UserData.ReplaceDefs is null)
                {
                    UserData.ReplaceDefs = new List <ReplaceDefinition>();
                    UserData.ReplaceDefs.Add(new ReplaceDefinition()
                    {
                        Apply = true, MatchingPattern = @"([^0-90-9])[88]{3,}", ReplaceText = @"$1パチパチパチ"
                    });
                    UserData.ReplaceDefs.Add(new ReplaceDefinition()
                    {
                        Apply = true, MatchingPattern = @"^[88]{3,}", ReplaceText = @"パチパチパチ"
                    });
                    UserData.ReplaceDefs.Add(new ReplaceDefinition()
                    {
                        Apply = true, MatchingPattern = @"([^a-zA-Za-zA-Z])[WwWw]{1,}", ReplaceText = @"$1わらわら"
                    });
                    UserData.ReplaceDefs.Add(new ReplaceDefinition()
                    {
                        Apply = true, MatchingPattern = @"^[WwWw]{2,}", ReplaceText = @"わらわら"
                    });
                    UserData.ReplaceDefs.Add(new ReplaceDefinition()
                    {
                        Apply = true, MatchingPattern = @"https*:\/\/[^\t  ]{1,}", ReplaceText = @"URL省略"
                    });
                }
            }
            catch (Exception e0)
            {
                MessageBox.Show(e0.Message, "設定値読み込みの問題4");
                Application.Current.Shutdown();
                return;
            }

            try
            {
                // 以前より話者が増えていた場合、その話者の音声パラメタを初期化

                foreach (ListenInterface InterfaceIdx in Enum.GetValues(typeof(ListenInterface)))
                {
                    foreach (BouyomiVoice BouIdx in Enum.GetValues(typeof(BouyomiVoice)))
                    {
                        foreach (int cid in Config.AvatorNames.Keys)
                        {
                            if (!UserData.VoiceParams[(int)InterfaceIdx][(int)BouIdx].ContainsKey(cid))
                            {
                                UserData.VoiceParams[(int)InterfaceIdx][(int)BouIdx][cid] = Config.AvatorParams(cid);
                            }
                        }
                    }
                }
            }
            catch (Exception e0)
            {
                MessageBox.Show(e0.Message, "設定値読み込みの問題5");
                Application.Current.Shutdown();
                return;
            }

            // サイレントメッセージ最大待ち時間
            if (UserData.QuietMessages.Count != 0)
            {
                QuietMessageKeyMax = UserData.QuietMessages.Max(c => c.Key);
                if (QuietMessageKeyMax > (2 * 24 * 60 * 60))
                {
                    QuietMessageKeyMax     = 2 * 24 * 60 * 60; // 2日間
                    UserData.QuietMessages = UserData.QuietMessages.Where(c => c.Key <= QuietMessageKeyMax).ToDictionary(c => c.Key, v => v.Value);
                }

                CheckBoxIsSilent.IsChecked = UserData.IsSilentAvator;
            }
            else
            {
                QuietMessageKeyMax         = 0;
                CheckBoxIsSilent.IsChecked = false;
                CheckBoxIsSilent.IsEnabled = false;
            }

            // バックグラウンドタスク用オブジェクト
            IpcTask   = new IpcTasks(ref Config, ref MessQueWrapper, ref WcfClient, ref UserData);
            SockTask  = new SocketTasks(ref Config, ref MessQueWrapper, ref WcfClient, ref UserData);
            SockTask2 = new SocketTasks(ref Config, ref MessQueWrapper, ref WcfClient, ref UserData);
            HttpTask  = new HttpTasks(ref Config, ref MessQueWrapper, ref WcfClient, ref UserData);
            HttpTask2 = new HttpTasks(ref Config, ref MessQueWrapper, ref WcfClient, ref UserData);

            // 非同期発声時のGUI操作用
            IpcTask.OnCallAsyncTalk   += TalkAsyncCall;
            SockTask.OnCallAsyncTalk  += TalkAsyncCall;
            SockTask2.OnCallAsyncTalk += TalkAsyncCall;
            HttpTask.OnCallAsyncTalk  += TalkAsyncCall;
            HttpTask2.OnCallAsyncTalk += TalkAsyncCall;

            // 話者設定 コンボボックス設定
            ComboBoxInterface.ItemsSource       = null;
            ComboBoxInterface.ItemsSource       = ConstClass.BouyomiInterface;
            ComboBoxInterface.DisplayMemberPath = "LabelData";
            ComboBoxInterface.SelectedValuePath = "ValueData";

            ComboBoxCallMethod.ItemsSource       = null;
            ComboBoxCallMethod.ItemsSource       = ConstClass.BouyomiCallMethod;
            ComboBoxCallMethod.DisplayMemberPath = "LabelData";
            ComboBoxCallMethod.SelectedValuePath = "ValueData";

            List <ComboBox> MapAvatorsComboBoxList = new List <ComboBox>()
            {
                ComboBoxMapVoice0,
                ComboBoxMapVoice1,
                ComboBoxMapVoice2,
                ComboBoxMapVoice3,
                ComboBoxMapVoice4,
                ComboBoxMapVoice5,
                ComboBoxMapVoice6,
                ComboBoxMapVoice7,
                ComboBoxMapVoice8
            };

            for (int idx = 0; idx < MapAvatorsComboBoxList.Count; idx++)
            {
                MapAvatorsComboBoxList[idx].ItemsSource = null;
                MapAvatorsComboBoxList[idx].ItemsSource = Config.AvatorNames;
                MapAvatorsComboBoxList[idx].IsEnabled   = true;
                MapAvatorsComboBoxList[idx].Tag         = idx;
            }

            ComboBoxRandomAssignVoice.ItemsSource   = null;
            ComboBoxRandomAssignVoice.ItemsSource   = ConstClass.RandomAssignMethod;
            ComboBoxRandomAssignVoice.SelectedIndex = UserData.RandomVoiceMethod[(int)ListenInterface.IPC1];

            ComboBoxInterface.SelectedIndex = (int)ListenInterface.IPC1;

            // 音声設定 コンボボックス設定
            ComboBoxEditInterface.ItemsSource       = null;
            ComboBoxEditInterface.DisplayMemberPath = "LabelData";
            ComboBoxEditInterface.SelectedValuePath = "ValueData";
            ComboBoxEditInterface.ItemsSource       = ConstClass.BouyomiInterface;

            ComboBoxEditBouyomiVoice.ItemsSource       = null;
            ComboBoxEditBouyomiVoice.DisplayMemberPath = "LabelData";
            ComboBoxEditBouyomiVoice.SelectedValuePath = "ValueData";
            ComboBoxEditBouyomiVoice.ItemsSource       = ConstClass.BouyomiVoiceName;

            ComboBoxEditInterface.SelectedIndex    = 0;
            ComboBoxEditBouyomiVoice.SelectedIndex = 0;

            // 置換設定 テキストボックス設定
            TextBoxTextLength.Text           = UserData.TextLength.ToString();
            EditParamsBefore.LimitTextLength = UserData.TextLength;
            CheckBoxAddSuffix.IsChecked      = EditParamsBefore.IsUseSuffixString = UserData.AddSuffix;
            TextBoxAddSuffixStr.Text         = EditParamsBefore.SuffixString = UserData.AddSuffixStr;
            Regexs = new ObservableCollection <ReplaceDefinition>(UserData.ReplaceDefs);
            DataGridRepDefs.DataContext = null;
            DataGridRepDefs.DataContext = Regexs;
            if (UserData.ReplaceDefs.Count != 0)
            {
                DataGridRepDefs.SelectedIndex = 0;
            }
            EditParamsBefore.CopyRegExs(ref Regexs);

            // 状態 受信インタフェース
            EllipseIpc.Tag     = 0;
            EllipseSocket.Tag  = 1;
            EllipseHTTP.Tag    = 2;
            EllipseSocket2.Tag = 3;
            EllipseHTTP2.Tag   = 4;

            try
            {
                // 読み上げバックグラウンドタスク起動
                LonelyAvatorNames   = Config.AvatorNames;
                LonelyCidList       = LonelyAvatorNames.Select(c => c.Key).ToList();
                LonelyCount         = 0;
                KickTalker          = new DispatcherTimer();
                KickTalker.Tick    += new EventHandler(KickTalker_Tick);
                KickTalker.Interval = new TimeSpan(0, 0, 1);
                ReEntry             = true;
                KickTalker.Start();

                // 受信インタフェース バックグラウンドタスク起動

                if ((UserData.InterfaceSwitch[(int)ListenInterface.IPC1] == true) && IpcTask.StartIpcTasks(Config.IPCChannelName))
                {
                    EllipseIpc.Fill = Brushes.LightGreen;
                    UserData.InterfaceSwitch[(int)ListenInterface.IPC1] = true;
                }
                else
                {
                    EllipseIpc.Fill = Brushes.Black;
                    UserData.InterfaceSwitch[(int)ListenInterface.IPC1] = false;
                }

                if (UserData.InterfaceSwitch[(int)ListenInterface.Socket1] == true)
                {
                    SockTask.StartSocketTasks(Config.SocketAddress, Config.SocketPortNum);
                    EllipseSocket.Fill = Brushes.LightGreen;
                    UserData.InterfaceSwitch[(int)ListenInterface.Socket1] = true;
                }
                else
                {
                    EllipseSocket.Fill = Brushes.Black;
                    UserData.InterfaceSwitch[(int)ListenInterface.Socket1] = false;
                }

                if (UserData.InterfaceSwitch[(int)ListenInterface.Http1] == true)
                {
                    HttpTask.StartHttpTasks(Config.HttpAddress, Config.HttpPortNum);
                    EllipseHTTP.Fill = Brushes.LightGreen;
                    UserData.InterfaceSwitch[(int)ListenInterface.Http1] = true;
                }
                else
                {
                    EllipseHTTP.Fill = Brushes.Black;
                    UserData.InterfaceSwitch[(int)ListenInterface.Http1] = false;
                }

                if (UserData.InterfaceSwitch[(int)ListenInterface.Socket2] == true)
                {
                    SockTask2.StartSocketTasks(Config.SocketAddress2, Config.SocketPortNum2);
                    EllipseSocket2.Fill = Brushes.LightGreen;
                    UserData.InterfaceSwitch[(int)ListenInterface.Socket2] = true;
                }
                else
                {
                    EllipseSocket2.Fill = Brushes.Black;
                    UserData.InterfaceSwitch[(int)ListenInterface.Socket2] = false;
                }

                if (UserData.InterfaceSwitch[(int)ListenInterface.Http2] == true)
                {
                    HttpTask2.StartHttpTasks(Config.HttpAddress2, Config.HttpPortNum2);
                    EllipseHTTP2.Fill = Brushes.LightGreen;
                    UserData.InterfaceSwitch[(int)ListenInterface.Http2] = true;
                }
                else
                {
                    EllipseHTTP2.Fill = Brushes.Black;
                    UserData.InterfaceSwitch[(int)ListenInterface.Http2] = false;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message, titleStr + " 1");
                Application.Current.Shutdown();
            }
        }