// SND_PaintChannelFrom8 static void PaintChannelFrom8(channel_t ch, sfxcache_t sc, int count) { if (ch.leftvol > 255) { ch.leftvol = 255; } if (ch.rightvol > 255) { ch.rightvol = 255; } int lscale = ch.leftvol >> 3; int rscale = ch.rightvol >> 3; byte[] sfx = sc.data; int offset = ch.pos; for (int i = 0; i < count; i++) { int data = sfx[offset + i]; _PaintBuffer[i].left += _ScaleTable[lscale, data]; _PaintBuffer[i].right += _ScaleTable[rscale, data]; } ch.pos += count; }
// S_SoundList private static void SoundList() { int total = 0; for (int i = 0; i < _NumSfx; i++) { sfx_t sfx = _KnownSfx[i]; sfxcache_t sc = (sfxcache_t)Cache.Check(sfx.cache); if (sc == null) { continue; } int size = sc.length * sc.width * (sc.stereo + 1); total += size; if (sc.loopstart >= 0) { Con.Print("L"); } else { Con.Print(" "); } Con.Print("({0:d2}b) {1:g6} : {2}\n", sc.width * 8, size, sfx.name); } Con.Print("Total resident: {0}\n", total); }
// S_LoadSound private static sfxcache_t LoadSound(sfx_t s) { // see if still in memory sfxcache_t sc = (sfxcache_t)Cache.Check(s.cache); if (sc != null) { return(sc); } // load it in string namebuffer = "sound/" + s.name; byte[] data = common.LoadFile(namebuffer); if (data == null) { Con.Print("Couldn't load {0}\n", namebuffer); return(null); } wavinfo_t info = GetWavInfo(s.name, data); if (info.channels != 1) { Con.Print("{0} is a stereo sample\n", s.name); return(null); } float stepscale = info.rate / (float)_shm.speed; int len = (int)(info.samples / stepscale); len *= info.width * info.channels; s.cache = Cache.Alloc(len, s.name); if (s.cache == null) { return(null); } sc = new sfxcache_t(); sc.length = info.samples; sc.loopstart = info.loopstart; sc.speed = info.rate; sc.width = info.width; sc.stereo = info.channels; s.cache.data = sc; ResampleSfx(s, sc.speed, sc.width, new ByteArraySegment(data, info.dataofs)); return(sc); }
// SND_PaintChannelFrom16 static void PaintChannelFrom16(channel_t ch, sfxcache_t sc, int count) { int leftvol = ch.leftvol; int rightvol = ch.rightvol; byte[] sfx = sc.data; int offset = ch.pos * 2; // sfx = (signed short *)sc->data + ch->pos; for (int i = 0; i < count; i++) { int data = (short)((ushort)sfx[offset] + ((ushort)sfx[offset + 1] << 8)); // Uze: check is this is right!!! int left = (data * leftvol) >> 8; int right = (data * rightvol) >> 8; _PaintBuffer[i].left += left; _PaintBuffer[i].right += right; offset += 2; } ch.pos += count; }
// S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation) public static void StaticSound(sfx_t sfx, ref Vector3 origin, float vol, float attenuation) { if (sfx == null) { return; } if (_TotalChannels == MAX_CHANNELS) { Con.Print("total_channels == MAX_CHANNELS\n"); return; } channel_t ss = _Channels[_TotalChannels]; _TotalChannels++; sfxcache_t sc = LoadSound(sfx); if (sc == null) { return; } if (sc.loopstart == -1) { Con.Print("Sound {0} not looped\n", sfx.name); return; } ss.sfx = sfx; ss.origin = origin; ss.master_vol = (int)vol; ss.dist_mult = (attenuation / 64) / _SoundNominalClipDist; ss.end = _PaintedTime + sc.length; Spatialize(ss); }
// S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation) public static void StartSound(int entnum, int entchannel, sfx_t sfx, ref Vector3 origin, float fvol, float attenuation) { if (!_SoundStarted || sfx == null) { return; } if (_NoSound.Value != 0) { return; } int vol = (int)(fvol * 255); // pick a channel to play on channel_t target_chan = PickChannel(entnum, entchannel); if (target_chan == null) { return; } // spatialize //memset (target_chan, 0, sizeof(*target_chan)); target_chan.origin = origin; target_chan.dist_mult = attenuation / _SoundNominalClipDist; target_chan.master_vol = vol; target_chan.entnum = entnum; target_chan.entchannel = entchannel; Spatialize(target_chan); if (target_chan.leftvol == 0 && target_chan.rightvol == 0) { return; // not audible at all } // new channel sfxcache_t sc = LoadSound(sfx); if (sc == null) { target_chan.sfx = null; return; // couldn't load the sound's data } target_chan.sfx = sfx; target_chan.pos = 0; target_chan.end = _PaintedTime + sc.length; // 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 for (int i = Ambients.NUM_AMBIENTS; i < Ambients.NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS; i++) { channel_t check = _Channels[i]; if (check == target_chan) { continue; } if (check.sfx == sfx && check.pos == 0) { int skip = sys.Random((int)(0.1 * _shm.speed)); // rand() % (int)(0.1 * shm->speed); if (skip >= target_chan.end) { skip = target_chan.end - 1; } target_chan.pos += skip; target_chan.end -= skip; break; } } }
// ResampleSfx private static void ResampleSfx(sfx_t sfx, int inrate, int inwidth, ByteArraySegment data) { sfxcache_t sc = (sfxcache_t)Cache.Check(sfx.cache); if (sc == null) { return; } float stepscale = (float)inrate / _shm.speed; // this is usually 0.5, 1, or 2 int outcount = (int)(sc.length / stepscale); sc.length = outcount; if (sc.loopstart != -1) { sc.loopstart = (int)(sc.loopstart / stepscale); } sc.speed = _shm.speed; if (_LoadAs8bit.Value != 0) { sc.width = 1; } else { sc.width = inwidth; } sc.stereo = 0; sc.data = new byte[outcount * sc.width]; // uze: check this later!!! // resample / decimate to the current source rate byte[] src = data.Data; if (stepscale == 1 && inwidth == 1 && sc.width == 1) { // fast special case for (int i = 0; i < outcount; i++) { int v = src[data.StartIndex + i] - 128; sc.data[i] = (byte)((sbyte)v); //((signed char *)sc.data)[i] = (int)( (unsigned char)(data[i]) - 128); } } else { // general case int samplefrac = 0; int fracstep = (int)(stepscale * 256); int sample; short[] sa = new short[1]; for (int i = 0; i < outcount; i++) { int srcsample = samplefrac >> 8; samplefrac += fracstep; if (inwidth == 2) { Buffer.BlockCopy(src, data.StartIndex + srcsample * 2, sa, 0, 2); sample = Common.LittleShort(sa[0]); // ((short *)data)[srcsample] ); } else { sample = (int)(src[data.StartIndex + srcsample] - 128) << 8; //sample = (int)( (unsigned char)(data[srcsample]) - 128) << 8; } if (sc.width == 2) { sa[0] = (short)sample; Buffer.BlockCopy(sa, 0, sc.data, i * 2, 2); //((short *)sc->data)[i] = sample; } else { sc.data[i] = (byte)(sbyte)(sample >> 8); //((signed char *)sc->data)[i] = sample >> 8; } } } }
// S_PaintChannels static void PaintChannels(int endtime) { while (_PaintedTime < endtime) { // if paintbuffer is smaller than DMA buffer int end = endtime; if (endtime - _PaintedTime > PAINTBUFFER_SIZE) { end = _PaintedTime + PAINTBUFFER_SIZE; } // clear the paint buffer Array.Clear(_PaintBuffer, 0, end - _PaintedTime); // paint in the channels. for (int i = 0; i < _TotalChannels; i++) { channel_t ch = _Channels[i]; if (ch.sfx == null) { continue; } if (ch.leftvol == 0 && ch.rightvol == 0) { continue; } sfxcache_t sc = LoadSound(ch.sfx); if (sc == null) { continue; } int count, ltime = _PaintedTime; while (ltime < end) { // paint up to end if (ch.end < end) { count = ch.end - ltime; } else { count = end - ltime; } if (count > 0) { if (sc.width == 1) { PaintChannelFrom8(ch, sc, count); } else { PaintChannelFrom16(ch, sc, count); } ltime += count; } // if at end of loop, restart if (ltime >= ch.end) { if (sc.loopstart >= 0) { ch.pos = sc.loopstart; ch.end = ltime + sc.length - ch.pos; } else { // channel just stopped ch.sfx = null; break; } } } } // transfer out according to DMA format TransferPaintBuffer(end); _PaintedTime = end; } }