55import matplotlib
66import logging
77
8+ try :
9+ import librosa
10+ except :
11+ print ("librosa not found. Please install it with `pip install librosa` if you plan to use it." )
12+
813from . import Image
914from mltu .annotations .audio import Audio
1015
@@ -67,7 +72,6 @@ def __init__(
6772 self .logger .setLevel (log_level )
6873
6974 try :
70- import librosa
7175 librosa .__version__
7276 except AttributeError :
7377 raise ImportError ("librosa is required to read WAV files. Please install it with `pip install librosa`." )
@@ -89,7 +93,7 @@ def __call__(self, audio_path: str, label: typing.Any) -> typing.Tuple[np.ndarra
8993 else :
9094 raise TypeError (f"Audio { audio_path } is not a string." )
9195
92- audio = Audio (audio_path , sample_rate = self .sample_rate , library = self . librosa )
96+ audio = Audio (audio_path , sample_rate = self .sample_rate , library = librosa )
9397
9498 if not audio .init_successful :
9599 audio = None
@@ -120,7 +124,6 @@ def __init__(
120124 matplotlib .interactive (False )
121125 # Check if librosa is installed
122126 try :
123- import librosa
124127 librosa .__version__
125128 except AttributeError :
126129 raise ImportError ("librosa is required to read WAV files. Please install it with `pip install librosa`." )
@@ -139,12 +142,12 @@ def get_spectrogram(wav_path: str, frame_length: int, frame_step: int, fft_lengt
139142 np.ndarray: Spectrogram of the WAV file.
140143 """
141144 # Load the wav file and store the audio data in the variable 'audio' and the sample rate in 'orig_sr'
142- audio , orig_sr = WavReader . librosa .load (wav_path )
145+ audio , orig_sr = librosa .load (wav_path )
143146
144147 # Compute the Short Time Fourier Transform (STFT) of the audio data and store it in the variable 'spectrogram'
145148 # The STFT is computed with a hop length of 'frame_step' samples, a window length of 'frame_length' samples, and 'fft_length' FFT components.
146149 # The resulting spectrogram is also transposed for convenience
147- spectrogram = WavReader . librosa .stft (audio , hop_length = frame_step , win_length = frame_length , n_fft = fft_length ).T
150+ spectrogram = librosa .stft (audio , hop_length = frame_step , win_length = frame_length , n_fft = fft_length ).T
148151
149152 # Take the absolute value of the spectrogram to obtain the magnitude spectrum
150153 spectrogram = np .abs (spectrogram )
@@ -168,7 +171,7 @@ def plot_raw_audio(wav_path: str, title: str = None, sr: int = 16000) -> None:
168171 title (str, optional): Title
169172 """
170173 # Load the wav file and store the audio data in the variable 'audio' and the sample rate in 'orig_sr'
171- audio , orig_sr = WavReader . librosa .load (wav_path , sr = sr )
174+ audio , orig_sr = librosa .load (wav_path , sr = sr )
172175
173176 duration = len (audio ) / orig_sr
174177
0 commit comments