1 /*************************************************************************/
2 /*                                                                       */
3 /*                  Language Technologies Institute                      */
4 /*                     Carnegie Mellon University                        */
5 /*                        Copyright (c) 2001                             */
6 /*                        All Rights Reserved.                           */
7 /*                                                                       */
8 /*  Permission is hereby granted, free of charge, to use and distribute  */
9 /*  this software and its documentation without restriction, including   */
10 /*  without limitation the rights to use, copy, modify, merge, publish,  */
11 /*  distribute, sublicense, and/or sell copies of this work, and to      */
12 /*  permit persons to whom this work is furnished to do so, subject to   */
13 /*  the following conditions:                                            */
14 /*   1. The code must retain the above copyright notice, this list of    */
15 /*      conditions and the following disclaimer.                         */
16 /*   2. Any modifications must be clearly marked as such.                */
17 /*   3. Original authors' names are not deleted.                         */
18 /*   4. The authors' names are not used to endorse or promote products   */
19 /*      derived from this software without specific prior written        */
20 /*      permission.                                                      */
21 /*                                                                       */
22 /*  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         */
23 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
24 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
25 /*  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      */
26 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
27 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
28 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
29 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
30 /*  THIS SOFTWARE.                                                       */
31 /*                                                                       */
32 /*************************************************************************/
33 /*             Author:  Alan W Black (awb@cs.cmu.edu)                    */
34 /*               Date:  January 2001                                     */
35 /*************************************************************************/
36 /*                                                                       */
37 /*  Sun/Solaris audio support                                            */
38 /*                                                                       */
39 /*************************************************************************/
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <errno.h>
48 #include <sys/filio.h>
49 #include <sys/audioio.h>
50 #include "cst_string.h"
51 #include "cst_wave.h"
52 #include "cst_audio.h"
53 
54 static const char *sun_audio_device = "/dev/audio";
55 
audio_open_sun(int sps,int channels,cst_audiofmt fmt)56 cst_audiodev *audio_open_sun(int sps, int channels, cst_audiofmt fmt)
57 {
58     audio_info_t ainfo;
59     int fd;
60     cst_audiodev *ad;
61     char *audio_device;
62 
63     if ((fd = open(sun_audio_device,O_WRONLY)) < 0)
64     {
65         /* the device might be a SunRay, so get the AUDIODEV env var */
66         audio_device = getenv("AUDIODEV");
67 
68 	if (audio_device != NULL) {
69    	    if ((fd = open(audio_device,O_WRONLY)) < 0) {
70 	        cst_errmsg("sun_audio: failed to open audio device %s: %s\n",
71 			   audio_device, strerror(errno));
72 	    }
73 	} else {
74 	    cst_errmsg("sun_audio: failed to open audio device %s: %s\n",
75 		       sun_audio_device, strerror(errno));
76 	    cst_error();
77 	}
78     }
79     ioctl(fd,AUDIO_GETINFO,&ainfo);
80 
81     switch (fmt)
82     {
83     case CST_AUDIO_LINEAR16:
84 	ainfo.play.encoding = AUDIO_ENCODING_LINEAR;
85 	ainfo.play.precision = 16;
86 	break;
87     case CST_AUDIO_LINEAR8:
88 	ainfo.play.encoding = AUDIO_ENCODING_LINEAR;
89 	ainfo.play.precision = 8;
90 	break;
91     case CST_AUDIO_MULAW:
92 	ainfo.play.encoding = AUDIO_ENCODING_ULAW;
93 	ainfo.play.precision = 8;
94 	break;
95     }
96 
97     ainfo.play.channels = 1;
98     ainfo.play.sample_rate = sps;
99 
100     if (ioctl(fd,AUDIO_SETINFO,&ainfo) == -1)
101     {
102 	cst_errmsg("sun_audio: failed to set audio params: %s\n",
103 		   strerror(errno));
104 	close(fd);
105 	cst_error();
106     }
107 
108     ad = cst_alloc(cst_audiodev, 1);
109 
110     ad->sps = sps;
111     ad->real_sps = ainfo.play.sample_rate;
112 
113     ad->channels = channels;
114     ad->real_channels = ainfo.play.channels;
115 
116     ad->fmt = fmt;
117     if (ainfo.play.encoding == AUDIO_ENCODING_LINEAR)
118     {
119 	if (ainfo.play.precision == 16)
120 	    ad->real_fmt = CST_AUDIO_LINEAR16;
121 	else if (ainfo.play.precision == 8)
122 	    ad->real_fmt = CST_AUDIO_LINEAR8;
123 	else
124 	{
125 	    cst_errmsg("sun_audio: linear %d bit audio unsupported\n",
126 		       ainfo.play.precision);
127 	    close(fd);
128 	    cst_free(ad);
129 	    cst_error();
130 	}
131     }
132     else if (ainfo.play.encoding == AUDIO_ENCODING_ULAW)
133 	ad->real_fmt = CST_AUDIO_MULAW;
134     else
135     {
136 	    cst_errmsg("sun_audio: unsupported audio format (%d bit/encoding #%d)\n",
137 		       ainfo.play.precision, ainfo.play.encoding);
138 	    close(fd);
139 	    cst_free(ad);
140 	    cst_error();
141     }
142     ad->platform_data = (void *)fd;
143 
144     return ad;
145 }
146 
audio_close_sun(cst_audiodev * ad)147 int audio_close_sun(cst_audiodev *ad)
148 {
149     int rv;
150 
151     if (ad == NULL)
152 	    return 0;
153 
154     rv = close((int)ad->platform_data);
155     cst_free(ad);
156     return rv;
157 }
158 
audio_write_sun(cst_audiodev * ad,void * samples,int num_bytes)159 int audio_write_sun(cst_audiodev *ad, void *samples, int num_bytes)
160 {
161     return write((int)ad->platform_data,samples,num_bytes);
162 }
163 
audio_flush_sun(cst_audiodev * ad)164 int audio_flush_sun(cst_audiodev *ad)
165 {
166     return ioctl((int)ad->platform_data, AUDIO_DRAIN, 0);
167 }
168 
169 /* FIXME... */
audio_drain_sun(cst_audiodev * ad)170 int audio_drain_sun(cst_audiodev *ad)
171 {
172     return ioctl((int)ad->platform_data, AUDIO_DRAIN, 0);
173 }
174 
175