Debian Codegraph @

au4/src/playback/view/toolbars/components/beatsformatter.cpp

raw
1 /*
2 * Audacity: A Digital Audio Editor
3 */
4 #include "beatsformatter.h"
5
6 #include <cstdio>
7
8 #include "translation.h"
9 #include "log.h"
10
11 using namespace au::playback;
12
13 static constexpr size_t pow10(int power)
14 {
15 size_t result = 1;
16 for (int i = 0; i < power; ++i) {
17 result *= 10;
18 }
19 return result;
20 }
21
22 static constexpr std::array<size_t, 3> MIN_DIGITS { 3, 2, 2 };
23 static constexpr std::array<size_t, 3> UPPER_BOUNDS {
24 pow10(MIN_DIGITS[0] - 1) + 1, pow10(MIN_DIGITS[1] - 1) + 1,
25 pow10(MIN_DIGITS[2] - 1) + 1
26 };
27
28 static QString barString()
29 {
30 return muse::qtrc("playback", "bar");
31 }
32
33 static QString beatString()
34 {
35 return muse::qtrc("playback", "beat");
36 }
37
38 BeatsFormatter::BeatsFormatter(const QString& formatStr, int fracPart)
39 : TimecodeFormatter(formatStr), m_fracPart(fracPart)
40 {
41 }
42
43 void BeatsFormatter::init()
44 {
45 const bool formatOk = checkField(1, m_upperTimeSignature) && checkFracField(m_lowerTimeSignature);
46
47 // 1/4 = BPM is used for now
48 const auto quarterLength = 60.0 / m_tempo;
49 const auto beatLength = quarterLength * 4.0 / m_lowerTimeSignature;
50 const auto barLength = m_upperTimeSignature * beatLength;
51
52 m_fieldLengths[0] = barLength;
53 m_fieldLengths[1] = beatLength;
54
55 const auto hasFracPart = m_fracPart > m_lowerTimeSignature;
56
57 if (hasFracPart) {
58 const auto fracLength = beatLength * m_lowerTimeSignature / m_fracPart;
59 m_fieldLengths[2] = fracLength;
60 }
61
62 if (formatOk) {
63 return;
64 }
65
66 updateFields(MIN_DIGITS[0]);
67 }
68
69 BeatsFormatter::ConversionResult BeatsFormatter::valueToString(double value, bool nearest) const
70 {
71 UNUSED(nearest);
72
73 ConversionResult result;
74 result.fieldValueStrings.resize(m_fields.size());
75
76 if (value < 0) {
77 for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) {
78 const auto digitsCount = m_fields[fieldIndex].digits;
79 auto& fieldValue = result.fieldValueStrings[fieldIndex];
80 for (size_t digitIndex = 0; digitIndex < digitsCount; ++digitIndex) {
81 fieldValue += "-";
82 }
83 }
84
85 updateResultString(result);
86
87 return result;
88 }
89
90 // Calculate the epsilon only once, so the total loss of precision is addressed.
91 // This is a "multiplicative" epsilon, so there is no need to calculate 1 + eps every time.
92 const auto eps = 1.0 + std::max(1.0, value) * std::numeric_limits<double>::epsilon();
93
94 for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) {
95 const auto fieldLength = m_fieldLengths[fieldIndex];
96 const auto fieldValue = std::max(
97 0, static_cast<int>(std::floor(value * eps / fieldLength)));
98
99 char field[10];
100 snprintf(field, sizeof(field), m_fields[fieldIndex].formatStr.toStdString().c_str(), (int)(fieldValue + m_fieldValueOffset));
101
102 result.fieldValueStrings[fieldIndex] = field;
103
104 value = value - fieldValue * fieldLength;
105 }
106
107 updateResultString(result);
108 return result;
109 }
110
111 std::optional<double> BeatsFormatter::stringToValue(const QString& value) const
112 {
113 if (m_fields.size() > 0
114 && value.mid(m_fields[0].pos, 1) == '-') {
115 return std::nullopt;
116 }
117
118 double t = 0.0;
119 size_t lastIndex = 0;
120
121 for (size_t i = 0; i < m_fields.size(); i++) {
122 const auto& field = m_fields[i];
123
124 const size_t labelIndex = field.label.isEmpty()
125 ? muse::nidx
126 : value.indexOf(field.label, lastIndex);
127
128 const auto fieldStringValue = value.mid(lastIndex, labelIndex == muse::nidx ? labelIndex : labelIndex - lastIndex);
129
130 bool ok = false;
131 long val = fieldStringValue.toLong(&ok);
132
133 if (!ok) {
134 return std::nullopt;
135 }
136
137 t += (val - m_fieldValueOffset) * m_fieldLengths[i];
138
139 lastIndex = labelIndex + field.label.size();
140 }
141
142 return t;
143 }
144
145 double BeatsFormatter::singleStep(double value, int digitIndex, bool upwards)
146 {
147 if (digitIndex < 0 || size_t(digitIndex) >= m_digits.size()) {
148 return value;
149 }
150
151 const auto& digit = m_digits[digitIndex];
152 const auto& fieldIndex = digit.field;
153 const auto& field = m_fields[fieldIndex];
154
155 const auto stepSize = m_fieldLengths[fieldIndex]
156 * std::pow(10, field.digits - digit.index - 1);
157
158 return upwards ? value + stepSize : value - stepSize;
159 }
160
161 void BeatsFormatter::updateResultString(ConversionResult& result) const
162 {
163 for (size_t fieldIndex = 0; fieldIndex < m_fields.size(); ++fieldIndex) {
164 result.valueString += result.fieldValueStrings[fieldIndex] + m_fields[fieldIndex].label;
165 }
166 }
167
168 //! Check that field exists and has enough digits to fit the value
169 bool BeatsFormatter::checkField(size_t fieldIndex, int value) const
170 {
171 if (fieldIndex >= m_fields.size()) {
172 return false;
173 }
174
175 const auto digitsCount = m_fields[fieldIndex].digits;
176
177 // Format always allows at least two digits
178 const auto lowerRange = digitsCount > MIN_DIGITS[fieldIndex] ? pow10(digitsCount - 1) : 0;
179
180 const auto upperRange = pow10(digitsCount);
181
182 return value >= int(lowerRange) && value < int(upperRange);
183 }
184
185 bool BeatsFormatter::checkFracField(int newLts) const
186 {
187 if (m_fracPart > newLts) {
188 return checkField(2, m_fracPart / m_lowerTimeSignature);
189 } else {
190 return m_fields.size() == 2;
191 }
192 }
193
194 void BeatsFormatter::updateFields(size_t barsDigits)
195 {
196 m_fields.clear();
197 m_digits.clear();
198
199 // Range is assumed to allow 999 bars.
200 auto& barsField = m_fields.emplace_back(NumericField::withDigits(barsDigits));
201
202 barsField.label = " " + barString() + " ";
203
204 // Beats format is 1 based. For the time point "0" the expected output is
205 // "1 bar 1 beat [1]" For this reason we use (uts + 1) as the "range". On
206 // top of that, we want at least two digits to be shown. NumericField
207 // accepts range as in [0, range), so add 1.
208
209 auto& beatsField = m_fields.emplace_back(NumericField::range(std::max<size_t>(UPPER_BOUNDS[1], m_upperTimeSignature + 1)));
210
211 beatsField.label = " " + beatString();
212
213 const auto hasFracPart = m_fracPart > m_lowerTimeSignature;
214
215 if (hasFracPart) {
216 beatsField.label += " ";
217 // See the reasoning above about the range
218 m_fields.emplace_back(NumericField::range(std::max(11, m_fracPart / m_lowerTimeSignature + 1)));
219 }
220
221 // Fill the aux m_digits structure
222 size_t pos = 0;
223 for (size_t i = 0; i < m_fields.size(); i++) {
224 m_fields[i].pos = pos;
225
226 for (size_t j = 0; j < m_fields[i].digits; j++) {
227 m_digits.push_back(DigitInfo { i, j, pos });
228 pos++;
229 }
230
231 pos += m_fields[i].label.length();
232 }
233 }