diff --git a/audalign/__init__.py b/audalign/__init__.py index 70382b5..196a4a4 100644 --- a/audalign/__init__.py +++ b/audalign/__init__.py @@ -807,6 +807,7 @@ def uniform_level_file( width: float = 5, overlap_ratio: float = 0.5, exclude_min_db: float = -70, + config: BaseConfig = FingerprintConfig(), ) -> None: """ Levels the file using either of two methods: normalize or average. @@ -838,6 +839,7 @@ def uniform_level_file( width=width, overlap_ratio=overlap_ratio, exclude_min_db=exclude_min_db, + base_config=config, ) @@ -851,6 +853,7 @@ def uniform_level_directory( exclude_min_db: float = -70, multiprocessing: bool = True, num_processors: int = None, + config: BaseConfig = FingerprintConfig(), ) -> None: """ Levels the file using either of two methods: normalize or average. @@ -886,6 +889,7 @@ def uniform_level_directory( exclude_min_db=exclude_min_db, use_multiprocessing=multiprocessing, num_processes=num_processors, + config=config, ) @@ -897,6 +901,7 @@ def remove_noise_file( write_extension: str = None, alt_noise_filepath: str = None, prop_decrease: float = 1, + config: BaseConfig = FingerprintConfig(), **kwargs, ): """Remove noise from audio file by specifying start and end seconds of representative sound sections. Writes file to destination @@ -920,6 +925,7 @@ def remove_noise_file( write_extension=write_extension, alt_noise_filepath=alt_noise_filepath, prop_decrease=prop_decrease, + config=config, **kwargs, ) @@ -934,6 +940,7 @@ def remove_noise_directory( prop_decrease: float = 1, multiprocessing: bool = True, num_processors: int = None, + config: BaseConfig = FingerprintConfig(), **kwargs, ): """Remove noise from audio files in directory by specifying start and end seconds of @@ -962,6 +969,7 @@ def remove_noise_directory( prop_decrease=prop_decrease, use_multiprocessing=multiprocessing, num_processes=num_processors, + config=config, **kwargs, ) diff --git a/audalign/filehandler.py b/audalign/filehandler.py index 658f556..5e25354 100644 --- a/audalign/filehandler.py +++ b/audalign/filehandler.py @@ -212,6 +212,7 @@ def noise_remove( write_extension: str = None, alt_noise_filepath=None, prop_decrease=1, + config: BaseConfig = FingerprintConfig(), **kwargs, ): audiofile = create_audiosegment(filepath) @@ -238,6 +239,7 @@ def noise_remove( sr=BaseConfig.sample_rate, y_noise=noisy_part, prop_decrease=prop_decrease, + base_config=config, **kwargs, ) @@ -270,6 +272,7 @@ def noise_remove_directory( prop_decrease=1, use_multiprocessing=False, num_processes=None, + config: BaseConfig = FingerprintConfig(), **kwargs, ): noise_data = _floatify_data(create_audiosegment(noise_filepath))[ @@ -285,6 +288,7 @@ def noise_remove_directory( destination_directory=destination_directory, prop_decrease=prop_decrease, write_extension=write_extension, + base_config=config, **kwargs, ) @@ -395,6 +399,7 @@ def uniform_level_directory( exclude_min_db=-70, use_multiprocessing=False, num_processes=None, + config: BaseConfig = FingerprintConfig(), ): _uniform_level_ = partial( _uniform_level, @@ -404,6 +409,7 @@ def uniform_level_directory( width=width, overlap_ratio=overlap_ratio, exclude_min_db=exclude_min_db, + base_config=config, ) if use_multiprocessing == True: diff --git a/audalign/recognizers/__init__.py b/audalign/recognizers/__init__.py index 4a63148..75bfd69 100644 --- a/audalign/recognizers/__init__.py +++ b/audalign/recognizers/__init__.py @@ -4,6 +4,7 @@ import audalign.filehandler as filehandler from audalign.config import BaseConfig +from pydub.exceptions import CouldntDecodeError class BaseRecognizer(ABC): @@ -47,10 +48,18 @@ def align_get_file_names( file_names = [os.path.basename(x) for x in file_list] elif file_dir: file_names = filehandler.get_audio_files_directory( - file_dir, False, self.config.can_read_extensions, self.config.cant_read_extensions) + file_dir, + False, + self.config.can_read_extensions, + self.config.cant_read_extensions, + ) elif fine_aud_file_dict: + if fine_aud_file_dict == None or len(fine_aud_file_dict.keys()) == 0: + raise CouldntDecodeError("No files found", fine_aud_file_dict) file_names = [os.path.basename(x) for x in fine_aud_file_dict.keys()] else: + if file_list == None or len(file_list) == 0: + raise CouldntDecodeError("No files found", file_list) file_names = [os.path.basename(x) for x in file_list] return file_names diff --git a/audalign/recognizers/correcognize/correcognize.py b/audalign/recognizers/correcognize/correcognize.py index c3f98ba..bad0f24 100644 --- a/audalign/recognizers/correcognize/correcognize.py +++ b/audalign/recognizers/correcognize/correcognize.py @@ -336,8 +336,10 @@ def _correcognize_dir( **kwargs, ) - except CouldntDecodeError: + except CouldntDecodeError as e: print(f'File "{against_file_path}" could not be decoded') + if config.fail_on_decode_error: + raise e return {} diff --git a/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py b/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py index 5cb5552..289ec15 100644 --- a/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py +++ b/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py @@ -336,8 +336,10 @@ def _correcognize_dir( **kwargs, ) - except CouldntDecodeError: + except CouldntDecodeError as e: print(f'File "{against_file_path}" could not be decoded') + if config.fail_on_decode_error: + raise e return {} diff --git a/audalign/recognizers/fingerprint/__init__.py b/audalign/recognizers/fingerprint/__init__.py index 8f5f4e1..3adbb18 100644 --- a/audalign/recognizers/fingerprint/__init__.py +++ b/audalign/recognizers/fingerprint/__init__.py @@ -10,6 +10,7 @@ import pickle import json import typing +from pydub.exceptions import CouldntDecodeError class FingerprintRecognizer(BaseRecognizer): @@ -266,6 +267,8 @@ def _fingerprint_directory( print("All files in directory already fingerprinted") else: print("Directory contains 0 files or could not be found") + if self.config.fail_on_decode_error: + raise CouldntDecodeError("Directory contains 0 files or could not be found") return if _file_audsegs is not None: diff --git a/audalign/recognizers/fingerprint/fingerprinter.py b/audalign/recognizers/fingerprint/fingerprinter.py index 4d73137..f8adfff 100644 --- a/audalign/recognizers/fingerprint/fingerprinter.py +++ b/audalign/recognizers/fingerprint/fingerprinter.py @@ -52,10 +52,12 @@ def _fingerprint_worker( except FileNotFoundError: print(f'"{file_path}" not found') return None, None - except ( - CouldntDecodeError, - IndexError, - ): # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?) + except CouldntDecodeError as e: + print(f'File "{file_name}" could not be decoded') + if config.fail_on_decode_error: + raise e + return None, None + except IndexError: # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?) print(f'File "{file_name}" could not be decoded') return None, None elif type(file_path) == tuple: diff --git a/audalign/recognizers/visrecognize/visrecognize.py b/audalign/recognizers/visrecognize/visrecognize.py index 9d8a540..6b660dc 100644 --- a/audalign/recognizers/visrecognize/visrecognize.py +++ b/audalign/recognizers/visrecognize/visrecognize.py @@ -176,8 +176,10 @@ def _visrecognize_directory( imgB_title=os.path.basename(file_path), ) return single_file_match - except CouldntDecodeError: + except CouldntDecodeError as e: print(f'File "{file_path}" could not be decoded') + if config.fail_on_decode_error: + raise e return {} diff --git a/tests/test_recognize.py b/tests/test_recognize.py index 2517b89..6dc101b 100644 --- a/tests/test_recognize.py +++ b/tests/test_recognize.py @@ -1,6 +1,11 @@ +from pydub.exceptions import CouldntDecodeError import audalign as ad import os import pytest +from audalign.config.correlation import CorrelationConfig + +from audalign.config.fingerprint import FingerprintConfig + try: import skimage except ImportError: @@ -94,12 +99,26 @@ def test_fingerprint_directory_not_there_already_done(self): fingerprint_recognizer.load_fingerprinted_files("tests/test_fingerprints.json") fingerprint_recognizer.fingerprint_directory("test_audio/test_shifts") - def test_fingerprint_bad_file(self): + def test_fingerprint_bad_file_should_fail(self): fingerprint_recognizer = ad.FingerprintRecognizer() assert len(fingerprint_recognizer.fingerprinted_files) == 0 assert len(fingerprint_recognizer.file_names) == 0 assert fingerprint_recognizer.total_fingerprints == 0 + # both should print something and be just fine + with pytest.raises(CouldntDecodeError): + fingerprint_recognizer.fingerprint_file("filenot_even_there.txt") + with pytest.raises(CouldntDecodeError): + fingerprint_recognizer.fingerprint_file("requirements.txt") + + def test_fingerprint_bad_file(self): + config = FingerprintConfig() + config.fail_on_decode_error = False + fingerprint_recognizer = ad.FingerprintRecognizer(config) + assert len(fingerprint_recognizer.fingerprinted_files) == 0 + assert len(fingerprint_recognizer.file_names) == 0 + assert fingerprint_recognizer.total_fingerprints == 0 + # both should print something and be just fine fingerprint_recognizer.fingerprint_file("filenot_even_there.txt") fingerprint_recognizer.fingerprint_file("requirements.txt") @@ -326,8 +345,19 @@ def test_correcognize_locality_max_lags(self): assert min(offset_seconds) < _max_lags assert max(offset_seconds) < _max_lags - def test_correcognize_directory_no_return(self): + def test_correcognize_directory_fail_on_decode(self): recognizer = ad.CorrelationRecognizer() + with pytest.raises(CouldntDecodeError): + ad.recognize( + test_file, + "tests/", + recognizer=recognizer, + ) + + def test_correcognize_directory_no_return(self): + config = CorrelationConfig() + config.fail_on_decode_error = False + recognizer = ad.CorrelationRecognizer(config) results = ad.recognize( test_file, "tests/",