Skip to content

Commit

Permalink
Add check for config.fail_on_decode_error
Browse files Browse the repository at this point in the history
  • Loading branch information
benfmiller committed Sep 22, 2024
1 parent 4da464c commit 63911b1
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 10 deletions.
8 changes: 8 additions & 0 deletions audalign/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -838,6 +839,7 @@ def uniform_level_file(
width=width,
overlap_ratio=overlap_ratio,
exclude_min_db=exclude_min_db,
base_config=config,
)


Expand All @@ -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.
Expand Down Expand Up @@ -886,6 +889,7 @@ def uniform_level_directory(
exclude_min_db=exclude_min_db,
use_multiprocessing=multiprocessing,
num_processes=num_processors,
config=config,
)


Expand All @@ -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
Expand All @@ -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,
)

Expand All @@ -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
Expand Down Expand Up @@ -962,6 +969,7 @@ def remove_noise_directory(
prop_decrease=prop_decrease,
use_multiprocessing=multiprocessing,
num_processes=num_processors,
config=config,
**kwargs,
)

Expand Down
6 changes: 6 additions & 0 deletions audalign/filehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -238,6 +239,7 @@ def noise_remove(
sr=BaseConfig.sample_rate,
y_noise=noisy_part,
prop_decrease=prop_decrease,
base_config=config,
**kwargs,
)

Expand Down Expand Up @@ -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))[
Expand All @@ -285,6 +288,7 @@ def noise_remove_directory(
destination_directory=destination_directory,
prop_decrease=prop_decrease,
write_extension=write_extension,
base_config=config,
**kwargs,
)

Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion audalign/recognizers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import audalign.filehandler as filehandler
from audalign.config import BaseConfig
from pydub.exceptions import CouldntDecodeError


class BaseRecognizer(ABC):
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion audalign/recognizers/correcognize/correcognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}


Expand Down
3 changes: 3 additions & 0 deletions audalign/recognizers/fingerprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pickle
import json
import typing
from pydub.exceptions import CouldntDecodeError


class FingerprintRecognizer(BaseRecognizer):
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions audalign/recognizers/fingerprint/fingerprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion audalign/recognizers/visrecognize/visrecognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}


Expand Down
34 changes: 32 additions & 2 deletions tests/test_recognize.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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/",
Expand Down

0 comments on commit 63911b1

Please sign in to comment.