blob: 802aa5ad6d249b1ba88cb8b31cc6c84647b61d98 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* Copyright (C) 2013-2019 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 github.daneren2005.dsub.util.tags;
import android.util.LruCache;
import java.util.ArrayList;
import java.util.HashMap;
public class BastpUtil {
/**
* Our global instance cache
*/
private RGLruCache rgCache;
/**
* What we return & cache
*/
public class GainValues {
public float album;
public float track;
public boolean found;
}
/**
* LRU cache for ReplayGain values
*/
private class RGLruCache extends LruCache<String, GainValues> {
public RGLruCache(int size) {
super(size);
}
}
public BastpUtil() {
rgCache = new RGLruCache(64);
}
/**
* Returns a GainValues object for `path'
*/
public GainValues getReplayGainValues(String path) {
if(path == null) {
// path must not be null
path = "//null\\";
}
GainValues cached = rgCache.get(path);
if(cached == null) {
cached = getReplayGainValuesFromFile(path);
rgCache.put(path, cached);
}
return cached;
}
/**
* Parse given file and return track,album replay gain values
*/
private GainValues getReplayGainValuesFromFile(String path) {
HashMap tags = (new Bastp()).getTags(path);
GainValues gv = new GainValues();
// normal replay gain
if(tags.containsKey("REPLAYGAIN_TRACK_GAIN")) {
gv.track = getFloatFromString((String)((ArrayList)tags.get("REPLAYGAIN_TRACK_GAIN")).get(0));
gv.found = true;
}
if(tags.containsKey("REPLAYGAIN_ALBUM_GAIN")) {
gv.album = getFloatFromString((String)((ArrayList)tags.get("REPLAYGAIN_ALBUM_GAIN")).get(0));
gv.found = true;
}
// R128 replay gain
boolean r128 = false;
if(tags.containsKey("R128_BASTP_BASE_GAIN")) {
// This is the gain found in the opus header which automatically gets applied by the media framework.
// We therefore do not need to include it in our calculation, but we set the 'found' bit and reset
// both album and track gain information as an opus file should only ever contain r128 gain infos.
float base = getFloatFromString((String)((ArrayList)tags.get("R128_BASTP_BASE_GAIN")).get(0)) / 256.0f;
if (base != 0.0f) {
gv.track = 0;
gv.album = 0;
gv.found = true;
}
}
if(tags.containsKey("R128_TRACK_GAIN")) {
gv.track += getFloatFromString((String)((ArrayList)tags.get("R128_TRACK_GAIN")).get(0)) / 256.0f;
gv.found = true;
r128 = true;
}
if(tags.containsKey("R128_ALBUM_GAIN")) {
gv.album += getFloatFromString((String)((ArrayList)tags.get("R128_ALBUM_GAIN")).get(0)) / 256.0f;
gv.found = true;
r128 = true;
}
if (r128) {
gv.track += 5.0f;
gv.album += 5.0f;
}
return gv;
}
/**
* Parses common replayGain string values
*/
private float getFloatFromString(String rg_raw) {
float rg_float = 0f;
try {
String nums = rg_raw.replaceAll("[^0-9.-]","");
rg_float = Float.parseFloat(nums);
} catch(Exception e) {}
return rg_float;
}
}
|