Commit 6aceecc3 authored by Tristan Matthews's avatar Tristan Matthews

* #7097: cleanup, removed redundant code in audio backend

parent 6456ab37
......@@ -37,23 +37,21 @@
#include <cstring>
#include <cassert>
AudioLoop::AudioLoop() :_buffer (0), _size (0), _pos (0), _sampleRate (0)
AudioLoop::AudioLoop() : buffer_(0), size_(0), pos_(0), sampleRate_(0)
{
}
AudioLoop::~AudioLoop()
{
delete [] _buffer;
delete [] buffer_;
}
void
AudioLoop::getNext (SFLDataFormat* output, size_t total_samples, short volume)
{
size_t pos = _pos;
size_t pos = pos_;
assert(_size);
if(_size == 0) {
if (size_ == 0) {
_error("AudioLoop: Error: Audio loop size is 0");
return;
}
......@@ -61,26 +59,26 @@ AudioLoop::getNext (SFLDataFormat* output, size_t total_samples, short volume)
while (total_samples) {
size_t samples = total_samples;
if (samples > (_size-pos)) {
samples = _size-pos;
}
if (samples > (size_ - pos))
samples = size_ - pos;
memcpy(output, _buffer+pos, samples*sizeof (SFLDataFormat)); // short>char conversion
// short->char conversion
memcpy(output, buffer_ + pos, samples * sizeof(SFLDataFormat));
if (volume!=100) {
for (size_t i=0; i<samples; i++) {
*output = (*output * volume) /100;
if (volume != 100) {
double gain = volume * 0.01;
for (size_t i = 0; i < samples; i++) {
*output *= gain;
output++;
}
} else {
} else
output += samples; // this is the destination...
}
pos = (pos + samples) % _size;
pos = (pos + samples) % size_;
total_samples -= samples;
}
_pos = pos;
pos_ = pos;
}
......@@ -61,36 +61,36 @@ class AudioLoop
* @param nb of int16 to send
* @param volume The volume
*/
void getNext (SFLDataFormat* output, size_t samples, short volume=100);
void getNext(SFLDataFormat* output, size_t samples, short volume=100);
/**
* Reset the pointer position
*/
void reset() {
_pos = 0;
pos_ = 0;
}
/**
* Accessor to the size of the buffer
* @return unsigned int The size
*/
size_t getSize() {
return _size;
size_t getSize() const {
return size_;
}
protected:
/** The data buffer */
SFLDataFormat* _buffer;
SFLDataFormat* buffer_;
/** Number of samples inside the buffer */
size_t _size;
size_t size_;
/** current position, set to 0, when initialize */
size_t _pos;
size_t pos_;
/** Sample rate */
unsigned int _sampleRate;
unsigned int sampleRate_;
private:
......
......@@ -73,17 +73,9 @@ AudioRecord::AudioRecord() : fp (NULL)
AudioRecord::~AudioRecord()
{
if (mixBuffer_) {
delete [] mixBuffer_;
}
if (micBuffer_) {
delete [] micBuffer_;
}
if (spkBuffer_) {
delete [] spkBuffer_;
}
delete [] mixBuffer_;
delete [] micBuffer_;
delete [] spkBuffer_;
}
void AudioRecord::setSndSamplingRate (int smplRate)
......@@ -96,16 +88,12 @@ int AudioRecord::getSndSamplingRate() const
return sndSmplRate_;
}
void AudioRecord::setRecordingOption (FILE_TYPE type, SOUND_FORMAT format, int sndSmplRate, std::string path)
void AudioRecord::setRecordingOption(FILE_TYPE type, int sndSmplRate, const std::string &path)
{
fileType_ = type;
sndFormat_ = format;
channels_ = 1;
sndSmplRate_ = sndSmplRate;
savePath_ = path + "/";
}
......@@ -309,11 +297,6 @@ bool AudioRecord::setRawFile()
return false;
}
if (sndFormat_ != INT16) { // TODO need to change INT16 to SINT16
sndFormat_ = INT16;
_debug ("AudioRecord::setRawFile() : using 16-bit signed integer data format for file.");
}
_debug ("AudioRecord:setRawFile() : created RAW file.");
return true;
......@@ -342,9 +325,7 @@ bool AudioRecord::setWavFile()
hdr.num_chans = channels_;
if (sndFormat_ == INT16) { // TODO need to write INT16 to SINT16
hdr.bits_per_samp = 16;
}
hdr.bits_per_samp = 16;
hdr.bytes_per_samp = (SINT16) (channels_ * hdr.bits_per_samp / 8);
......@@ -499,15 +480,11 @@ void AudioRecord::recData (SFLDataFormat* buffer, int nSamples)
return;
}
if (sndFormat_ == INT16) { // TODO change INT16 to SINT16
if (fwrite (buffer, sizeof (SFLDataFormat), nSamples, fp) != (unsigned int) nSamples)
_warn ("AudioRecord: Could not record data! ");
else {
fflush (fp);
byteCounter_ += (unsigned long) (nSamples*sizeof (SFLDataFormat));
}
if (fwrite (buffer, sizeof (SFLDataFormat), nSamples, fp) != (unsigned int) nSamples)
_warn ("AudioRecord: Could not record data! ");
else {
fflush (fp);
byteCounter_ += (unsigned long) (nSamples*sizeof (SFLDataFormat));
}
}
......@@ -527,25 +504,16 @@ void AudioRecord::recData (SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int
return;
}
for (int k = 0; k < nSamples_1; k++) {
mixBuffer_[k] = (buffer_1[k]+buffer_2[k]);
if (sndFormat_ == INT16) { // TODO change INT16 to SINT16
for (int k=0; k<nSamples_1; k++) {
mixBuffer_[k] = (buffer_1[k]+buffer_2[k]);
if (fwrite (&mixBuffer_[k], 2, 1, fp) != 1)
_warn ("AudioRecord: Could not record data!");
else {
fflush (fp);
}
}
if (fwrite (&mixBuffer_[k], 2, 1, fp) != 1)
_warn ("AudioRecord: Could not record data!");
else
fflush (fp);
}
byteCounter_ += (unsigned long) (nSamples_1*sizeof (SFLDataFormat));
byteCounter_ += (unsigned long) (nSamples_1 * sizeof(SFLDataFormat));
}
return;
}
......@@ -40,25 +40,19 @@ class AudioRecord
{
public:
enum FILE_TYPE { FILE_RAW, FILE_WAV };
AudioRecord();
~AudioRecord();
/**
* Set the sampling rate for this recorder
*/
void setSndSamplingRate (int smplRate);
/**
* Get the recrding sampling rate
*/
void setSndSamplingRate(int smplRate);
/**
* Get the recrding sampling rate
*/
int getSndSamplingRate(void) const;
/**
* Set the recording option
*/
void setRecordingOption (FILE_TYPE type, SOUND_FORMAT format, int sndSmplRate, std::string path);
void setRecordingOption(FILE_TYPE type, int sndSmplRate, const std::string &path);
/**
* Init recording file path
......@@ -184,11 +178,6 @@ class AudioRecord
*/
FILE_TYPE fileType_;
/**
* Sound format (SINT16/SINT32)
*/
SOUND_FORMAT sndFormat_;
/**
* Number of channels
*/
......
......@@ -27,6 +27,8 @@
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "config.h"
#include "AudioZrtpSession.h"
#include "ZrtpSessionCallback.h"
......@@ -49,14 +51,13 @@ namespace sfl
{
AudioZrtpSession::AudioZrtpSession (SIPCall * sipcall, const std::string& zidFilename) :
// ost::SymmetricZRTPSession (ost::InetHostAddress (sipcall->getLocalIp().c_str()), sipcall->getLocalAudioPort()),
AudioRtpSession(sipcall, Zrtp, static_cast<ost::RTPDataQueue *>(this), static_cast<ost::Thread *>(this))
,ost::TRTPSessionBase<ost::SymmetricRTPChannel, ost::SymmetricRTPChannel, ost::ZrtpQueue> (ost::InetHostAddress (sipcall->getLocalIp().c_str()),
sipcall->getLocalAudioPort(),
0,
ost::MembershipBookkeeping::defaultMembersHashSize,
ost::defaultApplication())
, _zidFilename (zidFilename)
AudioRtpSession(sipcall, Zrtp, this, this),
ost::TRTPSessionBase<ost::SymmetricRTPChannel, ost::SymmetricRTPChannel, ost::ZrtpQueue>(ost::InetHostAddress(sipcall->getLocalIp().c_str()),
sipcall->getLocalAudioPort(),
0,
ost::MembershipBookkeeping::defaultMembersHashSize,
ost::defaultApplication()),
_zidFilename (zidFilename)
{
_debug ("AudioZrtpSession initialized");
initializeZid();
......@@ -96,7 +97,7 @@ void AudioZrtpSession::initializeZid (void)
// xdg_config = std::string (HOMEDIR) + DIR_SEPARATOR_STR + ".cache/sflphone";
std::string xdg_config = std::string (HOMEDIR) + DIR_SEPARATOR_STR + ".cache" + DIR_SEPARATOR_STR + PROGDIR + "/" + _zidFilename;
std::string xdg_config = std::string(HOMEDIR) + DIR_SEPARATOR_STR + ".cache" + DIR_SEPARATOR_STR + PACKAGE + "/" + _zidFilename;
_debug (" xdg_config %s", xdg_config.c_str());
......
......@@ -31,6 +31,7 @@
* as that of the covered work.
*/
#include "config.h"
#include "audiocodecfactory.h"
#include <cstdlib>
#include <algorithm> // for std::find
......@@ -142,7 +143,7 @@ std::vector<sfl::Codec*> AudioCodecFactory::scanCodecDirectory()
std::vector<sfl::Codec*> codecs;
std::vector<std::string> dirToScan;
dirToScan.push_back(std::string(HOMEDIR) + DIR_SEPARATOR_STR "." PROGDIR "/");
dirToScan.push_back(std::string(HOMEDIR) + DIR_SEPARATOR_STR "." PACKAGE "/");
dirToScan.push_back(CODECS_DIR "/");
const char *envDir = getenv("CODECS_PATH");
if (envDir)
......@@ -162,7 +163,7 @@ std::vector<sfl::Codec*> AudioCodecFactory::scanCodecDirectory()
dirent *dirStruct;
while ( (dirStruct = readdir (dir))) {
std::string file = dirStruct->d_name ;
if (file == CURRENT_DIR or file == PARENT_DIR)
if (file == "." or file == "..")
continue;
if (seemsValid (file) && !alreadyInCache (file)) {
......@@ -243,8 +244,9 @@ bool AudioCodecFactory::seemsValid (const std::string &lib)
{
// The name of the shared library seems valid <==> it looks like libcodec_xxx.so
// We check this
std::string prefix = SFL_CODEC_VALID_PREFIX;
std::string suffix = SFL_CODEC_VALID_EXTEN;
static const std::string prefix("libcodec_");
static const std::string suffix(".so");
ssize_t len = lib.length() - prefix.length() - suffix.length();
if (len < 0)
......@@ -257,17 +259,17 @@ bool AudioCodecFactory::seemsValid (const std::string &lib)
#ifndef HAVE_SPEEX_CODEC
if (lib.substr (prefix.length() , len) == SPEEX_STRING_DESCRIPTION)
if (lib.substr (prefix.length() , len) == "speex")
return false;
#endif
#ifndef HAVE_GSM_CODEC
if (lib.substr (prefix.length() , len) == GSM_STRING_DESCRIPTION)
if (lib.substr (prefix.length() , len) == "gsm")
return false;
#endif
#ifndef BUILD_ILBC
if (lib.substr (prefix.length() , len) == ILBC_STRING_DESCRIPTION)
if (lib.substr (prefix.length() , len) == "ilbc")
return false;
#endif
......
......@@ -32,13 +32,8 @@
Recordable::Recordable() : recorder (&recAudio, Manager::instance().getMainBuffer())
{
_debug("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Recordable Constructor -=-=-=-=-=-=-=-=-=--=-=-=-");
FILE_TYPE fileType = FILE_WAV;
SOUND_FORMAT soundFormat = INT16;
recAudio.setRecordingOption (fileType, soundFormat, 8000, Manager::instance().audioPreference.getRecordpath());
recAudio.setRecordingOption (AudioRecord::FILE_WAV, 8000, Manager::instance().audioPreference.getRecordpath());
}
......
......@@ -39,8 +39,8 @@ SamplerateConverter::SamplerateConverter (int freq) : _maxFreq(freq)
_samples = (freq * 20) / 1000; // start with 20 ms buffers
_floatBufferIn = new float32[_samples];
_floatBufferOut = new float32[_samples];
_floatBufferIn = new float[_samples];
_floatBufferOut = new float[_samples];
}
SamplerateConverter::~SamplerateConverter (void)
......@@ -61,7 +61,6 @@ SamplerateConverter::Short2FloatArray (const short *in, float *out, int len)
out[len] = (float) in[len] * .000030517578125f;
}
//TODO Add ifdef for int16 or float32 type
void SamplerateConverter::resample (SFLDataFormat* dataIn , SFLDataFormat* dataOut , int inputFreq , int outputFreq , int nbSamples)
{
double sampleFactor = (double) outputFreq / inputFreq;
......@@ -77,8 +76,8 @@ void SamplerateConverter::resample (SFLDataFormat* dataIn , SFLDataFormat* dataO
_samples = maxSamples;
delete [] _floatBufferIn;
delete [] _floatBufferOut;
_floatBufferIn = new float32[_samples];
_floatBufferOut = new float32[_samples];
_floatBufferIn = new float[_samples];
_floatBufferOut = new float[_samples];
}
SRC_DATA src_data;
......
......@@ -78,8 +78,8 @@ class SamplerateConverter
SamplerateConverter& operator= (const SamplerateConverter& rh);
/* temporary buffers */
float32* _floatBufferIn;
float32* _floatBufferOut;
float * _floatBufferIn;
float * _floatBufferOut;
size_t _samples; // size in samples of temporary buffers
int _maxFreq; // maximal output frequency
......
......@@ -73,51 +73,40 @@ RawFile::RawFile(const std::string& name, sfl::AudioCodec* codec, unsigned int s
SFLDataFormat *monoBuffer = new SFLDataFormat[decodedSize];
SFLDataFormat *bufpos = monoBuffer;
unsigned char *filepos = reinterpret_cast<unsigned char *>(fileBuffer);
_size = decodedSize;
while(length >= encFrameSize) {
size_ = decodedSize;
while (length >= encFrameSize) {
bufpos += audioCodec->decode (bufpos, filepos, encFrameSize);
filepos += encFrameSize;
length -= encFrameSize;
}
delete[] fileBuffer;
if (sampleRate == audioRate) {
#ifdef DATAFORMAT_IS_FLOAT
_buffer = new SFLDataFormat[_size];
src_short_to_float_array (monoBuffer, _buffer, _size);
delete[] monoBuffer;
#else
_buffer = monoBuffer;
#endif
} else {
delete [] fileBuffer;
if (sampleRate == audioRate)
buffer_ = monoBuffer;
else {
double factord = (double) sampleRate / audioRate;
float* floatBufferIn = new float[_size];
int sizeOut = ceil(factord*_size);
src_short_to_float_array (monoBuffer, floatBufferIn, _size);
delete[] monoBuffer;
SFLDataFormat* _buffer = new SFLDataFormat[sizeOut];
float* floatBufferIn = new float[size_];
int sizeOut = ceil(factord * size_);
src_short_to_float_array (monoBuffer, floatBufferIn, size_);
delete [] monoBuffer;
delete [] buffer_;
buffer_ = new SFLDataFormat[sizeOut];
SRC_DATA src_data;
src_data.data_in = floatBufferIn;
src_data.input_frames = _size;
src_data.input_frames = size_;
src_data.output_frames = sizeOut;
src_data.src_ratio = factord;
#ifdef DATAFORMAT_IS_FLOAT
src_data.data_out = _buffer;
src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1);
#else
float* floatBufferOut = new float[sizeOut];
src_data.data_out = floatBufferOut;
src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1);
src_float_to_short_array (floatBufferOut, _buffer, src_data.output_frames_gen);
src_float_to_short_array (floatBufferOut, buffer_, src_data.output_frames_gen);
delete [] floatBufferOut;
#endif
delete [] floatBufferIn;
_size = src_data.output_frames_gen;
size_ = src_data.output_frames_gen;
}
}
......@@ -148,8 +137,8 @@ WaveFile::WaveFile (const std::string& fileName, unsigned int audioSamplingRate)
SINT32 chunk_size; // fmt chunk size
unsigned short formatTag; // data compression tag
fileStream.read ( (char*) &chunk_size, 4); // Read fmt chunk size.
fileStream.read ( (char*) &formatTag, 2);
fileStream.read(reinterpret_cast<char*>(&chunk_size), 4); // Read fmt chunk size.
fileStream.read(reinterpret_cast<char*>(&formatTag), 2);
if (formatTag != 1) // PCM = 1, FLOAT = 3
throw AudioFileException("File contains an unsupported data format type");
......@@ -158,12 +147,12 @@ WaveFile::WaveFile (const std::string& fileName, unsigned int audioSamplingRate)
SINT16 chan;
fileStream.read ( (char*) &chan, 2);
if(chan > 2)
if (chan > 2)
throw AudioFileException("WaveFile: unsupported number of channels");
// Get file sample rate from the header.
SINT32 srate;
fileStream.read ( (char*) &srate, 4);
fileStream.read( (char*) &srate, 4);
SINT32 avgb;
fileStream.read ( (char*) &avgb, 4);
......@@ -185,7 +174,7 @@ WaveFile::WaveFile (const std::string& fileName, unsigned int audioSamplingRate)
// Sample rate converter initialized with 88200 sample long
int converterSamples = ((unsigned int)srate > audioSamplingRate) ? srate : audioSamplingRate;
SamplerateConverter _converter (converterSamples);
SamplerateConverter converter_(converterSamples);
// Get length of data from the header.
SINT32 bytes;
......@@ -197,31 +186,28 @@ WaveFile::WaveFile (const std::string& fileName, unsigned int audioSamplingRate)
nbSamples, bytes, blockal, srate, avgb, chunk_size, dt);
// Should not be longer than a minute
if (nbSamples > (unsigned int) (60*srate))
nbSamples = 60*srate;
if (nbSamples > static_cast<unsigned int>(60 * srate))
nbSamples = 60 * srate;
SFLDataFormat *tempBuffer = new SFLDataFormat[nbSamples];
if (!tempBuffer)
throw AudioFileException("Could not allocate temporary buffer");
fileStream.read ( (char *) tempBuffer, nbSamples*sizeof (SFLDataFormat));
fileStream.read (reinterpret_cast<char *>(tempBuffer), nbSamples * sizeof(SFLDataFormat));
// mix two channels together if stereo
if(chan == 2) {
for(unsigned int i = 0; i < nbSamples-1; i+=2)
tempBuffer[i/2] = (tempBuffer[i] + tempBuffer[i+1]) / 2;
nbSamples /= 2;
if (chan == 2) {
for (unsigned int i = 0; i < nbSamples - 1; i += 2)
tempBuffer[static_cast<size_t>(i * 0.5)] = (tempBuffer[i] + tempBuffer[i + 1]) * 0.5;
nbSamples *= 0.5;
}
if ((unsigned int)srate != audioSamplingRate) {
if ((unsigned int) srate != audioSamplingRate) {
int outSamples = ((float) nbSamples * ( (float) audioSamplingRate / (float) srate));
_buffer = new SFLDataFormat[outSamples];
_converter.resample (tempBuffer, _buffer, srate, audioSamplingRate, nbSamples);
delete[] tempBuffer;
} else {
_buffer = tempBuffer;
}
_size = nbSamples;
_sampleRate = audioSamplingRate;
buffer_ = new SFLDataFormat[outSamples];
converter_.resample (tempBuffer, buffer_, srate, audioSamplingRate, nbSamples);
delete [] tempBuffer;
} else
buffer_ = tempBuffer;
size_ = nbSamples;
sampleRate_ = audioSamplingRate;
}
......@@ -35,31 +35,26 @@
* YM: 2006-11-15: changes unsigned int to std::string::size_type, thanks to Pierre Pomes (AMD64 compilation)
*/
#include "tone.h"
#include <math.h>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define TABLE_LENGTH 4096
double TWOPI = 2 * M_PI;
static const double TWOPI = 2.0 * M_PI;
Tone::Tone (const std::string& definition, unsigned int sampleRate) : AudioLoop(), _sampleRate (sampleRate), _xhigher (0.0), _xlower (0.0)
Tone::Tone(const std::string& definition, unsigned int sampleRate) :
sampleRate_(sampleRate), xhigher_(0.0), xlower_(0.0)
{
fillWavetable();
genBuffer (definition); // allocate memory with definition parameter
}
Tone::~Tone()
{
genBuffer(definition); // allocate memory with definition parameter
}
void
Tone::genBuffer (const std::string& definition)
Tone::genBuffer(const std::string& definition)
{
if (definition.empty()) {
if (definition.empty())
return;
}
_size = 0;
size_ = 0;
SFLDataFormat* buffer = new SFLDataFormat[SIZEBUF]; //1kb
SFLDataFormat* bufferPos = buffer;
......@@ -76,67 +71,61 @@ Tone::genBuffer (const std::string& definition)
do {
posEnd = definition.find (',', posStart);
if (posEnd == std::string::npos) {
if (posEnd == std::string::npos)
posEnd = deflen;
}
/* begin scope */
{
// Sample string: "350+440" or "350+440/2000,244+655/2000"
int freq1, freq2, time;
s = definition.substr (posStart, posEnd-posStart);
s = definition.substr(posStart, posEnd-posStart);
// The 1st frequency is before the first + or the /
std::string::size_type pos_plus = s.find ('+');
std::string::size_type pos_slash = s.find ('/');
std::string::size_type len = s.length();
std::string::size_type endfrequency = 0;
size_t pos_plus = s.find('+');
size_t pos_slash = s.find('/');
size_t len = s.length();
size_t endfrequency = 0;
if (pos_slash == std::string::npos) {
time = 0;
endfrequency = len;
} else {
time = atoi ( (s.substr (pos_slash+1,len-pos_slash-1)).data());
time = atoi(s.substr(pos_slash + 1, len - pos_slash - 1).c_str());
endfrequency = pos_slash;
}
// without a plus = 1 frequency
if (pos_plus == std::string::npos) {
freq1 = atoi ( (s.substr (0,endfrequency)).data());
freq1 = atoi(s.substr(0, endfrequency).c_str());
freq2 = 0;
} else {
freq1 = atoi ( (s.substr (0,pos_plus)).data());
freq2 = atoi ( (s.substr (pos_plus+1, endfrequency-pos_plus-1)).data());
freq1 = atoi(s.substr(0, pos_plus).c_str());
freq2 = atoi(s.substr(pos_plus + 1, endfrequency - pos_plus - 1).c_str());
}
// If there is time or if it's unlimited
if (time == 0) {
count = _sampleRate;
} else {
count = (_sampleRate * time) / 1000;
}
if (time == 0)
count = sampleRate_;
else
count = (sampleRate_ * time) / 1000;
// Generate SAMPLING_RATE samples of sinus, buffer is the result
_debug ("genSin(%d, %d)", freq1, freq2);
genSin (bufferPos, freq1, freq2, count);
_debug("genSin(%d, %d)", freq1, freq2);
genSin(bufferPos, freq1, freq2, count);
// To concatenate the different buffers for each section.
_size += (count);
size_ += count;
bufferPos += count;
} /* end scope */
bufferPos += (count);
}
posStart = posEnd+1;
posStart = posEnd + 1;
} while (posStart < deflen);
_buffer = new SFLDataFormat[_size];
memcpy (_buffer, buffer, _size*sizeof (SFLDataFormat)); // copy char, not SFLDataFormat.
delete[] buffer;
buffer_ = new SFLDataFormat[size_];
buffer=0;
memcpy(buffer_, buffer, size_ * sizeof(SFLDataFormat)); // copy char, not SFLDataFormat.
bufferPos=0;
delete [] buffer;
}
void
......@@ -144,9 +133,8 @@ Tone::fillWavetable()
{
double tableSize = (double) TABLE_LENGTH;
for (int i = 0; i < TABLE_LENGTH; i++) {
_wavetable[i] = sin ( ( (double) i / (tableSize - 1.0)) * TWOPI);