From 675ded0d66b9fd60777d3037ded1446a3f9ef986 Mon Sep 17 00:00:00 2001 From: kartofen Date: Wed, 31 Aug 2022 12:31:38 +0300 Subject: Big Bang --- src/audio.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/audio.c (limited to 'src/audio.c') diff --git a/src/audio.c b/src/audio.c new file mode 100644 index 0000000..0c40f9f --- /dev/null +++ b/src/audio.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +#include +#include + +#include "audio.h" +#include "typedef.h" + +static const pa_sample_spec ss = { + .format = PA_SAMPLE_S16LE, + .rate = 44100, + .channels = 1 +}; + +static pa_simple *play = NULL; + +int audip_play(char *buf) +{ + int ret = 1; + int error; + + if(!play) { + if (!(play = pa_simple_new(NULL, __FILE__, PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) { + fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); + goto finish; + } + } + + if(pa_simple_write(play, buf, REC_CAP, &error) < 0) { + fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); + goto finish; + } + + if(pa_simple_drain(play, &error) < 0) { + fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error)); + goto finish; + } + + ret = 0; + +finish: + + if (play) + pa_simple_free(play); + + return ret; +} + +int audio_record(int fd) +{ + pa_simple *rec = NULL; + int ret = 1; + int error; + + if (!(rec = pa_simple_new(NULL, __FILE__, PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) { + fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); + goto finish; + } + + for(;;) + { + char buf[REC_CAP]; + + if (pa_simple_read(rec, buf, sizeof(buf), &error) < 0) { + fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); + goto finish; + } + + write(fd, buf, sizeof(buf)); + } + +finish: + if(rec) + pa_simple_free(rec); + + return ret; +} -- cgit v1.2.3