16  Sound Mixer Core Implementation

This tour examines the sound mixer implementation in s_sound.c, covering: - Channel management for simultaneous sounds - Distance-based sound attenuation - Real-time parameter adjustment - Channel allocation and cleanup - Music playback control


channel_t defines one mixing channel. Each holds a sound, its origin, and an audio system handle. File: linuxdoom-1.10/s_sound.c, lines 93–104

Three constants control how sound volume changes with distance: - S_CLIPPING_DIST: Maximum hearing distance - S_CLOSE_DIST: Distance for full volume - S_ATTENUATOR: Controls volume falloff rate File: linuxdoom-1.10/s_sound.c, lines 53–64


16.1 3D Sound Mixing

In S_AdjustSoundParams, approx_dist computes an approximate Euclidean distance. Sounds beyond S_CLIPPING_DIST return 0 (silent). File: linuxdoom-1.10/s_sound.c, lines 767–775

Calculates stereo separation (0–255) based on listener-to-source angle. File: linuxdoom-1.10/s_sound.c, lines 792–794

Volume attenuates with distance, except when very close to the sound source. File: linuxdoom-1.10/s_sound.c, lines 796–804

S_AdjustSoundParams computes sound parameters based on listener and origin positions. If it returns 0, the sound is out of audible range and playback is skipped. File: linuxdoom-1.10/s_sound.c, lines 306–310

If the sound becomes inaudible, the channel is freed. Otherwise the new parameters are applied. File: linuxdoom-1.10/s_sound.c, lines 586–590


16.2 Channel Allocation

S_getChannel finds an unused channel or reuses one from the same origin. File: linuxdoom-1.10/s_sound.c, lines 837–842

If no channels have lower priority sounds, returns -1. File: linuxdoom-1.10/s_sound.c, lines 852–860

Stops any currently playing sound before freeing a channel. File: linuxdoom-1.10/s_sound.c, lines 716–724

Decrements the sound’s usefulness counter and frees the channel. File: linuxdoom-1.10/s_sound.c, lines 737–741


16.3 Initialization and Start

S_Init performs sound system initialization by setting up channels and configuring default volumes. File: linuxdoom-1.10/s_sound.c, lines 167–174

Allocates and initializes the sound channels array in permanent memory. File: linuxdoom-1.10/s_sound.c, lines 177–184

S_Start clears all sound channels when loading a new level. File: linuxdoom-1.10/s_sound.c, lines 207–211

Selects appropriate background music for the current level based on the game mode. File: linuxdoom-1.10/s_sound.c, lines 213–219


16.4 Starting and Updating Sounds

S_StartSoundAtVolume validates the sound effect ID and retrieves its info structure. File: linuxdoom-1.10/s_sound.c, lines 277–284

Adjusts parameters for sounds not coming from the player. File: linuxdoom-1.10/s_sound.c, lines 302–310

Stops any existing sound at the origin before requesting a new channel. File: linuxdoom-1.10/s_sound.c, lines 348–354

The handle returned by I_StartSound identifies this sound instance in the audio system. File: linuxdoom-1.10/s_sound.c, lines 387–394


16.5 Updating Sounds

S_UpdateSounds iterates through all sound channels to check their current state. File: linuxdoom-1.10/s_sound.c, lines 553–561

Adjusts volume, stereo separation, and pitch for sounds based on distance and angle from the listener. File: linuxdoom-1.10/s_sound.c, lines 586–591

Channels get freed back to the pool when their sounds finish playing. File: linuxdoom-1.10/s_sound.c, lines 601–605


16.6 Music Playback

S_ChangeMusic validates the music number and returns if the track is already playing. File: linuxdoom-1.10/s_sound.c, lines 657–666

Constructs the WAD lump name for music data by adding d_ prefix. File: linuxdoom-1.10/s_sound.c, lines 671–676

Starts playback of the new music track after registering the data. File: linuxdoom-1.10/s_sound.c, lines 678–684

S_StopMusic handles cleanup when stopping background music. File: linuxdoom-1.10/s_sound.c, lines 689–696


16.7 Volume Controls

S_SetMusicVolume controls the game’s music volume level. File: linuxdoom-1.10/s_sound.c, lines 616–623

S_SetSfxVolume sets the runtime sound effects volume. File: linuxdoom-1.10/s_sound.c, lines 631–639


The sound mixer uses a channel-based architecture to manage simultaneous sounds. Each channel tracks a sound’s origin and parameters, while distance-based attenuation and stereo separation create spatial audio. The system efficiently reuses channels based on priority and maintains separate volume controls for music and sound effects.