/* Open the audio device and initalize it. */ static void open_audio(void) { int err; double vol; /* Return if already open */ if (Audio_fd >= 0) return; /* Try opening without waiting, first */ Audio_fd = open(Audio_dev, O_WRONLY | O_NONBLOCK); if ((Audio_fd < 0) && (errno == EBUSY)) { if (Immediate) { Error(stderr, MGET("%s: %s is busy\n"), prog, Audio_dev); exit(1); } if (Verbose) { Error(stderr, MGET("%s: waiting for %s..."), prog, Audio_dev); (void) fflush(stderr); } /* Now hang until it's open */ Audio_fd = open(Audio_dev, O_WRONLY); if (Verbose) Error(stderr, (Audio_fd < 0) ? "\n" : MGET("open\n")); } if (Audio_fd < 0) { Error(stderr, MGET("%s: error opening "), prog); perror(Audio_dev); exit(1); } /* Clear the non-blocking flag (in System V it persists after open) */ (void) fcntl(Audio_fd, F_SETFL, (fcntl(Audio_fd, F_GETFL, 0) & ~(O_NDELAY | O_NONBLOCK))); /* Get the device output encoding configuration */ if (audio_get_play_config(Audio_fd, &Dev_hdr) != AUDIO_SUCCESS) { Error(stderr, MGET("%s: %s is not an audio device\n"), prog, Audio_dev); exit(1); } /* If -v flag, set the output volume now */ if (Volume != INT_MAX) { vol = (double)Volume / (double)MAX_GAIN; (void) audio_get_play_gain(Audio_fd, &Savevol); err = audio_set_play_gain(Audio_fd, &vol); if (err != AUDIO_SUCCESS) { Error(stderr, MGET("%s: could not set output volume for %s\n"), prog, Audio_dev); exit(1); } } }
static int init_device (int volume, unsigned char *data, int fd, unsigned int *header_length) { #ifdef SUNOS4_0_3 if (header_length) *header_length = 0; return 0; #else Audio_hdr file_hdr; reset_volume_p = 0; reset_device_p = 0; if (data && fd) ABORT (); /* one or the other */ if (AUDIO_SUCCESS != audio_get_play_config (audio_fd, &dev_hdr)) { perror ("Not a valid audio device"); return 1; } if (AUDIO_SUCCESS != (data ? audio_decode_filehdr (data, &file_hdr, header_length) : audio_read_filehdr (fd, &file_hdr, 0, 0))) { if (data) perror ("invalid audio data"); else perror ("invalid audio file"); return 1; } audio_flush_play (audio_fd); if (!initialized_device_p || (0 != audio_cmp_hdr (&dev_hdr, &file_hdr))) { Audio_hdr new_hdr; new_hdr = file_hdr; reset_device_p = 1; initialized_device_p = 1; if (AUDIO_SUCCESS != audio_set_play_config (audio_fd, &new_hdr)) { char buf1 [100], buf2 [100], buf3 [250]; audio_enc_to_str (&file_hdr, buf1); audio_enc_to_str (&new_hdr, buf2); sprintf (buf3, "wanted %s, got %s", buf1, buf2); warn (buf3); return 1; } } if (volume < 0 || volume > 100) { char buf [255]; sprintf (buf, "volume must be between 0 and 100 (not %d)", volume); warn (buf); return 1; } { /* set the volume; scale it to 0.0 - 1.0 */ double V = (volume / 100.0); audio_get_play_gain (audio_fd, &old_volume); reset_volume_p = 1; audio_set_play_gain (audio_fd, &V); } return 0; #endif }