Index: pjproject/trunk/pjmedia/src/pjmedia/silencedet.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/vad.c	(revision 205)
+++ pjproject/trunk/pjmedia/src/pjmedia/silencedet.c	(revision 228)
@@ -17,5 +17,5 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  */
-#include <pjmedia/vad.h>
+#include <pjmedia/silencedet.h>
 #include <pjmedia/errno.h>
 #include <pj/assert.h>
@@ -24,17 +24,17 @@
 
 
-#define THIS_FILE   "vad.c"
-
-typedef enum pjmedia_vad_mode {
+#define THIS_FILE   "silencedet.c"
+
+typedef enum pjmedia_silence_det_mode {
     VAD_MODE_NONE,
     VAD_MODE_FIXED,
     VAD_MODE_ADAPTIVE
-} pjmedia_vad_mode;
+} pjmedia_silence_det_mode;
 
 
 /**
- * This structure holds the vad state.
+ * This structure holds the silence detector state.
  */
-struct pjmedia_vad
+struct pjmedia_silence_det
 {
     int	      mode;		/**< VAD mode.				    */
@@ -60,66 +60,66 @@
 
 
-PJ_DEF(pj_status_t) pjmedia_vad_create( pj_pool_t *pool,
-					pjmedia_vad **p_vad)
-{
-    pjmedia_vad *vad;
-
-    PJ_ASSERT_RETURN(pool && p_vad, PJ_EINVAL);
-
-    vad = pj_pool_zalloc(pool, sizeof(struct pjmedia_vad));
-
-    vad->weakest_signal = 0xFFFFFFFFUL;
-    vad->loudest_silence = 0;
-    vad->signal_cnt = 0;
-    vad->silence_cnt = 0;
+PJ_DEF(pj_status_t) pjmedia_silence_det_create( pj_pool_t *pool,
+						pjmedia_silence_det **p_sd)
+{
+    pjmedia_silence_det *sd;
+
+    PJ_ASSERT_RETURN(pool && p_sd, PJ_EINVAL);
+
+    sd = pj_pool_zalloc(pool, sizeof(struct pjmedia_silence_det));
+
+    sd->weakest_signal = 0xFFFFFFFFUL;
+    sd->loudest_silence = 0;
+    sd->signal_cnt = 0;
+    sd->silence_cnt = 0;
     
     /* Restart in adaptive, silent mode */
-    vad->in_talk = PJ_FALSE;
-    pjmedia_vad_set_adaptive( vad, 160 );
-
-    *p_vad = vad;
-    return PJ_SUCCESS;
-}
-
-PJ_DEF(pj_status_t) pjmedia_vad_set_adaptive( pjmedia_vad *vad,
-					      unsigned frame_size)
-{
-    PJ_ASSERT_RETURN(vad && frame_size, PJ_EINVAL);
-
-    vad->frame_size = frame_size;
-    vad->mode = VAD_MODE_ADAPTIVE;
-    vad->min_signal_cnt = 10;
-    vad->min_silence_cnt = 64;
-    vad->recalc_cnt = 250;
-    vad->cur_threshold = 20;
-
-    return PJ_SUCCESS;
-}
-
-PJ_DEF(pj_status_t) pjmedia_vad_set_fixed( pjmedia_vad *vad,
-					   unsigned frame_size,
-					   unsigned threshold )
-{
-    PJ_ASSERT_RETURN(vad && frame_size, PJ_EINVAL);
-
-    vad->mode = VAD_MODE_FIXED;
-    vad->frame_size = frame_size;
-    vad->cur_threshold = threshold;
-
-    return PJ_SUCCESS;
-}
-
-PJ_DEF(pj_status_t) pjmedia_vad_disable( pjmedia_vad *vad )
-{
-    PJ_ASSERT_RETURN(vad, PJ_EINVAL);
-
-    vad->mode = VAD_MODE_NONE;
-
-    return PJ_SUCCESS;
-}
-
-
-PJ_DEF(pj_int32_t) pjmedia_vad_calc_avg_signal(const pj_int16_t samples[],
-					       pj_size_t count)
+    sd->in_talk = PJ_FALSE;
+    pjmedia_silence_det_set_adaptive( sd, 160 );
+
+    *p_sd = sd;
+    return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pjmedia_silence_det_set_adaptive( pjmedia_silence_det *sd,
+						      unsigned frame_size)
+{
+    PJ_ASSERT_RETURN(sd && frame_size, PJ_EINVAL);
+
+    sd->frame_size = frame_size;
+    sd->mode = VAD_MODE_ADAPTIVE;
+    sd->min_signal_cnt = 10;
+    sd->min_silence_cnt = 64;
+    sd->recalc_cnt = 250;
+    sd->cur_threshold = 20;
+
+    return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pjmedia_silence_det_set_fixed( pjmedia_silence_det *sd,
+						   unsigned frame_size,
+						   unsigned threshold )
+{
+    PJ_ASSERT_RETURN(sd && frame_size, PJ_EINVAL);
+
+    sd->mode = VAD_MODE_FIXED;
+    sd->frame_size = frame_size;
+    sd->cur_threshold = threshold;
+
+    return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pjmedia_silence_det_disable( pjmedia_silence_det *sd )
+{
+    PJ_ASSERT_RETURN(sd, PJ_EINVAL);
+
+    sd->mode = VAD_MODE_NONE;
+
+    return PJ_SUCCESS;
+}
+
+
+PJ_DEF(pj_int32_t) pjmedia_silence_det_calc_avg_signal(const pj_int16_t samples[],
+						       pj_size_t count)
 {
     pj_uint32_t sum = 0;
@@ -141,8 +141,8 @@
 }
 
-PJ_DEF(pj_bool_t) pjmedia_vad_detect_silence( pjmedia_vad *vad,
-					      const pj_int16_t samples[],
-					      pj_size_t count,
-					      pj_int32_t *p_level)
+PJ_DEF(pj_bool_t) pjmedia_silence_det_detect_silence( pjmedia_silence_det *sd,
+						      const pj_int16_t samples[],
+						      pj_size_t count,
+						      pj_int32_t *p_level)
 {
     pj_uint32_t level;
@@ -150,5 +150,5 @@
 
     /* Always return false if VAD is disabled */
-    if (vad->mode == VAD_MODE_NONE) {
+    if (sd->mode == VAD_MODE_NONE) {
 	if (p_level)
 	    *p_level = -1;
@@ -157,5 +157,5 @@
     
     /* Calculate average signal level. */
-    level = pjmedia_vad_calc_avg_signal(samples, count);
+    level = pjmedia_silence_det_calc_avg_signal(samples, count);
     
     /* Report to caller, if required. */
@@ -167,5 +167,5 @@
     
     /* Do we have signal? */
-    have_signal = level > vad->cur_threshold;
+    have_signal = level > sd->cur_threshold;
     
     /* We we're in transition between silence and signel, increment the 
@@ -173,32 +173,32 @@
      * frames.
      */
-    if (vad->in_talk != have_signal) {
+    if (sd->in_talk != have_signal) {
 	unsigned limit;
 
-	vad->cur_cnt++;
-
-	limit = (vad->in_talk ? vad->min_silence_cnt : 
-				vad->min_signal_cnt);
-
-	if (vad->cur_cnt > limit) {
+	sd->cur_cnt++;
+
+	limit = (sd->in_talk ? sd->min_silence_cnt : 
+				sd->min_signal_cnt);
+
+	if (sd->cur_cnt > limit) {
 
 	    /* Swap mode */
-	    vad->in_talk = !vad->in_talk;
+	    sd->in_talk = !sd->in_talk;
 	    
 	    /* Restart adaptive cur_threshold measurements */
-	    vad->weakest_signal = 0xFFFFFFFFUL;
-	    vad->loudest_silence = 0;
-	    vad->signal_cnt = 0;
-	    vad->silence_cnt = 0;
+	    sd->weakest_signal = 0xFFFFFFFFUL;
+	    sd->loudest_silence = 0;
+	    sd->signal_cnt = 0;
+	    sd->silence_cnt = 0;
 	}
 
     } else {
 	/* Reset frame count */
-	vad->cur_cnt = 0;
-    }
-    
-    /* For fixed threshold vad, everything is done. */
-    if (vad->mode == VAD_MODE_FIXED) {
-	return !vad->in_talk;
+	sd->cur_cnt = 0;
+    }
+    
+    /* For fixed threshold sd, everything is done. */
+    if (sd->mode == VAD_MODE_FIXED) {
+	return !sd->in_talk;
     }
     
@@ -206,12 +206,12 @@
     /* Count the number of silent and signal frames and calculate min/max */
     if (have_signal) {
-	if (level < vad->weakest_signal)
-	    vad->weakest_signal = level;
-	vad->signal_cnt++;
+	if (level < sd->weakest_signal)
+	    sd->weakest_signal = level;
+	sd->signal_cnt++;
     }
     else {
-	if (level > vad->loudest_silence)
-	    vad->loudest_silence = level;
-	vad->silence_cnt++;
+	if (level > sd->loudest_silence)
+	    sd->loudest_silence = level;
+	sd->silence_cnt++;
     }
 
@@ -219,24 +219,24 @@
      * silence/signal frames.
      */
-    if ((vad->signal_cnt + vad->silence_cnt) > vad->recalc_cnt) {
+    if ((sd->signal_cnt + sd->silence_cnt) > sd->recalc_cnt) {
 	
 	/* Adjust silence threshold by looking at the proportions of
 	 * signal and silence frames.
 	 */
-	if (vad->signal_cnt >= vad->recalc_cnt) {
+	if (sd->signal_cnt >= sd->recalc_cnt) {
 	    /* All frames where signal frames.
 	     * Increase silence threshold.
 	     */
-	    vad->cur_threshold += (vad->weakest_signal - vad->cur_threshold)/4;
+	    sd->cur_threshold += (sd->weakest_signal - sd->cur_threshold)/4;
 	    PJ_LOG(6,(THIS_FILE, "Vad cur_threshold increased to %d",
-		      vad->cur_threshold));
-	}
-	else if (vad->silence_cnt >= vad->recalc_cnt) {
+		      sd->cur_threshold));
+	}
+	else if (sd->silence_cnt >= sd->recalc_cnt) {
 	    /* All frames where silence frames.
 	     * Decrease silence threshold.
 	     */
-	    vad->cur_threshold = (vad->cur_threshold+vad->loudest_silence)/2+1;
+	    sd->cur_threshold = (sd->cur_threshold+sd->loudest_silence)/2+1;
 	    PJ_LOG(6,(THIS_FILE, "Vad cur_threshold decreased to %d",
-		      vad->cur_threshold));
+		      sd->cur_threshold));
 	}
 	else { 
@@ -244,8 +244,8 @@
 
 	    /* Adjust according to signal/silence proportions. */
-	    if (vad->signal_cnt > vad->silence_cnt * 2)
-		vad->cur_threshold++;
-	    else if (vad->silence_cnt >  vad->signal_cnt* 2)
-		vad->cur_threshold--;
+	    if (sd->signal_cnt > sd->silence_cnt * 2)
+		sd->cur_threshold++;
+	    else if (sd->silence_cnt >  sd->signal_cnt* 2)
+		sd->cur_threshold--;
 	    else
 		updated = PJ_FALSE;
@@ -254,16 +254,16 @@
 		PJ_LOG(6,(THIS_FILE,
 			  "Vad cur_threshold updated to %d",
-			  vad->cur_threshold));
+			  sd->cur_threshold));
 	    }
 	}
 
 	/* Reset. */
-	vad->weakest_signal = 0xFFFFFFFFUL;
-	vad->loudest_silence = 0;
-	vad->signal_cnt = 0;
-	vad->silence_cnt = 0;
-    }
-    
-    return !vad->in_talk;
-}
-
+	sd->weakest_signal = 0xFFFFFFFFUL;
+	sd->loudest_silence = 0;
+	sd->signal_cnt = 0;
+	sd->silence_cnt = 0;
+    }
+    
+    return !sd->in_talk;
+}
+
