Page 1 of 2

Have a text read vocally

Posted: Fri Jan 31, 2025 11:30 am
by jak64
Good morning,
I have a program, written a while ago that reads a text.
I used a procedure that I found on the forum.

It works very well with v6.12 of PureBasic but I would like to use another voice.

In the narrator, I have the voice: Microsoft Henri (Natural) - French (France).

This voice works perfectly when I use Narrator on my computer.

What I don't know how to do is what changes I need to make to my program to make it use this voice.

I asked ChatGpt, but he only offered me programs that didn't work... He doesn't have the level of all these nice forum users who have been helping me for several years...

Can you help me?

I would like to point out that I am French.

Code: Select all

EnableExplicit
Global silence.s
silence="."+Space(1)
silence=". "
Enumeration Fichier
    #Fichier
EndEnumeration
;************** VALEUR POUR "SpeechVoiceSpeakFlags".*****************
Enumeration
    #SVSFDefault            = 0
    #SVSFlagsAsync          = 1
    #SVSFPurgeBeforeSpeak   = 2
    #SVSFIsFilename         = 4
    #SVSFIsXML              = 8
    #SVSFIsNotXML           = 16
    #SVSFPersistXML         = 32
    #SVSFNLPSpeakPunc       = 64
    #SVSFNLPMask            = 64
    #SVSFVoiceMask          = 127
    #SVSFUnusedFlags        = -128
EndEnumeration
Enumeration
    #CLSCTX_INPROC_SERVER   = $1
    #CLSCTX_INPROC_HANDLER  = $2
    #CLSCTX_LOCAL_SERVER    = $4
    #CLSCTX_REMOTE_SERVER   = $10
    #CLSCTX_ALL             = (#CLSCTX_INPROC_SERVER | #CLSCTX_INPROC_HANDLER | #CLSCTX_LOCAL_SERVER | #CLSCTX_REMOTE_SERVER)
EndEnumeration
Interface ISpeechVoice Extends IDispatch
    get_Status(*ISpeechVoiceStatus .l)
    get_Voice(*ISpeechObjectToken.l)
    put_Voice(*ISpeechObjectToken.l)
    get_AudioOutput(*ISpeechObjectToken.l)
    put_AudioOutput(*ISpeechObjectToken.l)
    get_AudioOutputStream(*ISpeechBaseStream.l)
    put_AudioOutputStream(*ISpeechBaseStream.l)
    get_Rate(long.l)
    put_Rate(long.l)
    get_Volume(long.l)
    put_Volume(long.l)
    put_AllowAudioOutputFormatChangesOnNextSet(VARIANT_BOOL.l)
    get_AllowAudioOutputFormatChangesOnNextSet(VARIANT_BOOL.l)
    get_EventInterests(SpeechVoiceEvents .l)
    put_EventInterests(SpeechVoiceEvents .l)
    put_Priority(SpeechVoicePriority.l)
    get_Priority(SpeechVoicePriority.l)
    put_AlertBoundary(SpeechVoiceEvents.l)
    get_AlertBoundary(SpeechVoiceEvents.l)
    put_SynchronousSpeakTimeout(long.l)
    SynchronousSpeakTimeout(long.l)
    Speak(*Text, SpeechVoiceSpeakFlags.l, long.l)
    SpeakStream(*ISpeechBaseStream, SpeechVoiceSpeakFlags.l, long.l)
    Pause()
    Resume()
    Skip(*Type, NumItems.l, long.l)
    GetVoices(*RequiredAttributes.l, *OptionalAttributes, *ISpeechObjectTokens.l)
    GetAudioOutputs(*RequiredAttributes, *OptionalAttributes, *ISpeechObjectTokens.l)
    WaitUntilDone(msTimeout.l, VARIANT_BOOL.l)
    SpeakCompleteEvent(long.l)
    IsUISupported(*TypeOfUI, *ExtraData.VARIANT, VARIANT_BOOL.l)
    DisplayUI(hWndParent.l, *Title, *TypeOfUI, *ExtraData.VARIANT)
