2020from osekit .core .audio_item import AudioItem
2121from osekit .core .base_data import BaseData
2222from osekit .core .instrument import Instrument
23- from osekit .utils .audio import Normalization , normalize
23+ from osekit .utils .audio import Butterworth , Normalization , normalize
2424
2525if TYPE_CHECKING :
2626 from pathlib import Path
@@ -45,6 +45,7 @@ def __init__(
4545 instrument : Instrument | None = None ,
4646 normalization : Normalization = Normalization .RAW ,
4747 normalization_values : dict | None = None ,
48+ butter : Butterworth | None = None ,
4849 ) -> None :
4950 """Initialize an ``AudioData`` from a list of ``AudioItems``.
5051
@@ -67,13 +68,16 @@ def __init__(
6768 the wav audio data.
6869 normalization: Normalization
6970 The type of normalization to apply to the audio data.
71+ butter: Butterworth | None
72+ Butterworth filter to apply to the audio data.
7073
7174 """
7275 super ().__init__ (items = items , begin = begin , end = end , name = name )
7376 self ._set_sample_rate (sample_rate = sample_rate )
7477 self .instrument = instrument
7578 self .normalization = normalization
7679 self .normalization_values = normalization_values
80+ self .butter = butter
7781
7882 @property
7983 def nb_channels (self ) -> int :
@@ -123,6 +127,15 @@ def normalization_values(self, value: dict | None) -> None:
123127 }
124128 )
125129
130+ @property
131+ def butter (self ) -> Butterworth :
132+ """The Butterworth filter to apply to the audio data."""
133+ return self ._butter
134+
135+ @butter .setter
136+ def butter (self , value : Butterworth ) -> None :
137+ self ._butter = value
138+
126139 @classmethod
127140 def _make_item (
128141 cls ,
@@ -178,7 +191,7 @@ def get_normalization_values(self) -> dict:
178191 "std": standard deviation used for z-score normalization
179192
180193 """
181- values = np .array (self .get_raw_value ())
194+ values = np .array (self .get_filtered_value ())
182195 self .normalization_values = {
183196 "mean" : values .mean (),
184197 "peak" : values .max (),
@@ -222,6 +235,22 @@ def get_raw_value(self) -> np.ndarray:
222235 """
223236 return np .vstack (list (self .stream ()))
224237
238+ def get_filtered_value (self ) -> np .ndarray :
239+ """Return the value of the audio data after filtering.
240+
241+ Returns
242+ -------
243+ np.ndarray:
244+ The value of the audio data filtered by the ``self.butter`` Butterworth filter.
245+
246+ """
247+ output = self .get_raw_value ()
248+ return (
249+ output
250+ if self .butter is None
251+ else self .butter .filter (sig = output , fs = self .sample_rate )
252+ )
253+
225254 @staticmethod
226255 def _flush (
227256 resampler : soxr .ResampleStream ,
@@ -320,7 +349,7 @@ def get_value(self) -> np.ndarray:
320349
321350 """
322351 return normalize (
323- values = self .get_raw_value (),
352+ values = self .get_filtered_value (),
324353 normalization = self .normalization ,
325354 ** self .normalization_values ,
326355 )
@@ -547,9 +576,13 @@ def to_dict(self) -> dict:
547576 None if self .instrument is None else self .instrument .to_dict ()
548577 ),
549578 }
579+ butter_dict = {
580+ "butter" : (None if self .butter is None else self .butter .to_dict ()),
581+ }
550582 return (
551583 base_dict
552584 | instrument_dict
585+ | butter_dict
553586 | {
554587 "sample_rate" : self .sample_rate ,
555588 "normalization" : self .normalization .value ,
@@ -595,6 +628,11 @@ def _from_base_dict(
595628 if dictionary ["instrument" ] is None
596629 else Instrument .from_dict (dictionary ["instrument" ])
597630 )
631+ butter = (
632+ None
633+ if "butter" not in dictionary or dictionary ["butter" ] is None
634+ else Butterworth .from_dict (dictionary ["butter" ])
635+ )
598636 return cls .from_files (
599637 files = files ,
600638 begin = begin ,
@@ -603,6 +641,7 @@ def _from_base_dict(
603641 sample_rate = dictionary ["sample_rate" ],
604642 normalization = Normalization (dictionary ["normalization" ]),
605643 normalization_values = dictionary ["normalization_values" ],
644+ butter = butter ,
606645 )
607646
608647 @classmethod
@@ -641,6 +680,9 @@ def from_files(
641680 normalization: Normalization
642681 The type of normalization to apply to the audio data.
643682
683+ butter: Butterworth
684+ Butterworth filter to apply to the audio data.
685+
644686 Returns
645687 -------
646688 Self:
0 commit comments