summaryrefslogtreecommitdiffstats
path: root/src/sample.c
blob: b0d0cdbd03fbcf27df47baf8afe183c72666ad10 (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
#include <assert.h>

#include "sample.h"

size_t pa_sample_size(struct pa_sample_spec *spec) {
    assert(spec);
    size_t b = 1;

    switch (spec->format) {
        case PA_SAMPLE_U8:
        case PA_SAMPLE_ULAW:
        case PA_SAMPLE_ALAW:
            b = 1;
            break;
        case PA_SAMPLE_S16LE:
        case PA_SAMPLE_S16BE:
            b = 2;
            break;
        case PA_SAMPLE_FLOAT32:
            b = 4;
            break;
        default:
            assert(0);
    }

    return b * spec->channels;
}

size_t pa_bytes_per_second(struct pa_sample_spec *spec) {
    assert(spec);
    return spec->rate*pa_sample_size(spec);
}


uint32_t pa_samples_usec(size_t length, struct pa_sample_spec *spec) {
    assert(spec);

    return (uint32_t) (((double) length /pa_sample_size(spec))/spec->rate*1000000);
}

int pa_sample_spec_valid(struct pa_sample_spec *spec) {
    assert(spec);

    if (!spec->rate || !spec->channels)
        return 0;

    if (spec->format <= 0 || spec->format >= PA_SAMPLE_MAX)
        return 0;

    return 1;
}