~audio-recorder/audio-recorder/trunk

« back to all changes in this revision

Viewing changes to src/gst-vad.c

  • Committer: Osmo Antero
  • Date: 2012-09-29 18:12:44 UTC
  • Revision ID: osmoma@gmail.com-20120929181244-gmrxd5xww9pua60a
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) Linux community.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Library General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Library General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
*/
 
19
#include "gst-vad.h"
 
20
#include "audio-sources.h"
 
21
#include "dconf.h"
 
22
#include "log.h"
 
23
#include "utility.h"
 
24
 
 
25
#include "gst-pipeline.h"
 
26
#include "gst-recorder.h"
 
27
 
 
28
#include <string.h>
 
29
#include <glib.h>
 
30
#include <gdk/gdk.h>
 
31
#include <math.h>
 
32
#include <gst/gst.h>
 
33
 
 
34
// GStreamer pipeline for VAD
 
35
static GstElement *g_vad_pipeline = NULL;
 
36
 
 
37
// Debug flag (--debug-signal or -d argument)
 
38
static gboolean g_debug_flag = FALSE;
 
39
 
 
40
// Parameters for VAD-pipeline
 
41
static PipelineParms *g_curr_parms = NULL;
 
42
 
 
43
// Time to wait between each test
 
44
#define TRIGGER_TIME_MS 150  // in milliseconds
 
45
 
 
46
static GstElement *vad_create_pipeline(PipelineParms *parms);
 
47
static void vad_save_parms(PipelineParms *new_parms);
 
48
static gboolean vad_is_running();
 
49
static void vad_shutdown_pipeline();
 
50
static gboolean vad_message_handler(GstBus * bus, GstMessage * message, gpointer data);
 
51
 
 
52
void vad_module_init() {
 
53
    LOG_DEBUG("Init gst-vad.c.\n");
 
54
 
 
55
    // Initialize
 
56
    g_vad_pipeline = NULL;
 
57
    g_curr_parms = NULL;
 
58
    g_debug_flag = FALSE;
 
59
}
 
60
 
 
61
void vad_module_exit() {
 
62
    LOG_DEBUG("Clean up gst-vad.c.\n");
 
63
 
 
64
    vad_stop_VAD();
 
65
}
 
66
 
 
67
void vad_set_debug_flag(gboolean on_off) {
 
68
    // Set debug flag. Please see application options:
 
69
    // $ audio-recorder --help
 
70
    // VAD process will now print level values in a terminal window.
 
71
    g_debug_flag = on_off;
 
72
}
 
73
 
 
74
void vad_save_parms(PipelineParms *new_parms) {
 
75
    if (g_curr_parms) {
 
76
        pipeline_free_parms(g_curr_parms);
 
77
    }
 
78
    g_curr_parms = NULL;
 
79
 
 
80
    if (!new_parms) return;
 
81
 
 
82
    // Save the pointer
 
83
    g_curr_parms = new_parms;
 
84
}
 
85
 
 
86
void vad_start_VAD() {
 
87
    PipelineParms *parms = g_malloc0(sizeof(PipelineParms));
 
88
 
 
89
    // Get audio source and device list
 
90
    parms->source = NULL;
 
91
    parms->dev_list = audio_sources_get_device_NEW(&(parms->source));
 
92
 
 
93
    gboolean changed = TRUE;
 
94
    if (g_curr_parms) {
 
95
        changed = g_strcmp0(parms->source, g_curr_parms->source);
 
96
        changed = changed || (!str_lists_equal(parms->dev_list, g_curr_parms->dev_list));
 
97
    }
 
98
 
 
99
    // Changed?
 
100
    if (changed) {
 
101
        // Yes, stop VAD.
 
102
        vad_stop_VAD();
 
103
    }
 
104
 
 
105
    // Already running (or values were unchanged)?
 
106
    if (vad_is_running()) {
 
107
 
 
108
        // Values not used!
 
109
        pipeline_free_parms(parms);
 
110
        parms = NULL;            
 
111
        return;
 
112
    }
 
113
 
 
114
    // Create new GStreamer pipeline for VAD
 
115
    g_vad_pipeline = vad_create_pipeline(parms);
 
116
 
 
117
    // Save values
 
118
    vad_save_parms(parms);
 
119
}
 
120
 
 
121
void vad_stop_VAD() {
 
122
    // Shutdown pipeline
 
123
    vad_shutdown_pipeline();
 
124
 
 
125
    // Reset static variables
 
126
    vad_message_handler(NULL, NULL, NULL);
 
127
 
 
128
    // Clear PipelineParms
 
129
    vad_save_parms(NULL);
 
130
}
 
