Improve audio duration api
This commit is contained in:
parent
5fa8e4a3a3
commit
f1c0959fba
3 changed files with 51 additions and 20 deletions
|
|
@ -9,14 +9,24 @@ namespace psemek::audio
|
||||||
constexpr int frequency = 44100;
|
constexpr int frequency = 44100;
|
||||||
constexpr float inv_frequency = 1.f / frequency;
|
constexpr float inv_frequency = 1.f / frequency;
|
||||||
|
|
||||||
|
inline std::int64_t seconds_to_frames(float seconds)
|
||||||
|
{
|
||||||
|
return static_cast<std::int64_t>(std::round(seconds * frequency));
|
||||||
|
}
|
||||||
|
|
||||||
inline std::int64_t seconds_to_samples(float seconds)
|
inline std::int64_t seconds_to_samples(float seconds)
|
||||||
{
|
{
|
||||||
return static_cast<std::int64_t>(2 * std::round(seconds * frequency));
|
return 2 * seconds_to_frames(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float frames_to_seconds(std::int64_t frames)
|
||||||
|
{
|
||||||
|
return static_cast<float>(frames) * inv_frequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float samples_to_seconds(std::int64_t samples)
|
inline float samples_to_seconds(std::int64_t samples)
|
||||||
{
|
{
|
||||||
return static_cast<float>(samples) * 0.5f * inv_frequency;
|
return frames_to_seconds(samples / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float to_db(float amplitude)
|
inline float to_db(float amplitude)
|
||||||
|
|
|
||||||
|
|
@ -10,66 +10,87 @@ namespace psemek::audio
|
||||||
|
|
||||||
struct duration
|
struct duration
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
struct samples_tag{};
|
||||||
|
struct frames_tag{};
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
duration()
|
duration()
|
||||||
: samples_{0}
|
: frames_{0}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
duration(std::int64_t samples)
|
static duration from_samples(std::int64_t samples)
|
||||||
: samples_{samples}
|
{
|
||||||
{}
|
return duration(samples_tag{}, samples);
|
||||||
|
}
|
||||||
|
|
||||||
duration(std::size_t samples)
|
static duration from_frames(std::int64_t samples)
|
||||||
: samples_{samples}
|
{
|
||||||
{}
|
return duration(frames_tag{}, samples);
|
||||||
|
}
|
||||||
|
|
||||||
duration(float seconds)
|
duration(float seconds)
|
||||||
: samples_{seconds_to_samples(seconds)}
|
: frames_{seconds_to_frames(seconds)}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
duration(duration const & other)
|
duration(duration const & other)
|
||||||
: samples_{other.samples_}
|
: frames_{other.frames_}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
float seconds() const
|
float seconds() const
|
||||||
{
|
{
|
||||||
return samples_to_seconds(samples_);
|
return frames_to_seconds(frames_);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::int64_t frames() const
|
||||||
|
{
|
||||||
|
return frames_;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::int64_t samples() const
|
std::int64_t samples() const
|
||||||
{
|
{
|
||||||
return samples_;
|
return frames_ * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
duration & operator = (duration const & other)
|
duration & operator = (duration const & other)
|
||||||
{
|
{
|
||||||
samples_ = other.samples_;
|
frames_ = other.frames_;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
duration & operator += (duration const & other)
|
duration & operator += (duration const & other)
|
||||||
{
|
{
|
||||||
samples_ += other.samples_;
|
frames_ += other.frames_;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
duration & operator -= (duration const & other)
|
duration & operator -= (duration const & other)
|
||||||
{
|
{
|
||||||
samples_ -= other.samples_;
|
frames_ -= other.frames_;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend duration operator + (duration const & d1, duration const & d2)
|
friend duration operator + (duration const & d1, duration const & d2)
|
||||||
{
|
{
|
||||||
return duration{d1.samples() + d2.samples()};
|
return duration{d1.frames() + d2.frames()};
|
||||||
}
|
}
|
||||||
|
|
||||||
friend duration operator - (duration const & d1, duration const & d2)
|
friend duration operator - (duration const & d1, duration const & d2)
|
||||||
{
|
{
|
||||||
return duration{d1.samples() - d2.samples()};
|
return duration{d1.frames() - d2.frames()};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::int64_t samples_;
|
std::int64_t frames_;
|
||||||
|
|
||||||
|
duration(samples_tag, std::int64_t samples)
|
||||||
|
: frames_(samples / 2)
|
||||||
|
{}
|
||||||
|
|
||||||
|
duration(frames_tag, std::int64_t frames)
|
||||||
|
: frames_(frames)
|
||||||
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ namespace psemek::audio
|
||||||
|
|
||||||
if (result <= start_.samples())
|
if (result <= start_.samples())
|
||||||
{
|
{
|
||||||
start_ -= result;
|
start_ -= duration::from_samples(result);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue