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

I tried to fix the sound, but nah, it's noisy...
parent b30dfc99
No related branches found
No related tags found
No related merge requests found
...@@ -35,7 +35,6 @@ AudioLayer::AudioLayer(ManagerImpl& manager) ...@@ -35,7 +35,6 @@ AudioLayer::AudioLayer(ManagerImpl& manager)
, _stream(NULL), _manager(manager) , _stream(NULL), _manager(manager)
{ {
portaudio::System::initialize(); portaudio::System::initialize();
listDevices();
} }
// Destructor // Destructor
...@@ -180,41 +179,44 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer, ...@@ -180,41 +179,44 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer,
int16 *in = (int16 *) inputBuffer; int16 *in = (int16 *) inputBuffer;
int16 *out = (int16 *) outputBuffer; int16 *out = (int16 *) outputBuffer;
int toGet, toPut, urgentAvail, normalAvail, micAvailPut; int toGet, toPut;
int urgentAvail, // number of int16 right and int16 left
urgentAvail = _urgentRingBuffer.AvailForGet(); normalAvail, // number of int16 right and int16 left
micAvailPut;
// 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(char) * CHANNELS;
urgentAvail = _urgentRingBuffer.AvailForGet() / NBCHARFORTWOINT16;
if (urgentAvail > 0) { if (urgentAvail > 0) {
// Urgent data (dtmf, incoming call signal) come first. // Urgent data (dtmf, incoming call signal) come first.
if (urgentAvail < (int)framesPerBuffer) { toGet = (urgentAvail < (int)framesPerBuffer) ? urgentAvail : framesPerBuffer;
toGet = urgentAvail; _urgentRingBuffer.Get(out, toGet * NBCHARFORTWOINT16, _manager.getSpkrVolume());
} else {
toGet = framesPerBuffer;
}
_urgentRingBuffer.Get(out, SAMPLES_SIZE(toGet), _manager.getSpkrVolume());
// Consume the regular one as well (same amount of bytes) // Consume the regular one as well (same amount of bytes)
_mainSndRingBuffer.Discard(SAMPLES_SIZE(toGet)); _mainSndRingBuffer.Discard(toGet * NBCHARFORTWOINT16);
} }
else { else {
// If nothing urgent, play the regular sound samples // If nothing urgent, play the regular sound samples
normalAvail = _mainSndRingBuffer.AvailForGet() / (MIC_CHANNELS * SAMPLE_BYTES); normalAvail = _mainSndRingBuffer.AvailForGet() / NBCHARFORTWOINT16;
toGet = (normalAvail < (int)framesPerBuffer) ? normalAvail : framesPerBuffer; toGet = (normalAvail < (int)framesPerBuffer) ? normalAvail : framesPerBuffer;
// MIC_CHANNELS * SAMPLE_BYTES
//_debug("mainsndringbuffer.get: %d vs %d : %d\n", normalAvail, (int)framesPerBuffer, toGet); //_debug("mainsndringbuffer.get: %d vs %d : %d\n", normalAvail, (int)framesPerBuffer, toGet);
if (toGet) { if (toGet) {
_mainSndRingBuffer.Get(out, SAMPLES_SIZE(toGet), _manager.getSpkrVolume()); _mainSndRingBuffer.Get(out, toGet*NBCHARFORTWOINT16, _manager.getSpkrVolume());
} else { } else {
toGet = SAMPLES_SIZE(framesPerBuffer); toGet = framesPerBuffer * NBCHARFORTWOINT16;
_mainSndRingBuffer.PutZero(toGet); _mainSndRingBuffer.PutZero(toGet);
_mainSndRingBuffer.Get(out, toGet, 100); _mainSndRingBuffer.Get(out, toGet, 100);
} }
} }
// Additionally handle the mike's audio stream // Additionally handle the mic's audio stream
short micVolume = _manager.getMicVolume();
micAvailPut = _micRingBuffer.AvailForPut(); micAvailPut = _micRingBuffer.AvailForPut();
toPut = (micAvailPut <= (int)framesPerBuffer) ? micAvailPut : framesPerBuffer; toPut = (micAvailPut <= (int)framesPerBuffer) ? micAvailPut : framesPerBuffer;
_micRingBuffer.Put(in, SAMPLES_SIZE(toPut), _manager.getMicVolume()); _micRingBuffer.Put(in, SAMPLES_SIZE(toPut), micVolume );
return paContinue; return paContinue;
} }
......
...@@ -103,6 +103,7 @@ RingBuffer::Put(void* buffer, int toCopy, unsigned short volume) { ...@@ -103,6 +103,7 @@ RingBuffer::Put(void* buffer, int toCopy, unsigned short volume) {
for (int i=0; i < int16len; i++) { src16[i] = src16[i] * volume / 100; } for (int i=0; i < int16len; i++) { src16[i] = src16[i] * volume / 100; }
} }
// bcopy(src, dest, len) // bcopy(src, dest, len)
//fprintf(stderr, "has %d put %d\t", len, block);
bcopy (src, mBuffer + pos, block); bcopy (src, mBuffer + pos, block);
src += block; src += block;
pos = (pos + block) % mBufferSize; pos = (pos + block) % mBufferSize;
...@@ -153,6 +154,7 @@ RingBuffer::Get(void *buffer, int toCopy, unsigned short volume) { ...@@ -153,6 +154,7 @@ RingBuffer::Get(void *buffer, int toCopy, unsigned short volume) {
} }
// bcopy(src, dest, len) // bcopy(src, dest, len)
bcopy (mBuffer + mStart, dest, block); bcopy (mBuffer + mStart, dest, block);
//fprintf(stderr, "has %d get %d\t", len, block);
//_debug("get %d chars at address %ld, mBufferSize=%d, toCopy=%d\n", block, mBuffer+mStart, mBufferSize, toCopy); //_debug("get %d chars at address %ld, mBufferSize=%d, toCopy=%d\n", block, mBuffer+mStart, mBufferSize, toCopy);
dest += block; dest += block;
mStart = (mStart + block) % mBufferSize; mStart = (mStart + block) % mBufferSize;
......
...@@ -60,29 +60,26 @@ ToneThread::run (void) { ...@@ -60,29 +60,26 @@ ToneThread::run (void) {
bool started = false; bool started = false;
// How long do 'size' samples play ? // How long do 'size' samples play ?
unsigned int play_time = (_size * 1000) / SAMPLING_RATE - 10; unsigned int play_time = (_size * 1000) / SAMPLING_RATE - 20;
ManagerImpl& manager = Manager::instance(); ManagerImpl& manager = Manager::instance();
manager.getAudioDriver()->flushMain(); manager.getAudioDriver()->flushMain();
// this loop can be outside the stream, since we put the volume inside the ringbuffer // this loop can be outside the stream, since we put the volume inside the ringbuffer
// Create a new stereo buffer
// Push the tone to the audio FIFO
for (int j = 0; j < _size; j++) { for (int j = 0; j < _size; j++) {
k = j<<1; // channels is 2 (global.h) k = j<<1; // channels is 2 (global.h)
// split in two // split in two
buf_ctrl_vol[k] = buf_ctrl_vol[k+1] = buffer[j]; buf_ctrl_vol[k] = buf_ctrl_vol[k+1] = buffer[j];
// * spkrVolume/100;
} }
// Create a new stereo buffer with the volume adjusted
// spkrVolume = manager.getSpkrVolume();
// Push the tone to the audio FIFO
// size = number of int16 * 2 (two channels) * // size = number of int16 * 2 (two channels) *
// int16 are the buf_ctrl_vol // int16 are the buf_ctrl_vol
// unsigned char are the sample_ptr inside ringbuffer // unsigned char are the sample_ptr inside ringbuffer
int size_in_char = _size * 2 * (sizeof(int16)/sizeof(unsigned char)); int size_in_char = _size * 2 * (sizeof(int16)/sizeof(unsigned char));
_debug(" size : %d\t size_in_char : %d\n", _size, size_in_char); //_debug(" size : %d\t size_in_char : %d\n", _size, size_in_char);
while (!testCancel()) { while (!testCancel()) {
manager.getAudioDriver()->putMain(buf_ctrl_vol, size_in_char); manager.getAudioDriver()->putMain(buf_ctrl_vol, size_in_char);
......
...@@ -48,7 +48,4 @@ typedef short int16; ...@@ -48,7 +48,4 @@ typedef short int16;
#define SAMPLING_RATE 8000 #define SAMPLING_RATE 8000
#define SIZEBUF 1024*1024 #define SIZEBUF 1024*1024
#define FORMAT 4 #define FORMAT 4
#define OCTETS SAMPLING_RATE * FORMAT // Number of writen
// bytes in buffer
#endif // __GLOBAL_H__ #endif // __GLOBAL_H__
...@@ -510,36 +510,44 @@ ManagerImpl::playDtmf(char code) ...@@ -510,36 +510,44 @@ ManagerImpl::playDtmf(char code)
{ {
stopTone(); stopTone();
int16* _buf = new int16[SIZEBUF]; // length in milliseconds
int pulselen = getConfigInt(SIGNALISATION, PULSE_LENGTH);
if (!pulselen) { return false; }
// numbers of int = length in milliseconds / 1000 (number of seconds)
// = number of seconds * SAMPLING_RATE by SECONDS
int size = pulselen * (SAMPLING_RATE/1000);
// this buffer is for mono
int16* _buf = new int16[size];
bool returnValue = false; bool returnValue = false;
// Handle dtmf // Handle dtmf
_key.startTone(code); _key.startTone(code);
if ( _key.generateDTMF(_buf, SAMPLING_RATE) ) {
// copy the sound...
if ( _key.generateDTMF(_buf, size) ) {
int k; int k;
//int spkrVolume;
int16* buf_ctrl_vol;
// Determine dtmf pulse length
int pulselen = getConfigInt(SIGNALISATION, PULSE_LENGTH);
int size = pulselen * (OCTETS /1000);
buf_ctrl_vol = new int16[size*CHANNELS]; // allocation of more space, for stereo conversion
//spkrVolume = getSpkrVolume(); int16* buf_ctrl_vol = new int16[size*CHANNELS];
// Control volume and format mono->stereo // Control volume and format mono->stereo
for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) {
k = j<<1; // fast multiply by two k = j<<1; // fast multiplication by two
buf_ctrl_vol[k] = buf_ctrl_vol[k+1] = _buf[j]; buf_ctrl_vol[k] = buf_ctrl_vol[k+1] = _buf[j];
// * spkrVolume/100;
} }
_toneMutex.enterMutex();
AudioLayer *audiolayer = getAudioDriver(); AudioLayer *audiolayer = getAudioDriver();
_toneMutex.enterMutex();
audiolayer->urgentRingBuffer().flush(); audiolayer->urgentRingBuffer().flush();
// Put buffer to urgentRingBuffer // Put buffer to urgentRingBuffer
audiolayer->urgentRingBuffer().Put(buf_ctrl_vol, size * CHANNELS); // put the size in bytes...
// so size * CHANNELS * 2 (bytes for the int16)
int nbInt16InChar = sizeof(int16)/sizeof(char);
audiolayer->urgentRingBuffer().Put(buf_ctrl_vol, size * CHANNELS * nbInt16InChar);
// We activate the stream if it's not active yet. // We activate the stream if it's not active yet.
if (!audiolayer->isStreamActive()) { if (!audiolayer->isStreamActive()) {
...@@ -548,7 +556,7 @@ ManagerImpl::playDtmf(char code) ...@@ -548,7 +556,7 @@ ManagerImpl::playDtmf(char code)
audiolayer->urgentRingBuffer().flush(); audiolayer->urgentRingBuffer().flush();
audiolayer->stopStream(); audiolayer->stopStream();
} else { } else {
audiolayer->sleep(pulselen); audiolayer->sleep(pulselen); // in milliseconds
} }
_toneMutex.leaveMutex(); _toneMutex.leaveMutex();
//setZonetone(false); //setZonetone(false);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment