AUDIO: Add rate adjustment functionality to Mixer

Extended the Mixer class to support manually setting the
sample rate of a channel.
This commit is contained in:
Kaloyan Chehlarski 2023-05-01 23:54:39 +03:00 committed by Eugene Sandulenko
parent 5ca776639f
commit 0f045a4ce4
3 changed files with 94 additions and 0 deletions

View File

@ -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

View File

@ -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.
*/

View File

@ -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);