- max_sample_size = fragment_size / ((stereo ? 2 : 1) * sizeof(short));
- }
-
- 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(short));
-
- 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;
- }
-
- /* expand sample from 8 to 16 bit */
- for(j=0; j<sample_size; j++)
- premix_second_buffer[j] = premix_first_buffer[j] << 8;
-
- /* 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_second_buffer[j] =
- (playlist[i].volume * (int)premix_second_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 * premix_second_buffer[j])
- >> PSND_MAX_LEFT2RIGHT_BITS;
- premix_right_buffer[j] =
- (right_volume * premix_second_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] += premix_second_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] < -65535)
- playing_buffer[i] = -32767;
- else if (premix_last_buffer[i] > 65535)
- playing_buffer[i] = 32767;
- else
- playing_buffer[i] = (short)(premix_last_buffer[i] >> 1);
- }
-
- /* 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);
- }
- }