File size: 2,128 Bytes
036e934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f1dadd
036e934
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Utility functions for VibeVoice TTS Tester
"""

import os
import tempfile
import base64
from typing import Optional, Tuple


def format_duration(seconds: float) -> str:
    """Format duration in seconds to human-readable string."""
    if seconds < 60:
        return f"{seconds:.1f}s"
    minutes = int(seconds // 60)
    secs = seconds % 60
    return f"{minutes}m {secs:.1f}s"


def get_audio_duration(audio_path: str) -> Optional[float]:
    """Get the duration of an audio file in seconds."""
    try:
        import wave
        with wave.open(audio_path, 'rb') as audio_file:
            frames = audio_file.getnframes()
            rate = audio_file.getframerate()
            duration = frames / float(rate)
            return duration
    except Exception:
        return None


def create_temp_audio_file(audio_bytes: bytes, suffix: str = ".wav") -> str:
    """Create a temporary audio file and return its path."""
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_file:
        tmp_file.write(audio_bytes)
        return tmp_file.name


def audio_to_base64(audio_path: str) -> str:
    """Convert audio file to base64 string for embedding."""
    with open(audio_path, 'rb') as audio_file:
        audio_bytes = audio_file.read()
        return base64.b64encode(audio_bytes).decode('utf-8')


def validate_text_input(text: str, max_length: int = 5000) -> Tuple[bool, str]:
    """Validate text input for TTS."""
    if not text or not text.strip():
        return False, "Text cannot be empty."
    
    if len(text) > max_length:
        return False, f"Text exceeds maximum length of {max_length} characters."
    
    return True, "Valid input."


def estimate_generation_time(text: str) -> float:
    """Estimate the generation time based on text length."""
    base_time = 2.0
    char_time = len(text) * 0.05
    return base_time + char_time


def cleanup_temp_files(file_paths: list) -> None:
    """Clean up temporary files."""
    for path in file_paths:
        try:
            if os.path.exists(path):
                os.remove(path)
        except Exception:
            pass