VAD should now work on wideband too.


git-svn-id: http://svn.xiph.org/trunk/speex@4147 0101bb08-14d6-0310-b084-bc0e0c8e3800
diff --git a/libspeex/nb_celp.c b/libspeex/nb_celp.c
index e991386..914ed13 100644
--- a/libspeex/nb_celp.c
+++ b/libspeex/nb_celp.c
@@ -400,7 +400,7 @@
          /*VAD only case*/
          int mode;
          if (st->relative_quality<2.0)
-            mode=0;
+            mode=1;
          else
             mode=st->submodeSelect;
          /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
diff --git a/libspeex/sb_celp.c b/libspeex/sb_celp.c
index e366e62..f8b789e 100644
--- a/libspeex/sb_celp.c
+++ b/libspeex/sb_celp.c
@@ -145,7 +145,7 @@
    st->bufSize=mode->bufSize;
 
    st->submodes=mode->submodes;
-   st->submodeID=mode->defaultSubmode;
+   st->submodeSelect = st->submodeID=mode->defaultSubmode;
    
    {
       /* FIXME: Should do this without explicit reference to a mode */
@@ -220,6 +220,7 @@
 
    st->vbr_quality = 8;
    st->vbr_enabled = 0;
+   st->vad_enabled = 0;
    st->relative_quality=0;
 
    st->complexity=2;
@@ -349,7 +350,7 @@
       st->lsp[i] = acos(st->lsp[i]);
 
    /* VBR code */
-   if (st->vbr_enabled){
+   if (st->vbr_enabled || st->vad_enabled){
       float e_low=0, e_high=0;
       float ratio;
       for (i=0;i<st->frame_size;i++)
@@ -364,10 +365,11 @@
       if (ratio>2)
          ratio=2;
       /*if (ratio>-2)*/
-      st->relative_quality+=1.0*(ratio+2);
+      if (st->vbr_enabled) 
       {
          int modeid;
          modeid = mode->nb_modes-1;
+         st->relative_quality+=1.0*(ratio+2);
          while (modeid)
          {
             int v1;
@@ -385,6 +387,16 @@
          /*fprintf (stderr, "%f %d\n", low_qual, modeid);*/
          speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
          /*fprintf (stderr, "%d %d\n", st->submodeID, modeid);*/
+      } else {
+         /* VAD only */
+         int modeid;
+         if (st->relative_quality<2.0)
+            modeid=1;
+         else
+            modeid=st->submodeSelect;
+         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
+         st->submodeID=modeid;
+
       }
       /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
    }
@@ -1059,7 +1071,7 @@
       (*(int*)ptr) = st->full_frame_size;
       break;
    case SPEEX_SET_HIGH_MODE:
-      st->submodeID = (*(int*)ptr);
+      st->submodeSelect = st->submodeID = (*(int*)ptr);
       break;
    case SPEEX_SET_LOW_MODE:
       speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, ptr);
@@ -1074,6 +1086,13 @@
    case SPEEX_GET_VBR:
       (*(int*)ptr) = st->vbr_enabled;
       break;
+   case SPEEX_SET_VAD:
+      st->vad_enabled = (*(int*)ptr);
+      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
+      break;
+   case SPEEX_GET_VAD:
+      (*(int*)ptr) = st->vad_enabled;
+      break;
    case SPEEX_SET_VBR_QUALITY:
       {
          int q;
@@ -1096,7 +1115,7 @@
             quality = 0;
          if (quality > 10)
             quality = 10;
-         st->submodeID = ((SpeexSBMode*)(st->mode->mode))->quality_map[quality];
+         st->submodeSelect = st->submodeID = ((SpeexSBMode*)(st->mode->mode))->quality_map[quality];
          nb_qual = ((SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
          speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
       }
diff --git a/libspeex/sb_celp.h b/libspeex/sb_celp.h
index cd393dd..091a36a 100644
--- a/libspeex/sb_celp.h
+++ b/libspeex/sb_celp.h
@@ -92,10 +92,12 @@
 
    float  vbr_quality;         /**< Quality setting for VBR encoding */
    int    vbr_enabled;         /**< 1 for enabling VBR, 0 otherwise */
+   int    vad_enabled;         /**< 1 for enabling VAD, 0 otherwise */
    float  relative_quality;
 
    SpeexSubmode **submodes;
    int    submodeID;
+   int    submodeSelect;
    int    complexity;
    int    sampling_rate;