Debian Codegraph @

src/libout123/modules/sgi.c

raw
1 /*
2 sgi: audio output on SGI boxen
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 (as it seems) by Thomas Woerner
7 */
8
9 #include "../out123_int.h"
10 #include <fcntl.h>
11 #include <dmedia/audio.h>
12 #include <errno.h>
13 #include "../../common/debug.h"
14
15 static int set_rate(out123_handle *ao, ALconfig config)
16 {
17 int dev = alGetDevice(config);
18 ALpv params[1];
19
20 /* Make sure the device is OK */
21 if(dev < 0)
22 {
23 if(!AOQUIET)
24 error1("set_rate: %s", alGetErrorString(oserror()));
25 return -1;
26 }
27 if(ao->rate > 0)
28 {
29 params[0].param = AL_RATE;
30 params[0].value.ll = alDoubleToFixed((double)ao->rate);
31 if(alSetParams(dev, params, 1) < 0)
32 {
33 if(!AOQUIET)
34 error1("set_rate: %s", alGetErrorString(oserror()));
35 return -1;
36 }
37 }
38 return 0;
39 }
40
41
42 static int set_channels(out123_handle *ao, ALconfig config)
43 {
44 int ret;
45
46 if(ao->channels == 2)
47 ret = alSetChannels(config, AL_STEREO);
48 else
49 ret = alSetChannels(config, AL_MONO);
50
51 if(ret < 0)
52 {
53 if(!AOQUIET)
54 error1("set_channels : %s", alGetErrorString(oserror()));
55 return -1;
56 }
57 return 0;
58 }
59
60 static int set_format(out123_handle *ao, ALconfig config)
61 {
62 if(ao->format == MPG123_ENC_FLOAT_32)
63 {
64 if(alSetSampFmt(config, AL_SAMPFMT_FLOAT) < 0)
65 {
66 if(!AOQUIET)
67 error1("SetSampFmt: %s", alGetErrorString(oserror()));
68 return -1;
69 }
70 } else
71 {
72 if(alSetSampFmt(config, AL_SAMPFMT_TWOSCOMP) < 0)
73 {
74 if(!AOQUIET)
75 error1("SetSampFmt: %s", alGetErrorString(oserror()));
76 return -1;
77 }
78 if(alSetWidth(config, AL_SAMPLE_16) < 0)
79 {
80 if(!AOQUIET)
81 error1("SetWidth: %s", alGetErrorString(oserror()));
82 return -1;
83 }
84 }
85 return 0;
86 }
87
88
89 static int open_sgi(out123_handle *ao)
90 {
91 int current_dev;
92 ALport port = NULL;
93 ALconfig config = alNewConfig();
94
95 ao->userptr = NULL;
96
97 /* Test for correct completion */
98 if(config == 0)
99 {
100 if(!AOQUIET)
101 error1("open_sgi: %s", alGetErrorString(oserror()));
102 return -1;
103 }
104
105 /* Setup output device to specified device name. If there is no device name
106 specified in ao structure, use the default for output */
107 if((ao->device) != NULL)
108 {
109 current_dev = alGetResourceByName(AL_SYSTEM, ao->device, AL_OUTPUT_DEVICE_TYPE);
110
111 debug2("Dev: %s %i", ao->device, current_dev);
112
113 if(!current_dev && !AOQUIET)
114 {
115 int i, numOut;
116 char devname[32];
117 ALpv pv[1];
118 ALvalue *alvalues;
119
120 error2("Invalid audio resource: %s (%s)", ao->device, alGetErrorString(oserror()));
121
122 if((numOut= alQueryValues(AL_SYSTEM,AL_DEFAULT_OUTPUT,0,0,0,0))>=0)
123 fprintf(stderr, "There are %d output devices on this system.\n", numOut);
124 else
125 {
126 fprintf(stderr, "Can't find output devices. alQueryValues failed: %s\n", alGetErrorString(oserror()));
127 goto open_sgi_bad;
128 }
129
130 alvalues = malloc(sizeof(ALvalue) * numOut);
131 i = alQueryValues(AL_SYSTEM, AL_DEFAULT_OUTPUT, alvalues, numOut, pv, 0);
132 if(i == -1)
133 error1("alQueryValues: %s", alGetErrorString(oserror()));
134 else
135 {
136 for(i=0; i < numOut; i++)
137 {
138 pv[0].param = AL_NAME;
139 pv[0].value.ptr = devname;
140 pv[0].sizeIn = 32;
141 alGetParams(alvalues[i].i, pv, 1);
142
143 fprintf(stderr, "%i: %s\n", i, devname);
144 }
145 }
146 free(alvalues);
147 }
148 if(!current_dev)
149 goto open_sgi_bad;
150
151 if(alSetDevice(config, current_dev) < 0)
152 {
153 if(!AOQUIET)
154 error1("open: alSetDevice : %s",alGetErrorString(oserror()));
155 goto open_sgi_bad;
156 }
157 } else
158 current_dev = AL_DEFAULT_OUTPUT;
159
160 /* Set the device */
161 if(alSetDevice(config, current_dev) < 0)
162 {
163 if(!AOQUIET)
164 error1("open_sgi: %s", alGetErrorString(oserror()));
165 goto open_sgi_bad;
166 }
167
168 /* Set port parameters */
169
170 if(alSetQueueSize(config, 131069) < 0)
171 {
172 if(!AOQUIET)
173 error1("open_sgi: setting audio buffer failed: %s", alGetErrorString(oserror()));
174 goto open_sgi_bad;
175 }
176
177 if( set_format(ao, config) < 0
178 || set_rate(ao, config) < 0
179 || set_channels(ao, config) < 0 )
180 goto open_sgi_bad;
181
182 /* Open the audio port */
183 port = alOpenPort("mpg123-VSC", "w", config);
184 if(port == NULL)
185 {
186 if(!AOQUIET)
187 error1("Unable to open audio channel: %s", alGetErrorString(oserror()));
188 goto open_sgi_bad;
189 }
190
191 ao->userptr = (void*)port;
192
193 alFreeConfig(config);
194 return 1;
195
196 open_sgi_bad:
197 /* clean up and return error */
198 alFreeConfig(config);
199 return -1;
200 }
201
202
203 static int get_formats_sgi(out123_handle *ao)
204 {
205 return MPG123_ENC_SIGNED_16|MPG123_ENC_FLOAT_32;
206 }
207
208
209 static int write_sgi(out123_handle *ao, unsigned char *buf, int len)
210 {
211 int length = len;
212
213 if(!ao || !ao->userptr) return -1;
214
215 ALport port = (ALport)ao->userptr;
216
217 if(ao->channels == 2) length >>= 2;
218 else length >>= 1;
219
220 if(ao->format == MPG123_ENC_FLOAT_32) length >>=1;
221
222 /* Not much error checking ... */
223 alWriteFrames(port, buf, length);
224
225 return len;
226 }
227
228
229 static int close_sgi(out123_handle *ao)
230 {
231 if(!ao || !ao->userptr) return -1;
232
233 ALport port = (ALport)ao->userptr;
234
235 if(port)
236 {
237 /* play all remaining samples */
238 while(alGetFilled(port) > 0) sginap(1);
239
240 alClosePort(port);
241 ao->userptr=NULL;
242 }
243 return 0;
244 }
245
246 static void flush_sgi(out123_handle *ao)
247 {
248 ALport port = (ALport)ao->userptr;
249
250 if(port) alDiscardFrames(port, alGetFilled(port));
251 }
252
253 static int init_sgi(out123_handle* ao)
254 {
255 if(ao == NULL) return -1;
256
257 /* Set callbacks */
258 ao->open = open_sgi;
259 ao->flush = flush_sgi;
260 ao->write = write_sgi;
261 ao->get_formats = get_formats_sgi;
262 ao->close = close_sgi;
263
264 /* Success */
265 return 0;
266 }
267
268 /*
269 Module information data structure
270 */
271 mpg123_module_t mpg123_output_module_info =
272 {
273 /* api_version */ MPG123_MODULE_API_VERSION,
274 /* name */ "sgi",
275 /* description */ "Audio output for SGI.",
276 /* revision */ "$Rev:$",
277 /* handle */ NULL,
278
279 /* init_output */ init_sgi,
280 };