File: test_fingerprint.py

package info (click to toggle)
quodlibet 4.6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,016 kB
  • sloc: python: 85,817; sh: 385; xml: 110; makefile: 91
file content (162 lines) | stat: -rw-r--r-- 6,193 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
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
# 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
except ImportError:
    Gst = None
else:
    chromaprint = Gst.ElementFactory.find("chromaprint")
    vorbisdec = Gst.ElementFactory.find("vorbisdec")


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


@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

    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)
        self.assertTrue(done)
        s, result, error = done
        # silence doesn't produce a fingerprint
        self.assertTrue(error)
        self.assertFalse(result)
        self.assertTrue(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"], u'Merkw\xfcrdig/Unangenehm')
        self.assertEqual(tags["artist"], u'Kinderzimmer Productions')
        self.assertEqual(tags["date"], u'2002-01')
        self.assertEqual(tags["tracknumber"], u'7/15')
        self.assertEqual(tags["discnumber"], "")
        self.assertTrue("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"], u"Kinderzimmer Productions")
        self.assertEqual(tags["album"], u'Wir sind da wo oben ist')
        self.assertTrue("musicbrainz_albumid" in tags)

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

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


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