1 /* GStreamer
2  * Copyright (C) 2009 Tiago Katcipis <tiagokatcipis@gmail.com>
3  * Copyright (C) 2009 Paulo Pizarro  <paulo.pizarro@gmail.com>
4  * Copyright (C) 2009 Rogério Santos <rogerio.santos@digitro.com.br>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <glib.h>
27 #include "vad_private.h"
28 
29 #define VAD_POWER_ALPHA     0x0800      /* Q16 */
30 #define VAD_ZCR_THRESHOLD   0
31 #define VAD_BUFFER_SIZE     256
32 
33 
34 union pgen
35 {
36   guint64 a;
37   gpointer v;
38   guint64 *l;
39   guchar *b;
40   guint16 *w;
41   gint16 *s;
42 };
43 
44 struct _cqueue_s
45 {
46   union pgen base;
47   union pgen tail;
48   union pgen head;
49   gint size;
50 };
51 
52 typedef struct _cqueue_s cqueue_t;
53 
54 struct _vad_s
55 {
56   gint16 vad_buffer[VAD_BUFFER_SIZE];
57   cqueue_t cqueue;
58   gint vad_state;
59   guint64 hysteresis;
60   guint64 vad_samples;
61   guint64 vad_power;
62   guint64 threshold;
63   long vad_zcr;
64 };
65 
66 VADFilter *
vad_new(guint64 hysteresis,gint threshold)67 vad_new (guint64 hysteresis, gint threshold)
68 {
69   VADFilter *vad = malloc (sizeof (VADFilter));
70   vad_reset (vad);
71   vad->hysteresis = hysteresis;
72   vad_set_threshold (vad, threshold);
73   return vad;
74 }
75 
76 void
vad_reset(VADFilter * vad)77 vad_reset (VADFilter * vad)
78 {
79   memset (vad, 0, sizeof (*vad));
80   vad->cqueue.base.s = vad->vad_buffer;
81   vad->cqueue.tail.a = vad->cqueue.head.a = 0;
82   vad->cqueue.size = VAD_BUFFER_SIZE;
83   vad->vad_state = VAD_SILENCE;
84 }
85 
86 void
vad_destroy(VADFilter * p)87 vad_destroy (VADFilter * p)
88 {
89   free (p);
90 }
91 
92 void
vad_set_hysteresis(struct _vad_s * p,guint64 hysteresis)93 vad_set_hysteresis (struct _vad_s *p, guint64 hysteresis)
94 {
95   p->hysteresis = hysteresis;
96 }
97 
98 guint64
vad_get_hysteresis(struct _vad_s * p)99 vad_get_hysteresis (struct _vad_s *p)
100 {
101   return p->hysteresis;
102 }
103 
104 void
vad_set_threshold(struct _vad_s * p,gint threshold_db)105 vad_set_threshold (struct _vad_s *p, gint threshold_db)
106 {
107   gint power = (gint) (threshold_db / 10.0);
108   p->threshold = (guint64) (pow (10, (power)) * 4294967295UL);
109 }
110 
111 gint
vad_get_threshold_as_db(struct _vad_s * p)112 vad_get_threshold_as_db (struct _vad_s *p)
113 {
114   return (gint) (10 * log10 (p->threshold / 4294967295.0));
115 }
116 
117 gint
vad_update(struct _vad_s * p,gint16 * data,gint len)118 vad_update (struct _vad_s * p, gint16 * data, gint len)
119 {
120   guint64 tail;
121   gint frame_type;
122   gint16 sample;
123   gint i;
124 
125   for (i = 0; i < len; i++) {
126     p->vad_power = VAD_POWER_ALPHA * ((data[i] * data[i] >> 14) & 0xFFFF) +
127         (0xFFFF - VAD_POWER_ALPHA) * (p->vad_power >> 16) +
128         ((0xFFFF - VAD_POWER_ALPHA) * (p->vad_power & 0xFFFF) >> 16);
129     /* Update VAD buffer */
130     p->cqueue.base.s[p->cqueue.head.a] = data[i];
131     p->cqueue.head.a = (p->cqueue.head.a + 1) & (p->cqueue.size - 1);
132     if (p->cqueue.head.a == p->cqueue.tail.a)
133       p->cqueue.tail.a = (p->cqueue.tail.a + 1) & (p->cqueue.size - 1);
134   }
135 
136   tail = p->cqueue.tail.a;
137   p->vad_zcr = 0;
138   for (;;) {
139     sample = p->cqueue.base.s[tail];
140     tail = (tail + 1) & (p->cqueue.size - 1);
141     if (tail == p->cqueue.head.a)
142       break;
143     p->vad_zcr +=
144         ((sample & 0x8000) != (p->cqueue.base.s[tail] & 0x8000)) ? 1 : -1;
145   }
146 
147   frame_type = (p->vad_power > p->threshold
148       && p->vad_zcr < VAD_ZCR_THRESHOLD) ? VAD_VOICE : VAD_SILENCE;
149 
150   if (p->vad_state != frame_type) {
151     /* Voice to silence transition */
152     if (p->vad_state == VAD_VOICE) {
153       p->vad_samples += len;
154       if (p->vad_samples >= p->hysteresis) {
155         p->vad_state = frame_type;
156         p->vad_samples = 0;
157       }
158     } else {
159       p->vad_state = frame_type;
160       p->vad_samples = 0;
161     }
162   } else {
163     p->vad_samples = 0;
164   }
165 
166   return p->vad_state;
167 }
168