An attempt to work around an impossible-to-reproduce bug in PulseOut. It seems that sometimes setting the stream volume doesn't work immediately, maybe because the stream isn't ready. Or maybe due a floating point rounding error.

This commit is contained in:
casey langen 2017-01-28 08:31:08 +00:00
parent bf18797b26
commit 3c75ab2a3d
3 changed files with 12 additions and 2 deletions

View File

@ -46,6 +46,7 @@ PulseOut::PulseOut() {
this->audioConnection = nullptr;
this->state = StateStopped;
this->volume = 1.0f;
this->volumeUpdated = false;
this->channels = 0;
this->rate = 0;
}
@ -142,10 +143,14 @@ void PulseOut::Resume() {
void PulseOut::SetVolume(double volume) {
Lock lock(this->stateMutex);
if (volume > 1.0) { volume = 1.0; }
if (volume < 0) { volume = 0; }
this->volume = volume;
this->volumeUpdated = false;
if (this->audioConnection) {
int normalized = (int) round((double) PA_VOLUME_NORM * volume);
pa_blocking_set_volume(this->audioConnection, normalized, 0);
this->volumeUpdated = pa_blocking_set_volume(this->audioConnection, normalized, 0) == 0;
std::cerr << "PulseOut: volumeUpdated = " << this->volumeUpdated << "\n";
}
}
@ -169,6 +174,10 @@ int PulseOut::Play(IBuffer *buffer, IBufferProvider* provider) {
return OutputInvalidState;
}
if (!this->volumeUpdated) {
this->SetVolume(this->volume);
}
pa_blocking_write(
this->audioConnection,
buffer->BufferPointer(),

View File

@ -76,4 +76,5 @@ class PulseOut : public musik::core::sdk::IOutput {
State state;
int channels, rate;
double volume;
bool volumeUpdated;
};

View File

@ -515,7 +515,7 @@ int pa_blocking_set_volume(pa_blocking *p, int volume, int *rerror) {
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
CHECK_VALIDITY_RETURN_ANY(rerror, volume >= 0, PA_ERR_INVALID, -1);
CHECK_VALIDITY_RETURN_ANY(rerror, volume <= 65535, PA_ERR_INVALID, -1);
CHECK_VALIDITY_RETURN_ANY(rerror, volume <= 65536, PA_ERR_INVALID, -1);
pa_threaded_mainloop_lock(p->mainloop);
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);