MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Stroke.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::Kinesis {
4
5/**
6 * @brief Write a level into an evenly spaced sample array at a fractional
7 * position, filling linearly from the previously written index.
8 *
9 * @p position in [0, 1] selects the index (0 is the first sample, 1 the
10 * last). @p level is clamped to [0, 1]. When @p previous holds a different
11 * valid index, every sample between that index and the new one is set on the
12 * line from the value last stored at @p previous to @p level, in either
13 * direction, so a fast gesture leaves no gaps. @p previous is updated to the
14 * index just written; reset it to clear the gesture.
15 *
16 * A NaN position selects the first sample; infinite positions clamp to the
17 * nearest end. An empty array is untouched.
18 *
19 * @param samples Samples to write into.
20 * @param previous Index written by the previous call of this gesture.
21 * @param position Fractional position in [0, 1].
22 * @param level Level in [0, 1].
23 */
24inline void stroke_samples(
25 std::span<float> samples,
26 std::optional<size_t>& previous,
27 float position,
28 float level) noexcept
29{
30 if (samples.empty())
31 return;
32
33 if (std::isnan(position))
34 position = 0.F;
35
36 const size_t last = samples.size() - 1;
37 const size_t index = static_cast<size_t>(std::clamp(position, 0.F, 1.F) * static_cast<float>(last));
38 const float target = std::clamp(level, 0.F, 1.F);
39
40 if (previous && *previous != index && *previous <= last) {
41 const size_t from = *previous;
42 const float start = samples[from];
43 const size_t lo = std::min(from, index);
44 const size_t hi = std::max(from, index);
45 const auto run = static_cast<float>(hi - lo);
46
47 for (size_t i = lo; i <= hi; ++i) {
48 const size_t travelled = i > from ? i - from : from - i;
49 const float fraction = static_cast<float>(travelled) / run;
50 samples[i] = start + (target - start) * fraction;
51 }
52 } else {
53 samples[index] = target;
54 }
55
56 previous = index;
57}
58
59} // namespace MayaFlux::Kinesis
std::optional< glm::vec3 > position
Definition Decoder.cpp:93
uint32_t index
Definition VKDevice.cpp:122
void run()
Definition main.cpp:21
void stroke_samples(std::span< float > samples, std::optional< size_t > &previous, float position, float level) noexcept
Write a level into an evenly spaced sample array at a fractional position, filling linearly from the ...
Definition Stroke.hpp:24