changeset 475:e2716b9352ca

Fixes to a few unusual cases in RealTime
author Chris Cannam
date Tue, 15 Nov 2016 16:53:46 +0000
parents 8e846c1aed96
children 15348e89c1d7
files src/vamp-sdk/RealTime.cpp
diffstat 1 files changed, 10 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/vamp-sdk/RealTime.cpp	Fri Nov 04 10:44:27 2016 +0000
+++ b/src/vamp-sdk/RealTime.cpp	Tue Nov 15 16:53:46 2016 +0000
@@ -155,7 +155,7 @@
 std::string
 RealTime::toText(bool fixedDp) const
 {
-    if (*this < RealTime::zeroTime) return "-" + (-*this).toText();
+    if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp);
 
     std::stringstream out;
 
@@ -226,8 +226,8 @@
 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate)
 {
     if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
-    double s = time.sec + double(time.nsec + 1) / 1000000000.0;
-    return long(s * sampleRate);
+    double s = time.sec + double(time.nsec) / ONE_BILLION;
+    return long(s * sampleRate + 0.5);
 }
 
 RealTime
@@ -235,11 +235,13 @@
 {
     if (frame < 0) return -frame2RealTime(-frame, sampleRate);
 
-    RealTime rt;
-    rt.sec = int(frame / long(sampleRate));
-    frame -= rt.sec * long(sampleRate);
-    rt.nsec = (int)(((double(frame) * 1000000.0) / sampleRate) * 1000.0);
-    return rt;
+    int sec = int(frame / long(sampleRate));
+    frame -= sec * long(sampleRate);
+    int nsec = (int)((double(frame) / double(sampleRate)) * ONE_BILLION + 0.5);
+    // Use ctor here instead of setting data members directly to
+    // ensure nsec > ONE_BILLION is handled properly.  It's extremely
+    // unlikely, but not impossible.
+    return RealTime(sec, nsec);
 }
 
 const RealTime RealTime::zeroTime(0,0);