Commit 9035c596 authored by Rafaël Carré's avatar Rafaël Carré

* #6611 : make sure samplerate converters are called correctly

Try to avoid confusion between size in bytes and number of samples
Use statically allocated buffers for decoders / encoders / filters / resamplers

We assume we won't be processing more than 20ms of audio mono at 44.1kHz
parent 706d0888
......@@ -292,7 +292,7 @@ class AlsaLayer : public AudioLayer
*/
void handle_xrun_playback (snd_pcm_t *handle);
void* adjustVolume (void* buffer , int len, int stream);
void adjustVolume (SFLDataFormat* buffer , int samples, int volume);
/**
* Handles to manipulate playback stream
......
......@@ -46,20 +46,19 @@ AudioLoop::~AudioLoop()
_buffer = 0;
}
int
AudioLoop::getNext (SFLDataFormat* output, int nb, short volume)
void
AudioLoop::getNext (SFLDataFormat* output, int samples, short volume)
{
int copied = 0;
int block;
int pos = _pos;
if(_size == 0) {
_error("AudioLoop: Error: Audio loop size is 0");
return 0;
return;
}
while (nb) {
block = nb;
while (samples) {
block = samples;
if (block > (_size-pos)) {
block = _size-pos;
......@@ -79,13 +78,9 @@ AudioLoop::getNext (SFLDataFormat* output, int nb, short volume)
// should adjust sound here, in output???
pos = (pos + block) % _size;
nb -= block;
copied += block;
samples -= block;
}
_pos = pos;
return copied;
}
......@@ -60,9 +60,8 @@ class AudioLoop
* @param output The data buffer
* @param nb of int16 to send
* @param volume The volume
* @return the number of int16 sent (nb*2)
*/
int getNext (SFLDataFormat* output, int nb, short volume=100);
void getNext (SFLDataFormat* output, int samples, short volume=100);
/**
* Reset the pointer position
......
......@@ -95,21 +95,15 @@ class AudioRtpRecord
ost::Mutex audioCodecMutex;
int _codecPayloadType;
bool _hasDynamicPayloadType;
SFLDataFormat *_micData;
SFLDataFormat *_micDataConverted;
SFLDataFormat *_micDataEchoCancelled;
unsigned char *_micDataEncoded;
SFLDataFormat *_spkrDataDecoded;
SFLDataFormat *_spkrDataConverted;
SFLDataFormat decData[DEC_BUFFER_SIZE];
SFLDataFormat resampledData[DEC_BUFFER_SIZE];
unsigned char encodedData[DEC_BUFFER_SIZE];
SamplerateConverter *_converter;
int _codecSampleRate;
int _codecFrameSize;
int _converterSamplingRate;
EventQueue _eventQueue;
bool _micFadeInComplete;
bool _spkrFadeInComplete;
SFLDataFormat _micAmplFactor;
SFLDataFormat _spkrAmplFactor;
AudioProcessing *_audioProcess;
NoiseSuppress *_noiseSuppress;
ost::Mutex audioProcessMutex;
......@@ -162,30 +156,14 @@ class AudioRtpRecordHandler
return _audioRtpRecord._eventQueue.size();
}
SFLDataFormat *getMicData (void) {
return _audioRtpRecord._micData;
const unsigned char *getMicDataEncoded (void) const {
return _audioRtpRecord.encodedData;
}
SFLDataFormat *getMicDataConverted (void) const {
return _audioRtpRecord._micDataConverted;
}
unsigned char *getMicDataEncoded (void) const {
return _audioRtpRecord._micDataEncoded;
}
void init (void);
/**
* Allocate memory for RTP buffers and fill them with zeros
* @prereq Session codec needs to be initialized prior calling this method
*/
void initBuffers (void);
void initNoiseSuppress (void);
void updateNoiseSuppress (void);
/**
* Encode audio data from mainbuffer
*/
......@@ -199,7 +177,7 @@ class AudioRtpRecordHandler
/**
* Ramp In audio data to avoid audio click from peer
*/
bool fadeIn (SFLDataFormat *audio, int size, SFLDataFormat *factor);
void fadeIn (SFLDataFormat *audio, int size, SFLDataFormat *factor);
void setDtmfPayloadType(unsigned int payloadType) {
_audioRtpRecord._dtmfPayloadType = payloadType;
......
......@@ -38,6 +38,11 @@
#include "Codec.h"
// We assume all decoders will be fed 20ms of audio or less
// And we'll resample them to 44.1kHz or less
// Also assume mono
#define DEC_BUFFER_SIZE ((44100 * 20) / 1000)
namespace ost {
class PayloadFormat;
class DynamicPayloadFormat;
......
......@@ -388,7 +388,6 @@ bool AudioCodecFactory::isCodecLoaded (int payload)
std::vector <std::string> AudioCodecFactory::getCodecSpecifications (const int32_t& payload)
{
_debug ("CodecDescriptor: Gathering codec specifications for payload %i", payload);
std::vector<std::string> v;
......@@ -408,5 +407,4 @@ std::vector <std::string> AudioCodecFactory::getCodecSpecifications (const int32
ss.str ("");
return v;
}
......@@ -66,8 +66,8 @@ void SamplerateConverter::resample (SFLDataFormat* dataIn , SFLDataFormat* dataO
{
assert(outputFreq <= _maxFreq);
double sampleFactor = (double) inputFreq / outputFreq;
if (sampleFactor == 1)
double sampleFactor = (double) outputFreq / inputFreq;
if (sampleFactor == 1.0)
return;
int outSamples = nbSamples * sampleFactor;
......
......@@ -241,19 +241,9 @@ WaveFile::WaveFile (const std::string& fileName, unsigned int audioSamplingRate)
}
if (srate != audioSamplingRate) {
nbSamples = (int) ( (float) nbSamples * ( (float) audioSamplingRate / (float) srate));
_buffer = new SFLDataFormat[nbSamples];
if (_buffer == NULL) {
delete[] tempBuffer;
throw AudioFileException("Could not allocate buffer for audio");
}
if (srate < audioSamplingRate)
_converter.resample (tempBuffer, _buffer, srate, audioSamplingRate, nbSamples);
else if (srate > audioSamplingRate)
_converter.resample (tempBuffer, _buffer, audioSamplingRate, srate, nbSamples);
int outSamples = ((float) nbSamples * ( (float) audioSamplingRate / (float) srate));
_buffer = new SFLDataFormat[outSamples];
_converter.resample (tempBuffer, _buffer, srate, audioSamplingRate, nbSamples);
delete[] tempBuffer;
} else {
_buffer = tempBuffer;
......
This diff is collapsed.
......@@ -306,14 +306,10 @@ class IAXVoIPLink : public VoIPLink
/** Connection to audio card/device */
AudioLayer* audiolayer;
/** Mic-data related buffers */
SFLDataFormat* micData;
SFLDataFormat* micDataConverted;
unsigned char* micDataEncoded;
/** Speaker-data related buffers */
SFLDataFormat* spkrDataDecoded;
SFLDataFormat* spkrDataConverted;
/** encoder/decoder/resampler buffers */
SFLDataFormat decData[DEC_BUFFER_SIZE];
SFLDataFormat resampledData[DEC_BUFFER_SIZE];
unsigned char encodedData[DEC_BUFFER_SIZE];
/** Sample rate converter object */
SamplerateConverter* converter;
......
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