File: test_fingerprint.py

package info (click to toggle)
quodlibet 4.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,228 kB
  • sloc: python: 89,728; sh: 381; xml: 110; makefile: 91
file content (254 lines) | stat: -rw-r--r-- 8,927 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Copyright 2014 Nick Boultbee
#
# 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.

import time

from gi.repository import Gtk

try:
    from gi.repository import Gst

    Gst  # noqa
except ImportError:
    Gst = None
else:
    chromaprint = Gst.ElementFactory.find("chromaprint")
    vorbisdec = Gst.ElementFactory.find("vorbisdec")


from quodlibet import config
from quodlibet.formats import MusicFile
from tests import get_data_path, skipUnless
from tests.plugin import PluginTestCase


@skipUnless(Gst and chromaprint and vorbisdec, "gstreamer plugins missing")
class TFingerprint(PluginTestCase):
    TIMEOUT = 20.0

    def setUp(self):
        config.init()
        self.mod = self.modules["AcoustidSearch"]

    def tearDown(self):
        config.quit()

        self.mod  # noqa

    def test_analyze_silence(self):
        pipeline = self.mod.analyze.FingerPrintPipeline()
        song = MusicFile(get_data_path("silence-44-s.ogg"))
        done = []

        def callback(self, *args):
            done.extend(args)

        pipeline.start(song, callback)
        t = time.time()
        while not done and time.time() - t < self.TIMEOUT:
            Gtk.main_iteration_do(False)
        assert done
        s, result, error = done
        # silence doesn't produce a fingerprint
        assert error
        assert not result
        assert song is s

    def test_analyze_pool(self):
        pool = self.mod.analyze.FingerPrintPool()
        song = MusicFile(get_data_path("silence-44-s.ogg"))

        events = []

        def handler(*args):
            events.append(args)

        pool.connect("fingerprint-started", handler, "start")
        pool.connect("fingerprint-done", handler, "done")
        pool.connect("fingerprint-error", handler, "error")
        pool.push(song)

        t = time.time()
        while len(events) < 2 and time.time() - t < self.TIMEOUT:
            Gtk.main_iteration_do(False)

        self.assertEqual(len(events), 2)
        self.assertEqual(events[0][-1], "start")
        self.assertEqual(events[1][-1], "error")


@skipUnless(Gst and chromaprint, "gstreamer plugins missing")
class TAcoustidLookup(PluginTestCase):
    def setUp(self):
        config.init()
        self.mod = self.modules["AcoustidSearch"]

    def tearDown(self):
        config.quit()

    def test_parse_response_1(self):
        parse = self.mod.acoustid.parse_acoustid_response

        release = parse(ACOUSTID_RESPONSE)[0]
        self.assertEqual(release.id, "14bb7304-b763-456b-a438-7bab619d41e3")
        self.assertEqual(release.sources, 1)
        self.assertEqual(release.all_sources, 7)

        tags = release.tags
        self.assertEqual(tags["title"], "Merkw\xfcrdig/Unangenehm")
        self.assertEqual(tags["artist"], "Kinderzimmer Productions")
        self.assertEqual(tags["date"], "2002-01")
        self.assertEqual(tags["tracknumber"], "7/15")
        self.assertEqual(tags["discnumber"], "")
        assert "musicbrainz_albumid" in tags

    def test_parse_response_2(self):
        parse = self.mod.acoustid.parse_acoustid_response

        release = parse(ACOUSTID_RESPONSE)[1]
        self.assertEqual(release.id, "ed90bff9-ab41-4669-8d44-13c78e678507")
        tags = release.tags
        self.assertEqual(tags["albumartist"], "Kinderzimmer Productions")
        self.assertEqual(tags["album"], "Wir sind da wo oben ist")
        assert "musicbrainz_albumid" in tags

    def test_parse_response_2_mb(self):
        parse = self.mod.acoustid.parse_acoustid_response

        release = parse(ACOUSTID_RESPONSE)[1]
        assert "musicbrainz_albumid" in release.tags
        self.assertEqual(release.sources, 6)
        self.assertEqual(
            release.tags["musicbrainz_trackid"], "bc970841-b7d9-415a-b7e2-645b1d263cc3"
        )


