// TODO: Rewrite with alsa, fuck pulseaudio #include #include #include #include #include #include #include "audio.h" #include "typedef.h" static const pa_sample_spec ss = { .format = PA_SAMPLE_S16BE, .rate = 44100, .channels = 2 }; static pa_simple *play = NULL; static pa_simple *rec = NULL; int audio_play(char *buf) { int ret = 1; int error; if(play == NULL) { 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(char *buf) { int ret = 1; int error; if(!rec) { 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; } } if (pa_simple_read(rec, buf, REC_CAP, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); goto finish; } ret = 0; finish: // if(rec) // pa_simple_free(rec); return ret; }