Skip to content
Snippets Groups Projects
Commit ca08bac9 authored by Alexandre Savard's avatar Alexandre Savard
Browse files

#8672: Implement audio gain management in pulseaudio

parent 749713a6
Branches
Tags
No related merge requests found
......@@ -540,15 +540,6 @@ AlsaLayer::getAudioDeviceIndex(const std::string &description) const
return 0;
}
namespace {
void adjustVolume(SFLDataFormat *src , int samples, int volumePercentage)
{
if (volumePercentage != 100)
for (int i = 0 ; i < samples; i++)
src[i] = src[i] * volumePercentage * 0.01;
}
}
void AlsaLayer::capture()
{
unsigned int mainBufferSampleRate = Manager::instance().getMainBuffer()->getInternalSamplingRate();
......@@ -575,7 +566,7 @@ void AlsaLayer::capture()
goto end;
}
adjustVolume(in, toGetSamples, getCaptureGain());
AudioLayer::applyGain(in, toGetSamples, getCaptureGain());
if (resample) {
int outSamples = toGetSamples * ((double) audioSampleRate_ / mainBufferSampleRate);
......@@ -639,7 +630,7 @@ void AlsaLayer::playback(int maxSamples)
SFLDataFormat *out = (SFLDataFormat*) malloc(toGet);
Manager::instance().getMainBuffer()->getData(out, toGet);
adjustVolume(out, toGet / sizeof(SFLDataFormat), getPlaybackGain());
AudioLayer::applyGain(out, toGet / sizeof(SFLDataFormat), getPlaybackGain());
if (resample) {
int inSamples = toGet / sizeof(SFLDataFormat);
......@@ -676,7 +667,7 @@ void AlsaLayer::audioCallback()
SFLDataFormat *out = (SFLDataFormat*) malloc(toGet);
urgentRingBuffer_.Get(out, toGet);
adjustVolume(out, toGet / sizeof(SFLDataFormat), getPlaybackGain());
AudioLayer::applyGain(out, toGet / sizeof(SFLDataFormat), getPlaybackGain());
write(out, toGet, playbackHandle_);
free(out);
......
......@@ -78,6 +78,13 @@ void AudioLayer::putUrgent(void* buffer, int toCopy)
urgentRingBuffer_.Put(buffer, toCopy);
}
void AudioLayer::applyGain(SFLDataFormat *src , int samples, int gain)
{
if (gain != 100)
for (int i = 0 ; i < samples; i++)
src[i] = src[i] * gain* 0.01;
}
// Notify (with a beep) an incoming call when there is already a call in progress
void AudioLayer::notifyincomingCall()
{
......
......@@ -105,6 +105,11 @@ class AudioLayer {
*/
void flushUrgent();
/**
* Apply gain to audio frame
*/
static void applyGain(SFLDataFormat *src , int samples, int gain);
/**
* Set capture stream gain (microphone)
*/
......
......@@ -331,6 +331,7 @@ void PulseLayer::writeToSpeaker()
if (urgentBytes) {
pa_stream_begin_write(s, &data, &urgentBytes);
urgentRingBuffer_.Get(data, urgentBytes);
applyGain(static_cast<SFLDataFormat *>(data), urgentBytes / sizeof(SFLDataFormat), getPlaybackGain());
pa_stream_write(s, data, urgentBytes, NULL, 0, PA_SEEK_RELATIVE);
// Consume the regular one as well (same amount of bytes)
Manager::instance().getMainBuffer()->discard(urgentBytes);
......@@ -343,6 +344,7 @@ void PulseLayer::writeToSpeaker()
if (playback_->isReady()) {
pa_stream_begin_write(s, &data, &bytes);
toneToPlay->getNext((SFLDataFormat*)data, bytes / sizeof(SFLDataFormat), 100);
applyGain(static_cast<SFLDataFormat *>(data), bytes / sizeof(SFLDataFormat), getPlaybackGain());
pa_stream_write(s, data, bytes, NULL, 0, PA_SEEK_RELATIVE);
}
......@@ -388,11 +390,14 @@ void PulseLayer::writeToSpeaker()
if (resample) {
SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc(outBytes);
converter_->resample((SFLDataFormat*)data, rsmpl_out, mainBufferSampleRate, audioSampleRate_, inSamples);
applyGain(rsmpl_out, outBytes / sizeof(SFLDataFormat), getPlaybackGain());
pa_stream_write(s, rsmpl_out, outBytes, NULL, 0, PA_SEEK_RELATIVE);
pa_xfree(rsmpl_out);
} else
} else {
applyGain(static_cast<SFLDataFormat *>(data), inBytes / sizeof(SFLDataFormat), getPlaybackGain());
pa_stream_write(s, data, inBytes, NULL, 0, PA_SEEK_RELATIVE);
}
}
void PulseLayer::readFromMic()
{
......@@ -455,8 +460,10 @@ void PulseLayer::ringtoneToSpeaker()
pa_stream_begin_write(s, &data, &bytes);
AudioLoop *fileToPlay = Manager::instance().getTelephoneFile();
if (fileToPlay)
if (fileToPlay) {
fileToPlay->getNext((SFLDataFormat *) data, bytes / sizeof(SFLDataFormat), 100);
applyGain(static_cast<SFLDataFormat *>(data), bytes / sizeof(SFLDataFormat), getPlaybackGain());
}
else
memset(data, 0, bytes);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment