From f3f23ef0f3381f43463dbb41c44167af7c42ac17 Mon Sep 17 00:00:00 2001
From: yanmorin <yanmorin>
Date: Mon, 17 Oct 2005 14:46:29 +0000
Subject: [PATCH] Try to remove the sound bug for the tone But I don't
 understand there is one...

---
 src/audio/audiolayer.cpp    |  2 +-
 src/audio/ringbuffer.cpp    |  6 ++---
 src/audio/tonegenerator.cpp | 53 +++++++++++++++++++++----------------
 src/managerimpl.cpp         |  6 ++---
 4 files changed, 36 insertions(+), 31 deletions(-)

diff --git a/src/audio/audiolayer.cpp b/src/audio/audiolayer.cpp
index bf9fb95005..7cb08167c5 100644
--- a/src/audio/audiolayer.cpp
+++ b/src/audio/audiolayer.cpp
@@ -201,7 +201,7 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer,
 		toGet = (normalAvail < (int)framesPerBuffer) ? normalAvail : framesPerBuffer;
     // MIC_CHANNELS * SAMPLE_BYTES
 
-    //_debug("%d vs %d : %d\t", normalAvail, (int)framesPerBuffer, toGet);
+    //_debug("mainsndringbuffer.get: %d vs %d : %d\n", normalAvail, (int)framesPerBuffer, toGet);
     if (toGet) {
 		  _mainSndRingBuffer.Get(out, SAMPLES_SIZE(toGet), _manager.getSpkrVolume());
     } else {
diff --git a/src/audio/ringbuffer.cpp b/src/audio/ringbuffer.cpp
index d8f30deee4..aa193b73b3 100644
--- a/src/audio/ringbuffer.cpp
+++ b/src/audio/ringbuffer.cpp
@@ -20,7 +20,6 @@
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-
 #include <assert.h>
 #include <stdlib.h> 
 #include <string.h>  
@@ -94,7 +93,7 @@ RingBuffer::Put(void* buffer, int toCopy, unsigned short volume) {
 
    while(toCopy) {
       block = toCopy;
-      if (block > mBufferSize - pos) // from current pos. to end of buffer
+      if (block > (mBufferSize - pos)) // from current pos. to end of buffer
          block = mBufferSize - pos;
 
       // put the data inside the buffer.
@@ -105,7 +104,6 @@ RingBuffer::Put(void* buffer, int toCopy, unsigned short volume) {
       }
       // bcopy(src, dest, len)
       bcopy (src, mBuffer + pos, block);
-
       src += block;
       pos = (pos + block) % mBufferSize;
       toCopy -= block;
@@ -142,6 +140,7 @@ RingBuffer::Get(void *buffer, int toCopy, unsigned short volume) {
    dest = (samplePtr) buffer;
    copied = 0;
 
+   //fprintf(stderr, "get start... ");
    while(toCopy) {
       block = toCopy;
       if (block > (mBufferSize - mStart))
@@ -154,6 +153,7 @@ RingBuffer::Get(void *buffer, int toCopy, unsigned short volume) {
       }
       // bcopy(src, dest, len)
       bcopy (mBuffer + mStart, dest, block);
+      //_debug("get %d chars at address %ld, mBufferSize=%d, toCopy=%d\n", block, mBuffer+mStart, mBufferSize, toCopy);
       dest += block;
       mStart = (mStart + block) % mBufferSize;
       toCopy -= block;
diff --git a/src/audio/tonegenerator.cpp b/src/audio/tonegenerator.cpp
index 606fd5e691..31f83bd0bd 100644
--- a/src/audio/tonegenerator.cpp
+++ b/src/audio/tonegenerator.cpp
@@ -60,7 +60,7 @@ ToneThread::run (void) {
 	bool started = false;
 
 	// How long do 'size' samples play ?
-	unsigned int play_time = (size * 1000) / SAMPLING_RATE;
+	unsigned int play_time = (size * 1000) / SAMPLING_RATE - 10;
 
   ManagerImpl& manager = Manager::instance();
   manager.getAudioDriver()->flushMain();
@@ -73,15 +73,17 @@ ToneThread::run (void) {
     // * spkrVolume/100;
 	}
 
-		// Create a new stereo buffer with the volume adjusted
-		//spkrVolume = manager.getSpkrVolume();
-		// Push the tone to the audio FIFO
+  // 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);
+
  	while (!testCancel()) {
     manager.getAudioDriver()->putMain(buf_ctrl_vol, size_in_char);
 		// The first iteration will start the audio stream if not already.
@@ -176,9 +178,11 @@ ToneGenerator::generateSin (int lowerfreq, int higherfreq, int16* ptr, int len)
 													
 	var1 = (double)2 * (double)M_PI * (double)higherfreq / (double)SAMPLING_RATE; 
 	var2 = (double)2 * (double)M_PI * (double)lowerfreq / (double)SAMPLING_RATE;
+
+  double amp = (double)(AMPLITUDE >> 2);
 	
 	for(int t = 0; t < len; t++) {
-		ptr[t] = (int16)((double)(AMPLITUDE >> 1) * ((sin(var1 * t) + sin(var2 * t))));
+		ptr[t] = (int16)(amp * ((sin(var1 * t) + sin(var2 * t))));
 	}
 }
 
@@ -367,29 +371,32 @@ ToneGenerator::playRingtone (const char *fileName) {
 		return 0;
   	}
 
-	// get length of file:
-  	file.seekg (0, std::ios::end);
-  	length = file.tellg();
-  	file.seekg (0, std::ios::beg);
+  // get length of file:
+  file.seekg (0, std::ios::end);
+  length = file.tellg();
+  file.seekg (0, std::ios::beg);
 
-  	// allocate memory:
+    // allocate memory:
   _src = new char [length];
   _dst = new short[length*2];
-	
- 	// read data as a block:
- 	file.read (_src,length);
-	file.close();
-	
-	// Decode file.ul
-	expandedsize = _ulaw->codecDecode (_dst, (unsigned char *)_src, length);
 
-	if (tonethread == NULL) {
+  // read data as a block:
+  file.read (_src,length);
+  file.close();
+
+  // Decode file.ul
+  expandedsize = _ulaw->codecDecode (_dst, (unsigned char *)_src, length);
+
+  _debug("length (pre-ulaw) : %d\n", length);
+  _debug("expandedsize (post-ulaw) : %d\n", expandedsize);
+
+  if (tonethread == NULL) {
     _debug("Thread: start tonethread\n");
-		tonethread = new ToneThread ((int16*)_dst, expandedsize);
-		tonethread->start();
-	}
-	
-	return 1;
+    tonethread = new ToneThread ((int16*)_dst, expandedsize);
+    tonethread->start();
+  }
+
+  return 1;
 }
 
 int
diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 58893427ea..9494ce5d83 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -276,7 +276,6 @@ ManagerImpl::hangupCall (CALLID id)
     return -1;
   }
   int result = call->hangup();
-
   deleteCall(id);
   stopTone(); // stop tone, like a 700 error: number not found Not Found
   return result;
@@ -297,7 +296,6 @@ ManagerImpl::cancelCall (CALLID id)
     return -1; 
   }
   int result = call->cancel();
-
   deleteCall(id);
   stopTone();
   return result;
@@ -705,11 +703,11 @@ ManagerImpl::peerHungupCall (CALLID id)
     return -1;
   }
   stopTone();
-  call->setState(Call::Hungup);
 
   if (_gui) _gui->peerHungupCall(id);
-
   deleteCall(id);
+  call->setState(Call::Hungup);
+
   setCurrentCallId(0);
   return 1;
 }
-- 
GitLab