diff options
Diffstat (limited to 'src/audio.c')
| -rw-r--r-- | src/audio.c | 80 | 
1 files changed, 80 insertions, 0 deletions
| 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 <stdio.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +#include <pulse/simple.h> +#include <pulse/error.h> + +#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; +} | 
