努力したWiki

推敲の足りないメモ書き多数

ユーザ用ツール

サイト用ツール


documents:voiceroid:voiceroid-004

seikaServerへ接続するWindowsクライアント

2018/04/14

2017/08/20

  • ページが大きくなってきたので、rechoseika.exeページを分離。
  • seikaServerでMicrosoft Speechの音声合成を使うため一部修正。

概要

VOICEROID+EX/CeVIOをリモートからしゃべらせるのサーバプログラムへ接続する、Windows用クライアントです。

ダウンロードと使い方

VOICEROID+EX/CeVIOをリモートからしゃべらせるを参照してください。アーカイブに rechoseika.exeが含まれています。

ソース

rechoseikaのソースは以下の通り。

rechoseika.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using System.Net;
using System.Media;
 
namespace rechoseika
{
    class rechoseika
    {
        static void Main(string[] args)
        {
            getopt opts = new getopt(args);
            byte[] data = null;
 
            if (opts.isUsable)
            {
                Task<byte[]> t = request(opts);
 
                t.Wait();
                data = t.Result;
                if ((opts.place == "LOCAL") && (data != null))
                {
                    play(opts, data);
                }
            }
 
            return;
        }
 
        /// <summary>
        /// 音声データ取得
        /// </summary>
        static async Task<byte[]> request(getopt opts)
        {
            HttpClient req = new HttpClient();
            byte[] ans = null;
 
            try
            {
                HttpResponseMessage res = await req.PostAsync(opts.Url, new FormUrlEncodedContent(opts.postdata));
 
                if (res.StatusCode != HttpStatusCode.OK)
                {
                    string mes = await res.Content.ReadAsStringAsync();
                    HttpStatusCode code = res.StatusCode;
                    Console.WriteLine("{0} {1}", (int)code, mes);
                }
                else
                {
                    ans = await res.Content.ReadAsByteArrayAsync();
                }
            }
            catch (AggregateException e)
            {
                Console.WriteLine("{0}", e.Message);
                Console.WriteLine("{0}", opts.Url);
                ans = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
                Console.WriteLine("{0}", opts.Url);
                ans = null;
            }
 
            return ans;
        }
 
        /// <summary>
        /// 取得したデータの再生
        /// </summary>
        static void play(getopt opts, byte[] data)
        {
            string filename = (opts.wavFilePath == "") ? Path.GetTempFileName() : opts.wavFilePath;
 
            using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                fs.Write(data, 0, data.Length);
            }
 
            SoundPlayer sp = new SoundPlayer(filename);
            sp.PlaySync();
            sp.Dispose();
 
            if ((opts.place == "LOCAL") && (opts.wavFilePath == "")) File.Delete(filename);
        }
 
    }
 
 
    /// <summary>
    /// やっつけオプション解析
    /// </summary>
    class getopt
    {
        public string Url {
            get
            {
                return string.Format("http://{0}:7180/{1}/{2}", seikaserver, place == "LOCAL" ? "SAVE" : "PLAY", cv);
            }
        }
        public string wavFilePath { get; }
        public Dictionary<string, string> postdata { get; }
        public bool isUsable { get; }
        public string place { get; }
 
 
        private IPAddress seikaserver = IPAddress.Loopback;
        private string cv = "SEIKA";
 
        private Dictionary<string, int> optfmt = new Dictionary<string, int>() {
            {"-h",0 },
            {"-p",1},
            {"-s",1},
            {"-cv",1},
            {"-wav",1},
            {"-volume",1},
            {"-pitch",1},
            {"-speed",1},
            {"-alpha",1},
            {"-intonation",1},
        };
 
        public getopt(string[] args)
        {
            decimal pValue = 0.00m;
 
            postdata = new Dictionary<string, string>();
            wavFilePath = "";
            place = "LOCAL";
            isUsable = true;
 
            int i = 0;
 
            while (i < args.Length)
            {
                string opt = args[i];
                int optargcnt = optfmt.ContainsKey(opt) ? optfmt[opt] : 0; ;
                string[] subargs = new string[optargcnt];
 
                for(int ii=0; ii<optargcnt; ii++)
                {
                    subargs[ii] = args[i + ii + 1];
                }
 
                try
                {
                    switch (opt)
                    {
                        default:
                            postdata["TALKTEXT"] = args[i];
                            i = args.Length;
                            break;
 
                        case "-p":
                            switch (subargs[0].ToUpper())
                            {
                                case "REMOTE":
                                case "LOCAL":
                                    place = subargs[0].ToUpper();
                                    break;
 
                                default:
                                    throw new Exception(string.Format("{0} error. Unknown '{1}'", opt, subargs[0]));
                            }
                            break;
 
                        case "-s":
                            seikaserver = IPAddress.Parse(subargs[0]);
                            break;
 
                        case "-wav":
                            wavFilePath = Path.GetFullPath(subargs[0]);
                            if (wavFilePath == "") throw new Exception("wavefile path invalid.");
                            break;
 
                        case "-cv":
                            cv = subargs[0].ToUpper();
                            break;
 
                        case "-volume":
                            pValue = decimal.Parse(subargs[0]);
                            postdata["VOLUME"] = pValue.ToString();
                            break;
 
                        case "-pitch":
                            pValue = decimal.Parse(subargs[0]);
                            postdata["PITCH"] = pValue.ToString();
                            break;
 
                        case "-speed":
                            pValue = decimal.Parse(subargs[0]);
                            postdata["SPEED"] = pValue.ToString();
                            break;
 
                        case "-alpha":
                            pValue = decimal.Parse(subargs[0]);
                            postdata["ALPHA"] = pValue.ToString();
                            break;
 
                        case "-intonation":
                            pValue = decimal.Parse(subargs[0]);
                            postdata["INTONATION"] = pValue.ToString();
                            break;
 
                        case "-h":
                            Console.WriteLine("usage: echoseika [-s host] [[[option1] option2] ... optionN] TalkText");
                            Console.WriteLine("");
                            Console.WriteLine("options:");
                            Console.WriteLine("-s host         : Hostname or IP address of seikaserver. default is localhost");
                            Console.WriteLine("-p place        : place to play. place: remote or local. default is local");
                            Console.WriteLine("-wav wavefile   : save voice to wavefile.wav");
                            Console.WriteLine("-cv voiceroid   : use product name. default is SEIKA");
                            Console.WriteLine("-volume P       : set volume parameter P");
                            Console.WriteLine("-speed P        : set speed parameter P");
                            Console.WriteLine("-pitch P        : set pitch parameter P");
                            Console.WriteLine("-intonation P   : set intonation parameter P");
                            Console.WriteLine("-alpha P        : set alpha parameter P");
                            Console.WriteLine("TalkText        : text to vocalize");
                            i = args.Length;
                            isUsable = false;
                            break;
                    }
                    i += optargcnt;
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} error. {1}", opt, e.Message);
                    isUsable = false;
                    i = args.Length;
                }
 
                i++;
 
            } // end of while
        }
    }
}

コメント

コメントを入力. Wiki文法が有効です:
画像の文字が読めなければ、文字を読んだ.wavファイルをダウンロードして下さい。
 
documents/voiceroid/voiceroid-004.txt · 最終更新: 2018/04/14 20:29 by k896951

ページ用ツール