int ch_idx;
int first_to_die;
int life_left;
+ channel_t* ch;
// Check for replacement sound, or find the best one to replace
first_to_die = -1;
life_left = 0x7fffffff;
for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
{
+ ch = &channels[ch_idx];
if (entchannel != 0 // channel 0 never overrides
- && channels[ch_idx].entnum == entnum
- && (channels[ch_idx].entchannel == entchannel || entchannel == -1) )
+ && ch->entnum == entnum
+ && (ch->entchannel == entchannel || entchannel == -1) )
{ // always override sound from same entity
first_to_die = ch_idx;
break;
}
// don't let monster sounds override player sounds
- if (channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && channels[ch_idx].sfx)
+ if (ch->entnum == cl.viewentity && entnum != cl.viewentity && ch->sfx)
continue;
- if (channels[ch_idx].end - paintedtime < life_left)
+ // don't override looped sounds
+ if (ch->forceloop || (ch->sfx != NULL && ch->sfx->loopstart >= 0))
+ continue;
+
+ if (ch->end - paintedtime < life_left)
{
- life_left = channels[ch_idx].end - paintedtime;
+ life_left = ch->end - paintedtime;
first_to_die = ch_idx;
}
}
target_chan->sfx = sfx;
target_chan->pos = 0.0;
target_chan->end = paintedtime + sfx->total_length;
+ target_chan->lastptime = paintedtime;
// if an identical sound has also been started this frame, offset the pos
// a bit to keep it from just making the first one louder
ss->master_vol = vol;
ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
ss->end = paintedtime + sfx->total_length;
+ ss->lastptime = paintedtime;
SND_Spatialize (ss, true);
}
if (!S_LoadSound (sfx, true))
continue;
- ltime = paintedtime;
+ // if the sound hasn't been painted last time, update his position
+ if (ch->lastptime < paintedtime)
+ {
+ ch->pos += paintedtime - ch->lastptime;
+
+ // If the sound should have ended by then
+ if ((unsigned int)ch->pos > sfx->total_length)
+ {
+ int loopstart;
+ if (ch->forceloop)
+ loopstart = 0;
+ else
+ loopstart = -1;
+ if (sfx->loopstart >= 0)
+ loopstart = sfx->loopstart;
+
+ // If the sound is looped
+ if (loopstart >= 0)
+ ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
+ else
+ ch->pos = sfx->total_length;
+ ch->end = paintedtime + sfx->total_length - ch->pos;
+ }
+ }
+
+ ltime = paintedtime;
while (ltime < end)
{
qboolean stop_paint;
else
stop_paint = !SND_PaintChannelFrom16(ch, count);
- ltime += count;
+ if (!stop_paint)
+ {
+ ltime += count;
+ ch->lastptime = ltime;
+ }
}
else
stop_paint = false;
int leftvol; // 0-255 volume
int rightvol; // 0-255 volume
int end; // end time in global paintsamples
- int pos; // sample position in sfx
+ int lastptime; // last time this channel was painted
+ int pos; // sample position in sfx
int looping; // where to loop, -1 = no looping
int entnum; // to allow overriding a specific sound
int entchannel;