binutils/ld: Don't add /usr/lib to the library search path twice.
[dragonfly.git] / share / examples / isdn / contrib / hplay.c
1 /*---------------------------------------------------------------------------*
2  *
3  *      rsynth driver to output to 
4  *              - an open isdn4bsd telephone connection         or
5  *              - an output file                                or
6  *              - the /dev/audio device
7  *      ----------------------------------------------------------------
8  *
9  *      tested with rsynth-2.0
10  *
11  *      written by Hellmuth Michaelis (hm@kts.org)
12  *
13  *      last edit-date: [Fri May 25 15:21:33 2001]
14  *
15  * $FreeBSD: src/share/examples/isdn/contrib/hplay.c,v 1.1.2.1 2001/08/10 14:59:47 obrien Exp $
16  * $DragonFly: src/share/examples/isdn/contrib/hplay.c,v 1.2 2003/06/17 04:36:57 dillon Exp $
17  *
18  *---------------------------------------------------------------------------*/
19
20 #include <config.h>
21 #include <useconfig.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <sys/file.h>
28 #include <sys/stat.h>
29 #include <sys/param.h>
30 #include <sys/signal.h>
31 #include <sys/ioctl.h>
32
33 #include <machine/i4b_tel_ioctl.h>
34
35 #include "proto.h"
36 #include "getargs.h"
37 #include "hplay.h"
38 #include "l2u.h"
39
40 #define SAMP_RATE 8000
41 long samp_rate = SAMP_RATE;
42
43 char *prog = "hplay";
44
45 static int use_audio = 1;
46 static int use_isdn = 0;
47 static int unit_no = 0;
48
49 static int audio_fd = -1;
50 static int isdn_fd = -1;
51 static int file_fd = -1;
52
53 char *audio_dev = "/dev/dsp";
54 char *isdn_dev = "/dev/i4btel";
55 static char *ulaw_file = NULL;
56
57 int
58 audio_init(int argc, char *argv[])
59 {
60         char dev[64];
61         int format = CVT_ALAW2ULAW;
62
63         prog = argv[0];
64
65         argc = getargs("FreeBSD audio/i4b/file output driver",argc, argv,
66                 "a", NULL, &use_audio, "use /dev/audio (default)",
67                 "i", NULL, &use_isdn,  "use /dev/i4btel",
68                 "u", "%d", &unit_no,   "/dev/i4btel unit number (def = 0)",
69                 "f", "",   &ulaw_file, "u-law output to file",
70                 NULL);
71
72         if(help_only)
73                 return argc;
74
75         if(ulaw_file)
76         {
77                 if(strcmp(ulaw_file, "-") == 0)
78                 {
79                         file_fd = 1;                 /* stdout */
80                 }
81                 else
82                 {
83                         file_fd = open(ulaw_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
84                         if(file_fd < 0)
85                                 fprintf(stderr, "ERROR: cannot open %s, error = %s\n", ulaw_file, strerror(errno));
86                 }
87         }
88
89         if(use_isdn)
90         {
91                 sprintf(dev, "%s%d", isdn_dev, unit_no);
92         
93                 if((isdn_fd = open(dev, O_WRONLY)) < 0)
94                 {
95                         fprintf(stderr, "ERROR: cannot open %s, error = %s\n", dev, strerror(errno));
96                 }
97         
98                 if((ioctl(isdn_fd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
99                 {
100                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
101                 }
102         }
103
104         if(use_audio)
105         {
106                 audio_fd = open(audio_dev, O_WRONLY | O_NDELAY);
107                 if(audio_fd < 0)
108                 {
109                         fprintf(stderr, "ERROR: cannot open %s, error = %s\n", audio_dev, strerror(errno));
110                 }
111         }
112
113         return argc;
114 }
115
116 void
117 audio_term()
118 {
119         int format = CVT_NONE;
120
121         if(isdn_fd >= 0)
122         {
123                 if((ioctl(isdn_fd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
124                 {
125                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
126                 }
127                 close(isdn_fd);
128                 isdn_fd = -1;
129         }
130
131         if(audio_fd >= 0)
132         {
133 #if 0
134                 ioctl(audio_fd, SNDCTL_DSP_SYNC, &dummy);
135 #endif
136                 close(audio_fd);
137                 audio_fd = -1;
138         }
139
140         if(file_fd >= 0)
141         {
142                 close(file_fd);
143                 file_fd = -1;
144         }
145 }
146
147 void
148 audio_play(int n, short *data)
149 {
150         int ret;
151         unsigned char *p;
152
153         if (n > 0)
154         {
155                 unsigned char *converted = (unsigned char *) malloc(n);
156                 int i;
157
158                 if(converted == NULL)
159                 {
160                         fprintf(stderr, "Could not allocate memory for conversion\n");
161                         exit(3);
162                 }
163
164                 for (i = 0; i < n; i++)
165                 {
166                         converted[i] = short2ulaw(data[i]);
167                 }
168
169                 if (isdn_fd >= 0)
170                 {
171                         p = converted;
172                         errno = 0;
173                         
174                         while((ret = write(isdn_fd, p, n)) != n)
175                         {
176                                 if(!errno)
177                                 {
178                                         p += ret;
179                                         if(p > (converted + n))
180                                                 break;
181                                 }
182                                 else
183                                 {
184                                         fprintf(stderr, "write /dev/i4btel ERROR: ret (%d) != n (%d), error = %s\n", ret, n, strerror(errno));
185                                         break;
186                                 }
187                         }
188                 }
189
190                 for (i = 0; i < n; i++)
191                         converted[i] = (data[i] - 32768) / 256;
192
193                 if(audio_fd >= 0)
194                 {
195                         p = converted;
196
197                         errno = 0;
198                         
199                         while((ret = write(audio_fd, p, n)) != n)
200                         {
201                                 if(!errno)
202                                 {
203                                         p += ret;
204                                         if(p > (converted + n))
205                                                 break;
206                                 }
207                                 else
208                                 {
209                                         fprintf(stderr, "write /dev/dsp ERROR: ret (%d) != n (%d), error = %s\n", ret, n, strerror(errno));
210                                         break;
211                                 }
212                         }
213                 }
214
215                 if(file_fd >= 0)
216                 {
217                         int ret;
218                         p = converted;
219
220                         errno = 0;
221                         
222                         while((ret = write(file_fd, p, n)) != n)
223                         {
224                                 if(!errno)
225                                 {
226                                         p += ret;
227                                         if(p > (converted + n))
228                                                 break;
229                                 }
230                                 else
231                                 {
232                                         fprintf(stderr, "write file ERROR: ret (%d) != n (%d), error = %s\n", ret, n, strerror(errno));
233                                         break;
234                                 }
235                         }
236                 }
237
238                 free(converted);
239         }
240 }
241
242 /* EOF */