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

[#2352] Use 50 sec latency on pulseaudio stream creation

This parameter may vary from 20 to 200 ms, the longer it is,
the more noticable the latency is.

Long latency however, reduce interups, which reduce CPU
parent af96f97f
No related branches found
No related tags found
No related merge requests found
...@@ -192,30 +192,23 @@ AudioStream::createStream (pa_context* c) ...@@ -192,30 +192,23 @@ AudioStream::createStream (pa_context* c)
// parameters are defined as number of bytes // parameters are defined as number of bytes
// 2048 bytes (1024 int16) is 20 ms at 44100 Hz // 2048 bytes (1024 int16) is 20 ms at 44100 Hz
if (_streamType == PLAYBACK_STREAM) { if (_streamType == PLAYBACK_STREAM) {
// 20 ms framesize TODO: take framesize value from config
attributes->maxlength = (uint32_t) -1; attributes->maxlength = (uint32_t) -1;
attributes->tlength = pa_usec_to_bytes(20 * PA_USEC_PER_MSEC, &sample_spec); attributes->tlength = pa_usec_to_bytes(50 * PA_USEC_PER_MSEC, &sample_spec);
attributes->prebuf = (uint32_t) -1; attributes->prebuf = (uint32_t) -1;
attributes->minreq = (uint32_t) -1; attributes->minreq = (uint32_t) -1;
attributes->fragsize = (uint32_t) -1; attributes->fragsize = (uint32_t) -1;
_debug("tlength: %i\n", pa_usec_to_bytes(20 * PA_USEC_PER_MSEC, &sample_spec));
/*
attributes->maxlength = 88200;
attributes->tlength = 4096;
attributes->prebuf = 4096;
attributes->minreq = 2048;
attributes->fragsize = 4096;
*/
pa_stream_connect_playback( s , NULL , attributes, PA_STREAM_ADJUST_LATENCY, &_volume, NULL); pa_stream_connect_playback( s , NULL , attributes, PA_STREAM_ADJUST_LATENCY, &_volume, NULL);
} else if (_streamType == CAPTURE_STREAM) { } else if (_streamType == CAPTURE_STREAM) {
// 20 ms framesize TODO: take framesize value from config
attributes->maxlength = (uint32_t) -1; attributes->maxlength = (uint32_t) -1;
attributes->tlength = (uint32_t) -1; attributes->tlength = (uint32_t) -1;
attributes->prebuf = (uint32_t) -1; attributes->prebuf = (uint32_t) -1;
attributes->minreq = (uint32_t) -1; attributes->minreq = (uint32_t) -1;
attributes->fragsize = 4096; attributes->fragsize = pa_usec_to_bytes(50 * PA_USEC_PER_MSEC, &sample_spec);
......
...@@ -546,22 +546,16 @@ void PulseLayer::writeToSpeaker (void) ...@@ -546,22 +546,16 @@ void PulseLayer::writeToSpeaker (void)
} else { } else {
int _mainBufferSampleRate = getMainBuffer()->getInternalSamplingRate(); int _mainBufferSampleRate = getMainBuffer()->getInternalSamplingRate();
// int maxNbSamplesToGet = 0;
int maxNbBytesToGet = 0; int maxNbBytesToGet = 0;
_debug("--------------- Playback -----------\n");
_debug("writeableSize: %i\n", writeableSize);
// test if audio resampling is needed // test if audio resampling is needed
if (_mainBufferSampleRate && ((int)_audioSampleRate != _mainBufferSampleRate)) { if (_mainBufferSampleRate && ((int)_audioSampleRate != _mainBufferSampleRate)) {
// upsamplefactor is used to compute the number of bytes to get in the ring buffer // upsamplefactor is used to compute the number of bytes to get in the ring buffer
double upsampleFactor = (double) _mainBufferSampleRate / _audioSampleRate; double upsampleFactor = (double) _mainBufferSampleRate / _audioSampleRate;
_debug("upsampleFactor: %f\n", upsampleFactor);
// maxNbSamplesToGet is the number of sample to get in the ring buffer which,
// once resampled, will not be over the writeableSize
// maxNbSamplesToGet = (int) ((double) framesPerBuffer * upsampleFactor);
maxNbBytesToGet = ((double) writeableSize * upsampleFactor); maxNbBytesToGet = ((double) writeableSize * upsampleFactor);
} else { } else {
...@@ -571,15 +565,10 @@ void PulseLayer::writeToSpeaker (void) ...@@ -571,15 +565,10 @@ void PulseLayer::writeToSpeaker (void)
} }
// maxNbBytesToGet = maxNbSamplesToGet * sizeof(SFLDataFormat);
_debug("maxNbBytesToGet: %i\n", maxNbBytesToGet);
out = (SFLDataFormat*) pa_xmalloc (maxNbBytesToGet); out = (SFLDataFormat*) pa_xmalloc (maxNbBytesToGet);
normalAvailBytes = _mainBuffer.availForGet(); normalAvailBytes = _mainBuffer.availForGet();
_debug("normalAvailBytes: %i\n", normalAvailBytes);
byteToGet = (normalAvailBytes < (int)(maxNbBytesToGet)) ? normalAvailBytes : maxNbBytesToGet; byteToGet = (normalAvailBytes < (int)(maxNbBytesToGet)) ? normalAvailBytes : maxNbBytesToGet;
_debug("byteToGet: %i\n", byteToGet);
if (byteToGet) { if (byteToGet) {
...@@ -597,12 +586,8 @@ void PulseLayer::writeToSpeaker (void) ...@@ -597,12 +586,8 @@ void PulseLayer::writeToSpeaker (void)
// Do sample rate conversion // Do sample rate conversion
int nb_sample_down = byteToGet / sizeof(SFLDataFormat); int nb_sample_down = byteToGet / sizeof(SFLDataFormat);
_debug("nbSampleDown: %i\n", nb_sample_down);
int nbSample = _converter->upsampleData((SFLDataFormat*)out, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_down); int nbSample = _converter->upsampleData((SFLDataFormat*)out, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_down);
_debug("nbSample converted: %i\n", nbSample);
_debug("bytes to be written: %i\n", nbSample*sizeof(SFLDataFormat));
pa_stream_write (playback->pulseStream(), rsmpl_out, nbSample*sizeof(SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE); pa_stream_write (playback->pulseStream(), rsmpl_out, nbSample*sizeof(SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE);
...@@ -618,24 +603,18 @@ void PulseLayer::writeToSpeaker (void) ...@@ -618,24 +603,18 @@ void PulseLayer::writeToSpeaker (void)
} else { } else {
if((tone == 0) && (file_tone == 0)) { if((tone == 0) && (file_tone == 0)) {
// _debug("maxNbBytesToGet: %i\n", maxNbBytesToGet);
SFLDataFormat* zeros = (SFLDataFormat*)pa_xmalloc (writeableSize); SFLDataFormat* zeros = (SFLDataFormat*)pa_xmalloc (writeableSize);
bzero (zeros, writeableSize); bzero (zeros, writeableSize);
pa_stream_write(playback->pulseStream(), zeros, writeableSize, pa_xfree, 0, PA_SEEK_RELATIVE); pa_stream_write(playback->pulseStream(), zeros, writeableSize, pa_xfree, 0, PA_SEEK_RELATIVE);
// pa_xfree (zeros);
} }
} }
_urgentRingBuffer.Discard(byteToGet); _urgentRingBuffer.Discard(byteToGet);
// pa_xfree (out);
} }
} }
...@@ -647,8 +626,6 @@ void PulseLayer::readFromMic (void) ...@@ -647,8 +626,6 @@ void PulseLayer::readFromMic (void)
const char* data = NULL; const char* data = NULL;
size_t r; size_t r;
// _debug("--------------- Capture -----------\n");
if (pa_stream_peek (record->pulseStream() , (const void**) &data , &r) < 0 || !data) { if (pa_stream_peek (record->pulseStream() , (const void**) &data , &r) < 0 || !data) {
_debug("pa_stream_peek() failed: %s\n" , pa_strerror( pa_context_errno( context) )); _debug("pa_stream_peek() failed: %s\n" , pa_strerror( pa_context_errno( context) ));
} }
...@@ -666,16 +643,12 @@ void PulseLayer::readFromMic (void) ...@@ -666,16 +643,12 @@ void PulseLayer::readFromMic (void)
int nbSample = r / sizeof(SFLDataFormat); int nbSample = r / sizeof(SFLDataFormat);
int nb_sample_up = nbSample; int nb_sample_up = nbSample;
// _debug("nbSample from mic: %i\n", nbSample);
nbSample = _converter->downsampleData ((SFLDataFormat*)data, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_up); nbSample = _converter->downsampleData ((SFLDataFormat*)data, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_up);
// remove dc offset // remove dc offset
dcblocker->filter_signal( rsmpl_out, nbSample ); dcblocker->filter_signal( rsmpl_out, nbSample );
// _debug("nbSample copied: %i\n", nbSample);
_mainBuffer.putData ( (void*) rsmpl_out, nbSample*sizeof(SFLDataFormat), 100); _mainBuffer.putData ( (void*) rsmpl_out, nbSample*sizeof(SFLDataFormat), 100);
pa_xfree (rsmpl_out); pa_xfree (rsmpl_out);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment