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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1989-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains routines to read and write the audio device state.
*/
#include <errno.h>
#include <stropts.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <libaudio_impl.h>
#include <audio_errno.h>
#include <audio_hdr.h>
#include <audio_device.h>
/*
* Get device information structure.
*/
int
audio_getinfo(int fd, Audio_info *ip)
{
if (ioctl(fd, AUDIO_GETINFO, (char *)ip) < 0) {
return (AUDIO_UNIXERROR);
} else {
return (AUDIO_SUCCESS);
}
}
/*
* Set device information structure.
* The calling routine should use AUDIO_INITINFO prior to setting new values.
*/
int
audio_setinfo(int fd, Audio_info *ip)
{
if (ioctl(fd, AUDIO_SETINFO, (char *)ip) < 0) {
return (AUDIO_UNIXERROR);
} else {
return (AUDIO_SUCCESS);
}
}
/*
* Return an Audio_hdr corresponding to the encoding configuration
* of the audio device on 'fd'.
*/
int
audio__setplayhdr(int fd, Audio_hdr *hdrp, unsigned int which)
{
Audio_hdr thdr;
Audio_info info;
struct audio_prinfo *prinfo;
int err;
if (which & AUDIO__PLAY)
prinfo = &info.play;
else if (which & AUDIO__RECORD)
prinfo = &info.record;
else
return (AUDIO_ERR_BADARG);
if (which & AUDIO__SET) {
thdr = *hdrp; /* save original hdr */
AUDIO_INITINFO(&info);
prinfo->sample_rate = hdrp->sample_rate;
prinfo->channels = hdrp->channels;
prinfo->encoding = hdrp->encoding;
prinfo->precision = hdrp->bytes_per_unit * 8;
err = audio_setinfo(fd, &info);
} else {
err = audio_getinfo(fd, &info);
}
/* Decode back into the header structure */
/* since the I_SMSG is set, upon completion of updating */
/* the format, the driver sends the msg so the */
/* system call is interrupted. For now, just ignore this */
if ((err == AUDIO_SUCCESS) || (errno == EINTR)) {
hdrp->sample_rate = prinfo->sample_rate;
hdrp->channels = prinfo->channels;
hdrp->data_size = AUDIO_UNKNOWN_SIZE;
hdrp->encoding = prinfo->encoding;
switch (hdrp->encoding) {
case AUDIO_ENCODING_ULAW:
case AUDIO_ENCODING_ALAW:
case AUDIO_ENCODING_LINEAR:
case AUDIO_ENCODING_LINEAR8:
case AUDIO_ENCODING_FLOAT:
hdrp->bytes_per_unit = prinfo->precision / 8;
hdrp->samples_per_unit = 1;
break;
default:
return (AUDIO_ERR_ENCODING);
}
if (which & AUDIO__SET) {
/* Check to see if *all* changes took effect */
if (audio_cmp_hdr(hdrp, &thdr) != 0)
return (AUDIO_ERR_NOEFFECT);
}
}
return (err);
}
/*
* Attempt to configure the audio device to match a particular encoding.
*/
/*
* Set and/or set individual state values.
* This routine is generally invoked by using the audio_set_*()
* and audio_get_*() macros.
* The 'valp' argument is always a pointer to an unsigned int.
* Conversions to/from (unsigned char) flags are taken care of.
*/
int
audio__setval(int fd, unsigned int *valp, unsigned int which)
{
Audio_info info;
struct audio_prinfo *prinfo;
int err;
unsigned *up;
unsigned char *cp;
/* Set a pointer to the value of interest */
if (which & AUDIO__PLAY)
prinfo = &info.play;
else if (which & AUDIO__RECORD)
prinfo = &info.record;
else if ((which & AUDIO__SETVAL_MASK) != AUDIO__MONGAIN)
return (AUDIO_ERR_BADARG);
up = NULL;
switch (which & AUDIO__SETVAL_MASK) {
case AUDIO__PORT:
up = &prinfo->port; break;
case AUDIO__SAMPLES:
up = &prinfo->samples; break;
case AUDIO__ERROR:
cp = &prinfo->error; break;
case AUDIO__EOF:
up = &prinfo->eof; break;
case AUDIO__OPEN:
cp = &prinfo->open; break;
case AUDIO__ACTIVE:
cp = &prinfo->active; break;
case AUDIO__WAITING:
cp = &prinfo->waiting; break;
case AUDIO__GAIN:
up = &prinfo->gain; break;
case AUDIO__MONGAIN:
up = &info.monitor_gain; break;
case AUDIO__BALANCE:
cp = &prinfo->balance; break;
default:
return (AUDIO_ERR_BADARG);
}
if (which & AUDIO__SET) {
/* Init so that only the value of interest is changed */
AUDIO_INITINFO(&info);
if (up != NULL) {
*up = *valp;
} else {
*cp = (unsigned char) *valp;
}
err = audio_setinfo(fd, &info);
} else {
err = audio_getinfo(fd, &info);
}
if (err == AUDIO_SUCCESS) {
if (up != NULL)
*valp = *up;
else
*valp = (unsigned)*cp;
}
return (err);
}
/*
* Get/set gain value.
* NOTE: legal values are floating-point double 0. - 1.
*/
int
audio__setgain(int fd, double *valp, unsigned int which)
{
int err;
unsigned x;
if (which & AUDIO__SET) {
if ((*valp < 0.) || (*valp > 1.))
return (AUDIO_ERR_BADARG);
/* Map value into legal range */
x = ((unsigned)(*valp * (AUDIO_MAX_GAIN - AUDIO_MIN_GAIN))) +
AUDIO_MIN_GAIN;
}
/* Get or set the new value */
err = audio__setval(fd, &x, which);
if (err == AUDIO_SUCCESS) {
/* Map value back to double */
*valp = ((double)(x - AUDIO_MIN_GAIN) /
(AUDIO_MAX_GAIN - AUDIO_MIN_GAIN));
}
return (err);
}
/*
* Set Pause/resume flags.
* Can set play and record individually or together.
*/
int
audio__setpause(int fd, unsigned int which)
{
Audio_info info;
int err;
unsigned char x;
AUDIO_INITINFO(&info);
if ((which & AUDIO__SETVAL_MASK) == AUDIO__PAUSE) {
x = TRUE;
} else if ((which & AUDIO__SETVAL_MASK) == AUDIO__RESUME) {
x = FALSE;
} else {
return (AUDIO_ERR_BADARG);
}
if (which & AUDIO__PLAY) {
info.play.pause = x;
}
if (which & AUDIO__RECORD) {
info.record.pause = x;
}
/* Set the new value */
err = audio_setinfo(fd, &info);
/* Check to see if this took effect */
if (err == AUDIO_SUCCESS) {
if (((which & AUDIO__PLAY) && (info.play.pause != x)) ||
((which & AUDIO__RECORD) && (info.record.pause != x)))
return (AUDIO_ERR_NOEFFECT);
}
return (err);
}
/*
* Flush play and/or record buffers.
*/
int
audio__flush(int fd, unsigned int which)
{
int flag;
flag = (which & AUDIO__PLAY) ? FLUSHW : 0;
flag |= (which & AUDIO__RECORD) ? FLUSHR : 0;
return ((ioctl(fd, I_FLUSH, flag) < 0) ?
AUDIO_UNIXERROR : AUDIO_SUCCESS);
}
/*
* Wait synchronously for output buffers to finish playing.
* If 'sig' is TRUE, signals may interrupt the drain.
*/
int
audio_drain(int fd, int sig)
{
while (ioctl(fd, AUDIO_DRAIN, 0) < 0) {
if (errno != EINTR) {
return (AUDIO_UNIXERROR);
}
if (sig) {
return (AUDIO_ERR_INTERRUPTED);
}
}
return (AUDIO_SUCCESS);
}
/*
* Write an EOF marker to the output audio stream.
*/
int
audio_play_eof(int fd)
{
return (write(fd, (char *)NULL, 0) < 0 ?
AUDIO_UNIXERROR : AUDIO_SUCCESS);
}
|