public IActionResult SpeakerSetting(string voice_db)
        {
            SpeakerSettingModel model = Setting.Speaker.Clone();

            model.VoiceDbName = voice_db ?? Setting.Speaker.VoiceDbName;

            // 話者名のリストを取得する
            string[] voice_names = null;
            Setting.Lock();
            try
            {
                if ((model.VoiceDbName != null) && (0 < model.VoiceDbName.Length))
                {
                    AitalkWrapper.LoadVoice(model.VoiceDbName);
                    voice_names = AitalkWrapper.Parameter.VoiceNames;
                }
            }
            catch (Exception) { }
            finally
            {
                Setting.Unlock();
            }

            // 音声ライブラリと話者のリストを作成してビューに渡す
            ViewData["VoiceDbListItems"] = GenerateSelectListItems(AitalkWrapper.VoiceDbList);
            ViewData["SpeakerListItems"] = GenerateSelectListItems(voice_names);
            return(View(model));
        }
Example #2
0
        public IActionResult SpeectTextFromPost([FromBody] SpeechModel speech_model)
        {
            Setting.Lock();
            try
            {
                if (speech_model.SpeakerSetting != null &&
                    speech_model.SpeakerSetting.VoiceDbName.Length > 0 &&
                    speech_model.SpeakerSetting.SpeakerName.Length > 0 &&
                    AitalkWrapper.Parameter.CurrentSpeakerName != speech_model.SpeakerSetting.SpeakerName)
                {
                    var error_message = Setting.ApplySpeakerSetting(speech_model.SpeakerSetting);
                    if (error_message != null)
                    {
                        return(BadRequest($"Saved but {error_message}"));
                    }
                }

                // 話者パラメータを設定する
                var speaker = speech_model.Speaker ?? new SpeakerModel();
                AitalkWrapper.Parameter.VoiceVolume   = (0 <= speaker.Volume) ? speaker.Volume : Setting.DefaultSpeakerParameter.Volume;
                AitalkWrapper.Parameter.VoiceSpeed    = (0 <= speaker.Speed) ? speaker.Speed : Setting.DefaultSpeakerParameter.Speed;
                AitalkWrapper.Parameter.VoicePitch    = (0 <= speaker.Pitch) ? speaker.Pitch : Setting.DefaultSpeakerParameter.Pitch;
                AitalkWrapper.Parameter.VoiceEmphasis = (0 <= speaker.Emphasis) ? speaker.Emphasis : Setting.DefaultSpeakerParameter.Emphasis;
                AitalkWrapper.Parameter.PauseMiddle   = (0 <= speaker.PauseMiddle) ? speaker.PauseMiddle : Setting.DefaultSpeakerParameter.PauseMiddle;
                AitalkWrapper.Parameter.PauseLong     = (0 <= speaker.PauseLong) ? speaker.PauseLong : Setting.DefaultSpeakerParameter.PauseLong;
                AitalkWrapper.Parameter.PauseSentence = (0 <= speaker.PauseSentence) ? speaker.PauseSentence : Setting.DefaultSpeakerParameter.PauseSentence;

                // テキストが与えられた場合は仮名に変換する
                string kana = null;
                if ((speech_model.Kana != null) && (0 < speech_model.Kana.Length))
                {
                    kana = speech_model.Kana;
                }
                else if ((speech_model.Text != null) && (0 < speech_model.Text.Length))
                {
                    kana = AitalkWrapper.TextToKana(speech_model.Text, Setting.System.KanaTimeout);
                }
                if ((kana == null) || (kana.Length <= 0))
                {
                    return(new NoContentResult());
                }

                // 音声変換して結果を返す
                var wave_stream = new MemoryStream();
                AitalkWrapper.KanaToSpeech(kana, wave_stream, Setting.System.SpeechTimeout);
                return(new FileContentResult(wave_stream.ToArray(), "audio/wav"));
            }
            catch (Exception)
            {
                return(new NoContentResult());
            }
            finally
            {
                Setting.Unlock();
            }
        }
        /// <summary>
        /// 話者リストを取得する.全ライブラリをロードするので時間がかかる.
        /// </summary>
        /// <returns></returns>
        public IActionResult GetSpeakers()
        {
            var libname2speaker = new Dictionary <string, string[]>();

            Setting.Lock();
            foreach (var libname in AitalkWrapper.VoiceDbList)
            {
                AitalkWrapper.LoadVoice(libname);
                var voice_names = AitalkWrapper.Parameter.VoiceNames;
                libname2speaker.Add(libname, voice_names);
            }
            _ = Setting.ApplySpeakerSetting(Setting.Speaker);
            Setting.Unlock();
            return(Ok(libname2speaker));
        }
