#import requests
1 messages · Page 1 of 1 (latest)
please wrap your code to activate syntax highlight, it'll make it much easier to help you
import numpy
# etc
As for the problem @kindred atlas,
Are you able to make successful requests when using the openapi tester?
https://api.elevenlabs.io/docs#/text-to-speech/Text_to_speech_v1_text_to_speech__voice_id__post
I think you're missing this header:
accept: audio/mpeg
hello, thank you so much for your answer, I changer the code and finally manged to get the audio and save as MP3 file, but for some reason the stream dont work as expected, just play a buzz sound, in fact i can play audio file with vlc or online player but not with python, i only mange to play it with simple audio and sounds asexpected but any other try sounds exactly as playing the stream with my code just the buzz sound
import requests
import io
import pyaudio
xi_api_key = "2b7c593f290ad4ed88aba209fd36d84d"
headers = {"xi-api-key": "2b7c593f290ad4ed88aba209fd36d84d","Content-Type": "application/json"}
text = "Hello"
# Realizar la petición POST
response = requests.post(
f"https://api.elevenlabs.io/v1/text-to-speech/xyukEzlNDjfT1VucRcxP/stream",
json = {
"text": text
},
headers={
"xi-api-key": xi_api_key,
"accept": "audio/mpeg"
}
)
print (response.content)
# Verificar si la respuesta es exitosa
if response.status_code == 200:
# Obtener la respuesta en formato de audio
audio_stream = io.BytesIO(response.content)
# Inicializar PyAudio
p = pyaudio.PyAudio()
# Abrir un flujo de audio
stream = p.open(format=p.get_format_from_width(1),
channels=1,
rate=44100,
output=True)
# Reproducir la respuesta en el flujo de audio
chunk = 1024
while True:
data = audio_stream.read(chunk)
if not data:
break
stream.write(data)
# Detener y cerrar el flujo de audio
stream.stop_stream()
stream.close()
# Terminar PyAudio
p.terminate()
else:
# Imprimir un mensaje de error
print("La petición POST no fue exitosa. Código de estado:", response.status_code)
this is the last code that works kind of but just play the buzz sound
and to save the audio file I simply
# Write the audio to disk, response.content contains it as bytes
with open("audio.mp3", "wb") as f:
f.write(response.content)
I dont know is there an example of usage for the stream to directly play the sound. anyway i really appreciate you time to answer my original question!
don't show your API key (hope you've modified it, but still lol)
is it an mp3?
i think the stream is just raw bytes and you have to decode it
thaks jeje i changed it!, i simple used an example someone give on support i dont know why they give that format as you said its not actualy formatted
i actually did it 2 days ago but it was in C#
it should work
and i've deleted it though haha, let me write it in python for you
thank you so much
import requests
import pyaudio
xi_api_key = 'XXX'
url = 'https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM/stream'
header = {
'accept': '*/*',
'Content-Type': 'application/json',
"xi-api-key": xi_api_key,
}
payload = {'text': 'string'}
response = requests.post(url, headers=header, json=payload)
if response.status_code == 200:
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(2), channels=1, rate=16000, output=True)
stream.start_stream()
stream.write(response.content)
stream.stop_stream()
stream.close()
p.terminate()
with open('output.wav', 'wb') as f:
f.write(response.content)
else:
print('error', response)
yours might be better, but basically, try saving it as .wav file?
missing API key in the header, hold on
ok i modified it
be careful, it's gonna play as you execute it
(i got spooked cause my volume was loud)
playing output.wav works for me
👍