Author Topic: CoquiTTS / MozillaTTS  (Read 2870 times)

hunanbean

  • Newbie
  • *
  • Posts: 1
CoquiTTS / MozillaTTS
« on: March 13, 2023, 01:38:07 AM »
CoquiTTS is the continuation of MozillaTTS, AFAIK. They are open source projects. They have voices that take TTS to a new level, and the ability to CUDA capable GPUs  for the processing. Since these are not SAPI compliant, there is no good way to address these voices from VoiceAttack. One thing that both TTS projects share is that you can run them as a local server and go to http://localhost:5002 to type away and hear the awesome synthesized voices. I wanted to be able to, somehow, get VA to speak using these new voices. The solution I came up with was to use the built in function of VA that can Run a program with Arguments. So i wrote a small and simple command line program that you can pass text to, and it will send it to the Local(Offline)Server and then play the synthesized voice. If you setup your local TTS to use the GPU for inference, this whole thing is basically instantaneous. So, this is a 'formal' request to add support for these very cool Localized TTS solutions. Until then, if you've got your TTS setup with either of these, this Source Code will make a tiny program that does what is described. It is in c# and has only one requirement (aside from the default boilerplate) which is NAudio.
Code: [Select]
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using NAudio.Wave;

namespace speakthis
{
    class Program
    {
        static async Task Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Please provide a text to synthesize.");
                return;
            }

            string text = string.Join(" ", args);
            string url = "http://localhost:5002/api/tts?text=" + Uri.EscapeDataString(text);

            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.GetAsync(url))
                {
                    using (Stream audioStream = await response.Content.ReadAsStreamAsync())
                    {
                        using (WaveStream waveStream = new WaveFileReader(audioStream))
                        {
                            using (WaveOut waveOut = new WaveOut())
                            {
                                waveOut.Init(waveStream);
                                waveOut.Play();

                                await Task.Run(() =>
                                {
                                    while (waveOut.PlaybackState == PlaybackState.Playing)
                                    {
                                        Task.Delay(100).Wait();
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }
    }
}

The 5002 port is the default, as you saw when your set up CoquiTTS.
Of course, have your local server started.

So, this will let you use the VA command 'Run Application' with an argument of whatever Text you want.