import librosa
import numpy as np
from dataclasses import dataclass


# datova trida pro uchovani nastaveni nahravek
# sr - vzorkovaci frekvence
# frame_length - delka casoveho ramce
# hop_length - vzdalenost zacatku casovych ramcu
@dataclass
class Settings:
    sr: int = 22050
    frame_length: int = 2248
    hop_length: int = 512

# datova trida pro nacitani nahravek a uchovani nactenych dat
# path - cesta k souboru
# data - data zvukove nahravky
@dataclass
class WavData:
    stngs: Settings
    path: str
    data: np.ndarray = 0

    # nacte data ze souboru s definovanou vzorkovaci frekvenci
    # normalizuje nactena data
    def load_wav(self, range=None):
        if range is not None:
            offset = range[0]
            duration = range[1] - range[0]
            self.data, self.stngs.sr = librosa.load(self.path, sr=self.stngs.sr, offset=offset, duration=duration)
        else:
            self.data, self.stngs.sr = librosa.load(self.path, sr=self.stngs.sr, res_type="kaiser_fast")
        self.data = librosa.util.normalize(self.data, axis=0)
        return self.data
