Example #1
0
 private void StartSpeechSyn(string word)
 {
     StartCoroutine(_asr.Synthesis(word, s =>
     {
         if (s.Success)
         {
             audioSource.clip = s.clip;
             audioSource.Play();
             startPlaying = true;
             if (isOwnSpeak)
             {
                 return;
             }
             if (isListening)
             {
                 EventCenter.GetInstance().EventTrigger(MyEventType.GETWAVED);
             }
             else
             {
                 EventCenter.GetInstance().EventTrigger(MyEventType.SAY);
             }
         }
         else
         {
             Debug.LogError("合成失败!");
         }
     }));
 }
Example #2
0
        public byte[] SpeechGenerate(string content)
        {
            byte[] bytes = null;

            if (!string.IsNullOrEmpty(content))
            {
                Dictionary <string, object> options = null;

                if (_ttsParam.IsValid)
                {
                    options = new Dictionary <string, object>()
                    {
                        { "spd", _ttsParam.speed },
                        { "vol", _ttsParam.vol },
                        { "pit", _ttsParam.pit },
                        { "per", (int)_ttsParam.speaker }
                    };
                }

                TtsResponse resp = _tts.Synthesis(content, options);

                bytes = resp?.Data;
            }

            return(bytes);
        }
Example #3
0
        // 合成
        public string Tts(string tex, int per = 0, int spd = 5, int vol = 7)
        {
            var fileName = JlMd5.HashPassword(tex + "|per|" + per + "|spd|" + spd + "|vol|" + vol) + ".mp3";
            var pathName = JlConfig.GetValue <string>("SaveFilePath");
            var fullName = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + pathName + fileName;

            if (!Directory.Exists(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + pathName))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + pathName);
            }

            if (!File.Exists(fullName))
            {
                // 可选参数
                var option = new Dictionary <string, object>()
                {
                    { "spd", spd }, // 语速
                    { "vol", 7 },   // 音量
                    { "per", per }  // 发音人
                };
                var result = _ttsClient.Synthesis(tex, option);
                if (result.ErrorCode == 0)  // 或 result.Success
                {
                    File.WriteAllBytes(fullName, result.Data);
                }
            }

            return(fileName);
        }
