Simplified SoundHandle implementation

svn-id: r17107
This commit is contained in:
Max Horn 2005-03-12 20:26:49 +00:00
parent 7cd2cb2b17
commit 0e784d41d7
2 changed files with 41 additions and 83 deletions

View file

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

View file

@ -33,17 +33,7 @@ class Channel;
class File; class File;
class OSystem; class OSystem;
class SoundHandle { typedef uint32 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(); }
};
class SoundMixer { class SoundMixer {
public: public:
@ -99,6 +89,7 @@ private:
bool _paused; bool _paused;
uint32 _handleSeed;
Channel *_channels[NUM_CHANNELS]; Channel *_channels[NUM_CHANNELS];
bool _mixerReady; bool _mixerReady;
@ -212,9 +203,7 @@ public:
* @param handle the sound to query * @param handle the sound to query
* @return true if the sound is active * @return true if the sound is active
*/ */
bool isSoundHandleActive(SoundHandle handle) { bool isSoundHandleActive(SoundHandle handle);
return handle.isActive();
}
/** /**
* Check if the mixer is paused (using pauseAll). * Check if the mixer is paused (using pauseAll).