Hi @LeetGPT
Thank you for reaching out to the Microsoft Q&A forum and for providing your code snippet.
I understand that you're looking for a more streamlined approach to stream the Azure Text-to-Speech (TTS) audio output directly to cloud storage without saving it locally first.
While your current implementation effectively synthesizes speech and uploads it to Azure Blob Storage, it involves an intermediate step of saving the audio file locally before uploading it to the cloud storage, which introduces additional I/O overhead and latency.
To achieve a more direct streaming of audio data to cloud storage without saving it locally, you can leverage Azure Blob Storage's ability to accept byte data directly. Below, I've outlined a modified approach that worked for me.
import os
import azure.cognitiveservices.speech as speechsdk
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
# Azure Speech Service Configuration
speech_key = "YOUR_SPEECH_KEY"
service_region = "YOUR_SERVICE_REGION"
from_language = 'en-US'
# Azure Storage Configuration
storage_connection_string = "YOUR_STORAGE_CONNECTION_STRING"
container_name = 'YOUR_CONTAINER_NAME'
def synthesize_text_to_speech():
# Azure Speech Service Configuration
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
# Text to be synthesized
text = "Hello, how are you?"
# Synthesize speech
result = speech_synthesizer.speak_text_async(text).get()
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized successfully.")
audio_data = result.audio_data
upload_audio_to_storage(audio_data)
else:
print("Failed to synthesize speech:", result.reason)
def upload_audio_to_storage(audio_data):
blob_service_client = BlobServiceClient.from_connection_string(storage_connection_string)
container_client = blob_service_client.get_container_client(container_name)
blob_name = 'synthesized_audio.wav'
blob_client = container_client.get_blob_client(blob_name)
# Upload audio data to Azure Blob Storage
blob_client.upload_blob(audio_data, overwrite=True)
print("Audio uploaded to Azure Blob Storage.")
synthesize_text_to_speech()
This modified approach directly uploads the audio data obtained from the Azure Text-to-Speech service to Azure Blob Storage without the need for saving it locally first. By utilizing the upload_blob method directly with the audio data, you can effectively streamline the process and reduce unnecessary I/O operations and latency.
Please ensure you replace the placeholder values (e.g., "YOUR_SPEECH_KEY", "YOUR_SERVICE_REGION", "YOUR_STORAGE_CONNECTION_STRING") with your actual Azure Speech service key, service region, and storage connection string respectively.
Blob Upload result.
Hope you understand. Thank you.
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful.