-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.c
190 lines (169 loc) · 4.76 KB
/
remote.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* remote.c
*
* Copyright (C) 2019 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* parse audio data as a remote protocol
*
* remote [-f] [-l] [-i] [-d n] (file|dev) -- [amplify_factor [trigger_bound]]
* -f input is a sequence of numbers in ascii, one per line,
* instead of an AU file
* -c allow receiving the output of irblast
* -l log input to log.au or log.txt
* -d n debug protocol n, from 1 to 14 so far
* amplify_factor
* -1 to invert, default 1
* trigger_bound
* the default is to use the background noise canceler,
* which requires a bit of signal absence at the beginning
* and derives the bound from it
* file|dev
* input is read from file if it exists, otherwise from audio
* device dev (list available: arecord -L)
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include "microphone.h"
#include "filters.h"
#include "protocols.h"
/*
* filter debugging: stop the chain of filters at some point and print
*/
#define STOPHERE \
printf("%d\n", value); \
if (status.flush) \
fflush(stdout); \
continue;
/*
* main
*/
int main(int argc, char *argv[]) {
int opt;
char *filename, *logfile = NULL;
int debug, ascii, valleyfilter;
int bound;
double factor;
struct status status;
void *read, *microphone, *log;
void *valley, *diff, *amplify, *maximal, *stabilize;
void *background, *trigger, *runlength;
int value;
struct protocols_status *protocols_status;
struct key *key;
/* arguments */
ascii = 0;
valleyfilter = 0;
debug = 0;
while (-1 != (opt = getopt(argc, argv, "fcld:")))
switch (opt) {
case 'l':
logfile = "log.au";
break;
case 'f':
ascii = 1;
break;
case 'c':
valleyfilter = 1;
break;
case 'd':
debug = atoi(optarg);
break;
}
if (ascii && logfile)
logfile = "log.txt";
argc -= optind - 1;
argv += optind - 1;
if (argc - 1 < 1)
filename = "default";
else
filename = argv[1];
factor = argc - 1 >= 2 ? atof(argv[2]) : 1;
bound = argc - 1 >= 3 ? atoi(argv[3]) : -1;
/* init filters and protocols */
read = read_init(filename, ascii, &status);
if (read != NULL)
microphone = NULL;
else {
microphone = microphone_init(filename, &status);
if (microphone == NULL) {
printf("cannot open input file\n");
exit(EXIT_FAILURE);
}
}
log = log_init(logfile, ascii, &status);
valley = valley_init(10, &status);
diff = diff_init(&status);
amplify = amplify_init(factor, &status);
maximal = maximal_init(11, &status);
stabilize = stabilize_init(&status);
trigger = trigger_init(bound, &status);
background = background_init(&status);
runlength = runlength_init(&status);
protocols_status = protocols_init(debug);
/* process values */
while (! status.ended) {
// filter testing: STOPHERE to cut the pipe of filters short
value = 0;
if (read)
FILTER_VALUE(read, value, read, &status)
if (microphone)
FILTER_VALUE(microphone, value, microphone, &status)
FILTER_VALUE(log, value, log, &status)
if (valleyfilter)
FILTER_VALUE(valley, value, valley, &status);
FILTER_VALUE(diff, value, diff, &status)
FILTER_VALUE(amplify, value, amplify, &status)
FILTER_VALUE(stabilize, value, stabilize, &status)
FILTER_VALUE(maximal, value, maximal, &status)
if (bound == -1)
FILTER_VALUE(background, value, background, &status)
else
FILTER_VALUE(trigger, value, trigger, &status)
FILTER_VALUE(runlength, value, runlength, &status)
if (! debug) {
printf("*");
fflush(stdout);
}
key = protocols_value(value, protocols_status);
if (key) {
printf("\n");
printkey(key);
printf("\n");
}
}
/* finish filters */
if (read)
read_end(read, &status);
if (microphone)
microphone_end(microphone, &status);
log_end(log, &status);
valley_end(valley, &status);
diff_end(diff, &status);
amplify_end(amplify, &status);
stabilize_end(stabilize, &status);
maximal_end(maximal, &status);
trigger_end(trigger, &status);
background_end(background, &status);
value = runlength_end(runlength, &status);
protocols_value(value, protocols_status);
protocols_end(protocols_status);
if (! debug)
printf("\n");
return EXIT_SUCCESS;
}