~audio-recorder/audio-recorder/trunk

22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
1
/*
1 by Osmo Antero Maatta
Initial import 17.jan.2011
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
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
7
 * version 3 of the License (GPL3), or any later version.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
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
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
12
 * See the GNU Library General Public License 3 for more details.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
13
 *
14
 * You should have received a copy of the GNU Library General Public
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
15
 * License 3 along with this program; if not, see /usr/share/common-licenses/GPL file
16
 * or write to Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1 by Osmo Antero Maatta
Initial import 17.jan.2011
17
 * Boston, MA 02111-1307, USA.
18
*/
19
#include "timer.h"
20
#include "log.h"
21
#include "support.h"
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
22
#include "utility.h"
1 by Osmo Antero Maatta
Initial import 17.jan.2011
23
#include <stdlib.h>
24
25
/* Parsing of timer commands:
26
 Syntax:
27
 action := comment | ("start" | "stop" | "pause") command
28
29
 comment := '#'...\n
30
31
 command := action_prep data
32
33
 action_prep := "after" | "at" | "if" | "on"
34
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
35
 data := time_notation | filesize | word
1 by Osmo Antero Maatta
Initial import 17.jan.2011
36
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
37
 time_notation := (##:##:## | #hour #min #sec) time_suffix
1 by Osmo Antero Maatta
Initial import 17.jan.2011
38
39
 time_suffix := "am" | "pm" | ""
40
7 by Osmo Antero Maatta
Improved timer commands
41
 filesize := # ("bytes" | "kb"|"kib" | "mb"|"mib" | "gb"|"gib" | "tb"|"tib")
1 by Osmo Antero Maatta
Initial import 17.jan.2011
42
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
43
 word := ("silence" | "voice" | "sound" | "audio") time_notation signal_threshold
1 by Osmo Antero Maatta
Initial import 17.jan.2011
44
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
45
 signal_threshold := #dB | #[1.1, 100]% | #[0, 1.0]
1 by Osmo Antero Maatta
Initial import 17.jan.2011
46
47
 Note: # means an integer or floating point number.
48
 Comments begin with '#' and ends at newline \n.
49
50
 In simplified form:
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
51
 start | stop | pause (at|after|if|on) ##:##:## (am|pm|) | ### (bytes|kb|mb|gb|tb) | (silence|voice|sound|audio) ## seconds (##dB | ##% | ##)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
52
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
53
 The words "voice", "sound" and "audio" has exactly same meaning!
1 by Osmo Antero Maatta
Initial import 17.jan.2011
54
7 by Osmo Antero Maatta
Improved timer commands
55
 The file size units:
56
 https://wiki.ubuntu.com/UnitsPolicy
57
1 by Osmo Antero Maatta
Initial import 17.jan.2011
58
 Some examples:
59
 start at 10:10 pm
60
 stop after 20.5 min
61
62
 start if voice
63
 start if sound 5s 10%
64
65
 # this is a comment
66
67
 stop at 08:00:30 am
68
 stop after 6 min 20 sec
69
 stop after 5 MB
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
70
 stop if silence 7 s -20 db   # duration is 7 seconds, threshold is -20dB
71
 stop if silence 7 s 30%  # duration is 7 seconds, threshold is 30 (=0.3)
72
 stop if silence 7 s 0.3  # duration is 7 seconds, threshold is 0.3 (=30%)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
73
 stop after silence | 5GB | 35 min
74
75
 pause if silence 10s 7%
76
77
 start if audio -19dB
78
 stop if silence 10 sec -18 db | 20 GB | 10 pm
79
 stop if silence 10 16
80
81
 Usage:
82
 Call parser_parse_actions(txt), where txt is the command text.
83
 GList *parser_parse_actions(gchar *txt);
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
84
 It will return a pointer to g_timer_list. This is a GList of TimerRec records.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
85
*/
86
87
#define MAX_TOKEN_LEN 128
88
89
typedef enum {TOK_NONE, TOK_NUMERIC, TOK_TIME, TOK_TEXT} TokenType;
90
91
typedef struct {
92
    TokenType type;
93
    gchar tok[MAX_TOKEN_LEN+1];
94
} TokenRec;
95
96
typedef struct {
97
    gchar *buf;
98
    gchar *pos;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
99
    gint len;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
100
    gchar back_ch;
101
    guint line_no;
102
} ParserRec;
103
104
typedef struct {
105
    gchar *label;
106
    gchar *translation;
107
} LangRec;
108
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
109
LangRec g_transtable[] =  {
110
    // Example: start at 10:30 pm
111
    {"start",   NULL},
112
113
    // Example: stop after 200 kb
114
    {"stop",    NULL},
115
116
    // Start/stop/pause at, after, if, on
117
    // Example: start at 10:30 pm
118
    {"at",      NULL},
119
19 by Osmo Antero Maatta
Added instructions for translators
120
    // Example: stop after 1 hour 20 min
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
121
    {"after",   NULL},
19 by Osmo Antero Maatta
Added instructions for translators
122
123
    // Example: stop if 2GB
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
124
    {"if",      NULL},
19 by Osmo Antero Maatta
Added instructions for translators
125
126
    // Example: start on voice | 14:00 pm
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
127
    {"on",      NULL},
1 by Osmo Antero Maatta
Initial import 17.jan.2011
128
19 by Osmo Antero Maatta
Added instructions for translators
129
    // Start/pause on "voice".
130
    // Example: start on voice | 14:00 pm
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
131
    {"voice",   NULL},
19 by Osmo Antero Maatta
Added instructions for translators
132
3 by Osmo Antero Maatta
Updated some filenames and traslations
133
    // Start/pause on "audio"
19 by Osmo Antero Maatta
Added instructions for translators
134
    // Example: start on audio | 14:00 pm
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
135
    {"audio",   NULL},
19 by Osmo Antero Maatta
Added instructions for translators
136
3 by Osmo Antero Maatta
Updated some filenames and traslations
137
    // Start/pause on "sound"
19 by Osmo Antero Maatta
Added instructions for translators
138
    // Example: start on sound | 14:00 pm
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
139
    {"sound",   NULL},
140
141
    // Time: hour
142
    // Example: stop after 1 hour | 1 h
143
    {"hour",    NULL},
144
145
    // Time: hour
146
    // Example: stop after 1 hour | 1 h
147
    {"h",       NULL},
148
149
    // Time: minutes
150
    // Example: stop after 20 minutes | 20 min | 20 m
151
    {"minutes", NULL},
152
153
    // Time: minutes
154
    // Example: stop after 20 minutes | 20 min | 20 m
155
    {"min",     NULL},
156
157
    // Time: minutes
158
    // Example: stop after 20 minutes | 20 min | 20 m
159
    {"m",       NULL},
160
161
    // Time: seconds
162
    // Example: pause after 60 seconds | 60 sec | 60 s
163
    {"seconds", NULL},
164
165
    // Time: seconds
166
    // Example: pause after 60 seconds | 60 sec | 60 s
167
    {"sec",     NULL},
168
169
    // Time: seconds
170
    // Example: pause after 60 seconds | 60 sec | 60 s
171
    {"s",       NULL},
172
173
    // Example: pause if 2000 bytes | 2000 byte
174
    {"bytes",   NULL},
175
176
    // Example: pause if 2000 bytes | 2000 byte
177
    {"bytes",   NULL},
1 by Osmo Antero Maatta
Initial import 17.jan.2011
178
3 by Osmo Antero Maatta
Updated some filenames and traslations
179
    // "|" or "or"
19 by Osmo Antero Maatta
Added instructions for translators
180
    // Example: pause if silence -20 dB | 2 GB or 10:20 pm
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
181
    {"or",      NULL},
1 by Osmo Antero Maatta
Initial import 17.jan.2011
182
3 by Osmo Antero Maatta
Updated some filenames and traslations
183
    // Clock time; ante meridiem, before midday
19 by Osmo Antero Maatta
Added instructions for translators
184
    // Example: start at 09:00 am
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
185
    {"am",      NULL},
3 by Osmo Antero Maatta
Updated some filenames and traslations
186
187
    // Clock time; post meridiem, after midday
19 by Osmo Antero Maatta
Added instructions for translators
188
    // Example: stop at 09:00 pm
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
189
    {"pm",      NULL},
1 by Osmo Antero Maatta
Initial import 17.jan.2011
190
};
191
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
192
// Global variables to this module
1 by Osmo Antero Maatta
Initial import 17.jan.2011
193
static ParserRec g_parser;
194
static TokenRec g_curtoken;
195
static TokenRec g_backtoken;
196
static GList *g_timer_list = NULL;
197
198
static void parser_init(gchar *txt);
199
static void parser_clear();
200
201
static void parser_free_node(TimerRec *tr);
202
203
static void parser_parse_action();
204
static void parser_parse_data();
205
206
static gboolean match_lang(gchar *tok, gchar *text);
207
static gboolean match_word(gchar *l_word, gchar *word, ...);
208
209
static LangRec *get_translated_str(gchar *label);
210
211
static gchar parser_get_ch();
212
static gchar parser_get_ch_ex();
213
static void parser_put_back_ch(gchar ch);
214
215
static void parser_get_token();
216
217
static TimerRec *parser_get_last();
218
static TimerRec *parser_add_action(gchar action);
219
static void parser_fix_list();
220
221
static void parser_print_error(gchar *msg);
222
223
static gchar *parser_type_to_str(gint type);
224
225
void parser_module_init() {
226
    LOG_DEBUG("Init timer-parser.c.\n");
227
228
    g_timer_list = NULL;
229
    parser_init(NULL);
230
}
231
232
void parser_module_exit() {
233
    LOG_DEBUG("Clean up timer-parser.c.\n");
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
234
1 by Osmo Antero Maatta
Initial import 17.jan.2011
235
    parser_clear();
236
237
    // Free list
238
    parser_free_list();
239
}
240
241
GList *parser_parse_actions(gchar *txt) {
242
    parser_clear();
243
    parser_init(txt);
244
245
    // 'S'tart... | S'T'op..., 'P'ause...
246
    parser_parse_action();
247
248
    parser_fix_list();
249
250
    // Return the list head
251
    return g_timer_list;
252
}
253
254
static void parser_init(gchar *txt) {
255
    // Init parser record
256
    if (txt) {
257
        g_parser.buf = g_utf8_strdown(txt, -1);
258
        g_parser.len = g_utf8_strlen(g_parser.buf, -1);
259
    } else {
260
        g_parser.buf = NULL;
261
        g_parser.len = 0;
262
    }
263
264
    g_parser.pos = g_parser.buf;
265
    g_parser.back_ch = '\0';
266
267
    g_parser.line_no = 1;
268
269
    *g_curtoken.tok = '\0';
270
    *g_backtoken.tok = '\0';
271
272
    // Free the existing TimerRec list
273
    parser_free_list();
274
}
275
276
static void parser_clear() {
277
    // Clear parser record
278
    if (g_parser.buf)
279
        g_free(g_parser.buf);
280
281
    parser_init(NULL);
282
}
283
284
static void parser_print_error(gchar *msg) {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
285
    LOG_ERROR("Timer command, line %d: %s.\n", g_parser.line_no, msg);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
286
}
287
288
static gboolean match_lang(gchar *tok, gchar *text) {
289
    // Check if the given token (tok) matches the text (in plain english or translated language)
290
    gboolean ret = !g_strcmp0(tok, text);
291
    if (ret) return TRUE;
292
293
    LangRec *lang_rec = get_translated_str(text);
294
    ret = FALSE;
295
    if (lang_rec) {
296
        ret = !g_strcmp0(tok, lang_rec->label);
297
        if (!ret)
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
298
            ret = !g_strcmp0(tok, lang_rec->translation);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
299
    }
300
    return ret;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
301
}
1 by Osmo Antero Maatta
Initial import 17.jan.2011
302
303
static gboolean match_word(gchar *l_word, gchar *word, ...) {
304
    // Check if l_word matches one of the given words (in plain english or translated language).
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
305
    va_list args;
306
    va_start(args, word);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
307
308
    gboolean found = FALSE;
309
310
    gchar *text = word;
311
    while (text) {
312
        if (match_lang(l_word, text)) {
313
            found = TRUE;
314
            break;
315
        }
316
        text = va_arg(args, gchar *);
317
    }
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
318
    va_end(args);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
319
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
320
    return found;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
321
}
322
323
static void parser_put_back_ch(gchar ch) {
324
    // Put back a character
325
    ParserRec *p = &g_parser;
326
    p->back_ch = ch;
327
}
328
329
static gchar parser_get_ch() {
330
    // Get next character
331
    ParserRec *p = &g_parser;
332
333
    if (!p->buf) return '\0';
334
335
    // Check back buf
336
    gchar ch = '\0';
337
    if (p->back_ch != '\0') {
338
        ch = p->back_ch;
339
        p->back_ch = '\0';
340
        return ch;
341
    }
342
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
343
    gchar *end = g_utf8_offset_to_pointer(p->buf, p->len - 1);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
344
345
    if (p->pos > end) return '\0';
346
347
    if (*p->pos == '\0') return '\0';
348
349
    if (*p->pos == '\n') {
350
        // Count lines
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
351
        p->line_no++;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
352
    }
353
354
    // The char
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
355
    ch = *p->pos;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
356
357
    // Advance head
358
    p->pos++;
359
360
    return ch;
361
}
362
363
static void parser_remove_space() {
364
    // Remove white spaces from the text
365
    gchar ch = parser_get_ch();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
366
    while (g_unichar_isspace(ch) && ch != '\0') {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
367
        ch = parser_get_ch();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
368
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
369
    parser_put_back_ch(ch);
370
}
371
372
static void parser_remove_comment() {
373
    // Remove characters until \n
374
    gchar ch = parser_get_ch();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
375
    while (ch != '\0' && ch != '\n') {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
376
        ch = parser_get_ch();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
377
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
378
}
379
380
static gchar parser_get_ch_ex() {
381
    // Get next char, remove spaces and comments
382
383
    // Remove spaces
384
    parser_remove_space();
385
    gchar ch = '\0';
386
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
387
LBL_1:
1 by Osmo Antero Maatta
Initial import 17.jan.2011
388
    ch = parser_get_ch();
389
    if (ch == '\0') return ch;
390
391
    if (g_unichar_isspace(ch)) {
392
        // Remove spaces
393
        parser_remove_space();
394
        goto LBL_1;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
395
    } else if (ch == '#') {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
396
        // Remove EOL comment
397
        parser_remove_comment();
398
        goto LBL_1;
399
    }
400
401
    return ch;
402
}
403
404
static TokenRec parser_get_token_ex() {
405
    // Get next token from the text.
406
    // Return token.
407
    TokenRec t;
408
    *t.tok = '\0';
409
    t.type = TOK_NONE;
410
411
    gchar ch = parser_get_ch_ex();
412
    if (ch == '\0') return t;
413
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
414
    // An integer or decimal number: +-0-9, 0-9.0-9
415
    // Ot time value of format ##:##:##
1 by Osmo Antero Maatta
Initial import 17.jan.2011
416
    gint i = 0;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
417
    while ((g_unichar_isdigit(ch) || g_utf8_strchr(".:+-", 4, ch)) && ch != '\0') {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
418
        if (i >= MAX_TOKEN_LEN) break;
419
        t.tok[i++] = ch;
420
        ch = parser_get_ch();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
421
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
422
423
    // Got a numeric token?
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
424
    if (i > 0) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
425
        // Put last ch back
426
        parser_put_back_ch(ch);
427
428
        // 0-terminate
429
        t.tok[i] = '\0';
430
431
        // Time notation hh:mm:ss?
432
        if (g_utf8_strchr(t.tok, g_utf8_strlen(t.tok, -1), ':'))
433
            t.type = TOK_TIME;
434
        else
435
            // Got numeric value
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
436
            t.type = TOK_NUMERIC;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
437
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
438
        return t;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
439
    }
440
441
    // Is it a "|"  (OR token)?
442
    if (ch == '|') {
443
        g_utf8_strncpy(t.tok, "|", 1);
444
        t.type = TOK_TEXT;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
445
        return t;
446
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
447
    // Is it a "%" token?
448
    else if (ch == '%') {
449
        g_utf8_strncpy(t.tok, "%", 1);
450
        t.type = TOK_TEXT;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
451
        return t;
452
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
453
454
    // Is it a letter, a character?
455
    // Read a string.
456
    i = 0;
457
458
    // Note: we do NOT use ">", "<", ">=", "<=", "=", "==" tokens here. We merely get rid of them.
459
    // Just in case user writes "stop if filezize >= 100 MB". This is illegal syntax but we make our best to interpret it like "stop if 100 MB".
460
    while ((g_unichar_isalpha(ch) || g_utf8_strchr("><=", -1, ch)) && ch != '\0') {
461
        if (i >= MAX_TOKEN_LEN) break;
462
        t.tok[i++] = ch;
463
        ch = parser_get_ch();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
464
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
465
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
466
    if (i > 0) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
467
        // Put last ch back
468
        parser_put_back_ch(ch);
469
470
        // 0-terminate
471
        t.tok[i] = '\0';
472
        t.type = TOK_TEXT;
473
        return t;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
474
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
475
476
    return t;
477
}
478
479
static void parser_put_token_back() {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
480
    // Put token back
1 by Osmo Antero Maatta
Initial import 17.jan.2011
481
    g_backtoken = g_curtoken;
482
}
483
484
static void parser_get_token() {
485
    // Get next token. Check back buffer.
486
487
    if (*g_backtoken.tok) {
488
        g_curtoken = g_backtoken;
489
        *g_backtoken.tok = '\0';
490
        return;
491
    }
492
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
493
    // Set the current token
494
    g_curtoken = parser_get_token_ex();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
495
496
    // puts(g_curtoken.tok);
497
}
498
499
static LangRec *get_translated_str(gchar *label) {
500
    // Get LangRec for the given text label
501
    guint siz = sizeof(g_transtable) / sizeof(g_transtable[0]);
502
    guint i = 0;
503
    for (i=0; i< siz; i++) {
504
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
505
        if (!g_strcmp0(label, g_transtable[i].label))
1 by Osmo Antero Maatta
Initial import 17.jan.2011
506
            return &g_transtable[i];
507
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
508
        if (!g_strcmp0(label, g_transtable[i].translation))
1 by Osmo Antero Maatta
Initial import 17.jan.2011
509
            return &g_transtable[i];
510
    }
511
    return NULL;
512
}
513
514
static gboolean starts_with(gchar *s, gchar *what) {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
515
    // Test if string s starts with "what" (has that prefix)
516
517
    if (!(s && what)) return FALSE;
518
519
    const gchar *p = g_strstr_len(s, -1, what);
520
    // The "s" starts with "what", at 1.st byte.
521
    return (p == s);
522
}
1 by Osmo Antero Maatta
Initial import 17.jan.2011
523
524
static double tok_to_num(gchar *tok) {
525
    // Convert tok to double
526
    return atof(tok);
527
}
528
529
static void normalize_time(TimerRec *tr) {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
530
    // Normalize decimal values to real hours/minutes/seconds
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
531
    // Eg. 2.5h means 2h 30minutes 0seconds
1 by Osmo Antero Maatta
Initial import 17.jan.2011
532
533
    // Convert to 24 hours clock
534
    if (!g_strcmp0(tr->label, "pm") && tr->val[0] <= 12) {
535
        tr->val[0] += 12.0;
536
    }
537
538
    if (tr->val[0] > 24.0) tr->val[0] = 24.0;
539
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
540
    gdouble secs = tr->val[0]*3600.0 + tr->val[1]*60.0 + tr->val[2];
541
542
    // Convert back to hour/min/sec
543
    tr->val[0] = (guint)(secs / 3600);
544
    secs = secs - (tr->val[0]*3600);
545
546
    tr->val[1] = (guint)(secs / 60);
547
548
    tr->val[2] = secs - (tr->val[1]*60);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
549
}
550
551
static void parser_parse_data() {
552
553
    // Get the actual TimerRec record (last one)
554
    TimerRec *tr = parser_get_last();
555
556
    // Safety counter
557
    guint loop_count = 0;
558
559
    // State for some intricat values
560
    guint state = 0;
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
561
562
    gboolean seconds_set = FALSE;
563
    gboolean threshold_set = FALSE;
564
1 by Osmo Antero Maatta
Initial import 17.jan.2011
565
    while (1) {
566
567
        if (loop_count++ > 500) {
568
            // Insane loop
569
            break;
570
        }
571
572
        if (g_curtoken.type == TOK_NONE) {
573
            break;
574
        }
575
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
576
        gdouble val = 0.0;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
577
        gint tok_type = -1;
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
578
        // Numeric values for clock time/duration, file size or threshold (in dB, % or plain value [0, 1.0])
1 by Osmo Antero Maatta
Initial import 17.jan.2011
579
        if (g_curtoken.type == TOK_NUMERIC) {
580
581
            // Default data type
582
            // tr->data_type = 't';
583
584
            tok_type = g_curtoken.type;
585
586
            val = tok_to_num(g_curtoken.tok);
587
588
            // Next token
589
            parser_get_token();
590
        }
591
592
        // Clock time of format hh:mm:ss
593
        else if (g_curtoken.type == TOK_TIME) {
594
            // hh
595
            // hh:mm
596
            // hh:mm:ss
597
            tr->data_type = 't';
598
599
            tok_type = g_curtoken.type;
600
601
            // Split the time string on ":"
602
            // eg. 10:20:30 (=10 hours, 20 minutes, 30 seconds)
603
            gchar **args = g_strsplit_set(g_curtoken.tok, ":", -1);
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
604
1 by Osmo Antero Maatta
Initial import 17.jan.2011
605
            guint i =0;
606
            for (i=0; args[i]; i++) {
607
                if (i < 3) {
608
                    tr->val[i] = tok_to_num(args[i]);
609
                }
610
            }
611
612
            // Free the string list
613
            g_strfreev(args);
614
615
            // Next token
616
            parser_get_token();
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
617
618
            state = 3;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
619
        }
620
621
        // Token is string/text
622
623
        // am | pm
624
        // 11 am
625
        // 07 pm
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
626
        if (match_word(g_curtoken.tok, "am", NULL) ||
627
                match_word(g_curtoken.tok, "pm", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
628
629
            g_utf8_strncpy(tr->label, g_curtoken.tok ,-1);
630
631
            if (tok_type == TOK_NUMERIC) {
632
                tr->val[0] = val;
633
            }
634
            tr->data_type = 't';
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
635
636
            state = 3;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
637
        }
638
        // filesize: bytes | kb | mb | gb | tb
639
        // 123 mb
640
        // 14.5 gb
641
        else if (match_word(g_curtoken.tok, "bytes", "byte", NULL)) {
642
            tr->data_type = 'f';
643
            tr->val[0] = val;
644
            g_utf8_strncpy(tr->label, "bytes" , -1);
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
645
        } else if (starts_with(g_curtoken.tok, "kb") || starts_with(g_curtoken.tok, "kib")) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
646
            tr->data_type = 'f';
647
            tr->val[0] = val * 1E3;
648
            g_utf8_strncpy(tr->label, "kb" , -1);
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
649
        } else if (starts_with(g_curtoken.tok, "mb") || starts_with(g_curtoken.tok, "mib")) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
650
            tr->data_type = 'f';
651
            tr->val[0] = val * 1E6;
652
            g_utf8_strncpy(tr->label, "mb" , -1);
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
653
        } else if (starts_with(g_curtoken.tok, "gb") || starts_with(g_curtoken.tok, "gib")) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
654
            tr->data_type = 'f';
655
            tr->val[0] = val * 1E9;
656
            g_utf8_strncpy(tr->label, "gb" , -1);
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
657
        } else if (starts_with(g_curtoken.tok, "tb") || starts_with(g_curtoken.tok, "tib")) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
658
            tr->data_type = 'f';
659
            tr->val[0] = val * 1E12;
660
            g_utf8_strncpy(tr->label, "tb" , -1);
661
        }
662
663
        // hours, minutes, seconds
7 by Osmo Antero Maatta
Improved timer commands
664
        else if (match_word(g_curtoken.tok, "h", "hour", "hours", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
665
            // 'd' = time duration
666
            tr->data_type = 'd';
667
            tr->val[0] = val;
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
668
            state = 3;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
669
        } else if (match_word(g_curtoken.tok, "m", "min", "minutes", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
670
            // 'd' = time duration
671
            tr->data_type = 'd';
672
            tr->val[1] = val;
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
673
            state = 3;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
674
        } else if (match_word(g_curtoken.tok, "s", "sec", "seconds", NULL)) {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
675
676
            if (str_length0(tr->threshold_unit) < 1) /* == '\0'*/ {
677
                // Save threshold value (eg. 0.4)
678
                // "stop if silence 0.4 5s" => "stop if silence 5s 0.4"
679
                tr->threshold = tr->val[2];
680
            }     
681
1 by Osmo Antero Maatta
Initial import 17.jan.2011
682
            // 'd' = time duration
683
            tr->data_type = 'd';
684
            tr->val[2] = val;
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
685
686
            state = 3;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
687
        }
688
689
        // "silence"
690
        else if (match_word(g_curtoken.tok, "silence", NULL)) {
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
691
            tr->data_type = 'x';
1 by Osmo Antero Maatta
Initial import 17.jan.2011
692
            g_utf8_strncpy(tr->label, "silence" , -1);
693
            state = 2;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
694
        }
695
        // "voice" | "audio" | "sound"
1 by Osmo Antero Maatta
Initial import 17.jan.2011
696
        else if (match_word(g_curtoken.tok, "voice", NULL)) {
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
697
            tr->data_type = 'x';
1 by Osmo Antero Maatta
Initial import 17.jan.2011
698
            g_utf8_strncpy(tr->label, "voice" , -1);
699
            state = 2;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
700
        } else if (match_word(g_curtoken.tok, "audio", NULL)) {
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
701
            tr->data_type = 'x';
1 by Osmo Antero Maatta
Initial import 17.jan.2011
702
            g_utf8_strncpy(tr->label, "audio" , -1);
703
            state = 2;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
704
        } else if (match_word(g_curtoken.tok, "sound", NULL)) {
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
705
            tr->data_type = 'x';
1 by Osmo Antero Maatta
Initial import 17.jan.2011
706
            g_utf8_strncpy(tr->label, "sound" , -1);
707
            state = 2;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
708
        }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
709
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
710
        // threshold dB (-100dB - -5dB)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
711
        else if (match_word(g_curtoken.tok, "db", "decibel", NULL)) {
712
            // TODO: Check if "silence", "voice" | "audio" | "sound" token detected
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
713
            g_utf8_strncpy(tr->threshold_unit, "db", -1);
714
            tr->threshold = val;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
715
        }
716
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
717
        // Threshold % (0 - 100%)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
718
        else if (match_word(g_curtoken.tok, "%", NULL)) {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
719
            g_utf8_strncpy(tr->threshold_unit, "%", -1);
720
            tr->threshold = val;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
721
        }
722
723
        // "start", "stop", "pause"
724
        else if (match_word(g_curtoken.tok, "start", "stop", "pause", "|", NULL)) {
725
726
            // Put token back
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
727
            parser_put_token_back();
728
1 by Osmo Antero Maatta
Initial import 17.jan.2011
729
            // Return from this loop
730
            break;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
731
        } else {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
732
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
733
            if (state == 2)  {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
734
735
                // silence|voice 10 0.4  (take 10)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
736
                if (tok_type == TOK_NUMERIC) {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
737
                    tr->val[2] = val; // duration 10s
1 by Osmo Antero Maatta
Initial import 17.jan.2011
738
                }
739
                state = 3;
740
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
741
                seconds_set = TRUE;
742
1 by Osmo Antero Maatta
Initial import 17.jan.2011
743
                // Put token back
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
744
                parser_put_token_back();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
745
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
746
            } else if (state == 3) {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
747
748
                // silence|voice 7 30  # take 30 (=30%)
749
                // silence|voice 7 0.3  # take 0.3 (=30%)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
750
                if (tok_type == TOK_NUMERIC) {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
751
                    tr->threshold = val;
752
                    if (tr->threshold > 1.0) {
753
                      // Value between [1%, 100%], assume it's a %-value
754
                      g_utf8_strncpy(tr->threshold_unit, "%", -1);
755
                    } else {
756
                      // Value between [0, 1.0] is a plain number
757
                      *(tr->threshold_unit) = '\0';
758
                    }
759
                    
1 by Osmo Antero Maatta
Initial import 17.jan.2011
760
                }
761
                state = 0;
762
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
763
                threshold_set = TRUE;
764
1 by Osmo Antero Maatta
Initial import 17.jan.2011
765
                // Put token back
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
766
                parser_put_token_back();
767
            } else if (tok_type == TOK_NUMERIC) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
768
769
                if (val != 0.0) {
770
                    // Set default
771
                    tr->data_type = 't';
772
                    tr->val[0] = val;
773
                }
774
775
                // Put token back
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
776
                parser_put_token_back();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
777
778
                break;
779
            }
780
781
        }
782
783
        // Next token
784
        parser_get_token();
785
786
    } // while...
787
183 by osmoma at gmail
Fixed error in src/timer-parser.c
788
    if (!g_strcmp0(tr->label, "silence") ||
789
        !g_strcmp0(tr->label, "voice") ||
790
        !g_strcmp0(tr->label, "sound") ||
791
        !g_strcmp0(tr->label, "audio")) {
792
    
793
        tr->data_type = 'x'; 
794
    }      
795
1 by Osmo Antero Maatta
Initial import 17.jan.2011
796
    // We got time value?
797
    if (tr->data_type == 't' || tr->data_type == 'd') {
798
        // Normalize hours/minutes/seconds. Eg. 2.5 h becomes 2 h 30 minutes.
799
        normalize_time(tr);
800
    }
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
801
183 by osmoma at gmail
Fixed error in src/timer-parser.c
802
    // start if voice 0.5 --> start if voice 0 sec 0.5  (0.5 is a threshold level, not seconds!)
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
803
    if (seconds_set == TRUE && threshold_set == FALSE && tr->val[2] <= 1.00) {
804
        tr->threshold = tr->val[2];
805
        tr->val[2] = 0.0;
806
    }
807
1 by Osmo Antero Maatta
Initial import 17.jan.2011
808
}
809
810
static void parse_parse_line() {
811
    // start | stop | pause at|after|if|on 10:10:12 am/pm | 100 bytes/kb/mb/gb/tb | silence/voice/sound
812
813
    // Get next token
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
814
    parser_get_token();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
815
816
    // Get last TimerRec
817
    TimerRec *tr = parser_get_last();
818
819
    // Remove action preposition; "at" | "after" | "if" | "on"
820
    gboolean got_action_prep = FALSE;
821
822
    if (match_word(g_curtoken.tok, "at", NULL)) {
823
        got_action_prep = TRUE;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
824
    } else if (match_word(g_curtoken.tok, "after", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
825
        tr->action_prep = 'a'; // 'a' = after
826
        got_action_prep = TRUE;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
827
    } else if (match_word(g_curtoken.tok, "if", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
828
        got_action_prep = TRUE;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
829
    } else if (match_word(g_curtoken.tok, "on", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
830
        got_action_prep = TRUE;
831
    }
832
833
    // Consumed token?
834
    if (got_action_prep) {
835
        // Get next token
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
836
        parser_get_token();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
837
    }
838
839
    while (1) {
840
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
841
        // Parse data
1 by Osmo Antero Maatta
Initial import 17.jan.2011
842
        parser_parse_data();
843
844
        // Next token
845
        parser_get_token();
846
847
        // It is "|"  or "or"?
848
        if (!g_strcmp0(g_curtoken.tok, "|") || match_word(g_curtoken.tok, "or", NULL)) {
849
850
            // Add new record
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
851
            parser_add_action('X'); // 'X' = unknown at the moment
1 by Osmo Antero Maatta
Initial import 17.jan.2011
852
853
            // Next token
854
            parser_get_token();
855
856
        } else {
857
858
            // Put back
859
            parser_put_token_back();
860
            return;
861
        }
862
    }
863
}
864
865
static void parser_parse_action() {
866
867
    // Get first token
868
    parser_get_token();
869
870
    while (g_curtoken.type != TOK_NONE) {
871
872
        if (match_word(g_curtoken.tok, "start", NULL)) {
873
            // "start ..."
874
            parser_add_action('S'); // 'S' = start recording
875
876
            // Parse rest of the "start ..." line
877
            parse_parse_line();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
878
        } else if (match_word(g_curtoken.tok, "stop", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
879
            // "stop ..."
880
            parser_add_action('T'); // 'T' = sTop recording
881
882
            // Parse rest of the "stop ..." line
883
            parse_parse_line();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
884
        } else if (match_word(g_curtoken.tok, "pause", NULL)) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
885
            // "pause ..."
886
            parser_add_action('P'); // 'P' = Pause recording
887
888
            // Parse rest of the "pause ..." line
889
            parse_parse_line();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
890
        }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
891
892
        else {
893
            // Unknown token
894
            gchar *msg = g_strdup_printf("Unknown token: %s.\n", g_curtoken.tok);
895
            parser_print_error(msg);
896
            g_free(msg);
897
        }
898
899
        // Get next token
900
        parser_get_token();
901
    }
902
}
903
904
static gchar *parser_type_to_str(gint type) {
905
    // Convert type to text (for debugging)
906
    switch (type) {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
907
    case TOK_NUMERIC:
908
        return "TOK_NUMERIC";
909
    case TOK_TIME:
910
        return "TOK_TIME";
911
    case TOK_TEXT:
912
        return "TOK_TEXT";
913
    default:
914
        return "UNKNOWN TOKEN";
1 by Osmo Antero Maatta
Initial import 17.jan.2011
915
    }
916
}
917
918
919
// ==========================================
920
921
static void parser_free_node(TimerRec *tr) {
922
    // Free TimerRec node
923
    if (tr) g_free(tr);
924
}
925
926
static TimerRec *timer_new_rec(gchar action) {
927
    // Create new TimerRec node
928
    TimerRec *tr = g_malloc0(sizeof(TimerRec));
929
    tr->action = action;
930
    tr->day_of_year = -1;
931
    tr->done = FALSE;
932
    return tr;
933
}
934
935
static TimerRec *parser_add_action(gchar action) {
936
    // Add new TimerRec node to g_timer_list
937
    TimerRec *tr = tr = timer_new_rec(action);
938
    g_timer_list = g_list_append(g_timer_list, tr);
939
    return tr;
940
}
941
942
static TimerRec *parser_get_last() {
943
    // Return last TimerRec node from g_timer_list
944
    TimerRec *tr = NULL;
945
    if (!g_timer_list) {
946
        tr = timer_new_rec('X'); // X = Unknown action
947
        g_timer_list = g_list_append(g_timer_list, tr);
948
    }
949
950
    GList *last =  g_list_last(g_timer_list);
951
    return (TimerRec*)last->data;
952
}
953
954
static void parser_fix_list() {
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
955
    // Fix OR'ed commands.
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
956
    // Eg. "stop if 100 MB | 1 h 20 min | silence" becomes three separate nodes. 
957
    // It becomes:
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
958
    // 	   "stop if 100 MB"
959
    //     "stop if 1 h 20 min"
960
    //     "stop if silence"
961
    //
1 by Osmo Antero Maatta
Initial import 17.jan.2011
962
    // Replace 'X' with previous node's action char.
963
    // Replace action_prep with previous node's action_prep.
964
    GList *item = g_list_first(g_timer_list);
965
    gchar last_action = '\0';
966
    gchar last_prep = '\0';
967
    while (item) {
968
        TimerRec *tr = (TimerRec*)item->data;
969
970
        if (last_action != '\0' && tr->action == 'X') {
971
            tr->action = last_action;
972
        }
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
973
1 by Osmo Antero Maatta
Initial import 17.jan.2011
974
        if (last_action != tr->action) {
975
            last_prep = '\0';
976
        }
977
978
        last_action = tr->action;
979
980
        if (last_prep != '\0' && tr->action_prep == '\0') {
981
            tr->action_prep = last_prep;
982
        }
983
        last_prep = tr->action_prep;
984
985
        item = g_list_next(item);
986
    }
987
}
988
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
989
const gchar *parser_get_action_name(gchar action) {
990
    switch (action) {
991
    case 'S':
992
        return "Start recording";
993
994
    case 'c':
995
        return "Continue recording";
996
997
    case 'T':
998
        return "Stop recording";
999
1000
    case 'p':
1001
    case 'P':
1002
        return "Pause recording";
1003
1004
    default:
1005
        return "Unknown timer command";
1006
    }
1007
}
1008
1 by Osmo Antero Maatta
Initial import 17.jan.2011
1009
void parser_print_rec(TimerRec *tr) {
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
1010
    gchar *action_str = NULL;
1011
    switch (tr->action) {
1012
    case 'S':
1013
        action_str = "Start";
1014
        break;
1015
1016
    case 'T':
1017
        action_str = "sTop";
1018
        break;
1019
1020
    case 'P':
1021
        action_str = "Pause";
1022
        break;
1023
    }
1024
1025
    LOG_MSG("action:%c (%s)\n", tr->action, action_str);
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
1026
1027
    // Commented out:
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
1028
    // LOG_MSG("\taction preposition (if/after/on/etc.):%c\n", tr->action_prep);
1029
1030
    // Is it silence/voice/sound/audio command?
1031
    if (!g_strcmp0(tr->label, "silence") ||
1032
            !g_strcmp0(tr->label, "voice") ||
1033
            !g_strcmp0(tr->label, "sound") ||
1034
            !g_strcmp0(tr->label, "audio")) {
1035
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1036
        LOG_MSG("\tlabel: %s, delay:%3.1f %3.1f %3.1f threshold:%3.3f %s\n", tr->label, tr->val[0], tr->val[1], tr->val[2], 
1037
                tr->threshold, tr->threshold_unit);
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
1038
    }
1039
1040
    switch (tr->data_type) {
1041
    case 'd':
1042
        LOG_MSG("\t%c, time duration: %3.1f %3.1f %3.1f\n", tr->data_type, tr->val[0], tr->val[1], tr->val[2]);
1043
        break;
1044
1045
    case 't':
1046
        LOG_MSG("\t%c, clock time: %3.1f %3.1f %3.1f\n", tr->data_type, tr->val[0], tr->val[1], tr->val[2]);
1047
        break;
1048
1049
    case 'f':
1050
        LOG_MSG("\t%c, filesize: %3.1f  (from %s)\n", tr->data_type, tr->val[0], tr->label);
1051
        break;
1052
1053
    case 'x':
1054
        ;
1055
        break;
1056
1057
    default:
1058
        LOG_MSG("\tUnknown data type in timer command.\n");
1059
    }
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
1060
1 by Osmo Antero Maatta
Initial import 17.jan.2011
1061
}
1062
1063
void parser_print_list(GList *list) {
1064
    LOG_MSG("---------------------------\n");
1065
    g_list_foreach(list, (GFunc)parser_print_rec, NULL);
1066
}
1067
1068
void parser_free_list() {
1069
    // Free the TimerRec list
1070
    g_list_foreach(g_timer_list, (GFunc)parser_free_node, NULL);
1071
    g_list_free(g_timer_list);
1072
    g_timer_list = NULL;
1073
}
1074