- 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>=PSND_MAX_VOLUME/10)
- playlist[i].volume-=PSND_MAX_VOLUME/20;
-
- /* 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 <= PSND_MAX_VOLUME/10)
- {
- 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);
- }
- }