39 lines
970 B
C++
39 lines
970 B
C++
#pragma once
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <cstdint>
|
|
#include <algorithm>
|
|
|
|
namespace psemek::audio
|
|
{
|
|
|
|
using sample = std::int16_t;
|
|
|
|
inline sample clamp(float v)
|
|
{
|
|
static auto const min = std::numeric_limits<sample>::min();
|
|
static auto const max = std::numeric_limits<sample>::max();
|
|
|
|
return static_cast<sample>(std::min<int>(max, std::max<int>(min, std::round(v))));
|
|
}
|
|
|
|
// maps [-1, 1] range to samples
|
|
inline sample pack(float v)
|
|
{
|
|
static auto const min = static_cast<float>(std::numeric_limits<sample>::min());
|
|
static auto const max = static_cast<float>(std::numeric_limits<sample>::max());
|
|
|
|
return clamp(min + (v * 0.5f + 0.5f) * (max - min));
|
|
}
|
|
|
|
// maps samples to [-1, 1] range
|
|
inline float unpack(sample s)
|
|
{
|
|
static auto const min = static_cast<float>(std::numeric_limits<sample>::min());
|
|
static auto const max = static_cast<float>(std::numeric_limits<sample>::max());
|
|
|
|
return 2.f * (s - min) / (max - min) - 1.f;
|
|
}
|
|
|
|
}
|