IVONA For Developers

Develop with IVONA Text-to-Speech.

5. Synthesis in multiple threads

<< Using text normalization


C# PHP Python

Download source code

Introduction

IVONA Speech Cloud is an infrastructure designed by IVONA Software which deals withy multiple requests in multithreading manner. Thus user of Speech Cloud solution can request for multiple synthesis asynchronously and does not have to deal with handling threads management.

Prepare asynchronous method

First we need to prepare asynchronous method of generating IVONA Speech Cloud requests. This method should look like this:

/// This function creates a new sound file from text.
///
 
///Ivona Web Service
///User data
///Text data
private static void TTSAsync(IVONATTSSaaS webService, User u, TextData td)
{
    string token = webService.getToken(u.UserName);
    webService.createSpeechFileCompleted += new createSpeechFileCompletedEventHandler(FileCreatedCallback);
    webService.createSpeechFileAsync(token, User.GetMd5Hash(u.apiKey + token), td.text, td.contentType,
                                               td.defaultVoice, td.codecID, td.additionaParams.ToArray());
}

Asynchronous method needs also a callback method that will be called when file is being created and is ready for streaming. This method will look like this:

///
<summary> /// This function is called when the synthesis is completed. /// </summary>
 
private static void FileCreatedCallback(object sender, createSpeechFileCompletedEventArgs e)
{
    System.Console.WriteLine("Your file is placed right here:\n" + e.soundUrl);
    System.Console.WriteLine("Download price: " + e.charactersPrice);
}

Multiple requests

With already implemented asynchronuous requests, we need to use this function in our main program:

if (webService.checkToken(token, User.GetMd5Hash(u.apiKey + token)) == 1)
{
    foreach (TextData td in ArgsParser.parsedArgs)
    {
        // run synthesis in synchronous or asynchronous mode
        if (!ArgsParser.multithread)
            TTSSimple(webService, u, td);
        else
            TTSAsync(webService, u, td);
    }
}

For each text passed as parameter to the program, we will generate next synthesis request and handle it in incoming callback methods

Check result

Now it is time to check if our program is working correctly. Next lets run sample with following command than:

[Tutorial] --text "Hello world 1" "text/plain" --text "Hello world 2" "text/plain" --text "Hello world 3" "text/plain" --multithread

Command line above will request for synthesis of every text and write on screen name of file to be downloaded/streamed from Speech Cloud.

Conclusion

In above paragraphs we showed how to asynchronously request for multiple synthesis from IVONA Speech Cloud

Complete example

Download source code