-
Notifications
You must be signed in to change notification settings - Fork 0
/
dft_main.c
123 lines (113 loc) · 3.24 KB
/
dft_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
/*
* terminalDFT. DFT terminal computation and plotting
*
* Copyright (C) 2021 Alfredo Gonzalez Calvin
*
* 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
* 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 <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "dft_calc.h"
#include "dft_show.h"
#define OPEN_CLOSE_FAILED 1
#define DFT_FAILED 2
#define EXEC_FAILED 3
#define SIGNAL_FAILED 4
#define RM_FAILED 5
#define AUDIOFILE_PATH "./audiofile"
#define AUDIOPROG_PATH "/usr/bin/arecord"
char *audio_command[] = {"arecord", "-q", "-c", "1", "-s", "1024",
"-r", "8000", "-f", "U8", "-t", "raw",
"./audiofile", NULL};
int end_proccess = 0;
/* Finish proccess when ctrl + c SIGINT(errupt) */
void sa_finish_int(int arg)
{
end_proccess = 1;
}
/* Finish procces when sigterm */
void sa_finish_term(int arg)
{
end_proccess = 1;
}
int main(int argc, char *argv[])
{
FILE *file_audio;
double dft_audio[FFT_SIZE];
int audio[FFT_SIZE];
int ret = 0,k = 0;
pid_t pid_record;
struct sigaction sa_int, sa_term;
long int *audio_rounded = malloc(FFT_SIZE*sizeof(long int));
if((file_audio = fopen(AUDIOFILE_PATH, "w+")) == NULL){
fprintf(stderr, "fopen() error\n");
return OPEN_CLOSE_FAILED;
}
sa_int.sa_handler = sa_finish_int; sa_term.sa_handler = sa_finish_term;
sigemptyset(&sa_int.sa_mask); sigemptyset(&sa_term.sa_mask);
sa_term.sa_flags = 0; sa_term.sa_flags = 0;
if((ret = sigaction(SIGINT, &sa_int, 0)) == -1){
perror("sigaction failed(): ");
return SIGNAL_FAILED;
}
if((ret = sigaction(SIGTERM, &sa_term, 0)) == -1){
perror("sigaction failed(): ");
return SIGNAL_FAILED;
}
init_W();
while(!end_proccess){
pid_record = fork();
if(pid_record == 0){ /* Child */
ret = execv(AUDIOPROG_PATH, audio_command);
if(ret == -1){
perror("exec() error: ");
exit(EXEC_FAILED); /* Exit child on error */
}
}
else{ /* Parent */
wait(&ret);
if(ret == -1){
fprintf(stderr, "child error\n");
return EXEC_FAILED;
}
/* Child closes it when he finishes recording */
freopen(AUDIOFILE_PATH, "r", file_audio);
for(k = 0; k < FFT_SIZE; k++)
fread(audio + k, sizeof(u_int8_t), 1, file_audio);
}
//take_evenodd(audio, audio_even, audio_odd);
dft(audio, dft_audio);
clean_board();
array_norm_round(dft_audio, audio_rounded);
prepare_board(audio_rounded);
show_board();
}
clean_board();
if((ret = fclose(file_audio)) != 0){
perror("close() error: ");
return OPEN_CLOSE_FAILED;
}
/* Remove audio file */
if((ret = remove(AUDIOFILE_PATH)) == -1){
perror("remove() file error: ");
return RM_FAILED;
}
free(audio_rounded);
return 0;
}