-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio_trigger.c
129 lines (113 loc) · 2.96 KB
/
audio_trigger.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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <math.h>
#include <getopt.h>
#include <time.h>
#define BUF_SIZE 1024
void Usage(int asked, char *argv0)
{
fprintf(asked ? stdout : stderr,
"Usage: %s [OPTIONS]...\n"
"\t -p pid - process ID to send signal to\n"
"\t -d delay - post trigger delay (ms)\n"
"\t -t threshold - Sound power threshold at which to send signal\n"
"\t -o out_file - Output filename (default stdout)\n"
"\t -i in_file - file to get input (default stdin)\n\n\n"
"Example:\n"
" $ arecord -D hw:1,0 -f S16_LE | %s -p `pidof raspivid` -d 1000\n"
,argv0, argv0);
exit(-1);
}
void get_options(
int argc,
char *argv[],
int *pid,
int *post_delay,
int *threshold,
FILE **in,
FILE **out)
{
int c;
while((c = getopt(argc, argv, "hp:d:t:o:i:")) != -1)
{
switch(c)
{
case 'h':
Usage(1, argv[0]);
case 'p':
*pid = atoi(optarg);
break;
case 'd':
*post_delay = atoi(optarg);
break;
case 't':
*threshold = atoi(optarg);
break;
case 'o':
*out = fopen(optarg, "wb");
if(*out == NULL)
{
fprintf(stderr, "Failed to open output file %s\n", optarg);
exit(-1);
}
break;
case 'i':
*in = fopen(optarg, "rb");
if(*in == NULL)
{
fprintf(stderr, "Failed to open input file %s\n", optarg);
exit(-1);
}
break;
default:
Usage(0, argv[0]);
}
}
}
void main(int argc, char * argv[])
{
FILE * in = stdin;
FILE * out = stdout;
int pid = 0;
int post_delay = 0;
int threshold = 0;
short audio_data[BUF_SIZE];
get_options(argc, argv, &pid, &post_delay, &threshold, &in, &out);
fread(audio_data, sizeof(audio_data[0]), BUF_SIZE, in);
while(1)
{
short a_max = 0;
long long sum_squares = 0;
int i;
int power;
int count;
count = fread(audio_data, sizeof(audio_data[0]), BUF_SIZE, in);
if(count <= 0)
exit(0);
for(i = 0; i < BUF_SIZE; i++)
{
a_max = audio_data[i] > a_max ? audio_data[i] : a_max;
sum_squares += (long long) audio_data[i] * (long long) audio_data[i];
}
power = (int) sqrt(sum_squares / BUF_SIZE);
fprintf(out, "%f\n", sqrt(sum_squares / BUF_SIZE));
if(a_max == 32767)
{
printf("CLIPPING\n");
}
if(threshold && power > threshold)
{
if(post_delay)
{
struct timespec delay;
delay.tv_sec = post_delay / 1000;
delay.tv_nsec = ((long) post_delay - (delay.tv_sec * 1000)) * 1000000;
nanosleep(&delay, &delay);
}
if(pid)
kill(pid, SIGUSR1);
exit(0);
}
}
}