annotate base/RealTimeSV.cpp @ 1562:830972646ccd

Handle security errors, format slightly differently for message box
author Chris Cannam
date Fri, 02 Nov 2018 14:40:44 +0000
parents 6415ac3becb6
children c36ffc195988
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7
Chris@52 8 This program is free software; you can redistribute it and/or
Chris@52 9 modify it under the terms of the GNU General Public License as
Chris@52 10 published by the Free Software Foundation; either version 2 of the
Chris@52 11 License, or (at your option) any later version. See the file
Chris@52 12 COPYING included with this distribution for more information.
Chris@0 13 */
Chris@0 14
Chris@0 15 /*
Chris@0 16 This is a modified version of a source file from the
Chris@0 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@17 18 This file copyright 2000-2006 Chris Cannam.
Chris@0 19 */
Chris@0 20
Chris@0 21 #include <iostream>
Chris@1427 22 #include <limits.h>
Chris@0 23
Chris@405 24 #include <cstdlib>
Chris@0 25 #include <sstream>
Chris@0 26
Chris@150 27 #include "RealTime.h"
Chris@0 28
Chris@843 29 #include "Debug.h"
Chris@843 30
Chris@612 31 #include "Preferences.h"
Chris@612 32
Chris@0 33 // A RealTime consists of two ints that must be at least 32 bits each.
Chris@0 34 // A signed 32-bit int can store values exceeding +/- 2 billion. This
Chris@0 35 // means we can safely use our lower int for nanoseconds, as there are
Chris@0 36 // 1 billion nanoseconds in a second and we need to handle double that
Chris@0 37 // because of the implementations of addition etc that we use.
Chris@0 38 //
Chris@0 39 // The maximum valid RealTime on a 32-bit system is somewhere around
Chris@0 40 // 68 years: 999999999 nanoseconds longer than the classic Unix epoch.
Chris@0 41
Chris@0 42 #define ONE_BILLION 1000000000
Chris@0 43
Chris@0 44 RealTime::RealTime(int s, int n) :
Chris@0 45 sec(s), nsec(n)
Chris@0 46 {
Chris@1427 47 while (nsec <= -ONE_BILLION && sec > INT_MIN) { nsec += ONE_BILLION; --sec; }
Chris@1427 48 while (nsec >= ONE_BILLION && sec < INT_MAX) { nsec -= ONE_BILLION; ++sec; }
Chris@1427 49 while (nsec > 0 && sec < 0) { nsec -= ONE_BILLION; ++sec; }
Chris@1427 50 while (nsec < 0 && sec > 0) { nsec += ONE_BILLION; --sec; }
Chris@0 51 }
Chris@0 52
Chris@26 53 RealTime
Chris@26 54 RealTime::fromSeconds(double sec)
Chris@26 55 {
Chris@1012 56 if (sec >= 0) {
Chris@1012 57 return RealTime(int(sec), int((sec - int(sec)) * ONE_BILLION + 0.5));
Chris@1012 58 } else {
Chris@1012 59 return -fromSeconds(-sec);
Chris@1012 60 }
Chris@26 61 }
Chris@26 62
Chris@26 63 RealTime
Chris@1542 64 RealTime::fromMilliseconds(int64_t msec)
Chris@26 65 {
Chris@1542 66 int64_t sec = msec / 1000;
Chris@1542 67 if (sec > INT_MAX || sec < INT_MIN) {
Chris@1542 68 cerr << "WARNING: millisecond value out of range for RealTime, "
Chris@1542 69 << "returning zero instead: " << msec << endl;
Chris@1542 70 return RealTime::zeroTime;
Chris@1542 71 }
Chris@1542 72
Chris@1542 73 return RealTime(int(sec), int(msec % 1000) * 1000000);
Chris@26 74 }
Chris@26 75
Chris@26 76 RealTime
Chris@1542 77 RealTime::fromMicroseconds(int64_t usec)
Chris@1541 78 {
Chris@1542 79 int64_t sec = usec / 1000000;
Chris@1542 80 if (sec > INT_MAX || sec < INT_MIN) {
Chris@1542 81 cerr << "WARNING: microsecond value out of range for RealTime, "
Chris@1542 82 << "returning zero instead: " << usec << endl;
Chris@1542 83 return RealTime::zeroTime;
Chris@1542 84 }
Chris@1542 85
Chris@1542 86 return RealTime(int(sec), int(usec % 1000000) * 1000);
Chris@1541 87 }
Chris@1541 88
Chris@1541 89 RealTime
Chris@26 90 RealTime::fromTimeval(const struct timeval &tv)
Chris@26 91 {
Chris@1038 92 return RealTime(int(tv.tv_sec), int(tv.tv_usec * 1000));
Chris@26 93 }
Chris@0 94
Chris@439 95 RealTime
Chris@439 96 RealTime::fromXsdDuration(std::string xsdd)
Chris@439 97 {
Chris@439 98 RealTime t;
Chris@439 99
Chris@439 100 int year = 0, month = 0, day = 0, hour = 0, minute = 0;
Chris@439 101 double second = 0.0;
Chris@439 102
Chris@1298 103 char *loc = setlocale(LC_NUMERIC, 0);
Chris@1298 104 (void)setlocale(LC_NUMERIC, "C"); // avoid strtod expecting ,-separator in DE
Chris@1298 105
Chris@439 106 int i = 0;
Chris@439 107
Chris@439 108 const char *s = xsdd.c_str();
Chris@1038 109 int len = int(xsdd.length());
Chris@439 110
Chris@439 111 bool negative = false, afterT = false;
Chris@439 112
Chris@439 113 while (i < len) {
Chris@439 114
Chris@439 115 if (s[i] == '-') {
Chris@439 116 if (i == 0) negative = true;
Chris@439 117 ++i;
Chris@439 118 continue;
Chris@439 119 }
Chris@439 120
Chris@439 121 double value = 0.0;
Chris@439 122 char *eptr = 0;
Chris@439 123
Chris@439 124 if (isdigit(s[i]) || s[i] == '.') {
Chris@439 125 value = strtod(&s[i], &eptr);
Chris@1038 126 i = int(eptr - s);
Chris@439 127 }
Chris@439 128
Chris@439 129 if (i == len) break;
Chris@439 130
Chris@439 131 switch (s[i]) {
Chris@439 132 case 'Y': year = int(value + 0.1); break;
Chris@439 133 case 'D': day = int(value + 0.1); break;
Chris@439 134 case 'H': hour = int(value + 0.1); break;
Chris@439 135 case 'M':
Chris@439 136 if (afterT) minute = int(value + 0.1);
Chris@439 137 else month = int(value + 0.1);
Chris@439 138 break;
Chris@439 139 case 'S':
Chris@439 140 second = value;
Chris@439 141 break;
Chris@439 142 case 'T': afterT = true; break;
Chris@439 143 };
Chris@439 144
Chris@439 145 ++i;
Chris@439 146 }
Chris@439 147
Chris@439 148 if (year > 0) {
Chris@843 149 cerr << "WARNING: This xsd:duration (\"" << xsdd << "\") contains a non-zero year.\nWith no origin and a limited data size, I will treat a year as exactly 31556952\nseconds and you should expect overflow and/or poor results." << endl;
Chris@439 150 t = t + RealTime(year * 31556952, 0);
Chris@439 151 }
Chris@439 152
Chris@439 153 if (month > 0) {
Chris@843 154 cerr << "WARNING: This xsd:duration (\"" << xsdd << "\") contains a non-zero month.\nWith no origin and a limited data size, I will treat a month as exactly 2629746\nseconds and you should expect overflow and/or poor results." << endl;
Chris@439 155 t = t + RealTime(month * 2629746, 0);
Chris@439 156 }
Chris@439 157
Chris@439 158 if (day > 0) {
Chris@439 159 t = t + RealTime(day * 86400, 0);
Chris@439 160 }
Chris@439 161
Chris@439 162 if (hour > 0) {
Chris@439 163 t = t + RealTime(hour * 3600, 0);
Chris@439 164 }
Chris@439 165
Chris@439 166 if (minute > 0) {
Chris@439 167 t = t + RealTime(minute * 60, 0);
Chris@439 168 }
Chris@439 169
Chris@439 170 t = t + fromSeconds(second);
Chris@439 171
Chris@1298 172 setlocale(LC_NUMERIC, loc);
Chris@1298 173
Chris@928 174 if (negative) {
Chris@928 175 return -t;
Chris@928 176 } else {
Chris@928 177 return t;
Chris@928 178 }
Chris@439 179 }
Chris@439 180
Chris@439 181 double
Chris@439 182 RealTime::toDouble() const
Chris@439 183 {
Chris@439 184 double d = sec;
Chris@439 185 d += double(nsec) / double(ONE_BILLION);
Chris@439 186 return d;
Chris@439 187 }
Chris@439 188
Chris@0 189 std::ostream &operator<<(std::ostream &out, const RealTime &rt)
Chris@0 190 {
Chris@0 191 if (rt < RealTime::zeroTime) {
Chris@1429 192 out << "-";
Chris@0 193 } else {
Chris@1429 194 out << " ";
Chris@0 195 }
Chris@0 196
Chris@0 197 int s = (rt.sec < 0 ? -rt.sec : rt.sec);
Chris@0 198 int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec);
Chris@0 199
Chris@0 200 out << s << ".";
Chris@0 201
Chris@0 202 int nn(n);
Chris@0 203 if (nn == 0) out << "00000000";
Chris@0 204 else while (nn < (ONE_BILLION / 10)) {
Chris@1429 205 out << "0";
Chris@1429 206 nn *= 10;
Chris@0 207 }
Chris@0 208
Chris@0 209 out << n << "R";
Chris@0 210 return out;
Chris@0 211 }
Chris@0 212
Chris@0 213 std::string
Chris@121 214 RealTime::toString(bool align) const
Chris@0 215 {
Chris@0 216 std::stringstream out;
Chris@0 217 out << *this;
Chris@0 218
Chris@0 219 std::string s = out.str();
Chris@0 220
Chris@121 221 if (!align && *this >= RealTime::zeroTime) {
Chris@121 222 // remove leading " "
Chris@121 223 s = s.substr(1, s.length() - 1);
Chris@121 224 }
Chris@121 225
Chris@0 226 // remove trailing R
Chris@0 227 return s.substr(0, s.length() - 1);
Chris@0 228 }
Chris@0 229
Chris@350 230 RealTime
Chris@350 231 RealTime::fromString(std::string s)
Chris@350 232 {
Chris@350 233 bool negative = false;
Chris@957 234 int section = 0;
Chris@350 235 std::string ssec, snsec;
Chris@350 236
Chris@350 237 for (size_t i = 0; i < s.length(); ++i) {
Chris@350 238
Chris@350 239 char c = s[i];
Chris@350 240 if (isspace(c)) continue;
Chris@350 241
Chris@350 242 if (section == 0) {
Chris@350 243
Chris@350 244 if (c == '-') negative = true;
Chris@350 245 else if (isdigit(c)) { section = 1; ssec += c; }
Chris@350 246 else if (c == '.') section = 2;
Chris@350 247 else break;
Chris@350 248
Chris@350 249 } else if (section == 1) {
Chris@350 250
Chris@350 251 if (c == '.') section = 2;
Chris@350 252 else if (isdigit(c)) ssec += c;
Chris@350 253 else break;
Chris@350 254
Chris@350 255 } else if (section == 2) {
Chris@350 256
Chris@350 257 if (isdigit(c)) snsec += c;
Chris@350 258 else break;
Chris@350 259 }
Chris@350 260 }
Chris@350 261
Chris@350 262 while (snsec.length() < 8) snsec += '0';
Chris@350 263
Chris@350 264 int sec = atoi(ssec.c_str());
Chris@350 265 int nsec = atoi(snsec.c_str());
Chris@350 266 if (negative) sec = -sec;
Chris@350 267
Chris@690 268 // SVDEBUG << "RealTime::fromString: string " << s << " -> "
Chris@687 269 // << sec << " sec, " << nsec << " nsec" << endl;
Chris@350 270
Chris@350 271 return RealTime(sec, nsec);
Chris@350 272 }
Chris@350 273
Chris@0 274 std::string
Chris@0 275 RealTime::toText(bool fixedDp) const
Chris@0 276 {
Chris@247 277 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp);
Chris@0 278
Chris@612 279 Preferences *p = Preferences::getInstance();
Chris@1070 280 bool hms = true;
Chris@1542 281 std::string frameDelimiter = ":";
Chris@1070 282
Chris@612 283 if (p) {
Chris@1070 284 hms = p->getShowHMS();
Chris@612 285 int fps = 0;
Chris@612 286 switch (p->getTimeToTextMode()) {
Chris@1542 287 case Preferences::TimeToTextMs:
Chris@1542 288 break;
Chris@1542 289 case Preferences::TimeToTextUs:
Chris@1542 290 fps = 1000000;
Chris@1542 291 frameDelimiter = ".";
Chris@1542 292 break;
Chris@612 293 case Preferences::TimeToText24Frame: fps = 24; break;
Chris@612 294 case Preferences::TimeToText25Frame: fps = 25; break;
Chris@612 295 case Preferences::TimeToText30Frame: fps = 30; break;
Chris@612 296 case Preferences::TimeToText50Frame: fps = 50; break;
Chris@612 297 case Preferences::TimeToText60Frame: fps = 60; break;
Chris@612 298 }
Chris@1542 299 if (fps != 0) {
Chris@1542 300 return toFrameText(fps, hms, frameDelimiter);
Chris@1542 301 }
Chris@612 302 }
Chris@612 303
Chris@1070 304 return toMSText(fixedDp, hms);
Chris@1070 305 }
Chris@0 306
Chris@1070 307 static void
Chris@1070 308 writeSecPart(std::stringstream &out, bool hms, int sec)
Chris@1070 309 {
Chris@1070 310 if (hms) {
Chris@1031 311 if (sec >= 3600) {
Chris@1031 312 out << (sec / 3600) << ":";
Chris@1031 313 }
Chris@1031 314
Chris@1031 315 if (sec >= 60) {
Chris@1070 316 int minutes = (sec % 3600) / 60;
Chris@1070 317 if (sec >= 3600 && minutes < 10) out << "0";
Chris@1070 318 out << minutes << ":";
Chris@1031 319 }
Chris@1031 320
Chris@1031 321 if (sec >= 10) {
Chris@1031 322 out << ((sec % 60) / 10);
Chris@1031 323 }
Chris@1031 324
Chris@1031 325 out << (sec % 10);
Chris@1031 326
Chris@1031 327 } else {
Chris@1031 328 out << sec;
Chris@0 329 }
Chris@1070 330 }
Chris@1070 331
Chris@1070 332 std::string
Chris@1070 333 RealTime::toMSText(bool fixedDp, bool hms) const
Chris@1070 334 {
Chris@1070 335 if (*this < RealTime::zeroTime) return "-" + (-*this).toMSText(fixedDp, hms);
Chris@1070 336
Chris@1070 337 std::stringstream out;
Chris@1070 338
Chris@1070 339 writeSecPart(out, hms, sec);
Chris@0 340
Chris@0 341 int ms = msec();
Chris@0 342
Chris@0 343 if (ms != 0) {
Chris@1429 344 out << ".";
Chris@1429 345 out << (ms / 100);
Chris@1429 346 ms = ms % 100;
Chris@1429 347 if (ms != 0) {
Chris@1429 348 out << (ms / 10);
Chris@1429 349 ms = ms % 10;
Chris@1429 350 } else if (fixedDp) {
Chris@1429 351 out << "0";
Chris@1429 352 }
Chris@1429 353 if (ms != 0) {
Chris@1429 354 out << ms;
Chris@1429 355 } else if (fixedDp) {
Chris@1429 356 out << "0";
Chris@1429 357 }
Chris@0 358 } else if (fixedDp) {
Chris@1429 359 out << ".000";
Chris@0 360 }
Chris@1429 361
Chris@612 362 std::string s = out.str();
Chris@0 363
Chris@612 364 return s;
Chris@612 365 }
Chris@612 366
Chris@612 367 std::string
Chris@1542 368 RealTime::toFrameText(int fps, bool hms, std::string frameDelimiter) const
Chris@612 369 {
Chris@1542 370 if (*this < RealTime::zeroTime) {
Chris@1542 371 return "-" + (-*this).toFrameText(fps, hms);
Chris@1542 372 }
Chris@1031 373
Chris@612 374 std::stringstream out;
Chris@612 375
Chris@1070 376 writeSecPart(out, hms, sec);
Chris@1031 377
Chris@1070 378 // avoid rounding error if fps does not divide into ONE_BILLION
Chris@1070 379 int64_t fbig = nsec;
Chris@1070 380 fbig *= fps;
Chris@1070 381 int f = int(fbig / ONE_BILLION);
Chris@612 382
Chris@612 383 int div = 1;
Chris@612 384 int n = fps - 1;
Chris@612 385 while ((n = n / 10)) {
Chris@612 386 div *= 10;
Chris@612 387 }
Chris@612 388
Chris@1542 389 out << frameDelimiter;
Chris@612 390
Chris@843 391 // cerr << "div = " << div << ", f = "<< f << endl;
Chris@612 392
Chris@612 393 while (div) {
Chris@612 394 int d = (f / div) % 10;
Chris@612 395 out << d;
Chris@612 396 div /= 10;
Chris@612 397 }
Chris@1429 398
Chris@0 399 std::string s = out.str();
Chris@0 400
Chris@843 401 // cerr << "converted " << toString() << " to " << s << endl;
Chris@612 402
Chris@0 403 return s;
Chris@0 404 }
Chris@0 405
Chris@247 406 std::string
Chris@247 407 RealTime::toSecText() const
Chris@247 408 {
Chris@247 409 if (*this < RealTime::zeroTime) return "-" + (-*this).toSecText();
Chris@247 410
Chris@247 411 std::stringstream out;
Chris@247 412
Chris@1070 413 writeSecPart(out, true, sec);
Chris@247 414
Chris@247 415 if (sec < 60) {
Chris@247 416 out << "s";
Chris@247 417 }
Chris@247 418
Chris@247 419 std::string s = out.str();
Chris@247 420
Chris@247 421 return s;
Chris@247 422 }
Chris@247 423
Chris@494 424 std::string
Chris@494 425 RealTime::toXsdDuration() const
Chris@494 426 {
Chris@494 427 std::string s = "PT" + toString(false) + "S";
Chris@494 428 return s;
Chris@494 429 }
Chris@494 430
Chris@183 431 RealTime
Chris@183 432 RealTime::operator*(int m) const
Chris@183 433 {
Chris@183 434 double t = (double(nsec) / ONE_BILLION) * m;
Chris@183 435 t += sec * m;
Chris@183 436 return fromSeconds(t);
Chris@183 437 }
Chris@0 438
Chris@0 439 RealTime
Chris@0 440 RealTime::operator/(int d) const
Chris@0 441 {
Chris@0 442 int secdiv = sec / d;
Chris@0 443 int secrem = sec % d;
Chris@0 444
Chris@0 445 double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d;
Chris@0 446
Chris@0 447 return RealTime(secdiv, int(nsecdiv + 0.5));
Chris@0 448 }
Chris@0 449
Chris@378 450 RealTime
Chris@378 451 RealTime::operator*(double m) const
Chris@378 452 {
Chris@378 453 double t = (double(nsec) / ONE_BILLION) * m;
Chris@378 454 t += sec * m;
Chris@378 455 return fromSeconds(t);
Chris@378 456 }
Chris@378 457
Chris@378 458 RealTime
Chris@378 459 RealTime::operator/(double d) const
Chris@378 460 {
Chris@378 461 double t = (double(nsec) / ONE_BILLION) / d;
Chris@378 462 t += sec / d;
Chris@378 463 return fromSeconds(t);
Chris@378 464 }
Chris@378 465
Chris@0 466 double
Chris@0 467 RealTime::operator/(const RealTime &r) const
Chris@0 468 {
Chris@0 469 double lTotal = double(sec) * ONE_BILLION + double(nsec);
Chris@0 470 double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec);
Chris@0 471
Chris@0 472 if (rTotal == 0) return 0.0;
Chris@0 473 else return lTotal/rTotal;
Chris@0 474 }
Chris@0 475
Chris@1040 476 static RealTime
Chris@1040 477 frame2RealTime_i(sv_frame_t frame, sv_frame_t iSampleRate)
Chris@1040 478 {
Chris@1040 479 if (frame < 0) return -frame2RealTime_i(-frame, iSampleRate);
Chris@1040 480
Chris@1262 481 int sec = int(frame / iSampleRate);
Chris@1040 482 frame -= sec * iSampleRate;
Chris@1262 483 int nsec = int((double(frame) / double(iSampleRate)) * ONE_BILLION + 0.5);
Chris@1262 484 // Use ctor here instead of setting data members directly to
Chris@1262 485 // ensure nsec > ONE_BILLION is handled properly. It's extremely
Chris@1262 486 // unlikely, but not impossible.
Chris@1262 487 return RealTime(sec, nsec);
Chris@1040 488 }
Chris@1040 489
Chris@1038 490 sv_frame_t
Chris@1040 491 RealTime::realTime2Frame(const RealTime &time, sv_samplerate_t sampleRate)
Chris@0 492 {
Chris@0 493 if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
Chris@1262 494 double s = time.sec + double(time.nsec) / 1000000000.0;
Chris@1262 495 return sv_frame_t(s * sampleRate + 0.5);
Chris@0 496 }
Chris@0 497
Chris@0 498 RealTime
Chris@1040 499 RealTime::frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate)
Chris@0 500 {
Chris@1040 501 if (sampleRate == double(int(sampleRate))) {
Chris@1040 502 return frame2RealTime_i(frame, int(sampleRate));
Chris@1040 503 }
Chris@0 504
Chris@1040 505 double sec = double(frame) / sampleRate;
Chris@1040 506 return fromSeconds(sec);
Chris@0 507 }
Chris@0 508
Chris@0 509 const RealTime RealTime::zeroTime(0,0);
Chris@0 510