Read replaygain from Tag and adjust playback volume accordingly #5
Open
Description
This is the function I use to find the ReplayGain. Since where to write them differs widely, it iterates over all tagfields and applies regex matching (tag
needs to be an instance of org.jaudiotagger.tag.Tag
)
private static final float defaultGain = -6;
private static final Pattern gainPattern = Pattern.compile("-?[.\\d]+");
private float extractReplayGain() {
try {
Iterator<TagField> fields = tag.getFields();
while (fields.hasNext()) {
TagField field = fields.next();
if ((field.getId() + field.toString()).contains("replaygain_track_gain")) {
Matcher m = gainPattern.matcher(field.toString());
m.find();
float replaygain = Float.parseFloat(m.group());
return replaygain;
}
}
} catch (Exception e) {
}
return defaultGain;
}
float gain = (float) Math.pow(10, extractReplayGain() / 20);
Then you just need to multiply the volume with the gain
and you are ready to go!