Skip to content

Utils

check_path(filepath)

Check provided path if something wrong raise error.

Parameters:

Name Type Description Default
filepath str

path to check.

required
Source code in asrbench\utils.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def check_path(filepath: str) -> None:
    """Check provided path if something wrong raise error.

    Parameters:
        filepath: path to check.
    """
    if not filepath:
        raise ValueError("Empty path provided.")

    path = Path(filepath)

    if not path.exists():
        raise FileNotFoundError(f"File {path.name} in {path} does not exists.")

get_audio_duration(audio_path)

Get audio duration in seconds.

Parameters:

Name Type Description Default
audio_path str

path to audio file.

required

Returns:

Type Description
float

duration of audio file in seconds.

Source code in asrbench\utils.py
62
63
64
65
66
67
68
69
70
71
72
73
def get_audio_duration(audio_path: str) -> float:
    """Get audio duration in seconds.

    Parameters:
        audio_path: path to audio file.

    Returns:
        duration of audio file in seconds.
    """
    audio = AudioSegment.from_wav(audio_path)
    if audio is not None:
        return round(len(audio) / 1000, 3)

get_filename(filepath_)

Get filename by path provided.

Parameters:

Name Type Description Default
filepath_ str

entire path to file.

required

Returns:

Type Description
str

name of file.

Source code in asrbench\utils.py
21
22
23
24
25
26
27
28
29
30
def get_filename(filepath_: str) -> str:
    """Get filename by path provided.

    Parameters:
        filepath_: entire path to file.

    Returns:
        name of file.
    """
    return Path(filepath_).name

get_rtf(runtime, duration)

Calculate Real Time Factor [RTF] in seconds.

Parameters:

Name Type Description Default
runtime float

time for execution in seconds.

required
duration float

audio duration in seconds.

required

Returns:

Type Description
float

real time factor for data provided.

Source code in asrbench\utils.py
48
49
50
51
52
53
54
55
56
57
58
59
def get_rtf(runtime: float, duration: float) -> float:
    """Calculate Real Time Factor [RTF] in seconds.

    Parameters:
        runtime: time for execution in seconds.
        duration: audio duration in seconds.

    Returns:
        real time factor for data provided.
    """
    rtf: float = runtime / duration
    return round(rtf, 3)

get_runtime(start)

Calculate runtime by start time provided.

Parameters:

Name Type Description Default
start float

start time in ms.

required

Returns:

Type Description
float

time since start in seconds.

Source code in asrbench\utils.py
33
34
35
36
37
38
39
40
41
42
43
44
45
def get_runtime(start: float) -> float:
    """Calculate runtime by start time provided.

    Parameters:
        start: start time in ms.

    Returns:
        time since start in seconds.
    """
    return round(
        (time.time() - start),
        3,
    )