C# repeating text to speech synthesis

322 views Asked by At

So I am trying to write a text to speech program that will progressively get faster, leaving less of a gap between sentences and eventually over layering and running multiple of the commands at the same time so it becomes just a mess of noise. Its currently a console application and I have the relevant references included

Any ideas how I adapt this to run each speak command as its own instance. Would I have to re-learn how to multithread to get it to work?

Any help would be great, at the minute it loops (the number of iterations is not too important) and I have tried to get less of a pause after each one but cannot get one speak command to layer over the pervious.

for (int i = 0; i < 100; i++)
            {
                if (Console.KeyAvailable == true)
                {
                    break;
                }
                else
                {
                    if (i == 0)
                    {
                        string commandLine2 = "Hello darkness my old friend";
                        SpeechSynthesizer s = new SpeechSynthesizer();
                        s.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Child);
                        s.Speak(commandLine2);
                        commandLine2 = "Its been a while where should we begin";
                        //Thread.Sleep(1000);
                        s.Speak(commandLine2);
                    }
                    else
                    {
                        string commandLine2 = "Hello darkness my old friend";
                    SpeechSynthesizer s = new SpeechSynthesizer();
                    s.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Child);
                    s.Speak(commandLine2);
                    commandLine2 = "Its been a while where should we begin";
                    //Thread.Sleep(1000 / i);
                    s.Speak(commandLine2);
                }

            }
        }
1

There are 1 answers

0
PKYNI On

I just used multithreading in the end. It all came rushing back to me

 for (int i = 1; i < 100; i++)
        {
            Thread t1 = new Thread(mySpeach);
            t1.Name = "Thread1";
            t1.Start();
            Thread.Sleep(2000 / i);
            if (Console.KeyAvailable == true)
            {
                t1.Abort();
                break;
            }
        }

//other methods were here


    public static void typing()
        {
        string a = "Hello darkness my old friend\nIts been a while where should we begin";
        for (int i = 0; i < a.Length; i++)
        {
            Random rnd = new Random();
            Console.Write(a[i]);
            if (Console.KeyAvailable == true)
            {
                break;
            }
            Thread.Sleep(rnd.Next(50, 100));
        }
        Console.WriteLine("");
    }

    public static void mySpeach()
    {
        string commandLine2 = "Hello darkness my old friend";
        Thread t2 = new Thread(typing);

        t2.Name = "Thread2";

        t2.Start();
        SpeechSynthesizer s = new SpeechSynthesizer();
        s.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Child);
        s.Speak(commandLine2);
        commandLine2 = "Its been a while where should we begin";
        if (Console.KeyAvailable == true)
        {
            return;
        }
        s.Speak(commandLine2);
        Thread.Sleep(1000);
    }