Loading...
Searching...
No Matches
MusicalPosition.hpp
1#pragma once
2#include <Process/TimeValue.hpp>
3
4#include <ossia/editor/scenario/time_signature.hpp>
5
6#include <algorithm>
7#include <cstdint>
8#include <iterator>
9
10namespace Scenario
11{
19{
20 int columns{1};
21 int rows{1};
22 int pulses{1};
23
24 constexpr int row(int pulse) const noexcept { return pulse / columns; }
25 constexpr int column(int pulse) const noexcept { return pulse % columns; }
26 constexpr int pulsesInRow(int r) const noexcept
27 {
28 return r < rows - 1 ? columns : pulses - columns * (rows - 1);
29 }
30};
31
40constexpr PulseGridLayout pulseGridLayout(int numerator) noexcept
41{
42 const int n = std::max(1, numerator);
43
44 int cols = 1;
45 while(cols * cols < n)
46 cols++;
47
48 return {.columns = cols, .rows = (n + cols - 1) / cols, .pulses = n};
49}
50
53{
56 int64_t bar{};
58 int pulse{};
61 ossia::time_signature signature{};
62
63 friend bool operator==(const MusicalPosition&, const MusicalPosition&) = default;
64};
65
73template <typename Signatures>
74MusicalPosition musicalPosition(const Signatures& signatures, TimeVal date) noexcept
75{
76 constexpr int64_t whole = 4 * ossia::quarter_duration<int64_t>;
77 auto bar_duration = [](ossia::time_signature sig) {
78 return std::max<int64_t>(
79 1, whole * std::max<int64_t>(1, sig.upper) / std::max<int64_t>(1, sig.lower));
80 };
81
82 const int64_t t = std::max<int64_t>(0, date.impl);
83
84 int64_t bars = 0;
85 int64_t origin = 0;
86 ossia::time_signature sig{4, 4};
87
88 for(auto it = std::begin(signatures); it != std::end(signatures); ++it)
89 {
90 const int64_t change = it->first.impl;
91 if(change > t)
92 break;
93
94 if(change > origin)
95 {
96 const int64_t bar = bar_duration(sig);
97 bars += (change - origin + bar - 1) / bar;
98 origin = change;
99 }
100 sig = it->second;
101 }
102
103 sig.upper = std::max<uint16_t>(1, sig.upper);
104 sig.lower = std::max<uint16_t>(1, sig.lower);
105
106 const int64_t bar = bar_duration(sig);
107 const int64_t since = t - origin;
108 const int64_t in_bar = since % bar;
109
110 // Computed against the bar rather than from a rounded pulse duration, so
111 // that the last pulse of a bar never overflows into a pulse that does not
112 // exist.
113 const int64_t quarter_pulses = in_bar * sig.upper * 4 / bar;
114
115 MusicalPosition res;
116 res.bar = bars + since / bar;
117 res.pulse = int(std::min<int64_t>(quarter_pulses / 4, sig.upper - 1));
118 res.subdivision = int(quarter_pulses % 4);
119 res.signature = sig;
120 return res;
121}
122}
Main plug-in of score.
Definition ConnectionsList.hpp:22
MusicalPosition musicalPosition(const Signatures &signatures, TimeVal date) noexcept
Musical position of a date.
Definition MusicalPosition.hpp:74
constexpr PulseGridLayout pulseGridLayout(int numerator) noexcept
Grid for a time signature numerator.
Definition MusicalPosition.hpp:40
Where a date falls in the bars of a time signature map.
Definition MusicalPosition.hpp:53
int subdivision
Quarter of the pulse.
Definition MusicalPosition.hpp:60
int64_t bar
Definition MusicalPosition.hpp:56
int pulse
Pulse of the bar, in units of the signature's denominator.
Definition MusicalPosition.hpp:58
How the pulses of a bar are laid out in the transport's pulse grid.
Definition MusicalPosition.hpp:19
Definition TimeValue.hpp:21