Changeset 2011 for rat/trunk/auddev_sgi.c
- Timestamp:
- 10/12/98 18:32:54 (15 years ago)
- Files:
-
- 1 modified
-
rat/trunk/auddev_sgi.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
rat/trunk/auddev_sgi.c
r1990 r2011 1 1 /* 2 * FILE: sgi.c2 * FILE: auddev_sgi.c 3 3 * PROGRAM: RAT 4 * AUTHOR : Isidor Kouvelas + Colin Perkins4 * AUTHORS: Isidor Kouvelas + Colin Perkins + Orion Hodson 5 5 * 6 6 * $Revision$ … … 41 41 */ 42 42 43 #include <assert.h> 44 43 45 #include "config_unix.h" 44 46 #include "config_win32.h" 45 #include "audio.h" 47 #include "auddev_sgi.h" 48 #include "debug.h" 46 49 47 50 #if defined(IRIX) 48 51 49 #define READ_UNIT 8050 52 #define QSIZE 16000 /* Two seconds for now... */ 51 53 #define AVG_SIZE 20000 … … 53 55 #define BASEOFF 0x0a7f 54 56 55 #define bat_to_device(x) ((x) * 255 / MAX_AMP) 56 #define device_to_bat(x) ((x) * MAX_AMP / 255) 57 58 static ALport rp, wp; /* Read and write ports */ 57 #define RAT_TO_SGI_DEVICE(x) ((x) * 255 / MAX_AMP) 58 #define SGI_DEVICE_TO_RAT(x) ((x) * MAX_AMP / 255) 59 60 static int audio_fd = -1; 61 static ALport rp = NULL, wp = NULL; /* Read and write ports */ 59 62 static int iport = AUDIO_MICROPHONE; 60 63 static audio_format format; 61 64 /* 62 65 * Try to open the audio device. … … 64 67 */ 65 68 int 66 audio_open(audio_format format) 67 { 68 int audio_fd = -1; 69 sgi_audio_open(audio_desc_t ad, audio_format* fmt) 70 { 69 71 ALconfig c; 70 72 long cmd[8]; 73 74 if (audio_fd != -1) { 75 sgi_audio_close(ad); 76 } 77 78 memcpy(&format, fmt, sizeof(audio_format)); 71 79 72 80 if ((c = ALnewconfig()) == NULL) { … … 75 83 } 76 84 77 ALsetchannels(c, AL_MONO); 85 switch(fmt->num_channels) { 86 case 1: 87 ALsetchannels(c, AL_MONO); break; 88 case 2: 89 ALsetchannels(c, AL_STEREO); break; 90 default: 91 sgi_audio_close(ad); 92 } 93 78 94 ALsetwidth(c, AL_SAMPLE_16); 79 95 ALsetqueuesize(c, QSIZE); 80 96 ALsetsampfmt(c, AL_SAMPFMT_TWOSCOMP); 81 if ((wp = ALopenport("RAT write", "w", c)) == NULL) 97 98 if ((wp = ALopenport("RAT write", "w", c)) == NULL) { 82 99 fprintf(stderr, "ALopenport (write) error\n"); 83 if ((rp = ALopenport("RAT read", "r", c)) == NULL) 100 sgi_audio_close(ad); 101 return FALSE; 102 } 103 104 if ((rp = ALopenport("RAT read", "r", c)) == NULL) { 84 105 fprintf(stderr, "ALopenport (read) error\n"); 106 sgi_audio_close(ad); 107 return FALSE; 108 } 85 109 86 110 cmd[0] = AL_OUTPUT_RATE; … … 92 116 cmd[6] = AL_MONITOR_CTL; 93 117 cmd[7] = AL_MONITOR_OFF; 94 if (ALsetparams(AL_DEFAULT_DEVICE, cmd, 8L) == -1) 118 119 if (ALsetparams(AL_DEFAULT_DEVICE, cmd, 8L) == -1) { 95 120 fprintf(stderr, "audio_open/ALsetparams error\n"); 121 sgi_audio_close(ad); 122 } 96 123 97 124 /* Get the file descriptor to use in select */ 98 125 audio_fd = ALgetfd(rp); 99 126 100 ALsetfillpoint(rp, READ_UNIT); 101 127 if (ALsetfillpoint(rp, format.blocksize) < 0) { 128 debug_msg("ALsetfillpoint failed (%d samples)\n", format.blocksize); 129 } 130 102 131 /* We probably should free the config here... */ 103 104 return audio_fd;132 133 return TRUE; 105 134 } 106 135 107 136 /* Close the audio device */ 108 137 void 109 audio_close(int audio_fd) 110 { 138 sgi_audio_close(audio_desc_t ad) 139 { 140 UNUSED(ad); 111 141 ALcloseport(rp); 112 142 ALcloseport(wp); 143 audio_fd = -1; 113 144 } 114 145 115 146 /* Flush input buffer */ 116 147 void 117 audio_drain(int audio_fd)148 sgi_audio_drain(audio_desc_t ad) 118 149 { 119 150 sample buf[QSIZE]; 120 while(audio_read(audio_fd, buf, QSIZE) == QSIZE); 151 152 UNUSED(ad); assert(audio_fd > 0); 153 154 while(sgi_audio_read(audio_fd, buf, QSIZE) == QSIZE); 121 155 } 122 156 … … 124 158 125 159 void 126 audio_set_gain(int audio_fd, int gain)160 sgi_audio_set_gain(audio_desc_t ad, int gain) 127 161 { 128 162 long cmd[4]; 129 163 130 if (audio_fd == 0) 131 return; 164 UNUSED(ad); assert(audio_fd > 0); 132 165 133 166 cmd[0] = AL_LEFT_INPUT_ATTEN; 134 cmd[1] = 255 - bat_to_device(gain);167 cmd[1] = 255 - RAT_TO_SGI_DEVICE(gain); 135 168 cmd[2] = AL_RIGHT_INPUT_ATTEN; 136 169 cmd[3] = cmd[1]; … … 139 172 140 173 int 141 audio_get_gain(int audio_fd)174 sgi_audio_get_gain(audio_desc_t ad) 142 175 { 143 176 long cmd[2]; 144 177 145 if (audio_fd == 0) 146 return 50; 178 UNUSED(ad); assert(audio_fd > 0); 147 179 148 180 cmd[0] = AL_LEFT_INPUT_ATTEN; 149 181 ALgetparams(AL_DEFAULT_DEVICE, cmd, 2L); 150 return (255 - device_to_bat(cmd[1]));151 } 152 153 void 154 audio_set_volume(int audio_fd, int vol)182 return (255 - SGI_DEVICE_TO_RAT(cmd[1])); 183 } 184 185 void 186 sgi_audio_set_volume(audio_desc_t ad, int vol) 155 187 { 156 188 long cmd[4]; 157 189 158 if (audio_fd == 0) 159 return; 190 UNUSED(ad); assert(audio_fd > 0); 160 191 161 192 cmd[0] = AL_LEFT_SPEAKER_GAIN; 162 cmd[1] = bat_to_device(vol);193 cmd[1] = RAT_TO_SGI_DEVICE(vol); 163 194 cmd[2] = AL_RIGHT_SPEAKER_GAIN; 164 195 cmd[3] = cmd[1]; … … 167 198 168 199 int 169 audio_get_volume(int audio_fd)200 sgi_audio_get_volume(audio_desc_t ad) 170 201 { 171 202 long cmd[2]; 172 203 173 if (audio_fd == 0) 174 return 50; 204 UNUSED(ad); assert(audio_fd > 0); 175 205 176 206 cmd[0] = AL_LEFT_SPEAKER_GAIN; 177 207 ALgetparams(AL_DEFAULT_DEVICE, cmd, 2L); 178 return ( device_to_bat(cmd[1]));208 return (SGI_DEVICE_TO_RAT(cmd[1])); 179 209 } 180 210 … … 182 212 183 213 int 184 audio_read(int audio_fd, sample *buf, int samples)214 sgi_audio_read(audio_desc_t ad, sample *buf, int samples) 185 215 { 186 216 long len; 217 218 UNUSED(ad); assert(audio_fd > 0); 187 219 188 220 if (non_block) { 189 221 if ((len = ALgetfilled(rp)) <= 0) 190 222 return (0); 191 len -= len % READ_UNIT;223 len -= len % format.blocksize; 192 224 if (len <= 0) 193 len = READ_UNIT;225 len = format.blocksize; 194 226 if (len > samples) 195 227 len = samples; … … 208 240 209 241 int 210 audio_write(int audio_fd, sample *buf, int samples) 211 { 242 sgi_audio_write(audio_desc_t ad, sample *buf, int samples) 243 { 244 UNUSED(ad); assert(audio_fd > 0); 245 212 246 if (samples > QSIZE) { 213 247 fprintf(stderr, "audio_write: too big!\n"); … … 222 256 /* Set ops on audio device to be non-blocking */ 223 257 void 224 audio_non_block(int audio_fd) 225 { 258 sgi_audio_non_block(audio_desc_t ad) 259 { 260 UNUSED(ad); assert(audio_fd > 0); 261 226 262 non_block = 1; 227 263 } … … 229 265 /* Set ops on audio device to block */ 230 266 void 231 audio_block(int audio_fd) 232 { 267 sgi_audio_block(audio_desc_t ad) 268 { 269 UNUSED(ad); assert(audio_fd > 0); 270 233 271 non_block = 0; 234 272 } 235 273 236 274 void 237 audio_set_oport(int audio_fd, int port) 238 { 239 /* Not possible? */ 240 } 241 242 int 243 audio_get_oport(int audio_fd) 244 { 275 sgi_audio_set_oport(audio_desc_t ad, int port) 276 { 277 UNUSED(ad); assert(audio_fd > 0); 278 } 279 280 int 281 sgi_audio_get_oport(audio_desc_t ad) 282 { 283 UNUSED(ad); assert(audio_fd > 0); 284 245 285 return (AUDIO_SPEAKER); 246 286 } 247 287 248 288 int 249 audio_next_oport(int audio_fd) 250 { 289 sgi_audio_next_oport(audio_desc_t ad) 290 { 291 UNUSED(ad); assert(audio_fd > 0); 292 251 293 return (AUDIO_SPEAKER); 252 294 } 253 295 254 296 void 255 audio_set_iport(int audio_fd, int port)297 sgi_audio_set_iport(audio_desc_t ad, int port) 256 298 { 257 299 long pvbuf[2]; 258 300 259 if (audio_fd > 0) { 260 switch(port) { 261 case AUDIO_MICROPHONE : pvbuf[0] = AL_INPUT_SOURCE; 262 pvbuf[1] = AL_INPUT_MIC; 263 ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2); 264 iport = AUDIO_MICROPHONE; 265 break; 266 case AUDIO_LINE_IN : pvbuf[0] = AL_INPUT_SOURCE; 267 pvbuf[1] = AL_INPUT_LINE; 268 ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2); 269 iport = AUDIO_LINE_IN; 270 break; 271 default : printf("Illegal input port!\n"); 272 abort(); 273 } 274 } 275 } 276 277 int 278 audio_get_iport(int audio_fd) 279 { 301 UNUSED(ad); assert(audio_fd > 0); 302 303 switch(port) { 304 case AUDIO_MICROPHONE: 305 pvbuf[0] = AL_INPUT_SOURCE; 306 pvbuf[1] = AL_INPUT_MIC; 307 ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2); 308 iport = AUDIO_MICROPHONE; 309 break; 310 case AUDIO_LINE_IN: 311 pvbuf[0] = AL_INPUT_SOURCE; 312 pvbuf[1] = AL_INPUT_LINE; 313 ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2); 314 iport = AUDIO_LINE_IN; 315 break; 316 default: 317 printf("Illegal input port!\n"); 318 abort(); 319 } 320 } 321 322 int 323 sgi_audio_get_iport(audio_desc_t ad) 324 { 325 UNUSED(ad); assert(audio_fd > 0); 326 280 327 return iport; 281 328 } 282 329 283 330 int 284 audio_next_iport(int audio_fd) 285 { 286 switch (iport) { 287 case AUDIO_MICROPHONE : audio_set_iport(audio_fd, AUDIO_LINE_IN); 288 break; 289 case AUDIO_LINE_IN : audio_set_iport(audio_fd, AUDIO_MICROPHONE); 290 break; 291 default : printf("Unknown audio source!\n"); 292 } 293 return iport; 294 } 295 296 void 297 audio_switch_out(int audio_fd, struct s_cushion_struct *ap) 298 { 299 /* Full duplex device: do nothing! */ 300 } 301 302 void 303 audio_switch_in(int audio_fd) 304 { 305 /* Full duplex device: do nothing! */ 306 } 307 308 int 309 audio_duplex(int audio_fd) 310 { 311 return 1; 312 } 313 314 int 315 audio_get_channels(void) 316 { 331 sgi_audio_next_iport(audio_desc_t ad) 332 { 333 UNUSED(ad); assert(audio_fd > 0); 334 335 switch (iport) { 336 case AUDIO_MICROPHONE: 337 sgi_audio_set_iport(ad, AUDIO_LINE_IN); 338 break; 339 case AUDIO_LINE_IN: 340 sgi_audio_set_iport(ad, AUDIO_MICROPHONE); 341 break; 342 default: 343 printf("Unknown audio source!\n"); 344 } 345 return iport; 346 } 347 348 void sgi_audio_loopback(audio_desc_t ad, int gain) 349 { 350 long pvbuf[4]; 351 int pvcnt; 352 353 UNUSED(ad); assert(audio_fd > 0); 354 355 pvcnt = 2; 356 pvbuf[0] = AL_MONITOR_CTL; 357 pvbuf[1] = AL_MONITOR_OFF; 358 359 if (gain) { 360 pvcnt = 6; 361 pvbuf[1] = AL_MONITOR_ON; 362 pvbuf[2] = AL_LEFT_MONITOR_ATTEN; 363 pvbuf[3] = 255 - RAT_TO_SGI_DEVICE(gain); 364 pvbuf[4] = AL_RIGHT_MONITOR_ATTEN; 365 pvbuf[5] = pvbuf[3]; 366 } 367 368 if (ALsetparams(AL_DEFAULT_DEVICE, pvbuf, pvcnt) != 0) { 369 debug_msg("loopback failed\n"); 370 } 371 } 372 373 int 374 sgi_audio_duplex(audio_desc_t ad) 375 { 376 UNUSED(ad); assert(audio_fd > 0); 377 return 1; 378 } 379 380 int 381 sgi_audio_get_channels(audio_desc_t ad) 382 { 383 UNUSED(ad); assert(audio_fd > 0); 317 384 return ALgetchannels(ALgetconfig(rp)); 318 385 } 319 386 387 int 388 sgi_audio_get_blocksize(audio_desc_t ad) 389 { 390 UNUSED(ad); assert(audio_fd > 0); 391 return format.blocksize; /* Could use ALgetfillpoint */ 392 } 393 394 int 395 sgi_audio_get_freq(audio_desc_t ad) 396 { 397 long pvbuf[2]; 398 UNUSED(ad); assert(audio_fd > 0); 399 400 pvbuf[0] = AL_INPUT_RATE; 401 402 if (ALqueryparams(AL_DEFAULT_DEVICE, pvbuf, 2) == -1) { 403 debug_msg("Could not get freq"); 404 } 405 return (int)pvbuf[1]; 406 } 407 408 int 409 sgi_audio_is_ready(audio_desc_t ad) 410 { 411 UNUSED(ad); assert(audio_fd > 0); 412 413 if (ALgetfilled(rp) >= format.blocksize) { 414 return TRUE; 415 } else { 416 return FALSE; 417 } 418 } 419 420 void 421 sgi_audio_wait_for(audio_desc_t ad, int delay_ms) 422 { 423 struct timeval tv; 424 fd_set rfds; 425 UNUSED(ad); assert(audio_fd > 0); 426 427 tv.tv_sec = 0; 428 tv.tv_usec = delay_ms * 1000; 429 430 FD_ZERO(&rfds); 431 FD_SET(audio_fd, &rfds); 432 433 select(audio_fd + 1, &rfds, NULL, NULL, &tv); 434 } 435 320 436 #endif 321 437
