2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file oss_write.c Paraslash's oss output plugin. */
12 #include <sys/soundcard.h>
21 #include "oss_write.cmdline.h"
24 /** Always use 16 bit little endian. */
25 #define FORMAT AFMT_S16_LE
27 /** Data specific to the oss writer. */
28 struct private_oss_write_data
{
29 /** The file handle of the device. */
32 * The samplerate given by command line option or the decoder
33 * of the writer node group.
37 * The number of channels, given by command line option or the
38 * decoder of the writer node group.
41 /** Four bytes for stereo streams, two bytes for mono streams. */
45 static int oss_pre_select(struct sched
*s
, struct writer_node
*wn
)
47 struct private_oss_write_data
*powd
= wn
->private_data
;
48 struct writer_node_group
*wng
= wn
->wng
;
50 if (*wng
->loaded
- wn
->written
< powd
->bytes_per_frame
)
52 para_fd_set(powd
->fd
, &s
->wfds
, &s
->max_fileno
);
56 static int oss_post_select(struct sched
*s
, struct writer_node
*wn
)
59 struct private_oss_write_data
*powd
= wn
->private_data
;
60 struct writer_node_group
*wng
= wn
->wng
;
61 size_t frames
, bytes
= *wng
->loaded
- wn
->written
;
62 char *data
= *wng
->bufp
+ wn
->written
;
64 if (*wng
->input_error
< 0 && bytes
< powd
->bytes_per_frame
) {
65 wn
->written
= *wng
->loaded
;
66 return *wng
->input_error
;
68 frames
= bytes
/ powd
->bytes_per_frame
;
69 if (!frames
) /* less than a single frame available */
71 if (!FD_ISSET(powd
->fd
, &s
->wfds
))
73 ret
= write_nonblock(powd
->fd
, data
, frames
* powd
->bytes_per_frame
, 0);
81 static void oss_close(struct writer_node
*wn
)
83 struct private_oss_write_data
*powd
= wn
->private_data
;
90 * The Open Sound System Programmer's Guide sayeth:
92 * Set sampling parameters always so that number of channels (mono/stereo) is
93 * set before selecting sampling rate (speed). Failing to do this will make
94 * your program incompatible with cards such as the SoundBlaster Pro which
95 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
96 * selects 44.1 kHz speed and then sets the device to stereo mode will
97 * incorrectly believe that the device is still in 44.1 kHz mode when actually
98 * the speed is decreased to 22.05 kHz.
100 static int oss_open(struct writer_node
*wn
)
102 int ret
, format
= FORMAT
, channels
, samplerate
;
103 struct oss_write_args_info
*conf
= wn
->conf
;
104 struct writer_node_group
*wng
= wn
->wng
;
105 struct private_oss_write_data
*powd
;
107 PARA_INFO_LOG("opening %s\n", conf
->device_arg
);
108 ret
= para_open(conf
->device_arg
, O_WRONLY
, 0);
111 powd
= para_calloc(sizeof(*powd
));
112 wn
->private_data
= powd
;
114 ret
= mark_fd_nonblocking(powd
->fd
);
118 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SETFMT
, &format
);
120 ret
= -ERRNO_TO_PARA_ERROR(errno
);
123 ret
= -E_BAD_SAMPLE_FORMAT
;
124 if (format
!= FORMAT
)
126 /* set number of channels */
127 if (!conf
->channels_given
&& wng
->channels
)
128 channels
= *wng
->channels
;
130 channels
= conf
->channels_arg
;
131 ret
= -E_BAD_CHANNEL_COUNT
;
134 powd
->channels
= channels
;
135 ret
= ioctl(powd
->fd
, SNDCTL_DSP_CHANNELS
, &channels
);
137 ret
= -ERRNO_TO_PARA_ERROR(errno
);
140 if (powd
->channels
!= channels
)
142 powd
->bytes_per_frame
= channels
* 2;
147 * If we request a higher sampling rate than is supported by the
148 * device, the the highest possible speed is automatically used. The
149 * value actually used is returned as the new value of the argument.
151 if (!conf
->samplerate_given
&& wng
->samplerate
)
152 samplerate
= *wng
->samplerate
;
154 samplerate
= conf
->samplerate_arg
;
155 powd
->samplerate
= samplerate
;
156 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SPEED
, &samplerate
);
158 ret
= -ERRNO_TO_PARA_ERROR(errno
);
161 if (samplerate
!= powd
->samplerate
) {
162 int min
= PARA_MIN(samplerate
, powd
->samplerate
),
163 max
= PARA_MAX(samplerate
, powd
->samplerate
);
165 * Check whether the returned sample rate differs significantly
166 * from the requested one.
168 ret
= -E_BAD_SAMPLERATE
;
169 if (100 * max
> 110 * min
) /* more than 10% deviation */
171 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", samplerate
,
182 __malloc
static void *oss_parse_config(const char *options
)
185 struct oss_write_args_info
*conf
= para_calloc(sizeof(*conf
));
187 PARA_INFO_LOG("options: %s, %zd\n", options
, strcspn(options
, " \t"));
188 ret
= oss_cmdline_parser_string(options
, conf
, "oss_write");
198 * The init function of the oss writer.
200 * \param w Pointer to the writer to initialize.
204 void oss_write_init(struct writer
*w
)
206 struct oss_write_args_info dummy
;
208 oss_cmdline_parser_init(&dummy
);
210 w
->close
= oss_close
;
211 w
->pre_select
= oss_pre_select
;
212 w
->post_select
= oss_post_select
;
213 w
->parse_config
= oss_parse_config
;
215 w
->help
= (struct ggo_help
) {
216 .short_help
= oss_write_args_info_help
,
217 .detailed_help
= oss_write_args_info_detailed_help
219 oss_cmdline_parser_free(&dummy
);