Outputting speech to a file

by Richard Russell, October 2008

The supplied example program SPEAK.BBC illustrates how you can use Windows' built-in speech synthesis capabilities to output speech to your PC's loudspeakers, but what if you want to create a sound file containing the speech? Fortunately the SAPI 5 speech system (typically available in Windows 2000, XP and Vista) supports this facility directly.

The code below outputs speech to a specified WAV file:

        INSTALL @lib$+"COMLIBA"
 
        ON ERROR ON ERROR OFF : PROC_comexit : PRINT 'REPORT$ : END
        ON CLOSE PROC_comexit : QUIT
 
        PROC_cominit
        file$ = "test.wav"
        speech$ = "This is a test file"
 
        voice% = FN_createobject("SAPI.SpVoice")
        filestream% = FN_createobject("SAPI.SpFileStream")
        PROC_callmethod(filestream%, "Open("""+file$+""",3,0)")
        PROC_putobject(voice%, "AudioOutputStream(O filestream%)")
        PROC_callmethod(voice%, "Speak("""+speech$+""",0)")
        PROC_callmethod(filestream%, "Close")
        PROC_releaseobject(filestream%)
        PROC_releaseobject(voice%)
 
        PROC_comexit
 
        SYS "PlaySound", file$, 0, &20001

Note that this code requires COMLIB (or COMLIBA) version 3.3 or later, as supplied with BBC BASIC for Windows version 5.90a.