Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Record TTS to file in User selected Voice

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Record TTS to file in User selected Voice


    Code:
    '///////////////////////////////////////////////////
    '/     Record TTS to File in chosen voice          /
    '///////////////////////////////////////////////////
    '/How to capture a computer generated voice phrase /
    '/to a file using the voice that is selected by    /
    '/the user.  by Jim Fritts 06 JUN 2018             /
    '/Release for public consumption.                  /
    '///////////////////////////////////////////////////
    
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE "Win32api.inc"
    #INCLUDE ONCE "SAPI.INC"
    
    'This works with Jose' includes
    
    'EXTRACTED FROM SAPI.INC FILE
    ENUM SpeechStreamFileMode
        SSFMOpenForRead = 0
        SSFMOpenReadWrite = 1
        SSFMCreate = 2
        SSFMCreateForWrite = 3
    END ENUM
    
    $PROGID_SpeechLib_SpVoice = "SAPI.SpVoice"
    $PROGID_SpeechLib_SpFileStream = "SAPI.SpFileStream"
    
    '------------------------------------------------------------------------------
    FUNCTION PBMAIN () AS LONG
        GLOBAL oFileStream  AS ISpeechFileStream
        GLOBAL oVoiceX      AS ISpeechVoice
        LOCAL strTxt        AS STRING
        LOCAL oTokens       AS DISPATCH
        LOCAL vRes          AS VARIANT
        LOCAL oToken        AS DISPATCH
        LOCAL vToken        AS ISpeechObjectToken
        LOCAL i             AS LONG
        LOCAL vIdx          AS VARIANT
        LOCAL nCount        AS LONG
        LOCAL strDesc       AS STRING
        LOCAL wszFile       AS WSTRINGZ * %MAX_PATH
    
        LET oVoiceX = NEWCOM $PROGID_SpeechLib_SpVoice
        IF ISFALSE ISOBJECT(oVoiceX) THEN
            MSGBOX "Cannot get object: oVoiceX"
            EXIT FUNCTION
        END IF
    
        LET oFileStream = NEWCOM $PROGID_SpeechLib_SpFileStream
        IF ISFALSE ISOBJECT(oFileStream) THEN
            SET oVoiceX = NOTHING
            MSGBOX "Cannot get object: oFileStream"
            EXIT FUNCTION
        END IF
    
        strTxt = "Hello World"
    
        oVoiceX.GetVoices( ) TO oTokens
        ' Get the number of tokens
        OBJECT GET oTokens.Count TO vRes
        nCount = VARIANT#( vRes )
        ' Parse the collection (zero based)
        FOR i = 0 TO nCount - 1
            vIdx = i AS LONG
            ' Get the item by his index
            OBJECT CALL oTokens.Item( vIdx ) TO vRes
            IF ISFALSE OBJRESULT THEN
                LET oToken = vRes
                vRes = EMPTY
                IF ISFALSE OBJRESULT THEN
                    ' Get the description
                    OBJECT CALL oToken.GetDescription( ) TO vRes
                    IF ISFALSE OBJRESULT THEN
                        strDesc = VARIANT$( vRes )
                        ' If its the token we are after, set the Voice property
                        IF strDesc = "Microsoft Zira Desktop - English (United States)" THEN
                            LET vToken = oToken
                            CALL oVoiceX.PutRef_Voice() = vToken     '///this is the nuts to the problem///
                            LET oToken = NOTHING
                            EXIT FOR
                        END IF
                    END IF
                    LET oToken = NOTHING
                END IF
            END IF
        NEXT i
        LET oTokens = NOTHING
    
        wszFile = CURDIR$ + "\TTS_Test.wav"
    
        oFileStream.Open wszFile, %SpeechStreamFileMode.SSFMCreateForWrite, %TRUE
    
        oVoiceX.PutRef_AudioOutputStream = oFileStream
    
        ' Say something
        oVoiceX.Speak strTxt
    
        oFileStream.Close
    
        SET oVoiceX = NOTHING
        SET oFileStream = NOTHING
    
        MSGBOX "Finished"
    
    END FUNCTION
    '------------------------------------------------------------------------------
    
    

  • #2
    Brilliant Jim, one more question, I would need a way to pause the speech.
    I had used "........................" but it only last a second.

    Any suggestion that can enable pausing the speech by a longer duration ?

    Comment


    • #3
      The em-dash "—" inserts a ½ second delay.
      You may use XML text to get many options such as pausing... <break time=""5s"" /> will set a five second pause.

      [By the way, this is the Code section, no question should be asked here ;-)]'

      Lets continue here...

      Comment

      Working...
      X