X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=ac814e90f30a6fbcae9e2daea299796a9285037f;hb=71a1616f11627ab005c19fc6fc06ba23f6b4b633;hp=00019c6e3e5cc12a1c10e4c3be03ef2c6e692bb2;hpb=1085cba858dc9cccaa5da434ddb990de23fffdfc;p=rocksndiamonds.git diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 00019c6e..ac814e90 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -54,20 +54,13 @@ static struct SoundControl mixer[NUM_MIXER_CHANNELS]; static int mixer_active_channels = 0; /* forward declaration of internal functions */ -static void Mixer_Main(void); -static void Mixer_InsertNewSound(struct SoundControl); static void InitAudioDevice(struct AudioFormatInfo *); +static void Mixer_Main(void); -#if defined(PLATFORM_UNIX) -#if !defined(AUDIO_STREAMING_DSP) +#if defined(PLATFORM_UNIX) && !defined(AUDIO_STREAMING_DSP) static unsigned char linear_to_ulaw(int); static int ulaw_to_linear(unsigned char); #endif -#elif defined(PLATFORM_MSDOS) -static void Mixer_InsertNewSound(struct SoundControl); -static void Mixer_StopSound(struct SoundControl); -static void Mixer_StopAllSounds(); -#endif static void ReloadCustomSounds(); static void ReloadCustomMusic(); @@ -186,6 +179,29 @@ void UnixCloseAudio(void) kill(audio.mixer_pid, SIGTERM); } +static void WriteSoundControlToPipe(struct SoundControl snd_ctrl) +{ + if (audio.mixer_pid == 0) /* we are child process */ + return; + + if (write(audio.mixer_pipe[1], &snd_ctrl, sizeof(struct SoundControl)) < 0) + { + Error(ERR_WARN, "cannot pipe to child process -- no sounds"); + audio.sound_available = audio.sound_enabled = FALSE; + return; + } +} + +static void ReadSoundControlFromPipe(struct SoundControl *snd_ctrl) +{ + if (audio.mixer_pid != 0) /* we are parent process */ + return; + + if (read(audio.mixer_pipe[0], snd_ctrl, sizeof(struct SoundControl)) + != sizeof(struct SoundControl)) + Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds"); +} + static void WriteReloadInfoToPipe(char *set_name, int type) { struct SoundControl snd_ctrl; @@ -298,9 +314,40 @@ void Mixer_InitChannels() mixer_active_channels = 0; } -static void Mixer_FadeSound(int channel) +static void Mixer_PlayChannel(int channel) +{ +#if defined(PLATFORM_MSDOS) + mixer[channel].voice = allocate_voice((SAMPLE *)mixer[channel].data_ptr); + + if (mixer[channel].voice < 0) + return; + + if (IS_LOOP(mixer[channel])) + voice_set_playmode(mixer[channel].voice, PLAYMODE_LOOP); + + voice_set_volume(mixer[channel].voice, snd_ctrl.volume); + voice_set_pan(mixer[channel].voice, snd_ctrl.stereo); + voice_start(mixer[channel].voice); +#endif +} + +static void Mixer_StopChannel(int channel) +{ +#if defined(PLATFORM_MSDOS) + voice_set_volume(mixer[channel].voice, 0); + deallocate_voice(mixer[channel].voice); +#endif +} + +static void Mixer_FadeChannel(int channel) { mixer[channel].state |= SND_CTRL_FADE; + +#if defined(PLATFORM_MSDOS) + if (voice_check(mixer[channel].voice)) + voice_ramp_volume(mixer[channel].voice, 1000, 0); + mixer[channel].state &= ~SND_CTRL_IS_LOOP; +#endif } static void Mixer_RemoveSound(int channel) @@ -312,6 +359,8 @@ static void Mixer_RemoveSound(int channel) printf("REMOVING MIXER SOUND %d\n", channel); #endif + Mixer_StopChannel(channel); + mixer[channel].active = FALSE; mixer_active_channels--; } @@ -377,10 +426,7 @@ static void Mixer_InsertSound(struct SoundControl snd_ctrl) longest_nr = i; } } -#if defined(PLATFORM_MSDOS) - voice_set_volume(mixer[longest_nr].voice, 0); - deallocate_voice(mixer[longest_nr].voice); -#endif + Mixer_RemoveSound(longest_nr); } @@ -434,10 +480,6 @@ static void Mixer_InsertSound(struct SoundControl snd_ctrl) } } -#if defined(PLATFORM_MSDOS) - voice_set_volume(mixer[longest_nr].voice, 0); - deallocate_voice(mixer[longest_nr].voice); -#endif Mixer_RemoveSound(longest_nr); } @@ -487,16 +529,8 @@ static void Mixer_InsertSound(struct SoundControl snd_ctrl) printf("NEW SOUND %d ADDED TO MIXER\n", snd_ctrl.nr); #endif -#if defined(PLATFORM_MSDOS) - mixer[i].voice = allocate_voice((SAMPLE *)mixer[i].data_ptr); - - if (snd_ctrl.loop) - voice_set_playmode(mixer[i].voice, PLAYMODE_LOOP); + Mixer_PlayChannel(i); - voice_set_volume(mixer[i].voice, snd_ctrl.volume); - voice_set_pan(mixer[i].voice, snd_ctrl.stereo); - voice_start(mixer[i].voice); -#endif break; } } @@ -506,6 +540,20 @@ static void HandleSoundRequest(struct SoundControl snd_ctrl) { int i; +#if defined(PLATFORM_MSDOS) + for (i=0; idata_ptr, (state & SND_CTRL_LOOP ? -1 : 0)); } #elif defined(PLATFORM_UNIX) - if (audio.mixer_pid == 0) /* we are child process */ - return; - if (write(audio.mixer_pipe[1], &snd_ctrl, sizeof(snd_ctrl)) < 0) - { - Error(ERR_WARN, "cannot pipe to child process -- no sounds"); - audio.sound_available = audio.sound_enabled = FALSE; - return; - } + WriteSoundControlToPipe(snd_ctrl); + #elif defined(PLATFORM_MSDOS) - sound_handler(snd_ctrl); + + HandleSoundRequest(snd_ctrl); + #endif } @@ -1779,17 +1688,12 @@ void StopSoundExt(int nr, int state) #elif !defined(PLATFORM_MSDOS) - if (audio.mixer_pid == 0) /* we are child process */ - return; + WriteSoundControlToPipe(snd_ctrl); - if (write(audio.mixer_pipe[1], &snd_ctrl, sizeof(snd_ctrl)) < 0) - { - Error(ERR_WARN, "cannot pipe to child process -- no sounds"); - audio.sound_available = audio.sound_enabled = FALSE; - return; - } #else /* PLATFORM_MSDOS */ - sound_handler(snd_ctrl); + + HandleSoundRequest(snd_ctrl); + #endif }