From d3d1d37782952e9b1142a4735635aac3dd2db466 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 21 Sep 2018 19:32:35 +0200 Subject: AudioFormat: add TimeToSize(), SizeToTime() --- src/AudioFormat.hxx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/AudioFormat.hxx') diff --git a/src/AudioFormat.hxx b/src/AudioFormat.hxx index 8b0ffb557..8c9cda3e3 100644 --- a/src/AudioFormat.hxx +++ b/src/AudioFormat.hxx @@ -23,6 +23,8 @@ #include "pcm/SampleFormat.hxx" #include "util/Compiler.h" +#include + #include #include @@ -152,6 +154,28 @@ struct AudioFormat { * span to a storage size in bytes. */ double GetTimeToSize() const; + + template + constexpr auto TimeToFrames(D t) const noexcept { + using Period = typename D::period; + return ((t.count() * sample_rate) / Period::den) * Period::num; + } + + template + constexpr size_t TimeToSize(D t) const noexcept { + return size_t(size_t(TimeToFrames(t)) * GetFrameSize()); + } + + template + constexpr D FramesToTime(std::uintmax_t size) const noexcept { + using Period = typename D::period; + return D(((size / Period::num) * Period::den) / sample_rate); + } + + template + constexpr D SizeToTime(std::uintmax_t size) const noexcept { + return FramesToTime(size / GetFrameSize()); + } }; /** -- cgit v1.2.3