| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Speech.AudioFormat;
- using System.Speech.Synthesis;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web;
- using CallCenter.Utility.Common;
- using SpeechLib;
- namespace CallCenter.Utility
- {
- public class TtsHelper
- {
- public static string GetAudioPath(string text, string name = "")
- {
- if (!string.IsNullOrEmpty(text))
- {
- name = string.IsNullOrEmpty(name) ? CommonHelper.MD5(text) : name + "-" + CommonHelper.MD5(text);
- string filename = name + ".wav";
- string path = HttpContext.Current.Server.MapPath("..") + "\\Audio\\";
- if (!Directory.Exists(path)) Directory.CreateDirectory(path);
- string pathfile = path + filename;
- if (!File.Exists(pathfile))
- {
- File.Create(pathfile).Dispose();
- Task.Factory.StartNew(() =>
- {
- SpeechSynthesizer synth = new SpeechSynthesizer();
- SpeechAudioFormatInfo wavFromat = new SpeechAudioFormatInfo(EncodingFormat.Pcm, 1024 * 11, 16, 1, 16000, 2, null);
- synth.SetOutputToWaveFile(pathfile, wavFromat);
- synth.Speak(text);
- synth.SetOutputToNull();
- });
- }
- return pathfile;
- }
- else
- {
- return "";
- }
- }
- public static string GetAudioPath1(string text)
- {
- if (!string.IsNullOrEmpty(text))
- {
- string filename = CommonHelper.MD5(text) + ".wav";
- string path = HttpContext.Current.Server.MapPath("..") + "\\Audio\\";
- if (!Directory.Exists(path)) Directory.CreateDirectory(path);
- string pathfile = path + filename;
- if (!File.Exists(pathfile))
- {
- SpFileStream stream = new SpFileStream();
- stream.Open(pathfile, SpeechStreamFileMode.SSFMCreateForWrite, false);
- SpVoice sv = new SpVoice();
- sv.AudioOutputStream = stream;
- sv.Speak(text);
- sv.WaitUntilDone(Timeout.Infinite);
- stream.Close();
- }
- return pathfile;
- }
- else
- {
- return "";
- }
- }
- }
- }
|