volume correction for 8, 16 and 32 bit

This commit is contained in:
badaix 2016-05-01 19:19:24 +02:00
parent 2275466d09
commit 8588488f8f
2 changed files with 23 additions and 8 deletions

View file

@ -31,7 +31,8 @@ Player::Player(const PcmDevice& pcmDevice, Stream* stream) :
stream_(stream),
pcmDevice_(pcmDevice),
volume_(1.0),
muted_(false)
muted_(false),
volCorrection_(1.0)
{
}
@ -69,13 +70,14 @@ void Player::adjustVolume(char* buffer, size_t frames)
const SampleFormat& sampleFormat = stream_->getFormat();
if (volume < 1.0)
if ((volume < 1.0) || (volCorrection_ != 1.))
{
if (sampleFormat.bits == 8)
volume *= volCorrection_;
if (sampleFormat.sampleSize == 1)
adjustVolume<int8_t>(buffer, frames*sampleFormat.channels, volume);
else if (sampleFormat.bits == 16)
else if (sampleFormat.sampleSize == 2)
adjustVolume<int16_t>(buffer, frames*sampleFormat.channels, volume);
else if (sampleFormat.bits == 32)
else if (sampleFormat.sampleSize == 4)
adjustVolume<int32_t>(buffer, frames*sampleFormat.channels, volume);
}
}

View file

@ -52,9 +52,21 @@ protected:
void adjustVolume(char *buffer, size_t count, double volume)
{
T* bufferT = (T*)buffer;
//TODO: SWAP_T. Hard coded to 16 bit audio
for (size_t n=0; n<count; ++n)
bufferT[n] = (T)(SWAP_16(((T)SWAP_16(bufferT[n])) * volume));
if (sizeof(T) == 1)
{
for (size_t n=0; n<count; ++n)
bufferT[n] = bufferT[n] * volume;
}
else if (sizeof(T) == 2)
{
for (size_t n=0; n<count; ++n)
bufferT[n] = (T)(SWAP_16(((T)SWAP_16(bufferT[n])) * volume));
}
else if (sizeof(T) == 4)
{
for (size_t n=0; n<count; ++n)
bufferT[n] = (T)(SWAP_32(((T)SWAP_32(bufferT[n])) * volume));
}
}
void adjustVolume(char* buffer, size_t frames);
@ -65,6 +77,7 @@ protected:
PcmDevice pcmDevice_;
double volume_;
bool muted_;
double volCorrection_;
};