SE-09/HapticsProject
2
1from azure.storage.blob import BlobServiceClient2from io import BytesIO3from pydub import AudioSegment4import os5import ffmpeg6 7audio_clip_mapping = {8 "key1": "3_second_explosion_00001.flac",9 # Add more key-value pairs as needed10}11 12# Initialize Azure Blob Storage client13account_name = 'phonebrrdemonstration2'14account_key = 'Q+EneUx5hlODHCjsSo49mm1bGVNdBd2wZ/T0yZMtag1C6FUIwr/yKf+XqDPmVF1PU81eitB2L3tN+AStD/eZ+A=='15 16blob_service_client = BlobServiceClient(17 account_url=f"https://{account_name}.blob.core.windows.net",18 credential=account_key19)20 21# Define the container name22container_name = 'audio3second0001' # should define the what container should access 23 24# Define a function to retrieve audio clips by key25def retrieve_audio_clip(key):26 # Get the blob name corresponding to the key27 blob_name = audio_clip_mapping.get(key)28 if blob_name is None:29 print(f"No audio clip found for key: {key}")30 return None31 32 # Get the blob client for the audio clip33 blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)34 35 # Stream the audio data directly into an AudioSegment object36 stream = BytesIO()37 blob_client.download_blob().download_to_stream(stream)38 stream.seek(0)39 40 audio_segment = AudioSegment.from_file(stream)41 stream.close() # Close the stream to avoid memory leaks42 return audio_segment43 44# Example usage:45key_to_retrieve = "key1"46retrieved_audio_clip = retrieve_audio_clip(key_to_retrieve)47 48if retrieved_audio_clip:49 print(f"Retrieved audio clip for key {key_to_retrieve}")50 # Further process retrieved_audio_clip as needed