Example #4
0
        // 合成
        public void Tts(string text)
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                { "spd", 4 }, // 语速
                { "vol", 7 }, // 音量
                { "per", 4 }  // 发音人,4:情感度丫丫童声
            };
            var result = _ttsClient.Synthesis(text, option);

            if (result.ErrorCode == 0)  // 或 result.Success
            {
                if (!Directory.Exists(@"E:\Text2Voice\"))
                {
                    Directory.CreateDirectory(@"E:\Text2Voice\");
                }
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                string filePath = @"E:\Text2Voice\" + fileName + ".mp3";
                this.filePath = filePath;
                File.WriteAllBytes(filePath, result.Data);
                SetTipInfo(success, "Success");
            }
            else
            {
                SetTipInfo(error, "Error request");
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            var option = new Dictionary <string, object>()
            {
                { "spd", 5 }, // 语速
                { "vol", 5 }, // 音量
                { "per", 0 }  // 发音人,4:情感度丫丫童声
            };
            var            text   = "滴,滴,答,10,9,8,7,6,5,4,3,2,1";
            WaveFileWriter writer = null;
            Stream         output = null;

            foreach (var s in text.Split(','))
            {
                var result = _ttsClient.Synthesis(s, option);
                if (!result.Success)
                {
                    Console.WriteLine(result.ErrorMsg);
                }
                var ret = WriteSpeech(result.Data, 1, ref writer);
                if (ret != null)
                {
                    output = ret;
                }
            }
            writer?.Flush();
            output?.Seek(0, SeekOrigin.Begin);
            using (var fs = new FileStream("test.wav", FileMode.Create)) {
                output?.CopyTo(fs);
                fs.Close();
            }
            writer?.Close();
            Console.ReadLine();
        }
Example #6
0
        /// <summary>
        /// 合成语音,并保存语音文件(编辑器内)
        /// </summary>
        /// <param name="text">合成文本</param>
        /// <param name="savePath">语音文件保存路径</param>
        /// <param name="timeout">超时时长</param>
        /// <param name="audioType">音频文件格式</param>
        /// <param name="speaker">发音人</param>
        /// <param name="volume">音量</param>
        /// <param name="speed">音速</param>
        /// <param name="pitch">音调</param>
        private void SynthesisInEditor(string text, string savePath, SynthesisType audioType = SynthesisType.MP3, int timeout = 60000, Speaker speaker = Speaker.Woman_DuYaYa, int volume = 15, int speed = 5, int pitch = 5)
        {
            if (string.IsNullOrEmpty(text) || text == "" || Encoding.Default.GetByteCount(text) >= 1024)
            {
                GlobalTools.LogError("合成语音失败:文本为空或长度超出了1024字节的限制!");
                return;
            }
            if (File.Exists(savePath))
            {
                GlobalTools.LogError("合成语音失败:已存在音频文件 " + savePath);
                return;
            }

            Tts tts = new Tts(APIKEY, SECRETKEY);

            tts.Timeout        = timeout;
            _ttsOptions["spd"] = Mathf.Clamp(speed, 0, 9);
            _ttsOptions["pit"] = Mathf.Clamp(pitch, 0, 9);
            _ttsOptions["vol"] = Mathf.Clamp(volume, 0, 15);
            _ttsOptions["per"] = (int)speaker;
            _ttsOptions["aue"] = (int)audioType;
            TtsResponse response = tts.Synthesis(text, _ttsOptions);

            if (response.Success)
            {
                File.WriteAllBytes(savePath, response.Data);
                AssetDatabase.Refresh();
                Selection.activeObject = AssetDatabase.LoadAssetAtPath(string.Format("Assets{0}/{1}.{2}", _savePath, _saveName, _format), typeof(AudioClip));
            }
            else
            {
                GlobalTools.LogError("合成语音失败:" + response.ErrorCode + " " + response.ErrorMsg);
            }
        }
Example #7
0
        private static IEnumerator SynthesisByKEYCoroutine(string text, HTFAction <AudioClip> handler, int timeout, Speaker speaker, int volume, int speed, int pitch)
        {
            Tts tts = GetTts();

            tts.Timeout       = timeout;
            TtsOptions["spd"] = Mathf.Clamp(speed, 0, 9);
            TtsOptions["pit"] = Mathf.Clamp(pitch, 0, 9);
            TtsOptions["vol"] = Mathf.Clamp(volume, 0, 15);
            TtsOptions["per"] = (int)speaker;
            TtsOptions["aue"] = 6;
            TtsResponse response = tts.Synthesis(text, TtsOptions);

            yield return(response);

            if (response.Success)
            {
                AudioClip audioClip = SpeechUtility.ToAudioClip(response.Data);

                handler?.Invoke(audioClip);
            }
            else
            {
                GlobalTools.LogError("合成语音失败:" + response.ErrorCode + " " + response.ErrorMsg);
            }
            RecycleTts(tts);
        }
Example #8
0
        public static string Speech(string text, int spd, int vol, int per)
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                { "spd", spd }, // 语速 5
                { "vol", vol }, // 音量 7
                { "per", per }  // 发音人,4:情感度丫丫童声
            };

            try
            {
                var result = client.Synthesis(text, option);
                if (result.ErrorCode == 0)  // 或 result.Success
                {
                    var fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".mp3";
                    File.WriteAllBytes(fileName, result.Data);
                }
            }
            catch (Exception e)
            {
                //return null;
                throw e;
            }

            return(null);
        }
        /// <summary>
        /// 语音合成
        /// </summary>
        /// <param name="content"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static bool speech(string content, Dictionary <string, object> option)
        {
            try
            {
                if (option == null)
                {
                    // 可选参数
                    option = new Dictionary <string, object>()
                    {
                        { "spd", 5 }, // 语速
                        { "vol", 7 }, // 音量
                        { "per", 4 }  // 发音人,4:情感度丫丫童声
                    };
                }

                var result = speechClient.Synthesis(content, option);

                if (result.ErrorCode == 0)
                {
                    File.WriteAllBytes("temp.mp3", result.Data);
                    Play("temp.mp3");
                }
                return(result.Success);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
Example #10
0
        // 合成
        public void Tts()
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                { "spd", 5 }, // 语速
                { "vol", 7 }, // 音量
                { "per", 2 }  // 发音人,4:情感度丫丫童声
            };
            var result = _ttsClient.Synthesis("众里寻他千百度", option);

            if (result.ErrorCode == 0)  // 或 result.Success
            {
                System.IO.File.WriteAllBytes(@"C:\Users\Administrator\Desktop\Speech\合成的语音文件本地存储地址.mp3", result.Data);
            }
        }
Example #11
0
    /// <summary>
    /// 转换语音文件
    /// </summary>
    /// <returns>The string2 audio.</returns>
    /// <param name="gameConfig">Game config.</param>
    IEnumerator TurnString2Audio(GameConfig gameConfig)
    {
        List <string> updateList = new List <string>();

        //通用语音检测部分
        CheckStringToAudio(ConfigDataMgr.ExamStartTip, updateList);
        CheckStringToAudio(ConfigDataMgr.ExamEnd.question, updateList);
        //试题语音检测部分
        for (int i = 0; i < gameConfig.questions.Count; i++)
        {
            QuestionData questionData = gameConfig.questions[i];
            CheckStringToAudio(questionData.question, updateList);
        }

        for (int i = 0; i < updateList.Count; i++)
        {
            //设置转换进度
            uiLoginWindow.SetProgress((float)i / updateList.Count);
            yield return(StartCoroutine(ttsString2Audio.Synthesis(updateList[i], result =>
            {
                if (result.Success)
                {
                    Debug.LogFormat("Trun Success:{0}", updateList[i]);
                    string fileName = System.Guid.NewGuid().ToString("N");
                    ResourcesMgr.Instance.WriteAudioFile(fileName, result.data);
                    ConfigDataMgr.Instance.resourceDict.Add(updateList[i], fileName);
                }
                else
                {
                    Debug.LogErrorFormat("Error:Str2Audio errorno<{0}> errormsg<{1}>", result.err_no, result.err_msg);
                }
            })));
        }
        uiLoginWindow.SetProgress(1.0f);
    }
Example #12
0
    public BaiduTts Play(string content)
    {
        StartCoroutine(tts.Synthesis(content, s =>
        {
            if (s.Success)
            {
                audioSource.clip = s.clip;
                audioSource.Play();

                isPlaying = true;
            }
            else
            {
            }
        }));
        return(this);
    }
Example #13
0
        private byte[] GetSpeech(string speech)
        {
            _logger.Debug($"baidu synthesis {speech}.");
            var result = _tts.Synthesis(speech, _ttsOptions);

            if (result.Success)
            {
                return(GetPcmData(result.Data));
            }
            _logger.Error(result.ErrorMsg);
            return(null);
        }
Example #14
0
    public void SysncBaiduAudio(string text)
    {
        Debug.Log("audio sync");
        StartCoroutine(_asr.Synthesis(text, s =>
        {
            test11.text = s.err_msg;
            if (s.Success)
            {
                //      DescriptionText.text = "合成成功,正在播放";
                _audioSource.clip = s.clip;
                _audioSource.Play();

                _startPlaying = true;

                makrtest.PlayClipTu();
            }
            else
            {
                //   DescriptionText.text = s.err_msg;
                Debug.Log("合成失败");
            }
        }));
    }
        private void StopRecording()
        {
            //此时判断是否处于说话状态,如果是说话状态则将按钮复位,并上传说话内容
            if (StartSpeech)
            {
                //上传内容
                Log.Debug("麦克风停止录音");
                Microphone.End(null);
                //转换格式
                var data = Asr.ConvertAudioClipToPCM16(clipRecord);
                asr.Recognize(data, s =>
                {
                    Log.Debug("进来了");
                    if (s.result == null && s.result.Length < 0)
                    {
                        Log.Debug("结果为空,表示麦克风未识别到声音");
                        //提示有问题,复位
                        entry2.callback.AddListener(N);
                        StartSpeech = false;
                    }
                    else
                    {
                        //有结果,发送给机器人进行语音回复
                        tts.Synthesis(s.result[0], r =>
                        {
                            if (r.Success)
                            {
                                //正常播放
                                Log.Debug("合成成功,正在播放,声音有几秒:" + audioSource.clip.length);
                                audioSource.clip = r.clip;
                                audioSource.Play();
                                //复位
                                entry2.callback.AddListener(N);
                                StartSpeech = false;
                            }
                            else
                            {
                                //这是出问题了
                                Debug.Log(s.err_msg);
                                //提示有问题,复位
                                entry2.callback.AddListener(N);
                                StartSpeech = false;
                            }
                        });
                    }

                });
            }
        }
Example #16
0
        public FileResult SpeechAPI(string txt, int spd = 5, int pit = 5, int vol = 5, int per = 0)
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                { "spd", spd },
                { "pit", pit },
                { "vol", vol },
                { "per", per }
            };

            var         client = new Tts(API_KEY, SECRET_KEY);
            TtsResponse result = client.Synthesis(txt, option);

            return(result.Success ? File(result.Data, "audio/mp3", Math.Abs(txt.GetHashCode()).ToString() + ".mp3") : null);
        }
