private void getRobotSpeak(string readervalue) { var API_KEY = "dkdLZdH306a2dEquuoBZHIrx"; var SECRET_KEY = "k3r6ROeXUMgHpfe6z5DzgD2bzk3knQRb"; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); ThreadStart childref = new ThreadStart(CallToChildThread); Thread childThread = new Thread(childref); childThread.Start(); var option = new Dictionary <string, object>() { { "spd", 5 }, { "vol", 6 }, { "per", 4 } }; var result = client.Synthesis(readervalue, option); if (result.Success) { File.WriteAllBytes(@"..\Data\tmp.wav", result.Data); SoundPlayer sound = new SoundPlayer(@"..\Data\tmp.wav"); sound.Play(); } childThread.Abort(); }
private void button4_Click(object sender, EventArgs e) { //spd 选填 语速,取值0 - 15,默认为5中语速 //pit 选填 音调,取值0 - 15,默认为5中语调 //vol 选填 音量,取值0 - 15,默认为5中音量 //per 选填 发音人选择, 0为普通女声,1为普通男生,3为情感合成 - 度逍遥,4为情感合成 - 度丫丫,默认为普通女声 //aue 选填 3为mp3格式(默认); 4为pcm - 16k;5为pcm - 8k;6为wav(内容同pcm - 16k); 注意aue = 4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。 var option = new Dictionary <string, object>() { { "spd", 5 }, // 语速 { "vol", 7 }, // 音量 { "per", 4 }, // 发音人,4:情感度丫丫童声 { "aue", 6 } }; var _ttsClient = new Baidu.Aip.Speech.Tts("qLq1FHkCUdGGoKRaabFuNCh7", "246GQ7qzIzTZu7XBxy8iVikARvIIYzuN "); var result = _ttsClient.Synthesis(this.textBox1.Text, option); if (result.ErrorCode == 0) // 或 result.Success { File.WriteAllBytes("d:\\1.wav", result.Data); } Stream stream = new MemoryStream(result.Data); SoundPlayer sound = new SoundPlayer(stream); sound.Play(); }
// 合成 public void Hecheng(string wenzi) { // 设置APPID/AK/SK var APP_ID = ""; var API_KEY = ""; var SECRET_KEY = ""; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 // 可选参数 var option = new Dictionary <string, object>() { { "spd", 5 }, // 语速 { "vol", 1 }, // 音量 { "per", 4 } // 发音人,4:情感度丫丫童声 }; if (Danmu1) { var result = client.Synthesis(wenzi, option); if (result.ErrorCode == 0) // 或 result.Success { File.WriteAllBytes("tmp/" + abc + ".mp3", result.Data); Landu("tmp/" + abc + ".mp3"); abc++; } } }
/// <summary> /// 构造函数 /// </summary> /// <param name="apiKey">API_KEY</param> /// <param name="secretKey">SECRET_KEY</param> public TtsBaidu(string apiKey, string secretKey) { _apiKey = apiKey; _secretKey = secretKey; _tts = new Baidu.Aip.Speech.Tts(_apiKey, _secretKey); _tts.Timeout = 60000; }
public Speech(IOptions <ApiConfig> apiConfig, ILogger <Speech> logger) { _logger = logger; _client = new Baidu.Aip.Speech.Tts(apiConfig.Value.API_KEY, apiConfig.Value.SECRET_KEY) { Timeout = 60000 }; }
/// <summary> /// 百度语音合成接口 /// </summary> /// <param name="APP_ID"></param> /// <param name="API_KEY"></param> /// <param name="SECRET_KEY"></param> /// <param name="Gettext">要合成的文字</param> /// <param name="filename">合成后的文件路径</param> public void Tts(string APP_ID, string API_KEY, string SECRET_KEY, string Gettext, string filename) { var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); // 可选参数 var option = new Dictionary <string, object>() { { "spd", 5 }, // 语速 { "vol", 5 }, // 音量 { "per", 0 } // 发音人:0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫 }; var result = client.Synthesis(Gettext, option); if (result.ErrorCode == 0) // 或 result.Success { System.IO.File.WriteAllBytes(filename, result.Data); } }
public void Speak(string text, dynamic playDevice, bool isSync, float?volume) { var settings = _plugin.Settings.BaiduTtsSettings; var person = settings.Person; if (person > 1) { person++; } var option = new Dictionary <string, object>() { { "spd", settings.Speed }, // 语速 { "pit", settings.Pitch }, // 语调 { "vol", settings.Volume }, // 音量 { "per", person } // 发音人 }; // Calculate hash var wave = this.GetCacheFileName(text.Replace(Environment.NewLine, "+"), "mp3", option.GetString()); lock (this) { if (!File.Exists(wave)) { var apiKey = settings.ApiKey; var secretKey = settings.SecretKey; if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(secretKey)) { return; } var client = new Baidu.Aip.Speech.Tts(apiKey, secretKey); var result = client.Synthesis(text, option); if (result.Success) { File.WriteAllBytes(wave, result.Data); } } } _plugin.SoundPlayer.Play(wave, playDevice, isSync, volume); }
/// <summary> /// 语音合成,文本转语音 /// </summary> /// <param name="text"></param> /// <param name="spd"></param> /// <param name="pit"></param> /// <returns></returns> public static async Task <SpeechResult> Tts(string text, int spd, int pit) { SpeechResult result = new SpeechResult(); //var APP_ID = "14965195"; //var API_KEY = "R2qXXgwr9xKtge3kxU5U7up2"; //var SECRET_KEY = "Gnm2KhHcgZEDDLwy0Qtl66y4fFc8FmTj"; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 // 可选参数 var option = new Dictionary <string, object>() { { "spd", spd }, // 语速 { "vol", 7 }, // 音量 { "pit", pit }, // 语调 { "per", 0 }, // 发音人,0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声 { "aue", 3 } }; var res = Task.Run(() => { var res1 = client.Synthesis(text, option); if (res1.ErrorCode == 0) // 或 result.Success { //成功数据 result.status = 0; result.speech = res1.Data; //File.WriteAllBytes("合成的语音文件本地存储地址.mp3", result.Data); } else { //失败数据 result.status = 1; result.error = "错误:" + res1.ErrorCode.ToString(); } }); await res; return(result); }
/// <summary> /// 语音播放接口 /// </summary> /// <param name="obj"></param> private void VoiceBroadcast(object obj) { try { string strAllUserInfo = obj as string; var client = new Baidu.Aip.Speech.Tts(voiceAppKey, voiceSshKey); var options = new Dictionary <string, object>() { { "spd", 5 }, //语速 { "vol", 7 }, //音量 { "per", 4 } //语音人 }; var ret = client.Synthesis(strAllUserInfo, options); if (ret.ErrorCode == 0) { this.BeginInvoke(new Action(() => { File.WriteAllBytes("temp.mp3", ret.Data); mciSendString("open temp.mp3" + " alias temp_alias", null, 0, IntPtr.Zero); mciSendString("play temp_alias", null, 0, IntPtr.Zero); StringBuilder strRet = new StringBuilder(64); do { mciSendString("status temp_alias mode", strRet, 64, IntPtr.Zero); } while (!strRet.ToString().Contains("stopped")); mciSendString("close temp_alias", null, 0, IntPtr.Zero); })); } else { MessageBox.Show(ret.ErrorCode.ToString() + ret.ErrorMsg); } } catch (Exception exp) { MessageBox.Show("合成失败,异常信息:" + exp.Message); } }
public ActionResult Test() { var API_KEY = "2AakWsz5sF1Mj9gKhWqqjLMG"; var SECRET_KEY = "mTqOIslrK5nIFmcsvPPtXnRgyYY0yFGI"; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); var option = new Dictionary <string, object>() { { "spd", 5 }, // 语速 { "vol", 7 }, // 音量 { "per", 4 } // 发音人,4:情感度丫丫童声 }; var result = client.Synthesis("众里寻他千百度", option); if (result.Success) // 或 result.Success { System.IO.File.WriteAllBytes(_hostingEnvironment.WebRootPath + "tt.mp3", result.Data); } return(null); }
public void StartVoice(string msg) { var thread = new Thread(() => { string strFullFileName = "baidu.mp3"; var APP_ID = "11718615"; var API_KEY = "Nmp8VnnFcS1F2NhI3pxLyGGi"; var SECRET_KEY = "0AKxk3FoevGp8GbIQ2v2r5GnuiIKxCWV"; try { var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); var result = client.Synthesis(msg); if (result.Success) { var VoiceData = result.Data; using (Stream stream = new MemoryStream(VoiceData)) { using (FileStream fs = new FileStream(strFullFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { stream.CopyTo(fs); stream.Close(); } PlayWait(strFullFileName); } } } catch (Exception ex) { throw; } }); //线程必须为单线程 thread.SetApartmentState(ApartmentState.STA); thread.IsBackground = true; thread.Start(); }
/// <summary> /// 发送语音信息并将合成后的语音文件存储到指定位置 /// </summary> /// <param name="text">文本信息</param> /// <param name="option">配置信息</param> /// <param name="fileName">存储路径</param> /// <returns></returns> internal bool Send(string text, Dictionary <string, object> option, string fileName) { // 设置APPID/AK/SK //var APP_ID = "11339468"; //"你的 App ID"; var API_KEY = System.Configuration.ConfigurationManager.AppSettings["API_KEY"]; //"你的 Api Key"; var SECRET_KEY = System.Configuration.ConfigurationManager.AppSettings["SECRET_KEY"]; //"你的 Secret Key"; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); var result = client.Synthesis(text, option); if (result.ErrorCode == 0) // 或 result.Success { System.IO.File.WriteAllBytes(fileName, result.Data); return(true); } return(false); }
// 合成 public void Tts(string content) { var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 // 可选参数 var option = new Dictionary <string, object>() { { "spd", speed }, // 语速 { "vol", volume }, // 音量 { "per", style } // 发音人,4:情感度丫丫童声 }; var result = client.Synthesis(content, option); if (result.ErrorCode == 0) // 或 result.Success { File.WriteAllBytes(mp3id + ".mp3", result.Data); } wplayer.URL = mp3id + ".mp3"; mp3id += 1; wplayer.controls.play(); }
public static bool SpeechSynthesis(string fileName, string readContent) { bool isSuccess = false; try { var _ttsClient = new Baidu.Aip.Speech.Tts(StringResource.BaiduSpeechAPIKey, StringResource.BaiduSpeechSecretKey); _ttsClient.Timeout = 5000; // 修改超时时间 //tex String 合成的文本,使用UTF - 8编码, //cuid String 用户唯一标识,用来区分用户, //spd String 语速,取值0 - 9,默认为5中语速 否 //pit String 音调,取值0 - 9,默认为5中语调 否 //vol String 音量,取值0 - 15,默认为5中音量 否 //per String 发音人选择, 0为女声,1为男声,3为情感合成 - 度逍遥,4为情感合成 - 度丫丫 var option = new Dictionary <string, object>() { { "spd", 3 }, // 语速 { "vol", 13 }, // 音量 { "pit", 4 }, // 音调 { "per", 0 } // 发音人,4:情感度丫丫童声 }; var result = _ttsClient.Synthesis(readContent, option); if (result.ErrorCode == 0) // 或 result.Success { File.WriteAllBytes(string.Format("wwwroot/audio/{0}.mp3", fileName), result.Data); isSuccess = true; } } catch (Exception) { throw; } return(isSuccess); }
private void Form1_Load(object sender, EventArgs e) { comboBox2.Items.Add("pcm"); comboBox2.Items.Add("wav"); comboBox2.Items.Add("amr"); client = new Baidu.Aip.Speech.Asr(apiKey, apiSecret); client.Timeout = 60000; ttsClient = new Baidu.Aip.Speech.Tts(apiKey, apiSecret); ttsClient.Timeout = 60000; for (int i = 0; i <= 9; i++) { comboBox1.Items.Add(i); comboBox3.Items.Add(i); } for (int i = 0; i <= 4; i++) { comboBox4.Items.Add(i); } comboBox1.SelectedItem = 0; comboBox3.SelectedItem = 0; comboBox4.SelectedItem = 0; }
/// <summary> /// 语音合成 /// </summary> /// <param name="text">合成文本(合成文本长度必须小于1024字节,如果本文长度较长)</param> /// <param name="outFilePath">输出MP3文件名,不加后缀</param> public void TtsAndPlaySound(string text, int speed = 5, int volume = 5, int voices = 0, string outFileName = null) { var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); // 可选参数 var option = new Dictionary <string, object>() { { "spd", speed }, // 语速 { "vol", volume }, // 音量 { "per", voices } // 发音人,4:情感度丫丫童声 }; var result = client.Synthesis(text, option); string filePath = null; if (result.ErrorCode == 0) // 或 result.Success { if (null == outFileName) { //如果合成的文本长于6个字符,前面6个字符作为文件名,小于6个以文本内容作文件名 filePath = "..\\..\\cache\\" + text.Substring(0, text.Length > 6 ? 6 : text.Length) + ".mp3"; } else { //自定义文件名 filePath = outFileName; filePath = filePath.Substring(0, filePath.Length - 4) + ".mp3"; Console.WriteLine(filePath); } File.WriteAllBytes(filePath, result.Data); } filePath = WpfApp1.AudioFileFormatChange.Mp3ToWav(filePath); if (aoRuiAudio == null) { aoRuiAudio = new WpfApp1.AoRuiAudio(); } aoRuiAudio.PlayWavSound(filePath); }
//合成语音 static int genVoice(string text) { var API_KEY = "cngu1wvCy4edGGoXVjPaoSOG"; var SECRET_KEY = "bGI7m7r0a5k2cAP9DM90yGXHc4BKqaTN"; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 var option = new Dictionary <string, object>() { { "spd", 5 }, // 语速 { "vol", 5 }, // 音量 { "per", 1 } // 发音人,4:情感度丫丫童声 }; //超过百度API512字限制的情况 if (text.Length > 511) { int errCode = 0; decimal cirTime = Math.Ceiling((decimal)text.Length / (decimal)511); //循环的次数 for (int i = 1; i < cirTime; i++) { var result = client.Synthesis(text.Substring((i - 1) * 511, 511), option); if (result.ErrorCode == 0) // 或 result.Success { System.IO.File.WriteAllBytes("tts" + i + ".mp3", result.Data); } errCode += result.ErrorCode; } var result1 = client.Synthesis(text.Substring(((int)cirTime - 1) * 511, text.Length - ((int)cirTime - 1) * 511), option); if (result1.ErrorCode == 0) // 或 result.Success { System.IO.File.WriteAllBytes("tts" + cirTime + ".mp3", result1.Data); } errCode += result1.ErrorCode; //合并MP3参数; string argStr = "-y "; for (int i = 0; i < cirTime; i++) { argStr += "-i tts" + (i + 1).ToString() + ".mp3 "; } argStr += "-filter_complex \""; for (int i = 0; i < cirTime; i++) { argStr += "[" + i + ":0] "; } argStr += "concat=n=" + cirTime.ToString(); argStr += ":v=0:a=1 [a]\" -map [a] tts.mp3"; ProcessStartInfo start = new ProcessStartInfo("ffmpeg"); //设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到 //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe start.Arguments = argStr; //设置命令参数 start.CreateNoWindow = false; //不显示dos命令行窗口 start.RedirectStandardOutput = true; // start.RedirectStandardInput = true; // start.UseShellExecute = false; //是否指定操作系统外壳进程启动程序 Process p = Process.Start(start); p.WaitForExit(); //等待程序执行完退出进程 p.Close(); //关闭进程 return(errCode); } else { var result = client.Synthesis(text, option); if (result.ErrorCode == 0) // 或 result.Success { System.IO.File.WriteAllBytes("tts.mp3", result.Data); } return(result.ErrorCode); } }
/// <summary> /// 下载语音文件 /// </summary> /// <param name="obj"></param> private void DownLoadVoice(object obj) { lock (objLock) { List <IMovieShowList.MovieShow> list = obj as List <IMovieShowList.MovieShow>; //存储下载失败的项 List <IMovieShowList.MovieShow> erroList = new List <IMovieShowList.MovieShow>(); //得到配置文件信息 var spd = setJson.Rate; //npSpd.Value; var vol = setJson.Vol; //npVol.Value; var per = setJson.Per; //npPer.Value; var pit = setJson.Pit; //npPit.Value; var option = new Dictionary <string, object>() { { "spd", spd }, // 语速 { "vol", vol }, // 音量 { "per", per }, // 发音人,4:情感度丫丫童声 { "pit", pit } }; // 设置APPID/AK/SK // var APP_ID = "11339468"; //"你的 App ID"; var API_KEY = System.Configuration.ConfigurationManager.AppSettings["API_KEY"]; //"你的 Api Key"; var SECRET_KEY = System.Configuration.ConfigurationManager.AppSettings["SECRET_KEY"]; //"你的 Secret Key"; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); client.Timeout = 3000; string fomartStr = File.ReadAllText(SetPath.FomartPath); // WriteLog($"{DateTime.Now.ToShortTimeString()}==> 准备开始下载" + Environment.NewLine); foreach (IMovieShowList.MovieShow movie in list) { if (!Directory.Exists(SetPath.voicePath)) { Directory.CreateDirectory(SetPath.voicePath); } if (cts.IsCancellationRequested) { return; } string fileName = SetPath.voicePath + movie.BeginTime.Replace(":", "") + ".mp3"; //下载每一个文件对应的语音包 //得到配置文件下的格式信息 string text = ParseText(fomartStr, movie); try { var result = client.Synthesis(text, option); if (result.Success) // 或 result.code==0 { File.WriteAllBytes(fileName, result.Data); } else { //没有下载成功 Invoke(new Action(() => { erroList.Add(movie); })); } } catch { //出现异常没有下载成功 Invoke(new Action(() => { })); } } //下载完成了,查看有没有下载失败的项 if (erroList.Count > 0) { ResertUpdateEnd = 1; DownLoadVoice(erroList); } else { //下载全部完成 ResertUpdateEnd = 3; } } }
/// <summary> /// 下载语音信息包 /// </summary> /// <param name="list"></param> private void UpdateLoad(List <MovieShow> list) { //存储下载失败的项 List <IMovieShowList.MovieShow> erroList = new List <IMovieShowList.MovieShow>(); //得到配置文件信息 var spd = GetSet().Rate; //npSpd.Value; var vol = GetSet().Vol; //npVol.Value; var per = GetSet().Per; //npPer.Value; var pit = GetSet().Pit; //npPit.Value; var option = new Dictionary <string, object>() { { "spd", spd }, // 语速 { "vol", vol }, // 音量 { "per", per }, // 发音人,4:情感度丫丫童声 { "pit", pit } }; // 设置APPID/AK/SK // var APP_ID = "11339468"; //"你的 App ID"; var API_KEY = System.Configuration.ConfigurationManager.AppSettings["API_KEY"]; //"你的 Api Key"; var SECRET_KEY = System.Configuration.ConfigurationManager.AppSettings["SECRET_KEY"]; //"你的 Secret Key"; var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); client.Timeout = 3000; string fomartStr = File.ReadAllText(SetPath.FomartPath); foreach (IMovieShowList.MovieShow movie in list) { if (!Directory.Exists(SetPath.voicePath)) { Directory.CreateDirectory(SetPath.voicePath); } if (cts.IsCancellationRequested) { return; } string fileName = SetPath.voicePath + movie.BeginTime.Replace(":", "") + ".mp3"; //下载每一个文件对应的语音包 //得到配置文件下的格式信息 string text = ParseText(fomartStr, movie); try { var result = client.Synthesis(text, option); Invoke(new Action(() => { Text = $"正在下载:{movie.Name}"; })); if (result.Success) // 或 result.code==0 { File.WriteAllBytes(fileName, result.Data); Invoke(new Action(() => { blList.Add(movie); })); Invoke(new Action(() => { Text = $"下载:{movie.Name}成功"; })); } else { //没有下载成功 Invoke(new Action(() => { erroList.Add(movie); })); } } catch { //出现异常没有下载成功 try { Invoke(new Action(() => { this.Text = $"下载:{movie.Name}失败"; erroList.Add(movie); })); } catch { } } } //下载完成了,查看有没有下载失败的项 if (erroList.Count > 0) { UpdateLoad(erroList); } else { Invoke(new Action(() => { Text = "影院语音播报"; })); //排下序 Invoke(new Action(() => { blList.OrderByDescending(m => m.BeginTime); UpdateEnd = true; })); } }
private void MainWindow_Loaded(object sender, RoutedEventArgs e) { // 设置APPID/AK/SK _ttsClient = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY); _ttsClient.Timeout = 60000; // 修改超时时间 }