macaodha commited on
Commit
b3aa5aa
1 Parent(s): 73fd754

Update bat_detect/utils/audio_utils.py

Browse files
Files changed (1) hide show
  1. bat_detect/utils/audio_utils.py +6 -1
bat_detect/utils/audio_utils.py CHANGED
@@ -64,7 +64,7 @@ def generate_spectrogram(audio, sampling_rate, params, return_spec_for_viz=False
64
  return spec, spec_for_viz
65
 
66
 
67
- def load_audio_file(audio_file, time_exp_fact, target_samp_rate, scale=False):
68
  with warnings.catch_warnings():
69
  warnings.filterwarnings('ignore', category=wavfile.WavFileWarning)
70
  #sampling_rate, audio_raw = wavfile.read(audio_file)
@@ -79,6 +79,11 @@ def load_audio_file(audio_file, time_exp_fact, target_samp_rate, scale=False):
79
  sampling_rate = target_samp_rate
80
  audio_raw = librosa.resample(audio_raw, orig_sr=sampling_rate_old, target_sr=sampling_rate, res_type='polyphase')
81
 
 
 
 
 
 
82
  # convert to float32 and scale
83
  audio_raw = audio_raw.astype(np.float32)
84
  if scale:
64
  return spec, spec_for_viz
65
 
66
 
67
+ def load_audio_file(audio_file, time_exp_fact, target_samp_rate, scale=False, max_duration=False):
68
  with warnings.catch_warnings():
69
  warnings.filterwarnings('ignore', category=wavfile.WavFileWarning)
70
  #sampling_rate, audio_raw = wavfile.read(audio_file)
79
  sampling_rate = target_samp_rate
80
  audio_raw = librosa.resample(audio_raw, orig_sr=sampling_rate_old, target_sr=sampling_rate, res_type='polyphase')
81
 
82
+ # clipping maximum duration
83
+ if max_duration is not False:
84
+ max_duration = np.minimum(int(sampling_rate*max_duration), audio_raw.shape[0])
85
+ audio_raw = audio_raw[:max_duration]
86
+
87
  # convert to float32 and scale
88
  audio_raw = audio_raw.astype(np.float32)
89
  if scale: