File size: 1,275 Bytes
195bb33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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