using System; using System.Collections.Generic; using System.IO; using Baidu.Aip.Speech;
namespaceBaidu.Aip.Demo { classSpeechDemo { privatereadonly Asr _asrClient; privatereadonly Tts _ttsClient;
publicSpeechDemo() { _asrClient = new Asr("Api Key", "Secret Key"); _ttsClient = new Tts("Api Key", "Secret Key"); }
// 识别本地文件 publicvoidAsrData() { var data = File.ReadAllBytes("语音pcm文件地址"); var result = _asrClient.Recognize(data, "pcm", 16000); Console.Write(result); }
// 识别URL中的语音文件 publicvoidAsrUrl() { var result = _asrClient.Recoginze( "http://xxx.com/待识别的pcm文件地址", "http://xxx.com/识别结果回调地址", "pcm", 16000); Console.WriteLine(result); }
// 合成 publicvoidTts() { // 可选参数 var option = new Dictionary<string, object>() { {"spd", 5}, // 语速 {"vol", 7}, // 音量 {"per", 4} // 发音人,4:情感度丫丫童声 }; var result = _ttsClient.Synthesis("众里寻他千百度", option);
using System.Collections.Generic; using System.IO; using Baidu.Aip.Speech;
namespaceBaiduSpeechDemo { staticclassSpeechHelper { privatestaticreadonly Asr AsrClient; privatestaticreadonly Tts TtsClient;
staticSpeechHelper() { AsrClient = new Asr("BWf8AWrvS5h6Y45NAOP3zaGp", "490737eca7a6ff4d20375d1696c7e548"); TtsClient = new Tts("BWf8AWrvS5h6Y45NAOP3zaGp", "490737eca7a6ff4d20375d1696c7e548"); }
// 识别本地文件 publicstatic AsrResult AsrData(string path) { var data = File.ReadAllBytes(path); var result = AsrClient.Recognize(data, "pcm", 8000); return result.ToObject<AsrResult>(); }
// 发言人 var per = cbPer.SelectedIndex >= 2 ? cbPer.SelectedIndex + 1 : cbPer.SelectedIndex;
//调用Baidu TTS Api if (!SpeechHelper.Tts(tbContext.Text, musicPath, (int)nudSpd.Value, (int)nudPit.Value, (int)nudVol.Value, per)) return;
//播放请求得到的结果 IWavePlayer waveOutDevice = new WaveOut(); var audioFileReader = new AudioFileReader(musicPath); waveOutDevice.Init(audioFileReader); waveOutDevice.Play();