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
|
# 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.
from gi.repository import Gtk, GLib
import re
import time
from quodlibet.ext.songsmenu.replaygain import UpdateMode, RGDialog, ReplayGainPipeline
from quodlibet.formats import MusicFile
from quodlibet.formats import AudioFile
from tests.plugin import PluginTestCase
from tests import get_data_path, TestCase
class TReplayGain(PluginTestCase):
# Give up analysis after some time, in case GStreamer dies.
TIMEOUT = 20
@classmethod
def setUpClass(cls):
cls.mod = cls.modules["ReplayGain"]
cls.kind = cls.plugins["ReplayGain"].cls
@classmethod
def tearDownClass(cls):
del cls.mod
del cls.kind
def setUp(self):
self.song = AudioFile({"artist": "foo", "album": "the album"})
self.plugin = self.kind([self.song], None)
def tearDown(self):
self.plugin.destroy()
del self.plugin
del self.song
def test_RGSong_properties(self):
rgs = self.mod.RGSong(self.song)
assert not rgs.has_album_tags
assert not rgs.has_track_tags
assert not rgs.has_all_rg_tags
rgs.done = True
rgs._write(-1.23, 0.99)
assert rgs.has_album_tags, "Didn't write album tags"
assert not rgs.has_track_tags
assert not rgs.has_all_rg_tags
def test_RGSong_zero(self):
rgs = self.mod.RGSong(self.song)
rgs.done = True
rgs._write(0.0, 0.0)
self.assertTrue(rgs.has_album_tags, msg=f"Failed with 0.0 album tags ({rgs})")
def test_RGAlbum_properties(self):
rga = self.mod.RGAlbum([self.mod.RGSong(self.song)], UpdateMode.ALWAYS)
assert not rga.done
self.assertEqual(rga.title, "foo - the album")
def test_delete_bs1770gain(self):
tags = [
"replaygain_reference_loudness",
"replaygain_algorithm",
"replaygain_album_range",
"replaygain_track_range",
]
for tag in tags:
self.song[tag] = "foo"
rgs = self.mod.RGSong(self.song)
rgs.done = True
rgs._write(0.0, 0.0)
for tag in tags:
assert not self.song(tag)
def _analyse_song(self, song):
mode = self.mod.UpdateMode.ALWAYS
self.album = album = self.mod.RGAlbum.from_songs([song], mode)
self.analysed = None
def _run_main_loop():
def on_complete(pipeline, album):
album.write()
self.analysed = [album]
pipeline = self.mod.ReplayGainPipeline()
sig = pipeline.connect("done", on_complete)
pipeline.start(album)
start = time.time()
while not self.analysed and abs(time.time() - start) < self.TIMEOUT:
Gtk.main_iteration_do(False)
pipeline.quit()
pipeline.disconnect(sig)
_run_main_loop()
assert self.analysed, "Timed out"
def test_analyze_sinewave(self):
song = MusicFile(get_data_path("sine-110hz.flac"))
self.assertEqual(song("~#length"), 2)
assert not song("~replaygain_track_gain")
self._analyse_song(song)
self.assertAlmostEqual(
song("~#replaygain_track_peak"), 1.0, msg="Track peak should be 1.0"
)
track_gain = song("~#replaygain_track_gain")
assert track_gain, "No Track Gain added"
assert re.match(r"\-[0-9]\.[0-9]{1,2}", str(track_gain))
# For one-song album, track == album
self.assertEqual(track_gain, song("~#replaygain_album_gain"))
def test_analyze_silence(self):
song = MusicFile(get_data_path("silence-44-s.ogg"))
assert not song("~replaygain_track_gain")
self._analyse_song(song)
self.assertAlmostEqual(
song("~#replaygain_track_peak"), 0.0, msg="Track peak should be 0.0"
)
track_gain = song("~#replaygain_track_gain")
assert track_gain, "No Track Gain added"
# For one-song album, track == album
self.assertEqual(track_gain, song("~#replaygain_album_gain"))
class FakePipeline(ReplayGainPipeline):
def __init__(self):
super().__init__()
self.started = []
def quit(self):
pass
def _setup_pipe(self):
pass
def start(self, album):
self.started.append(album)
super().start(album)
def _next_song(self, first=False):
GLib.idle_add(self._emit)
def _emit(self):
self.emit("done", self._album)
class FakeRGDialog(RGDialog):
def create_pipelines(self):
self.pipes = [FakePipeline(), FakePipeline()]
class TRGDialog(TestCase):
def test_some_songs_needing_update(self):
songs = [[a_song(x)] for x in range(8)]
d = FakeRGDialog(songs, None, UpdateMode.ALBUM_MISSING)
d.start_analysis()
self.run_main_loop()
d.destroy()
# One should have got half of the albums needing update (and no more)
self.assertEqual(self.track_nums_from(d.pipes[0].started), [0, 4])
# And the other processor should get the other half
self.assertEqual(self.track_nums_from(d.pipes[1].started), [2, 6])
def run_main_loop(self, timeout=0.25):
start = time.time()
while abs(time.time() - start) < timeout:
Gtk.main_iteration_do(False)
def track_nums_from(self, album):
return [s.songs[0].song("~#tracknumber") for s in album]
def a_song(n):
d = {"replaygain_album_gain": -6.0} if n % 2 else {}
d["tracknumber"] = n
return AudioFile(d)
|