AudioFormat: add TimeToSize(), SizeToTime()

This commit is contained in:
Max Kellermann
2018-09-21 19:32:35 +02:00
parent 1a2012a97e
commit d3d1d37782
5 changed files with 28 additions and 6 deletions

View File

@@ -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());
}
};
/**