summaryrefslogtreecommitdiffstats
path: root/share/examples/isdn/contrib/hplay.c
blob: 10660ac7b7b15d33173876ba91f23c7284927b0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*---------------------------------------------------------------------------*
 *
 *	rsynth driver to output to 
 *		- an open isdn4bsd telephone connection		or
 *		- an output file				or
 *		- the /dev/audio device
 *      ----------------------------------------------------------------
 *
 *	tested with rsynth-2.0
 *
 *	written by Hellmuth Michaelis (hm@kts.org)
 *
 *	last edit-date: [Fri May 25 15:21:33 2001]
 *
 * $FreeBSD$
 *
 *---------------------------------------------------------------------------*/

#include <config.h>
#include <useconfig.h>
#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/signal.h>
#include <sys/ioctl.h>

#include <machine/i4b_tel_ioctl.h>

#include "proto.h"
#include "getargs.h"
#include "hplay.h"
#include "l2u.h"

#define SAMP_RATE 8000
long samp_rate = SAMP_RATE;

char *prog = "hplay";

static int use_audio = 1;
static int use_isdn = 0;
static int unit_no = 0;

static int audio_fd = -1;
static int isdn_fd = -1;
static int file_fd = -1;

char *audio_dev = "/dev/dsp";
char *isdn_dev = "/dev/i4btel";
static char *ulaw_file = NULL;

int
audio_init(int argc, char *argv[])
{
	char dev[64];
	int format = CVT_ALAW2ULAW;

	prog = argv[0];

	argc = getargs("FreeBSD audio/i4b/file output driver",argc, argv,
                "a", NULL, &use_audio, "use /dev/audio (default)",
                "i", NULL, &use_isdn,  "use /dev/i4btel",
		"u", "%d", &unit_no,   "/dev/i4btel unit number (def = 0)",
                "f", "",   &ulaw_file, "u-law output to file",
                NULL);

	if(help_only)
		return argc;

	if(ulaw_file)
	{
		if(strcmp(ulaw_file, "-") == 0)
		{
			file_fd = 1;                 /* stdout */
		}
		else
		{
			file_fd = open(ulaw_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
			if(file_fd < 0)
				fprintf(stderr, "ERROR: cannot open %s, error = %s\n", ulaw_file, strerror(errno));
		}
	}

	if(use_isdn)
	{
		sprintf(dev, "%s%d", isdn_dev, unit_no);
	
		if((isdn_fd = open(dev, O_WRONLY)) < 0)
		{
			fprintf(stderr, "ERROR: cannot open %s, error = %s\n", dev, strerror(errno));
		}
	
		if((ioctl(isdn_fd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
	        {
			fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
		}
	}

	if(use_audio)
	{
		audio_fd = open(audio_dev, O_WRONLY | O_NDELAY);
		if(audio_fd < 0)
  		{
			fprintf(stderr, "ERROR: cannot open %s, error = %s\n", audio_dev, strerror(errno));
		}
	}

	return argc;
}

void
audio_term()
{
	int format = CVT_NONE;

	if(isdn_fd >= 0)
	{
		if((ioctl(isdn_fd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
        	{
			fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
		}
		close(isdn_fd);
		isdn_fd = -1;
	}

	if(audio_fd >= 0)
	{
#if 0
		ioctl(audio_fd, SNDCTL_DSP_SYNC, &dummy);
#endif
		close(audio_fd);
		audio_fd = -1;
	}

	if(file_fd >= 0)
	{
		close(file_fd);
		file_fd = -1;
	}
}

void
audio_play(int n, short *data)
{
	int ret;
	unsigned char *p;

	if (n > 0)
	{
		unsigned char *converted = (unsigned char *) malloc(n);
		int i;

		if(converted == NULL)
		{
			fprintf(stderr, "Could not allocate memory for conversion\n");
			exit(3);
    		}

		for (i = 0; i < n; i++)
		{
			converted[i] = short2ulaw(data[i]);
		}

   		if (isdn_fd >= 0)
    		{
			p = converted;
			errno = 0;
    			
     			while((ret = write(isdn_fd, p, n)) != n)
     			{
     				if(!errno)
     				{
     					p += ret;
     					if(p > (converted + n))
     						break;
     				}
     				else
     				{
	     				fprintf(stderr, "write /dev/i4btel ERROR: ret (%d) != n (%d), error = %s\n", ret, n, strerror(errno));
	     				break;
	     			}
			}
    		}

		for (i = 0; i < n; i++)
			converted[i] = (data[i] - 32768) / 256;

   		if(audio_fd >= 0)
    		{
			p = converted;

			errno = 0;
    			
     			while((ret = write(audio_fd, p, n)) != n)
     			{
     				if(!errno)
     				{
     					p += ret;
     					if(p > (converted + n))
     						break;
     				}
     				else
     				{
	     				fprintf(stderr, "write /dev/dsp ERROR: ret (%d) != n (%d), error = %s\n", ret, n, strerror(errno));
	     				break;
	     			}
			}
    		}

   		if(file_fd >= 0)
    		{
    			int ret;
			p = converted;

			errno = 0;
    			
     			while((ret = write(file_fd, p, n)) != n)
     			{
     				if(!errno)
     				{
     					p += ret;
     					if(p > (converted + n))
     						break;
     				}
     				else
     				{
	     				fprintf(stderr, "write file ERROR: ret (%d) != n (%d), error = %s\n", ret, n, strerror(errno));
	     				break;
	     			}
			}
    		}

   		free(converted);
  	}
}

/* EOF */
OpenPOWER on IntegriCloud