csharp:Google TTS API text to speech

时间:2014-04-27 23:51:43   收藏:0   阅读:1188
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;
using NAudio.Wave;//http://naudio.codeplex.com/
using NAudio.CoreAudioApi;
using System.Web;
using System.Media;
using SpeechLib;//NET2.0 引用 Speech sdk 5.1 在COM选项卡里面的Microsoft Speech  object  library引用 已经有11.0版本
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
 
 
namespace Speech
{
    /// <summary>
    /// Csharp: Google TTS API 文本语音读取
    ///  涂聚文
    /// </summary>
    public partial class Form2 : Form
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="FileName"></param>
        public void PlaySound(string FileName)
        {
            //要加载COM组件:Microsoft speech object Library
            if (!System.IO.File.Exists(FileName))
            {
                return;
            }
 
            SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
            SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
            spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
            SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
            pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
            spFs.Close();
        }
        /// <summary>
        ///
        /// </summary>
        public Form2()
        {
            InitializeComponent();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = "中华人民共和国";
            //ok
            WebClient web = new WebClient();
            web.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)");
 
            string encstr = string.Empty;
 
            string filename = "tts.mp3"; //could also be tts.wav
 
            string s = "中华人民共和国涂聚文投诉";
 
            encstr = Uri.EscapeDataString(s);
 
            Console.WriteLine(encstr);
 
            web.DownloadFile("http://translate.google.com/translate_tts?ie=UTF-8&tl=zh-cn&q=" + encstr, ".\\" + filename);
 
            //PlaySound(Application.StartupPath+"\\"+filename);
            //SoundPlayer sp = new SoundPlayer();
            //sp.SoundLocation = Application.StartupPath + "\\" + filename;
            //sp.LoadAsync();
            //sp.PlaySync();
            //sp.PlayLooping();
            //Device dv = new Device();
            //SecondaryBuffer buf = new SecondaryBuffer(Application.StartupPath + "\\" + filename, dv);
            //buf.Play(0, BufferPlayFlags.Looping);
            this.axWindowsMediaPlayer1.URL = Application.StartupPath + "\\" + filename;
        }
        bool waiting = false;
        AutoResetEvent stop = new AutoResetEvent(false);
        /// <summary>
        /// 英文可以,中文不行
        /// </summary>
        /// <param name="url"></param>
        public void PlayMp3FromUrl(string url)
        {
            try
            {
                url = HttpUtility.UrlDecode(url);
                using (Stream ms = new MemoryStream())
                {
                    using (Stream stream = WebRequest.Create(url).GetResponse().GetResponseStream())//HttpUtility.UrlDecode(
                    {
                        //UTF8Encoding encoding = new UTF8Encoding();
                        //byte[] buffer = encoding.GetBytes(url);
                        //stream.Write(buffer, 0, buffer.Length);
                        //stream.Close();
 
 
                        byte[] buffer = new byte[32768];//32768
                        int read;
                        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        //using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                        //{
                        //    writer.Write(url);
                        //}
 
                    }
                    //using (Stream stream = request.GetRequestStream())
                    //using (StreamWriter writer = new StreamWriter(requestStream, Encoding.UTF8))
                    //{
                    //    writer.Write(url);
                    //}
                    ms.Position = 0;
                    using (WaveStream blockAlignedStream =
                        new BlockAlignReductionStream(
                            WaveFormatConversionStream.CreatePcmStream(
                                new Mp3FileReader(ms))))
                    {
                        using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                        {
                            waveOut.Init(blockAlignedStream);
                            waveOut.PlaybackStopped += (sender, e) =>
                            {
                                waveOut.Stop();
                            };
 
                            waveOut.Play();
                            waiting = true;
                            stop.WaitOne(10000);
                            waiting = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
             
             
            //var playThread = new Thread(() => PlayMp3FromUrl("http://translate.google.com/translate_tts?tl=en&q=" + HttpUtility.UrlEncode(this.textBox1.Text)));
            string str = HttpUtility.UrlEncode(this.textBox1.Text);
            var playThread = new Thread(() => PlayMp3FromUrl("http://translate.google.com/translate_tts?ie=UTF-8&tl=zh-cn&q=" + str));// HttpUtility.UrlEncode(
            playThread.IsBackground = true;
            playThread.Start();
        }
    }
}

 

csharp:Google TTS API text to speech,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!