[Scummvm-cvs-logs] SF.net SVN: scummvm:[47212] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sat Jan 9 23:36:32 CET 2010


Revision: 47212
          http://scummvm.svn.sourceforge.net/scummvm/?rev=47212&view=rev
Author:   fingolfin
Date:     2010-01-09 22:36:32 +0000 (Sat, 09 Jan 2010)

Log Message:
-----------
Fix Timestamp::addMsecs; some cleanup

Modified Paths:
--------------
    scummvm/trunk/sound/timestamp.cpp
    scummvm/trunk/sound/timestamp.h
    scummvm/trunk/test/sound/timestamp.h

Modified: scummvm/trunk/sound/timestamp.cpp
===================================================================
--- scummvm/trunk/sound/timestamp.cpp	2010-01-09 21:20:39 UTC (rev 47211)
+++ scummvm/trunk/sound/timestamp.cpp	2010-01-09 22:36:32 UTC (rev 47212)
@@ -43,7 +43,8 @@
 	_framerateFactor = 1000 / gcd(1000, fr);
 	_framerate = fr * _framerateFactor;
 
-	_numberOfFrames = (ms % 1000) * _framerate / 1000;
+	// Note that _framerate is always divisible by 1000.
+	_numberOfFrames = (ms % 1000) * (_framerate / 1000);
 }
 
 Timestamp::Timestamp(uint s, int frames, int fr) {
@@ -53,7 +54,7 @@
 	_framerateFactor = 1000 / gcd(1000, fr);
 	_framerate = fr * _framerateFactor;
 	_numberOfFrames = 0;
-	*this = addFrames(frames);
+	addFramesIntern(frames * _framerateFactor);
 }
 
 Timestamp Timestamp::convertToFramerate(int newFramerate) const {
@@ -123,26 +124,33 @@
 
 	// The frames are given in the original framerate, so we have to
 	// adjust by _framerateFactor accordingly.
-	ts._numberOfFrames += frames * _framerateFactor;
+	ts.addFramesIntern(frames * _framerateFactor);
 
-	if (ts._numberOfFrames < 0) {
-		int secsub = 1 + (-ts._numberOfFrames / ts._framerate);
+	return ts;
+}
 
-		ts._numberOfFrames += ts._framerate * secsub;
-		ts._secs -= secsub;
+void Timestamp::addFramesIntern(int frames) {
+	_numberOfFrames += frames;
+
+	if (_numberOfFrames < 0) {
+		int secsub = 1 + (-_numberOfFrames / _framerate);
+
+		_numberOfFrames += _framerate * secsub;
+		_secs -= secsub;
 	}
 
-	ts._secs += (ts._numberOfFrames / ts._framerate);
-	ts._numberOfFrames %= ts._framerate;
-
-	return ts;
+	// Wrap around if necessary
+	_secs += (_numberOfFrames / _framerate);
+	_numberOfFrames %= _framerate;
 }
 
 Timestamp Timestamp::addMsecs(int ms) const {
+	assert(ms >= 0);
 	Timestamp ts(*this);
 	ts._secs += ms / 1000;
 	// Add the remaining frames. Note that _framerate is always divisible by 1000.
-	return ts.addFrames((ms % 1000) * (_framerate / 1000));
+	ts.addFramesIntern((ms % 1000) * (ts._framerate / 1000));
+	return ts;
 }
 
 int Timestamp::frameDiff(const Timestamp &ts) const {
@@ -174,6 +182,7 @@
 }
 
 uint32 Timestamp::msecs() const {
+	// Note that _framerate is always divisible by 1000.
 	return _secs * 1000 + _numberOfFrames / (_framerate / 1000);
 }
 

Modified: scummvm/trunk/sound/timestamp.h
===================================================================
--- scummvm/trunk/sound/timestamp.h	2010-01-09 21:20:39 UTC (rev 47211)
+++ scummvm/trunk/sound/timestamp.h	2010-01-09 22:36:32 UTC (rev 47212)
@@ -164,6 +164,8 @@
 protected:
 
 	int cmp(const Timestamp &ts) const;
+
+	void addFramesIntern(int frames);
 };
 
 

Modified: scummvm/trunk/test/sound/timestamp.h
===================================================================
--- scummvm/trunk/test/sound/timestamp.h	2010-01-09 21:20:39 UTC (rev 47211)
+++ scummvm/trunk/test/sound/timestamp.h	2010-01-09 22:36:32 UTC (rev 47212)
@@ -5,7 +5,7 @@
 class TimestampTestSuite : public CxxTest::TestSuite
 {
 	public:
-	void test_diff_add() {
+	void test_diff_add_frames() {
 		const Audio::Timestamp a(10000, 1000);
 		const Audio::Timestamp b(10001, 1000);
 		const Audio::Timestamp c(10002, 1000);
@@ -23,6 +23,38 @@
 		TS_ASSERT_EQUALS(c.frameDiff(a), 2);
 	}
 
+	void test_diff_add_msecs() {
+		Audio::Timestamp ts0(3, 22050);
+		Audio::Timestamp ts1(0, 22050);
+		Audio::Timestamp ts2(0, 22050);
+
+		TS_ASSERT_EQUALS(ts0.msecs(), 3);
+		TS_ASSERT_EQUALS(ts0.totalNumberOfFrames(), 3 * 22050 / 1000);
+		TS_ASSERT_EQUALS(ts0.numberOfFrames(), 3 * 22050 / 1000);
+
+		ts1 = ts1.addFrames(53248);
+		TS_ASSERT_EQUALS(ts1.secs(), 2);
+		TS_ASSERT_EQUALS(ts1.msecs(), 53248 * 1000 / 22050);
+		TS_ASSERT_EQUALS(ts1.totalNumberOfFrames(), 53248);
+		TS_ASSERT_EQUALS(ts1.numberOfFrames(), 53248 - 2 * 22050);
+		ts1 = ts1.addMsecs(47);
+		TS_ASSERT_EQUALS(ts1.secs(), 2);
+		TS_ASSERT_EQUALS(ts1.msecs(), 2414+47);
+		TS_ASSERT_EQUALS(ts1.totalNumberOfFrames(), 47*22050 / 1000 + 53248);
+		TS_ASSERT_EQUALS(ts1.numberOfFrames(), 47*22050 / 1000 + 53248 - 2 * 22050);
+
+		ts2 = ts2.addMsecs(47);
+		TS_ASSERT_EQUALS(ts2.secs(), 0);
+		TS_ASSERT_EQUALS(ts2.msecs(), 47);
+		TS_ASSERT_EQUALS(ts2.totalNumberOfFrames(), 47*22050 / 1000);
+		TS_ASSERT_EQUALS(ts2.numberOfFrames(), 47*22050 / 1000);
+		ts2 = ts2.addFrames(53248);
+		TS_ASSERT_EQUALS(ts2.secs(), 2);
+		TS_ASSERT_EQUALS(ts2.msecs(), 2414+47);
+		TS_ASSERT_EQUALS(ts2.totalNumberOfFrames(), 47*22050 / 1000 + 53248);
+		TS_ASSERT_EQUALS(ts2.numberOfFrames(), 47*22050 / 1000 + 53248 - 2 * 22050);
+	}
+
 	void test_ticks() {
 		const Audio::Timestamp a(1234, 60);
 		const Audio::Timestamp b(5678, 60);


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list