Debian Codegraph @

src/libout123/modules/aix.c

raw
1 /*
2 aix: Driver for IBM RS/6000 with AIX Ultimedia Services
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 Juergen Schoew and Tomas Oegren
7
8 untested since quite some years now ...
9 */
10
11 #include "../out123_int.h"
12
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/audio.h>
20 #include <stropts.h>
21 #include <sys/types.h>
22 #include <sys/file.h>
23 #include <sys/stat.h>
24 #include <sys/param.h>
25
26 #include "../../common/debug.h"
27
28 /* use AUDIO_BSIZE to set the msec for audio buffering in Ultimedia library
29 */
30 /* #define AUDIO_BSIZE AUDIO_IGNORE */
31 #define AUDIO_BSIZE ( ao->device_buffer > 0. \
32 ? (long)(ao->device_buffer*1000) \
33 : 200 )
34
35 static int rate_best_match(out123_handle *ao)
36 {
37 static long valid [ ] = { 5510, 6620, 8000, 9600, 11025, 16000, 18900,
38 22050, 27420, 32000, 33075, 37800, 44100, 48000, 0 };
39 int i = 0;
40 long best = 8000;
41
42 if(!ao || ao->fn < 0 || ao->rate < 0) {
43 return -1;
44 }
45
46 while (valid [i]) {
47 if (abs(valid[i] - ao->rate) < abs(best - ao->rate))
48 {
49 best = valid [i];
50 }
51 i = i + 1;
52 }
53
54 ao->rate = best;
55 return best;
56 }
57
58 static int reset_parameters(out123_handle *ao)
59 {
60 audio_control acontrol;
61 audio_change achange;
62 audio_init ainit;
63 int ret;
64
65 memset ( & achange, '\0', sizeof (achange));
66 memset ( & acontrol, '\0', sizeof (acontrol));
67
68 achange.balance = 0x3fff0000;
69 achange.balance_delay = 0;
70 achange.volume = (long) (0x7fff << 16);
71 achange.volume_delay = 0;
72 achange.input = AUDIO_IGNORE;
73 if (ao->flags == -1) achange.output = INTERNAL_SPEAKER;
74 else achange.output = 0;
75
76 if(ao->flags & OUT123_INTERNAL_SPEAKER)
77 achange.output |= INTERNAL_SPEAKER;
78 if(ao->flags & OUT123_HEADPHONES)
79 achange.output |= EXTERNAL_SPEAKER;
80 if(ao->flags & OUT123_LINE_OUT)
81 achange.output |= OUTPUT_1;
82 if(ao->flags == 0)
83 achange.output = AUDIO_IGNORE;
84
85 achange.treble = AUDIO_IGNORE;
86 achange.bass = AUDIO_IGNORE;
87 achange.pitch = AUDIO_IGNORE;
88 achange.monitor = AUDIO_IGNORE;
89 achange.dev_info = (char *) NULL;
90
91 acontrol.ioctl_request = AUDIO_CHANGE;
92 acontrol.position = 0;
93 acontrol.request_info = (char *) & achange;
94
95 ret = ioctl (ao->fn, AUDIO_CONTROL, & acontrol);
96 if (ret < 0)
97 return ret;
98
99 /* Init Device for new values */
100 if (ao->rate >0) {
101 memset ( & ainit, '\0', sizeof (ainit));
102 ainit.srate = rate_best_match(ao);
103 if (ao->channels > 0)
104 ainit.channels = ao->channels;
105 else
106 ainit.channels = 1;
107 switch (ao->format) {
108 default :
109 ainit.mode = PCM;
110 ainit.bits_per_sample = 8;
111 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT;
112 break;
113 case MPG123_ENC_SIGNED_16:
114 ainit.mode = PCM;
115 ainit.bits_per_sample = 16;
116 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT;
117 break;
118 case MPG123_ENC_SIGNED_8:
119 ainit.mode = PCM;
120 ainit.bits_per_sample = 8;
121 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT;
122 break;
123 case MPG123_ENC_UNSIGNED_16:
124 ainit.mode = PCM;
125 ainit.bits_per_sample = 16;
126 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT | SIGNED;
127 break;
128 case MPG123_ENC_UNSIGNED_8:
129 ainit.mode = PCM;
130 ainit.bits_per_sample = 8;
131 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT | SIGNED;
132 break;
133 case MPG123_ENC_ULAW_8:
134 ainit.mode = MU_LAW;
135 ainit.bits_per_sample = 8;
136 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT;
137 break;
138 case MPG123_ENC_ALAW_8:
139 ainit.mode = A_LAW;
140 ainit.bits_per_sample = 8;
141 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT;
142 break;
143 }
144 ainit.operation = PLAY;
145 ainit.bsize = AUDIO_BSIZE;
146
147 ret = ioctl (ao->fn, AUDIO_INIT, & ainit);
148 if (ret < 0) {
149 if (!AOQUIET)
150 error("Can't set new audio parameters!");
151 return ret;
152 }
153 }
154
155 acontrol.ioctl_request = AUDIO_START;
156 acontrol.request_info = NULL;
157 acontrol.position = 0;
158
159 ret = ioctl (ao->fn, AUDIO_CONTROL, & acontrol);
160 if (ret < 0) {
161 if (!AOQUIET)
162 error("Can't reset audio!");
163 return ret;
164 }
165 return 0;
166 }
167
168 static int open_aix(out123_handle *ao)
169 {
170 audio_init ainit;
171 int ret;
172 const char *dev = ao->device;
173
174 if(!dev) {
175 if(getenv("AUDIODEV")) {
176 dev = getenv("AUDIODEV");
177 ao->fn = open(dev,O_WRONLY);
178 } else {
179 dev = "/dev/paud0/1"; /* paud0 for PCI */
180 ao->fn = open(dev,O_WRONLY);
181 if ((ao->fn == -1) & (errno == ENOENT)) {
182 dev = "/dev/baud0/1"; /* baud0 for MCA */
183 ao->fn = open(dev,O_WRONLY);
184 }
185 }
186 } else ao->fn = open(dev,O_WRONLY);
187
188 if(ao->fn < 0) {
189 if(!AOQUIET)
190 error("Can't open audio device!");
191 return ao->fn;
192 }
193
194 /* Init to default values */
195 memset ( & ainit, '\0', sizeof (ainit));
196 ainit.srate = 44100;
197 ainit.channels = 2;
198 ainit.mode = PCM;
199 ainit.bits_per_sample = 16;
200 ainit.flags = BIG_ENDIAN | TWOS_COMPLEMENT;
201 ainit.operation = PLAY;
202 ainit.bsize = AUDIO_BSIZE;
203
204 ret = ioctl (ao->fn, AUDIO_INIT, & ainit);
205 if (ret < 0) return ret;
206
207 reset_parameters(ao);
208 return ao->fn;
209 }
210
211
212
213 static int get_formats_aix(out123_handle *ao)
214 {
215 /* ULTIMEDIA DOCUMENTATION SAYS:
216 The Ultimedia Audio Adapter supports fourteen sample rates you can use to
217 capture and playback audio data. The rates are (in kHz): 5.51, 6.62, 8.0,
218 9.6, 11.025, 16.0, 18.9, 22.050, 27.42, 32.0, 33.075, 37.8, 44.1, and 48.0.
219 These rates are supported for mono and stereo PCM (8- and 16-bit), mu-law,
220 and A-law.
221 */
222
223 long rate;
224
225 rate = ao->rate;
226 rate_best_match(ao);
227 if (ao->rate == rate)
228 return (MPG123_ENC_SIGNED_16|MPG123_ENC_UNSIGNED_16|
229 MPG123_ENC_UNSIGNED_8|MPG123_ENC_SIGNED_8|
230 MPG123_ENC_ULAW_8|MPG123_ENC_ALAW_8);
231 else
232 return 0;
233 }
234
235 static int write_aix(out123_handle *ao,unsigned char *buf,int len)
236 {
237 return write(ao->fn,buf,len);
238 }
239
240 static int close_aix(out123_handle *ao)
241 {
242 audio_control acontrol;
243 audio_buffer abuffer;
244 int ret,i;
245
246 /* Don't close the audio-device until it's played all its contents */
247 memset ( & acontrol, '\0', sizeof ( acontrol ) );
248 acontrol.request_info = &abuffer;
249 acontrol.position = 0;
250 i=50; /* Don't do this forever on a bad day :-) */
251
252 while (i-- > 0) {
253 if ((ioctl(ao->fn, AUDIO_BUFFER, &acontrol))< 0) {
254 if (!AOQUIET)
255 error1("buffer read failed: %d", errno);
256 break;
257 } else {
258 if (abuffer.flags <= 0) break;
259 }
260 usleep(200000); /* sleep 0.2 sec */
261 }
262
263 memset ( & acontrol, '\0', sizeof ( acontrol ) );
264 acontrol.ioctl_request = AUDIO_STOP;
265 acontrol.request_info = NULL;
266 acontrol.position = 0;
267
268 ret = ioctl ( ao->fn, AUDIO_CONTROL, & acontrol );
269 if (ret < 0 && !AOQUIET)
270 error("Can't close audio!");
271 ret = close (ao->fn);
272 if (ret < 0 && !AOQUIET)
273 error("Can't close audio!");
274 return 0;
275 }
276
277 static void flush_aix(out123_handle *ao)
278 {
279 }
280
281 static int init_aix(out123_handle* ao)
282 {
283 if (ao==NULL) return -1;
284
285 /* Set callbacks */
286 ao->open = open_aix;
287 ao->flush = flush_aix;
288 ao->write = write_aix;
289 ao->get_formats = get_formats_aix;
290 ao->close = close_aix;
291
292 /* Success */
293 return 0;
294 }
295
296
297
298 /*
299 Module information data structure
300 */
301 mpg123_module_t mpg123_output_module_info = {
302 /* api_version */ MPG123_MODULE_API_VERSION,
303 /* name */ "aix",
304 /* description */ "Output audio on IBM RS/6000 with AIX Ultimedia Services.",
305 /* revision */ "$Rev: 932 $",
306 /* handle */ NULL,
307
308 /* init_output */ init_aix,
309 };
310