From abc7941daa9669049b99f7ceb0e15da66881d4ea Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Wed, 1 May 2013 17:54:11 +0200 Subject: [PATCH] Move RG-Parsing into BastpUtil and provide an LRU cache --- .../android/vanilla/BastpUtil.java | 80 +++++++++++++++++++ .../android/vanilla/PlaybackService.java | 33 ++------ 2 files changed, 86 insertions(+), 27 deletions(-) create mode 100644 src/ch/blinkenlights/android/vanilla/BastpUtil.java diff --git a/src/ch/blinkenlights/android/vanilla/BastpUtil.java b/src/ch/blinkenlights/android/vanilla/BastpUtil.java new file mode 100644 index 00000000..cc2efb17 --- /dev/null +++ b/src/ch/blinkenlights/android/vanilla/BastpUtil.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 Adrian Ulrich + * + * 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 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package ch.blinkenlights.android.vanilla; + +import android.util.LruCache; +import ch.blinkenlights.bastp.Bastp; +import java.util.HashMap; +import java.util.Vector; +import android.util.Log; + +public class BastpUtil { + private RGLruCache rgCache; + + public BastpUtil() { + rgCache = new RGLruCache(16); /* Cache up to 16 entries */ + } + + + /** Returns the ReplayGain values of 'path' as + */ + public float[] getReplayGainValues(String path) { + float[] cached = rgCache.get(path); + Log.d("VanillaMusic", "LRU cache("+rgCache.size()+"): "+path+" -> "+cached); + if(cached == null) { + cached = getReplayGainValuesFromFile(path); + rgCache.put(path, cached); + } + return cached; + } + + + + /** Parse given file and return track,album replay gain values + */ + private float[] getReplayGainValuesFromFile(String path) { + String[] keys = { "REPLAYGAIN_TRACK_GAIN", "REPLAYGAIN_ALBUM_GAIN" }; + float[] adjust= { 0f , 0f }; + HashMap tags = (new Bastp()).getTags(path); + + for (int i=0; i { + public RGLruCache(int size) { + super(size); + } + } + +} + diff --git a/src/ch/blinkenlights/android/vanilla/PlaybackService.java b/src/ch/blinkenlights/android/vanilla/PlaybackService.java index bf439d6f..67fcc7ea 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaybackService.java +++ b/src/ch/blinkenlights/android/vanilla/PlaybackService.java @@ -66,9 +66,6 @@ import java.io.EOFException; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Vector; -import ch.blinkenlights.bastp.Bastp; /** @@ -374,6 +371,8 @@ public final class PlaybackService extends Service private boolean mReplayGainAlbumEnabled; private int mReplayGainBump; + private BastpUtil mBastpUtil; + @Override public void onCreate() { @@ -385,6 +384,7 @@ public final class PlaybackService extends Service int state = loadState(); mMediaPlayer = getNewMediaPlayer(); + mBastpUtil = new BastpUtil(); mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE); @@ -610,7 +610,7 @@ public final class PlaybackService extends Service if(mReplayGainAlbumEnabled == false && mReplayGainTrackEnabled == false) return; /* no need to parse tags: RG is disabled */ - float[] rg = calculateReplayGainAdjustment(path); /* track, album */ + float[] rg = getReplayGainValues(path); /* track, album */ float adjust = 0f; if(mReplayGainAlbumEnabled) { @@ -635,29 +635,8 @@ public final class PlaybackService extends Service } - /** - * Returns TRACK, ALBUM gain values for given path - * A value of 0 means that the tag was not found in given file - */ - private float[] calculateReplayGainAdjustment(String path) { - String[] keys = { "REPLAYGAIN_TRACK_GAIN", "REPLAYGAIN_ALBUM_GAIN" }; - float[] adjust= { 0f , 0f }; - HashMap tags = (new Bastp()).getTags(path); - - for (int i=0; i