0

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;
using 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);
        }
    }
}
Charles
  • 50,943
  • 13
  • 104
  • 142
Syam
  • 13
  • 1
  • 4
  • First, check Google's TOS; I'm not sure such a loop would be allowed, or in fact this kind of request at all. Also, each word does have an effect on words before or after it; if you split a string in any way except after a full-stop (or question mark, etc.) you could change the way it sounds. – configurator Jul 14 '11 at 18:52
  • Why not use a real TTS engine? See Microsoft's Speech API http://www.codeproject.com/KB/audio-video/TTSinVBpackage.aspx – configurator Jul 14 '11 at 18:53
  • @configurator I understand the legality issue but I have seen this done in actionscript such as here: [http://peteshand.net/blog/index.php/actionscript-text-to-speech/](http://peteshand.net/blog/index.php/actionscript-text-to-speech/) I wanted to know if this could be done in C#. Microsoft's Speech API doesnt sound natural/good. Their Bing Speech API offers something similar to Google but again is limited to 500 characters so I run into the same problem again... :( – Syam Jul 14 '11 at 18:59

1 Answers1

0

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();
        }
Community
  • 1
  • 1
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45
  • Yes, it does have intonation. However, I should have clarified this in my original question, I do know how to use the SpeechSynthesizer Class in C# as I have used this before. I wanted to know how the loop would look like if I were to split the string in 100 character intervals and create a single mp3/wav file on the computer (regardless of intonation) from Google. The same for Microsoft's Bing translator but at 500 character intervals. – Syam Jul 14 '11 at 19:23
  • I see... so, your problem is that mp3 files concatenate together? if so, you can just concatenate them toghether without a problem. http://stackoverflow.com/questions/1160888/how-do-i-merge-join-mp3-files-with-c – Jin-Wook Chung Jul 14 '11 at 19:38
  • 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.. – Syam Jul 14 '11 at 20:10