ARver/arver/audio/checksums.py
arcctgx db6f7514ef improve FLAC processing performance
CRC32 support was initially implemented in the C extension as a separate
set of functions instead of integrating it with existing code from the
beginning. Functions for calculating AccurateRip and CRC32 checksums
were called separately in the source code, which resulted in loading
the same audio file twice. For FLAC files this meant double overhead
related to FLAC format decoding.

Refactoring the legacy code in the C extension allowed to merge the
duplicated code into a single function that calculates all checksums
but only loads the audio file once, avoiding the extra overhead. This
results in FLAC processing becoming nearly twice as fast as before. The
improvement is negligible for WAV files.

To adapt the code accuraterip_checksums() and copy_crc() are replaced
with get_checksums() which in turn is called by functions in Rip module.

Adaptation of _audio_test.py is straightforward: since there are no
separate functions for calculating AccurateRip and CRC32 checksums
anymore, it is sufficient to test the exceptions just once. Separate
set of tests for CRC32 became redundant and was removed.

Adaptation of checksums_test.py follows the path of least resistance.
AccurateRip and CRC32 tests remain separate, they simply extract the
relevant part of the triple returned by get_checksums() and verify
that.
2024-03-30 18:23:21 +01:00

26 lines
1 KiB
Python

"""Functions for calculating checksums of audio files."""
from typing import Tuple
from arver.audio import _audio # type: ignore
# pylint: disable=c-extension-no-member
def get_checksums(path: str, track_no: int, total_tracks: int) -> Tuple[int, int, int]:
"""
Calculate AccurateRip and CRC32 checksums of specified file.
Return a triple of checksums (v1, v2, crc32) as unsigned integers.
This function supports WAV and FLAC files compliant with CDDA
standard (16-bit stereo LPCM, 44100 Hz). Underlying C extension
will raise TypeError for any other audio format, or OSError when
libsndfile can't load audio samples from the file for any reason.
The track_no and total_tracks arguments are used to recognize
if the track is the first or the last one on CD. These two
tracks are treated specially by AccurateRip checksum algorithm.
ValueError is raised if the track numbers are not valid. These
arguments don't matter for CRC32 calculation.
"""
return _audio.checksums(path, track_no, total_tracks)