1 | /* -*- mode: C; tab-width:8; c-basic-offset:8 -*- |
---|
2 | * vi:set ts=8: |
---|
3 | * |
---|
4 | * solaris_native.c |
---|
5 | * |
---|
6 | * functions related to the aquisition and management of the native |
---|
7 | * audio on Solaris. |
---|
8 | * |
---|
9 | * This is a second-cut implementation. Playback is well |
---|
10 | * supported. Still no recording. |
---|
11 | * Yotam Gingold, April 28, 2001 |
---|
12 | * <ygingold@cs.brown.edu> |
---|
13 | * <ygingold@stat.wvu.edu> |
---|
14 | * |
---|
15 | * |
---|
16 | * This is a crude first-cut implementation, which doesn't do any |
---|
17 | * sophisticated error handling at all yet. |
---|
18 | * John E. Stone, September 13, 2000 |
---|
19 | * <johns@megapixel.com> |
---|
20 | * <j.stone@acm.org> |
---|
21 | */ |
---|
22 | #include <AL/al.h> |
---|
23 | |
---|
24 | #include <assert.h> |
---|
25 | #include <stdio.h> |
---|
26 | #include <stdlib.h> |
---|
27 | |
---|
28 | #include "backends/alc_backend.h" |
---|
29 | |
---|
30 | #include "al_main.h" |
---|
31 | #include "al_debug.h" |
---|
32 | #include "alc/alc_context.h" |
---|
33 | #include "audioconvert/ac_endian.h" |
---|
34 | |
---|
35 | |
---|
36 | #include <unistd.h> |
---|
37 | #include <sys/types.h> |
---|
38 | #include <sys/stat.h> |
---|
39 | #include <fcntl.h> |
---|
40 | #include <sys/audioio.h> /* Sun's audio system headers */ |
---|
41 | |
---|
42 | typedef struct { |
---|
43 | int fd; /* file descriptor of audio device */ |
---|
44 | audio_info_t ainfo; /* audio info structure used in ioctl calls */ |
---|
45 | } solaris_audio; |
---|
46 | |
---|
47 | |
---|
48 | #define EMAIL_ADDRESS "ygingold@stat.wvu.edu" |
---|
49 | #define EMAIL_ADDRESS2 "j.stone@acm.org" |
---|
50 | |
---|
51 | |
---|
52 | static const char *implement_me(const char *fn) { |
---|
53 | static char retval[2048]; |
---|
54 | |
---|
55 | snprintf(retval, sizeof(retval), |
---|
56 | "%s is not implemented under Solaris. Please contact %s for\n" |
---|
57 | "information on how you can help get %s implemented on Solaris.\n", |
---|
58 | fn, EMAIL_ADDRESS, fn); |
---|
59 | |
---|
60 | return retval; |
---|
61 | } |
---|
62 | |
---|
63 | static void *grab_read_native(void) { |
---|
64 | return NULL ; |
---|
65 | } |
---|
66 | |
---|
67 | static void *grab_write_native(void) { |
---|
68 | int fd ; |
---|
69 | solaris_audio* saudio ; |
---|
70 | fprintf(stderr, "solaris_native: opening /dev/audio\n"); |
---|
71 | |
---|
72 | fd = open("/dev/audio", O_WRONLY | O_NONBLOCK ); |
---|
73 | if (fd < 0) { |
---|
74 | perror("open /dev/audio"); |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | |
---|
78 | if(fcntl(fd, F_SETFL, ~O_NONBLOCK) == -1) { |
---|
79 | perror("fcntl"); |
---|
80 | fprintf(stderr,"fnctl error \n"); |
---|
81 | } |
---|
82 | |
---|
83 | fprintf(stderr, "Opened /dev/audio successfully\n"); |
---|
84 | |
---|
85 | saudio = (solaris_audio*) malloc( sizeof( solaris_audio ) ) ; |
---|
86 | if( saudio == NULL ) { |
---|
87 | close( fd ) ; |
---|
88 | return NULL ; |
---|
89 | } |
---|
90 | saudio -> fd = fd ; |
---|
91 | AUDIO_INITINFO( &(saudio->ainfo) ) ; |
---|
92 | |
---|
93 | return saudio ; |
---|
94 | } |
---|
95 | |
---|
96 | void * |
---|
97 | alcBackendOpenNative_( ALC_OpenMode mode ) |
---|
98 | { |
---|
99 | return mode == ALC_OPEN_INPUT_ ? grab_read_native() : grab_write_native(); |
---|
100 | } |
---|
101 | |
---|
102 | void native_blitbuffer(void *handle, |
---|
103 | void *dataptr, |
---|
104 | int bytes_to_write) { |
---|
105 | solaris_audio* sa ; |
---|
106 | |
---|
107 | /* arch/bsd graft */ |
---|
108 | struct timeval tv = { 1, 0 }; /* wait 1 sec max */ |
---|
109 | int iterator = 0; |
---|
110 | int err; |
---|
111 | fd_set sa_fd_set ; |
---|
112 | |
---|
113 | |
---|
114 | /*fprintf(stderr, "Writing to audio device bytes_to_write{ %d }...\n", bytes_to_write );*/ |
---|
115 | |
---|
116 | if( handle == NULL ) |
---|
117 | return ; |
---|
118 | sa = (solaris_audio*) handle ; |
---|
119 | if( sa->fd == NULL ) |
---|
120 | return ; |
---|
121 | |
---|
122 | /* This is the original write. */ |
---|
123 | /* write(sa->fd, (char *) dataptr, bytes_to_write);*/ |
---|
124 | |
---|
125 | /* This is the write() adapted from arch/bsd */ |
---|
126 | |
---|
127 | FD_SET(sa->fd, &sa_fd_set); |
---|
128 | |
---|
129 | for(iterator = bytes_to_write; iterator > 0; ) { |
---|
130 | if(select(sa->fd + 1, NULL, &sa_fd_set, NULL, &tv) == 0) { |
---|
131 | /* timeout occured, don't try and write */ |
---|
132 | #ifdef DEBUG_MAXIMUS |
---|
133 | fprintf(stderr, "native_blitbuffer: timeout occured\n"); |
---|
134 | #endif |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | FD_ZERO(&sa_fd_set); |
---|
139 | FD_SET(sa->fd, &sa_fd_set); |
---|
140 | |
---|
141 | assert(iterator > 0); |
---|
142 | assert(iterator <= bytes_to_write); |
---|
143 | |
---|
144 | err = write(sa->fd, |
---|
145 | (char *) dataptr + bytes_to_write - iterator, |
---|
146 | iterator); |
---|
147 | if(err < 0) { |
---|
148 | #ifdef DEBUG_MAXIMUS |
---|
149 | perror("write"); |
---|
150 | #endif |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | iterator -= err; |
---|
155 | }; |
---|
156 | |
---|
157 | return; |
---|
158 | } |
---|
159 | |
---|
160 | void release_native(void *handle) { |
---|
161 | solaris_audio* sa ; |
---|
162 | |
---|
163 | fprintf(stderr, "Closing audio device...\n"); |
---|
164 | |
---|
165 | if( handle == NULL ) |
---|
166 | return ; |
---|
167 | sa = (solaris_audio*) handle ; |
---|
168 | if( sa->fd == NULL ) |
---|
169 | return ; |
---|
170 | |
---|
171 | close(sa->fd); |
---|
172 | |
---|
173 | free( sa ) ; |
---|
174 | |
---|
175 | return; |
---|
176 | } |
---|
177 | |
---|
178 | int set_nativechannel(UNUSED(void *handle), |
---|
179 | UNUSED(ALuint channel), |
---|
180 | UNUSED(ALfloat volume)) { |
---|
181 | fprintf(stderr, implement_me("set_nativechannel")); |
---|
182 | |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | void pause_nativedevice(UNUSED(void *handle)) { |
---|
187 | fprintf(stderr, implement_me("pause_nativedevice")); |
---|
188 | |
---|
189 | return; |
---|
190 | } |
---|
191 | |
---|
192 | void resume_nativedevice(UNUSED(void *handle)) { |
---|
193 | fprintf(stderr, implement_me("resume_nativedevice")); |
---|
194 | |
---|
195 | return; |
---|
196 | } |
---|
197 | |
---|
198 | ALfloat get_nativechannel(UNUSED(void *handle), UNUSED(ALuint channel)) { |
---|
199 | fprintf(stderr, implement_me("get_nativechannel")); |
---|
200 | |
---|
201 | return 0.0; |
---|
202 | } |
---|
203 | |
---|
204 | ALsizei capture_nativedevice(UNUSED(void *handle), |
---|
205 | UNUSED(void *capture_buffer), |
---|
206 | UNUSED(int bufsiz)) { |
---|
207 | return 0; /* unimplemented */ |
---|
208 | } |
---|
209 | |
---|
210 | static ALboolean set_write_native(void *handle, |
---|
211 | unsigned int *bufsiz, |
---|
212 | ALenum *fmt, |
---|
213 | unsigned int *speed) { |
---|
214 | solaris_audio* sa ; |
---|
215 | |
---|
216 | ALuint channels = _alGetChannelsFromFormat(*fmt); |
---|
217 | |
---|
218 | if( handle == NULL ) |
---|
219 | return AL_FALSE ; |
---|
220 | sa = (solaris_audio*) handle ; |
---|
221 | |
---|
222 | if( sa->fd == NULL ) |
---|
223 | return AL_FALSE ; |
---|
224 | |
---|
225 | AUDIO_INITINFO(&(sa->ainfo)); |
---|
226 | /*if( ioctl(gaudio.fd, AUDIO_GETINFO, &(gaudio.ainfo)) < 0 ) |
---|
227 | return AL_FALSE ; */ |
---|
228 | |
---|
229 | fprintf(stderr, "Setting audio device...\n"); |
---|
230 | sa->ainfo.play.sample_rate = *speed; |
---|
231 | sa->ainfo.play.channels = channels; |
---|
232 | /*fprintf(stderr, "solaris: set_write_native speed{ %d }, channels{ %d }, format{ %d }, buffer_size{ %u } \n", *speed, channels, *fmt, *bufsiz ) ;*/ |
---|
233 | switch (*fmt) { |
---|
234 | case AL_FORMAT_MONO8: |
---|
235 | case AL_FORMAT_STEREO8: |
---|
236 | /*fprintf(stderr, "Setting Mono8/Stereo8... \n");*/ |
---|
237 | sa->ainfo.play.precision = 8; |
---|
238 | sa->ainfo.play.encoding = AUDIO_ENCODING_LINEAR8; |
---|
239 | break; |
---|
240 | case AL_FORMAT_MONO16: |
---|
241 | case AL_FORMAT_STEREO16: |
---|
242 | sa->ainfo.play.precision = 16; |
---|
243 | /*#ifdef WORDS_BIGENDIAN*/ |
---|
244 | /*fprintf( stderr, "WORDS BIGENDIAN" ) ;*/ |
---|
245 | sa->ainfo.play.encoding = AUDIO_ENCODING_LINEAR; |
---|
246 | /*#else |
---|
247 | fprintf( stderr, "WORDS LITTLEENDIAN" ) ; |
---|
248 | sa->ainfo.play.encoding = AUDIO_ENCODING_LINEAR;*/ |
---|
249 | /* #endif*/ /* WORDS_BIGENDIAN */ |
---|
250 | break; |
---|
251 | |
---|
252 | default: |
---|
253 | fprintf(stderr, "Unsuported audio format:%d\n", *fmt); |
---|
254 | return AL_FALSE; |
---|
255 | } |
---|
256 | |
---|
257 | sa->ainfo.play.buffer_size = *bufsiz; |
---|
258 | |
---|
259 | /*if (ioctl(gaudio.fd, AUDIO_SETINFO, &gaudio.ainfo) < 0)*/ |
---|
260 | if (ioctl(sa->fd, AUDIO_SETINFO, &(sa->ainfo)) < 0) |
---|
261 | return AL_FALSE; |
---|
262 | else |
---|
263 | return AL_TRUE; |
---|
264 | } |
---|
265 | |
---|
266 | static ALboolean set_read_native(UNUSED(void *handle), |
---|
267 | UNUSED(unsigned int *bufsiz), |
---|
268 | UNUSED(ALenum *fmt), |
---|
269 | UNUSED(unsigned int *speed)) { |
---|
270 | return AL_FALSE; |
---|
271 | } |
---|
272 | |
---|
273 | ALboolean |
---|
274 | alcBackendSetAttributesNative_(ALC_OpenMode mode, void *handle, ALuint *bufsiz, ALenum *fmt, ALuint *speed) |
---|
275 | { |
---|
276 | return mode == ALC_OPEN_INPUT_ ? |
---|
277 | set_read_native(handle, bufsiz, fmt, speed) : |
---|
278 | set_write_native(handle, bufsiz, fmt, speed); |
---|
279 | } |
---|