xyzzyrz
xyzzyrz

Reputation: 16416

When using SAPI.SpVoice to output to a WAV file, result sounds different than when outputting directly to speakers

I'm trying to write a simple script to run some txt files through the Windows 7 text-to-speech engine (which has the decent Anna voice) and produce wav files. However, the wav files don't sound as nice as when I just have it output directly to speakers. I've tried this on two completely different Windows 7 systems already. Any way to remedy this?

Script:

set x = createobject("SAPI.SpVoice")
' Uncomment following lines to output to file
'set ofs = createobject("SAPI.SpFileStream")
'ofs.Open "msg.wav", 3, vbFalse
'set x.AudioOutputStream = ofs
x.Speak "In the fall of 2003, ..."

Upvotes: 3

Views: 7150

Answers (3)

William Lee
William Lee

Reputation: 115

Little less 1990.

# run in powershell
$voice = New-Object -ComObject SAPI.SpVoice
$file = New-Object -ComObject SAPI.SpFileStream
$file.Open('d:\ok.wav', 3)
$voice.AudioOutputStream = $file
$voice.Speak('hi there')
$file.Close()

Upvotes: 1

James
James

Reputation: 571

SapiFileType is defined here: http://msdn.microsoft.com/en-us/library/ms720595%28v=vs.85%29.aspx

Enum 18 = 16kHz 16Bit Mono

Upvotes: 3

Brandon
Brandon

Reputation: 21

I have to admit, I was wondering the same thing up until a few days ago. Here is the solution, however the number '18' on the first line might be voice specific. I've been trying to get that high quality version into a wav file for a long time, so I finally ran through every number (0-64), and listened to all of the samples until I found the right one.

Paste the code below into notepad, save as 'SapiSomething.vbs', run, and hopefully it's the high quality output you're looking for. For me, the sound quality in the file output is finally the same as when speech is sent straight to the speakers.

Const SapiFileType=18 ' Magic number, possibly voice specific (0 to 64)

strText=Trim(InputBox("What do you want me to say?","Listen to Sapi.SpFileStream.Format.Type Quality",""))
If NOT len(strText)>0 Then WScript.Quit
With CreateObject("Scripting.FileSystemObject")
 strFile=.BuildPath(.GetParentFolderName(WScript.ScriptFullName),"Sapi.SpFileStream.Format.Type_"&SapiFileType&".wav")
 If .FileExists(strFile) Then .DeleteFile strFile
End With
With CreateObject("Sapi.SpVoice")
 Set ss=CreateObject("Sapi.SpFileStream")
 ss.Format.Type=SapiFileType
 ss.Open strFile,3,False
 Set .AudioOutputStream=ss
 .Speak strText,8
 .waituntildone(-1)
 ss.Close
 Set ss=Nothing
End With
With CreateObject("WMPlayer.OCX"):.settings.autoStart=True:.settings.volume=100:.URL=strFile:Do until .playState=1:Wscript.Sleep 200:Loop:End With

Upvotes: 2

Related Questions