1 /*
2  * audio_out_oss.c
3  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of a52dec, a free ATSC A-52 stream decoder.
7  * See http://liba52.sourceforge.net/ for updates.
8  *
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.
13  *
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.
18  *
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
22  */
23 
24 #include "config.h"
25 
26 #ifdef LIBAO_OSS
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/ioctl.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 
35 #if defined(__OpenBSD__)
36 #include <soundcard.h>
37 #elif defined(__FreeBSD__)
38 #include <machine/soundcard.h>
39 #ifndef AFMT_S16_NE
40 #include <machine/endian.h>
41 #if BYTE_ORDER == LITTLE_ENDIAN
42 #define AFMT_S16_NE AFMT_S16_LE
43 #else
44 #define AFMT_S16_NE AFMT_S16_BE
45 #endif
46 #endif
47 #else
48 #include <sys/soundcard.h>
49 #endif
50 
51 #include "a52.h"
52 #include "audio_out.h"
53 #include "audio_out_internal.h"
54 
55 typedef struct oss_instance_s {
56     ao_instance_t ao;
57     int fd;
58     int sample_rate;
59     int set_params;
60     int flags;
61 } oss_instance_t;
62 
oss_setup(ao_instance_t * _instance,int sample_rate,int * flags,sample_t * level,sample_t * bias)63 static int oss_setup (ao_instance_t * _instance, int sample_rate, int * flags,
64 		      sample_t * level, sample_t * bias)
65 {
66     oss_instance_t * instance = (oss_instance_t *) _instance;
67 
68     if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
69 	return 1;
70     instance->sample_rate = sample_rate;
71 
72     *flags = instance->flags;
73     *level = 1;
74     *bias = 384;
75 
76     return 0;
77 }
78 
oss_play(ao_instance_t * _instance,int flags,sample_t * _samples)79 static int oss_play (ao_instance_t * _instance, int flags, sample_t * _samples)
80 {
81     oss_instance_t * instance = (oss_instance_t *) _instance;
82     int16_t int16_samples[256*6];
83     int chans = -1;
84 
85 #ifdef LIBA52_DOUBLE
86     float samples[256 * 6];
87     int i;
88 
89     for (i = 0; i < 256 * 6; i++)
90 	samples[i] = _samples[i];
91 #else
92     float * samples = _samples;
93 #endif
94 
95     chans = channels_multi (flags);
96     flags &= A52_CHANNEL_MASK | A52_LFE;
97 
98     if (instance->set_params) {
99 	int tmp;
100 
101 	tmp = chans;
102 	if ((ioctl (instance->fd, SNDCTL_DSP_CHANNELS, &tmp) < 0) ||
103 	    (tmp != chans)) {
104 	    fprintf (stderr, "Can not set number of channels\n");
105 	    return 1;
106 	}
107 
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");
112 	    return 1;
113 	}
114 
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)
124 	return 1;
125 
126     float2s16_multi (samples, int16_samples, flags);
127     write (instance->fd, int16_samples, 256 * sizeof (int16_t) * chans);
128 
129     return 0;
130 }
131 
oss_close(ao_instance_t * _instance)132 static void oss_close (ao_instance_t * _instance)
133 {
134     oss_instance_t * instance = (oss_instance_t *) _instance;
135 
136     close (instance->fd);
137 }
138 
oss_open(int flags)139 static ao_instance_t * oss_open (int flags)
140 {
141     oss_instance_t * instance;
142     int format;
143 
144     instance = malloc (sizeof (oss_instance_t));
145     if (instance == NULL)
146 	return NULL;
147 
148     instance->ao.setup = oss_setup;
149     instance->ao.play = oss_play;
150     instance->ao.close = oss_close;
151 
152     instance->sample_rate = 0;
153     instance->set_params = 1;
154     instance->flags = flags;
155 
156     instance->fd = open ("/dev/dsp", O_WRONLY);
157     if (instance->fd < 0) {
158 	fprintf (stderr, "Can not open /dev/dsp\n");
159 	free (instance);
160 	return NULL;
161     }
162 
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");
167 	free (instance);
168 	return NULL;
169     }
170 
171     return (ao_instance_t *) instance;
172 }
173 
ao_oss_open(void)174 ao_instance_t * ao_oss_open (void)
175 {
176     return oss_open (A52_STEREO);
177 }
178 
ao_ossdolby_open(void)179 ao_instance_t * ao_ossdolby_open (void)
180 {
181     return oss_open (A52_DOLBY);
182 }
183 
ao_oss4_open(void)184 ao_instance_t * ao_oss4_open (void)
185 {
186     return oss_open (A52_2F2R);
187 }
188 
ao_oss6_open(void)189 ao_instance_t * ao_oss6_open (void)
190 {
191     return oss_open (A52_3F2R | A52_LFE);
192 }
193 
194 #endif
195