Esempio n. 1
0
        void CollectAudioData()
        {
            var ms         = new MemoryStream();
            var adpcmIndex = 0;

            bool compressed = false;

            for (var i = 0; i < Frames; i++)
            {
                stream.Seek(offsets[i], SeekOrigin.Begin);
                var end = (i < Frames - 1) ? offsets[i + 1] : stream.Length;

                while (stream.Position < end)
                {
                    var type   = stream.ReadASCII(4);
                    var length = int2.Swap(stream.ReadUInt32());

                    switch (type)
                    {
                    case "SND0":
                    case "SND2":
                        var rawAudio = stream.ReadBytes((int)length);
                        ms.Write(rawAudio);
                        compressed = (type == "SND2");
                        break;

                    default:
                        stream.ReadBytes((int)length);
                        break;
                    }

                    // Chunks are aligned on even bytes; advance by a byte if the next one is null
                    if (stream.Peek() == 0)
                    {
                        stream.ReadByte();
                    }
                }
            }

            audioData = (compressed) ? AudLoader.LoadSound(ms.ToArray(), ref adpcmIndex) : ms.ToArray();
        }
Esempio n. 2
0
        void CollectAudioData()
        {
            var ms         = new MemoryStream();
            var adpcmIndex = 0;

            bool compressed = false;

            for (var i = 0; i < Frames; i++)
            {
                stream.Seek(offsets[i], SeekOrigin.Begin);
                BinaryReader reader = new BinaryReader(stream);
                var          end    = (i < Frames - 1) ? offsets[i + 1] : stream.Length;

                while (reader.BaseStream.Position < end)
                {
                    var type   = new String(reader.ReadChars(4));
                    var length = int2.Swap(reader.ReadUInt32());

                    switch (type)
                    {
                    case "SND0":
                    case "SND2":
                        var rawAudio = reader.ReadBytes((int)length);
                        ms.Write(rawAudio);
                        compressed = (type == "SND2");
                        break;

                    default:
                        reader.ReadBytes((int)length);
                        break;
                    }

                    if (reader.PeekChar() == 0)
                    {
                        reader.ReadByte();
                    }
                }
            }

            audioData = (compressed) ? AudLoader.LoadSound(ms.ToArray(), ref adpcmIndex) : ms.ToArray();
        }
Esempio n. 3
0
        void CollectAudioData()
        {
            var audio1     = new MemoryStream();         // left channel / mono
            var audio2     = new MemoryStream();         // right channel
            var adpcmIndex = 0;
            var compressed = false;

            for (var i = 0; i < Frames; i++)
            {
                stream.Seek(offsets[i], SeekOrigin.Begin);
                var end = (i < Frames - 1) ? offsets[i + 1] : stream.Length;

                while (stream.Position < end)
                {
                    var type = stream.ReadASCII(4);
                    if (type == "SN2J")
                    {
                        var jmp = int2.Swap(stream.ReadUInt32());
                        stream.Seek(jmp, SeekOrigin.Current);
                        type = stream.ReadASCII(4);
                    }

                    var length = int2.Swap(stream.ReadUInt32());

                    switch (type)
                    {
                    case "SND0":
                    case "SND2":
                        if (audioChannels == 1)
                        {
                            var rawAudio = stream.ReadBytes((int)length);
                            audio1.Write(rawAudio);
                        }
                        else
                        {
                            var rawAudio = stream.ReadBytes((int)length / 2);
                            audio1.Write(rawAudio);
                            rawAudio = stream.ReadBytes((int)length / 2);
                            audio2.Write(rawAudio);
                        }

                        compressed = type == "SND2";
                        break;

                    default:
                        stream.ReadBytes((int)length);
                        break;
                    }

                    // Chunks are aligned on even bytes; advance by a byte if the next one is null
                    if (stream.Peek() == 0)
                    {
                        stream.ReadByte();
                    }
                }
            }

            if (audioChannels == 1)
            {
                audioData = compressed ? AudLoader.LoadSound(audio1.ToArray(), ref adpcmIndex) : audio1.ToArray();
            }
            else
            {
                byte[] leftData, rightData;
                if (!compressed)
                {
                    leftData  = audio1.ToArray();
                    rightData = audio2.ToArray();
                }
                else
                {
                    adpcmIndex = 0;
                    leftData   = AudLoader.LoadSound(audio1.ToArray(), ref adpcmIndex);
                    adpcmIndex = 0;
                    rightData  = AudLoader.LoadSound(audio2.ToArray(), ref adpcmIndex);
                }

                audioData = new byte[rightData.Length + leftData.Length];
                var rightIndex = 0;
                var leftIndex  = 0;
                for (var i = 0; i < audioData.Length;)
                {
                    audioData[i++] = leftData[leftIndex++];
                    audioData[i++] = leftData[leftIndex++];
                    audioData[i++] = rightData[rightIndex++];
                    audioData[i++] = rightData[rightIndex++];
                }
            }

            hasAudio = audioData.Length > 0;
        }