Beispiel #1
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();
            }
        }
Beispiel #2
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();
     }
 }
Beispiel #3
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();
        }