From 1f0f4a75a03621e766b55608c12d7ba04b6ea66d Mon Sep 17 00:00:00 2001 From: Chris Moeller Date: Sun, 8 Feb 2015 19:20:24 -0800 Subject: [PATCH] Implemented bitrate reporting for vgmstream --- .../vgmstream/vgmstream/src/vgmstream.c | 36 +++++++++++++++++++ .../vgmstream/vgmstream/src/vgmstream.h | 4 +++ Plugins/vgmstream/vgmstream/VGMDecoder.h | 1 + Plugins/vgmstream/vgmstream/VGMDecoder.m | 4 ++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Frameworks/vgmstream/vgmstream/src/vgmstream.c b/Frameworks/vgmstream/vgmstream/src/vgmstream.c index c33b9cb94..da33db289 100644 --- a/Frameworks/vgmstream/vgmstream/src/vgmstream.c +++ b/Frameworks/vgmstream/vgmstream/src/vgmstream.c @@ -3307,3 +3307,39 @@ void try_dual_file_stereo(VGMSTREAM * opened_stream, STREAMFILE *streamFile) { fail: return; } + +static int get_vgmstream_channel_average_bitrate(VGMSTREAMCHANNEL * channel, int sample_rate, int length_samples) +{ + return (int)((int64_t)get_streamfile_size(channel->streamfile) * 8 * sample_rate / length_samples); +} + +int get_vgmstream_average_bitrate(VGMSTREAM * vgmstream) +{ + char path_current[PATH_LIMIT]; + char path_compare[PATH_LIMIT]; + + unsigned int i, j; + int bitrate = 0; + int sample_rate = vgmstream->sample_rate; + int length_samples = vgmstream->num_samples; + + if (vgmstream->channels >= 1) + bitrate += get_vgmstream_channel_average_bitrate(&vgmstream->ch[0], sample_rate, length_samples); + + for (i = 1; i < vgmstream->channels; ++i) + { + VGMSTREAMCHANNEL * ch = &vgmstream->ch[i]; + ch->streamfile->get_name(ch->streamfile, path_current, sizeof(path_current)); + for (j = 0; j < i; ++j) + { + VGMSTREAMCHANNEL * chc = &vgmstream->ch[j]; + chc->streamfile->get_name(chc->streamfile, path_compare, sizeof(path_compare)); + if (!strcmp(path_current, path_compare)) + break; + } + if (j == i) + bitrate += get_vgmstream_channel_average_bitrate(ch, sample_rate, length_samples); + } + + return bitrate; +} diff --git a/Frameworks/vgmstream/vgmstream/src/vgmstream.h b/Frameworks/vgmstream/vgmstream/src/vgmstream.h index 20976d9ad..3458b73d2 100644 --- a/Frameworks/vgmstream/vgmstream/src/vgmstream.h +++ b/Frameworks/vgmstream/vgmstream/src/vgmstream.h @@ -901,4 +901,8 @@ void describe_vgmstream(VGMSTREAM * vgmstream, char * desc, int length); * stereo stream. */ void try_dual_file_stereo(VGMSTREAM * opened_stream, STREAMFILE *streamFile); +/* Return the average bitrate in bps of all unique files contained within this + * stream. Compares files by absolute paths. */ +int get_vgmstream_average_bitrate(VGMSTREAM * vgmstream); + #endif diff --git a/Plugins/vgmstream/vgmstream/VGMDecoder.h b/Plugins/vgmstream/vgmstream/VGMDecoder.h index 206ce24cc..4658e9d19 100644 --- a/Plugins/vgmstream/vgmstream/VGMDecoder.h +++ b/Plugins/vgmstream/vgmstream/VGMDecoder.h @@ -18,6 +18,7 @@ int sampleRate; int channels; + int bitrate; long totalFrames; long framesLength; long framesFade; diff --git a/Plugins/vgmstream/vgmstream/VGMDecoder.m b/Plugins/vgmstream/vgmstream/VGMDecoder.m index cd158c73f..5815247e6 100644 --- a/Plugins/vgmstream/vgmstream/VGMDecoder.m +++ b/Plugins/vgmstream/vgmstream/VGMDecoder.m @@ -131,6 +131,8 @@ err1: framesRead = 0; + bitrate = get_vgmstream_average_bitrate(stream); + [self willChangeValueForKey:@"properties"]; [self didChangeValueForKey:@"properties"]; @@ -140,7 +142,7 @@ err1: - (NSDictionary *)properties { return [NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithInt:0], @"bitrate", + [NSNumber numberWithInt:bitrate / 1000], @"bitrate", [NSNumber numberWithInt:sampleRate], @"sampleRate", [NSNumber numberWithDouble:totalFrames], @"totalFrames", [NSNumber numberWithInt:16], @"bitsPerSample",