diff options
author | Joursoir <chat@joursoir.net> | 2021-01-26 12:56:51 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-01-26 12:56:51 +0000 |
commit | b5ba7f5524e763df8778574f5503730722381375 (patch) | |
tree | 35aa6ff66548596db06f8082b5ed92d701250cbd | |
parent | f8e0175263eafae4d7df3ddbe2bb8ac8c1831dce (diff) | |
download | audio-tools-b5ba7f5524e763df8778574f5503730722381375.tar.gz audio-tools-b5ba7f5524e763df8778574f5503730722381375.tar.bz2 audio-tools-b5ba7f5524e763df8778574f5503730722381375.zip |
add SIGINT handler for correct exit
-rw-r--r-- | parecord/parecord.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/parecord/parecord.c b/parecord/parecord.c index 55434a0..fff705d 100644 --- a/parecord/parecord.c +++ b/parecord/parecord.c @@ -1,11 +1,19 @@ #include <stdio.h> #include <unistd.h> - +#include <signal.h> #include <pulse/simple.h> #define BUFSIZE 1024 +volatile sig_atomic_t flag_do = 1; -int loop_write(int fd, const void *data, size_t size) { +void handler(int s) +{ + signal(SIGINT, handler); + flag_do = 0; +} + +int loop_write(int fd, const void *data, size_t size) +{ int ret = 0; while(size > 0) { @@ -27,6 +35,7 @@ int loop_write(int fd, const void *data, size_t size) { int main(int argc, char *argv[]) { + signal(SIGINT, handler); pa_sample_spec ss; ss.format = PA_SAMPLE_S16LE; ss.channels = 2; @@ -43,28 +52,29 @@ int main(int argc, char *argv[]) NULL, NULL); if(!s) { - fprintf(stderr, "pa_simple_new() failed\n"); + fprintf(stderr, "[Error] pa_simple_new() failed\n"); return 1; } - for(;;) { + fprintf(stderr, "[Message] Use \"Ctrl + C\" for exit\n"); + while(flag_do) { uint8_t buf[BUFSIZE]; // Record some data if(pa_simple_read(s, buf, sizeof(buf), NULL) < 0) { - fprintf(stderr, "pa_simple_read() failed\n"); + fprintf(stderr, "[Error] pa_simple_read() failed\n"); return 1; } // And write it to STDOUT */ if(loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) { - fprintf(stderr, "write() failed\n"); + fprintf(stderr, "[Error] write() failed\n"); return 1; } } if(s) - pa_simple_free(s); + pa_simple_free(s); return 0; }
\ No newline at end of file |