xref: /netbsd-src/usr.bin/audio/common/sun.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: sun.c,v 1.7 2011/08/28 01:17:47 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * XXX this is slightly icky in places...
31  */
32 #include <sys/cdefs.h>
33 
34 #ifndef lint
35 __RCSID("$NetBSD: sun.c,v 1.7 2011/08/28 01:17:47 joerg Exp $");
36 #endif
37 
38 
39 #include <sys/types.h>
40 #include <sys/audioio.h>
41 #include <sys/ioctl.h>
42 #include <sys/time.h>
43 
44 #include <ctype.h>
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "libaudio.h"
51 
52 /*
53  * SunOS/NeXT .au format helpers
54  */
55 static const struct {
56 	int	file_encoding;
57 	int	encoding;
58 	int	precision;
59 } file2sw_encodings[] = {
60 	{ AUDIO_FILE_ENCODING_MULAW_8,		AUDIO_ENCODING_ULAW,	8 },
61 	{ AUDIO_FILE_ENCODING_LINEAR_8,		AUDIO_ENCODING_SLINEAR_BE, 8 },
62 	{ AUDIO_FILE_ENCODING_LINEAR_16,	AUDIO_ENCODING_SLINEAR_BE, 16 },
63 	{ AUDIO_FILE_ENCODING_LINEAR_24,	AUDIO_ENCODING_SLINEAR_BE, 24 },
64 	{ AUDIO_FILE_ENCODING_LINEAR_32,	AUDIO_ENCODING_SLINEAR_BE, 32 },
65 #if 0
66 	/*
67 	 * we should make some of these available.  the, eg ultrasparc, port
68 	 * can use the VIS instructions (if available) do do some of these
69 	 * mpeg ones.
70 	 */
71 	{ AUDIO_FILE_ENCODING_FLOAT,		AUDIO_ENCODING_ULAW,	32 },
72 	{ AUDIO_FILE_ENCODING_DOUBLE,		AUDIO_ENCODING_ULAW,	64 },
73 	{ AUDIO_FILE_ENCODING_ADPCM_G721,	AUDIO_ENCODING_ULAW,	4 },
74 	{ AUDIO_FILE_ENCODING_ADPCM_G722,	AUDIO_ENCODING_ULAW,	0 },
75 	{ AUDIO_FILE_ENCODING_ADPCM_G723_3,	AUDIO_ENCODING_ULAW,	3 },
76 	{ AUDIO_FILE_ENCODING_ADPCM_G723_5,	AUDIO_ENCODING_ULAW,	5 },
77 #endif
78 	{ AUDIO_FILE_ENCODING_ALAW_8,		AUDIO_ENCODING_ALAW,	8 },
79 	{ -1, -1, -1 }
80 };
81 
82 int
83 audio_sun_to_encoding(int sun_encoding, u_int *encp, u_int *precp)
84 {
85 	int i;
86 
87 	for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
88 		if (file2sw_encodings[i].file_encoding == sun_encoding) {
89 			*precp = file2sw_encodings[i].precision;
90 			*encp = file2sw_encodings[i].encoding;
91 			return (0);
92 		}
93 	return (1);
94 }
95 
96 int
97 audio_encoding_to_sun(int encoding, int precision, int *sunep)
98 {
99 	int i;
100 
101 	for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
102 		if (file2sw_encodings[i].encoding == encoding &&
103 		    file2sw_encodings[i].precision == precision) {
104 			*sunep = file2sw_encodings[i].file_encoding;
105 			return (0);
106 		}
107 	return (1);
108 }
109