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

[#3946] Init audio buffers after malloc

parent 82e195f9
Branches
Tags
No related merge requests found
...@@ -64,6 +64,9 @@ AlsaLayer::AlsaLayer (ManagerImpl* manager) ...@@ -64,6 +64,9 @@ AlsaLayer::AlsaLayer (ManagerImpl* manager)
AudioLayer::_echocancelstate = true; AudioLayer::_echocancelstate = true;
AudioLayer::_noisesuppressstate = true; AudioLayer::_noisesuppressstate = true;
// captureFile = new ofstream ("probeFile", ofstream::binary);
} }
// Destructor // Destructor
...@@ -76,6 +79,9 @@ AlsaLayer::~AlsaLayer (void) ...@@ -76,6 +79,9 @@ AlsaLayer::~AlsaLayer (void)
delete _converter; delete _converter;
_converter = NULL; _converter = NULL;
} }
// captureFile->close();
// delete captureFile;
} }
bool bool
...@@ -95,6 +101,7 @@ AlsaLayer::closeLayer() ...@@ -95,6 +101,7 @@ AlsaLayer::closeLayer()
throw; throw;
} }
/* Then close the audio devices */ /* Then close the audio devices */
closeCaptureStream(); closeCaptureStream();
closePlaybackStream(); closePlaybackStream();
...@@ -934,6 +941,7 @@ void AlsaLayer::audioCallback (void) ...@@ -934,6 +941,7 @@ void AlsaLayer::audioCallback (void)
// Urgent data (dtmf, incoming call signal) come first. // Urgent data (dtmf, incoming call signal) come first.
toGet = (urgentAvailBytes < (int) (playbackAvailBytes)) ? urgentAvailBytes : playbackAvailBytes; toGet = (urgentAvailBytes < (int) (playbackAvailBytes)) ? urgentAvailBytes : playbackAvailBytes;
out = (SFLDataFormat*) malloc (toGet); out = (SFLDataFormat*) malloc (toGet);
memset (out, 0, toGet);
if (out) { if (out) {
_urgentRingBuffer.Get (out, toGet, spkrVolume); _urgentRingBuffer.Get (out, toGet, spkrVolume);
...@@ -953,6 +961,7 @@ void AlsaLayer::audioCallback (void) ...@@ -953,6 +961,7 @@ void AlsaLayer::audioCallback (void)
if (tone && (normalAvailBytes <= 0)) { if (tone && (normalAvailBytes <= 0)) {
out = (SFLDataFormat *) malloc (playbackAvailBytes); out = (SFLDataFormat *) malloc (playbackAvailBytes);
memset (out, 0, playbackAvailBytes);
if (out) { if (out) {
tone->getNext (out, playbackAvailSmpl, spkrVolume); tone->getNext (out, playbackAvailSmpl, spkrVolume);
...@@ -965,6 +974,7 @@ void AlsaLayer::audioCallback (void) ...@@ -965,6 +974,7 @@ void AlsaLayer::audioCallback (void)
} else if (file_tone && !_RingtoneHandle && (normalAvailBytes <= 0)) { } else if (file_tone && !_RingtoneHandle && (normalAvailBytes <= 0)) {
out = (SFLDataFormat *) malloc (playbackAvailBytes); out = (SFLDataFormat *) malloc (playbackAvailBytes);
memset (out, 0, playbackAvailBytes);
if (out) { if (out) {
file_tone->getNext (out, playbackAvailSmpl, spkrVolume); file_tone->getNext (out, playbackAvailSmpl, spkrVolume);
...@@ -996,6 +1006,7 @@ void AlsaLayer::audioCallback (void) ...@@ -996,6 +1006,7 @@ void AlsaLayer::audioCallback (void)
toGet = (normalAvailBytes < (int) maxNbBytesToGet) ? normalAvailBytes : maxNbBytesToGet; toGet = (normalAvailBytes < (int) maxNbBytesToGet) ? normalAvailBytes : maxNbBytesToGet;
out = (SFLDataFormat*) malloc (maxNbBytesToGet); out = (SFLDataFormat*) malloc (maxNbBytesToGet);
memset (out, 0, maxNbBytesToGet);
if (normalAvailBytes) { if (normalAvailBytes) {
...@@ -1012,6 +1023,8 @@ void AlsaLayer::audioCallback (void) ...@@ -1012,6 +1023,8 @@ void AlsaLayer::audioCallback (void)
if (rsmpl_out) { if (rsmpl_out) {
rsmpl_out = (SFLDataFormat*) malloc (playbackAvailBytes); rsmpl_out = (SFLDataFormat*) malloc (playbackAvailBytes);
memset (out, 0, playbackAvailBytes);
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);
write (rsmpl_out, nbSample*sizeof (SFLDataFormat), _PlaybackHandle); write (rsmpl_out, nbSample*sizeof (SFLDataFormat), _PlaybackHandle);
free (rsmpl_out); free (rsmpl_out);
...@@ -1139,6 +1152,7 @@ void AlsaLayer::audioCallback (void) ...@@ -1139,6 +1152,7 @@ void AlsaLayer::audioCallback (void)
if (filter_out) { if (filter_out) {
_audiofilter->processAudio (in, filter_out, toPut); _audiofilter->processAudio (in, filter_out, toPut);
// captureFile->write ( (const char *) filter_out, toPut);
// int sampleready = AudioLayer::_echoCanceller->processAudio (filter_out, echoCancelledMic, toPut); // int sampleready = AudioLayer::_echoCanceller->processAudio (filter_out, echoCancelledMic, toPut);
// getMainBuffer()->putData (echoCancelledMic, sampleready*sizeof (SFLDataFormat), 100); // getMainBuffer()->putData (echoCancelledMic, sampleready*sizeof (SFLDataFormat), 100);
getMainBuffer()->putData (filter_out, toPut, 100); getMainBuffer()->putData (filter_out, toPut, 100);
......
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#include "eventthread.h" #include "eventthread.h"
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
// #include <fstream>
class RingBuffer; class RingBuffer;
class ManagerImpl; class ManagerImpl;
...@@ -350,6 +352,8 @@ class AlsaLayer : public AudioLayer ...@@ -350,6 +352,8 @@ class AlsaLayer : public AudioLayer
/** Sample rate converter object */ /** Sample rate converter object */
SamplerateConverter* _converter; SamplerateConverter* _converter;
// ofstream *captureFile;
}; };
#endif // _ALSA_LAYER_H_ #endif // _ALSA_LAYER_H_
...@@ -33,9 +33,13 @@ ...@@ -33,9 +33,13 @@
#include "managerimpl.h" #include "managerimpl.h"
// #include <fstream>
int framesPerBuffer = 2048; int framesPerBuffer = 2048;
static void playback_callback (pa_stream* s, size_t bytes, void* userdata) static void playback_callback (pa_stream* s, size_t bytes, void* userdata)
{ {
...@@ -161,7 +165,7 @@ static void source_input_info_callback (pa_context *c UNUSED, const pa_source_in ...@@ -161,7 +165,7 @@ static void source_input_info_callback (pa_context *c UNUSED, const pa_source_in
} }
static void context_changed_callback (pa_context* c, pa_subscription_event_type_t t, uint32_t idx, void* userdata) static void context_changed_callback (pa_context* c, pa_subscription_event_type_t t, uint32_t idx UNUSED, void* userdata)
{ {
switch (t) { switch (t) {
...@@ -246,11 +250,10 @@ PulseLayer::PulseLayer (ManagerImpl* manager) ...@@ -246,11 +250,10 @@ PulseLayer::PulseLayer (ManagerImpl* manager)
byteCounter = 0; byteCounter = 0;
/*
captureFile = new ofstream("captureFile", ofstream::binary); // captureFile = new ofstream ("probeCaptureFile", ofstream::binary);
captureRsmplFile = new ofstream("captureRsmplFile", ofstream::binary); // spkrFile = new ofstream ("probeSpkrFile", ofstream::binary);
captureFilterFile = new ofstream("captureFilterFile", ofstream::binary);
*/
openLayer(); openLayer();
} }
...@@ -276,15 +279,12 @@ PulseLayer::~PulseLayer (void) ...@@ -276,15 +279,12 @@ PulseLayer::~PulseLayer (void)
delete AudioLayer::_audiofilter; delete AudioLayer::_audiofilter;
AudioLayer::_audiofilter = NULL; AudioLayer::_audiofilter = NULL;
/*
captureFile->close();
captureRsmplFile->close();
captureFilterFile->close();
delete captureFile; // captureFile->close();
delete captureRsmplFile; // spkrFile->close();
delete captureFilterFile;
*/ // delete captureFile;
// delete spkrFile;
} }
void void
...@@ -792,8 +792,11 @@ void PulseLayer::writeToSpeaker (void) ...@@ -792,8 +792,11 @@ void PulseLayer::writeToSpeaker (void)
if (urgentAvailBytes > writeableSize) { if (urgentAvailBytes > writeableSize) {
out = (SFLDataFormat*) pa_xmalloc (writeableSize); out = (SFLDataFormat*) pa_xmalloc (writeableSize);
memset (out, 0, writeableSize);
_urgentRingBuffer.Get (out, writeableSize, 100); _urgentRingBuffer.Get (out, writeableSize, 100);
// spkrFile->write ( (const char *) out, writeableSize);
pa_stream_write (playback->pulseStream(), out, writeableSize, NULL, 0, PA_SEEK_RELATIVE); pa_stream_write (playback->pulseStream(), out, writeableSize, NULL, 0, PA_SEEK_RELATIVE);
pa_xfree (out); pa_xfree (out);
...@@ -818,8 +821,11 @@ void PulseLayer::writeToSpeaker (void) ...@@ -818,8 +821,11 @@ void PulseLayer::writeToSpeaker (void)
if (playback->getStreamState() == PA_STREAM_READY) { if (playback->getStreamState() == PA_STREAM_READY) {
out = (SFLDataFormat*) pa_xmalloc (writeableSize); out = (SFLDataFormat*) pa_xmalloc (writeableSize);
memset (out, 0, writeableSize);
int copied = tone->getNext (out, writeableSize / sizeof (SFLDataFormat), 100); int copied = tone->getNext (out, writeableSize / sizeof (SFLDataFormat), 100);
//spkrFile->write ( (const char *) out, copied*sizeof (SFLDataFormat));
pa_stream_write (playback->pulseStream(), out, copied * sizeof (SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE); pa_stream_write (playback->pulseStream(), out, copied * sizeof (SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE);
pa_xfree (out); pa_xfree (out);
...@@ -857,6 +863,7 @@ void PulseLayer::writeToSpeaker (void) ...@@ -857,6 +863,7 @@ void PulseLayer::writeToSpeaker (void)
byteToGet = byteToGet-1; byteToGet = byteToGet-1;
out = (SFLDataFormat*) pa_xmalloc (maxNbBytesToGet); out = (SFLDataFormat*) pa_xmalloc (maxNbBytesToGet);
memset (out, 0, maxNbBytesToGet);
getMainBuffer()->getData (out, byteToGet, 100); getMainBuffer()->getData (out, byteToGet, 100);
...@@ -868,6 +875,7 @@ void PulseLayer::writeToSpeaker (void) ...@@ -868,6 +875,7 @@ void PulseLayer::writeToSpeaker (void)
if (_mainBufferSampleRate && ( (int) _audioSampleRate != _mainBufferSampleRate)) { if (_mainBufferSampleRate && ( (int) _audioSampleRate != _mainBufferSampleRate)) {
SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc (writeableSize); SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc (writeableSize);
memset (out, 0, writeableSize);
// Do sample rate conversion // Do sample rate conversion
int nb_sample_down = byteToGet / sizeof (SFLDataFormat); int nb_sample_down = byteToGet / sizeof (SFLDataFormat);
...@@ -877,14 +885,16 @@ void PulseLayer::writeToSpeaker (void) ...@@ -877,14 +885,16 @@ void PulseLayer::writeToSpeaker (void)
if ( (nbSample*sizeof (SFLDataFormat)) > (unsigned int) writeableSize) if ( (nbSample*sizeof (SFLDataFormat)) > (unsigned int) writeableSize)
_warn ("Audio: Error: nbsbyte exceed buffer length"); _warn ("Audio: Error: nbsbyte exceed buffer length");
// spkrFile->write ( (const char *) out, 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);
pa_xfree (rsmpl_out); pa_xfree (rsmpl_out);
} else { } else {
// spkrFile->write ( (const char *) out, byteToGet);
pa_stream_write (playback->pulseStream(), out, byteToGet, NULL, 0, PA_SEEK_RELATIVE); pa_stream_write (playback->pulseStream(), out, byteToGet, NULL, 0, PA_SEEK_RELATIVE);
} }
...@@ -898,9 +908,9 @@ void PulseLayer::writeToSpeaker (void) ...@@ -898,9 +908,9 @@ void PulseLayer::writeToSpeaker (void)
if (tone == 0) { if (tone == 0) {
SFLDataFormat* zeros = (SFLDataFormat*) pa_xmalloc (writeableSize); SFLDataFormat* zeros = (SFLDataFormat*) pa_xmalloc (writeableSize);
bzero (zeros, writeableSize); bzero (zeros, writeableSize);
// spkrFile->write ( (const char *) zeros, writeableSize);
pa_stream_write (playback->pulseStream(), zeros, writeableSize, NULL, 0, PA_SEEK_RELATIVE); pa_stream_write (playback->pulseStream(), zeros, writeableSize, NULL, 0, PA_SEEK_RELATIVE);
pa_xfree (zeros); pa_xfree (zeros);
...@@ -940,13 +950,12 @@ void PulseLayer::readFromMic (void) ...@@ -940,13 +950,12 @@ void PulseLayer::readFromMic (void)
if (_mainBufferSampleRate && ( (int) _audioSampleRate != _mainBufferSampleRate)) { if (_mainBufferSampleRate && ( (int) _audioSampleRate != _mainBufferSampleRate)) {
SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc (readableSize); SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc (readableSize);
memset (rsmpl_out, 0, readableSize);
int nbSample = r / sizeof (SFLDataFormat); int nbSample = r / sizeof (SFLDataFormat);
int nb_sample_up = nbSample; int nb_sample_up = nbSample;
// captureFile->write ((const char *)data, nbSample*sizeof(SFLDataFormat));
nbSample = _converter->downsampleData ( (SFLDataFormat *) data, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_up); nbSample = _converter->downsampleData ( (SFLDataFormat *) data, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_up);
// captureRsmplFile->write ((const char *)rsmpl_out, nbSample*sizeof(SFLDataFormat)); // captureRsmplFile->write ((const char *)rsmpl_out, nbSample*sizeof(SFLDataFormat));
...@@ -968,11 +977,15 @@ void PulseLayer::readFromMic (void) ...@@ -968,11 +977,15 @@ void PulseLayer::readFromMic (void)
} else { } else {
SFLDataFormat* filter_out = (SFLDataFormat*) pa_xmalloc (r); SFLDataFormat* filter_out = (SFLDataFormat*) pa_xmalloc (r);
memset (filter_out, 0, r);
// remove dc offset // remove dc offset
_audiofilter->processAudio ( (SFLDataFormat *) data, filter_out, r); _audiofilter->processAudio ( (SFLDataFormat *) data, filter_out, r);
// captureFile->write ( (const char *) filter_out, r);
// echo cancellation processing // echo cancellation processing
// int sampleready = _echoCanceller->processAudio((SFLDataFormat *)filter_out, echoCancelledMic, r); // int sampleready = _echoCanceller->processAudio((SFLDataFormat *)filter_out, echoCancelledMic, r);
...@@ -1007,6 +1020,8 @@ void PulseLayer::ringtoneToSpeaker (void) ...@@ -1007,6 +1020,8 @@ void PulseLayer::ringtoneToSpeaker (void)
if (ringtone->getStreamState() == PA_STREAM_READY) { if (ringtone->getStreamState() == PA_STREAM_READY) {
out = (SFLDataFormat *) pa_xmalloc (writableSize); out = (SFLDataFormat *) pa_xmalloc (writableSize);
memset (out, 0, writableSize);
int copied = file_tone->getNext (out, writableSize/sizeof (SFLDataFormat), 100); int copied = file_tone->getNext (out, writableSize/sizeof (SFLDataFormat), 100);
pa_stream_write (ringtone->pulseStream(), out, copied*sizeof (SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE); pa_stream_write (ringtone->pulseStream(), out, copied*sizeof (SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE);
...@@ -1018,6 +1033,7 @@ void PulseLayer::ringtoneToSpeaker (void) ...@@ -1018,6 +1033,7 @@ void PulseLayer::ringtoneToSpeaker (void)
out = (SFLDataFormat*) pa_xmalloc (writableSize); out = (SFLDataFormat*) pa_xmalloc (writableSize);
memset (out, 0, writableSize); memset (out, 0, writableSize);
pa_stream_write (ringtone->pulseStream(), out, writableSize, NULL, 0, PA_SEEK_RELATIVE); pa_stream_write (ringtone->pulseStream(), out, writableSize, NULL, 0, PA_SEEK_RELATIVE);
pa_xfree (out); pa_xfree (out);
......
...@@ -292,11 +292,9 @@ class PulseLayer : public AudioLayer ...@@ -292,11 +292,9 @@ class PulseLayer : public AudioLayer
int spkrVolume; int spkrVolume;
int micVolume; int micVolume;
/*
ofstream *captureFile; // ofstream *captureFile;
ofstream *captureRsmplFile; // ofstream *spkrFile;
ofstream *captureFilterFile;
*/
DeviceList _sinkList; DeviceList _sinkList;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment