changeset 137:dbab8c3a6571

* Improve resilience of frame - real-time - frame round-trip conversions
author cannam
date Mon, 16 Jun 2008 12:22:16 +0000
parents 1b1ebb0f10ac
children 147de5e64d28
files vamp-sdk/RealTime.cpp
diffstat 1 files changed, 3 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/vamp-sdk/RealTime.cpp	Wed Jun 11 09:14:27 2008 +0000
+++ b/vamp-sdk/RealTime.cpp	Mon Jun 16 12:22:16 2008 +0000
@@ -224,17 +224,8 @@
 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate)
 {
     if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
-
-    // We like integers.  The last term is always zero unless the
-    // sample rate is greater than 1MHz, but hell, you never know...
-
-    long frame =
-	time.sec * sampleRate +
-	(time.msec() * sampleRate) / 1000 +
-	((time.usec() - 1000 * time.msec()) * sampleRate) / 1000000 +
-	((time.nsec - 1000 * time.usec()) * sampleRate) / 1000000000;
-
-    return frame;
+    double s = time.sec + double(time.nsec + 1) / 1000000000.0;
+    return long(s * sampleRate);
 }
 
 RealTime
@@ -245,7 +236,7 @@
     RealTime rt;
     rt.sec = frame / long(sampleRate);
     frame -= rt.sec * long(sampleRate);
-    rt.nsec = (int)(((float(frame) * 1000000) / long(sampleRate)) * 1000);
+    rt.nsec = (int)(((double(frame) * 1000000.0) / sampleRate) * 1000.0);
     return rt;
 }