Beispiel #1
0
        public snd(Host host)
        {
            Host = host;

            for (var i = 0; i < _KnownSfx.Length; i++)
            {
                _KnownSfx[i] = new SoundEffect_t( );
            }
        }
Beispiel #2
0
        // S_LoadSound
        private SoundEffectCache_t LoadSound(SoundEffect_t s)
        {
            // see if still in memory
            var sc = ( SoundEffectCache_t )Host.Cache.Check(s.cache);

            if (sc != null)
            {
                return(sc);
            }

            // load it in
            var namebuffer = "sound/" + s.name;

            var data = FileSystem.LoadFile(namebuffer);

            if (data == null)
            {
                Host.Console.Print("Couldn't load {0}\n", namebuffer);
                return(null);
            }

            var info = GetWavInfo(s.name, data);

            if (info.channels != 1)
            {
                Host.Console.Print("{0} is a stereo sample\n", s.name);
                return(null);
            }

            var stepscale = info.rate / ( Single )_shm.speed;
            var len       = ( Int32 )(info.samples / stepscale);

            len *= info.width * info.channels;

            s.cache = Host.Cache.Alloc(len, s.name);
            if (s.cache == null)
            {
                return(null);
            }

            sc           = new SoundEffectCache_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);
        }
Beispiel #3
0
        private SoundEffect_t _SfxRExp3;                                          // cl_sfx_r_exp3

        // CL_InitTEnts
        private void InitTempEntities()
        {
            _SfxWizHit   = Host.Sound.PrecacheSound("wizard/hit.wav");
            _SfxKnigtHit = Host.Sound.PrecacheSound("hknight/hit.wav");
            _SfxTink1    = Host.Sound.PrecacheSound("weapons/tink1.wav");
            _SfxRic1     = Host.Sound.PrecacheSound("weapons/ric1.wav");
            _SfxRic2     = Host.Sound.PrecacheSound("weapons/ric2.wav");
            _SfxRic3     = Host.Sound.PrecacheSound("weapons/ric3.wav");
            _SfxRExp3    = Host.Sound.PrecacheSound("weapons/r_exp3.wav");

            for (var i = 0; i < _TempEntities.Length; i++)
            {
                _TempEntities[i] = new Entity();
            }

            for (var i = 0; i < _Beams.Length; i++)
            {
                _Beams[i] = new beam_t();
            }
        }
Beispiel #4
0
        private SoundEffect_t _SfxRExp3;                                          // cl_sfx_r_exp3

        // CL_InitTEnts
        private void InitTempEntities()
        {
            this._SfxWizHit   = this.Host.Sound.PrecacheSound("wizard/hit.wav");
            this._SfxKnigtHit = this.Host.Sound.PrecacheSound("hknight/hit.wav");
            this._SfxTink1    = this.Host.Sound.PrecacheSound("weapons/tink1.wav");
            this._SfxRic1     = this.Host.Sound.PrecacheSound("weapons/ric1.wav");
            this._SfxRic2     = this.Host.Sound.PrecacheSound("weapons/ric2.wav");
            this._SfxRic3     = this.Host.Sound.PrecacheSound("weapons/ric3.wav");
            this._SfxRExp3    = this.Host.Sound.PrecacheSound("weapons/r_exp3.wav");

            for (var i = 0; i < this._TempEntities.Length; i++)
            {
                this._TempEntities[i] = new();
            }

            for (var i = 0; i < this._Beams.Length; i++)
            {
                this._Beams[i] = new();
            }
        }
Beispiel #5
0
        // S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
        public void StaticSound(SoundEffect_t sfx, ref Vector3 origin, Single vol, Single attenuation)
        {
            if (sfx == null)
            {
                return;
            }

            if (_TotalChannels == MAX_CHANNELS)
            {
                Host.Console.Print("total_channels == MAX_CHANNELS\n");
                return;
            }

            var ss = _Channels[_TotalChannels];

            _TotalChannels++;

            var sc = LoadSound(sfx);

            if (sc == null)
            {
                return;
            }

            if (sc.loopstart == -1)
            {
                Host.Console.Print("Sound {0} not looped\n", sfx.name);
                return;
            }

            ss.sfx        = sfx;
            ss.origin     = origin;
            ss.master_vol = ( Int32 )vol;
            ss.dist_mult  = (attenuation / 64) / _SoundNominalClipDist;
            ss.end        = _PaintedTime + sc.length;

            Spatialize(ss);
        }
Beispiel #6
0
        // S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol,  float attenuation)
        public void StartSound(Int32 entnum, Int32 entchannel, SoundEffect_t sfx, ref Vector3 origin, Single fvol, Single attenuation)
        {
            if (!_SoundStarted || sfx == null)
            {
                return;
            }

            if (Host.Cvars.NoSound.Get <Boolean>( ))
            {
                return;
            }

            var vol = ( Int32 )(fvol * 255);

            // pick a channel to play on
            var 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
            var 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 (var i = AmbientDef.NUM_AMBIENTS; i < AmbientDef.NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS; i++)
            {
                var check = _Channels[i];
                if (check == target_chan)
                {
                    continue;
                }

                if (check.sfx == sfx && check.pos == 0)
                {
                    var skip = MathLib.Random(( Int32 )(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;
                }
            }
        }
Beispiel #7
0
        // ResampleSfx
        private void ResampleSfx(SoundEffect_t sfx, int inrate, int inwidth, ByteArraySegment data)
        {
            var sc = ( SoundEffectCache_t )this.Host.Cache.Check(sfx.cache);

            if (sc == null)
            {
                return;
            }

            var stepscale = ( float )inrate / this._shm.speed;              // this is usually 0.5, 1, or 2

            var outcount = ( int )(sc.length / stepscale);

            sc.length = outcount;
            if (sc.loopstart != -1)
            {
                sc.loopstart = ( int )(sc.loopstart / stepscale);
            }

            sc.speed = this._shm.speed;
            if (this.Host.Cvars.LoadAs8bit.Get <bool>())
            {
                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
            var src = data.Data;

            if (stepscale == 1 && inwidth == 1 && sc.width == 1)
            {
                // fast special case
                for (var i = 0; i < outcount; i++)
                {
                    var 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
                var samplefrac = 0;
                var fracstep   = ( int )(stepscale * 256);
                int sample;
                var sa = new short[1];
                for (var i = 0; i < outcount; i++)
                {
                    var srcsample = samplefrac >> 8;
                    samplefrac += fracstep;
                    if (inwidth == 2)
                    {
                        Buffer.BlockCopy(src, data.StartIndex + srcsample * 2, sa, 0, 2);
                        sample = EndianHelper.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;
                    }
                }
            }
        }