Stuck with Streaming Text-to-Speech Audio in FastAPI (React Frontend, Azure TTS)

I’m a newbie to Python and I’m facing a bit of a roadblock in my current project. I’ve been at it for a while now and tried searching for solutions, but I’m still stuck. Any help would be greatly appreciated!

Project Setup:

Backend: FastAPI app

Frontend: React app

Text-to-Speech: Azure Cognitive Services Speech SDK

Goal:

I’m building a route in my FastAPI app that streams audio data from Azure Text-to-Speech to the React frontend.

Code Snippet:

Define the text-to-speech stream function

async def text_to_speech_stream(text):
try:
result = speech_synthesizer.speak_text_async(text).get()
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
audio_data_stream = speechsdk.AudioDataStream(result)
# read data from audio data stream and process it in memory
# Reset the stream position to the beginning since saving to file puts the position to end.
audio_data_stream.position = 0

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> # Prepare an in-memory stream for the WAV file
wav_header = io.BytesIO()
wf = wave.open(wav_header, 'wb')
wf.setnchannels(1) # Mono channel
wf.setsampwidth(2) # Sample width in bytes
wf.setframerate(16000) # Sample rate in Hz
# First, write headers and any required initial data
wf.writeframes(b'') # Write empty data to put the header
# Continue to stream the actual audio data
audio_buffer = bytes(16000)
while True:
filled_size = audio_data_stream.read_data(audio_buffer)
if filled_size > 0:
print(f"{filled_size} bytes received.")
# Write to the in-memory WAV file
wf.writeframes(audio_buffer[:filled_size])
# Go to the start to read what was just written and stream it
wav_header.seek(0)
data_to_send = wav_header.read() # Read all data to send
yield data_to_send # Yield the read data
else:
break
wf.close()
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
except Exception as ex:
print(f"Error synthesizing audio: {ex}")
yield b''
</code>
<code> # Prepare an in-memory stream for the WAV file wav_header = io.BytesIO() wf = wave.open(wav_header, 'wb') wf.setnchannels(1) # Mono channel wf.setsampwidth(2) # Sample width in bytes wf.setframerate(16000) # Sample rate in Hz # First, write headers and any required initial data wf.writeframes(b'') # Write empty data to put the header # Continue to stream the actual audio data audio_buffer = bytes(16000) while True: filled_size = audio_data_stream.read_data(audio_buffer) if filled_size > 0: print(f"{filled_size} bytes received.") # Write to the in-memory WAV file wf.writeframes(audio_buffer[:filled_size]) # Go to the start to read what was just written and stream it wav_header.seek(0) data_to_send = wav_header.read() # Read all data to send yield data_to_send # Yield the read data else: break wf.close() elif result.reason == speechsdk.ResultReason.Canceled: cancellation_details = result.cancellation_details print("Speech synthesis canceled: {}".format(cancellation_details.reason)) if cancellation_details.reason == speechsdk.CancellationReason.Error: print("Error details: {}".format(cancellation_details.error_details)) except Exception as ex: print(f"Error synthesizing audio: {ex}") yield b'' </code>
        # Prepare an in-memory stream for the WAV file
        wav_header = io.BytesIO()
        wf = wave.open(wav_header, 'wb')
        wf.setnchannels(1)  # Mono channel
        wf.setsampwidth(2)  # Sample width in bytes
        wf.setframerate(16000)  # Sample rate in Hz

        # First, write headers and any required initial data
        wf.writeframes(b'')  # Write empty data to put the header
        
        # Continue to stream the actual audio data
        audio_buffer = bytes(16000)
        while True:
            filled_size = audio_data_stream.read_data(audio_buffer)
            if filled_size > 0:
                print(f"{filled_size} bytes received.")
                # Write to the in-memory WAV file
                wf.writeframes(audio_buffer[:filled_size])
                # Go to the start to read what was just written and stream it
                wav_header.seek(0)
                data_to_send = wav_header.read()  # Read all data to send
                yield data_to_send  # Yield the read data
            else:
                break
        
        wf.close()
            
    elif result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = result.cancellation_details
        print("Speech synthesis canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print("Error details: {}".format(cancellation_details.error_details))
except Exception as ex:
    print(f"Error synthesizing audio: {ex}")
    yield b''

Define text to speech chat route

@app.post(“/chat-text-to-speech/”)
async def chat_text_to_speech(data: Chat_Request):
# Send the text to the LLM and get the response
#response = response_from_LLM(data.message)
response={“response”: “this is an example of text to speech, I need you to speek to me. this is really important.”}
# Check if the response is successful
if response:
# Convert the response to speech
audio_stream = text_to_speech_stream(response[“response”])

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> if audio_stream:
return StreamingResponse(audio_stream, media_type='audio/wav')
else:
return {"error": "Unable to synthesize audio"}
else:
return {"error": "Unable to get response from LLM"}
</code>
<code> if audio_stream: return StreamingResponse(audio_stream, media_type='audio/wav') else: return {"error": "Unable to synthesize audio"} else: return {"error": "Unable to get response from LLM"} </code>
    if  audio_stream:
        return StreamingResponse(audio_stream, media_type='audio/wav')
    else:
        return {"error": "Unable to synthesize audio"}
else:
    return {"error": "Unable to get response from LLM"}

Problem:

When I use Postman to send a request to the /chat-text-to-speech/ route, I only receive the initial chunk of the audio data, which is the size of audio_buffer (16000 bytes). If I increase the buffer size to 160000 bytes, I get a longer audio clip, and the audio clip contain correct content. and I got those info in my terminal when I call the text_to_speech_stream function:

“””

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

16000 bytes received.

15200 bytes received.

“””

Question:

I’m not sure how to modify my code to receive the entire streaming audio data from Azure TTS. Is the issue with how I’m reading the audio_data_stream or how I’m handling the in-memory WAV file creation?

Additional Notes:

I’ve gone through the Azure Speech SDK documentation and checked their github example(azure speech example) but haven’t found a solution specific to this scenario.

Any pointers or suggestions would be a huge help!

Thanks in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật