# Need help with code translation from VB

**URL:** <https://www.ruby-forum.com/t/need-help-with-code-translation-from-vb/150769>\
**Category:** Ruby\
**Created:** [October 13, 2008, 2:15am UTC](https://www.ruby-forum.com/t/need-help-with-code-translation-from-vb/150769 "2008-10-13T02:15:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 13, 2008, 2:15am UTC](https://www.ruby-forum.com/t/need-help-with-code-translation-from-vb/150769/1 "2008-10-13T02:15:07Z")

</div>

Hello,

I copy the following scripts from  
[Simple TTS Guide (SAPI 5.3) | Microsoft Learn](http://msdn.microsoft.com/en-us/library/ms717065(VS.85).aspx)  
Can anyone kindly translate them into Ruby scripts ?

Thank you very much,

Li

###############################  
Speak to a wav file in automation  
The following example is written in Visual Basic. It has the same  
functionality as the above in C++. After the creation of an SpFileStream  
object, a default format, SAFT22kHz16BitMono, is assigned to the object  
so that user does not need to explicitly assign a wav format to it  
unless a specific wav format is needed. In this example,  
ISpeechFileStream.Open creates a wav file, ttstemp.wav, and binds the  
FileStream to the file. The third parameter of ISpeechFileStream.Open is  
the Boolean, DoEvents. The default of this parameter is set to False.  
However, the user should always set it to True to display SAPI events  
while playing back the wav file. If the parameter is set to False, no  
engine events will be stored in the file, resulting in that no engine  
events will be fired during the wav file play back.

Dim FileName As String  
Dim FileStream As New SpFileStream  
Dim Voice As SpVoice

'Create a SAPI voice  
Set Voice = New SpVoice

'The output audio data will be saved to ttstemp.wav file  
FileName = “c:\ttstemp.wav”

'Create a file; set DoEvents=True so TTS events will be saved to the  
file  
FileStream.Open FileName, SSFMCreateForWrite, True

'Set the output to the FileStream  
Set Voice.AudioOutputStream = FileStream

'Speak the text  
Voice.Speak “hello world”

'Close the Stream  
FileStream.Close

'Release the objects  
Set FileStream = Nothing  
Set Voice = Nothing

############################  
Speak a Text File in Automation  
The following code illustrates how to speak a text file in a specific  
voice in Visual Basic. This example assumes a text file (ttstemp.txt)  
containing the text to be spoken already exists.  
ISpeechVoice.SpeakStream is used here to speak an SpFileStream that has  
been bound to the file.

Dim FileName As String  
Dim FileStream As New SpFileStream  
Dim Voice As SpVoice

'Create SAPI voice  
Set Voice = New SpVoice

'Assume that ttstemp.txt exists  
FileName = “c:\ttstemp.txt”

'Open the text file  
FileStream.Open FileName, SSFMOpenForRead, True

'Select Microsoft Sam voice  
Set Voice.voice = voice.GetVoices(“Name=Microsoft Sam”,  
“Language=409”).Item(0)

'Speak the file stream  
Voice.SpeakStream FileStream

'Close the Stream  
FileStream.Close

'Release the objects  
Set FileStream = Nothing  
Set Voice = Nothing

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 13, 2008, 10:40am UTC](https://www.ruby-forum.com/t/need-help-with-code-translation-from-vb/150769/2 "2008-10-13T10:40:33Z")

</div>

2008/10/13 Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I copy the following scripts from  
> [Simple TTS Guide (SAPI 5.3) | Microsoft Learn](http://msdn.microsoft.com/en-us/library/ms717065(VS.85).aspx)  
> Can anyone kindly translate them into Ruby scripts ?

How much are you willing to pay?

robert

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 13, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/need-help-with-code-translation-from-vb/150769/3 "2008-10-13T12:14:30Z")

</div>

On Mon, 13 Oct 2008 02:12:26 +0200, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Simple TTS Guide (SAPI 5.3) | Microsoft Learn](http://msdn.microsoft.com/en-us/library/ms717065(VS.85).aspx)

Using properly encoded URLs makes life easier:

> **[Simple TTS Guide (SAPI 5.3)](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms717065(v=vs.85)?redirectedfrom=MSDN)**

Josef ‘Jupp’ Schugt
