/
main.c
186 lines (147 loc) · 5.05 KB
/
main.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
/***************************************************************************
* 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 2 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "vocal.h"
#include "fdct.h"
typedef enum { capture, append } mode;
void helpandusage() {
fprintf (stderr,
"---=== BlackLight's & evilsocket's vocal recognition v.0.1b ===---\n"
"author @ blacklight@autistici.org\n"
"author @ evilsocket@gmail.com\n\n"
"Usage: vocal [-d <device>] [-a] [-c <file>] [-h]\n\n"
"\t-d <device>\tUse a different device instead the default one (/dev/dsp)\n"
"\t-a\t\tAppend a new vocal sample with an associated command\n"
"\t-c <file>\tUse a different rc file (default: ~/.vocalrc)\n"
"\t-h\t\tPrint this help and exit\n"
);
}
int main (int argc, char **argv) {
int i, j, dsp, fd, ch, size, recognized;
char *line, *tmp, *cmd = NULL, *fname = NULL, *device = NULL;
char **match = NULL;
fdct_t dct;
double sum, deviance, value;
double *neutral;
FILE *fp;
time_t t1, t2;
u8 *buf;
mode m = capture;
while ((ch=getopt(argc, argv, "ahc:d:"))>0) {
switch(ch){
case 'd' : device = strdup(optarg); break;
case 'a' : m = append; break;
case 'c' : fname = strdup(optarg); break;
case 'h' : helpandusage();
return 0; break;
default :
helpandusage();
return 1;
}
}
buf = (u8*) malloc(TOTSIZE);
neutral = (double*) malloc(TOTSIZE*sizeof(double));
memset(buf, 0x80, TOTSIZE );
if (!fname) {
fname = (char*) malloc(0x100);
snprintf (fname, 0x100, "%s/.vocalrc", getenv("HOME"));
}
if ((fd=open("neutral.ft", O_RDONLY, 0644))<0) {
perror ("could not find file neutral.ft");
return 1;
}
read (fd, neutral, TOTSIZE*sizeof(double));
close(fd);
if ((dsp = init_dsp( device ))<0)
return 1;
memset (buf, 0x0, TOTSIZE);
printf ("now capturing, say something...\n");
if (read(dsp, buf, TOTSIZE) != TOTSIZE) {
perror("read the wrong number of bytes");
return 1;
}
close(dsp);
printf ("capture done, computing DCT...\n" );
t1 = time((unsigned) NULL);
fdct_init( &dct, buf );
sum = fdct_sum( &dct, 36927.96, NULL/*neutral*/, TOTSIZE );
fdct_release(&dct);
t2 = time((unsigned) NULL);
printf ("DCT computing: done in %u seconds\n", (unsigned int) (t2-t1));
printf ("vocal sequence successfully acquired, Fourier sum = %g\n", sum);
if (m == append) {
printf ("which command would you like to associate for this vocal sequence? ");
cmd = getline(stdin);
if (!(fp=fopen(fname,"a"))) {
perror ("Unable to open configuration file");
return 1;
}
fprintf (fp, "%s: %g\n", cmd, sum);
fclose(fp);
free(cmd);
printf ("sequence successfully saved\n");
} else {
if (!(fp=fopen(fname,"r"))) {
perror ("Unable to open configuration file");
return 1;
}
recognized=0;
for (i=1; (line=getline(fp)) && !feof(fp); i++) {
if ((match=preg_match("^.+:\\s", line, &size))) {
tmp = strdup(match[0]);
tmp[strlen(match[0])-2] = 0;
if ((match=preg_match(":\\s.+$", line, &size))) {
for (j=2; j<strlen(match[0]); j++)
match[0][j-2] = match[0][j];
match[j-2]=0;
value = atof(match[0]);
double delta = ABS(ABS(value)-ABS(sum));
if ( delta <= 0.025 ) {
if (!recognized) {
deviance = delta;
cmd = strdup(tmp);
recognized = 1;
} else {
if (delta < deviance) {
deviance = delta;
cmd = strdup(tmp);
}
}
}
} else {
fprintf (stderr, "Parse error at %s:%d\n", fname, i);
return 1;
}
free(tmp);
} else {
fprintf (stderr, "Parse error at %s:%d\n", fname, i);
return 1;
}
}
if (recognized) {
printf ("value = %g, deviance = %g: vocal command recognized: %s\n",
value, deviance, cmd);
cmd = (char*) realloc(cmd, strlen(cmd)+3);
sprintf (cmd, "%s &", cmd);
system (cmd);
free(cmd);
} else
printf ("I'm sorry, I'm not smart enough to understand what you said :(\n");
}
if (fname)
free(fname);
return 0;
}