From c37ccb985125f5efdf7600989331085f60d8a973 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 10 Aug 2023 21:34:48 +1000 Subject: Reintroduce track timer, now relying more on codecs --- src/codecs/foxenflac.cpp | 2 +- src/codecs/include/codec.hpp | 2 +- src/codecs/mad.cpp | 4 ++-- src/codecs/opus.cpp | 15 +++++++++++++++ src/codecs/vorbis.cpp | 7 +++++++ 5 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src/codecs') diff --git a/src/codecs/foxenflac.cpp b/src/codecs/foxenflac.cpp index eef8225a..b1ba348a 100644 --- a/src/codecs/foxenflac.cpp +++ b/src/codecs/foxenflac.cpp @@ -64,7 +64,7 @@ auto FoxenFlacDecoder::OpenStream(std::shared_ptr input) uint64_t num_samples = fx_flac_get_streaminfo(flac_, FLAC_KEY_N_SAMPLES); if (num_samples > 0) { - format.duration_seconds = num_samples / fs; + format.total_samples = num_samples / fs; } return format; diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp index ece3d4fe..2f95389c 100644 --- a/src/codecs/include/codec.hpp +++ b/src/codecs/include/codec.hpp @@ -91,7 +91,7 @@ class ICodec { struct OutputFormat { uint8_t num_channels; uint32_t sample_rate_hz; - std::optional duration_seconds; + std::optional total_samples; bool operator==(const OutputFormat&) const = default; }; diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp index ce3a9cac..dfa568d4 100644 --- a/src/codecs/mad.cpp +++ b/src/codecs/mad.cpp @@ -92,7 +92,7 @@ auto MadMp3Decoder::OpenStream(std::shared_ptr input) auto vbr_length = GetVbrLength(header); if (vbr_length) { - output.duration_seconds = vbr_length; + output.total_samples = vbr_length; } return output; } @@ -234,7 +234,7 @@ auto MadMp3Decoder::GetVbrLength(const mad_header& header) return {}; } - return (double)(frames_count * samples_per_frame) / header.samplerate; + return (double)(frames_count * samples_per_frame); } } // namespace codecs diff --git a/src/codecs/opus.cpp b/src/codecs/opus.cpp index c0727c6b..f5ff471a 100644 --- a/src/codecs/opus.cpp +++ b/src/codecs/opus.cpp @@ -122,9 +122,24 @@ auto XiphOpusDecoder::OpenStream(std::shared_ptr input) return cpp::fail(Error::kMalformedData); } + auto l = op_pcm_total(opus_, -1); + std::optional length; + if (l > 0) { + // Output is always downmixed to two channels, but the pcm count does not + // reflect this. + int channels = op_channel_count(opus_, -1); + if (channels == 1) { + l *= 2; + } else if (channels > 2) { + l /= channels * 2; + } + length = l; + } + return OutputFormat{ .num_channels = 2, .sample_rate_hz = 48000, + .total_samples = length, }; } diff --git a/src/codecs/vorbis.cpp b/src/codecs/vorbis.cpp index aa367e02..cf783978 100644 --- a/src/codecs/vorbis.cpp +++ b/src/codecs/vorbis.cpp @@ -118,9 +118,16 @@ auto TremorVorbisDecoder::OpenStream(std::shared_ptr input) return cpp::fail(Error::kMalformedData); } + auto l = ov_pcm_total(&vorbis_, -1); + std::optional length; + if (l > 0) { + length = l; + } + return OutputFormat{ .num_channels = static_cast(info->channels), .sample_rate_hz = static_cast(info->rate), + .total_samples = length, }; } -- cgit v1.2.3