@@ -581,11 +581,6 @@ class RandomAudioPitchShift(Augmentor):
581581 augment_annotation (bool): Whether to augment the annotation. Defaults to False.
582582 max_n_steps (int): Maximum number of steps to shift audio. Defaults to 5.
583583 """
584- try :
585- import librosa
586- # samplerate
587- except ImportError :
588- raise ImportError ("librosa is required to augment Audio. Please install it with `pip install librosa`." )
589584 def __init__ (
590585 self ,
591586 random_chance : float = 0.5 ,
@@ -596,6 +591,12 @@ def __init__(
596591 super (RandomAudioPitchShift , self ).__init__ (random_chance , log_level , augment_annotation )
597592 self .max_n_steps = max_n_steps
598593
594+ try :
595+ import librosa
596+ # samplerate
597+ except ImportError :
598+ raise ImportError ("librosa is required to augment Audio. Please install it with `pip install librosa`." )
599+
599600 def augment (self , audio : Audio ) -> Audio :
600601 random_n_steps = np .random .randint (- self .max_n_steps , self .max_n_steps )
601602 # changing default res_type "kaiser_best" to "linear" for speed and memory efficiency
@@ -617,10 +618,6 @@ class RandomAudioTimeStretch(Augmentor):
617618 min_rate (float): Minimum rate to stretch audio. Defaults to 0.8.
618619 max_rate (float): Maximum rate to stretch audio. Defaults to 1.2.
619620 """
620- try :
621- import librosa
622- except ImportError :
623- raise ImportError ("librosa is required to augment Audio. Please install it with `pip install librosa`." )
624621 def __init__ (
625622 self ,
626623 random_chance : float = 0.5 ,
@@ -633,6 +630,11 @@ def __init__(
633630 self .min_rate = min_rate
634631 self .max_rate = max_rate
635632
633+ try :
634+ import librosa
635+ except ImportError :
636+ raise ImportError ("librosa is required to augment Audio. Please install it with `pip install librosa`." )
637+
636638 def augment (self , audio : Audio ) -> Audio :
637639 random_rate = np .random .uniform (self .min_rate , self .max_rate )
638640 stretch_audio = self .librosa .effects .time_stretch (audio .numpy (), rate = random_rate )
0 commit comments