From c1a999c4927181d5b9621af7a7808266e17f64e2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 10 Nov 2009 22:03:58 +0100 Subject: decoder/flac: don't use float to calculate song duration Simple (up-rounding) integer division is good enough. We're casting the result back to an integer anyway. --- src/decoder/_flac_common.h | 7 +++++++ src/decoder/flac_plugin.c | 7 ++----- src/decoder/oggflac_plugin.c | 4 +--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/decoder/_flac_common.h b/src/decoder/_flac_common.h index fc240aa11..363f1ae8a 100644 --- a/src/decoder/_flac_common.h +++ b/src/decoder/_flac_common.h @@ -159,6 +159,13 @@ struct flac_data { struct tag *tag; }; +static inline unsigned +flac_duration(const FLAC__StreamMetadata_StreamInfo *stream_info) +{ + return (stream_info->total_samples + stream_info->sample_rate - 1) / + stream_info->sample_rate; +} + /* initializes a given FlacData struct */ void flac_data_init(struct flac_data *data, struct decoder * decoder, diff --git a/src/decoder/flac_plugin.c b/src/decoder/flac_plugin.c index e2620c676..1694f71fc 100644 --- a/src/decoder/flac_plugin.c +++ b/src/decoder/flac_plugin.c @@ -272,8 +272,7 @@ flac_tag_load(const char *file, const char *char_tnum) flac_vorbis_comments_to_tag(tag, char_tnum, &block->data.vorbis_comment); } else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) { - tag->time = ((float)block->data.stream_info.total_samples) / - block->data.stream_info.sample_rate + 0.5; + tag->time = flac_duration(&block->data.stream_info); } FLAC__metadata_object_delete(block); } while (FLAC__metadata_simple_iterator_next(it)); @@ -853,9 +852,7 @@ oggflac_tag_dup(const char *file) flac_vorbis_comments_to_tag(ret, NULL, &block->data.vorbis_comment); } else if (block->type == FLAC__METADATA_TYPE_STREAMINFO) { - ret->time = ((float)block->data.stream_info. - total_samples) / - block->data.stream_info.sample_rate + 0.5; + ret->time = flac_duration(&block->data.stream_info); } } while (FLAC__metadata_iterator_next(it)); FLAC__metadata_iterator_delete(it); diff --git a/src/decoder/oggflac_plugin.c b/src/decoder/oggflac_plugin.c index 0f5e774c3..a4912f8ac 100644 --- a/src/decoder/oggflac_plugin.c +++ b/src/decoder/oggflac_plugin.c @@ -172,9 +172,7 @@ static void of_metadata_dup_cb(G_GNUC_UNUSED const OggFLAC__SeekableStreamDecode switch (block->type) { case FLAC__METADATA_TYPE_STREAMINFO: - data->tag->time = ((float)block->data.stream_info. - total_samples) / - block->data.stream_info.sample_rate + 0.5; + data->tag->time = flac_duration(&block->data.stream_info); return; case FLAC__METADATA_TYPE_VORBIS_COMMENT: flac_vorbis_comments_to_tag(data->tag, NULL, -- cgit v1.2.3