StableVersion4.3/HL_FristAidPlatform_Frame/SpeechLisen.cs

138 lines
5.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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");
//}
/// <summary>
/// 识别本地文件
/// </summary>
//public void AsrData()
//{
// var data = File.ReadAllBytes("语音pcm文件地址");
// var result = _asrClient.Recognize(data, "pcm", 16000);
// Console.Write(result);
//}
/// <summary>
/// 识别URL中的语音文件
/// </summary>
//public void AsrUrl()
//{
// var result = _asrClient.Recoginze(
// "http://xxx.com/待识别的pcm文件地址",
// "http://xxx.com/识别结果回调地址",
// "pcm",
// 16000);
// Console.WriteLine(result);
//}
//List<string> listdata = new List<string>();
/// <summary>
/// 语音文件的生成
/// </summary>
/// <param name="str">需要生成的语音字符</param>
/// <returns></returns>
//public string Tts(string str)
//{
// lock (this)
// {
// //音频文件生成路径和名称
// string pathwave = AppDomain.CurrentDomain.BaseDirectory + @"notice.mp3";
// // 可选参数
// var option = new Dictionary<string, object>()
// {
// { "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 "";
// }
//}
/// <summary>
/// MP3文件转WAV
/// </summary>
/// <param name="mp3File">MP3文件路径</param>
/// <param name="outputFile">WAV文件输出目录</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 转换成Wav文件
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
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;
}
}
}