1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Magnus Holmgren
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
29 #include "strcasecmp.h"
33 #include "replaygain.h"
34 #include "fixedpoint.h"
37 #define FP_ONE (1 << FP_BITS)
38 #define FP_MIN (-48 * FP_ONE)
39 #define FP_MAX ( 17 * FP_ONE)
41 void replaygain_itoa(char* buffer
, int length
, long int_gain
)
43 /* int_gain uses Q19.12 format. */
44 int one
= abs(int_gain
) >> FP_BITS
;
45 int cent
= ((abs(int_gain
) & 0x0fff) * 100 + (FP_ONE
/2)) >> FP_BITS
;
46 snprintf(buffer
, length
, "%s%d.%02d dB", (int_gain
<0) ? "-":"", one
, cent
);
49 static long fp_atof(const char* s
, int precision
)
52 long int_one
= BIT_N(precision
);
55 long frac_max
= ((precision
* 4) + 12) / 13;
56 long frac_max_int
= 1;
60 while ((*s
!= '\0') && isspace(*s
))
90 if (frac_count
< frac_max
)
92 frac_part
= frac_part
* 10 + (*s
- '0');
99 int_part
= int_part
* 10 + (*s
- '0');
110 while (frac_count
< frac_max
)
117 return sign
* ((int_part
* int_one
)
118 + (((int64_t) frac_part
* int_one
) / frac_max_int
));
121 static long convert_gain(long gain
)
123 /* Don't allow unreasonably low or high gain changes.
124 * Our math code can't handle it properly anyway. :) */
125 gain
= MAX(gain
, FP_MIN
);
126 gain
= MIN(gain
, FP_MAX
);
128 return fp_factor(gain
, FP_BITS
) << (24 - FP_BITS
);
131 /* Get the sample scale factor in Q19.12 format from a gain value. Returns 0
134 * str Gain in dB as a string. E.g., "-3.45 dB"; the "dB" part is ignored.
136 static long get_replaygain(const char* str
)
138 return fp_atof(str
, FP_BITS
);
141 /* Get the peak volume in Q7.24 format.
143 * str Peak volume. Full scale is specified as "1.0". Returns 0 for no peak.
145 static long get_replaypeak(const char* str
)
147 return fp_atof(str
, 24);
150 /* Get a sample scale factor in Q7.24 format from a gain value.
152 * int_gain Gain in dB, multiplied by 100.
154 long get_replaygain_int(long int_gain
)
156 return convert_gain(int_gain
* FP_ONE
/ 100);
159 /* Parse a ReplayGain tag conforming to the "VorbisGain standard". If a
160 * valid tag is found, update mp3entry struct accordingly. Existing values
161 * are not overwritten.
163 * key Name of the tag.
164 * value Value of the tag.
165 * entry mp3entry struct to update.
167 void parse_replaygain(const char* key
, const char* value
,
168 struct mp3entry
* entry
)
170 if (((strcasecmp(key
, "replaygain_track_gain") == 0) ||
171 (strcasecmp(key
, "rg_radio") == 0)) &&
174 entry
->track_level
= get_replaygain(value
);
175 entry
->track_gain
= convert_gain(entry
->track_level
);
177 else if (((strcasecmp(key
, "replaygain_album_gain") == 0) ||
178 (strcasecmp(key
, "rg_audiophile") == 0)) &&
181 entry
->album_level
= get_replaygain(value
);
182 entry
->album_gain
= convert_gain(entry
->album_level
);
184 else if (((strcasecmp(key
, "replaygain_track_peak") == 0) ||
185 (strcasecmp(key
, "rg_peak") == 0)) &&
188 entry
->track_peak
= get_replaypeak(value
);
190 else if ((strcasecmp(key
, "replaygain_album_peak") == 0) &&
193 entry
->album_peak
= get_replaypeak(value
);
197 /* Set ReplayGain values from integers. Existing values are not overwritten.
199 * album If true, set album values, otherwise set track values.
200 * gain Gain value in dB, multiplied by 512. 0 for no gain.
201 * peak Peak volume in Q7.24 format, where 1.0 is full scale. 0 for no
203 * entry mp3entry struct to update.
205 void parse_replaygain_int(bool album
, long gain
, long peak
,
206 struct mp3entry
* entry
)
208 gain
= gain
* FP_ONE
/ 512;
212 entry
->album_level
= gain
;
213 entry
->album_gain
= convert_gain(gain
);
214 entry
->album_peak
= peak
;
218 entry
->track_level
= gain
;
219 entry
->track_gain
= convert_gain(gain
);
220 entry
->track_peak
= peak
;