简体   繁体   English

如何使用SpVoice在C#中说毫秒的静音?

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

How to actually speak silence for X # of milliseconds and not by using Thread.Sleep(). 如何实际说出X#毫秒的静音而不是使用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. 我正在尝试使用SpVoice变量的SpeechLib库中的.Speak()函数,根据指定的毫秒数说出特定的静默持续时间。 Particularly, in the output of a .wav file wherein I am inserting periods of silence between spoken lines of text. 特别是,在.wav文件的输出中,其中我在语音文本行之间插入静默时段。 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. 使用Thread.Sleep()会花费大量的时间来说话或保存,因为我计划将近5000行语音文本保存到.wav,并在行之间暂停。

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. 其中22050.0是采样率,pauseLineBreak.Value是毫秒数。 The multiplier 2.0 is for the 2-byte length of a short in the .wav data. 乘法器2.0用于.wav数据中的短字节的2字节长度。

AudioOutputStream.Write simply writes the correct # of 00's to the file for silence. AudioOutputStream.Write只是将正确的#00写入文件以保持静音。

This is not an ideal solution but... 这不是一个理想的解决方案,但......

You could use a certain number of "silence" phoneme, ie '_' (underscored) (see http://msdn.microsoft.com/en-us/library/ms717239(v=vs.85).aspx ) after checking how many ms it lasts. 您可以在检查后使用一定数量的“沉默”音素,即'_'(强调)(请参阅http://msdn.microsoft.com/en-us/library/ms717239(v=vs.85).aspx )它持续多少毫秒。 You may have to adjust the number of number of silences depending on the Rate that you set. 您可能需要根据您设置的速率调整静音次数。

A couple options: 几个选项:

  • You could spin up another thread to do the operation 您可以启动另一个线程来执行操作
  • You could simply figure out how many bytes it takes per second and write them to out output stream. 您可以简单地计算出每秒需要多少字节并将它们写入输出流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM