summaryrefslogtreecommitdiffstats
path: root/src/oss-util.c
blob: 02bf8cd1c8382af3d86243519bc9b3afa78630f8 (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
#include <assert.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "oss.h"

int oss_auto_format(int fd, struct pa_sample_spec *ss) {
    int format, channels, speed;

    assert(fd >= 0 && ss);
    
    format = AFMT_S16_NE;
    if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
        int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
        format = f;
        if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
            format = AFMT_U8;
            if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
                fprintf(stderr, "SNDCTL_DSP_SETFMT: %s\n", format != AFMT_U8 ? "No supported sample format" : strerror(errno));
                return -1;
            } else
                ss->format = SAMPLE_U8;
        } else
            ss->format = f == AFMT_S16_LE ? SAMPLE_S16LE : SAMPLE_S16BE;
    } else
        ss->format = SAMPLE_S16NE;
        
    channels = 2;
    if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
        fprintf(stderr, "SNDCTL_DSP_CHANNELS: %s\n", strerror(errno));
        return -1;
    }
    assert(channels);
    ss->channels = channels;

    speed = 44100;
    if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
        fprintf(stderr, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
        return -1;
    }
    assert(speed);
    ss->rate = speed;

    return 0;
}