File size: 1,291 Bytes
36e0dea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import soundfile as sf
from core.config import MAX_DURATION_SEC, MIN_DURATION_SEC, ALLOWED_EXTENSIONS
from utils.logger import logger

class AudioValidationError(ValueError):
    pass


def validate_audio_duration(filepath: str, max_duration: float = MAX_DURATION_SEC):
    try:
        info = sf.info(filepath)
    except RuntimeError:
        raise AudioValidationError("Invalid or corrupted audio file.")

    duration = info.frames / float(info.samplerate)

    if duration > max_duration or duration < MIN_DURATION_SEC:
        logger.warning(f"Unsupported file length received: {original_filename}")
        raise AudioValidationError(
            f"Audio duration {duration:.2f}s invalid. "
            f"Allowed range: {MIN_DURATION_SEC:.2f}s – {max_duration:.2f}s."
        )

    return duration

def validate_audio_file(file_path: str, original_filename: str):
    ext = original_filename.lower().rsplit(".", 1)[-1]
    ext = "." + ext

    if ext not in ALLOWED_EXTENSIONS:
        logger.warning(f"Unsupported file format received: {original_filename}")
        raise AudioValidationError(
            f"Unsupported file type {ext}. Allowed formats are: " + ", ".join(ALLOWED_EXTENSIONS)
            )

    validate_audio_duration(file_path)