Exemplo n.º 1
0
        /// <summary>
        /// TTS 语音合成
        /// </summary>
        /// <param name="appid">appId</param>
        /// <param name="apikey">apiKey</param>
        /// <param name="apisecret">apiSecret</param>
        /// <param name="config">配置文件,不传则选择默认配置</param>
        public XunFeiTTS(string appid, string apikey, string apisecret, ConfigSetting config = null)
        {
            appId     = appid;
            apiKey    = apikey;
            apiSecret = apisecret;
            if (config != null)
            {
                configSetting = config;
            }

            date = TimeSpanHelper.GetTimeRFC1123();

            string sign = $"host: {host}\ndate: {date}\n{requestLine}";

            string signature         = RSAHelper.HMACSha256(apiSecret, sign);
            string authorization_org = $"api_key=\"{apiKey}\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"{signature}\"";
            string authorization     = Base64Helper.EncodeBase64("utf-8", authorization_org);
            string reqUrl            = string.Format(baseUrl + "?authorization={0}&date={1}&host={2}", authorization, HttpUtility.UrlEncode(date).Replace("+", "%20"), host);

            try
            {
                websocket            = new WebSocket(reqUrl);
                websocket.OnMessage += Websocket_OnMessage;
                websocket.OnOpen    += Websocket_OnOpen;

                websocket.Connect();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="message">文本内容,最大约2000汉字</param>
        public void SendData(string message)
        {
            if (message.Length > 2000)
            {
                //默认传输都是汉字
                throw new Exception("传入字数过长,不得超过2000汉字!");
            }
            try
            {
                JObject frame    = new JObject();
                JObject business = new JObject();
                JObject common   = new JObject();
                JObject data     = new JObject();
                // 填充common
                common.Add("app_id", appId);
                //填充business
                business.Add("aue", configSetting.aue);
                business.Add("tte", configSetting.tte);
                business.Add("ent", configSetting.ent);
                business.Add("vcn", configSetting.vcn);
                business.Add("pitch", configSetting.pitch);
                business.Add("speed", configSetting.speed);
                business.Add("volume", configSetting.volume);
                business.Add("auf", configSetting.auf);
                business.Add("ram", configSetting.ram);
                business.Add("rdn", configSetting.rdn);
                business.Add("reg", configSetting.reg);
                business.Add("sfl", configSetting.sfl);
                data.Add("status", 2);//数据状态,固定位2
                data.Add("text", Base64Helper.EncodeBase64("utf-8", message));

                frame.Add("common", common);
                frame.Add("business", business);
                frame.Add("data", data);

                byte[] value = Encoding.Default.GetBytes(frame.ToString());

                sendMessage(value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }