| 1 |
/* |
| 2 |
* Audacity: A Digital Audio Editor |
| 3 |
*/ |
| 4 |
#include "numericformatter.h" |
| 5 |
|
| 6 |
#include <cstdio> |
| 7 |
|
| 8 |
using namespace au::playback; |
| 9 |
|
| 10 |
/** |
| 11 |
NumericConverter |
| 12 |
\class NumericConverter |
| 13 |
\brief NumericConverter provides the advanced formatting control used |
| 14 |
in the selection bar of Audacity. |
| 15 |
|
| 16 |
Any negative value given to the converter is considered invalid and |
| 17 |
all digit positions of the resulting string will be filled with hyphens. |
| 18 |
Otherwise: |
| 19 |
|
| 20 |
The NumericConverter makes use of a format string to specify the |
| 21 |
exact way that a single value is split into several fields, |
| 22 |
such as the hh:mm:ss format. The advantage of this format string |
| 23 |
is that it is very small and compact, but human-readable and |
| 24 |
somewhat intuitive, so that it's easy to add NEW layouts |
| 25 |
in the future. It's also designed to make it easier to add |
| 26 |
i18n support, since the way that numbers are displayed in different |
| 27 |
languages could conceivably vary a lot. |
| 28 |
|
| 29 |
The number to be formatted may be expressed in seconds, so the format |
| 30 |
string can specify the relationship of each field to the number of |
| 31 |
seconds. |
| 32 |
|
| 33 |
The class is also reused to format some non-time values such as |
| 34 |
frequency and log of frequency. |
| 35 |
|
| 36 |
Let's start by considering an example: here's the format string |
| 37 |
that prints an integer number of seconds in the hour minute |
| 38 |
second h:m:s format: |
| 39 |
|
| 40 |
*:60:60 |
| 41 |
|
| 42 |
The "*" is a wildcard, saying that the leftmost field can contain |
| 43 |
numbers of arbitrary magnitude. The next character, ':', since it |
| 44 |
is not a digit or a wildcard, is interpreted as a delimiter, and |
| 45 |
will be displayed between those fields. The next number, 60, |
| 46 |
indicates that the range of the next field (minutes) is 60. |
| 47 |
Then there's another ':' delimiter, and finally the last field |
| 48 |
(seconds) is 60. So, if you give it a number like 3758 |
| 49 |
it is formatted as: |
| 50 |
|
| 51 |
3758 seconds, "*:60:60" -> "1:2:38" |
| 52 |
|
| 53 |
Note that 3758 = 1*60*60 + 2*60 + 38. |
| 54 |
|
| 55 |
When NumericConverter formats an integer, you can think of its process |
| 56 |
as working from right to left. Given the value "3758", it fills |
| 57 |
in the seconds by dividing by 60, sticking the remainder in the |
| 58 |
seconds field and then passing the quotient to the next field to |
| 59 |
the left. |
| 60 |
|
| 61 |
In order to format a field with leading zeros, simply add a leading |
| 62 |
zero to that field, like this: |
| 63 |
|
| 64 |
3758 seconds, "*:060:060" -> "1:02:38" |
| 65 |
|
| 66 |
In order to format fractions, simply include a field delimiter |
| 67 |
ending with a decimal point. If the delimiter is simply '.' with |
| 68 |
nothing else, then the '.' is actually displayed. Otherwise the |
| 69 |
'.' is dropped, and the other characters in the delimiter are |
| 70 |
displayed instead. |
| 71 |
|
| 72 |
Here's how we'd display hours, minutes, and seconds with three |
| 73 |
decimal places after the seconds: |
| 74 |
|
| 75 |
3758.5 seconds, "*:060:060.01000" -> "1:02:38.500" |
| 76 |
|
| 77 |
Similarly, here's how we'd display the fractional part of |
| 78 |
seconds as film frames (24 per second) instead of milliseconds: |
| 79 |
|
| 80 |
3758.5 seconds, "*:060:060 and .24 frames" -> "1:02:38 and 12 frames" |
| 81 |
|
| 82 |
Note that the decimal '.' is associated with the delimiter, not |
| 83 |
with the 24. |
| 84 |
|
| 85 |
Additionally, the special character '#' can be used in place of a number |
| 86 |
to represent the current sample rate. Use '0#' to add leading |
| 87 |
zeros to that field. For example: |
| 88 |
|
| 89 |
3758.5 seconds, "*:060:060+.#samples" -> "1:02:38+22050samples" |
| 90 |
|
| 91 |
(Almost) Finally, there is a rule that allows you to change the units into |
| 92 |
something other than seconds. To do this, put a "|" character on |
| 93 |
the far right, followed by a number specifying the scaling factor. |
| 94 |
As an exception to previous rules, decimal points are allowed |
| 95 |
in the final scaling factor - the period is not interpreted as it |
| 96 |
would be before the "|" character. (This is fine, because all |
| 97 |
previous fields must be integers to make sense.) Anyway, if you |
| 98 |
include a scaling factor after a "|", the number will be |
| 99 |
multiplied by this factor before it is formatted. For example, to |
| 100 |
express the current time in NTSC frames (~29.97 fps), you could |
| 101 |
use the following formatting: |
| 102 |
|
| 103 |
3758.5 seconds, "*.01000 frames|29.97002997" -> "112642.358 frames" |
| 104 |
|
| 105 |
Finally there is a further special character that can be used after a "|" |
| 106 |
and that is "N". This applies special rule for NTSC drop-frame timecode. |
| 107 |
|
| 108 |
Summary of format string rules: |
| 109 |
|
| 110 |
- The characters '0-9', '*', and '#' are numeric. Any sequence of |
| 111 |
these characters is treated as defining a NEW field by specifying |
| 112 |
its range. All other characters become delimiters between fields. |
| 113 |
(The one exception is that '.' is treated as numeric after the |
| 114 |
optional '|'.) |
| 115 |
- A field with a range of '*', which only makes sense as the |
| 116 |
leftmost field, means the field should display as large a number |
| 117 |
as necessary. (Note: this no longer makes sense here and applies to a |
| 118 |
previous version). |
| 119 |
- The character '#' represents the current sample rate. |
| 120 |
- If a field specifier beings with a leading zero, it will be formatted |
| 121 |
with leading zeros, too - enough to display the maximum value |
| 122 |
that field can display. So the number 7 in a field specified |
| 123 |
as '01000' would be formatted as '007'. Bond. James Bond. |
| 124 |
- Any non-numeric characters before the first field are treated |
| 125 |
as a prefix, and will be displayed to the left of the first field. |
| 126 |
- A delimiter ending in '.' is treated specially. All fields after |
| 127 |
this delimiter are fractional fields, after the decimal point. |
| 128 |
- The '|' character is treated as a special delimiter. The number |
| 129 |
to the right of this character (which is allowed to contain a |
| 130 |
decimal point) is treated as a scaling factor. The number is |
| 131 |
multiplied by this factor before converting. |
| 132 |
- The special character 'N' after '|' is only used for NTSC drop-frame. |
| 133 |
|
| 134 |
*******************************************************************/ |
| 135 |
NumericFormatter::NumericFormatter(const QString& formatStr) |
| 136 |
: TimecodeFormatter(formatStr) |
| 137 |
{ |
| 138 |
} |
| 139 |
|
| 140 |
void NumericFormatter::init() |
| 141 |
{ |
| 142 |
m_prefix.clear(); |
| 143 |
m_fields.clear(); |
| 144 |
m_digits.clear(); |
| 145 |
m_fieldConfigs.clear(); |
| 146 |
|
| 147 |
m_scalingFactor = 1.0; |
| 148 |
|
| 149 |
// We will change inFrac to true when we hit our first decimal point. |
| 150 |
bool inFrac = false; |
| 151 |
int fracMult = 1; |
| 152 |
int numWholeFields = 0; |
| 153 |
int numFracFields = 0; |
| 154 |
QString numStr; |
| 155 |
QString delimStr; |
| 156 |
unsigned int i; |
| 157 |
|
| 158 |
m_ntscDrop = false; |
| 159 |
for (i = 0; i < m_format.length(); i++) { |
| 160 |
bool handleDelim = false; |
| 161 |
bool handleNum = false; |
| 162 |
|
| 163 |
if (m_format[i] == '|') { |
| 164 |
QString remainder = m_format.right(m_format.length() - i - 1); |
| 165 |
// For languages which use , as a separator. |
| 166 |
remainder.replace(',', '.'); |
| 167 |
|
| 168 |
m_scalingFactorIsSamples = remainder == "#"; |
| 169 |
|
| 170 |
if (m_scalingFactorIsSamples) { |
| 171 |
m_scalingFactor = m_sampleRate; |
| 172 |
} else if (remainder == "N") { |
| 173 |
m_ntscDrop = true; |
| 174 |
} else { |
| 175 |
// Use the C locale here for string to number. |
| 176 |
// Translations are often incomplete. |
| 177 |
// We can't rely on the correct ',' or '.' in the |
| 178 |
// translation, so we work based on '.' for decimal point. |
| 179 |
m_scalingFactor = remainder.toDouble(); |
| 180 |
} |
| 181 |
i = m_format.length() - 1; // force break out of loop |
| 182 |
if (!delimStr.isEmpty()) { |
| 183 |
handleDelim = true; |
| 184 |
} |
| 185 |
if (!numStr.isEmpty()) { |
| 186 |
handleNum = true; |
| 187 |
} |
| 188 |
} else if ( |
| 189 |
(m_format[i] >= '0' && m_format[i] <= '9') |
| 190 |
|| m_format[i] == '*' || m_format[i] == '#') { |
| 191 |
numStr += m_format[i]; |
| 192 |
if (!delimStr.isEmpty()) { |
| 193 |
handleDelim = true; |
| 194 |
} |
| 195 |
} else { |
| 196 |
delimStr += m_format[i]; |
| 197 |
if (!numStr.isEmpty()) { |
| 198 |
handleNum = true; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (i == m_format.length() - 1) { |
| 203 |
if (!numStr.isEmpty()) { |
| 204 |
handleNum = true; |
| 205 |
} |
| 206 |
if (!delimStr.isEmpty()) { |
| 207 |
handleDelim = true; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if (handleNum) { |
| 212 |
bool zeropad = false; |
| 213 |
long range = 0; |
| 214 |
|
| 215 |
if (numStr.right(1) == "#") { |
| 216 |
range = static_cast<long int>(m_sampleRate); |
| 217 |
} else if (numStr.right(1) != "*") { |
| 218 |
range = numStr.toLong(); |
| 219 |
} |
| 220 |
if (numStr[0] == '0' && numStr.length() > 1) { |
| 221 |
zeropad = true; |
| 222 |
} |
| 223 |
|
| 224 |
// Hack: always zeropad |
| 225 |
zeropad = true; |
| 226 |
|
| 227 |
if (inFrac) { |
| 228 |
int base = fracMult * range; |
| 229 |
m_fieldConfigs.push_back({ inFrac, base, range }); |
| 230 |
m_fields.push_back(NumericField::range(range, zeropad)); |
| 231 |
fracMult *= range; |
| 232 |
numFracFields++; |
| 233 |
} else { |
| 234 |
unsigned int j; |
| 235 |
for (j = 0; j < m_fields.size(); j++) { |
| 236 |
m_fieldConfigs[j].base *= range; |
| 237 |
} |
| 238 |
m_fieldConfigs.push_back({ inFrac, 1, range }); |
| 239 |
m_fields.push_back(NumericField::range(range, zeropad)); |
| 240 |
numWholeFields++; |
| 241 |
} |
| 242 |
numStr = ""; |
| 243 |
} |
| 244 |
|
| 245 |
if (handleDelim) { |
| 246 |
bool goToFrac = false; |
| 247 |
|
| 248 |
if (!inFrac) { |
| 249 |
QChar delim = delimStr[delimStr.length() - 1]; |
| 250 |
if (delim == '<' || delim == '>') { |
| 251 |
goToFrac = true; |
| 252 |
if (delimStr.length() > 1) { |
| 253 |
delimStr = delimStr.left(delimStr.lastIndexOf(delim)); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if (inFrac) { |
| 259 |
if (numFracFields == 0) { |
| 260 |
// Should never happen |
| 261 |
return; |
| 262 |
} |
| 263 |
if (handleNum && numFracFields > 1) { |
| 264 |
m_fields[m_fields.size() - 2].label = delimStr; |
| 265 |
} else { |
| 266 |
m_fields[m_fields.size() - 1].label = delimStr; |
| 267 |
} |
| 268 |
} else { |
| 269 |
if (numWholeFields == 0) { |
| 270 |
m_prefix = delimStr; |
| 271 |
} else { |
| 272 |
delimStr.replace('<', ','); |
| 273 |
delimStr.replace('>', '.'); |
| 274 |
m_fields[numWholeFields - 1].label = delimStr; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (goToFrac) { |
| 279 |
inFrac = true; |
| 280 |
} |
| 281 |
delimStr = ""; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
size_t pos = 0; |
| 286 |
|
| 287 |
pos += m_prefix.length(); |
| 288 |
|
| 289 |
for (i = 0; i < m_fields.size(); i++) { |
| 290 |
m_fields[i].pos = pos; |
| 291 |
|
| 292 |
for (size_t j = 0; j < m_fields[i].digits; j++) { |
| 293 |
m_digits.push_back(DigitInfo { i, j, pos }); |
| 294 |
pos++; |
| 295 |
} |
| 296 |
|
| 297 |
pos += m_fields[i].label.length(); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
NumericFormatter::ConversionResult NumericFormatter::valueToString(double value, bool nearest) const |
| 302 |
{ |
| 303 |
ConversionResult result; |
| 304 |
double rawValue = floor(value * m_sampleRate + (nearest ? 0.5f : 0.0f)) |
| 305 |
/ m_sampleRate; // put on a sample |
| 306 |
double theValue = rawValue * m_scalingFactor |
| 307 |
// PRL: what WAS this .000001 for? Nobody could explain. |
| 308 |
// + .000001 |
| 309 |
; |
| 310 |
|
| 311 |
int t_int; // todo |
| 312 |
bool round = true; |
| 313 |
// We round on the last field. If we have a fractional field we round |
| 314 |
// using it. Otherwise we round to nearest integer. |
| 315 |
for (size_t i = 0; i < m_fields.size(); i++) { |
| 316 |
if (m_fieldConfigs[i].frac) { |
| 317 |
round = false; |
| 318 |
} |
| 319 |
} |
| 320 |
if (theValue < 0) { |
| 321 |
t_int = -1; |
| 322 |
} else if (round) { |
| 323 |
t_int = int(theValue + (nearest ? 0.5f : 0.0f)); |
| 324 |
} else { |
| 325 |
theValue += (nearest ? 0.5f : 0.0f) / m_fieldConfigs.back().base; |
| 326 |
t_int = int(theValue); |
| 327 |
} |
| 328 |
double t_frac; |
| 329 |
if (theValue < 0) { |
| 330 |
t_frac = -1; |
| 331 |
} else { |
| 332 |
t_frac = (theValue - double(t_int)); |
| 333 |
} |
| 334 |
|
| 335 |
int tenMins; |
| 336 |
int mins; |
| 337 |
int addMins; |
| 338 |
int secs; |
| 339 |
int frames; |
| 340 |
|
| 341 |
result.valueString = m_prefix; |
| 342 |
|
| 343 |
if (m_ntscDrop && theValue >= 0) { |
| 344 |
frames = (int)(theValue * 30. / 1.001 + (nearest ? 0.5f : 0.0f)); |
| 345 |
tenMins = frames / 17982; |
| 346 |
frames -= tenMins * 17982; |
| 347 |
mins = tenMins * 10; |
| 348 |
if (frames >= 1800) { |
| 349 |
frames -= 1800; |
| 350 |
mins++; |
| 351 |
addMins = frames / 1798; |
| 352 |
frames -= addMins * 1798; |
| 353 |
mins += addMins; |
| 354 |
secs = frames / 30; |
| 355 |
frames -= secs * 30; |
| 356 |
frames += 2; |
| 357 |
if (frames >= 30) { |
| 358 |
secs++; |
| 359 |
frames -= 30; |
| 360 |
} |
| 361 |
} else { |
| 362 |
secs = frames / 30; |
| 363 |
frames -= secs * 30; |
| 364 |
} |
| 365 |
t_int = mins * 60 + secs; |
| 366 |
t_frac = frames / 30.; |
| 367 |
} |
| 368 |
|
| 369 |
for (size_t i = 0; i < m_fields.size(); i++) { |
| 370 |
long long value = -1; |
| 371 |
|
| 372 |
if (m_fieldConfigs[i].frac) { |
| 373 |
// JKC: This old code looks bogus to me. |
| 374 |
// The rounding is not propagating to earlier fields in the frac |
| 375 |
// case. |
| 376 |
// value = (int)(t_frac * mFields[i].base + 0.5); // +0.5 as |
| 377 |
// rounding required |
| 378 |
// I did the rounding earlier. |
| 379 |
if (t_frac >= 0) { |
| 380 |
value = t_frac * m_fieldConfigs[i].base; |
| 381 |
} |
| 382 |
// JKC: TODO: Find out what the range is supposed to do. |
| 383 |
// It looks bogus too. |
| 384 |
// if (mFields[i].range > 0) |
| 385 |
// value = value % mFields[i].range; |
| 386 |
} else { |
| 387 |
if (t_int >= 0) { |
| 388 |
value = long(t_int) / m_fieldConfigs[i].base; |
| 389 |
if (m_fieldConfigs[i].range > 0) { |
| 390 |
value = value % m_fieldConfigs[i].range; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
char field[10]; |
| 396 |
|
| 397 |
if (value < 0) { |
| 398 |
for (int ii = 0; ii < m_fields[i].digits; ++ii) { |
| 399 |
field[0] = '-'; |
| 400 |
} |
| 401 |
} else { |
| 402 |
snprintf(field, sizeof(field), m_fields[i].formatStr.toStdString().c_str(), (int)value); |
| 403 |
} |
| 404 |
|
| 405 |
result.fieldValueStrings.push_back(field); |
| 406 |
|
| 407 |
result.valueString += field; |
| 408 |
result.valueString += m_fields[i].label; |
| 409 |
} |
| 410 |
|
| 411 |
return result; |
| 412 |
} |
| 413 |
|
| 414 |
std::optional<double> NumericFormatter::stringToValue(const QString& value) const |
| 415 |
{ |
| 416 |
unsigned int i; |
| 417 |
double t = 0.0; |
| 418 |
|
| 419 |
if (m_fields.size() > 0 && value.mid(m_fields[0].pos, 1) == '-') { |
| 420 |
return std::nullopt; |
| 421 |
} |
| 422 |
|
| 423 |
for (i = 0; i < m_fields.size(); i++) { |
| 424 |
const auto pos = m_fields[i].pos; |
| 425 |
const auto digits = m_fields[i].digits; |
| 426 |
|
| 427 |
if (pos >= value.size() || pos + digits > value.size()) { |
| 428 |
return std::nullopt; |
| 429 |
} |
| 430 |
|
| 431 |
long val; |
| 432 |
|
| 433 |
const auto fieldStringValue |
| 434 |
=value.mid(m_fields[i].pos, m_fields[i].digits); |
| 435 |
|
| 436 |
bool ok = false; |
| 437 |
val = fieldStringValue.toLong(&ok); |
| 438 |
if (!ok) { |
| 439 |
return std::nullopt; |
| 440 |
} |
| 441 |
|
| 442 |
if (m_fieldConfigs[i].frac) { |
| 443 |
t += (val / (double)m_fieldConfigs[i].base); |
| 444 |
} else { |
| 445 |
t += (val * (double)m_fieldConfigs[i].base); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
t /= m_scalingFactor; |
| 450 |
|
| 451 |
if (m_ntscDrop) { |
| 452 |
int t_int = (int)(t + .000000001); |
| 453 |
double t_frac = (t - t_int); |
| 454 |
int tenMins = t_int / 600; |
| 455 |
double frames = tenMins * 17982; |
| 456 |
t_int -= tenMins * 600; |
| 457 |
int mins = t_int / 60; |
| 458 |
int addMins = 0; |
| 459 |
if (mins > 0) { |
| 460 |
frames += 1800; |
| 461 |
addMins = mins - 1; |
| 462 |
} |
| 463 |
frames += addMins * 1798; |
| 464 |
t_int -= mins * 60; |
| 465 |
if (mins == 0) { // first min of a block of 10, don't drop frames 0 and 1 |
| 466 |
frames += t_int * 30 + t_frac * 30.; |
| 467 |
} else { // drop frames 0 and 1 of first seconds of these minutes |
| 468 |
if (t_int > 0) { |
| 469 |
frames += 28 + (t_int - 1) * 30 + t_frac * 30.; |
| 470 |
} else { |
| 471 |
frames += t_frac * 30. - 2.; |
| 472 |
} |
| 473 |
} |
| 474 |
t = frames * 1.001 / 30.; |
| 475 |
} |
| 476 |
|
| 477 |
return t; |
| 478 |
} |
| 479 |
|
| 480 |
double NumericFormatter::singleStep(double value, int digitIndex, bool upwards) |
| 481 |
{ |
| 482 |
const auto dir = upwards ? 1 : -1; |
| 483 |
for (size_t i = 0; i < m_fields.size(); i++) { |
| 484 |
if ( |
| 485 |
(m_digits[digitIndex].pos >= m_fields[i].pos) |
| 486 |
&& (m_digits[digitIndex].pos < m_fields[i].pos + m_fields[i].digits)) { // it's this field |
| 487 |
if (value < 0) { |
| 488 |
value = 0; |
| 489 |
} |
| 490 |
|
| 491 |
value *= m_scalingFactor; |
| 492 |
|
| 493 |
const double mult = pow( |
| 494 |
10., m_fields[i].digits |
| 495 |
- (m_digits[digitIndex].pos - m_fields[i].pos) - 1); |
| 496 |
|
| 497 |
if (m_fieldConfigs[i].frac) { |
| 498 |
value += ((mult / (double)m_fieldConfigs[i].base) * dir); |
| 499 |
} else { |
| 500 |
value += ((mult * (double)m_fieldConfigs[i].base) * dir); |
| 501 |
} |
| 502 |
|
| 503 |
if (m_ntscDrop) { |
| 504 |
if ((value - (int)value) * 30 < 2) { |
| 505 |
if ((((int)value) % 60 == 0) && (((int)value) % 600 != 0)) { |
| 506 |
value = (int)value + (dir > 0 ? 2. : -1.) / 30.; |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
if (value < 0.) { |
| 512 |
value = 0.; |
| 513 |
} |
| 514 |
|
| 515 |
value /= m_scalingFactor; |
| 516 |
|
| 517 |
if (m_ntscDrop) { |
| 518 |
m_ntscDrop = false; |
| 519 |
auto result = valueToString(value, false); |
| 520 |
m_ntscDrop = true; |
| 521 |
return *stringToValue(result.valueString); |
| 522 |
} |
| 523 |
|
| 524 |
return value; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
return value; |
| 529 |
} |