~audio-recorder/audio-recorder/trunk

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