Exemple #1
0
def collect_files(music_dir, files, cache, supported_formats):
    i = 0
    for dirpath, dirnames, filenames in os.walk(music_dir):
        for filename in filenames:
            filepath = un(relpath(os.path.join(dirpath, filename), music_dir),
                                  sys.getfilesystemencoding())
            properpath = os.path.join(dirpath, filename)
            mtime = os.path.getmtime(properpath)

            # check the cache
            if filepath in cache:
                cache[filepath] = True
                record = files[filepath]
                if mtime <= record[1]:
                    # the file's still ok
                    continue

            ext = os.path.splitext(filename)[1]
            if ext in supported_formats:
                i += 1
                print ou(u"  [%i] %s |" % (i, filepath)),
                album_id = get_album_id(music_dir, filepath)
                print ou(album_id or u"<single track>")
                # fields here: album_id, mtime, already_processed
                files[filepath] = (album_id, mtime, False)
Exemple #2
0
def show_rgain_info(filenames, mp3_format="ql"):
    formats_map = rgio.BaseFormatsMap(mp3_format)
    
    for filename in filenames:
        filename = un(filename, sys.getfilesystemencoding())
        print ou(filename)
        try:
            trackdata, albumdata = formats_map.read_gain(filename)
        except Exception, exc:
            print "  <Error reading Replay Gain: %r>" % exc
            continue
        
        if not trackdata and not albumdata:
            print "  <No Replay Gain information>"
        
        if trackdata and trackdata.ref_level:
            ref_level = trackdata.ref_level
        elif albumdata and albumdata.ref_level:
            ref_level = albumdata.ref_level
        else:
            ref_level = None
        
        if ref_level is not None:
            print "  Reference loudness %i dB" % ref_level
        
        if trackdata:
            print "  Track gain %.2f dB" % trackdata.gain
            print "  Track peak %.8f" % trackdata.peak
        if albumdata:
            print "  Album gain %.2f dB" % albumdata.gain
            print "  Album peak %.8f" % albumdata.peak
Exemple #3
0
def write_cache(cache_file, files):
    cache_dir = os.path.dirname(cache_file)

    try:
        if not os.path.isdir(cache_dir):
            os.makedirs(cache_dir, 0755)
        f = open(cache_file, "wb")
        pickle.dump(files, f, 2)
    except Exception, exc:
        print ou(u"Error while writing the cache - %s" % exc)
Exemple #4
0
def read_cache(cache_file):
    if not os.path.isfile(cache_file):
        files = {}
    else:
        try:
            f = open(cache_file, "rb")
            files = pickle.load(f)
        except Exception, exc:
            print ou(u"Error while reading the cache - %s" % exc)
        finally:
Exemple #5
0
def do_gain_all(music_dir, albums, single_tracks, files, ref_level=89,
              force=False, dry_run=False, mp3_format="ql", stop_on_error=False):
    if single_tracks:
        do_gain((os.path.join(music_dir, path) for path in single_tracks),
                ref_level, force, dry_run, False, mp3_format)
        # update cache information
        if not dry_run:
            update_cache(files, music_dir, single_tracks, None)
        print

    for album_id, album_files in albums.iteritems():
        print ou(u"%s:" % album_id),
        do_gain((os.path.join(music_dir, path) for path in album_files),
                ref_level, force, dry_run, True, mp3_format)
        # update cache
        if not dry_run:
            update_cache(files, music_dir, album_files, album_id)
        print
Exemple #6
0
def do_gain(files, ref_level=89, force=False, dry_run=False, album=True,
            mp3_format="ql"):
    
    files = [un(filename, sys.getfilesystemencoding()) for filename in files]
    
    formats_map = rgio.BaseFormatsMap(mp3_format)
    
    newfiles = []
    for filename in files:
        if not os.path.splitext(filename)[1] in formats_map.supported_formats:
            print ou(u"%s: not supported, ignoring it" % filename)
        else:
            newfiles.append(filename)
    files = newfiles
    
    if not force:
        print "Checking for Replay Gain information ..."
        newfiles = []
        for filename in files:
            print ou(u"  %s:" % filename),
            try:
                trackdata, albumdata = formats_map.read_gain(filename)
            except Exception, exc:
                raise Error(u"%s: error - %s" % (filename, exc))
            else:
                if trackdata and albumdata:
                    print "track and album"
                elif not trackdata and albumdata:
                    print "album only"
                    newfiles.append(filename)
                elif trackdata and not albumdata:
                    print "track only"
                    if album:
                        newfiles.append(filename)
                else:
                    print "none"
                    newfiles.append(filename)
        
        if not album:
            files = newfiles
        elif not len(newfiles):
            files = newfiles
Exemple #7
0
def collectiongain():
    optparser = collectiongain_options()
    opts, args = optparser.parse_args()
    if len(args) != 1:
        optparser.error("pass one directory path")
    
    try:
        do_collectiongain(args[0], opts.ref_level, opts.force, opts.dry_run,
                          opts.mp3_format, opts.ignore_cache)
    except Error, exc:
        print >> sys.stderr, ou(unicode(exc))
        sys.exit(1)
Exemple #8
0
def rgain():
    optparser = rgain_options()
    opts, args = optparser.parse_args()
    if not args:
        optparser.error("pass one or several audio file names")
    
    if opts.show:
        show_rgain_info(args, opts.mp3_format)
    else:
        try:
            do_gain(args, opts.ref_level, opts.force, opts.dry_run, opts.album,
                    opts.mp3_format)
        except Error, exc:
            print >> sys.stderr, ou(unicode(exc))
            sys.exit(1)
        except KeyboardInterrupt:
            print "Interrupted."
Exemple #9
0
 def on_trk_started(evsrc, filename):
     print ou("  %s:" % filename.decode("utf-8")),
     sys.stdout.flush()
Exemple #10
0
    print "Calculating Replay Gain information ..."
    try:
        tracks_data, albumdata = calculate_gain(files, ref_level)
        if album:
            print "  Album gain: %.2f dB" % albumdata.gain
    except Exception, exc:
        raise Error(u"Error while calculating gain - %s" % exc)
    
    if not album:
        albumdata = None
    
    # write gain
    if not dry_run:
        print "Writing Replay Gain information to files ..."
        for filename, trackdata in tracks_data.iteritems():
            print ou(u"  %s:" % filename),
            try:
                formats_map.write_gain(filename, trackdata, albumdata)
            except Exception, exc:
                raise Error(u"%s: error - %s" % (filename, exc))
            else:
                print "done"
    
    print "Done"


# a simple Replay Gain dump
def show_rgain_info(filenames, mp3_format="ql"):
    formats_map = rgio.BaseFormatsMap(mp3_format)
    
    for filename in filenames: