25 double fImpulse[HALF_SIZE + 2 *
BLIP_RES] = {};
31 double to_fraction =
M_PI / (2 * (HALF_SIZE - 1));
32 for (
auto i :
xrange(HALF_SIZE)) {
33 double angle = ((i - HALF_SIZE) * 2 + 1) * to_angle;
34 out[i] = cstd::sin<2>(angle) / angle;
35 out[i] *= 0.54 - 0.46 * cstd::cos<2>((2 * i + 1) * to_fraction);
40 out[HALF_SIZE + i] = out[HALF_SIZE - 1 - i];
45 for (
auto i :
xrange(HALF_SIZE)) {
48 double rescale = 1.0 / (2.0 * total);
52 float imp[IMPULSES_SIZE] = {};
55 for (
auto i :
xrange(IMPULSES_SIZE)) {
56 imp[i] = float((next -
sum) * rescale);
67 std::array<std::array<float, BLIP_IMPULSE_WIDTH>,
BLIP_RES> result = {};
69 const auto* imp_fwd = &imp[
BLIP_RES - phase];
70 const auto* imp_rev = &imp[phase];
71 auto* p = result[phase].data();
87 std::cout <<
"\t{ " <<
impulses[i][0];
98 memset(buffer, 0,
sizeof(buffer));
104 assert(tmp < BUFFER_SIZE);
105 availSamp = std::max<int>(availSamp, tmp);
108 unsigned ofst = time.
toInt() + offset;
109 const float* __restrict impulse =
impulses[phase].data();
111 float* __restrict result = &buffer[ofst];
113 result[i] += impulse[i] * delta;
117 buffer[(ofst + i) & BUFFER_MASK] += impulse[i] * delta;
124 template<
unsigned PITCH>
125 void BlipBuffer::readSamplesHelper(
float* __restrict out,
unsigned samples) __restrict
127 assert((offset + samples) <= BUFFER_SIZE);
129 unsigned ofst = offset;
130 for (
auto i :
xrange(samples)) {
131 out[i * PITCH] = acc;
138 offset = ofst & BUFFER_MASK;
141 static bool isSilent(
float x)
149 constexpr
float threshold = 1.0f / 32768;
150 return std::abs(
x) < threshold;
153 template<
unsigned PITCH>
156 if (availSamp <= 0) {
159 assert(
ranges::all_of(buffer, [](
const auto& b) {
return b == 0.0f; }));
161 if (isSilent(accum)) {
165 for (
auto i :
xrange(samples)) {
166 out[i * PITCH] = acc;
171 availSamp -= samples;
172 unsigned t1 =
std::min(samples, BUFFER_SIZE - offset);
173 readSamplesHelper<PITCH>(out, t1);
176 unsigned t2 = samples - t1;
177 assert(t2 < BUFFER_SIZE);
178 readSamplesHelper<PITCH>(&out[t1 * PITCH], t2);
180 assert(offset < BUFFER_SIZE);
185 template bool BlipBuffer::readSamples<1>(
float*,
unsigned);
186 template bool BlipBuffer::readSamples<2>(
float*,
unsigned);
bool readSamples(float *out, unsigned samples)
static constexpr int BLIP_PHASE_BITS
void addDelta(TimeIndex time, float delta)
A fixed point number, implemented by a 32-bit signed integer.
constexpr int toInt() const
Returns the integer part (rounded down) of this fixed point number.
constexpr unsigned fractAsInt() const
Returns the fractional part of this value as an integer.
constexpr vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
This file implemented 3 utility functions:
constexpr int BLIP_IMPULSE_WIDTH
constexpr KeyMatrixPosition x
Keyboard bindings.
constexpr float BASS_FACTOR
bool all_of(InputRange &&range, UnaryPredicate pred)
auto sum(InputRange &&range, Proj proj={})
constexpr auto xrange(T e)