#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; }