root/rat/trunk/auddev_sgi.c @ 1990

Revision 1990, 7.0 KB (checked in by ucaccsp, 16 years ago)

Fix compilation on Irix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * FILE:    sgi.c
3 * PROGRAM: RAT
4 * AUTHOR:  Isidor Kouvelas + Colin Perkins
5 *
6 * $Revision$
7 * $Date$
8 *
9 * Copyright (c) 1995,1996 University College London
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, is permitted, for non-commercial use only, provided
14 * that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *      This product includes software developed by the Computer Science
23 *      Department at University College London
24 * 4. Neither the name of the University nor of the Department may be used
25 *    to endorse or promote products derived from this software without
26 *    specific prior written permission.
27 * Use of this software for commercial purposes is explicitly forbidden
28 * unless prior written permission is obtained from the authors.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43#include "config_unix.h"
44#include "config_win32.h"
45#include "audio.h"
46
47#if defined(IRIX)
48
49#define READ_UNIT       80
50#define QSIZE           16000           /* Two seconds for now... */
51#define AVG_SIZE        20000
52#define SAMSIG          7500.0
53#define BASEOFF         0x0a7f
54
55#define bat_to_device(x)        ((x) * 255 / MAX_AMP)
56#define device_to_bat(x)        ((x) * MAX_AMP / 255)
57
58static ALport   rp, wp;         /* Read and write ports */
59static int      iport = AUDIO_MICROPHONE;
60
61/*
62 * Try to open the audio device.
63 * Return TRUE if successfull FALSE otherwise.
64 */
65int
66audio_open(audio_format format)
67{
68        int             audio_fd = -1;
69        ALconfig        c;
70        long            cmd[8];
71
72        if ((c = ALnewconfig()) == NULL) {
73                fprintf(stderr, "ALnewconfig error\n");
74                exit(1);
75        }
76
77        ALsetchannels(c, AL_MONO);
78        ALsetwidth(c, AL_SAMPLE_16);
79        ALsetqueuesize(c, QSIZE);
80        ALsetsampfmt(c, AL_SAMPFMT_TWOSCOMP);
81        if ((wp = ALopenport("RAT write", "w", c)) == NULL)
82                fprintf(stderr, "ALopenport (write) error\n");
83        if ((rp = ALopenport("RAT read", "r", c)) == NULL)
84                fprintf(stderr, "ALopenport (read) error\n");
85
86        cmd[0] = AL_OUTPUT_RATE;
87        cmd[1] = format.sample_rate;
88        cmd[2] = AL_INPUT_SOURCE;
89        cmd[3] = AL_INPUT_MIC;
90        cmd[4] = AL_INPUT_RATE;
91        cmd[5] = format.sample_rate;
92        cmd[6] = AL_MONITOR_CTL;
93        cmd[7] = AL_MONITOR_OFF;
94        if (ALsetparams(AL_DEFAULT_DEVICE, cmd, 8L) == -1)
95                fprintf(stderr, "audio_open/ALsetparams error\n");
96
97        /* Get the file descriptor to use in select */
98        audio_fd = ALgetfd(rp);
99
100        ALsetfillpoint(rp, READ_UNIT);
101
102        /* We probably should free the config here... */
103
104        return audio_fd;
105}
106
107/* Close the audio device */
108void
109audio_close(int audio_fd)
110{
111        ALcloseport(rp);
112        ALcloseport(wp);
113}
114
115/* Flush input buffer */
116void
117audio_drain(int audio_fd)
118{
119        sample  buf[QSIZE];
120        while(audio_read(audio_fd, buf, QSIZE) == QSIZE);
121}
122
123/* Gain and volume values are in the range 0 - MAX_AMP */
124
125void
126audio_set_gain(int audio_fd, int gain)
127{
128        long    cmd[4];
129
130        if (audio_fd == 0)
131                return;
132
133        cmd[0] = AL_LEFT_INPUT_ATTEN;
134        cmd[1] = 255 - bat_to_device(gain);
135        cmd[2] = AL_RIGHT_INPUT_ATTEN;
136        cmd[3] = cmd[1];
137        ALsetparams(AL_DEFAULT_DEVICE, cmd, 4L);
138}
139
140int
141audio_get_gain(int audio_fd)
142{
143        long    cmd[2];
144
145        if (audio_fd == 0)
146                return 50;
147
148        cmd[0] = AL_LEFT_INPUT_ATTEN;
149        ALgetparams(AL_DEFAULT_DEVICE, cmd, 2L);
150        return (255 - device_to_bat(cmd[1]));
151}
152
153void
154audio_set_volume(int audio_fd, int vol)
155{
156        long    cmd[4];
157
158        if (audio_fd == 0)
159                return;
160
161        cmd[0] = AL_LEFT_SPEAKER_GAIN;
162        cmd[1] = bat_to_device(vol);
163        cmd[2] = AL_RIGHT_SPEAKER_GAIN;
164        cmd[3] = cmd[1];
165        ALsetparams(AL_DEFAULT_DEVICE, cmd, 4L);
166}
167
168int
169audio_get_volume(int audio_fd)
170{
171        long    cmd[2];
172
173        if (audio_fd == 0)
174                return 50;
175
176        cmd[0] = AL_LEFT_SPEAKER_GAIN;
177        ALgetparams(AL_DEFAULT_DEVICE, cmd, 2L);
178        return (device_to_bat(cmd[1]));
179}
180
181static int non_block = 1;       /* Initialise to non blocking */
182
183int
184audio_read(int audio_fd, sample *buf, int samples)
185{
186        long            len;
187       
188        if (non_block) {
189                if ((len = ALgetfilled(rp)) <= 0)
190                        return (0);
191                len -= len % READ_UNIT;
192                if (len <= 0)
193                        len = READ_UNIT;
194                if (len > samples)
195                        len = samples;
196        } else
197                len = (long)samples;
198
199        if (len > QSIZE) {
200                fprintf(stderr, "audio_read: too big!\n");
201                len = QSIZE;
202        }
203
204        ALreadsamps(rp, buf, len);
205
206        return ((int)len);
207}
208
209int
210audio_write(int audio_fd, sample *buf, int samples)
211{
212        if (samples > QSIZE) {
213                fprintf(stderr, "audio_write: too big!\n");
214                samples = QSIZE;
215        }
216
217        /* Will block */
218        ALwritesamps(wp, buf, (long)samples);
219        return (samples);
220}
221
222/* Set ops on audio device to be non-blocking */
223void
224audio_non_block(int audio_fd)
225{
226        non_block = 1;
227}
228
229/* Set ops on audio device to block */
230void
231audio_block(int audio_fd)
232{
233        non_block = 0;
234}
235
236void
237audio_set_oport(int audio_fd, int port)
238{
239        /* Not possible? */
240}
241
242int
243audio_get_oport(int audio_fd)
244{
245        return (AUDIO_SPEAKER);
246}
247
248int
249audio_next_oport(int audio_fd)
250{
251        return (AUDIO_SPEAKER);
252}
253
254void
255audio_set_iport(int audio_fd, int port)
256{
257        long pvbuf[2];
258
259        if (audio_fd > 0) {
260                switch(port) {
261                case AUDIO_MICROPHONE : pvbuf[0] = AL_INPUT_SOURCE;
262                                        pvbuf[1] = AL_INPUT_MIC;
263                                        ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2);
264                                        iport = AUDIO_MICROPHONE;
265                                        break;
266                case AUDIO_LINE_IN    : pvbuf[0] = AL_INPUT_SOURCE;
267                                        pvbuf[1] = AL_INPUT_LINE;
268                                        ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2);
269                                        iport = AUDIO_LINE_IN;
270                                        break;
271                default               : printf("Illegal input port!\n");
272                                        abort();
273                }
274        }
275}
276
277int
278audio_get_iport(int audio_fd)
279{
280        return iport;
281}
282
283int
284audio_next_iport(int audio_fd)
285{
286  switch (iport) {
287    case AUDIO_MICROPHONE : audio_set_iport(audio_fd, AUDIO_LINE_IN);
288                            break;
289    case AUDIO_LINE_IN    : audio_set_iport(audio_fd, AUDIO_MICROPHONE);
290                            break;
291    default               : printf("Unknown audio source!\n");
292  }
293  return iport;
294}
295
296void
297audio_switch_out(int audio_fd, struct s_cushion_struct *ap)
298{
299        /* Full duplex device: do nothing! */
300}
301   
302void
303audio_switch_in(int audio_fd)
304{
305        /* Full duplex device: do nothing! */
306}
307
308int
309audio_duplex(int audio_fd)
310{
311  return 1;
312}
313
314int
315audio_get_channels(void)
316{
317        return ALgetchannels(ALgetconfig(rp));
318}
319
320#endif
Note: See TracBrowser for help on using the browser.