xref: /netbsd-src/usr.bin/audio/common/sun.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: sun.c,v 1.6 2008/05/29 14:51:27 mrg 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.6 2008/05/29 14:51:27 mrg 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 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(sun_encoding, encp, precp)
84 	int	sun_encoding;
85 	u_int	*encp;
86 	u_int	*precp;
87 {
88 	int i;
89 
90 	for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
91 		if (file2sw_encodings[i].file_encoding == sun_encoding) {
92 			*precp = file2sw_encodings[i].precision;
93 			*encp = file2sw_encodings[i].encoding;
94 			return (0);
95 		}
96 	return (1);
97 }
98 
99 int
100 audio_encoding_to_sun(encoding, precision, sunep)
101 	int	encoding;
102 	int	precision;
103 	int	*sunep;
104 {
105 	int i;
106 
107 	for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
108 		if (file2sw_encodings[i].encoding == encoding &&
109 		    file2sw_encodings[i].precision == precision) {
110 			*sunep = file2sw_encodings[i].file_encoding;
111 			return (0);
112 		}
113 	return (1);
114 }
115