File size: 1,587 Bytes
d7efa84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Lazy per-item audio loading and clean audio copy."""

import shutil
import logging
from pathlib import Path

logger = logging.getLogger(__name__)


def load_audio_bytes(folder_path: str, filename: str) -> bytes:
    """Load audio bytes for a single file on demand.

    Args:
        folder_path: Path to the audio folder.
        filename: Name of the WAV file.

    Returns:
        Raw bytes of the audio file.

    Raises:
        FileNotFoundError: If the audio file does not exist.
    """
    audio_path = Path(folder_path) / filename
    if not audio_path.exists():
        raise FileNotFoundError(f"Audio file not found: {filename}")

    return audio_path.read_bytes()


def copy_to_clean(source_folder: str, filename: str, clean_audios_dir: str) -> None:
    """Copy accepted audio to the clean audios folder.

    Creates the clean audios directory if it doesn't exist.

    Args:
        source_folder: Path to the source audio folder.
        filename: Name of the WAV file to copy.
        clean_audios_dir: Path to the destination clean audios folder.

    Raises:
        IOError: If the copy fails.
    """
    src = Path(source_folder) / filename
    dst_dir = Path(clean_audios_dir)
    dst_dir.mkdir(parents=True, exist_ok=True)
    dst = dst_dir / filename

    try:
        shutil.copy2(str(src), str(dst))
        logger.info(f"Copied '{filename}' to clean audios: {clean_audios_dir}")
    except Exception as e:
        logger.error(f"Failed to copy '{filename}' to clean audios: {e}")
        raise IOError(f"Failed to copy audio file '{filename}'.") from e