2009-03-04 06:58:28 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2009-03-04 06:58:28 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2009-03-04 06:58:28 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/timestamp.h"
|
2010-05-17 20:53:04 +00:00
|
|
|
#include "common/algorithm.h"
|
2009-03-04 06:58:28 +00:00
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
Timestamp::Timestamp(uint ms, uint fr) {
|
2010-01-06 15:56:04 +00:00
|
|
|
assert(fr > 0);
|
2010-01-06 12:09:14 +00:00
|
|
|
|
|
|
|
_secs = ms / 1000;
|
2010-05-17 20:53:04 +00:00
|
|
|
_framerateFactor = 1000 / Common::gcd<uint>(1000, fr);
|
2010-01-06 15:56:04 +00:00
|
|
|
_framerate = fr * _framerateFactor;
|
2010-01-06 12:09:14 +00:00
|
|
|
|
2010-01-09 22:36:32 +00:00
|
|
|
// Note that _framerate is always divisible by 1000.
|
2010-01-11 10:17:55 +00:00
|
|
|
_numFrames = (ms % 1000) * (_framerate / 1000);
|
2010-01-04 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
Timestamp::Timestamp(uint s, uint frames, uint fr) {
|
2010-01-06 20:24:56 +00:00
|
|
|
assert(fr > 0);
|
|
|
|
|
|
|
|
_secs = s;
|
2010-05-17 20:53:04 +00:00
|
|
|
_framerateFactor = 1000 / Common::gcd<uint>(1000, fr);
|
2010-01-06 20:24:56 +00:00
|
|
|
_framerate = fr * _framerateFactor;
|
2010-01-11 10:17:55 +00:00
|
|
|
_numFrames = frames * _framerateFactor;
|
|
|
|
|
|
|
|
normalize();
|
2010-01-06 20:24:56 +00:00
|
|
|
}
|
2010-01-04 22:48:28 +00:00
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
Timestamp Timestamp::convertToFramerate(uint newFramerate) const {
|
2010-01-04 22:48:28 +00:00
|
|
|
Timestamp ts(*this);
|
|
|
|
|
2010-01-06 15:56:04 +00:00
|
|
|
if (ts.framerate() != newFramerate) {
|
2010-05-17 20:53:04 +00:00
|
|
|
ts._framerateFactor = 1000 / Common::gcd<uint>(1000, newFramerate);
|
2010-01-06 12:09:14 +00:00
|
|
|
ts._framerate = newFramerate * ts._framerateFactor;
|
2010-01-04 22:48:28 +00:00
|
|
|
|
2010-05-17 20:53:04 +00:00
|
|
|
const uint g = Common::gcd(_framerate, ts._framerate);
|
2010-01-04 22:48:28 +00:00
|
|
|
const uint p = _framerate / g;
|
|
|
|
const uint q = ts._framerate / g;
|
|
|
|
|
|
|
|
// Convert the frame offset to the new framerate.
|
|
|
|
// We round to the nearest (as opposed to always
|
|
|
|
// rounding down), to minimize rounding errors during
|
|
|
|
// round trip conversions.
|
2010-01-11 10:17:55 +00:00
|
|
|
ts._numFrames = (ts._numFrames * q + p/2) / p;
|
2010-01-04 22:48:28 +00:00
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
ts.normalize();
|
2010-01-04 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
void Timestamp::normalize() {
|
|
|
|
// Convert negative _numFrames values to positive ones by adjusting _secs
|
|
|
|
if (_numFrames < 0) {
|
|
|
|
int secsub = 1 + (-_numFrames / _framerate);
|
|
|
|
|
|
|
|
_numFrames += _framerate * secsub;
|
|
|
|
_secs -= secsub;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap around if necessary
|
|
|
|
_secs += (_numFrames / _framerate);
|
|
|
|
_numFrames %= _framerate;
|
|
|
|
}
|
|
|
|
|
2010-01-04 22:48:28 +00:00
|
|
|
bool Timestamp::operator==(const Timestamp &ts) const {
|
2010-01-06 12:15:05 +00:00
|
|
|
return cmp(ts) == 0;
|
2010-01-04 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Timestamp::operator!=(const Timestamp &ts) const {
|
2010-01-06 12:15:05 +00:00
|
|
|
return cmp(ts) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timestamp::operator<(const Timestamp &ts) const {
|
|
|
|
return cmp(ts) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timestamp::operator<=(const Timestamp &ts) const {
|
|
|
|
return cmp(ts) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timestamp::operator>(const Timestamp &ts) const {
|
|
|
|
return cmp(ts) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timestamp::operator>=(const Timestamp &ts) const {
|
|
|
|
return cmp(ts) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Timestamp::cmp(const Timestamp &ts) const {
|
|
|
|
int delta = _secs - ts._secs;
|
|
|
|
if (!delta) {
|
2010-05-17 20:53:04 +00:00
|
|
|
const uint g = Common::gcd(_framerate, ts._framerate);
|
2010-01-06 12:15:05 +00:00
|
|
|
const uint p = _framerate / g;
|
|
|
|
const uint q = ts._framerate / g;
|
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
delta = (_numFrames * q - ts._numFrames * p);
|
2010-01-06 12:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return delta;
|
2009-03-04 06:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp Timestamp::addFrames(int frames) const {
|
2010-01-04 22:48:28 +00:00
|
|
|
Timestamp ts(*this);
|
2010-01-06 12:09:14 +00:00
|
|
|
|
|
|
|
// The frames are given in the original framerate, so we have to
|
|
|
|
// adjust by _framerateFactor accordingly.
|
2010-01-11 10:17:55 +00:00
|
|
|
ts._numFrames += frames * _framerateFactor;
|
|
|
|
ts.normalize();
|
2009-03-04 06:58:28 +00:00
|
|
|
|
2010-01-09 22:36:32 +00:00
|
|
|
return ts;
|
|
|
|
}
|
2009-03-04 06:58:28 +00:00
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
Timestamp Timestamp::addMsecs(int ms) const {
|
|
|
|
Timestamp ts(*this);
|
|
|
|
ts._secs += ms / 1000;
|
|
|
|
// Add the remaining frames. Note that _framerate is always divisible by 1000.
|
|
|
|
ts._numFrames += (ms % 1000) * (ts._framerate / 1000);
|
|
|
|
|
|
|
|
ts.normalize();
|
|
|
|
|
|
|
|
return ts;
|
|
|
|
}
|
2009-03-04 06:58:28 +00:00
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
void Timestamp::addIntern(const Timestamp &ts) {
|
2011-02-09 00:12:22 +00:00
|
|
|
assert(_framerate == ts._framerate);
|
2010-01-11 10:17:55 +00:00
|
|
|
_secs += ts._secs;
|
2011-02-09 00:12:22 +00:00
|
|
|
_numFrames += ts._numFrames;
|
2010-01-09 22:36:32 +00:00
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
normalize();
|
2009-03-04 06:58:28 +00:00
|
|
|
}
|
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
Timestamp Timestamp::operator-() const {
|
|
|
|
Timestamp result(*this);
|
|
|
|
result._secs = -_secs;
|
|
|
|
result._numFrames = -_numFrames;
|
|
|
|
result.normalize();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timestamp Timestamp::operator+(const Timestamp &ts) const {
|
|
|
|
Timestamp result(*this);
|
|
|
|
result.addIntern(ts);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timestamp Timestamp::operator-(const Timestamp &ts) const {
|
|
|
|
Timestamp result(*this);
|
|
|
|
result.addIntern(-ts);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-01-04 22:48:28 +00:00
|
|
|
int Timestamp::frameDiff(const Timestamp &ts) const {
|
|
|
|
|
|
|
|
int delta = 0;
|
2010-01-06 12:09:14 +00:00
|
|
|
if (_secs != ts._secs)
|
2010-01-11 10:17:55 +00:00
|
|
|
delta = (_secs - ts._secs) * _framerate;
|
2010-01-04 22:48:28 +00:00
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
delta += _numFrames;
|
2009-03-04 06:58:28 +00:00
|
|
|
|
2010-01-04 22:48:28 +00:00
|
|
|
if (_framerate == ts._framerate) {
|
2010-01-11 10:17:55 +00:00
|
|
|
delta -= ts._numFrames;
|
2010-01-04 22:48:28 +00:00
|
|
|
} else {
|
|
|
|
// We need to multiply by the quotient of the two framerates.
|
|
|
|
// We cancel the GCD in this fraction to reduce the risk of
|
|
|
|
// overflows.
|
2010-05-17 20:53:04 +00:00
|
|
|
const uint g = Common::gcd(_framerate, ts._framerate);
|
2010-01-04 22:48:28 +00:00
|
|
|
const uint p = _framerate / g;
|
|
|
|
const uint q = ts._framerate / g;
|
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
delta -= ((long)ts._numFrames * p + q/2) / (long)q;
|
2010-01-04 22:48:28 +00:00
|
|
|
}
|
2009-03-04 06:58:28 +00:00
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
return delta / (int)_framerateFactor;
|
2009-03-04 06:58:28 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 22:48:28 +00:00
|
|
|
int Timestamp::msecsDiff(const Timestamp &ts) const {
|
2010-01-11 10:17:55 +00:00
|
|
|
return msecs() - ts.msecs();
|
2009-03-04 06:58:28 +00:00
|
|
|
}
|
|
|
|
|
2010-01-11 10:17:55 +00:00
|
|
|
int Timestamp::msecs() const {
|
2010-01-09 22:36:32 +00:00
|
|
|
// Note that _framerate is always divisible by 1000.
|
2010-01-11 10:17:55 +00:00
|
|
|
return _secs * 1000 + _numFrames / (_framerate / 1000);
|
2009-03-04 06:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // End of namespace Audio
|