using NAudio.Wave; using System; using System.Collections.Generic; using System.IO; using System.Media; namespace HL_FristAidPlatform_Frame { public class SpeechLisen { //调用代码 new Lisen().Tts(str); //private readonly Asr _asrClient; //private readonly Tts _ttsClient; //App ID: 19744948 //API Key: MlUf8KEUgpoFthdbesgUQGab //Secret Key: vQWEIymCVP189imqu53rynlypZLiZRGX //public SpeechLisen() //{ // _asrClient = new Asr("19744948", "MlUf8KEUgpoFthdbesgUQGab", "vQWEIymCVP189imqu53rynlypZLiZRGX"); // _ttsClient = new Tts("MlUf8KEUgpoFthdbesgUQGab", "vQWEIymCVP189imqu53rynlypZLiZRGX"); //} /// /// 识别本地文件 /// //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); //} //List listdata = new List(); /// /// 语音文件的生成 /// /// 需要生成的语音字符 /// //public string Tts(string str) //{ // lock (this) // { // //音频文件生成路径和名称 // string pathwave = AppDomain.CurrentDomain.BaseDirectory + @"notice.mp3"; // // 可选参数 // var option = new Dictionary() // { // { "spd", 5},//语速,取值0-9,默认为5中语速 // { "vol", 10},//音量,取值0-9,默认为5中音量 // { "per", 2}//发音人,0:女声,1:男声,3:情感合成(度逍遥),4:情感合成(度YY) // }; // var result = _ttsClient.Synthesis(str, option); // if (result.ErrorCode == 0) // 或 result.Success // { // File.WriteAllBytes(pathwave, result.Data); // var mp3FileName = pathwave; // var outputFileName = mp3FileName.Substring(0, mp3FileName.Length - 3) + "wav"; // Mp3ToWav(mp3FileName, outputFileName);//mp3转wav文件 // SoundPlayer player = new SoundPlayer();//进行语音文件播放 // player.SoundLocation = outputFileName; // player.Load(); //同步加载声音 // player.Play(); //启用新线程播放 // } // return ""; // } //} /// /// MP3文件转WAV /// /// MP3文件路径 /// WAV文件输出目录 /// public static string Mp3ToWav(string mp3File, string outputFile) { using (Mp3FileReader reader = new Mp3FileReader(mp3File)) { var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)) { WaveFileWriter.CreateWaveFile(outputFile, conversionStream); } return outputFile; } } /// /// 转换成Wav文件 /// /// /// public static string Convert2Wav(string filePath) { string directoryName = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); string tempDir = directoryName + "\\temp" + DateTime.Now.ToString("yyyyMMddHHmmss") + "\\"; if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } if (filePath.EndsWith(".wav", StringComparison.CurrentCultureIgnoreCase)) { using (var reader = new WaveFileReader(filePath)) { var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)) { WaveFileWriter.CreateWaveFile(tempDir + fileName, conversionStream); } } } else if (filePath.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase)) { using (Mp3FileReader reader = new Mp3FileReader(filePath)) { var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)) { WaveFileWriter.CreateWaveFile(tempDir + fileName, conversionStream); } } } return tempDir + fileName; } } }