Example #4
0
        /// <summary>
        /// 新しい話者設定値を適用する。
        /// </summary>
        /// <param name="setting">新しい話者設定値</param>
        /// <returns>エラーメッセージ、もしくはnull</returns>
        public static string ApplySpeakerSetting(SpeakerSettingModel setting)
        {
            Speaker = setting;
            try
            {
                // ボイスライブラリを読み込む
                if (0 < Speaker.VoiceDbName.Length)
                {
                    // 指定されたボイスライブラリを読み込む
                    string voice_db_name = Speaker.VoiceDbName;
                    AitalkWrapper.LoadVoice(voice_db_name);

                    // 話者が指定されているときはその話者を選択する
                    if (0 < Speaker.SpeakerName.Length)
                    {
                        AitalkWrapper.Parameter.CurrentSpeakerName = Speaker.SpeakerName;
                    }
                }
                else
                {
                    // 未指定の場合、初めに見つけたものを読み込む
                    string voice_db_name = AitalkWrapper.VoiceDbList.FirstOrDefault() ?? "";
                    AitalkWrapper.LoadVoice(voice_db_name);
                }

                // 話者パラメータの初期値を記憶する
                DefaultSpeakerParameter = new SpeakerModel
                {
                    Volume        = AitalkWrapper.Parameter.VoiceVolume,
                    Speed         = AitalkWrapper.Parameter.VoiceSpeed,
                    Pitch         = AitalkWrapper.Parameter.VoicePitch,
                    Emphasis      = AitalkWrapper.Parameter.VoiceEmphasis,
                    PauseMiddle   = AitalkWrapper.Parameter.PauseMiddle,
                    PauseLong     = AitalkWrapper.Parameter.PauseLong,
                    PauseSentence = AitalkWrapper.Parameter.PauseSentence
                };

                return(null);
            }
            catch (AitalkException ex)
            {
                return(ex.Message);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Example #5
0
 public string ConvertTextFromPost([FromBody] SpeechModel speech_model)
 {
     Setting.Lock();
     try
     {
         if ((speech_model.Text == null) || (speech_model.Text.Length <= 0))
         {
             return(null);
         }
         return(AitalkWrapper.TextToKana(speech_model.Text, Setting.System.KanaTimeout));
     }
     catch (Exception)
     {
         return(null);
     }
     finally
     {
         Setting.Unlock();
     }
 }
Example #6
0
        /// <summary>
        /// 新しいシステム設定値を適用する。
        /// </summary>
        /// <param name="setting">新しいシステム設定値</param>
        /// <returns>エラーメッセージ、もしくはnull</returns>
        public static string ApplySystemSetting(SystemSettingModel setting)
        {
            System        = setting;
            IconByteArray = null;
            try
            {
                // インストールディレクトリと実行ファイルの存在を確認する
                if (Directory.Exists(System.InstallPath) == false)
                {
                    return("インストールディレクトリが存在しません。");
                }
                string exe_path = $"{System.InstallPath}\\{System.VoiceroidEditorExe}";
                if (File.Exists(exe_path) == false)
                {
                    return("VOICEROID2エディタの実行ファイルが存在しません。");
                }

                // アイコンを取得する
                try
                {
                    using (var icon = Icon.ExtractAssociatedIcon(exe_path))
                        using (var bitmap = icon.ToBitmap())
                            using (var stream = new MemoryStream())
                            {
                                bitmap.Save(stream, ImageFormat.Png);
                                IconByteArray = stream.ToArray();
                            }
                }
                catch (Exception) { }

                // AITalkを初期化する
                AitalkWrapper.Initialize(System.InstallPath, System.AuthCodeSeed);

                // 言語ライブラリを読み込む
                if ((System.LanguageName != null) && (0 < System.LanguageName.Length))
                {
                    // 指定された言語ライブラリを読み込む
                    AitalkWrapper.LoadLanguage(System.LanguageName);
                }
                else
                {
                    // 未指定の場合、初めに見つけたものを読み込む
                    string language_name = AitalkWrapper.LanguageList.FirstOrDefault() ?? "";
                    AitalkWrapper.LoadLanguage(language_name);
                }

                // フレーズ辞書が指定されていれば読み込む
                if (File.Exists(System.PhraseDictionaryPath))
                {
                    AitalkWrapper.ReloadPhraseDictionary(System.PhraseDictionaryPath);
                }

                // 単語辞書が指定されていれば読み込む
                if (File.Exists(System.WordDictionaryPath))
                {
                    AitalkWrapper.ReloadWordDictionary(System.WordDictionaryPath);
                }

                // 記号ポーズ辞書が指定されていれば読み込む
                if (File.Exists(System.SymbolDictionaryPath))
                {
                    AitalkWrapper.ReloadSymbolDictionary(System.SymbolDictionaryPath);
                }

                return(null);
            }
            catch (AitalkException ex)
            {
                return(ex.Message);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Example #7
0
        // リクエストを処理する
        private void ProcessRequest(HttpListenerContext context)
        {
            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse responce = context.Response;

            try
            {
                if (request.HttpMethod != "GET")
                {
                    throw new NotImplementedException();
                }

                int    offset = 0;
                string path   = request.RawUrl.Substring(1);
                var    query  = HttpUtility.ParseQueryString(request.Url.Query);

                // メソッド名を調べる
                if (UrlMatch(path, "kana/", ref offset) == true)
                {
                    // 仮名変換メソッドを呼び出している
                    if (UrlMatch(path, "fromtext/", ref offset) == false)
                    {
                        throw new ArgumentException("変換するテキストが指定されていません。");
                    }

                    // 変換するテキストを取得する
                    string text_encoded = path.Substring(offset, path.Length - offset);
                    string text         = HttpUtility.UrlDecode(text_encoded);
                    string kana         = AitalkWrapper.TextToKana(text, Config.KanaTimeout);

                    // 仮名を返す
                    byte[] result = Encoding.UTF8.GetBytes(kana);
                    responce.OutputStream.Write(result, 0, result.Length);
                    responce.ContentEncoding = Encoding.UTF8;
                    responce.ContentType     = "text/plain";
                }
                else if (UrlMatch(path, "speech/", ref offset) == true)
                {
                    // 音声変換メソッドを呼び出している
                    string kana = null;
                    if (UrlMatch(path, "fromtext/", ref offset) == true)
                    {
                        // テキストが入力されたときは仮名に変換する
                        string text_encoded = path.Substring(offset, path.Length - offset);
                        string text         = HttpUtility.UrlDecode(text_encoded);
                        kana = AitalkWrapper.TextToKana(text, Config.KanaTimeout);
                    }
                    else if (UrlMatch(path, "fromkana/", ref offset) == true)
                    {
                        string kana_encoded = path.Substring(offset, path.Length - offset);
                        kana = HttpUtility.UrlDecode(kana_encoded);
                    }
                    else
                    {
                        throw new ArgumentException("変換するテキストが指定されていません。");
                    }

                    // 音声に変換する
                    var stream = new MemoryStream();
                    AitalkWrapper.KanaToSpeech(kana, stream, Config.SpeechTimeout);

                    // 音声を返す
                    byte[] result = stream.ToArray();
                    responce.OutputStream.Write(result, 0, result.Length);
                    responce.ContentType = "audio/wav";
                }
                else if (path == "voicedb.json")
                {
                    // ボイスライブラリの一覧を返す
                    string[] voice_db_list = AitalkWrapper.VoiceDbList;
                    using (var stream = new MemoryStream())
                    {
                        var serializer = new DataContractJsonSerializer(typeof(string[]));
                        serializer.WriteObject(stream, voice_db_list);
                        byte[] result = stream.ToArray();
                        responce.OutputStream.Write(result, 0, result.Length);
                        responce.ContentEncoding = Encoding.UTF8;
                        responce.ContentType     = "application/json";
                    }
                }
                else if (path == "param.json")
                {
                    // TTSパラメータを返す
                    byte[] result = AitalkWrapper.Parameter.ToJson();
                    responce.OutputStream.Write(result, 0, result.Length);
                    responce.ContentEncoding = Encoding.UTF8;
                    responce.ContentType     = "application/json";
                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
            catch (NotImplementedException)
            {
                responce.StatusCode = (int)HttpStatusCode.NotImplemented;
            }
            catch (FileNotFoundException)
            {
                responce.StatusCode = (int)HttpStatusCode.NotFound;
            }
            catch (Exception ex)
            {
                // 例外を文字列化して返す
                responce.StatusCode = (int)HttpStatusCode.InternalServerError;
                byte[] byte_data = Encoding.UTF8.GetBytes(ex.ToString());
                responce.OutputStream.Write(byte_data, 0, byte_data.Length);
                responce.ContentEncoding = Encoding.UTF8;
                responce.ContentType     = "text/plain";
            }

            // レスポンスを返す
            responce.Close();
        }
Example #8
0
        /// <summary>
        /// HTTPサーバーを起動するモード
        /// </summary>
        private void StartServer()
        {
#if DEBUG
            // Debugビルドの場合、ログファイルを出力する
            Trace.Listeners.Add(new TextWriterTraceListener("trace.log"));
            Trace.AutoFlush = true;
#endif

            // AITalkを初期化する
            AitalkWrapper.Initialize(Config.InstallPath, Config.AuthCodeSeed);

            try
            {
                // 言語ライブラリを読み込む
                AitalkWrapper.LoadLanguage(Config.LanguageName);

                // フレーズ辞書が設定されていれば読み込む
                if (File.Exists(Config.PhraseDictionaryPath))
                {
                    AitalkWrapper.ReloadPhraseDictionary(Config.PhraseDictionaryPath);
                }

                // 単語辞書が設定されていれば読み込む
                if (File.Exists(Config.WordDictionaryPath))
                {
                    AitalkWrapper.ReloadWordDictionary(Config.WordDictionaryPath);
                }

                // 記号ポーズ辞書が設定されていれば読み込む
                if (File.Exists(Config.SymbolDictionaryPath))
                {
                    AitalkWrapper.ReloadSymbolDictionary(Config.SymbolDictionaryPath);
                }

                // ボイスライブラリを読み込む
                AitalkWrapper.LoadVoice(Config.VoiceDbName);

                // 話者を設定する
                AitalkWrapper.Parameter.CurrentSpeakerName = Config.VoiceName;

                // 処理を別スレッドで実行する
                Task task = Task.Factory.StartNew(Run);

                // トレイアイコンを作成する
                // アイコンはVOICEROIDエディタのものを使用するが、ダメならこの実行ファイルのものを使用する
                NotifyIcon notify_icon = new NotifyIcon();
                try
                {
                    notify_icon.Icon = Icon.ExtractAssociatedIcon($"{Config.InstallPath}\\{Config.VoiceroidEditorExe}");
                }
                catch (Exception)
                {
                    notify_icon.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
                }
                notify_icon.Text    = $"{Caption} : {Config.VoiceName}\nListening at {Config.ListeningAddress}";
                notify_icon.Visible = true;

                // トレイアイコンのコンテキストメニューを作成する
                ContextMenu menu = new ContextMenu();
                menu.MenuItems.Add(new MenuItem("Exit", new EventHandler((object sender, EventArgs e) =>
                {
                    StopServerCancelToken.Cancel();
                    task.Wait();
                    notify_icon.Visible = false;
                    Application.Exit();
                    Environment.Exit(1);
                })));
                notify_icon.ContextMenu = menu;

                // メッセージループを開始する
                Application.Run();
            }
            finally
            {
                AitalkWrapper.Finish();
            }
        }