From acb31b7ebec36acbbc99d70d16bc8545e03b1135 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Sat, 19 Oct 2019 12:26:58 +0200 Subject: [PATCH] PICARD-1652: Use TAK implementation from mutagen if available Fallback to our existing more simple implementation. --- picard/formats/mutagenext/tak.py | 73 ++++++++++++++++++++------------ test/formats/test_apev2.py | 11 +++++ 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/picard/formats/mutagenext/tak.py b/picard/formats/mutagenext/tak.py index 3861cc855..cc21caf53 100644 --- a/picard/formats/mutagenext/tak.py +++ b/picard/formats/mutagenext/tak.py @@ -18,43 +18,62 @@ and http://en.wikipedia.org/wiki/TAK_(audio_codec) __all__ = ["TAK", "Open", "delete"] -from mutagen import StreamInfo -from mutagen.apev2 import ( - APEv2File, - delete, - error, -) +try: + from mutagen.tak import ( + Open, + TAK, + TAKHeaderError, + TAKInfo, + delete + ) + native_tak = True -class TAKHeaderError(error): - pass +except ImportError: + from mutagen import StreamInfo + from mutagen.apev2 import ( + APEv2File, + delete, + error, + ) + native_tak = False -class TAKInfo(StreamInfo): + class TAKHeaderError(error): + pass - """TAK stream information. + class TAKInfo(StreamInfo): - Attributes: - (none at the moment) - """ + """TAK stream information. - def __init__(self, fileobj): - header = fileobj.read(4) - if len(header) != 4 or not header.startswith(b"tBaK"): - raise TAKHeaderError("not a TAK file") + Attributes: + (none at the moment) + """ - @staticmethod - def pprint(): - return "Tom's lossless Audio Kompressor" + def __init__(self, fileobj): + header = fileobj.read(4) + if len(header) != 4 or not header.startswith(b"tBaK"): + raise TAKHeaderError("not a TAK file") + @staticmethod + def pprint(): + return "Tom's lossless Audio Kompressor" -class TAK(APEv2File): - _Info = TAKInfo - _mimes = ["audio/x-tak"] + class TAK(APEv2File): + """TAK(filething) - @staticmethod - def score(filename, fileobj, header): - return header.startswith(b"tBaK") + filename.lower().endswith(".tak") + Arguments: + filething (filething) + Attributes: + info (`TAKInfo`) + """ -Open = TAK + _Info = TAKInfo + _mimes = ["audio/x-tak"] + + @staticmethod + def score(filename, fileobj, header): + return header.startswith(b"tBaK") + filename.lower().endswith(".tak") + + Open = TAK diff --git a/test/formats/test_apev2.py b/test/formats/test_apev2.py index 162f5cf0e..15e80d564 100644 --- a/test/formats/test_apev2.py +++ b/test/formats/test_apev2.py @@ -12,6 +12,7 @@ from picard.formats import ( apev2, open_, ) +from picard.formats.mutagenext.tak import native_tak from picard.metadata import Metadata from .common import ( @@ -179,6 +180,16 @@ class TAKTest(CommonApeTests.ApeTestCase): supports_ratings = False unexpected_info = ['~video'] + def setUp(self): + super().setUp() + if native_tak: + self.expected_info = { + 'length': 82, + '~channels': '2', + '~sample_rate': '44100', + '~bits_per_sample': '16' + } + class OptimFROGLosslessTest(CommonApeTests.ApeTestCase): testfile = 'test.ofr'