Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added backed audiotools to replaygain module and updated documentation #1070

Merged
merged 4 commits into from
Nov 10, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions beetsplug/replaygain.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,119 @@ def _on_pad_removed(self, decbin, pad):
assert(peer is None)


class AudioToolsBackend(Backend):
def __init__(self, config):
self._import_audiotools()

def _import_audiotools(self):
try:
import audiotools
import audiotools.replaygain
except ImportError:
raise FatalReplayGainError(
"Failed to load audiotools: audiotools not found"
)
self._mod_audiotools = audiotools
self._mod_replaygain = audiotools.replaygain

def open_audio_file(self, item):
try:
audiofile = self._mod_audiotools.open(item.path)
except IOError:
raise ReplayGainError(
"File {} was not found".format(item.path)
)
except self._mod_audiotools.UnsupportedFile:
raise ReplayGainError(
"Unsupported file type {}".format(item.format)
)

return audiofile

def init_replaygain(self, audiofile, item):
try:
rg = self._mod_replaygain.ReplayGain(audiofile.sample_rate())
except ValueError:
raise ReplayGainError(
"Unsupported sample rate {}".format(item.samplerate)
)
return
return rg

def compute_track_gain(self, items):
return [self._compute_track_gain(item) for item in items]

def _compute_track_gain(self, item):

audiofile = self.open_audio_file(item)

rg = self.init_replaygain(audiofile, item)

rg_track_gain, rg_track_peak = rg.title_gain(audiofile.to_pcm())

log.info(
u'ReplayGain for track {0} - {1}: gain {2:.2f} peak {3:.2f}'.format(
item.artist,
item.title,
rg_track_gain,
rg_track_peak
)
)

return Gain(gain=rg_track_gain, peak=rg_track_peak)

def compute_album_gain(self, album):

log.info(
u'Analysing album {0} - {1}'.format(
album.albumartist,
album.album
)
)

item = list(album.items())[0]

audiofile = self.open_audio_file(item)
rg = self.init_replaygain(audiofile, item)

track_gains = []

for item in album.items():
audiofile = self.open_audio_file(item)
rg_track_gain, rg_track_peak = rg.title_gain(audiofile.to_pcm())
track_gains.append(
Gain(gain=rg_track_gain, peak=rg_track_peak)
)
log.info(
u'ReplayGain for track {0} - {1}: gain {2:.2f} peak {3:.2f}'.format(
item.artist,
item.title,
rg_track_gain,
rg_track_peak
)
)

rg_album_gain, rg_album_peak = rg.album_gain()

log.info('-' * 100)

log.info(
u'ReplayGain for Album {0} - {1}: gain {2:.2f} peak {3:.2f}'.format(
album.albumartist,
album.album,
rg_album_gain,
rg_album_peak
)
)

log.info('=' * 100)

return AlbumGain(
Gain(gain=rg_album_gain, peak=rg_album_peak),
track_gains=track_gains
)


# Main plugin logic.

class ReplayGainPlugin(BeetsPlugin):
Expand All @@ -472,6 +585,7 @@ class ReplayGainPlugin(BeetsPlugin):
backends = {
"command": CommandBackend,
"gstreamer": GStreamerBackend,
"audiotools": AudioToolsBackend
}

def __init__(self):
Expand Down
8 changes: 7 additions & 1 deletion docs/plugins/replaygain.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ you can configure the path explicitly like so::
replaygain:
command: /Applications/MacMP3Gain.app/Contents/Resources/aacgain

Python Audio Tools
``````````````````

This backend uses the `audiotools`_ module to compute ReplayGain, in a range of different file formats. Installation of the module is not available through PyPI. The requirements for most of its dependencies can be installed on Mac OS X via `Homebrew`_: ``brew install mpg123 mp3gain vorbisgain faad2 libvorbis``

.. _audiotools: http://audiotools.sourceforge.net

Configuration
-------------
Expand All @@ -70,7 +76,7 @@ configuration file. The available options are:

- ``auto``: Enable ReplayGain analysis during import.
Default: ``yes``.
- ``backend``: The analysis backend; either ``gstreamer`` or ``command``.
- ``backend``: The analysis backend; either ``gstreamer``, ``command`` or ``audtiotools`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

audtiotools should be audiotools

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now, let me know if it works for you.

Default: ``command``.
- ``overwrite``: Re-analyze files that already have ReplayGain tags.
Default: ``no``.
Expand Down