Ms Speech

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Ms Speech

Post by jak64 »

Good morning,
the procedure below which uses an included file created by Justin.

https://www.purebasic.fr/english/viewto ... hilit=sapi

I don't know how to tell if a sound is playing or not!

Here is the procedure I use

Code: Select all

XIncludeFile "sapihelper.pbi"

 Procedure Lire_Texte(Texte.s, Vitesse.w = 0) ; Vitesse = 0, vitesse "normale" par défaut
  Protected.IEnumSpObjectTokens cpEnum
  Protected.ISpObjectToken      voiceToken
  Protected.ISpVoice            pVoice
  Protected.IID                 CLSID_SpVoice, IID_ISpVoice
  Protected NewMap              voicetokens.ISpObjectToken()
  Protected.i                   voiceId
  Protected.s                   name
  Protected Numero_De_Voix.w
  
  CoInitialize_(0)
  IIDFromString_( #CLSID_SpVoice$, @CLSID_SpVoice )
  IIDFromString_( #IID_ISpVoice$,  @IID_ISpVoice  )
  
  If CoCreateInstance_( @CLSID_SpVoice, #Null, #CLSCTX_INPROC_SERVER, @IID_ISpVoice, @pVoice ) <> #S_OK
    Debug "Failed To create voice opbject"
  Else  
    pVoice\SetInterest( #SPFEI_ALL_EVENTS, #SPFEI_ALL_EVENTS     )
    If SpEnumTokens( #SPCAT_VOICES, "", "", @cpEnum ) <> #S_OK	
      Debug "Failed to enumerate voices"
    Else
      While cpEnum\Next( 1, @voiceToken, #Null ) = #S_OK
        If voiceToken\GetId( @voiceId )          = #S_OK
          voicetokens(GetFilePart(PeekS(voiceId)) ) = voiceToken
        EndIf 
      Wend 
      cpEnum\Release()	
            
      ForEach voicetokens()
        Numero_De_Voix + 1
        If Numero_De_Voix = 2
          pvoice\setvoice(voicetokens() )
          pvoice\SetRate(Vitesse) ; Vitesse de lecture
          pvoice\SetVolume(100) ; Volume
          pvoice\speak( Texte, #SPF_ASYNC, #NUL) ; Lecture phrase en mode asynchrone
        EndIf
      Next 
      ForEach voicetokens()
        voicetokens()\release()
      Next 
    EndIf 
  EndIf
EndProcedure

Lire_Texte("Bonjour, comment vas-tu ?")
Delay(5000)
Can you help me?
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Ms Speech

Post by Caronte3D »

With pvoice\WaitUntilDone(-1) you can wait Until the voice was finished:

Code: Select all

XIncludeFile "sapihelper.pbi"

 Procedure Lire_Texte(Texte.s, Vitesse.w = 0) ; Vitesse = 0, vitesse "normale" par défaut
  Protected.IEnumSpObjectTokens cpEnum
  Protected.ISpObjectToken      voiceToken
  Protected.ISpVoice            pVoice
  Protected.IID                 CLSID_SpVoice, IID_ISpVoice
  Protected NewMap              voicetokens.ISpObjectToken()
  Protected.i                   voiceId
  Protected.s                   name
  Protected Numero_De_Voix.w
  

  CoInitialize_(0)
  IIDFromString_( #CLSID_SpVoice$, @CLSID_SpVoice )
  IIDFromString_( #IID_ISpVoice$,  @IID_ISpVoice  )
  
  If CoCreateInstance_( @CLSID_SpVoice, #Null, #CLSCTX_INPROC_SERVER, @IID_ISpVoice, @pVoice ) <> #S_OK
    Debug "Failed To create voice opbject"
  Else  
    pVoice\SetInterest( #SPFEI_ALL_EVENTS, #SPFEI_ALL_EVENTS     )
    If SpEnumTokens( #SPCAT_VOICES, "", "", @cpEnum ) <> #S_OK	
      Debug "Failed to enumerate voices"
    Else
      While cpEnum\Next( 1, @voiceToken, #Null ) = #S_OK
        If voiceToken\GetId( @voiceId )          = #S_OK
          voicetokens(GetFilePart(PeekS(voiceId)) ) = voiceToken
        EndIf 
      Wend 
      cpEnum\Release()	
            
      ForEach voicetokens()
        Numero_De_Voix + 1
        If Numero_De_Voix = 2
          pvoice\setvoice(voicetokens() )
          pvoice\SetRate(Vitesse) ; Vitesse de lecture
          pvoice\SetVolume(100) ; Volume
          pvoice\speak( Texte, #SPF_ASYNC, #NUL) ; Lecture phrase en mode asynchrone
          pvoice\WaitUntilDone(-1)
        EndIf
      Next 
      ForEach voicetokens()
        voicetokens()\release()
      Next 
    EndIf 
  EndIf
EndProcedure

Lire_Texte("Bonjour, comment vas-tu ?")
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Ms Speech

Post by jak64 »

Hello Caronte3D,

Thank you for your answer, but I think I wasn't specific enough in the question.

I'm writing a program that reads multiple texts, with each text in a different string variable.

I want to put a waiting time between each reading, that's why I use several strings, each containing a text and I call the reading of these texts one after the other. It works very well, the readings follow each other without problem.

I still want to be able to stop reading at any time, which is why I need to know if a text is being read or not.

What I noticed is that the program creates as many different (pVoice) IDs as there are different texts, so I don't know which pVoice ID to release on!

Thank you for your help
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Ms Speech

Post by Caronte3D »

I think on this case you need: "SpeakCompleteEvent" Method
Take a look at Windows documentation:
https://learn.microsoft.com/en-us/previ ... 0(v=vs.85)
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Ms Speech

Post by jak64 »

Hello Caronte3D,
I looked at the link you provided, unfortunately, I do not have sufficient knowledge to implement this in PureBasic.

Thank you all the same for answering me.

In fact, the goal of my program which is a Quiz program:
1) View and read the question
2) Display and read 4 answer suggestions
3) Wait for a few seconds, 5, or 10 or 15 while playing a waiting ticking sound
4) View and read the response
5) Move on to the next question, etc.

Question which has nothing to do with it.
When I access the PureBasic site, it takes much longer than before, is this normal or is it coming from my computer? It's still the same computer and I don't have any slowness problems on other sites.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Ms Speech

Post by boddhi »

Salut Jak
jak64 wrote: Question which has nothing to do with it.
When I access the PureBasic site, it takes much longer than before, is this normal or is it coming from my computer? It's still the same computer and I don't have any slowness problems on other sites.
For a few days now, the site has been experiencing slowness problems. There's a strong suspicion that AI bots are browsing the site (up to over 4,000 guests some days). Although it seemed to have calmed down yesterday, today it's slow again.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Ms Speech

Post by jak64 »

Hello boddhi,
thank you for your explanations.
Ah the A.I. !!!
Like any evolution, there is the good and the bad side...
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Ms Speech

Post by Caronte3D »

jak64 wrote: Wed Feb 19, 2025 10:31 am In fact, the goal of my program which is a Quiz program...
For that, you don't need more than you already have.
I don't know if not understand you but...
With the line of code I tell you on my first post the execution wait each time until the voice finished.
Simply when your text where wrote to the screen, use the Lire_Texte() to read all in order (one after other) and put a delay after the last one.
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Ms Speech

Post by jak64 »

Hello Caronte3D

Let me explain again.

YES, it works in continuous reading but I WANT TO BE ABLE, by pressing the ESC key, TO STOP reading at ANY TIME, whether it is reading the question or proposition 1, 2, whatever, without having to wait for the program to have read everything, that's why I read asynchronously
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Ms Speech

Post by Quin »

Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: Ms Speech

Post by Justin »

Not sure if i understand but i think the problem is that you create a voice for every line of text. Try creating just one ISpVoice instance and use it to read each line, you can pause and resume that voice and will effect whatever is reading.
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Ms Speech

Post by jak64 »

Hello everyone,
Thank you for taking the time to respond to me. As I am French, I use Google translate and, undoubtedly, I cannot express what I want.
By testing the great program provided by Jens-Arne https://www.purebasic.fr/english/viewto ... i&start=15, page 2, I will be able to do what I want. I will post an example when I have adapted his program, it will be clearer to explain to you, I think.

Have a nice day everyone
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Ms Speech

Post by Caronte3D »

jak64 wrote: Wed Feb 19, 2025 8:50 pm ...I WANT TO BE ABLE, by pressing the ESC key, TO STOP reading at ANY TIME...
Ok, It's easy! You just need to send an empty string when you want to stop the voice.

Code: Select all

Lire_Texte("")
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Ms Speech

Post by jak64 »

OK, Thanks
Post Reply