result = {
    "recordings": [
        {
            "releases": [
                {
                    "track_count": 15,
                    "title": "Spex CD #15",
                    "country": "DE",
                    "artists": [
                        {
                            "id": "89ad4ac3-39f7-470e-963a-56509c546377",
                            "name": "Various Artists",
                        }
                    ],
                    "date": {"year": 2002, "month": 1},
                    "releaseevents": [
                        {"date": {"year": 2002, "month": 1}, "country": "DE"}
                    ],
                    "mediums": [
                        {
                            "position": 1,
                            "tracks": [
                                {
                                    "position": 7,
                                    "title": "Merkw\xfcrdig/Unangenehm",
                                    "id": "7426320b-7646-3d06-bd5a-4762ecc0536b",
                                    "artists": [
                                        {
                                            "id": "ad728059-6823-4f98-a283-0dac3fb79a91",  # noqa: E501
                                            "name": "Kinderzimmer Productions",
                                        }
                                    ],
                                }
                            ],
                            "track_count": 15,
                            "format": "CD",
                        }
                    ],
                    "medium_count": 1,
                    "id": "14bb7304-b763-456b-a438-7bab619d41e3",
                }
            ],
            "title": "Merkw\xfcrdig/Unangenehm",
            "sources": 1,
            "artists": [
                {
                    "id": "ad728059-6823-4f98-a283-0dac3fb79a91",
                    "name": "Kinderzimmer Productions",
                }
            ],
            "duration": 272,
            "id": "9104a525-40b2-40dc-83bf-c31c3d6d1861",
        },
        {
            "releases": [
                {
                    "track_count": 12,
                    "title": "Wir sind da wo oben ist",
                    "country": "DE",
                    "artists": [
                        {
                            "id": "ad728059-6823-4f98-a283-0dac3fb79a91",
                            "name": "Kinderzimmer Productions",
                        }
                    ],
                    "date": {"year": 2002, "day": 22, "month": 2},
                    "releaseevents": [
                        {
                            "date": {"year": 2002, "day": 22, "month": 2},
                            "country": "DE",
                        }
                    ],
                    "mediums": [
                        {
                            "position": 1,
                            "tracks": [
                                {
                                    "position": 11,
                                    "title": "Merkw\xfcrdig/unangenehm",
                                    "id": "2520fe8a-005b-3a18-a8e2-ba9bef6009fb",
                                    "artists": [
                                        {
                                            "joinphrase": " feat. ",
                                            "name": "Kinderzimmer Productions",
                                            "id": "ad728059-6823-4f98-a283-0dac3fb79a91",  # noqa: E501
                                        },
                                        {
                                            "id": "bf02bc50-251d-4a47-b5f9-ca462038ae8a",  # noqa: E501
                                            "name": "Tek Beton",
                                        },
                                    ],
                                }
                            ],
                            "track_count": 12,
                            "format": "CD",
                        }
                    ],
                    "medium_count": 1,
                    "id": "ed90bff9-ab41-4669-8d44-13c78e678507",
                }
            ],
            "title": "Merkw\xfcrdig/unangenehm",
            "sources": 6,
            "artists": [
                {
                    "joinphrase": " feat. ",
                    "name": "Kinderzimmer Productions",
                    "id": "ad728059-6823-4f98-a283-0dac3fb79a91",
                },
                {
                    "id": "bf02bc50-251d-4a47-b5f9-ca462038ae8a",
                    "name": "Tek Beton",
                },
            ],
            "duration": 272,
            "id": "bc970841-b7d9-415a-b7e2-645b1d263cc3",
        },
    ],
    "score": 1.0,
    "id": "f176baca-a4f7-4f39-906b-43136d9b3815",
}
ACOUSTID_RESPONSE = {
    "status": "ok",
    "results": [result],
}