1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/* 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.
* 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.
* 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.
*
* $URL$
* $Id$
*
*/
#include "sound/timestamp.h"
namespace Audio {
static uint gcd(uint a, uint b) {
while (a > 0) {
int tmp = a;
a = b % a;
b = tmp;
}
return b;
}
Timestamp::Timestamp(uint32 m, int framerate) :
_msecs(m), _framerate(framerate), _numberOfFrames(0) {
assert(_framerate > 0);
}
Timestamp Timestamp::convertToFramerate(int newFramerate) const {
Timestamp ts(*this);
if (ts._framerate != newFramerate) {
ts._framerate = newFramerate;
const uint g = gcd(_framerate, ts._framerate);
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.
ts._numberOfFrames = (ts._numberOfFrames * q + p/2) / p;
ts._msecs += (ts._numberOfFrames / ts._framerate) * 1000;
ts._numberOfFrames %= ts._framerate;
}
return ts;
}
bool Timestamp::operator==(const Timestamp &ts) const {
// TODO: Alternatively, we could define equality to mean that
// two timestamps describe the exacts same moment in time.
return (_msecs == ts._msecs) &&
(_numberOfFrames == ts._numberOfFrames) &&
(_framerate == ts._framerate);
}
bool Timestamp::operator!=(const Timestamp &ts) const {
return !(*this == ts);
}
Timestamp Timestamp::addFrames(int frames) const {
Timestamp ts(*this);
ts._numberOfFrames += frames;
if (ts._numberOfFrames < 0) {
int secsub = 1 + (-ts._numberOfFrames / ts._framerate);
ts._numberOfFrames += ts._framerate * secsub;
ts._msecs -= secsub * 1000;
}
ts._msecs += (ts._numberOfFrames / ts._framerate) * 1000;
ts._numberOfFrames %= ts._framerate;
return ts;
}
Timestamp Timestamp::addMsecs(int ms) const {
Timestamp ts(*this);
ts._msecs += ms;
return ts;
}
int Timestamp::frameDiff(const Timestamp &ts) const {
int delta = 0;
if (_msecs != ts._msecs)
delta = (long(_msecs) - long(ts._msecs)) * _framerate / 1000;
delta += _numberOfFrames;
if (_framerate == ts._framerate) {
delta -= ts._numberOfFrames;
} 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.
const uint g = gcd(_framerate, ts._framerate);
const uint p = _framerate / g;
const uint q = ts._framerate / g;
delta -= (ts._numberOfFrames * p + q/2) / q;
}
return delta;
}
int Timestamp::msecsDiff(const Timestamp &ts) const {
return long(msecs()) - long(ts.msecs());
}
uint32 Timestamp::msecs() const {
return _msecs + _numberOfFrames * 1000L / _framerate;
}
} // End of namespace Audio
|