131
 
 
132
static gboolean vad_is_running() {
 
133
    // Valid pipeline?
 
134
    if (!GST_IS_OBJECT(g_vad_pipeline)) return FALSE;
 
135
 
 
136
    // The state is?
 
137
    // Ref: http://www.gstreamer.net/data/doc/gstreamer/head/gstreamer/html/GstElement.html#gst-element-get-state
 
138
    GstState status, pending;
 
139
    gst_element_get_state(g_vad_pipeline, &status, &pending, 0);
 
140
 
 
141
    return (status != GST_STATE_NULL);
 
142
}
 
143
 
 
144
static void vad_check_triggers(GstClockTime timestamp, GstClockTimeDiff time_diff, 
 
145
                               gdouble rms_dB_left, gdouble rms_dB_right, gdouble peak_dB) {
 
146
    // From dB (decibel) to normalized value between [0 - 1.0].
 
147
    //
 
148
    // Normalized value [0 - 1.0] = exp(dB / 20).
 
149
    // Decibel value = log(normalized value) * 20.
 
150
 
 
151
    // Calc normalized values
 
152
    gdouble rms_left = exp(rms_dB_left/20.0);
 
153
    gdouble rms_right = exp(rms_dB_right/20.0);
 
154
    gdouble peak = exp(peak_dB/20.0);
 
155
 
 
156
    // Calculate average
 
157
    gdouble sum = 0.0;
 
158
    guint count = 0;
 
159
 
 
160
    // Left channel gave a value?
 
161
    if (rms_left > 0.001) {
 
162
        sum += rms_left;
 
163
        count++;  
 
164
    }
 
165
 
 
166
    // Right channel gave a value?
 
167
    if (rms_right > 0.001) {
 
168
        sum += rms_right;
 
169
        count++;  
 
170
    }
 
171
 
 
172
    // Calc avg
 
173
    gdouble rms_avg = 0.0;
 
174
    if (count > 0) {
 
175
        rms_avg = sum / count;
 
176
    }
 
177
 
 
178
    // Print values in a terminal window?
 
179
    // Got --debug-signal or -d argument?
 
180
    if (g_debug_flag) {
 
181
        gdouble rms_dB_avg = rms_dB_left > -120 ? ((rms_dB_left + rms_dB_right)/2.0) : rms_dB_right;
 
182
        g_print("Audio level. Time:%" GST_TIME_FORMAT ", RMS:%3.2f dB, normalized RMS:%3.2f, peak value:%3.2f.\n",
 
183
                GST_TIME_ARGS(timestamp), rms_dB_avg, rms_avg, peak);
 
184
    }
 
185
 
 
186
    // Evaluate triggers related to RMS value
 
187
    timer_evaluate_triggers(time_diff, rms_avg);
 
188
}
 
189
 
 
190
static gboolean vad_message_handler(GstBus * bus, GstMessage * message, gpointer data) {
 
191
    static GstClockTime g_last_timestamp = 0L;
 
192
 
 
193
    if (bus == NULL) {
 
194
        // Reset static variables
 
195
        g_last_timestamp = 0L;
 
196
        // And return
 
197
        return TRUE;
 
198
    }
 
199
 
 
200
    if (GST_MESSAGE_TYPE(message) != GST_MESSAGE_ELEMENT) {
 
201
        return TRUE;
 
202
    }
 
203
 
 
204
    const GstStructure *s = gst_message_get_structure(message);
 
205
    const gchar *name = gst_structure_get_name(s);
 
206
 
 
207
    GstClockTime timestamp;
 
208
    if (!gst_structure_get_clock_time(s, "timestamp", &timestamp)) {
 
209
        LOG_ERROR("vad_message_handler: Cannot read timestamp.\n");
 
210
    }
 
211
 
 
212
    // Wait at least TRIGGER_TIME_MS milliseconds
 
213
    GstClockTimeDiff diff = GST_CLOCK_DIFF(g_last_timestamp, timestamp);
 
214
    if (diff < TRIGGER_TIME_MS * GST_MSECOND) {
 
215
        goto LBL_1;
 
216
    }
 
217
 
 
218
    g_last_timestamp = timestamp;
 
219
 
 
220
    if (g_str_equal(name, "level")) {
 
221
        // Ref: http://www.gstreamer.net/data/doc/gstreamer/head/gst-plugins-good-plugins/html/gst-plugins-good-plugins-level.html
 
222
 
 
223
        // Field names:
 
224
        //level field:endtime
 
225
        //level field:timestamp
 
226
        //level field:stream-time
 
227
        //level field:running-time
 
228
        //level field:duration
 
229
        //level field:rms
 
230
        //level field:peak
 
231
        //level field:decay
 
232
 
 
233
        const GValue *list = gst_structure_get_value(s, "rms");
 
234
        const GValue *value = NULL;
 
235
 
 
236
        gint channels = gst_value_list_get_size(list);
 
237
        if (channels < 1) goto LBL_1;
 
238
 
 
239
        gdouble rms_dB_left = -G_MAXDOUBLE;  // left channel
 
240
        gdouble rms_dB_right = -G_MAXDOUBLE; // right channel
 
241
 
 
242
        guint count = gst_value_list_get_size(list);
 
243
 
 
244
        if (count > 0) {
 
245
            value = gst_value_list_get_value(list, 0);
 
246
            rms_dB_left = g_value_get_double(value);
 
247
        }
 
248
 
 
249
        if (count > 1) {
 
250
            value = gst_value_list_get_value(list, 1);
 
251
            rms_dB_right = g_value_get_double(value);
 
252
        }
 
253
 
 
254
        // peak_dB:
 
255
        list = gst_structure_get_value(s, "peak");
 
256
        value = gst_value_list_get_value (list, 0);
 
257
        gdouble peak_dB = g_value_get_double(value);
 
258
 
 
259
        // Evaluate timer triggers
 
260
        vad_check_triggers(timestamp, diff, rms_dB_left, rms_dB_right, peak_dB);
 
261
    }
 
262
  
 
263
 LBL_1:
 
264
    // We handled the message we want, and ignored the ones we didn't want. so the core can unref the message for us
 
265
    return TRUE;
 
266
}
 
