Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speak milliseconds of silence in C# using SpVoice?

Tags:

c#

How to actually speak silence for X # of milliseconds and not by using Thread.Sleep(). I'm trying to use the .Speak() function in the SpeechLib library of an SpVoice variable to speak a specific duration of silence according to a specified number of milliseconds. Particularly, in the output of a .wav file wherein I am inserting periods of silence between spoken lines of text. Using Thread.Sleep() will take an obscene amount of time to either speak or save, as I am planning to save nearly 5000 lines of spoken text to .wav with pauses in between the lines.

This is the solution I have so far:

        int pauseA = (int)(22050.0 * ((double)pauseTargetToSource.Value / 1000.0) * 2.0);
        int pauseB = (int)(22050.0 * ((double)pauseLineBreak.Value / 1000.0) * 2.0);
        while (
            (lineSource = srSource.ReadLine()) != null &&
            (lineTarget = srTarget.ReadLine()) != null)
        {
            voiceSource.Speak(lineSource, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            voiceSource.WaitUntilDone(Timeout.Infinite);
            voiceSource.AudioOutputStream.Write(new byte[pauseA]);
            voiceTarget.Speak(lineTarget, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            voiceTarget.WaitUntilDone(Timeout.Infinite);
            voiceSource.AudioOutputStream.Write(new byte[pauseB]);
        }

Where 22050.0 is the sample rate and pauseLineBreak.Value is the # of milliseconds. The multiplier 2.0 is for the 2-byte length of a short in the .wav data.

AudioOutputStream.Write simply writes the correct # of 00's to the file for silence.

like image 740
giangurgolo Avatar asked Oct 20 '11 20:10

giangurgolo


1 Answers

This is not an ideal solution but...

You could use a certain number of "silence" phoneme, i.e. '_' (underscored) (see http://msdn.microsoft.com/en-us/library/ms717239(v=vs.85).aspx) after checking how many ms it lasts. You may have to adjust the number of number of silences depending on the Rate that you set.

like image 91
Thomas Materna Avatar answered Nov 10 '22 04:11

Thomas Materna