ElevenLabs text-to-speech API call

Share your advanced PureBasic knowledge/code with the community.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

ElevenLabs text-to-speech API call

Post 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
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ElevenLabs text-to-speech API call

Post by infratec »

Here is a way with windows onboard tools:
viewtopic.php?t=71402
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElevenLabs text-to-speech API call

Post by mk-soft »

Procedure.b foo() ; result type byte :twisted:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: ElevenLabs text-to-speech API call

Post 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?
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ElevenLabs text-to-speech API call

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: ElevenLabs text-to-speech API call

Post 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!
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: ElevenLabs text-to-speech API call

Post by Caronte3D »

:shock: Why don't you download the latest version?
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: ElevenLabs text-to-speech API call

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: ElevenLabs text-to-speech API call

Post 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.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ElevenLabs text-to-speech API call

Post 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
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: ElevenLabs text-to-speech API call

Post 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.
Taishan
New User
New User
Posts: 6
Joined: Wed May 03, 2023 2:20 am

Re: ElevenLabs text-to-speech API call

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: ElevenLabs text-to-speech API call

Post 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().
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply