From 673a21b249d9a1753cb2622be1085636299e0990 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 4 Mar 2009 06:58:28 +0000 Subject: Added Audio::Timestamp class, based on SCI's sfx_timestamp_t; also provide a unit test for it, based on the old (and very outdated) timetest.cpp. To be used by Audio::Mixer one day... svn-id: r39112 --- sound/audiostream.h | 6 ++-- sound/module.mk | 1 + sound/timestamp.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ sound/timestamp.h | 61 ++++++++++++++++++++++++++++++++++++++++ test/module.mk | 4 +-- test/sound/timestamp.h | 34 +++++++++++++++++++++++ 6 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 sound/timestamp.cpp create mode 100644 sound/timestamp.h create mode 100644 test/sound/timestamp.h diff --git a/sound/audiostream.h b/sound/audiostream.h index 04133936ee..a917957a77 100644 --- a/sound/audiostream.h +++ b/sound/audiostream.h @@ -59,6 +59,9 @@ public: /** Is this a stereo stream? */ virtual bool isStereo() const = 0; + /** Sample rate of the stream. */ + virtual int getRate() const = 0; + /** * End of data reached? If this returns true, it means that at this * time there is no data available in the stream. However there may be @@ -78,9 +81,6 @@ public: */ virtual bool endOfStream() const { return endOfData(); } - /** Sample rate of the stream. */ - virtual int getRate() const = 0; - /** * Tries to load a file by trying all available formats. * In case of an error, the file handle will be closed, but deleting diff --git a/sound/module.mk b/sound/module.mk index f00f9e3adb..c11716a107 100644 --- a/sound/module.mk +++ b/sound/module.mk @@ -18,6 +18,7 @@ MODULE_OBJS := \ musicplugin.o \ null.o \ shorten.o \ + timestamp.o \ voc.o \ vorbis.o \ wave.o \ diff --git a/sound/timestamp.cpp b/sound/timestamp.cpp new file mode 100644 index 0000000000..8b0f660385 --- /dev/null +++ b/sound/timestamp.cpp @@ -0,0 +1,75 @@ +/* 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 { + +Timestamp::Timestamp() : + _msecs(0), _frameRate(0), _frameOffset(0) { +} + +Timestamp::Timestamp(uint32 m, int frameRate) : + _msecs(m), _frameRate(frameRate), _frameOffset(0) { +} + + +Timestamp Timestamp::addFrames(int frames) const { + Timestamp timestamp(*this); + timestamp._frameOffset += frames; + + if (timestamp._frameOffset < 0) { + int secsub = 1 + (-timestamp._frameOffset / timestamp._frameRate); + + timestamp._frameOffset += timestamp._frameRate * secsub; + timestamp._msecs -= secsub * 1000; + } + + timestamp._msecs += (timestamp._frameOffset / timestamp._frameRate) * 1000; + timestamp._frameOffset %= timestamp._frameRate; + + return timestamp; +} + +int Timestamp::frameDiff(const Timestamp &b) const { + assert(_frameRate == b._frameRate); + + int msecdelta = 0; + if (_msecs != b._msecs) + msecdelta = (long(_msecs) - long(b._msecs)) * _frameRate / 1000; + + return msecdelta + _frameOffset - b._frameOffset; +} + +int Timestamp::msecsDiff(const Timestamp &b) const { + return long(msecs()) - long(b.msecs()); +} + +uint32 Timestamp::msecs() const { + return _msecs + _frameOffset * 1000L / _frameRate; +} + + +} // End of namespace Audio diff --git a/sound/timestamp.h b/sound/timestamp.h new file mode 100644 index 0000000000..c68c4b7357 --- /dev/null +++ b/sound/timestamp.h @@ -0,0 +1,61 @@ +/* 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$ + * + */ + +#ifndef SOUND_TIMESTAMP_H +#define SOUND_TIMESTAMP_H + +#include "common/scummsys.h" + + +namespace Audio { + +class Timestamp { +protected: + uint32 _msecs; + int _frameRate; + int _frameOffset; + /* Total time: msecs + frame_offset/frame_rate */ + +public: + Timestamp(); + Timestamp(uint32 msecs, int frameRate); + + /** Adds a number of frames to a timestamp. */ + Timestamp addFrames(int frames) const; + + /** Computes the difference (# of frames) between this timestamp and b. */ + int frameDiff(const Timestamp &b) const; + + /** Computes the difference (# of milliseconds) between this timestamp and b. */ + int msecsDiff(const Timestamp &b) const; + + /** Determines the time in milliseconds described by this timestamp. */ + uint32 msecs() const; +}; + + +} // End of namespace Audio + +#endif diff --git a/test/module.mk b/test/module.mk index 22c2dfa2c5..bf6a9efe8a 100644 --- a/test/module.mk +++ b/test/module.mk @@ -5,8 +5,8 @@ # ###################################################################### -TESTS := test/common/*.h -TEST_LIBS := common/libcommon.a +TESTS := test/common/*.h test/sound/*.h +TEST_LIBS := common/libcommon.a sound/libsound.a # TEST_FLAGS := --runner=StdioPrinter diff --git a/test/sound/timestamp.h b/test/sound/timestamp.h new file mode 100644 index 0000000000..d6477bc336 --- /dev/null +++ b/test/sound/timestamp.h @@ -0,0 +1,34 @@ +#include + +#include "sound/timestamp.h" + +class TimestampTestSuite : public CxxTest::TestSuite +{ + public: + void test_diff_add(void) { + Audio::Timestamp a(10000, 1000); + Audio::Timestamp b(10001, 1000); + Audio::Timestamp c(10002, 1000); + + TS_ASSERT_EQUALS(a.frameDiff(b), -1); + TS_ASSERT_EQUALS(b.frameDiff(a), 1); + TS_ASSERT_EQUALS(c.frameDiff(a), 2); + TS_ASSERT_EQUALS(b.addFrames(2000).frameDiff(a), 2001); + TS_ASSERT_EQUALS(a.frameDiff(b), -1); + TS_ASSERT_EQUALS(b.frameDiff(a), 1); + TS_ASSERT_EQUALS(c.frameDiff(a), 2); + TS_ASSERT_EQUALS(b.addFrames(2000).frameDiff(a.addFrames(-1000)), 3001); + TS_ASSERT_EQUALS(a.frameDiff(b), -1); + TS_ASSERT_EQUALS(b.frameDiff(a), 1); + TS_ASSERT_EQUALS(c.frameDiff(a), 2); + } + + void test_more_add_diff(void) { + const Audio::Timestamp c(10002, 1000); + + for (int i = -10000; i < 10000; i++) { + int v = c.addFrames(i).frameDiff(c); + TS_ASSERT_EQUALS(v, i); + } + } +}; -- cgit v1.2.3