summaryrefslogtreecommitdiff
path: root/audio_oss.c
blob: 05c915516442ec693ef843428e676ebc0d94a8f0 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "config.h"

#ifdef HAVE_MACHINE_SOUNDCARD_H
#  include <machine/soundcard.h>
#else
#  ifdef HAVE_SOUNDCARD_H
#    include <soundcard.h>
#  else
#    include <sys/soundcard.h>
#  endif
#endif

#include <errno.h>

/* FreeBSD uses a different identifier? what other BSDs? */
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#define SNDCTL_DSP_SETDUPLEX DSP_CAP_DUPLEX
#endif

#define ARCH_esd_audio_devices
const char *esd_audio_devices()
{
    return "/dev/dsp, /dev/dsp2, etc.";
}

#define NFRAGS 32

#define ARCH_esd_audio_open
int esd_audio_open()
{
    const char *device;

    int afd = -1, value = 0, test = 0;
    int mode = O_WRONLY;
    int rate;
    int fragsize;
    int frag;
    struct timeval tv;
    fd_set set;

    rate = esd_audio_rate *
        ( ( ( esd_audio_format & ESD_MASK_CHAN) == ESD_STEREO ) ? 2 : 1 ) *
        ( ( ( esd_audio_format & ESD_MASK_BITS) == ESD_BITS16 ) ? 2 : 1 );

    for ( fragsize = 0; 1L << fragsize < rate / 25; fragsize++ )
	;

    fragsize--;

    frag = (NFRAGS << 16) | fragsize;

    /* if recording, set for full duplex mode */
    if ( (esd_audio_format & ESD_MASK_FUNC) == ESD_RECORD )
        mode = O_RDWR;

    mode |= O_NONBLOCK;

    /* open the sound device */
    device = esd_audio_device ? esd_audio_device : "/dev/dsp";
    if ((afd = open(device, mode, 0)) == -1)
    {   /* Opening device failed */
	if (errno != ENOENT)
		perror(device);
        return( -2 );
    }

    mode = fcntl(afd, F_GETFL);
    mode &= ~O_NONBLOCK;
    fcntl(afd, F_SETFL, mode);

#if defined(SNDCTL_DSP_SETFRAGMENT)
    ioctl(afd, SNDCTL_DSP_SETFRAGMENT, &frag);
#endif

    /* TODO: check that this is allowable */
    /* set for full duplex operation, if recording */
#if defined(SNDCTL_DSP_SETDUPLEX)
    if ( (esd_audio_format & ESD_MASK_FUNC) == ESD_RECORD ) {
        ioctl( afd, SNDCTL_DSP_SETDUPLEX, 0 );
    }
#endif

    /* set the sound driver audio format for playback */
    
    value = test = ( (esd_audio_format & ESD_MASK_BITS) == ESD_BITS16 )
        ? /* 16 bit */ AFMT_S16_NE : /* 8 bit */ AFMT_U8;

    if (ioctl(afd, SNDCTL_DSP_SETFMT, &test) == -1)
    {   /* Fatal error */
        perror("SNDCTL_DSP_SETFMT");
        close( afd );
        esd_audio_fd = -1;
        return( -1 );
    }

    ioctl(afd, SNDCTL_DSP_GETFMTS, &test);
    if ( !(value & test) ) /* TODO: should this be if ( value XOR test ) ??? */
    {   /* The device doesn't support the requested audio format. */
        fprintf( stderr, "unsupported sound format: %d\n", esd_audio_format );
        close( afd );
        esd_audio_fd = -1;
        return( -1 );
    }

    /* set the sound driver number of channels for playback */
    value = test = ( ( ( esd_audio_format & ESD_MASK_CHAN) == ESD_STEREO )
        ? /* stereo */ 1 : /* mono */ 0 );
    if (ioctl(afd, SNDCTL_DSP_STEREO, &test) == -1)
    {   /* Fatal error */
        perror( "SNDCTL_DSP_STEREO" );
        close( afd );
        esd_audio_fd = -1;
        return( -1 );
    }

    /* set the sound driver number playback rate */
    test = esd_audio_rate;
    if ( ioctl(afd, SNDCTL_DSP_SPEED, &test) == -1)
    { /* Fatal error */
        perror("SNDCTL_DSP_SPEED");
        close( afd );
        esd_audio_fd = -1;
        return( -1 );
    }

    /* see if actual speed is within 5% of requested speed */
    if( fabs( test - esd_audio_rate ) > esd_audio_rate * 0.05 )
    {
        fprintf( stderr, "unsupported playback rate: %d\n", esd_audio_rate );
        close( afd );
        esd_audio_fd = -1;
        return( -1 );
    }

    if ( ioctl(afd, SNDCTL_DSP_GETBLKSIZE, &test) == -1)
    { /* Fatal error */
        perror("SNDCTL_DSP_GETBLKSIZE");
        close( afd );
        esd_audio_fd = -1;
        return( -1 );
    }
    esd_write_size = test > ESD_MAX_WRITE_SIZE ? ESD_MAX_WRITE_SIZE : test;

    /* value = test = buf_size; */
    esd_audio_fd = afd;
    /*
     * From XMMS:
     * Stupid hack to find out if the driver supports select; some
     * drivers won't work properly without a select and some won't
     * work with a select :/
     */

    tv.tv_sec = 0;
    tv.tv_usec = 10;
    FD_ZERO(&set);
    FD_SET(afd, &set);
    test = select(afd + 1, NULL, &set, NULL, &tv);
    if (test > 0)
	    select_works = 1;
    return afd;
}

#define ARCH_esd_audio_pause
void esd_audio_pause()
{
    /* per oss specs */
    ioctl( esd_audio_fd, SNDCTL_DSP_POST, 0 );
    return;
}