Move RG-Parsing into BastpUtil and provide an LRU cache
This commit is contained in:
parent
5050cd047c
commit
abc7941daa
80
src/ch/blinkenlights/android/vanilla/BastpUtil.java
Normal file
80
src/ch/blinkenlights/android/vanilla/BastpUtil.java
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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 <track,album>
|
||||||
|
*/
|
||||||
|
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<keys.length; i++) {
|
||||||
|
String curKey = keys[i];
|
||||||
|
if(tags.containsKey(curKey)) {
|
||||||
|
String rg_raw = (String)((Vector)tags.get(curKey)).get(0);
|
||||||
|
String rg_numonly = "";
|
||||||
|
float rg_float = 0f;
|
||||||
|
try {
|
||||||
|
String nums = rg_raw.replaceAll("[^0-9.-]","");
|
||||||
|
rg_float = Float.parseFloat(nums);
|
||||||
|
} catch(Exception e) {}
|
||||||
|
adjust[i] = rg_float;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return adjust;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** LRU cache for ReplayGain values
|
||||||
|
*/
|
||||||
|
private class RGLruCache extends LruCache<String, float[]> {
|
||||||
|
public RGLruCache(int size) {
|
||||||
|
super(size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -66,9 +66,6 @@ import java.io.EOFException;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
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 boolean mReplayGainAlbumEnabled;
|
||||||
private int mReplayGainBump;
|
private int mReplayGainBump;
|
||||||
|
|
||||||
|
private BastpUtil mBastpUtil;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate()
|
public void onCreate()
|
||||||
{
|
{
|
||||||
@ -385,6 +384,7 @@ public final class PlaybackService extends Service
|
|||||||
int state = loadState();
|
int state = loadState();
|
||||||
|
|
||||||
mMediaPlayer = getNewMediaPlayer();
|
mMediaPlayer = getNewMediaPlayer();
|
||||||
|
mBastpUtil = new BastpUtil();
|
||||||
|
|
||||||
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||||
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
|
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
|
||||||
@ -610,7 +610,7 @@ public final class PlaybackService extends Service
|
|||||||
if(mReplayGainAlbumEnabled == false && mReplayGainTrackEnabled == false)
|
if(mReplayGainAlbumEnabled == false && mReplayGainTrackEnabled == false)
|
||||||
return; /* no need to parse tags: RG is disabled */
|
return; /* no need to parse tags: RG is disabled */
|
||||||
|
|
||||||
float[] rg = calculateReplayGainAdjustment(path); /* track, album */
|
float[] rg = getReplayGainValues(path); /* track, album */
|
||||||
float adjust = 0f;
|
float adjust = 0f;
|
||||||
|
|
||||||
if(mReplayGainAlbumEnabled) {
|
if(mReplayGainAlbumEnabled) {
|
||||||
@ -635,29 +635,8 @@ public final class PlaybackService extends Service
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public float[] getReplayGainValues(String path) {
|
||||||
* Returns TRACK, ALBUM gain values for given path
|
return mBastpUtil.getReplayGainValues(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<keys.length; i++) {
|
|
||||||
String curKey = keys[i];
|
|
||||||
if(tags.containsKey(curKey)) {
|
|
||||||
String rg_raw = (String)((Vector)tags.get(curKey)).get(0);
|
|
||||||
String rg_numonly = "";
|
|
||||||
float rg_float = 0f;
|
|
||||||
try {
|
|
||||||
String nums = rg_raw.replaceAll("[^0-9.-]","");
|
|
||||||
rg_float = Float.parseFloat(nums);
|
|
||||||
} catch(Exception e) {}
|
|
||||||
adjust[i] = rg_float;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return adjust;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user