终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

150 line
5.6 KiB

  1. using Microsoft.CognitiveServices.Speech;
  2. using Microsoft.CognitiveServices.Speech.Audio;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Collections.Concurrent;
  10. using System.Threading;
  11. using System.Windows.Documents;
  12. namespace WpfApp1
  13. {
  14. public class Speech
  15. {
  16. public static uint SND_ASYNC = 0x0001;
  17. public static uint SND_FILENAME = 0x0002;
  18. [DllImport("winmm.dll", CharSet = CharSet.Auto)]
  19. public static extern int mciSendString(string m_strCmd, string m_strReceive, int m_v1, int m_v2);
  20. [DllImport("Kernel32", CharSet = CharSet.Auto)]
  21. public static extern Int32 GetShortPathName(string path, StringBuilder shortPath, Int32 shortPathLength);
  22. private volatile static Speech _Instance;
  23. public static Speech GetInstance => _Instance ?? (_Instance = new Speech());
  24. private Speech()
  25. {
  26. //Task.Factory.StartNew(() =>
  27. //{
  28. // while (true)
  29. // {
  30. // while (speechInfos.Count > 0)
  31. // {
  32. // if (speechInfos.TryDequeue(out SpeechInfo si))
  33. // {
  34. // for (int i = 0; i < si.Count; i++)
  35. // {
  36. // Speak(si.Text);
  37. // Thread.Sleep(si.time);
  38. // }
  39. // }
  40. // }
  41. // Thread.Sleep(1000);
  42. // }
  43. //});
  44. }
  45. private ConcurrentQueue<SpeechInfo> speechInfos = new ConcurrentQueue<SpeechInfo>();
  46. public void SpeechSpeak(SpeechInfo si)
  47. {
  48. speechInfos.Enqueue(si);
  49. }
  50. private void OutputSpeechSynthesisResult(SpeechSynthesisResult speechSynthesisResult, string text)
  51. {
  52. switch (speechSynthesisResult.Reason)
  53. {
  54. case ResultReason.SynthesizingAudioCompleted:
  55. Console.WriteLine($"Speech synthesized for text: [{text}]");
  56. break;
  57. case ResultReason.Canceled:
  58. var cancellation = SpeechSynthesisCancellationDetails.FromResult(speechSynthesisResult);
  59. Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
  60. if (cancellation.Reason == CancellationReason.Error)
  61. {
  62. Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
  63. Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
  64. Console.WriteLine($"CANCELED: Did you set the speech resource key and region values?");
  65. }
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. public async void aa(string text)
  72. {
  73. var speechConfig = SpeechConfig.FromSubscription("b88f6907c8f64075a6169dc4d7dff65a", "chinanorth2");
  74. // The language of the voice that speaks.
  75. speechConfig.SpeechSynthesisVoiceName = "zh-CN";
  76. using (var speechSynthesizer = new SpeechSynthesizer(speechConfig))
  77. {
  78. // Get text from the console and synthesize to the default speaker.
  79. Console.WriteLine("Enter some text that you want to speak >");
  80. //string text = Console.ReadLine();
  81. var speechSynthesisResult = await speechSynthesizer.SpeakTextAsync(text);
  82. OutputSpeechSynthesisResult(speechSynthesisResult, text);
  83. }
  84. Console.WriteLine("Press any key to exit...");
  85. }
  86. /// <summary>
  87. /// 播放语音
  88. /// </summary>
  89. /// <param name="SpeakText"></param>
  90. public void Speak(string SpeakText)
  91. {
  92. string path = $"{AppDomain.CurrentDomain.BaseDirectory}Sound";//设置音频文件保存路径
  93. Directory.CreateDirectory(path);
  94. string SpeakPath = $"{path}\\{SpeakText}.wav";
  95. SpeakPath = SpeakPath.Replace(' ', '_');
  96. if (!File.Exists(SpeakPath))
  97. {
  98. var res = SynthesizeAudioAsync(SpeakText, SpeakPath);
  99. }
  100. StringBuilder shortPath = new StringBuilder(200);
  101. int result = GetShortPathName(SpeakPath, shortPath, shortPath.Capacity);
  102. string shortName = shortPath.ToString();
  103. mciSendString(@"close all", null, 0, 0);
  104. mciSendString(@"open " + shortName + " alias song", null, 0, 0);
  105. mciSendString(@"play song", null, 0, 0);
  106. //return "22";
  107. }
  108. /// <summary>
  109. /// 文字转音频
  110. /// </summary>
  111. /// <param name="text"></param>
  112. /// <returns></returns>
  113. private string SynthesizeAudioAsync(string text, string path)
  114. {
  115. var config = SpeechConfig.FromSubscription("b88f6907c8f64075a6169dc4d7dff65a", "chinanorth2");
  116. config.SpeechSynthesisLanguage = "zh-CN"; //config.SpeechSynthesisVoiceName = "";
  117. using var audioConfig = AudioConfig.FromWavFileOutput(path);
  118. using var synthesizer = new SpeechSynthesizer(config, audioConfig);
  119. synthesizer.SpeakTextAsync(text).Wait();
  120. //await Task.Delay(TimeSpan.FromSeconds(2));
  121. return "1";
  122. }
  123. }
  124. public class SpeechInfo
  125. {
  126. public string Text { get; set; }
  127. public int Count { get; set; } = 1;
  128. public int time { get; set; } = 2000;
  129. }
  130. }