Debian Codegraph @

src/libout123/modules/sun.c

raw
1 /*
2 sun: audio output for Sun systems
3
4 copyright ?-2020 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Michael Hipp
7 */
8
9 #include "../out123_int.h"
10
11 #ifdef HAVE_SYS_IOCTL_H
12 #include <sys/ioctl.h>
13 #endif
14
15 #ifdef HAVE_SUN_AUDIOIO_H
16 #include <sun/audioio.h>
17 #endif
18
19 #ifdef HAVE_SYS_AUDIOIO_H
20 #include <sys/audioio.h>
21 #endif
22
23 #ifdef HAVE_SYS_AUDIO_H
24 #include <sys/audio.h>
25 #endif
26
27 #ifdef HAVE_ASM_AUDIOIO_H
28 #include <asm/audioio.h>
29 #endif
30
31 #include <fcntl.h>
32 #include "../../common/debug.h"
33
34 static void set_format_helper(out123_handle *ao, audio_info_t *ainfo)
35 {
36
37 switch(ao->format) {
38 case -1:
39 case MPG123_ENC_SIGNED_16:
40 default:
41 #ifndef AUDIO_ENCODING_LINEAR /* not supported */
42 #define AUDIO_ENCODING_LINEAR 3
43 #endif
44 ainfo->play.encoding = AUDIO_ENCODING_LINEAR;
45 ainfo->play.precision = 16;
46 break;
47 case MPG123_ENC_UNSIGNED_8:
48 #if defined(SOLARIS) || defined(SPARCLINUX)
49 ainfo->play.encoding = AUDIO_ENCODING_LINEAR8;
50 ainfo->play.precision = 8;
51 break;
52 #endif
53 case MPG123_ENC_SIGNED_8:
54 if(!AOQUIET)
55 error("Linear signed 8 bit not supported!");
56 return;
57 case MPG123_ENC_ULAW_8:
58 ainfo->play.encoding = AUDIO_ENCODING_ULAW;
59 ainfo->play.precision = 8;
60 break;
61 case MPG123_ENC_ALAW_8:
62 ainfo->play.encoding = AUDIO_ENCODING_ALAW;
63 ainfo->play.precision = 8;
64 break;
65 }
66
67 }
68
69
70 static int reset_parameters_sun(out123_handle *ao)
71 {
72 audio_info_t ainfo;
73
74 AUDIO_INITINFO(&ainfo);
75 if(ao->rate != -1) ainfo.play.sample_rate = ao->rate;
76
77 if(ao->channels >= 0) ainfo.play.channels = ao->channels;
78
79 set_format_helper(ao,&ainfo);
80 if(ioctl(ao->fn, AUDIO_SETINFO, &ainfo) == -1) return -1;
81
82 return 0;
83 }
84
85 static int rate_best_match(out123_handle *ao)
86 {
87 audio_info_t ainfo;
88 AUDIO_INITINFO(&ainfo);
89
90 ainfo.play.sample_rate = ao->rate;
91 if(ioctl(ao->fn, AUDIO_SETINFO, &ainfo) < 0) {
92 ao->rate = 0;
93 return 0;
94 }
95 if(ioctl(ao->fn, AUDIO_GETINFO, &ainfo) < 0) {
96 return -1;
97 }
98
99 ao->rate = ainfo.play.sample_rate;
100 return 0;
101 }
102
103 static int set_rate(out123_handle *ao)
104 {
105 audio_info_t ainfo;
106
107 if(ao->rate != -1) {
108 AUDIO_INITINFO(&ainfo);
109 ainfo.play.sample_rate = ao->rate;
110 if(ioctl(ao->fn, AUDIO_SETINFO, &ainfo) == -1) return -1;
111 return 0;
112 }
113
114 return -1;
115 }
116
117 static int set_channels(out123_handle *ao)
118 {
119 audio_info_t ainfo;
120
121 AUDIO_INITINFO(&ainfo);
122 ainfo.play.channels = ao->channels;
123 if(ioctl(ao->fn, AUDIO_SETINFO, &ainfo) == -1)
124 return -1;
125
126 return 0;
127 }
128
129 static int set_format(out123_handle *ao)
130 {
131 audio_info_t ainfo;
132
133 AUDIO_INITINFO(&ainfo);
134 set_format_helper(ao,&ainfo);
135 if(ioctl(ao->fn, AUDIO_SETINFO, &ainfo) == -1)
136 return -1;
137
138 return 0;
139 }
140
141 static int open_sun(out123_handle *ao)
142 {
143 audio_info_t ainfo;
144 const char *dev = ao->device;
145
146 if(!dev) {
147 if(getenv("AUDIODEV")) {
148 dev = getenv("AUDIODEV");
149 } else {
150 dev = "/dev/audio";
151 }
152 }
153
154 ao->fn = open(dev,O_WRONLY);
155 if(ao->fn < 0) return ao->fn;
156
157 #if defined(SUNOS) && defined(AUDIO_GETDEV)
158 {
159 int type;
160 if(ioctl(ao->fn, AUDIO_GETDEV, &type) == -1) return -1;
161 if(type == AUDIO_DEV_UNKNOWN || type == AUDIO_DEV_AMD)
162 return -1;
163 }
164 #else
165 #if defined(SOLARIS) || defined(SPARCLINUX)
166 {
167 struct audio_device ad;
168 if(ioctl(ao->fn, AUDIO_GETDEV, &ad) == -1)
169 return -1;
170 if(!strstr(ad.name,"dbri") && !strstr(ad.name,"CS4231") && !AOQUIET)
171 warning1("Unknown sound system %s. But we try it.",ad.name);
172 }
173 #endif
174 #endif
175
176 if(reset_parameters_sun(ao) < 0) return -1;
177
178 AUDIO_INITINFO(&ainfo);
179
180 if(ao->flags > 0)
181 ainfo.play.port = 0;
182 if(ao->flags & OUT123_INTERNAL_SPEAKER)
183 ainfo.play.port |= AUDIO_SPEAKER;
184 if(ao->flags & OUT123_HEADPHONES)
185 ainfo.play.port |= AUDIO_HEADPHONE;
186 #ifdef AUDIO_LINE_OUT
187 if(ao->flags & OUT123_LINE_OUT)
188 ainfo.play.port |= AUDIO_LINE_OUT;
189 #endif
190
191 if(ao->gain != -1)
192 ainfo.play.gain = ao->gain;
193
194 if(ioctl(ao->fn, AUDIO_SETINFO, &ainfo) == -1)
195 return -1;
196
197 return ao->fn;
198 }
199
200
201
202
203 static int get_formats_sun(out123_handle *ao)
204 {
205 static int tab[][3] = {
206 { AUDIO_ENCODING_ULAW , 8, MPG123_ENC_ULAW_8 } ,
207 { AUDIO_ENCODING_ALAW , 8, MPG123_ENC_ALAW_8 } ,
208 { AUDIO_ENCODING_LINEAR , 16, MPG123_ENC_SIGNED_16 } ,
209 #if 0
210 #if defined(SOLARIS) || defined(SPARCLINUX)
211 { AUDIO_ENCODING_LINEAR8 , 8, MPG123_ENC_UNSIGNED_8 } ,
212 #endif
213 #endif
214 };
215
216 audio_info_t ainfo;
217 int i,fmts=0;
218
219 for(i=0;i<sizeof(tab)/sizeof(tab[0]);i++) {
220 AUDIO_INITINFO(&ainfo);
221 ainfo.play.encoding = tab[i][0];
222 ainfo.play.precision = tab[i][1];
223 #if 1
224 ainfo.play.sample_rate = ao->rate;
225 ainfo.play.channels = ao->channels;
226 #endif
227 if(ioctl(ao->fn, AUDIO_SETINFO, &ainfo) >= 0) {
228 fmts |= tab[i][2];
229 }
230 }
231 return fmts;
232 }
233
234 static int write_sun(out123_handle *ao,unsigned char *buf,int len)
235 {
236 return write(ao->fn,buf,len);
237 }
238
239 static int close_sun(out123_handle *ao)
240 {
241 close (ao->fn);
242 return 0;
243 }
244
245 static void flush_sun(out123_handle *ao)
246 {
247 /*ioctl (ao->fn, I_FLUSH, FLUSHRW);*/
248 }
249
250
251 static int init_sun(out123_handle* ao)
252 {
253 if (ao==NULL) return -1;
254
255 /* Set callbacks */
256 ao->open = open_sun;
257 ao->flush = flush_sun;
258 ao->write = write_sun;
259 ao->get_formats = get_formats_sun;
260 ao->close = close_sun;
261
262 /* Success */
263 return 0;
264 }
265
266
267
268
269
270 /*
271 Module information data structure
272 */
273 mpg123_module_t mpg123_output_module_info = {
274 /* api_version */ MPG123_MODULE_API_VERSION,
275 /* name */ "sun",
276 /* description */ "Audio output for Sun Audio.",
277 /* revision */ "$Rev: 5386 $",
278 /* handle */ NULL,
279
280 /* init_output */ init_sun,
281 };
282
283