Commit 5e686add authored by Max Kellermann's avatar Max Kellermann

wavpack: added wavpack_tag_float()

The function simplifies wavpack_replaygain(), because it already contains the float parser, and it works with a fixed buffer instead of doing expensive heap allocations.
parent c883d761
...@@ -213,20 +213,22 @@ wavpack_decode(struct decoder * decoder, WavpackContext *wpc, bool canseek, ...@@ -213,20 +213,22 @@ wavpack_decode(struct decoder * decoder, WavpackContext *wpc, bool canseek,
} while (samplesgot == samplesreq); } while (samplesgot == samplesreq);
} }
static char * /**
wavpack_tag(WavpackContext *wpc, const char *key) * Locate and parse a floating point tag. Returns true if it was
* found.
*/
static bool
wavpack_tag_float(WavpackContext *wpc, const char *key, float *value_r)
{ {
char *value = NULL; char buffer[64];
int size; int ret;
size = WavpackGetTagItem(wpc, key, NULL, 0);
if (size > 0) {
size++;
value = g_malloc(size);
WavpackGetTagItem(wpc, key, value, size);
}
return value; ret = WavpackGetTagItem(wpc, key, buffer, sizeof(buffer));
if (ret <= 0)
return false;
*value_r = atof(buffer);
return true;
} }
static struct replay_gain_info * static struct replay_gain_info *
...@@ -234,38 +236,20 @@ wavpack_replaygain(WavpackContext *wpc) ...@@ -234,38 +236,20 @@ wavpack_replaygain(WavpackContext *wpc)
{ {
struct replay_gain_info *replay_gain_info; struct replay_gain_info *replay_gain_info;
bool found = false; bool found = false;
char *value;
replay_gain_info = replay_gain_info_new(); replay_gain_info = replay_gain_info_new();
value = wavpack_tag(wpc, "replaygain_track_gain"); found = wavpack_tag_float(wpc, "replaygain_track_gain",
if (value) { &replay_gain_info->track_gain)
replay_gain_info->track_gain = atof(value); ||
free(value); wavpack_tag_float(wpc, "replaygain_track_peak",
found = true; &replay_gain_info->track_peak)
} ||
wavpack_tag_float(wpc, "replaygain_album_gain",
value = wavpack_tag(wpc, "replaygain_album_gain"); &replay_gain_info->album_gain)
if (value) { ||
replay_gain_info->album_gain = atof(value); wavpack_tag_float(wpc, "replaygain_album_peak",
free(value); &replay_gain_info->album_peak);
found = true;
}
value = wavpack_tag(wpc, "replaygain_track_peak");
if (value) {
replay_gain_info->track_peak = atof(value);
free(value);
found = true;
}
value = wavpack_tag(wpc, "replaygain_album_peak");
if (value) {
replay_gain_info->album_peak = atof(value);
free(value);
found = true;
}
if (found) { if (found) {
return replay_gain_info; return replay_gain_info;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment