Exemplo n.º 1
0
    static void SND_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;
    }
Exemplo n.º 2
0
        static void PaintChannelFrom8(channel_t ch, sfxcache_t sc, Int32 count, Int32 offset)
        {
            Int32 data;

            Int32[] lscale;
            Int32[] rscale;
            Int32   sfx;
            Int32   i;
            portable_samplepair_t samp;

            if (ch.leftvol > 255)
            {
                ch.leftvol = 255;
            }
            if (ch.rightvol > 255)
            {
                ch.rightvol = 255;
            }
            lscale = snd_scaletable[ch.leftvol >> 3];
            rscale = snd_scaletable[ch.rightvol >> 3];
            sfx    = ch.pos;
            for (i = 0; i < count; i++, offset++)
            {
                var left  = paInt32Buffer.Get(offset * 2);
                var right = paInt32Buffer.Get(offset * 2 + 1);
                data   = sc.data[sfx + i];
                left  += lscale[data];
                right += rscale[data];
                paInt32Buffer.Put(offset * 2, left);
                paInt32Buffer.Put(offset * 2 + 1, right);
            }

            ch.pos += count;
        }
Exemplo n.º 3
0
        static void PaintChannelFrom16(channel_t ch, sfxcache_t sc, Int32 count, Int32 offset)
        {
            Int32 data;
            Int32 left, right;
            Int32 leftvol, rightvol;
            Int32 sfx;
            Int32 i;
            portable_samplepair_t samp;

            leftvol  = ch.leftvol * snd_vol;
            rightvol = ch.rightvol * snd_vol;
            ByteBuffer bb = ByteBuffer.Wrap(sc.data);

            bb.Order = ByteOrder.LittleEndian;
            sb       = bb.AsInt16Buffer();
            sfx      = ch.pos;
            for (i = 0; i < count; i++, offset++)
            {
                left   = paInt32Buffer.Get(offset * 2);
                right  = paInt32Buffer.Get(offset * 2 + 1);
                data   = sb.Get(sfx + i);
                left  += (data * leftvol) >> 8;
                right += (data * rightvol) >> 8;
                paInt32Buffer.Put(offset * 2, left);
                paInt32Buffer.Put(offset * 2 + 1, right);
            }

            ch.pos += count;
        }
Exemplo n.º 4
0
    static void S_SoundList()
    {
        int total = 0;

        for (int i = 0; i < num_sfx; 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_Printf("L");
            }
            else
            {
                Con_Printf(" ");
            }
            Con_Printf("({0:d2}b) {1:g6} : {2}\n", sc.width * 8, size, sfx.name);
        }
        Con_Printf("Total resident: {0}\n", total);
    }
Exemplo n.º 5
0
 public void clear()
 {
     this.name  = this.truename = null;
     this.cache = null;
     this.registration_sequence = 0;
     this.bufferId = -1;
     this.isCached = false;
 }
Exemplo n.º 6
0
    static sfxcache_t S_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 = COM_LoadFile(namebuffer);
        if (data == null)
        {
            Con_Printf("Couldn't load {0}\n", namebuffer);
            return(null);
        }

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

        if (info.channels != 1)
        {
            Con_Printf("{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);
    }
Exemplo n.º 7
0
        /*
         * ==============
         * S_LoadSound
         * ==============
         */
        public static sfxcache_t S_LoadSound(sfx_t s)
        {
            string namebuffer = new string(new char[256]);

            byte[]     data;
            sfxcache_t sc;

            // see if still in memory
            sc = s.cache;
            if (sc != null)
            {
                return(sc);
            }

            //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
            // load it in
            namebuffer  = "sound/";
            namebuffer += s.name;
            namebuffer += ".mp3";

            //	Con_Printf ("loading %s\n",namebuffer);

            data = common.COM_LoadStackFile(namebuffer, null, 0);
            if (data == null)
            {
                console.Con_Printf("Couldn't load " + namebuffer + "\n");
                return(null);
            }

            sc      = new sfxcache_t();
            s.cache = sc;
            if (sc == null)
            {
                return(null);
            }

            sc.loopstart = -1;
            for (int kk = 0; kk < loops.Length; kk++)
            {
                if (s.name.CompareTo(loops[kk]) == 0)
                {
                    sc.loopstart = 1;
                    break;
                }
            }
            sc.data = data;

            return(sc);
        }
Exemplo n.º 8
0
        public sfxcache_t LoadSound(sfx_t s)
        {
            if (s.isCached)
            {
                return(s.cache);
            }
            sfxcache_t sc = WaveLoader.LoadSound(s);

            if (sc != null)
            {
                InitBuffer(sc.data, s.bufferId, sc.speed);
                s.isCached   = true;
                s.cache.data = null;
            }

            return(sc);
        }
Exemplo n.º 9
0
        /*
        ==============
        S_LoadSound
        ==============
        */
        public static sfxcache_t S_LoadSound(sfx_t s)
        {
            string	        namebuffer = StringExtensions.StringOfLength(256);
            Uint8Array data;
            sfxcache_t	    sc;

            // see if still in memory
            sc = s.cache;
            if (sc != null)
                return sc;

            //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
            // load it in
            namebuffer = "sound/";
            namebuffer += s.name;
            namebuffer += ".mp3";

            //	Con_Printf ("loading %s\n",namebuffer);

            data = common.COM_LoadStackFile(namebuffer, null, 0);
            if (data == null)
            {
                console.Con_Printf ("Couldn't load " + namebuffer + "\n");
                return null;
            }

            sc = new sfxcache_t ();
            s.cache = sc;
            if (sc == null)
                return null;

            sc.loopstart = -1;
            for(int kk = 0; kk < loops.Length; kk++)
            {
                if (s.name.CompareTo(loops[kk]) == 0)
                {
                    sc.loopstart = 1;
                    break;
                }
            }
            sc.data = data;

            return sc;
        }
Exemplo n.º 10
0
        // 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;
        }
Exemplo n.º 11
0
        // 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;
        }
Exemplo n.º 12
0
    static void SND_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;
    }
Exemplo n.º 13
0
    public static void S_StaticSound(sfx_t sfx, ref Vector3 origin, float vol, float attenuation)
    {
        if (sfx == null)
        {
            return;
        }

        if (total_channels == q_shared.MAX_CHANNELS)
        {
            Con_Printf("total_channels == MAX_CHANNELS\n");
            return;
        }

        channel_t ss = channels[total_channels];

        total_channels++;

        sfxcache_t sc = S_LoadSound(sfx);

        if (sc == null)
        {
            return;
        }

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

        ss.sfx        = sfx;
        ss.origin     = origin;
        ss.master_vol = (int)vol;
        ss.dist_mult  = (attenuation / 64) / sound_nominal_clip_dist;
        ss.end        = paintedtime + sc.length;

        SND_Spatialize(ss);
    }
Exemplo n.º 14
0
        public static void StartSound(Single[] origin, Int32 entnum, Int32 entchannel, sfx_t sfx, Single fvol, Single attenuation, Single timeofs)
        {
            if (!sound_started)
            {
                return;
            }
            if (sfx == null)
            {
                return;
            }
            if (sfx.name[0] == '*')
            {
                sfx = RegisterSexedSound(cl_entities[entnum].current, sfx.name);
            }
            sfxcache_t sc = WaveLoader.LoadSound(sfx);

            if (sc == null)
            {
                return;
            }
            var         vol = ( Int32 )(fvol * 255);
            playsound_t ps  = AllocPlaysound();

            if (ps == null)
            {
                return;
            }
            if (origin != null)
            {
                Math3D.VectorCopy(origin, ps.origin);
                ps.fixed_origin = true;
            }
            else
            {
                ps.fixed_origin = false;
            }
            ps.entnum      = entnum;
            ps.entchannel  = entchannel;
            ps.attenuation = attenuation;
            ps.volume      = vol;
            ps.sfx         = sfx;
            var start = ( Int32 )(cl.frame.servertime * 0.001F * dma.speed + s_beginofs);

            if (start < paintedtime)
            {
                start      = paintedtime;
                s_beginofs = ( Int32 )(start - (cl.frame.servertime * 0.001F * dma.speed));
            }
            else if (start > paintedtime + 0.3F * dma.speed)
            {
                start      = ( Int32 )(paintedtime + 0.1F * dma.speed);
                s_beginofs = ( Int32 )(start - (cl.frame.servertime * 0.001F * dma.speed));
            }
            else
            {
                s_beginofs -= 10;
            }

            if (timeofs == 0F)
            {
                ps.begin = paintedtime;
            }
            else
            {
                ps.begin = ( Int64 )(start + timeofs * dma.speed);
            }
            playsound_t sort = new playsound_t();

            ps.next      = sort;
            ps.prev      = sort.prev;
            ps.next.prev = ps;
            ps.prev.next = ps;
        }
Exemplo n.º 15
0
    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 = 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;
                }
            }
        }
    }
Exemplo n.º 16
0
    static void S_PaintChannels(int endtime)
    {
        while (paintedtime < endtime)
        {
            // if paintbuffer is smaller than DMA buffer
            int end = endtime;
            if (endtime - paintedtime > q_shared.PAINTBUFFER_SIZE)
            {
                end = paintedtime + q_shared.PAINTBUFFER_SIZE;
            }

            // clear the paint buffer
            Array.Clear(paintbuffer, 0, end - paintedtime);

            // paint in the channels.
            for (int i = 0; i < total_channels; i++)
            {
                channel_t ch = channels[i];

                if (ch.sfx == null)
                {
                    continue;
                }
                if (ch.leftvol == 0 && ch.rightvol == 0)
                {
                    continue;
                }

                sfxcache_t sc = S_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)
                        {
                            SND_PaintChannelFrom8(ch, sc, count);
                        }
                        else
                        {
                            SND_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
            S_TransferPaintBuffer(end);
            paintedtime = end;
        }
    }
Exemplo n.º 17
0
    public static void S_StartSound(int entnum, int entchannel, sfx_t sfx, ref Vector3 origin, float fvol, float attenuation)
    {
        if (!sound_started || sfx == null)
        {
            return;
        }

        if (nosound.value != 0)
        {
            return;
        }

        int vol = (int)(fvol * 255);

        // pick a channel to play on
        channel_t target_chan = SND_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 / sound_nominal_clip_dist;
        target_chan.master_vol = vol;
        target_chan.entnum     = entnum;
        target_chan.entchannel = entchannel;
        SND_Spatialize(target_chan);

        if (target_chan.leftvol == 0 && target_chan.rightvol == 0)
        {
            return;             // not audible at all
        }
        // new channel
        sfxcache_t sc = S_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 = q_shared.NUM_AMBIENTS; i < q_shared.NUM_AMBIENTS + q_shared.MAX_DYNAMIC_CHANNELS; i++)
        {
            channel_t check = channels[i];
            if (check == target_chan)
            {
                continue;
            }

            if (check.sfx == sfx && check.pos == 0)
            {
                int skip = 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;
            }
        }
    }