[Scummvm-cvs-logs] CVS: scummvm/sound mixer.cpp,1.185,1.186 mixer.h,1.98,1.99

Max Horn fingolfin at users.sourceforge.net
Sat Mar 12 12:27:09 CET 2005


Update of /cvsroot/scummvm/scummvm/sound
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1294

Modified Files:
	mixer.cpp mixer.h 
Log Message:
Simplified SoundHandle implementation

Index: mixer.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.cpp,v
retrieving revision 1.185
retrieving revision 1.186
diff -u -d -r1.185 -r1.186
--- mixer.cpp	12 Mar 2005 18:56:06 -0000	1.185
+++ mixer.cpp	12 Mar 2005 20:26:48 -0000	1.186
@@ -45,9 +45,9 @@
 class Channel {
 public:
 	const SoundMixer::SoundType	_type;
+	SoundHandle _handle;
 private:
 	SoundMixer *_mixer;
-	SoundHandle *_handle;
 	bool _autofreeStream;
 	bool _permanent;
 	byte _volume;
@@ -64,8 +64,8 @@
 
 public:
 
-	Channel(SoundMixer *mixer, SoundHandle *handle, SoundMixer::SoundType type, int id = -1);
-	Channel(SoundMixer *mixer, SoundHandle *handle, SoundMixer::SoundType type, AudioStream *input, bool autofreeStream, bool reverseStereo = false, int id = -1, bool permanent = false);
+	Channel(SoundMixer *mixer, SoundMixer::SoundType type, int id = -1);
+	Channel(SoundMixer *mixer, SoundMixer::SoundType type, AudioStream *input, bool autofreeStream, bool reverseStereo = false, int id = -1, bool permanent = false);
 	virtual ~Channel();
 
 	void mix(int16 *data, uint len);
@@ -103,6 +103,8 @@
 SoundMixer::SoundMixer() {
 	_syst = &OSystem::instance();
 
+	_handleSeed = 0;
+
 	_premixChannel = 0;
 	int i = 0;
 
@@ -145,7 +147,7 @@
 		return;
 
 	// Create the channel
-	_premixChannel = new Channel(this, 0, type, stream, false);
+	_premixChannel = new Channel(this, type, stream, false);
 }
 
 void SoundMixer::insertChannel(SoundHandle *handle, Channel *chan) {
@@ -164,8 +166,11 @@
 	}
 
 	_channels[index] = chan;
-	if (handle)
-		handle->setIndex(index);
+	 chan->_handle = index + (_handleSeed * NUM_CHANNELS);
+	_handleSeed++;
+	if (handle) {
+		*handle = chan->_handle;
+	}
 }
 
 void SoundMixer::playRaw(SoundHandle *handle, void *sound, uint32 size, uint rate, byte flags,
@@ -196,7 +201,7 @@
 	}
 
 	// Create the channel
-	Channel *chan = new Channel(this, handle, type, input, true, (flags & SoundMixer::FLAG_REVERSE_STEREO) != 0, id);
+	Channel *chan = new Channel(this, type, input, true, (flags & SoundMixer::FLAG_REVERSE_STEREO) != 0, id);
 	chan->setVolume(volume);
 	chan->setBalance(balance);
 	insertChannel(handle, chan);
@@ -222,7 +227,7 @@
 	}
 
 	// Create the channel
-	Channel *chan = new Channel(this, handle, type, input, autofreeStream, false, id, permanent);
+	Channel *chan = new Channel(this, type, input, autofreeStream, false, id, permanent);
 	chan->setVolume(volume);
 	chan->setBalance(balance);
 	insertChannel(handle, chan);
@@ -283,54 +288,32 @@
 	Common::StackLock lock(_mutex);
 
 	// Simply ignore stop requests for handles of sounds that already terminated
-	if (!handle.isActive())
-		return;
-
-	int index = handle.getIndex();
-
-	if ((index < 0) || (index >= NUM_CHANNELS)) {
-		warning("soundMixer::stopHandle has invalid index %d", index);
+	const int index = handle % NUM_CHANNELS;
+	if (!_channels[index] || _channels[index]->_handle != handle)
 		return;
-	}
 
