Commit ff1cdf81 authored by Alexandre Savard's avatar Alexandre Savard

[#2209] Add a samplerate converter in PulseLayer

parent ba1d7499
......@@ -224,6 +224,7 @@ namespace sfl {
_spkrDataDecoded = new SFLDataFormat[nbSamplesMax];
_manager->addStream(_ca->getCallId());
_audiolayer->getMainBuffer()->setInternalSamplingRate(_layerSampleRate);
}
template <typename D>
......@@ -298,6 +299,8 @@ namespace sfl {
{
assert(_audiocodec);
assert(_audiolayer);
int _mainBufferSampleRate = _audiolayer->getMainBuffer()->getInternalSamplingRate();
// compute codec framesize in ms
float fixed_codec_framesize = computeCodecFrameSize (_audiocodec->getFrameSize(), _audiocodec->getClockRate());
......@@ -321,10 +324,10 @@ namespace sfl {
int compSize = 0;
// test if resampling is required
if (_audiocodec->getClockRate() != _layerSampleRate) {
if (_audiocodec->getClockRate() != _mainBufferSampleRate) {
int nb_sample_up = nbSample;
_nSamplesMic = nbSample;
nbSample = _converter->downsampleData (_micData , _micDataConverted , _audiocodec->getClockRate(), _layerSampleRate , nb_sample_up);
nbSample = _converter->downsampleData (_micData , _micDataConverted , _audiocodec->getClockRate(), _mainBufferSampleRate, nb_sample_up);
compSize = _audiocodec->codecEncode (_micDataEncoded, _micDataConverted, nbSample*sizeof (int16));
} else {
// no resampling required
......@@ -338,6 +341,9 @@ namespace sfl {
void AudioRtpSession<D>::processDataDecode(unsigned char * spkrData, unsigned int size, int& countTime)
{
if (_audiocodec != NULL) {
int _mainBufferSampleRate = _audiolayer->getMainBuffer()->getInternalSamplingRate();
// Return the size of data in bytes
int expandedSize = _audiocodec->codecDecode (_spkrDataDecoded , spkrData , size);
......@@ -345,11 +351,11 @@ namespace sfl {
int nbSample = expandedSize / sizeof (SFLDataFormat);
// test if resampling is required
if (_audiocodec->getClockRate() != _layerSampleRate) {
if (_audiocodec->getClockRate() != _mainBufferSampleRate) {
// Do sample rate conversion
int nb_sample_down = nbSample;
nbSample = _converter->upsampleData (_spkrDataDecoded, _spkrDataConverted, _codecSampleRate, _layerSampleRate , nb_sample_down);
nbSample = _converter->upsampleData (_spkrDataDecoded, _spkrDataConverted, _codecSampleRate, _mainBufferSampleRate, nb_sample_down);
// Store the number of samples for recording
_nSamplesSpkr = nbSample;
......
......@@ -47,6 +47,8 @@ PulseLayer::PulseLayer (ManagerImpl* manager)
PulseLayer::~PulseLayer (void)
{
closeLayer ();
delete _converter; _converter = NULL;
}
bool
......@@ -230,6 +232,8 @@ bool PulseLayer::openDevice (int indexIn UNUSED, int indexOut UNUSED, int sample
// startStream();
_converter = new SamplerateConverter (_sampleRate, _frameSize);
_debug ("Connection Done!! \n");
return true;
......@@ -399,8 +403,28 @@ void PulseLayer::writeToSpeaker (void)
if (toGet) {
int _mainBufferSampleRate = getMainBuffer()->getInternalSamplingRate();
_mainBuffer.getData (out, toGet, 100);
pa_stream_write (playback->pulseStream(), out, toGet, NULL, 0, PA_SEEK_RELATIVE);
// test if resampling is required
if (_sampleRate != _mainBufferSampleRate) {
SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc (framesPerBuffer * sizeof (SFLDataFormat));
// Do sample rate conversion
int nb_sample_down = toGet / sizeof(SFLDataFormat);
int nbSample = _converter->upsampleData((SFLDataFormat*)out, rsmpl_out, _mainBufferSampleRate, _sampleRate, nb_sample_down);
pa_stream_write (playback->pulseStream(), rsmpl_out, nbSample*sizeof(SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE);
pa_xfree (rsmpl_out);
} else {
pa_stream_write (playback->pulseStream(), out, toGet, NULL, 0, PA_SEEK_RELATIVE);
}
} else {
......@@ -425,7 +449,31 @@ void PulseLayer::readFromMic (void)
}
if (data != 0) {
_mainBuffer.putData ( (void*) data ,r, 100);
int _mainBufferSampleRate = getMainBuffer()->getInternalSamplingRate();
// test if resampling is required
if (_sampleRate != _mainBufferSampleRate) {
SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc (framesPerBuffer * sizeof (SFLDataFormat));
int nbSample = r / sizeof(SFLDataFormat);
int nb_sample_up = nbSample;
nbSample = _converter->downsampleData ((SFLDataFormat*)data, rsmpl_out, _mainBufferSampleRate, _sampleRate, nb_sample_up);
_mainBuffer.putData ( (void*) rsmpl_out, nbSample*sizeof(SFLDataFormat), 100);
pa_xfree (rsmpl_out);
} else {
// no resampling required
_mainBuffer.putData ( (void*) data, r, 100);
}
}
if (pa_stream_drop (record->pulseStream()) < 0) {
......
......@@ -21,6 +21,7 @@
#define _PULSE_LAYER_H
#include "audio/audiolayer.h"
#include "audio/samplerateconverter.h"
#include "audiostream.h"
#include <pulse/pulseaudio.h>
......@@ -189,6 +190,9 @@ class PulseLayer : public AudioLayer {
*/
AudioStream* record;
/** Sample rate converter object */
SamplerateConverter * _converter;
bool isCorked;
int spkrVolume;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment