Skip to content

Commit

Permalink
replaygain: apply review feedback: fixup previous refactor, improve t…
Browse files Browse the repository at this point in the history
…ests

by adding files which are not completely silent, thus hitting a different
code path in some calculations

The sample files were generated using
> sox -n whitenoise.flac synth 00:00:02 whitenoise
> ffmpeg -i whitenoise.flac whitenoise.opus
> ffmpeg -i whitenoise.flac whitenoise.mp3
  • Loading branch information
wisp3rwind committed Jul 22, 2023
1 parent b8be2af commit 3965858
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
6 changes: 3 additions & 3 deletions beetsplug/replaygain.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def compute_album_gain(self, task):

def sum_of_track_powers(track_gain, track_n_blocks):
# convert `LU to target_level` -> LUFS
loudness = target_level_lufs - track_gain
loudness = target_level_lufs - track_gain.gain

# This reverses ITU-R BS.1770-4 p. 6 equation (5) to convert
# from loudness to power. The result is the average gating
Expand Down Expand Up @@ -586,7 +586,7 @@ def compute_gain(self, items, target_level, is_album):
When computing album gain, the last TrackGain object returned is
the album gain
"""
if len(items) == 0:
if not items:
self._log.debug('no supported tracks to analyze')
return []

Expand Down Expand Up @@ -1375,7 +1375,7 @@ def _interrupt(self, signal, frame):
pass

def close_pool(self):
"""Regularly lose the `ThreadPool` instance in `self.pool`.
"""Regularly close the `ThreadPool` instance in `self.pool`.
"""
if self.pool is not None:
self.pool.close()
Expand Down
13 changes: 11 additions & 2 deletions test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,20 @@ def add_item_fixtures(self, ext='mp3', count=1):
items.append(item)
return items

def add_album_fixture(self, track_count=1, ext='mp3', disc_count=1):
def add_album_fixture(
self,
track_count=1,
fname='full',
ext='mp3',
disc_count=1,
):
"""Add an album with files to the database.
"""
items = []
path = os.path.join(_common.RSRC, util.bytestring_path('full.' + ext))
path = os.path.join(
_common.RSRC,
util.bytestring_path(f'{fname}.{ext}'),
)
for discnumber in range(1, disc_count + 1):
for i in range(track_count):
item = Item.from_path(path)
Expand Down
Binary file added test/rsrc/whitenoise.flac
Binary file not shown.
Binary file added test/rsrc/whitenoise.mp3
Binary file not shown.
Binary file added test/rsrc/whitenoise.opus
Binary file not shown.
17 changes: 13 additions & 4 deletions test/test_replaygain.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def test_backend(self):


class ReplayGainCliTestBase(TestHelper):
FNAME: str

def setUp(self):
# Implemented by Mixins, see above. This may decide to skip the test.
self.test_backend()
Expand All @@ -99,7 +101,8 @@ def setUp(self):
self.unload_plugins()

def _add_album(self, *args, **kwargs):
album = self.add_album_fixture(*args, **kwargs)
# Use a file with non-zero volume (most test assets are total silence)
album = self.add_album_fixture(*args, fname=self.FNAME, **kwargs)
for item in album.items():
reset_replaygain(item)

Expand Down Expand Up @@ -305,19 +308,25 @@ def test_per_disc(self):
@unittest.skipIf(not GST_AVAILABLE, 'gstreamer cannot be found')
class ReplayGainGstCliTest(ReplayGainCliTestBase, unittest.TestCase,
GstBackendMixin):
pass
FNAME = "full" # file contains only silence


@unittest.skipIf(not GAIN_PROG_AVAILABLE, 'no *gain command found')
class ReplayGainCmdCliTest(ReplayGainCliTestBase, unittest.TestCase,
CmdBackendMixin):
pass
FNAME = "full" # file contains only silence


@unittest.skipIf(not FFMPEG_AVAILABLE, 'ffmpeg cannot be found')
class ReplayGainFfmpegCliTest(ReplayGainCliTestBase, unittest.TestCase,
FfmpegBackendMixin):
pass
FNAME = "full" # file contains only silence


@unittest.skipIf(not FFMPEG_AVAILABLE, 'ffmpeg cannot be found')
class ReplayGainFfmpegNoiseCliTest(ReplayGainCliTestBase, unittest.TestCase,
FfmpegBackendMixin):
FNAME = "whitenoise"


class ImportTest(TestHelper):
Expand Down

0 comments on commit 3965858

Please sign in to comment.