import os import soundfile as sf import hashlib def ensure_dir(directory): """Ensure that a directory exists""" if not os.path.exists(directory): os.makedirs(directory) def get_audio_filename(text, language, speaker, emotion, speed, pitch, background_noise, reverberation, quality): """Generate a unique filename based on input parameters""" # Create a string containing all parameters params = f"{text}{language}{speaker}{emotion}{speed}{pitch}{background_noise}{reverberation}{quality}" # Create a hash of the parameters filename = hashlib.md5(params.encode()).hexdigest() return filename def save_audio(audio_array, filename, sampling_rate=22050): """Save audio array to a file""" ensure_dir("static/audio") filepath = f"static/audio/{filename}.wav" sf.write(filepath, audio_array, sampling_rate) return filepath def get_cached_audio(text, language, speaker, emotion, speed, pitch, background_noise, reverberation, quality): """Get cached audio if it exists""" filename = get_audio_filename(text, language, speaker, emotion, speed, pitch, background_noise, reverberation, quality) filepath = f"static/audio/{filename}.wav" if os.path.exists(filepath): return filepath return None