aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
blob: f7a51a5671d346f949db9688879ae571a1ada78d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// TODO: Rewrite with alsa, fuck pulseaudio

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