Example #17
0
        // 合成
        public void Tts()
        {
            // 可选参数
            var option = new Dictionary <string, object>
            {
                { "spd", 5 }, // 语速
                { "vol", 7 }, // 音量
                { "per", 4 } // 发音人,4:情感度丫丫童声
            };
            var result = _ttsClient.Synthesis("众里寻他千百度", option);

            if (result.ErrorCode == 0) // 或 result.Success
            {
                File.WriteAllBytes("合成的语音文件本地存储地址.mp3", result.Data);
            }
        }
Example #18
0
        // 合成
        public void Tts(string message)
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                { "spd", 5 }, // 语速
                { "vol", 7 }, // 音量
                { "per", 4 }  // 发音人,4:情感度丫丫童声
            };
            var result = _ttsClient.Synthesis(message, option);

            if (result.ErrorCode == 0)  // 或 result.Success
            {
                File.WriteAllBytes("output.mp3", result.Data);
            }
        }
        // 识别本地文件
        //public void AsrData()
        //{
        //    var data = File.ReadAllBytes("语音pcm文件地址");
        //    var result = _asrClient.Recognize(data, "pcm", 16000);
        //    Console.Write(result);
        //}

        // 识别URL中的语音文件
        //public void AsrUrl()
        //{
        //    var result = _asrClient.Recoginze(
        //        "http://xxx.com/待识别的pcm文件地址",
        //        "http://xxx.com/识别结果回调地址",
        //        "pcm",
        //        16000);
        //    Console.WriteLine(result);
        //}

        // 合成
        public void Tts(string contents, string path)
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                { "ctp", 1 }, //客户端类型选择,web端填写1
                { "spd", 5 }, // 语速
                { "vol", 7 }, // 音量
                { "per", 4 }  // 发音人,0:情感度丫丫童声
            };
            var result = _ttsClient.Synthesis(contents, option);

            if (result.ErrorCode == 0)  // 或 result.Success
            {
                File.WriteAllBytes(path, result.Data);
            }
        }
