mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-09 06:11:45 +02:00
configurable volume for OpenSL player
This commit is contained in:
parent
0f90f05e9d
commit
abc1488cd3
5 changed files with 32 additions and 22 deletions
|
@ -177,22 +177,10 @@ void AlsaPlayer::worker()
|
|||
snd_pcm_delay(handle_, &framesDelay);
|
||||
chronos::usec delay((chronos::usec::rep) (1000 * (double) framesDelay / stream_->getFormat().msRate()));
|
||||
// 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 (volume < 1.0)
|
||||
{
|
||||
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);
|
||||
}
|
||||
adjustVolume(buff_, frames_);
|
||||
if ((pcm = snd_pcm_writei(handle_, buff_, frames_)) == -EPIPE)
|
||||
{
|
||||
logE << "XRUN\n";
|
||||
|
|
|
@ -47,17 +47,8 @@ private:
|
|||
void initAlsa();
|
||||
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_uframes_t frames_;
|
||||
|
||||
char *buff_;
|
||||
};
|
||||
|
||||
|
|
|
@ -98,6 +98,8 @@ void OpenslPlayer::playerCallback(SLAndroidSimpleBufferQueueItf bq)
|
|||
memset(buffer[curBuffer], 0, buff_size);
|
||||
}
|
||||
|
||||
adjustVolume(buffer[curBuffer], frames_);
|
||||
|
||||
while (active_)
|
||||
{
|
||||
SLresult result = (*bq)->Enqueue(bq, buffer[curBuffer], buff_size);
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -46,6 +46,16 @@ public:
|
|||
protected:
|
||||
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_;
|
||||
Stream* stream_;
|
||||
std::thread playerThread_;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue