开发者

C#: Google Text-to-speech Loop

I'm creating a simple program that takes a string, sends it to Google's text to speech server, and downloads the text to speech in a mp3/wav file on the computer. I have the code below, but it only works with up to 100 characters (Google's limit). How can I make a loop to cut the string into 100 character parts and then save it in one mp3/wav file on the computer? I know this is possible with javascript and actionscript (as I have seen them) but how can I do this in C#?

using System;
using System.Collections.Generic;
开发者_如何学Pythonusing System.Linq;
using System.Text;
using System.Net;
using System.Threading;

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            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 = "This string cannot be more than 100 characters.";

            encstr = Uri.EscapeDataString(s);

            Console.WriteLine(encstr);

            web.DownloadFile("http://translate.google.com/translate_tts?tl=en&q=" + encstr, ".\\" + filename);
        }
    }
}


This is not a direct answer, but I think the splitting is not good because TTS has word intonation as well as sentence intonation. Instead, I recommend you use SpeechSynthesizer Class with free TTS engine. However, I don't know which TTS engine is good as free and where it is. If finding goodness, I'll post it.


UPDATED

MP3 files are just concatenated without a problem, from this question.

well, before I get to concatenating the mp3 files, how would the while loop look like to first get those mp3 files on the computer? if i go through my loop, the tts.mp3 file would be overwritten and i would be left with only the last 100 character string that was received..

You can merge the two files like the code below. Finally, the fs1 will get all content.

        string tts1 = "tts1.mp3";
        string tts2 = "tts2.mp3";
        FileStream fs1 = null;
        FileStream fs2 = null;
        try
        {
            fs1 = File.Open(tts1, FileMode.Append);
            fs2 = File.Open(tts2, FileMode.Open);
            byte[] fs2Content = new byte[fs2.Length];
            fs2.Read(fs2Content, 0, (int)fs2.Length);
            fs1.Write(fs2Content, 0, (int)fs2.Length);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + " : " + ex.StackTrace);
        }
        finally
        {
            fs1.Close();
            fs2.Close();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