267
 
 
268
static void vad_shutdown_pipeline() {
 
269
    // Shutdown VAD-pipeline
 
270
    if (!GST_IS_PIPELINE(g_vad_pipeline)) {
 
271
        goto LBL_1;
 
272
    }
 
273
 
 
274
    LOG_VAD("Shutdown VAD pipeline.\n");
 
275
 
 
276
    // Switch to GST_STATE_NULL
 
277
    gst_element_set_state(g_vad_pipeline, GST_STATE_NULL);
 
278
 
 
279
    // Then destroy it
 
280
    gst_object_unref(GST_OBJECT(g_vad_pipeline));
 
281
    g_vad_pipeline = NULL;
 
282
 
 
283
LBL_1:
 
284
    ;
 
285
}
 
286
 
 
287
static GstElement *vad_create_pipeline(PipelineParms *parms) {
 
288
    if (!parms) return NULL;
 
289
 
 
290
#if defined(DEBUG_VAD) || defined(DEBUG_ALL)
 
291
    LOG_VAD("Start VAD for \"%s\"\n", parms->source);
 
292
    str_list_print("Monitor devices", parms->dev_list);
 
293
#endif
 
294
 
 
295
    gchar *err_msg = NULL;
 
296
    GstElement *pipeline = pipeline_create_VAD(parms, &err_msg);
 
297
 
 
298
    // Errors?
 
299
    if (!GST_IS_PIPELINE(pipeline) || err_msg) {
 
300
        goto LBL_1;
 
301
    }
 
302
 
 
303
    // Add message handler
 
304
    GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
 
305
    gst_bus_add_signal_watch(bus);
 
306
 
 
307
    // Monitor "level" messages
 
308
    g_signal_connect(bus, "message::element", G_CALLBACK(vad_message_handler), NULL);
 
309
 
 
310
    gst_object_unref(bus);
 
311
 
 
312
    // Roll pipeline
 
313
    if (gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
 
314
        err_msg = g_strdup(_("Cannot start reading from the stream/pipeline.\n"));
 
315
        goto LBL_1;
 
316
    } else {
 
317
        LOG_DEBUG("Pipeline for VAD (Voice Activity Detection) is running and OK.\n");
 
318
    }
 
319
 
 
320
    // Ok
 
321
    return pipeline;
 
322
 
 
323
LBL_1:
 
324
    if (!err_msg) {
 
325
        err_msg = g_strdup_printf(_("Cannot create audio pipeline. %s.\n"), "(VAD pipeline)");
 
326
    }  
 
327
    LOG_ERROR(err_msg);
 
328
    g_free(err_msg);
 
329
 
 
330
    // Destroy pipeline
 
331
    if (G_IS_OBJECT(pipeline)) {
 
332
        gst_element_set_state(pipeline, GST_STATE_NULL);
 
333
        gst_object_unref(GST_OBJECT(pipeline));
 
334
    }
 
335
 
 
336
    // Return NULL
 
337
    return NULL;
 
338
}
 
339
 
 
340
gboolean vad_get_debug_flag() {
 
341
    // --debug-signal or -d flag
 
342
    return g_debug_flag;
 
343
}