diff --git a/batdetect2/utils/audio_utils.py b/batdetect2/utils/audio_utils.py index 134fee7..a2cfe44 100644 --- a/batdetect2/utils/audio_utils.py +++ b/batdetect2/utils/audio_utils.py @@ -9,7 +9,6 @@ import torch import audioread import os import soundfile as sf -import io from batdetect2.detector import parameters @@ -17,7 +16,7 @@ from . import wavfile __all__ = [ "load_audio", - "load_audio_data", + "load_audio_and_samplerate", "generate_spectrogram", "pad_audio", ] @@ -174,10 +173,10 @@ def load_audio( ValueError: If the audio file is stereo. """ - sample_rate, audio_data, _ = load_audio_data(path, time_exp_fact, target_samp_rate, scale, max_duration) + sample_rate, audio_data, _ = load_audio_and_samplerate(path, time_exp_fact, target_samp_rate, scale, max_duration) return sample_rate, audio_data -def load_audio_data( +def load_audio_and_samplerate( path: Union[ str, int, os.PathLike[Any], sf.SoundFile, audioread.AudioFile, BinaryIO ], @@ -200,6 +199,7 @@ def load_audio_data( Returns: sampling_rate: The sampling rate of the audio. audio_raw: The audio signal in a numpy array. + file_sampling_rate: The original sampling rate of the audio Raises: ValueError: If the audio file is stereo. diff --git a/batdetect2/utils/detector_utils.py b/batdetect2/utils/detector_utils.py index f6e5776..93155b1 100644 --- a/batdetect2/utils/detector_utils.py +++ b/batdetect2/utils/detector_utils.py @@ -2,7 +2,6 @@ import json import os from typing import Any, Iterator, List, Optional, Tuple, Union, BinaryIO -import librosa import numpy as np import pandas as pd import torch @@ -759,6 +758,9 @@ def process_file( config : ProcessingConfiguration Configuration for processing. + + file_id: Optional[str], + Give the data an id. Defaults to the filename if path is a string. Otherwise Returns ------- @@ -773,7 +775,7 @@ def process_file( spec_slices = [] # load audio file - sampling_rate, audio_full, file_samp_rate = au.load_audio_data( + sampling_rate, audio_full, file_samp_rate = au.load_audio_and_samplerate( path, time_exp_fact=config.get("time_expansion", 1) or 1, target_samp_rate=config["target_samp_rate"], diff --git a/tests/test_audio_utils.py b/tests/test_audio_utils.py index a6c08fb..ed64b15 100644 --- a/tests/test_audio_utils.py +++ b/tests/test_audio_utils.py @@ -146,9 +146,9 @@ def test_load_audio_using_bytesio(): with open(path, "rb") as f: data = io.BytesIO(f.read()) - sample_rate, audio_data, file_sample_rate = audio_utils.load_audio_data(data, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ) + sample_rate, audio_data, file_sample_rate = audio_utils.load_audio_and_samplerate(data, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ) - expected_sample_rate, expected_audio_data, exp_file_sample_rate = audio_utils.load_audio_data(path, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ) + expected_sample_rate, expected_audio_data, exp_file_sample_rate = audio_utils.load_audio_and_samplerate(path, time_exp_fact=1, target_samp_rate=parameters.TARGET_SAMPLERATE_HZ) assert expected_sample_rate == sample_rate assert exp_file_sample_rate == file_sample_rate