Example #20
0
    //文字转语音,将音频文件输出到指定的路径
    public static void Transform(string strWord, string path)
    {
        TtsResponse result = null;

        try
        {
            result = tts.Synthesis(strWord, options);
            if (result.ErrorCode == 0)  // 或 result.Success
            {
                File.WriteAllBytes(path, result.Data);
            }
        }
        catch (AipException exception)
        {
            throw exception;
        }
    }
Example #21
0
    //合成
    void Synthesis(string tts_conent)
    {
        DescriptionText.text = "合成中...";

        StartCoroutine(_tts.Synthesis(tts_conent, s =>
        {
            if (s.Success)
            {
                DescriptionText.text = "合成成功,正在播放";
                _audioSource.clip    = s.clip;
                _audioSource.Play();
                _startPlaying = true;
            }
            else
            {
                DescriptionText.text = s.err_msg;
            }
        }));
    }
Example #22
0
        // 合成
        public static bool Tts(string input, string path, int spd = 5, int pit = 5, int vol = 6, int per = 4)
        {
            // 可选参数
            var option = new Dictionary <string, object>
            {
                { "spd", spd }, // 语速,取值0-9,默认为5中语速
                { "pit", pit }, // 音调,取值0-9,默认为5中语调
                { "vol", vol }, // 音量,取值0-15,默认为5中音量
                { "per", per }  // 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
            };
            var result = TtsClient.Synthesis(input, option);

            if (result.Success)
            {
                File.WriteAllBytes(path, result.Data);
            }

            //Console.WriteLine(result.Serialize());

            return(result.Success);
        }
