Page 1 of 1

ElevenLabs text-to-speech API call

Posted: Fri Feb 03, 2023 3:57 pm
by Seymour Clufley
This is just a quick procedure for using ElevenLabs to synthesise speech from text. To make it work, you will need to create an account with ElevenLabs and copy the API key that it gives you into this code.

Code: Select all

Procedure.b XI_TTS(api_key.s,voice_id.s,text.s,mp3_fn.s)
  
  If text=""
    Debug "XI_TTS: no text specified"
    ProcedureReturn #False
  EndIf
  
  If voice_id=""
    Debug "XI_TTS: no voice_id specified"
    ProcedureReturn #False
  EndIf
  
  If mp3_fn=""
    Debug "XI_TTS: no mp3 file specified"
    ProcedureReturn #False
  EndIf
  
  
  u.s = "https://api.elevenlabs.io/v1/text-to-speech/"+voice_id
  
  NewMap header.s()
  header("xi-api-key") = api_key
  header("accept") = "audio/mpeg"
  header("Content-Type") = "application/json"
  dat.s = "{ "+#DOUBLEQUOTE$+"text"+#DOUBLEQUOTE$+": "+#DOUBLEQUOTE$+text+#DOUBLEQUOTE$+" }"
  
  req.i = HTTPRequest(#PB_HTTP_Post,u,dat,0,header())
  If req
    Debug "StatusCode: " + HTTPInfo(req,#PB_HTTP_StatusCode)
    Debug "Response: " + HTTPInfo(req,#PB_HTTP_Response)

    If HTTPInfo(req,#PB_HTTP_StatusCode)="200"
      *ret = HTTPMemory(req)
      Debug "Response size: " + MemorySize(*ret)
      f = CreateFile(#PB_Any,mp3_fn)
      WriteData(f,*ret,MemorySize(*ret))
      CloseFile(f)
      FreeMemory(*ret)
    EndIf
    
    FinishHTTP(req)
    
    ProcedureReturn #True
  EndIf
  
EndProcedure



#VoiceID_Rachel = "21m00Tcm4TlvDq8ikWAM" ; American, mellow
#VoiceID_Domi = "AZnzlk1XvdvUeBnXmlld"   ; American, engaged
#VoiceID_Bella = "EXAVITQu4vr4xnSDxMaL"  ; American, soft
#VoiceID_Antoni = "ErXwobaYiN019PkySvjV" ; American, modulated
#VoiceID_Elli = "MF3mGyEYCl7XYWbV9V6O"   ; American, clear
#VoiceID_Josh = "TxGEqnHWrfWFTfGW9XjX"   ; American, silvery
#VoiceID_Arnold = "VR6AewLTigWG4xSOukaG" ; American, nasal
#VoiceID_Adam = "pNInz6obpgDQGcFmaJgB"   ; American, clear

#XIApiKey = "" ; insert your API key here
mp3_fn.s = "P:\xi-response.mp3" ; a file location here
txt.s = "my ElevenLabs text"
If XI_TTS(#XIApiKey,#VoiceID_Antoni,txt,mp3_fn)
  RunProgram(mp3_fn)
EndIf

Re: ElevenLabs text-to-speech API call

Posted: Fri Feb 03, 2023 8:26 pm
by infratec
Here is a way with windows onboard tools:
viewtopic.php?t=71402

Re: ElevenLabs text-to-speech API call

Posted: Fri Feb 03, 2023 9:39 pm
by mk-soft
Procedure.b foo() ; result type byte :twisted:

Re: ElevenLabs text-to-speech API call

Posted: Sat Feb 04, 2023 8:48 am
by Seymour Clufley
mk-soft wrote: Fri Feb 03, 2023 9:39 pm Procedure.b foo() ; result type byte :twisted:
Yes, because the procedure either succeeds or fails, #True or #False, so why not use a byte type for the result? Is it because this is an illusion, and actually the procedure returns an integer?

Re: ElevenLabs text-to-speech API call

Posted: Sat Feb 04, 2023 12:09 pm
by mk-soft
As a rule, a bool is an integer, since otherwise a type conversion must always be carried out.
So enlarge the translated ASM code.

Re: ElevenLabs text-to-speech API call

Posted: Mon Feb 06, 2023 4:30 pm
by ebs
Thank you for this code. They have some amazing TTS voices!

I'm stuck back on PB 5.43, which doesn't have the newer HTTP commands.
Can you point me to any code I can use for my older version of PB? Thanks!

Re: ElevenLabs text-to-speech API call

Posted: Mon Feb 06, 2023 6:05 pm
by Caronte3D
:shock: Why don't you download the latest version?

Re: ElevenLabs text-to-speech API call

Posted: Mon Feb 06, 2023 6:12 pm
by ebs
Why don't you download the latest version?
For business reasons, I have to stay with a non-Unicode version on my development PC.
If I can't find some code to adapt, I may download the latest version at home and try it.

Re: ElevenLabs text-to-speech API call

Posted: Mon Feb 06, 2023 6:33 pm
by Caronte3D
Ah! Ok
From PB 5.43 to 6.0 I think it's worth the change and spend some time converting old code.
But... yes, in commercial applications, the priority is that everything works well.

Re: ElevenLabs text-to-speech API call

Posted: Tue Feb 07, 2023 11:58 am
by infratec
@ebs

you can try my libcurl.pbi
there are also the new PB comands as pbi available.
Maybe it works in 5.43

Re: ElevenLabs text-to-speech API call

Posted: Tue Feb 07, 2023 2:46 pm
by ebs
@infratec and @Seymour Clufley,

Thanks for the suggestion of using libcurl.pbi.
I am currently using PB 5.43 and it doesn't have the necessary HTTP commands.

I did install PB 6.00, signed up for an account at ElevenLabs, and the code works fine.
They really do have some incredible voices! Thanks very much.

Re: ElevenLabs text-to-speech API call

Posted: Wed May 10, 2023 5:13 pm
by Taishan
Great example.
I'm new to PureBasic, coming from visual basic. I'm amazed by how clean the code is in PB.
What would it take to make this into a .exe that could take command line parameters?
Also, has anyone written a primer for people coming from VB?
Thanks in advance!
Tai

Re: ElevenLabs text-to-speech API call

Posted: Wed May 10, 2023 5:38 pm
by jacdelad
Taishan wrote: Wed May 10, 2023 5:13 pm Great example.
I'm new to PureBasic, coming from visual basic. I'm amazed by how clean the code is in PB.
What would it take to make this into a .exe that could take command line parameters?
Also, has anyone written a primer for people coming from VB?
Thanks in advance!
Tai
Hello and welcome!
Please create your own thread for that, this is not related to the topic.
To answer your question: look into the help for CountProgramParameters() and ProgramParameter().