3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
9 * a52dec is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * a52dec is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/ioctl.h>
35 #if defined(__OpenBSD__)
36 #include <soundcard.h>
37 #elif defined(__FreeBSD__)
38 #include <machine/soundcard.h>
40 #include <machine/endian.h>
41 #if BYTE_ORDER == LITTLE_ENDIAN
42 #define AFMT_S16_NE AFMT_S16_LE
44 #define AFMT_S16_NE AFMT_S16_BE
48 #include <sys/soundcard.h>
52 #include "audio_out.h"
53 #include "audio_out_internal.h"
55 typedef struct oss_instance_s {
63 int oss_setup (ao_instance_t * _instance, int sample_rate, int * flags,
64 sample_t * level, sample_t * bias)
66 oss_instance_t * instance = (oss_instance_t *) _instance;
68 if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
70 instance->sample_rate = sample_rate;
72 *flags = instance->flags;
79 int oss_play (ao_instance_t * _instance, int flags, sample_t * _samples)
81 oss_instance_t * instance = (oss_instance_t *) _instance;
82 int16_t int16_samples[256*6];
86 float samples[256 * 6];
89 for (i = 0; i < 256 * 6; i++)
90 samples[i] = _samples[i];
92 float * samples = _samples;
95 chans = channels_multi (flags);
96 flags &= A52_CHANNEL_MASK | A52_LFE;
98 if (instance->set_params) {
102 if ((ioctl (instance->fd, SNDCTL_DSP_CHANNELS, &tmp) < 0) ||
104 fprintf (stderr, "Can not set number of channels\n");
108 tmp = instance->sample_rate;
109 if ((ioctl (instance->fd, SNDCTL_DSP_SPEED, &tmp) < 0) ||
110 (tmp != instance->sample_rate)) {
111 fprintf (stderr, "Can not set sample rate\n");
115 instance->flags = flags;
116 instance->set_params = 0;
117 } else if ((flags == A52_DOLBY) && (instance->flags == A52_STEREO)) {
118 fprintf (stderr, "Switching from stereo to dolby surround\n");
119 instance->flags = A52_DOLBY;
120 } else if ((flags == A52_STEREO) && (instance->flags == A52_DOLBY)) {
121 fprintf (stderr, "Switching from dolby surround to stereo\n");
122 instance->flags = A52_STEREO;
123 } else if (flags != instance->flags)
126 float2s16_multi (samples, int16_samples, flags);
127 write (instance->fd, int16_samples, 256 * sizeof (int16_t) * chans);
132 void oss_close (ao_instance_t * _instance)
134 oss_instance_t * instance = (oss_instance_t *) _instance;
136 close (instance->fd);
139 ao_instance_t * oss_open (int flags)
141 oss_instance_t * instance;
144 instance = malloc (sizeof (oss_instance_t));
145 if (instance == NULL)
148 instance->ao.setup = oss_setup;
149 instance->ao.play = oss_play;
150 instance->ao.close = oss_close;
152 instance->sample_rate = 0;
153 instance->set_params = 1;
154 instance->flags = flags;
156 instance->fd = open ("/dev/dsp", O_WRONLY);
157 if (instance->fd < 0) {
158 fprintf (stderr, "Can not open /dev/dsp\n");
163 format = AFMT_S16_NE;
164 if ((ioctl (instance->fd, SNDCTL_DSP_SETFMT, &format) < 0) ||
165 (format != AFMT_S16_NE)) {
166 fprintf (stderr, "Can not set sample format\n");
171 return (ao_instance_t *) instance;
174 ao_instance_t * ao_oss_open (void)
176 return oss_open (A52_STEREO);
179 ao_instance_t * ao_ossdolby_open (void)
181 return oss_open (A52_DOLBY);
184 ao_instance_t * ao_oss4_open (void)
186 return oss_open (A52_2F2R);
189 ao_instance_t * ao_oss6_open (void)
191 return oss_open (A52_3F2R | A52_LFE);