Mercurial > hg > vamp-plugin-sdk
comparison src/vamp-sdk/RealTime.cpp @ 475:e2716b9352ca
Fixes to a few unusual cases in RealTime
author | Chris Cannam |
---|---|
date | Tue, 15 Nov 2016 16:53:46 +0000 |
parents | 35fa4733bc5d |
children | 15348e89c1d7 |
comparison
equal
deleted
inserted
replaced
474:8e846c1aed96 | 475:e2716b9352ca |
---|---|
153 } | 153 } |
154 | 154 |
155 std::string | 155 std::string |
156 RealTime::toText(bool fixedDp) const | 156 RealTime::toText(bool fixedDp) const |
157 { | 157 { |
158 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(); | 158 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp); |
159 | 159 |
160 std::stringstream out; | 160 std::stringstream out; |
161 | 161 |
162 if (sec >= 3600) { | 162 if (sec >= 3600) { |
163 out << (sec / 3600) << ":"; | 163 out << (sec / 3600) << ":"; |
224 | 224 |
225 long | 225 long |
226 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate) | 226 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate) |
227 { | 227 { |
228 if (time < zeroTime) return -realTime2Frame(-time, sampleRate); | 228 if (time < zeroTime) return -realTime2Frame(-time, sampleRate); |
229 double s = time.sec + double(time.nsec + 1) / 1000000000.0; | 229 double s = time.sec + double(time.nsec) / ONE_BILLION; |
230 return long(s * sampleRate); | 230 return long(s * sampleRate + 0.5); |
231 } | 231 } |
232 | 232 |
233 RealTime | 233 RealTime |
234 RealTime::frame2RealTime(long frame, unsigned int sampleRate) | 234 RealTime::frame2RealTime(long frame, unsigned int sampleRate) |
235 { | 235 { |
236 if (frame < 0) return -frame2RealTime(-frame, sampleRate); | 236 if (frame < 0) return -frame2RealTime(-frame, sampleRate); |
237 | 237 |
238 RealTime rt; | 238 int sec = int(frame / long(sampleRate)); |
239 rt.sec = int(frame / long(sampleRate)); | 239 frame -= sec * long(sampleRate); |
240 frame -= rt.sec * long(sampleRate); | 240 int nsec = (int)((double(frame) / double(sampleRate)) * ONE_BILLION + 0.5); |
241 rt.nsec = (int)(((double(frame) * 1000000.0) / sampleRate) * 1000.0); | 241 // Use ctor here instead of setting data members directly to |
242 return rt; | 242 // ensure nsec > ONE_BILLION is handled properly. It's extremely |
243 // unlikely, but not impossible. | |
244 return RealTime(sec, nsec); | |
243 } | 245 } |
244 | 246 |
245 const RealTime RealTime::zeroTime(0,0); | 247 const RealTime RealTime::zeroTime(0,0); |
246 | 248 |
247 } | 249 } |