From 694cded5872da1d1dcd3e34e2a4f8662e37d46d8 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 28 Jan 2018 00:31:12 +0100 Subject: [PATCH] Add stream_size to calculate bitrate in subsongs, print bitrate info --- src/vgmstream.c | 16 ++++++++++++++-- src/vgmstream.h | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/vgmstream.c b/src/vgmstream.c index f7969294..3677f6fc 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -2200,6 +2200,11 @@ void describe_vgmstream(VGMSTREAM * vgmstream, char * desc, int length) { } concatn(length,desc,temp); + snprintf(temp,TEMPSIZE, + "\nbitrate: %d kbps", + get_vgmstream_average_bitrate(vgmstream) / 1000); + concatn(length,desc,temp); + /* only interesting if more than one */ if (vgmstream->num_streams > 1) { snprintf(temp,TEMPSIZE, @@ -2448,9 +2453,11 @@ static STREAMFILE * get_vgmstream_average_bitrate_channel_streamfile(VGMSTREAM * return vgmstream->ch[channel].streamfile; } +static int get_vgmstream_average_bitrate_from_size(size_t size, int sample_rate, int length_samples) { + return (int)((int64_t)size * 8 * sample_rate / length_samples); +} static int get_vgmstream_average_bitrate_from_streamfile(STREAMFILE * streamfile, int sample_rate, int length_samples) { - // todo: not correct in subsongs or formats which only use part of the data - return (int)((int64_t)get_streamfile_size(streamfile) * 8 * sample_rate / length_samples); + return get_vgmstream_average_bitrate_from_size(get_streamfile_size(streamfile), sample_rate, length_samples); } /* Return the average bitrate in bps of all unique files contained within this stream. */ @@ -2468,6 +2475,11 @@ int get_vgmstream_average_bitrate(VGMSTREAM * vgmstream) { if (!sample_rate || !channels || !length_samples) return 0; + /* subsongs need to report this to properly calculate */ + if (vgmstream->stream_size) { + return get_vgmstream_average_bitrate_from_size(vgmstream->stream_size, sample_rate, length_samples); + } + if (channels >= 1) { streamFile = get_vgmstream_average_bitrate_channel_streamfile(vgmstream, 0); if (streamFile) { diff --git a/src/vgmstream.h b/src/vgmstream.h index 5a441927..ba647706 100644 --- a/src/vgmstream.h +++ b/src/vgmstream.h @@ -737,6 +737,7 @@ typedef struct { int num_streams; /* for multi-stream formats (0=not set/one stream, 1=one stream) */ int stream_index; /* selected stream (also 1-based) */ char stream_name[STREAM_NAME_SIZE]; /* name of the current stream (info), if the file stores it and it's filled */ + size_t stream_size; /* info to properly calculate bitrate */ /* looping */ int loop_flag; /* is this stream looped? */