From d82299d385915b6e492ce951bffef1d23aa2a6dd Mon Sep 17 00:00:00 2001
From: yanmorin <yanmorin>
Date: Tue, 18 Oct 2005 19:36:02 +0000
Subject: [PATCH] I tried to fix the sound, but nah, it's noisy...

---
 src/audio/audiolayer.cpp    | 42 +++++++++++++++++++------------------
 src/audio/ringbuffer.cpp    |  2 ++
 src/audio/tonegenerator.cpp | 11 ++++------
 src/global.h                |  3 ---
 src/managerimpl.cpp         | 38 ++++++++++++++++++++-------------
 5 files changed, 51 insertions(+), 45 deletions(-)

diff --git a/src/audio/audiolayer.cpp b/src/audio/audiolayer.cpp
index 7cb08167c5..76aaa634fd 100644
--- a/src/audio/audiolayer.cpp
+++ b/src/audio/audiolayer.cpp
@@ -35,7 +35,6 @@ AudioLayer::AudioLayer(ManagerImpl& manager)
   , _stream(NULL), _manager(manager)
 {
   portaudio::System::initialize();
-  listDevices();
 }
 
 // Destructor
@@ -113,7 +112,7 @@ AudioLayer::stopStream(void)
   if (_manager.isDriverLoaded()) {
     if (_stream && !_stream->isStopped()) {
       _debug("Thread: stop audiolayer stream\n");
-      _stream->stop();
+       _stream->stop();
       _mainSndRingBuffer.flush();
     }
   } 
@@ -180,41 +179,44 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer,
 	
 	int16 *in  = (int16 *) inputBuffer;
 	int16 *out = (int16 *) outputBuffer;
-	int toGet, toPut, urgentAvail, normalAvail, micAvailPut;
-
-	urgentAvail = _urgentRingBuffer.AvailForGet();
+	int toGet, toPut;
+  int urgentAvail, // number of int16 right and int16 left
+      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) {  
 	// Urgent data (dtmf, incoming call signal) come first.		
-		if (urgentAvail < (int)framesPerBuffer) {
-			toGet = urgentAvail;
-		} else {
-			toGet = framesPerBuffer;
-		}
-		_urgentRingBuffer.Get(out, SAMPLES_SIZE(toGet), _manager.getSpkrVolume());
+		toGet = (urgentAvail < (int)framesPerBuffer) ? urgentAvail : framesPerBuffer;
+		_urgentRingBuffer.Get(out, toGet * NBCHARFORTWOINT16, _manager.getSpkrVolume());
 		
 		// Consume the regular one as well (same amount of bytes)
-		_mainSndRingBuffer.Discard(SAMPLES_SIZE(toGet));
+		_mainSndRingBuffer.Discard(toGet * NBCHARFORTWOINT16);
 	}  
 	else {
 	// 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;
-    // MIC_CHANNELS * SAMPLE_BYTES
 
     //_debug("mainsndringbuffer.get: %d vs %d : %d\n", normalAvail, (int)framesPerBuffer, toGet);
     if (toGet) {
-		  _mainSndRingBuffer.Get(out, SAMPLES_SIZE(toGet), _manager.getSpkrVolume());
+		  _mainSndRingBuffer.Get(out, toGet*NBCHARFORTWOINT16, _manager.getSpkrVolume());
     } else {
-      toGet = SAMPLES_SIZE(framesPerBuffer);
+      toGet = framesPerBuffer * NBCHARFORTWOINT16;
       _mainSndRingBuffer.PutZero(toGet);
       _mainSndRingBuffer.Get(out, toGet, 100);
     }
 	}
 
-	// Additionally handle the mike's audio stream 
-	micAvailPut = _micRingBuffer.AvailForPut();
-	toPut = (micAvailPut <= (int)framesPerBuffer) ? micAvailPut : framesPerBuffer;
-	_micRingBuffer.Put(in, SAMPLES_SIZE(toPut), _manager.getMicVolume());
+	// 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 );
 
 	return paContinue;
 }
diff --git a/src/audio/ringbuffer.cpp b/src/audio/ringbuffer.cpp
index aa193b73b3..662f784c6a 100644
--- a/src/audio/ringbuffer.cpp
+++ b/src/audio/ringbuffer.cpp
@@ -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; }
       }
       // bcopy(src, dest, len)
+      //fprintf(stderr, "has %d put %d\t", len, block);
       bcopy (src, mBuffer + pos, block);
       src += block;
       pos = (pos + block) % mBufferSize;
@@ -153,6 +154,7 @@ RingBuffer::Get(void *buffer, int toCopy, unsigned short volume) {
       }
       // bcopy(src, dest, len)
       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);
       dest += block;
       mStart = (mStart + block) % mBufferSize;
diff --git a/src/audio/tonegenerator.cpp b/src/audio/tonegenerator.cpp
index 1c116b9946..4864a7ea18 100644
--- a/src/audio/tonegenerator.cpp
+++ b/src/audio/tonegenerator.cpp
@@ -60,29 +60,26 @@ ToneThread::run (void) {
 	bool started = false;
 
 	// 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();
   manager.getAudioDriver()->flushMain();
 
   // 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++) {
 		k = j<<1; // channels is 2 (global.h)
               // split in two
 		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) * 
   // int16 are the buf_ctrl_vol 
   //  unsigned char are the sample_ptr inside ringbuffer
-
   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()) {
     manager.getAudioDriver()->putMain(buf_ctrl_vol, size_in_char);
diff --git a/src/global.h b/src/global.h
index 021f6cd1a6..57cd515b87 100644
--- a/src/global.h
+++ b/src/global.h
@@ -48,7 +48,4 @@ typedef short int16;
 #define	SAMPLING_RATE 			8000
 #define SIZEBUF 				1024*1024
 #define	FORMAT					4			
-#define OCTETS					SAMPLING_RATE * FORMAT	// Number of writen 
-														// bytes in buffer
-
 #endif	// __GLOBAL_H__
diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 18bf93f100..e7f78f945b 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -510,36 +510,44 @@ ManagerImpl::playDtmf(char code)
 {
   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;
 
   // Handle dtmf
   _key.startTone(code);
-  if ( _key.generateDTMF(_buf, SAMPLING_RATE) ) {
 
+  // copy the sound...
+  if ( _key.generateDTMF(_buf, size) ) {
     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];
-    //spkrVolume = getSpkrVolume();
+    // allocation of more space, for stereo conversion
+    int16* buf_ctrl_vol = new int16[size*CHANNELS];
 
     // Control volume and format mono->stereo
     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];
-      // * spkrVolume/100;
     }
-    _toneMutex.enterMutex();
+
     AudioLayer *audiolayer = getAudioDriver();
+
+    _toneMutex.enterMutex();
     audiolayer->urgentRingBuffer().flush();
 
     // 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.
     if (!audiolayer->isStreamActive()) {
@@ -548,7 +556,7 @@ ManagerImpl::playDtmf(char code)
       audiolayer->urgentRingBuffer().flush();
       audiolayer->stopStream();
     } else {
-      audiolayer->sleep(pulselen);
+      audiolayer->sleep(pulselen); // in milliseconds
     }
     _toneMutex.leaveMutex();
     //setZonetone(false);
-- 
GitLab