-	if (_channels[index]) {
-		delete _channels[index];
-		_channels[index] = 0;
-	}
+	delete _channels[index];
+	_channels[index] = 0;
 }
 
 void SoundMixer::setChannelVolume(SoundHandle handle, byte volume) {
 	Common::StackLock lock(_mutex);
 
-	if (!handle.isActive())
-		return;
-
-	int index = handle.getIndex();
-
-	if ((index < 0) || (index >= NUM_CHANNELS)) {
-		warning("soundMixer::setChannelVolume has invalid index %d", index);
+	const int index = handle % NUM_CHANNELS;
+	if (!_channels[index] || _channels[index]->_handle != handle)
 		return;
-	}
 
-	if (_channels[index])
-		_channels[index]->setVolume(volume);
+	_channels[index]->setVolume(volume);
 }
 
 void SoundMixer::setChannelBalance(SoundHandle handle, int8 balance) {
 	Common::StackLock lock(_mutex);
 
-	if (!handle.isActive())
-		return;
-
-	int index = handle.getIndex();
-
-	if ((index < 0) || (index >= NUM_CHANNELS)) {
-		warning("soundMixer::setChannelBalance has invalid index %d", index);
+	const int index = handle % NUM_CHANNELS;
+	if (!_channels[index] || _channels[index]->_handle != handle)
 		return;
-	}
 
-	if (_channels[index])
-		_channels[index]->setBalance(balance);
+	_channels[index]->setBalance(balance);
 }
 
 uint32 SoundMixer::getSoundElapsedTimeOfSoundID(int id) {
@@ -344,21 +327,11 @@
 uint32 SoundMixer::getSoundElapsedTime(SoundHandle handle) {
 	Common::StackLock lock(_mutex);
 
-	if (!handle.isActive())
-		return 0;
-
-	int index = handle.getIndex();
-
-	if ((index < 0) || (index >= NUM_CHANNELS)) {
-		warning("soundMixer::getSoundElapsedTime has invalid index %d", index);
+	const int index = handle % NUM_CHANNELS;
+	if (!_channels[index] || _channels[index]->_handle != handle)
 		return 0;
-	}
 
-	if (_channels[index])
-		return _channels[index]->getElapsedTime();
-
-	warning("soundMixer::getSoundElapsedTime has no channel object for index %d", index);
-	return 0;
+	return _channels[index]->getElapsedTime();
 }
 
 void SoundMixer::pauseAll(bool paused) {
@@ -379,18 +352,11 @@
 	Common::StackLock lock(_mutex);
 
 	// Simply ignore pause/unpause requests for handles of sound that alreayd terminated
-	if (!handle.isActive())
-		return;
-
-	int index = handle.getIndex();
-
-	if ((index < 0) || (index >= NUM_CHANNELS)) {
-		warning("soundMixer::pauseHandle has invalid index %d", index);
+	const int index = handle % NUM_CHANNELS;
+	if (!_channels[index] || _channels[index]->_handle != handle)
 		return;
-	}
 
-	if (_channels[index])
-		_channels[index]->pause(paused);
+	_channels[index]->pause(paused);
 }
 
 bool SoundMixer::isSoundIDActive(int id) {
@@ -401,6 +367,11 @@
 	return false;
 }
 
+bool SoundMixer::isSoundHandleActive(SoundHandle handle) {
+	const int index = handle % NUM_CHANNELS;
+	return _channels[index] && _channels[index]->_handle == handle;
+}
+
 bool SoundMixer::hasActiveChannelOfType(SoundType type) {
 	Common::StackLock lock(_mutex);
 	for (int i = 0; i != NUM_CHANNELS; i++)
@@ -436,16 +407,16 @@
 #pragma mark -
 
 
-Channel::Channel(SoundMixer *mixer, SoundHandle *handle, SoundMixer::SoundType type, int id)
-	: _type(type), _mixer(mixer), _handle(handle), _autofreeStream(true),
+Channel::Channel(SoundMixer *mixer, SoundMixer::SoundType type, int id)
+	: _type(type), _mixer(mixer), _autofreeStream(true),
 	  _volume(SoundMixer::kMaxChannelVolume), _balance(0), _paused(false), _id(id), _samplesConsumed(0),
 	  _samplesDecoded(0), _mixerTimeStamp(0), _converter(0), _input(0) {
 	assert(mixer);
 }
 
-Channel::Channel(SoundMixer *mixer, SoundHandle *handle, SoundMixer::SoundType type, AudioStream *input,
+Channel::Channel(SoundMixer *mixer, SoundMixer::SoundType type, AudioStream *input,
 				bool autofreeStream, bool reverseStereo, int id, bool permanent)
-	: _type(type), _mixer(mixer), _handle(handle), _autofreeStream(autofreeStream),
+	: _type(type), _mixer(mixer), _autofreeStream(autofreeStream),
 	  _volume(SoundMixer::kMaxChannelVolume), _balance(0), _paused(false), _id(id), _samplesConsumed(0),
 	  _samplesDecoded(0), _mixerTimeStamp(0), _converter(0), _input(input), _permanent(permanent) {
 	assert(mixer);
@@ -459,8 +430,6 @@
 	delete _converter;
 	if (_autofreeStream)
 		delete _input;
-	if (_handle)
-		_handle->resetIndex();
 }
 
 /* len indicates the number of sample *pairs*. So a value of

Index: mixer.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.h,v
retrieving revision 1.98
retrieving revision 1.99
diff -u -d -r1.98 -r1.99
--- mixer.h	12 Mar 2005 18:56:06 -0000	1.98
+++ mixer.h	12 Mar 2005 20:26:49 -0000	1.99
@@ -33,17 +33,7 @@
 class File;
 class OSystem;
 
-class SoundHandle {
-	friend class Channel;
-	friend class SoundMixer;
-	int val;
-	int getIndex() const { return val - 1; }
-	void setIndex(int i) { val = i + 1; }
-	void resetIndex() { val = 0; }
-	bool isActive() const { return val > 0; }
-public:
-	SoundHandle() { resetIndex(); }
-};
+typedef uint32 SoundHandle;
 
 class SoundMixer {
 public:
@@ -99,6 +89,7 @@
 
 	bool _paused;
 	
+	uint32 _handleSeed;
 	Channel *_channels[NUM_CHANNELS];
 
 	bool _mixerReady;
@@ -212,9 +203,7 @@
 	 * @param handle the sound to query
 	 * @return true if the sound is active
 	 */
-	bool isSoundHandleActive(SoundHandle handle) {
-		return handle.isActive();
-	}
+	bool isSoundHandleActive(SoundHandle handle);
 
 	/**
 	 * Check if the mixer is paused (using pauseAll).





More information about the Scummvm-git-logs mailing list