summaryrefslogtreecommitdiff
path: root/src/AudioFormat.hxx
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2018-09-21 19:32:35 +0200
committerMax Kellermann <max@musicpd.org>2018-09-21 20:51:08 +0200
commitd3d1d37782952e9b1142a4735635aac3dd2db466 (patch)
tree24d16576ff0a361b7d55525a644d9d071ef3d411 /src/AudioFormat.hxx
parent1a2012a97e993869914a224d1603fdafcf3f9d3b (diff)
AudioFormat: add TimeToSize(), SizeToTime()
Diffstat (limited to 'src/AudioFormat.hxx')
-rw-r--r--src/AudioFormat.hxx24
1 files changed, 24 insertions, 0 deletions
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 <chrono>
+
#include <stdint.h>
#include <stddef.h>
@@ -152,6 +154,28 @@ struct AudioFormat {
* span to a storage size in bytes.
*/
double GetTimeToSize() const;
+
+ template<typename D>
+ constexpr auto TimeToFrames(D t) const noexcept {
+ using Period = typename D::period;
+ return ((t.count() * sample_rate) / Period::den) * Period::num;
+ }
+
+ template<typename D>
+ constexpr size_t TimeToSize(D t) const noexcept {
+ return size_t(size_t(TimeToFrames(t)) * GetFrameSize());
+ }
+
+ template<typename D>
+ constexpr D FramesToTime(std::uintmax_t size) const noexcept {
+ using Period = typename D::period;
+ return D(((size / Period::num) * Period::den) / sample_rate);
+ }
+
+ template<typename D>
+ constexpr D SizeToTime(std::uintmax_t size) const noexcept {
+ return FramesToTime<D>(size / GetFrameSize());
+ }
};
/**