Initial import from FreeBSD RELENG_4:
[games.git] / usr.sbin / i4b / isdnphone / audio.c
1 /*
2  * Copyright (c) 1999 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      isdnphone - audio operations
28  *      ============================
29  *
30  *      $Id: audio.c,v 1.5 1999/12/13 21:25:26 hm Exp $ 
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdnphone/audio.c,v 1.1.2.1 2001/08/01 17:45:06 obrien Exp $
33  *
34  *      last edit-date: [Mon Dec 13 21:52:39 1999]
35  *
36  *----------------------------------------------------------------------------*/
37
38 #include "defs.h"
39
40 /*---------------------------------------------------------------------------*
41  *
42  *---------------------------------------------------------------------------*/
43 int
44 init_audio(char *audiodevice)
45 {
46         int ret;
47         int fd;
48         u_long fmt = 0;
49
50         snd_chan_param pa;
51         struct snd_size sz;
52         snd_capabilities soundcaps;
53         
54         if((fd = open(audiodevice, O_RDWR)) < 0)
55         {
56                 fprintf(stderr, "unable to open %s: %s\n", audiodevice, strerror(errno));
57                 return(-1);
58         }
59
60         ret = ioctl(fd, AIOGCAP, &soundcaps);   
61
62         if(ret == -1)
63         {
64                 fprintf(stderr, "ERROR: ioctl AIOGCAP %s: %s\n", audiodevice, strerror(errno));
65                 return(-1);
66         }
67
68         fmt = soundcaps.formats;
69
70         if((fmt & AFMT_FULLDUPLEX) && (!(fmt & AFMT_WEIRD)))
71         {
72 #ifdef NOTDEF
73                         if(fmt & AFMT_A_LAW)
74                         {
75                                 play_fmt = rec_fmt = AFMT_A_LAW;
76                         }
77                         else
78 #endif
79                         if(fmt & AFMT_MU_LAW)
80                         {
81                                 play_fmt = rec_fmt = AFMT_MU_LAW;
82                         }
83                         else
84                         {
85                                 printf("sorry, A-law or u-law not supported!\n");
86                                 close(fd);
87                                 return(-1);
88                         }
89         }
90         else
91         {
92                 printf("no full-duplex available!\n");
93                 close (fd);
94                 return(-1);
95         }
96         
97         pa.play_format = play_fmt;
98         pa.rec_format = rec_fmt;
99         pa.play_rate = pa.rec_rate = AUDIORATE;
100
101         ret = ioctl(fd, AIOSFMT, &pa);
102         
103         if(ret == -1)
104         {
105                 fprintf(stderr, "ERROR: ioctl AIOSFMT %s: %s\n", audiodevice, strerror(errno));
106                 return(-1);
107         }
108
109         sz.play_size = BCH_MAX_DATALEN;
110         sz.rec_size = BCH_MAX_DATALEN;
111
112         ret = ioctl(fd, AIOSSIZE, &sz);
113
114         if(ret == -1)
115         {
116                 fprintf(stderr, "ERROR: ioctl AIOSSIZE %s: %s\n", audiodevice, strerror(errno));
117                 return(-1);
118         }
119
120         return(fd);
121 }
122         
123 /*---------------------------------------------------------------------------*
124  *      audio device has speech data from microphone
125  *---------------------------------------------------------------------------*/
126 void
127 audio_hdlr(void)
128 {
129         unsigned char buffer[BCH_MAX_DATALEN];
130         int ret;
131
132         ret = read(audiofd, buffer, BCH_MAX_DATALEN);
133         
134         if(ret < 0)
135         {
136                 fatal("read audio failed: %s", strerror(errno));
137         }
138
139         debug("audio_hdlr: read %d bytes\n", ret);
140
141         if(ret > 0)
142         {
143                 telwrite(ret, buffer);
144         }
145 }
146
147 /*---------------------------------------------------------------------------*
148  *      write audio data to loudspeaker
149  *---------------------------------------------------------------------------*/
150 void
151 audiowrite(int len, unsigned char *buf)
152 {
153         if((write(audiofd, buf, len)) < 0)
154         {
155                 fatal("write audio failed: %s", strerror(errno));
156         }
157 }
158
159 /* EOF */