Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.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  * $DragonFly: src/usr.sbin/i4b/isdnphone/audio.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
34  *
35  *      last edit-date: [Mon Dec 13 21:52:39 1999]
36  *
37  *----------------------------------------------------------------------------*/
38
39 #include "defs.h"
40
41 /*---------------------------------------------------------------------------*
42  *
43  *---------------------------------------------------------------------------*/
44 int
45 init_audio(char *audiodevice)
46 {
47         int ret;
48         int fd;
49         u_long fmt = 0;
50
51         snd_chan_param pa;
52         struct snd_size sz;
53         snd_capabilities soundcaps;
54         
55         if((fd = open(audiodevice, O_RDWR)) < 0)
56         {
57                 fprintf(stderr, "unable to open %s: %s\n", audiodevice, strerror(errno));
58                 return(-1);
59         }
60
61         ret = ioctl(fd, AIOGCAP, &soundcaps);   
62
63         if(ret == -1)
64         {
65                 fprintf(stderr, "ERROR: ioctl AIOGCAP %s: %s\n", audiodevice, strerror(errno));
66                 return(-1);
67         }
68
69         fmt = soundcaps.formats;
70
71         if((fmt & AFMT_FULLDUPLEX) && (!(fmt & AFMT_WEIRD)))
72         {
73 #ifdef NOTDEF
74                         if(fmt & AFMT_A_LAW)
75                         {
76                                 play_fmt = rec_fmt = AFMT_A_LAW;
77                         }
78                         else
79 #endif
80                         if(fmt & AFMT_MU_LAW)
81                         {
82                                 play_fmt = rec_fmt = AFMT_MU_LAW;
83                         }
84                         else
85                         {
86                                 printf("sorry, A-law or u-law not supported!\n");
87                                 close(fd);
88                                 return(-1);
89                         }
90         }
91         else
92         {
93                 printf("no full-duplex available!\n");
94                 close (fd);
95                 return(-1);
96         }
97         
98         pa.play_format = play_fmt;
99         pa.rec_format = rec_fmt;
100         pa.play_rate = pa.rec_rate = AUDIORATE;
101
102         ret = ioctl(fd, AIOSFMT, &pa);
103         
104         if(ret == -1)
105         {
106                 fprintf(stderr, "ERROR: ioctl AIOSFMT %s: %s\n", audiodevice, strerror(errno));
107                 return(-1);
108         }
109
110         sz.play_size = BCH_MAX_DATALEN;
111         sz.rec_size = BCH_MAX_DATALEN;
112
113         ret = ioctl(fd, AIOSSIZE, &sz);
114
115         if(ret == -1)
116         {
117                 fprintf(stderr, "ERROR: ioctl AIOSSIZE %s: %s\n", audiodevice, strerror(errno));
118                 return(-1);
119         }
120
121         return(fd);
122 }
123         
124 /*---------------------------------------------------------------------------*
125  *      audio device has speech data from microphone
126  *---------------------------------------------------------------------------*/
127 void
128 audio_hdlr(void)
129 {
130         unsigned char buffer[BCH_MAX_DATALEN];
131         int ret;
132
133         ret = read(audiofd, buffer, BCH_MAX_DATALEN);
134         
135         if(ret < 0)
136         {
137                 fatal("read audio failed: %s", strerror(errno));
138         }
139
140         debug("audio_hdlr: read %d bytes\n", ret);
141
142         if(ret > 0)
143         {
144                 telwrite(ret, buffer);
145         }
146 }
147
148 /*---------------------------------------------------------------------------*
149  *      write audio data to loudspeaker
150  *---------------------------------------------------------------------------*/
151 void
152 audiowrite(int len, unsigned char *buf)
153 {
154         if((write(audiofd, buf, len)) < 0)
155         {
156                 fatal("write audio failed: %s", strerror(errno));
157         }
158 }
159
160 /* EOF */