Example #23
0
    private void OnClickSynthesisButton()
    {
        SynthesisButton.gameObject.SetActive(false);
        DescriptionText.text = "合成中...";

        StartCoroutine(_asr.Synthesis(Input.text = stri, s =>
        {
            if (s.Success)
            {
                DescriptionText.text = "正在播放";
                _audioSource.clip    = s.clip;
                _audioSource.Play();

                _startPlaying = true;
            }
            else
            {
                DescriptionText.text = s.err_msg;
                SynthesisButton.gameObject.SetActive(true);
            }
        }));
    }
Example #24
0
        private void audioAlarm(object audioStr)
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                { "aue", "6" },     // 格式wav
                { "spd", 5 },       // 语速
                { "vol", 7 },       // 音量
                { "per", 4 }        // 发音人,4:情感度丫丫童声
            };
            var result2 = client2.Synthesis((string)audioStr, option);

            if (result2.ErrorCode == 0)  // 或 result.Success
            {
                File.WriteAllBytes("temp.wav", result2.Data);
                g_playsound.Alarm(Util.soundPath + "\\temp.wav");
            }
            else
            {
                MessageBox.Show(result2.ToString());
            }
        }
Example #25
0
        static void alarm(string voiceContent)
        {
            Console.WriteLine(voiceContent);
            var API_KEY    = "3TOIOWY3Kxqj3qIpn2epvsX8";
            var SECRET_KEY = "8ZUENcYUfR6E9mpthFwgcqm8TCoMvWTP";
            var client     = new Tts(API_KEY, SECRET_KEY);

            client.Timeout = 60000;  // 修改超时时间
            var option = new Dictionary <string, object>()
            {
                { "spd", 4 },
                { "vol", 10 }
            };
            var result = client.Synthesis(voiceContent, option);

            if (result.Success)
            {
                File.WriteAllBytes("voice.mp3", result.Data);
                Console.WriteLine("Playing voice");
                Process.Start(@"mplayer", @"voice.mp3");
            }
        }
Example #26
0
        public string Tts(string text)
        {
            if (text.Length == 0)
            {
                text = "无名";
            }

            logger.Info("语音合成:{}", text);

            string path = voiceCacheFloder + "/" + text + ".mp3";

            if (!Directory.Exists(voiceCacheFloder))
            {
                Directory.CreateDirectory(voiceCacheFloder);
            }

            if (File.Exists(path))
            {
                logger.Info("己存在[{}]语音合成文件:{}", text, path);
                return(path);
            }

            Tts _ttsClient = new Tts(API_KEY, SECRET_KEY);
            var option     = new Dictionary <string, object>()
            {
                { "spd", 5 }, // 语速
                { "vol", 7 }, // 音量
                { "per", 1 }  // 发音人,4:情感度丫丫童声
            };
            var result = _ttsClient.Synthesis(text, option);

            if (result.ErrorCode == 0)
            {
                File.WriteAllBytes(path, result.Data);
                return(path);
            }

            throw new IOException("语音合成失败");
        }
