diff --git a/audio/mixer.cpp b/audio/mixer.cpp index 4a663a2d8ca..87a778967c8 100644 --- a/audio/mixer.cpp +++ b/audio/mixer.cpp @@ -114,6 +114,26 @@ public: */ int8 getBalance(); + /** + * Set the channel's sample rate. + * + * @param rate The new sample rate. Must be less than 131072 + */ + void setRate(uint32 rate); + + /** + * Get the channel's sample rate. + * + * @return The current sample rate of the channel. + */ + uint32 getRate(); + + /** + * Reset the sample rate of the channel back to its + * AudioStream's native rate. + */ + void resetRate(); + /** * Notifies the channel that the global sound type * volume settings changed. @@ -401,6 +421,34 @@ int8 MixerImpl::getChannelBalance(SoundHandle handle) { return _channels[index]->getBalance(); } +void MixerImpl::setChannelRate(SoundHandle handle, uint32 rate) { + Common::StackLock lock(_mutex); + + const int index = handle._val % NUM_CHANNELS; + if (!_channels[index] || _channels[index]->getHandle()._val != handle._val) + return; + + _channels[index]->setRate(rate); +} + +uint32 MixerImpl::getChannelRate(SoundHandle handle) { + const int index = handle._val % NUM_CHANNELS; + if (!_channels[index] || _channels[index]->getHandle()._val != handle._val) + return 0; + + return _channels[index]->getRate(); +} + +void MixerImpl::resetChannelRate(SoundHandle handle) { + Common::StackLock lock(_mutex); + + const int index = handle._val % NUM_CHANNELS; + if (!_channels[index] || _channels[index]->getHandle()._val != handle._val) + return; + + _channels[index]->resetRate(); +} + uint32 MixerImpl::getSoundElapsedTime(SoundHandle handle) { return getElapsedTime(handle).msecs(); } @@ -559,6 +607,24 @@ int8 Channel::getBalance() { return _balance; } +void Channel::setRate(uint32 rate) { + if (_converter) + _converter->setInputRate(rate); +} + +uint32 Channel::getRate() { + if (_converter) + return _converter->getInputRate(); + + return 0; +} + +void Channel::resetRate() { + if (_converter && _stream) { + _converter->setInputRate(_stream->getRate()); + } +} + void Channel::updateChannelVolumes() { // From the channel balance/volume and the global volume, we compute // the effective volume for the left and right channel. Note the diff --git a/audio/mixer.h b/audio/mixer.h index ff521799617..6cc3d473d1e 100644 --- a/audio/mixer.h +++ b/audio/mixer.h @@ -254,6 +254,31 @@ public: */ virtual int8 getChannelBalance(SoundHandle handle) = 0; + /** + * Set the sample rate for the given handle. + * + * @param handle The sound to affect. + * @param rate The new sample rate. Must be less than 131072 + */ + virtual void setChannelRate(SoundHandle handle, uint32 rate) = 0; + + /** + * Get the sample rate for the given handle. + * + * @param handle The sound to affect. + * + * @return The current sample rate of the channel. + */ + virtual uint32 getChannelRate(SoundHandle handle) = 0; + + /** + * Reset the sample rate of the channel back to its + * AudioStream's native rate. + * + * @param handle The sound to affect. + */ + virtual void resetChannelRate(SoundHandle handle) = 0; + /** * Get an approximation of for how long the channel has been playing. */ diff --git a/audio/mixer_intern.h b/audio/mixer_intern.h index c7d67771cbd..18a0d97f176 100644 --- a/audio/mixer_intern.h +++ b/audio/mixer_intern.h @@ -118,6 +118,9 @@ public: virtual byte getChannelVolume(SoundHandle handle); virtual void setChannelBalance(SoundHandle handle, int8 balance); virtual int8 getChannelBalance(SoundHandle handle); + virtual void setChannelRate(SoundHandle handle, uint32 rate); + virtual uint32 getChannelRate(SoundHandle handle); + virtual void resetChannelRate(SoundHandle handle); virtual uint32 getSoundElapsedTime(SoundHandle handle); virtual Timestamp getElapsedTime(SoundHandle handle);