File: test_ac3.py

package info (click to toggle)
picard 2.13.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,012 kB
  • sloc: python: 68,818; ansic: 89; makefile: 14
file content (122 lines) | stat: -rw-r--r-- 4,177 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2019 Philipp Wolfer
# Copyright (C) 2020-2021 Laurent Monin
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


import os
import unittest

from picard import config
from picard.formats.ac3 import AC3File
from picard.formats.mutagenext.ac3 import native_ac3
from picard.metadata import Metadata

from .common import (
    CommonTests,
    load_metadata,
    save_and_load_metadata,
)
from .test_apev2 import CommonApeTests


class AC3WithAPETest(CommonApeTests.ApeTestCase):
    testfile = 'test.ac3'
    supports_ratings = False
    expected_info = {
        'length': 106,
        '~bitrate': '192.0',
        '~sample_rate': '44100',
        '~channels': '2',
    }
    unexpected_info = ['~video']

    def setUp(self):
        super().setUp()
        config.setting['ac3_save_ape'] = True
        config.setting['remove_ape_from_ac3'] = True

    @unittest.skipUnless(native_ac3, "mutagen.ac3 not available")
    def test_info(self):
        super().test_info()


class AC3NoTagsTest(CommonTests.BaseFileTestCase):
    testfile = 'test-apev2.ac3'

    def setUp(self):
        super().setUp()
        config.setting['ac3_save_ape'] = False
        config.setting['remove_ape_from_ac3'] = False

    def test_load_but_do_not_save_tags(self):
        metadata = load_metadata(self.filename)
        self.assertEqual('Test AC3 with APEv2 tags', metadata['title'])
        self.assertEqual('The Artist', metadata['artist'])
        metadata['artist'] = 'Foo'
        metadata['title'] = 'Bar'
        metadata = save_and_load_metadata(self.filename, metadata)
        self.assertEqual('Test AC3 with APEv2 tags', metadata['title'])
        self.assertEqual('The Artist', metadata['artist'])

    def test_remove_ape_tags(self):
        config.setting['remove_ape_from_ac3'] = True
        metadata = Metadata({
            'artist': 'Foo'
        })
        metadata = save_and_load_metadata(self.filename, metadata)
        self.assertEqual('AC-3', metadata['~format'])
        self.assertNotIn('title', metadata)
        self.assertNotIn('artist', metadata)

    def test_info_format(self):
        metadata = load_metadata(os.path.join('test', 'data', 'test.ac3'))
        self.assertEqual('AC-3', metadata['~format'])
        metadata = load_metadata(os.path.join('test', 'data', 'test-apev2.ac3'))
        self.assertEqual('AC-3 (APEv2)', metadata['~format'])
        if native_ac3:
            metadata = load_metadata(os.path.join('test', 'data', 'test.eac3'))
            self.assertEqual('Enhanced AC-3', metadata['~format'])

    def test_supports_tag(self):
        config.setting['ac3_save_ape'] = True
        self.assertTrue(AC3File.supports_tag('title'))
        config.setting['ac3_save_ape'] = False
        self.assertFalse(AC3File.supports_tag('title'))


@unittest.skipUnless(native_ac3, "mutagen.ac3 not available")
class EAC3Test(CommonTests.SimpleFormatsTestCase):
    testfile = 'test.eac3'
    expected_info = {
        '~format': 'Enhanced AC-3',
        'length': 107,
        '~sample_rate': '44100',
        '~channels': '2',
    }
    unexpected_info = ['~video']

    def setUp(self):
        super().setUp()
        config.setting['ac3_save_ape'] = True

    def test_bitrate(self):
        # For EAC3 bitrate is calculated and often a fractional value
        metadata = load_metadata(os.path.join('test', 'data', 'test.ac3'))
        self.assertAlmostEqual(192.0, float(metadata['~bitrate']))