2 * Copyright 2004-2005 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #if defined(__OpenBSD__)
25 #include <soundcard.h>
27 #include <sys/soundcard.h>
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
35 static sample_format_t oss_sf
;
36 static int oss_fd
= -1;
39 static char *oss_dsp_device
= NULL
;
41 static int oss_close(void);
43 static int oss_reset(void)
45 if (fcntl(oss_fd
, SNDCTL_DSP_RESET
, 0) == -1) {
51 static int oss_set_sf(sample_format_t sf
)
53 int tmp
, log2_fragment_size
, nr_fragments
, bytes_per_second
;
57 tmp
= sf_get_channels(oss_sf
) - 1;
58 if (ioctl(oss_fd
, SNDCTL_DSP_STEREO
, &tmp
) == -1)
61 if (sf_get_bits(oss_sf
) == 16) {
62 if (sf_get_signed(oss_sf
)) {
63 if (sf_get_bigendian(oss_sf
)) {
69 if (sf_get_bigendian(oss_sf
)) {
75 } else if (sf_get_bits(oss_sf
) == 8) {
76 if (sf_get_signed(oss_sf
)) {
84 if (ioctl(oss_fd
, SNDCTL_DSP_SAMPLESIZE
, &tmp
) == -1)
87 tmp
= sf_get_rate(oss_sf
);
88 if (ioctl(oss_fd
, SNDCTL_DSP_SPEED
, &tmp
) == -1)
91 bytes_per_second
= sf_get_second_size(oss_sf
);
92 log2_fragment_size
= 0;
93 while (1 << log2_fragment_size
< bytes_per_second
/ 25)
98 /* bits 0..15 = size of fragment, 16..31 = log2(number of fragments) */
99 tmp
= (nr_fragments
<< 16) + log2_fragment_size
;
100 if (ioctl(oss_fd
, SNDCTL_DSP_SETFRAGMENT
, &tmp
) == -1)
105 static int oss_device_exists(const char *device
)
109 if (stat(device
, &s
))
114 static int oss_init(void)
116 const char *new_dsp_dev
= "/dev/sound/dsp";
117 const char *dsp_dev
= "/dev/dsp";
119 if (oss_dsp_device
) {
120 if (oss_device_exists(oss_dsp_device
))
122 free(oss_dsp_device
);
123 oss_dsp_device
= NULL
;
126 if (oss_device_exists(new_dsp_dev
)) {
127 oss_dsp_device
= xstrdup(new_dsp_dev
);
130 if (oss_device_exists(dsp_dev
)) {
131 oss_dsp_device
= xstrdup(dsp_dev
);
137 static int oss_exit(void)
139 free(oss_dsp_device
);
140 oss_dsp_device
= NULL
;
144 static int oss_open(sample_format_t sf
)
146 oss_fd
= open(oss_dsp_device
, O_WRONLY
);
149 if (oss_set_sf(sf
) == -1) {
156 static int oss_close(void)
163 static int oss_write(const char *buffer
, int count
)
169 rc
= write(oss_fd
, buffer
, count
);
173 static int oss_pause(void)
175 if (ioctl(oss_fd
, SNDCTL_DSP_POST
, NULL
) == -1)
180 static int oss_unpause(void)
185 static int oss_buffer_space(void)
190 if (ioctl(oss_fd
, SNDCTL_DSP_GETOSPACE
, &info
) == -1)
196 static int op_oss_set_option(int key
, const char *val
)
200 free(oss_dsp_device
);
201 oss_dsp_device
= xstrdup(val
);
204 return -OP_ERROR_NOT_OPTION
;
209 static int op_oss_get_option(int key
, char **val
)
214 *val
= xstrdup(oss_dsp_device
);
217 return -OP_ERROR_NOT_OPTION
;
222 const struct output_plugin_ops op_pcm_ops
= {
229 .unpause
= oss_unpause
,
230 .buffer_space
= oss_buffer_space
,
231 .set_option
= op_oss_set_option
,
232 .get_option
= op_oss_get_option
235 const char * const op_pcm_options
[] = {
240 const int op_priority
= 1;