Example #27
0
        private void btn_VoiceCombine_Click(object sender, EventArgs e)
        {
            // 可选参数
            var option = new Dictionary <string, object>()
            {
                //{"spd", 3}, // 语速
                { "spd", 3 }, // 语速
                { "vol", 7 }, // 音量
                { "per", 4 }  // 发音人,4:情感度丫丫童声
            };
            var result = _ttsClient.Synthesis(txt_combine.Text, option);

            Random a    = new Random();
            string aa   = "AA" + a.Next();
            string path = Application.StartupPath + "\\wav\\" + aa + "voice.mp3";


            if (result.ErrorCode == 0)  // 或 result.Success
            {
                File.WriteAllBytes(path, result.Data);
            }
            txt_VoicePath.Text = path;
        }
        // 合成
        public static bool Tts(string content)
        {
            try
            {
                // 可选参数
                var option = new Dictionary <string, object>()
                {
                    { "spd", 5 }, // 语速
                    { "vol", 7 }, // 音量
                    { "per", 4 }  // 发音人,4:情感度丫丫童声
                };
                var result = _ttsClient.Synthesis(content, option);

                if (result.ErrorCode == 0)  // 或 result.Success
                {
                    SaveFile(result.Data);
                }
                return(result.Success);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
Example #29
0
        private static IEnumerator SynthesisByKEYCoroutine(string text, string savePath, SynthesisType audioType, int timeout, Speaker speaker, int volume, int speed, int pitch)
        {
            Tts tts = GetTts();

            tts.Timeout       = timeout;
            TtsOptions["spd"] = Mathf.Clamp(speed, 0, 9);
            TtsOptions["pit"] = Mathf.Clamp(pitch, 0, 9);
            TtsOptions["vol"] = Mathf.Clamp(volume, 0, 15);
            TtsOptions["per"] = (int)speaker;
            TtsOptions["aue"] = (int)audioType;
            TtsResponse response = tts.Synthesis(text, TtsOptions);

            yield return(response);

            if (response.Success)
            {
                File.WriteAllBytes(savePath, response.Data);
            }
            else
            {
                Debug.LogError("合成语音失败:" + response.ErrorCode + " " + response.ErrorMsg);
            }
            RecycleTts(tts);
        }
Example #30
0
        public async Task <IActionResult> SpeechTtsAsync()
        {
            using var client = _httpClient.CreateClient();

            var json = await client.GetStringAsync("http://open.iciba.com/dsapi/");

            var obj     = JObject.Parse(json);
            var note    = obj["note"].ToString();
            var content = obj["content"].ToString();

            var _ttsClient = new Tts(AppSettings.BaiduAI.APIKey, AppSettings.BaiduAI.SecretKey)
            {
                Timeout = 60000
            };

            // https://ai.baidu.com/docs#/TTS-Online-Csharp-SDK/d27a4e02
            var option = new Dictionary <string, object>()
            {
                { "spd", 5 }, // 语速,取值0-9,默认为5中语速
                { "vol", 7 }, // 音量,取值0-15,默认为5中音量
                { "per", 4 }  // 发音人, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫
            };

            var result = _ttsClient.Synthesis(string.Format(GlobalConsts.GreetWord, note, content), option);

            if (result.Success)
            {
                return(File(result.Data, "audio/mpeg"));
            }
            else
            {
                var ttsBytes = await client.GetByteArrayAsync(obj["tts"].ToString());

                return(File(ttsBytes, "audio/mpeg"));
            }
        }