コード例 #1
0
ファイル: AudioLoader.cs プロジェクト: SpectrumLib/Spectrum
        public unsafe override IAudioSource Load(ContentStream stream, LoaderContext ctx)
        {
            uint frameCount = stream.ReadUInt32();
            uint sampleRate = stream.ReadUInt32();
            bool isStereo   = stream.ReadBoolean();
            bool isLossy    = stream.ReadBoolean();

            if (ctx.ContentType == SOUNDEFFECT_TYPE)
            {
                uint fullLen = frameCount * 2 * (isStereo ? 2u : 1u);
                var  data    = Marshal.AllocHGlobal((int)fullLen);

                try
                {
                    RLADStream.DecodeAll(stream, (short *)data.ToPointer(), isStereo, frameCount);

                    var sb = new SoundBuffer();
                    sb.SetData(data, isStereo ? AudioFormat.Stereo16 : AudioFormat.Mono16, sampleRate, fullLen);
                    return(new SoundEffect(sb));
                }
                finally
                {
                    Marshal.FreeHGlobal(data);
                }
            }
            else             // Song
            {
                var astream = new RLADStream(stream.Duplicate(), isStereo, frameCount);
                return(new Song(astream, sampleRate));
            }
        }
コード例 #2
0
        public override Texture2D Load(ContentStream stream, LoaderContext ctx)
        {
            // Read the ushort dimensions
            uint w     = stream.ReadUInt16();
            uint h     = stream.ReadUInt16();
            uint count = w * h;

            if ((count * 4) != stream.Remaining)
            {
                throw new Exception($"the expected and available texture data lengths do not match ({count * 4} != {stream.Remaining}).");
            }

            // Read in the entirety of the pixel data  (stored as RGBA packed uints)
            var data = new uint[count];

            for (uint i = 0; i < count; ++i)
            {
                data[i] = stream.ReadUInt32();
            }

            // Create the texture, upload the pixels, and return
            Texture2D tex = new Texture2D(w, h);

            tex.SetData(data);
            return(tex);
        }