Skip to content

Data Processing

Data processing utilities for audio files.

This module provides functions to load and split audio files into segments.

Author: Esgr0bar

load_audio(file_path, sr=44100)

Load an audio file.

Parameters:

Name Type Description Default
file_path str

Path to the audio file.

required
sr int

Sample rate for loading audio.

44100

Returns:

Name Type Description
tuple

Tuple containing the audio time series (numpy.ndarray) and sample rate (int).

Source code in src/data_processing.py
11
12
13
14
15
16
17
18
19
20
21
22
23
def load_audio(file_path, sr=44100):
    """
    Load an audio file.

    Args:
        file_path (str): Path to the audio file.
        sr (int): Sample rate for loading audio.

    Returns:
        tuple: Tuple containing the audio time series (numpy.ndarray) and sample rate (int).
    """
    y, sr = librosa.load(file_path, sr=sr)
    return y, sr

split_tracks(file_path, segment_length=5)

Split an audio track into smaller segments.

Parameters:

Name Type Description Default
file_path str

Path to the audio file.

required
segment_length int

Length of each segment in seconds.

5

Returns:

Name Type Description
list

A list of audio segments (numpy.ndarray).

Source code in src/data_processing.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def split_tracks(file_path, segment_length=5):
    """
    Split an audio track into smaller segments.

    Args:
        file_path (str): Path to the audio file.
        segment_length (int): Length of each segment in seconds.

    Returns:
        list: A list of audio segments (numpy.ndarray).
    """
    y, sr = load_audio(file_path)
    segments = [y[i:i + sr * segment_length] for i in range(0, len(y), sr * segment_length)]
    return segments