Skip to content
Snippets Groups Projects
Commit 63c501dd authored by yanmorin's avatar yanmorin
Browse files

Little improvement
parent f0049091
No related branches found
No related tags found
No related merge requests found
......@@ -34,7 +34,10 @@ AudioLayer::AudioLayer(ManagerImpl& manager)
, _micRingBuffer(SIZEBUF)
, _stream(NULL), _manager(manager)
{
_debugInit(" portaudio initialization...");
portaudio::System::initialize();
_debugInit(" portaudio end initialization.");
NBCHARFORTWOINT16 = sizeof(int16)/sizeof(unsigned char) * CHANNELS;
}
// Destructor
......@@ -83,7 +86,8 @@ AudioLayer::openDevice (int index)
2, portaudio::INT16, true,
portaudio::System::instance().deviceByIndex(index).defaultLowInputLatency(),
NULL);
// we could put 0 instead of FRAME_PER_BUFFER to be variable
portaudio::StreamParameters const params(inParams, outParams,
SAMPLING_RATE, FRAME_PER_BUFFER, paNoFlag);
......@@ -190,43 +194,39 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer,
int urgentAvail, // number of int16 right and int16 left
normalAvail, // number of int16 right and int16 left
micAvailPut;
unsigned short spkrVolume = _manager.getSpkrVolume();
unsigned short micVolume = _manager.getMicVolume();
// AvailForGet tell the number of chars inside the buffer
// framePerBuffer are the number of int16 for one channel (left)
// so we divise by short/char * 2 channels
int NBCHARFORTWOINT16 = sizeof(int16)/sizeof(unsigned char) * CHANNELS;
urgentAvail = _urgentRingBuffer.AvailForGet() / NBCHARFORTWOINT16;
framesPerBuffer *= NBCHARFORTWOINT16;
urgentAvail = _urgentRingBuffer.AvailForGet();
if (urgentAvail > 0) {
// Urgent data (dtmf, incoming call signal) come first.
toGet = (urgentAvail < (int)framesPerBuffer) ? urgentAvail : framesPerBuffer;
_urgentRingBuffer.Get(out, toGet * NBCHARFORTWOINT16, _manager.getSpkrVolume());
_urgentRingBuffer.Get(out, toGet, spkrVolume);
// Consume the regular one as well (same amount of bytes)
_mainSndRingBuffer.Discard(toGet * NBCHARFORTWOINT16);
_mainSndRingBuffer.Discard(toGet);
}
else {
// If nothing urgent, play the regular sound samples
normalAvail = _mainSndRingBuffer.AvailForGet() / NBCHARFORTWOINT16;
normalAvail = _mainSndRingBuffer.AvailForGet();
toGet = (normalAvail < (int)framesPerBuffer) ? normalAvail : framesPerBuffer;
// _debug("mainsndringbuffer.get: %d vs %d : %d\n", normalAvail, (int)framesPerBuffer, toGet);
// fprintf(stderr, "%p:%d:%d:%ud\t",out,toGet*NBCHARFORTWOINT16,normalAvail,framesPerBuffer);
if (toGet) {
_mainSndRingBuffer.Get(out, toGet*NBCHARFORTWOINT16, _manager.getSpkrVolume());
_mainSndRingBuffer.Get(out, toGet, spkrVolume);
} else {
toGet = framesPerBuffer * NBCHARFORTWOINT16;
//fprintf(stderr, "put zero... %d (in bytes)\n", toGet);
toGet = framesPerBuffer;
_mainSndRingBuffer.PutZero(toGet);
_mainSndRingBuffer.Get(out, toGet, 100);
}
}
// Additionally handle the mic's audio stream
short micVolume = _manager.getMicVolume();
micAvailPut = _micRingBuffer.AvailForPut();
toPut = (micAvailPut <= (int)framesPerBuffer) ? micAvailPut : framesPerBuffer;
_micRingBuffer.Put(in, SAMPLES_SIZE(toPut), micVolume );
//fprintf(stderr, "|mic:%p|", in);
_micRingBuffer.Put(in, toPut, micVolume );
return paContinue;
}
......
......@@ -70,9 +70,10 @@ private:
RingBuffer _micRingBuffer;
portaudio::MemFunCallbackStream<AudioLayer> *_stream;
portaudio::AutoSystem autoSys;
// portaudio::AutoSystem autoSys;
ost::Mutex _mutex;
ManagerImpl& _manager;
int NBCHARFORTWOINT16;
};
#endif // _AUDIO_LAYER_H_
......
......@@ -35,14 +35,11 @@ RingBuffer::RingBuffer(int size) {
mStart = 0;
mEnd = 0;
mBuffer = new unsigned char[mBufferSize];
mBlank = new unsigned char[MIN_BUFFER_SIZE];
bzero(mBlank, MIN_BUFFER_SIZE);
assert (mBuffer != NULL);
}
// Free memory on object deletion
RingBuffer::~RingBuffer() {
delete[] mBlank; mBlank = NULL;
delete[] mBuffer; mBuffer = NULL;
}
......
......@@ -62,7 +62,6 @@ class RingBuffer {
int mEnd;
int mBufferSize;
samplePtr mBuffer;
samplePtr mBlank;
};
#endif /* __RING_BUFFER__ */
......
......@@ -19,11 +19,13 @@
#include "tcpstreampool.h"
#include "../../global.h"
#define WAITING_TIME 10UL
TCPStreamPool::~TCPStreamPool()
{
terminate();
std::string output;
while (good() && _outputPool.pop(output, 2LU)) {
while (good() && _outputPool.pop(output, WAITING_TIME)) {
*this << output << std::endl;
}
}
......@@ -35,7 +37,7 @@ TCPStreamPool::run() {
char cr13 = '\r'; // we don't want carriage return in empty line
while(!testCancel() && good()) {
while (isPending(ost::TCPSocket::pendingInput, 2LU)) {
while (isPending(ost::TCPSocket::pendingInput, WAITING_TIME)) {
std::getline(*this, input);
//_debug("TCPStreamPool getline %s\n", input.c_str());
if (input != null && input[0]!=cr13) {
......@@ -44,7 +46,7 @@ TCPStreamPool::run() {
// security check, since we are inside a loop
if (testCancel() || !good()) {break;}
}
if (_outputPool.pop(output, 2LU)) {
if (_outputPool.pop(output, WAITING_TIME)) {
//_debug("TCPStreamPool send %s\n", output.c_str());
*this << output << std::endl;
}
......@@ -62,7 +64,7 @@ TCPStreamPool::send(const std::string& response)
bool
TCPStreamPool::receive(std::string& request)
{
return _inputPool.pop(request, 2LU);
return _inputPool.pop(request, WAITING_TIME);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment