南阳电信API

TtsHelper.cs 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Speech.AudioFormat;
  6. using System.Speech.Synthesis;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. using CallCenter.Utility.Common;
  12. using SpeechLib;
  13. namespace CallCenter.Utility
  14. {
  15. public class TtsHelper
  16. {
  17. public static string GetAudioPath(string text)
  18. {
  19. if (!string.IsNullOrEmpty(text))
  20. {
  21. string filename = CommonHelper.MD5(text) + ".wav";
  22. string path = HttpContext.Current.Server.MapPath("..") + "\\Audio\\";
  23. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  24. string pathfile = path + filename;
  25. if (!File.Exists(pathfile))
  26. {
  27. File.Create(pathfile).Dispose();
  28. Task.Factory.StartNew(() =>
  29. {
  30. SpeechSynthesizer synth = new SpeechSynthesizer();
  31. SpeechAudioFormatInfo wavFromat = new SpeechAudioFormatInfo(EncodingFormat.Pcm, 1024 * 11, 16, 1, 16000, 2, null);
  32. synth.SetOutputToWaveFile(pathfile, wavFromat);
  33. synth.Speak(text);
  34. synth.SetOutputToNull();
  35. });
  36. }
  37. return pathfile;
  38. }
  39. else
  40. {
  41. return "";
  42. }
  43. }
  44. public static string GetAudioPath1(string text)
  45. {
  46. if (!string.IsNullOrEmpty(text))
  47. {
  48. string filename = CommonHelper.MD5(text) + ".wav";
  49. string path = HttpContext.Current.Server.MapPath("..") + "\\Audio\\";
  50. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  51. string pathfile = path + filename;
  52. if (!File.Exists(pathfile))
  53. {
  54. SpFileStream stream = new SpFileStream();
  55. stream.Open(pathfile, SpeechStreamFileMode.SSFMCreateForWrite, false);
  56. SpVoice sv = new SpVoice();
  57. sv.AudioOutputStream = stream;
  58. sv.Speak(text);
  59. sv.WaitUntilDone(Timeout.Infinite);
  60. stream.Close();
  61. }
  62. return pathfile;
  63. }
  64. else
  65. {
  66. return "";
  67. }
  68. }
  69. }
  70. }