Repository: beetbox/beets

Issue:
Calculating album replaygain with gstreamer still causes error
### Problem

I have the same issue as in #2636 when calculating replaygain for my whole collection.
Upgrading from 1.4.5 (which do not contain the fix for #2636 ) to 1.4.6 only reduces the error occurrence rate from 80% to 30%.

### Setup

* OS: Gentoo Linux
* Python version: 2.7.14
* beets version: 1.4.6

My configuration (output of `beet config`) is:

```yaml
lyrics:
    bing_lang_from: []
    sources: '*'
    google_API_key: REDACTED
    force: no
    auto: yes
    genius_api_key: REDACTED
    google_engine_ID: REDACTED
    bing_lang_to:
    bing_client_secret: REDACTED
    fallback:
    local: no

paths:
    comp: ${album_genre}/Various Artits/${year} - ${album}%aunique{}/${disc_and_track} - ${title} ($artist)
    singleton: ${single_genre}/${artist}/%if{${year},${year} - }${title}
    default: ${album_genre}/${albumartist}/${year} - ${album}%aunique{}/${disc_and_track} - ${title}
fetchart:
    minwidth: 500
    maxwidth: 600
    sources: filesystem coverart amazon albumart fanarttv
    fanarttv_key: REDACTED
    auto: yes
    google_engine: 001442825323518660753:hrh5ch1gjzm
    enforce_ratio: no
    cautious: no
    store_source: no
    google_key: REDACTED
    cover_names:
    - cover
    - front
    - art
    - album
    - folder
library: /srv/music/.beets.db

replace:
    '''': "\u2019"
    ^\.: _
    \s+$: ''
    ^\s+: ''
    ^-: _
original_date: yes
replaygain:
    auto: no
    backend: gstreamer
    targetlevel: 89
    r128: [Opus]
    overwrite: no
badfiles:
    commands:
        mpc: mpcdec -c

plugins: discogs chroma fetchart embedart scrub lastgenre badfiles missing edit inline info convert duplicates replaygain types web mbsync lyrics
types:
    mp3car: bool
    myrip: bool
web:
    reverse_proxy: yes
    include_paths: yes
    host: 127.0.0.1
    port: 8337
    cors: ''
convert:
    dest: /srv/music-export
    command: /usr/local/bin/gapless-mp3enc $source $dest
    never_convert_lossy_files: yes
    embed: no
    album_art_maxwidth: 0
    format: mp3
    auto: no
    threads: 2
    copy_album_art: no
    tmpdir:
    max_bitrate: 500

    paths: {}
    pretend: no
    quiet: no
    formats:
        alac:
            command: ffmpeg -i $source -y -vn -acodec alac $dest
            extension: m4a
        aac:
            command: ffmpeg -i $source -y -vn -acodec aac -aq 1 $dest
            extension: m4a
        opus: ffmpeg -i $source -y -vn -acodec libopus -ab 96k $dest
        mp3: ffmpeg -i $source -y -vn -aq 2 $dest
        flac: ffmpeg -i $source -y -vn -acodec flac $dest
        ogg: ffmpeg -i $source -y -vn -acodec libvorbis -aq 3 $dest
        wma: ffmpeg -i $source -y -vn -acodec wmav2 -vn $dest
    no_convert: ''
item_fields:
    disc_and_track: u'%i%02i' % (disc, track) if disctotal > 1 else u'%02i' % (track)
    single_genre: genre.split(', ')[0] if genre else u'Other'
per_disc_numbering: yes
id3v23: yes
album_fields:
    album_genre: genre.split(', ')[0] if genre else u'Other'
lastgenre:
    source: artist
    count: 1
    prefer_specific: yes
    whitelist: /srv/music/.beets.genres.txt
    canonical: /srv/music/.beets.genres-tree.yaml
    force: yes
    min_weight: 10
    auto: yes
    separator: ', '
    fallback:
path_sep_replace: '~'
directory: /srv/music

import:
    timid: yes
    detail: yes
acoustid:
    apikey: REDACTED
discogs:
    user_token: REDACTED
    tokenfile: discogs_token.json
    apikey: REDACTED
    apisecret: REDACTED
    source_weight: 0.5
embedart:
    compare_threshold: 0
    auto: yes
    ifempty: no
    remove_art_file: no
    maxwidth: 0
duplicates:
    count: no
    full: no
    format: ''
    keys: []
    move: ''
    tag: ''
    path: no
    copy: ''
    tiebreak: {}
    album: no
    strict: no
    checksum: ''
    merge: no
    delete: no
edit:
    itemfields: track title artist album
    albumfields: album albumartist
    ignore_fields: id path
missing:
    count: no
    album: no
    total: no
chroma:
    auto: yes
scrub:
    auto: yes
pathfields: {}
```



Discussion:

Resolving patch:
diff --git a/beetsplug/replaygain.py b/beetsplug/replaygain.py
--- a/beetsplug/replaygain.py
+++ b/beetsplug/replaygain.py
@@ -613,16 +613,6 @@ def _set_file(self):
 
         self._file = self._files.pop(0)
 
-        # Disconnect the decodebin element from the pipeline, set its
-        # state to READY to to clear it.
-        self._decbin.unlink(self._conv)
-        self._decbin.set_state(self.Gst.State.READY)
-
-        # Set a new file on the filesrc element, can only be done in the
-        # READY state
-        self._src.set_state(self.Gst.State.READY)
-        self._src.set_property("location", py3_path(syspath(self._file.path)))
-
         # Ensure the filesrc element received the paused state of the
         # pipeline in a blocking manner
         self._src.sync_state_with_parent()
@@ -633,9 +623,18 @@ def _set_file(self):
         self._decbin.sync_state_with_parent()
         self._decbin.get_state(self.Gst.CLOCK_TIME_NONE)
 
+        # Disconnect the decodebin element from the pipeline, set its
+        # state to READY to to clear it.
+        self._decbin.unlink(self._conv)
+        self._decbin.set_state(self.Gst.State.READY)
+
+        # Set a new file on the filesrc element, can only be done in the
+        # READY state
+        self._src.set_state(self.Gst.State.READY)
+        self._src.set_property("location", py3_path(syspath(self._file.path)))
+
         self._decbin.link(self._conv)
         self._pipe.set_state(self.Gst.State.READY)
-        self._pipe.set_state(self.Gst.State.PLAYING)
 
         return True
 


Test patch: