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. */
10 #include <sys/ioctl.h>
13 #include <sys/soundcard.h>
22 #include "oss_write.cmdline.h"
25 /** Always use 16 bit little endian. */
26 #define FORMAT AFMT_S16_LE
28 /** Data specific to the oss writer. */
29 struct private_oss_write_data
{
30 /** The file handle of the device. */
33 * The samplerate given by command line option or the decoder
34 * of the writer node group.
38 * The number of channels, given by command line option or the
39 * decoder of the writer node group.
42 /** Four bytes for stereo streams, two bytes for mono streams. */
46 static int oss_pre_select(struct sched
*s
, struct writer_node
*wn
)
48 struct private_oss_write_data
*powd
= wn
->private_data
;
49 struct writer_node_group
*wng
= wn
->wng
;
51 if (*wng
->loaded
- wn
->written
< powd
->bytes_per_frame
)
53 para_fd_set(powd
->fd
, &s
->wfds
, &s
->max_fileno
);
57 static int oss_post_select(struct sched
*s
, struct writer_node
*wn
)
60 struct private_oss_write_data
*powd
= wn
->private_data
;
61 struct writer_node_group
*wng
= wn
->wng
;
62 size_t frames
, bytes
= *wng
->loaded
- wn
->written
;
63 char *data
= *wng
->bufp
+ wn
->written
;
65 if (*wng
->input_error
< 0 && bytes
< powd
->bytes_per_frame
) {
66 wn
->written
= *wng
->loaded
;
67 return *wng
->input_error
;
69 frames
= bytes
/ powd
->bytes_per_frame
;
70 if (!frames
) /* less than a single frame available */
72 if (!FD_ISSET(powd
->fd
, &s
->wfds
))
74 ret
= write_nonblock(powd
->fd
, data
, frames
* powd
->bytes_per_frame
, 0);
82 static void oss_close(struct writer_node
*wn
)
84 struct private_oss_write_data
*powd
= wn
->private_data
;
91 * The Open Sound System Programmer's Guide sayeth:
93 * Set sampling parameters always so that number of channels (mono/stereo) is
94 * set before selecting sampling rate (speed). Failing to do this will make
95 * your program incompatible with cards such as the SoundBlaster Pro which
96 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
97 * selects 44.1 kHz speed and then sets the device to stereo mode will
98 * incorrectly believe that the device is still in 44.1 kHz mode when actually
99 * the speed is decreased to 22.05 kHz.
101 static int oss_open(struct writer_node
*wn
)
103 int ret
, format
= FORMAT
, channels
, samplerate
;
104 struct oss_write_args_info
*conf
= wn
->conf
;
105 struct writer_node_group
*wng
= wn
->wng
;
106 struct private_oss_write_data
*powd
;
108 PARA_INFO_LOG("opening %s\n", conf
->device_arg
);
109 ret
= para_open(conf
->device_arg
, O_WRONLY
, 0);
112 powd
= para_calloc(sizeof(*powd
));
113 wn
->private_data
= powd
;
115 ret
= mark_fd_nonblocking(powd
->fd
);
119 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SETFMT
, &format
);
121 ret
= -ERRNO_TO_PARA_ERROR(errno
);
124 ret
= -E_BAD_SAMPLE_FORMAT
;
125 if (format
!= FORMAT
)
127 /* set number of channels */
128 if (!conf
->channels_given
&& wng
->channels
)
129 channels
= *wng
->channels
;
131 channels
= conf
->channels_arg
;
132 ret
= -E_BAD_CHANNEL_COUNT
;
135 powd
->channels
= channels
;
136 ret
= ioctl(powd
->fd
, SNDCTL_DSP_CHANNELS
, &channels
);
138 ret
= -ERRNO_TO_PARA_ERROR(errno
);
141 if (powd
->channels
!= channels
)
143 powd
->bytes_per_frame
= channels
* 2;
148 * If we request a higher sampling rate than is supported by the
149 * device, the the highest possible speed is automatically used. The
150 * value actually used is returned as the new value of the argument.
152 if (!conf
->samplerate_given
&& wng
->samplerate
)
153 samplerate
= *wng
->samplerate
;
155 samplerate
= conf
->samplerate_arg
;
156 powd
->samplerate
= samplerate
;
157 ret
= ioctl(powd
->fd
, SNDCTL_DSP_SPEED
, &samplerate
);
159 ret
= -ERRNO_TO_PARA_ERROR(errno
);
162 if (samplerate
!= powd
->samplerate
) {
163 int min
= PARA_MIN(samplerate
, powd
->samplerate
),
164 max
= PARA_MAX(samplerate
, powd
->samplerate
);
166 * Check whether the returned sample rate differs significantly
167 * from the requested one.
169 ret
= -E_BAD_SAMPLERATE
;
170 if (100 * max
> 110 * min
) /* more than 10% deviation */
172 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", samplerate
,
183 __malloc
static void *oss_parse_config(const char *options
)
186 struct oss_write_args_info
*conf
= para_calloc(sizeof(*conf
));
188 PARA_INFO_LOG("options: %s, %zd\n", options
, strcspn(options
, " \t"));
189 ret
= oss_cmdline_parser_string(options
, conf
, "oss_write");
199 * The init function of the oss writer.
201 * \param w Pointer to the writer to initialize.
205 void oss_write_init(struct writer
*w
)
207 struct oss_write_args_info dummy
;
209 oss_cmdline_parser_init(&dummy
);
211 w
->close
= oss_close
;
212 w
->pre_select
= oss_pre_select
;
213 w
->post_select
= oss_post_select
;
214 w
->parse_config
= oss_parse_config
;
216 w
->help
= (struct ggo_help
) {
217 .short_help
= oss_write_args_info_help
,
218 .detailed_help
= oss_write_args_info_detailed_help
220 oss_cmdline_parser_free(&dummy
);