- if (playing_sounds || snd_ctrl.active)
- {
- struct timeval delay = { 0, 0 };
- byte *sample_ptr;
- long sample_size;
- static long max_sample_size = 0;
- static long fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
- int sample_rate = DEFAULT_AUDIO_SAMPLE_RATE;
- int stereo = TRUE;
- /* 'ioctl()' expects pointer to integer value for stereo flag
- (boolean is defined as 'char', which will not work here) */
-
- if (playing_sounds ||
- (audio.device_fd = OpenAudioDevice(audio.device_name)) >= 0)
- {
- if (!playing_sounds) /* we just opened the audio device */
- {
- unsigned long fragment_spec = 0;
-
- /* determine logarithm (log2) of the fragment size */
- for (fragment_spec=0; (1 << fragment_spec) < fragment_size;
- fragment_spec++);
-
- /* use two fragments (play one fragment, prepare the other);
- one fragment would result in interrupted audio output, more
- than two fragments would raise audio output latency to much */
- fragment_spec |= 0x00020000;
-
- /* Example for fragment specification:
- - 2 buffers / 512 bytes (giving 1/16 second resolution for 8 kHz)
- - (with stereo the effective buffer size will shrink to 256)
- => fragment_size = 0x00020009 */
-
- if (ioctl(audio.device_fd,SNDCTL_DSP_SETFRAGMENT,&fragment_spec) < 0)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot set fragment size of /dev/dsp - no sounds");
-
- /* try if we can use stereo sound */
- if (ioctl(audio.device_fd, SNDCTL_DSP_STEREO, &stereo) < 0)
- {
-#ifdef DEBUG
- static boolean reported = FALSE;
-
- if (!reported)
- {
- Error(ERR_RETURN, "cannot get stereo sound on /dev/dsp");
- reported = TRUE;
- }
-#endif
- stereo = FALSE;
- }
-
- if (ioctl(audio.device_fd, SNDCTL_DSP_SPEED, &sample_rate) < 0)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot set sample rate of /dev/dsp - no sounds");
-
- /* get the real fragmentation size; this should return 512 */
- if (ioctl(audio.device_fd, SNDCTL_DSP_GETBLKSIZE,&fragment_size) < 0)
- Error(ERR_EXIT_SOUND_SERVER,
- "cannot get fragment size of /dev/dsp - no sounds");
-
- max_sample_size = fragment_size / (stereo ? 2 : 1);
- }
-
- if (snd_ctrl.active) /* new sound has arrived */
- SoundServer_InsertNewSound(snd_ctrl);
-
- while(playing_sounds &&
- select(audio.soundserver_pipe[0] + 1,
- &sound_fdset, NULL, NULL, &delay) < 1)
- {
- FD_SET(audio.soundserver_pipe[0], &sound_fdset);
-
- /* first clear the last premixing buffer */
- memset(premix_last_buffer, 0, fragment_size * sizeof(int));
-
- for(i=0;i<MAX_SOUNDS_PLAYING;i++)
- {
- int j;
-
- if (!playlist[i].active)
- continue;
-
- /* get pointer and size of the actual sound sample */
- sample_ptr = playlist[i].data_ptr+playlist[i].playingpos;
- sample_size =
- MIN(max_sample_size,playlist[i].data_len-playlist[i].playingpos);
- playlist[i].playingpos += sample_size;
-
- /* fill the first mixing buffer with original sample */
- memcpy(premix_first_buffer,sample_ptr,sample_size);
-
- /* are we about to restart a looping sound? */
- if (playlist[i].loop && sample_size<max_sample_size)
- {
- playlist[i].playingpos = max_sample_size-sample_size;
- memcpy(premix_first_buffer+sample_size,
- playlist[i].data_ptr,max_sample_size-sample_size);
- sample_size = max_sample_size;
- }
-
- /* decrease volume if sound is fading out */
- if (playlist[i].fade_sound &&
- playlist[i].volume >= SOUND_FADING_VOLUME_THRESHOLD)
- playlist[i].volume -= SOUND_FADING_VOLUME_STEP;
-
- /* adjust volume of actual sound sample */
- if (playlist[i].volume != PSND_MAX_VOLUME)
- for(j=0;j<sample_size;j++)
- premix_first_buffer[j] =
- (playlist[i].volume * (int)premix_first_buffer[j])
- >> PSND_MAX_VOLUME_BITS;
-
- /* fill the last mixing buffer with stereo or mono sound */
- if (stereo)
- {
- int middle_pos = PSND_MAX_LEFT2RIGHT/2;
- int left_volume = stereo_volume[middle_pos+playlist[i].stereo];
- int right_volume = stereo_volume[middle_pos-playlist[i].stereo];
-
- for(j=0;j<sample_size;j++)
- {
- premix_left_buffer[j] =
- (left_volume * (int)premix_first_buffer[j])
- >> PSND_MAX_LEFT2RIGHT_BITS;
- premix_right_buffer[j] =
- (right_volume * (int)premix_first_buffer[j])
- >> PSND_MAX_LEFT2RIGHT_BITS;
- premix_last_buffer[2*j+0] += premix_left_buffer[j];
- premix_last_buffer[2*j+1] += premix_right_buffer[j];
- }
- }
- else
- {
- for(j=0;j<sample_size;j++)
- premix_last_buffer[j] += (int)premix_first_buffer[j];
- }
-
- /* delete completed sound entries from the playlist */
- if (playlist[i].playingpos >= playlist[i].data_len)
- {
- if (playlist[i].loop)
- playlist[i].playingpos = 0;
- else
- {
- playlist[i] = emptySoundControl;
- playing_sounds--;
- }
- }
- else if (playlist[i].volume <= SOUND_FADING_VOLUME_THRESHOLD)
- {
- playlist[i] = emptySoundControl;
- playing_sounds--;
- }
- }
-
- /* put last mixing buffer to final playing buffer */
- for(i=0; i<fragment_size; i++)
- {
- if (premix_last_buffer[i]<-255)
- playing_buffer[i] = 0;
- else if (premix_last_buffer[i]>255)
- playing_buffer[i] = 255;
- else
- playing_buffer[i] = (premix_last_buffer[i]>>1)^0x80;
- }
-
- /* finally play the sound fragment */
- write(audio.device_fd, playing_buffer, fragment_size);
- }
-
- /* if no sounds playing, free device for other sound programs */
- if (!playing_sounds)
- close(audio.device_fd);
- }
- }