EndInterface
Procedure.b isSAPIInstalled() 
    ; VERIFICATION DE L'INSTALLATION DE SAPI 4 OU 5.
    Define a.i
    Define bresult.b = #False  
    CoInitialize_(0)
    If CoCreateInstance_(?CLSID_SpVoice, 0, #CLSCTX_ALL, ?IID_ISpVoice, @a) = 0
        bresult = #True
    Else
        If CoCreateInstance_(?CLSID_ITextToSpeech, 0, #CLSCTX_ALL, ?IID_ITextToSpeech, @a) = 0
            bresult = #True
        EndIf
    EndIf
    CoUninitialize_()
    ProcedureReturn bresult
EndProcedure
Procedure SAPISpeechtext(stext.s, speed = 0)
    Define.i itemp, bstrtext
    Define text1.s
    Define *buffer1, *buffer2, *buffer3
    Define clsid.CLSID, refiid.CLSID
    Define speechvoice.ISpeechVoice   
    If isSAPIInstalled() = #True And stext <> #Null$
        text1 = "SAPI.SpVoice"
        *buffer1 = AllocateMemory((Len(text1) + 1) * 2)
        PokeS(*buffer1, text1, -1, #PB_Unicode)
        text1 = "{269316D8-57BD-11D2-9EEE-00C04F797396}"
        *buffer2 = AllocateMemory((Len(text1) + 1) * 2)
        PokeS(*buffer2, text1, -1, #PB_Unicode)
        If CLSIDFromProgID_(*buffer1, @Clsid.clsid)    = #S_OK
            If CLSIDFromString_(*buffer2, @Refiid.clsid) = #S_OK
                CoInitialize_(0)
                If  CoCreateInstance_(clsid, #Null, #CLSCTX_INPROC_SERVER, refiid, @speechvoice) = #S_OK
                    *buffer3 = AllocateMemory((Len(stext) + 1) * 2)
                    PokeS(*buffer3, stext, -1, #PB_Unicode)
                    itemp    = #SVSFDefault
                    bstrtext = SysAllocString_(*buffer3)
                   
                    speechvoice\put_Rate(speed)
                    speechvoice\put_Volume(100)
                   
                    speechvoice\Speak(bstrtext, 0, @itemp)
                    SysFreeString_(bstrtext)
                    FreeMemory(*buffer3)
                    speechvoice\Release()
                EndIf
                CoUninitialize_()
            EndIf
        EndIf
        FreeMemory(*buffer1)
        FreeMemory(*buffer2)     
    EndIf  
EndProcedure

; Programme principal
Global TexteComplet$ = "Selon la Bible,. quelle créature a tenté Adam dans le jardin d'éden ?"
SAPISpeechtext(TexteComplet$, 0)            ; -5=lent, 0=vitesse normale, 5 vitesse rapide
TexteComplet$ = ". A .  un serpent"
SAPISpeechtext(TexteComplet$, 0)            ; -5=lent, 0=vitesse normale, 5 vitesse rapide
TexteComplet$=". B .  une colombe"
SAPISpeechtext(TexteComplet$, 0)            ; -5=lent, 0=vitesse normale, 5 vitesse rapide
TexteComplet$=". C .  un lapin"
SAPISpeechtext(TexteComplet$, 0)            ; -5=lent, 0=vitesse normale, 5 vitesse rapide
TexteComplet$=". D .  une cigogne."
SAPISpeechtext(TexteComplet$, 0)            ; -5=lent, 0=vitesse normale, 5 vitesse rapide

End
; fin programme principal

DataSection
    CLSID_SpVoice:
    Data.l $96749377
    Data.w $3391,$11D2
    Data.b $9E,$E3,$00,$C0,$4F,$79,$73,$96
    IID_ISpVoice:
    Data.l $269316D8
    Data.w $57BD,$11D2
    Data.b $9E,$EE,$00,$C0,$4F,$79,$73,$96
    CLSID_ITextToSpeech:
    Data.l $EEE78591
    Data.w $FE22,$11D0
    Data.b $8B,$EF,$00,$60,$08,$18,$41,$DE
    IID_ITextToSpeech:
    Data.l $EEE78590
    Data.w $FE22,$11D0
    Data.b $8B,$EF,$00,$60,$08,$18,$41,$DE
EndDataSection

Re: Have a text read vocally

Posted: Fri Jan 31, 2025 11:54 am
by Caronte3D
In the past I've used this code on the French forum:
https://www.purebasic.fr/french/viewtop ... ese+vocale
Anyway I think you can not use modern voices, only the ones supported by the API used on this code.

Re: Have a text read vocally

Posted: Fri Jan 31, 2025 12:02 pm
by Mijikai

Re: Have a text read vocally

Posted: Fri Jan 31, 2025 12:03 pm
by jak64
Thank you but I would like to be able to use the voice I indicated in my first message (if it is possible!)

Re: Have a text read vocally

Posted: Fri Jan 31, 2025 1:20 pm
by jak64
Thank you Mijikai
I tested the program, it works, but there are only 2 voices:

Microsoft Hortense Desktop - French
Microsoft Zira Desktop - English (United States)

while in the narrator I can choose :
Microsoft Henri (Natural) - French (France)

This is the voice I would like to use.

Re: Have a text read vocally

Posted: Fri Jan 31, 2025 3:12 pm
by Quin
These are Azure TTS voices, so using them will be very difficult. Microsoft specifically ties them to Narrator, although it's possible to strings the API ke out of Narrator and wrap the voices into other things like a SAPI adapter and NVDA add-on (this has been done, and I may or may not have had something to do with it).
If you wannt to get started learning about this SDK, here you go: https://learn.microsoft.com/en-us/azure ... ded-speech

Re: Have a text read vocally

Posted: Fri Jan 31, 2025 10:12 pm
by jak64
Thanks Quin,
I went to look but didn't understand anything... :? :? :?
Too bad, it was to write a program for a visually impaired person.

Re: Have a text read vocally

Posted: Fri Jan 31, 2025 11:06 pm
by Jens-Arne
Take a look at this thread in the forum:

https://www.purebasic.fr/english/viewto ... pi#p633901

WIth combined efforts we've created one single pbi that does everything you need. Voice change included. You have to look down the thread a bit to find the end result. It sits on page 2 at least for me (big screen). And go down a little bit further to get the thunder sound for the code example if you like.

You can install virtually any voice with Windows 11 for free, also French voices, via »Settings --> Time and Language --> Language and Region« (translated from German Win11, it might have slightly different designators for you).

Re: Have a text read vocally

Posted: Sat Feb 01, 2025 3:11 am
by idle
don't know if these are SAPI5 voices or not but you could try
https://www.microsoft.com/en-us/downloa ... x?id=27224

maybe this from Loquendo could work in a webviewgadget? If it requires a server you could probably do it with atomic webserver running on localhost
https://ttsreader.com/plugin/

https://atomicwebserver.com/tts.html

Re: Have a text read vocally

Posted: Thu Feb 13, 2025 1:10 pm
by jak64
Hello Caronte3D,

I tested the code mentioned at

https://www.purebasic.fr/french/viewtop ... ese+vocale

under Windows 11 64 bits but the program crashes (syntax error and others...)

Re: Have a text read vocally

Posted: Thu Feb 13, 2025 1:27 pm
by jak64
Hello everyone,

I tested the Mijikai program from the link

https://www.purebasic.fr/german/viewtop ... 55#p346057


It works but I would like to use this voice (information from my Windows 11 registry):

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices\Tokens\MSTTS_V110_frFR_PaulM

Value :

{179F3D56-1B0B-42B2-A962-59B7EF59FE1B}

Is this possible?

Thanks to you

Re: Have a text read vocally

Posted: Thu Feb 13, 2025 10:02 pm
by infratec
Have you already tested the stuff from Jens-Arne

https://www.purebasic.fr/english/viewto ... 32#p633532

:?:

He mentioned it already a few posts above.

Re: Have a text read vocally

Posted: Thu Feb 13, 2025 11:48 pm
by jak64
Hello infratec,

I tested Justin's code (on the first page of the post you pointed out to me), but not yet Jens-Arne's.
I just tested Jens-Arne's code, it works very well.

On the other hand, in the voices that the program offers, there are not certain voices that appear in the choice of voices in the narrator settings under my Windows 11.

For example, I would like to use this voice:
Microsoft Henri (Natural) - French (France)
because I am French and this voice is more natural.

I looked in the Windows registry and, unless I forgot something, I didn't find where the other voices were configured!

If anyone has an idea to use this voice in Jens-Arne's program, that would be great.

Thank you.

Re: Have a text read vocally

Posted: Fri Feb 14, 2025 2:16 am
by BarryG
jak64 wrote: Fri Jan 31, 2025 10:12 pmit was to write a program for a visually impaired person
In my experience and as told to me by a blind user (not in these forums), they use Narrator to hear the text in my app. So maybe you're reinventing the wheel?

Re: Have a text read vocally

Posted: Fri Feb 14, 2025 2:54 am
by jak64
Hello BarryG,
It's for a young child and I'm writing little games for him, like quizzes where he has to respond by clicking on big buttons, that's why I would like a solution integrated into my program.