configurable volume for OpenSL player

This commit is contained in:
badaix 2016-01-08 18:16:13 +01:00
parent 0f90f05e9d
commit abc1488cd3
5 changed files with 32 additions and 22 deletions

View file

@ -177,22 +177,10 @@ void AlsaPlayer::worker()
snd_pcm_delay(handle_, &framesDelay); snd_pcm_delay(handle_, &framesDelay);
chronos::usec delay((chronos::usec::rep) (1000 * (double) framesDelay / stream_->getFormat().msRate())); chronos::usec delay((chronos::usec::rep) (1000 * (double) framesDelay / stream_->getFormat().msRate()));
// logO << "delay: " << framesDelay << ", delay[ms]: " << delay.count() / 1000 << "\n"; // logO << "delay: " << framesDelay << ", delay[ms]: " << delay.count() / 1000 << "\n";
double volume = volume_;
if (muted_)
volume = 0.;
const msg::SampleFormat& sampleFormat = stream_->getFormat();
if (stream_->getPlayerChunk(buff_, delay, frames_)) if (stream_->getPlayerChunk(buff_, delay, frames_))
{ {
if (volume < 1.0) adjustVolume(buff_, frames_);
{
if (sampleFormat.bits == 8)
adjustVolume<int8_t>(buff_, frames_*sampleFormat.channels, volume);
else if (sampleFormat.bits == 16)
adjustVolume<int16_t>(buff_, frames_*sampleFormat.channels, volume);
else if (sampleFormat.bits == 32)
adjustVolume<int32_t>(buff_, frames_*sampleFormat.channels, volume);
}
if ((pcm = snd_pcm_writei(handle_, buff_, frames_)) == -EPIPE) if ((pcm = snd_pcm_writei(handle_, buff_, frames_)) == -EPIPE)
{ {
logE << "XRUN\n"; logE << "XRUN\n";

View file

@ -47,17 +47,8 @@ private:
void initAlsa(); void initAlsa();
void uninitAlsa(); void uninitAlsa();
template <typename T>
void adjustVolume(char *buffer, size_t count, double volume)
{
T* bufferT = (T*)buffer;
for (size_t n=0; n<count; ++n)
bufferT[n] *= volume;
}
snd_pcm_t* handle_; snd_pcm_t* handle_;
snd_pcm_uframes_t frames_; snd_pcm_uframes_t frames_;
char *buff_; char *buff_;
}; };

View file

@ -98,6 +98,8 @@ void OpenslPlayer::playerCallback(SLAndroidSimpleBufferQueueItf bq)
memset(buffer[curBuffer], 0, buff_size); memset(buffer[curBuffer], 0, buff_size);
} }
adjustVolume(buffer[curBuffer], frames_);
while (active_) while (active_)
{ {
SLresult result = (*bq)->Enqueue(bq, buffer[curBuffer], buff_size); SLresult result = (*bq)->Enqueue(bq, buffer[curBuffer], buff_size);

View file

@ -61,6 +61,25 @@ void Player::stop()
} }
void Player::adjustVolume(char* buffer, size_t frames)
{
double volume = volume_;
if (muted_)
volume = 0.;
const msg::SampleFormat& sampleFormat = stream_->getFormat();
if (volume < 1.0)
{
if (sampleFormat.bits == 8)
adjustVolume<int8_t>(buffer, frames*sampleFormat.channels, volume);
else if (sampleFormat.bits == 16)
adjustVolume<int16_t>(buffer, frames*sampleFormat.channels, volume);
else if (sampleFormat.bits == 32)
adjustVolume<int32_t>(buffer, frames*sampleFormat.channels, volume);
}
}
void Player::setVolume(double volume) void Player::setVolume(double volume)
{ {

View file

@ -46,6 +46,16 @@ public:
protected: protected:
virtual void worker() = 0; virtual void worker() = 0;
template <typename T>
void adjustVolume(char *buffer, size_t count, double volume)
{
T* bufferT = (T*)buffer;
for (size_t n=0; n<count; ++n)
bufferT[n] *= volume;
}
void adjustVolume(char* buffer, size_t frames);
std::atomic<bool> active_; std::atomic<bool> active_;
Stream* stream_; Stream* stream_;
std::thread playerThread_; std::thread playerThread_;