Mercurial > hg > svcore
comparison base/RealTime.cpp @ 427:72ec275e458b
* Basic implementation of add and remove point in data editor
* Improve resilience of frame - real-time - frame round-trip conversions
author | Chris Cannam |
---|---|
date | Mon, 16 Jun 2008 14:48:42 +0000 |
parents | 65311fb86166 |
children | beb2948baa77 |
comparison
equal
deleted
inserted
replaced
426:2386582f67cd | 427:72ec275e458b |
---|---|
302 | 302 |
303 long | 303 long |
304 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate) | 304 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate) |
305 { | 305 { |
306 if (time < zeroTime) return -realTime2Frame(-time, sampleRate); | 306 if (time < zeroTime) return -realTime2Frame(-time, sampleRate); |
307 | 307 double s = time.sec + double(time.nsec + 1) / 1000000000.0; |
308 // We like integers. The last term is always zero unless the | 308 return long(s * sampleRate); |
309 // sample rate is greater than 1MHz, but hell, you never know... | |
310 | |
311 long frame = | |
312 time.sec * sampleRate + | |
313 (time.msec() * sampleRate) / 1000 + | |
314 ((time.usec() - 1000 * time.msec()) * sampleRate) / 1000000 + | |
315 ((time.nsec - 1000 * time.usec()) * sampleRate) / 1000000000; | |
316 | |
317 return frame; | |
318 } | 309 } |
319 | 310 |
320 RealTime | 311 RealTime |
321 RealTime::frame2RealTime(long frame, unsigned int sampleRate) | 312 RealTime::frame2RealTime(long frame, unsigned int sampleRate) |
322 { | 313 { |
323 if (frame < 0) return -frame2RealTime(-frame, sampleRate); | 314 if (frame < 0) return -frame2RealTime(-frame, sampleRate); |
324 | 315 |
325 RealTime rt; | 316 RealTime rt; |
326 rt.sec = frame / long(sampleRate); | 317 rt.sec = frame / long(sampleRate); |
327 frame -= rt.sec * long(sampleRate); | 318 frame -= rt.sec * long(sampleRate); |
328 rt.nsec = (int)(((float(frame) * 1000000) / long(sampleRate)) * 1000); | 319 rt.nsec = (int)(((double(frame) * 1000000.0) / long(sampleRate)) * 1000.0); |
329 return rt; | 320 return rt; |
330 } | 321 } |
331 | 322 |
332 const RealTime RealTime::zeroTime(0,0); | 323 const RealTime RealTime::zeroTime(0,0); |
333 | 324 |