Esempio n. 1
0
        protected override WaveBank DoTransform(WaveBank obj)
        {
            if (obj != null)
            {
                return(obj);
            }

            mReader.Keep();
            mReader.PushAnchor();

            if (mReader.Read32() != WSYS)
            {
                mareep.WriteError("WSYS: could not find header.");
            }

            var size = mReader.ReadS32();

            mReader.Step(8);             // unused
            var winfOffset = mReader.ReadS32();
            var wbctOffset = mReader.ReadS32();

            mareep.WriteMessage("WSYS: header found, size {0:F1} KB\n", ((double)size / 1024.0d));

            var waveBank = new WaveBank();

            mReader.Goto(winfOffset);

            if (mReader.Read32() != WINF)
            {
                mareep.WriteError("WSYS: could not find WINF at 0x{0:X6}.", winfOffset);
            }

            var waveGroupCount = mReader.ReadS32();

            if (waveGroupCount < 0)
            {
                mareep.WriteError("WSYS: bad wave-group count '{0}' in WINF.", waveGroupCount);
            }

            mareep.WriteMessage("WSYS: WINF found, {0} wave group(s).\n", waveGroupCount);

            var waveGroupOffsets = mReader.ReadS32s(waveGroupCount);

            mReader.Goto(wbctOffset);

            if (mReader.Read32() != WBCT)
            {
                mareep.WriteError("WSYS: could not find WBCT at 0x{0:X6}.", wbctOffset);
            }

            mReader.Step(4);             // unused

            var sceneCount = mReader.ReadS32();

            if (sceneCount != waveGroupCount)
            {
                mareep.WriteError("WSYS: WINF count ({0}) does not match WBCT count ({1}).", waveGroupCount, sceneCount);
            }

            var sceneOffsets = mReader.ReadS32s(sceneCount);

            for (var i = 0; i < waveGroupCount; ++i)
            {
                mReader.Goto(waveGroupOffsets[i]);

                var archiveName   = mReader.ReadString <aCSTR>(112);
                var waveInfoCount = mReader.ReadS32();

                if (waveInfoCount < 0)
                {
                    mareep.WriteError("WSYS: bad wave count '{0}' in wave group #{1}.", waveInfoCount, i);
                }

                var waveInfoOffsets = mReader.ReadS32s(waveInfoCount);

                mReader.Goto(sceneOffsets[i]);

                if (mReader.Read32() != SCNE)
                {
                    mareep.WriteError("WSYS: could not find SCNE at 0x{0:X6}.", sceneOffsets[i]);
                }

                mReader.Step(8);                 // unused
                var cdfOffset = mReader.ReadS32();
                mReader.Goto(cdfOffset);

                if (mReader.Read32() != C_DF)
                {
                    mareep.WriteError("WSYS: could not find C-DF at 0x{0:X6}.", cdfOffset);
                }

                var waveidCount = mReader.ReadS32();

                if (waveidCount != waveInfoCount)
                {
                    mareep.WriteError("WSYS: C-DF count ({0}) does not match wave-info count ({1}).", waveidCount, waveInfoCount);
                }

                var waveidOffsets = mReader.ReadS32s(waveidCount);

                var waveGroup = new WaveGroup();
                waveGroup.ArchiveFileName = archiveName;

                for (var j = 0; j < waveInfoCount; ++j)
                {
                    var wave = new Wave();

                    mReader.Goto(waveidOffsets[j]);

                    var waveid = (mReader.ReadS32() & 0xFFFF);
                    wave.WaveId = waveid;

                    mReader.Goto(waveInfoOffsets[j]);
                    mReader.Step(1);                     // unknown

                    var format = (WaveFormat)mReader.Read8();

                    if (!format.IsDefined())
                    {
                        mareep.WriteError("WSYS: group #{0}: wave #{1}: bad format '{2}'.", i, j, (byte)format);
                    }
                    else
                    {
                        wave.Format = format;
                    }

                    var key = mReader.Read8();

                    if (key < 0 || key > 127)
                    {
                        mareep.WriteError("WSYS: group #{0}: wave #{1}: bad root key '{2}'.", i, j, key);
                    }
                    else
                    {
                        wave.RootKey = key;
                    }

                    mReader.Step(1);                     // alignment

                    var sampleRate = mReader.ReadF32();

                    if (sampleRate < 0.0f)
                    {
                        mareep.WriteError("WSYS: group #{0}: wave #{1}: bad sample rate '{2:F1}'.", i, j, sampleRate);
                    }
                    else
                    {
                        wave.SampleRate = sampleRate;
                    }

                    var waveStart = mReader.ReadS32();

                    if (waveStart < 0)
                    {
                        mareep.WriteError("WSYS: group #{0}: wave #{1}: bad wave start '{2}'.", i, j, waveStart);
                    }
                    else
                    {
                        wave.WaveStart = waveStart;
                    }

                    var waveSize = mReader.ReadS32();

                    if (waveSize < 0)
                    {
                        mareep.WriteError("WSYS: group #{0}: wave #{1}: bad wave size '{1}'.", i, j, waveSize);
                    }
                    else
                    {
                        wave.WaveSize = waveSize;
                    }

                    wave.Loop = (mReader.Read32() != 0);

                    var loopStart = mReader.ReadS32();

                    if (loopStart < 0)
                    {
                        mareep.WriteError("WSYS: group #{0}: wave #{1}: bad loop start '{2}'.", i, j, loopStart);
                    }
                    else
                    {
                        wave.LoopStart = loopStart;
                    }

                    var loopEnd = mReader.ReadS32();

                    if (loopEnd < 0)
                    {
                        mareep.WriteError("WSYS: group #{0}: wave #{1}: bad loop end '{2}'.", i, j, loopEnd);
                    }
                    else
                    {
                        wave.LoopEnd = loopEnd;
                    }

                    var sampleCount = mReader.ReadS32();
                    wave.SampleCount = mareep.CalculateSampleCount(format, waveSize);

                    if (loopStart > loopEnd)
                    {
                        mareep.WriteWarning("WSYS: group #{0}: wave #{1}: loop start '{2}' is greater than loop end '{3}'.\n", i, j, loopStart, loopEnd);
                    }

                    if (loopStart > wave.SampleCount)
                    {
                        mareep.WriteWarning("WSYS: group #{0}: wave #{1}: loop start '{2}' is greater than sample count '{3}'.\n", i, j, loopStart, wave.SampleCount);
                    }

                    if (loopEnd > wave.SampleCount)
                    {
                        mareep.WriteWarning("WSYS: group #{0}: wave #{1}: loop end '{2}' is greater than sample count '{3}'.\n", i, j, loopEnd, wave.SampleCount);
                    }

                    wave.HistoryLast   = mReader.ReadS16();
                    wave.HistoryPenult = mReader.ReadS16();

                    // rest of the fields are unknown or runtime

                    waveGroup.Add(wave);
                }

                waveBank.Add(waveGroup);
            }

            mReader.PopAnchor();
            mReader.Back();

            return(waveBank);
        }
Esempio n. 2
0
        Wave LoadWave(xElement xwave)
        {
            xAttribute attribute;

            var wave = new Wave();

            attribute = xwave.Attribute(cWaveId);

            if (attribute == null)
            {
                mareep.WriteWarning("XML: line #{0}: missing wave id\n", xwave.LineNumber);
                return(null);
            }

            var waveid = (attribute | -1);

            if (waveid < 0)
            {
                mareep.WriteWarning("XML: line #{0}: bad wave id '{1}'\n", attribute.LineNumber, attribute.Value);
                return(null);
            }

            wave.WaveId = waveid;

            attribute = xwave.Attribute(cWaveFile);

            if (attribute == null)
            {
                mareep.WriteWarning("XML: line #{0}: missing file\n", xwave.LineNumber);
                return(null);
            }

            wave.FileName = attribute.Value;

            attribute = xwave.Attribute(cWaveFormat);

            if (attribute == null)
            {
                mareep.WriteWarning("XML: line #{0}: missing format\n", xwave.LineNumber);
                return(null);
            }

            var format = attribute.AsEnum((WaveFormat)(-1));

            if (!format.IsDefined())
            {
                mareep.WriteWarning("XML: line #{0}: bad wave format '{1}'\n", attribute.LineNumber, attribute.Value);
                return(null);
            }

            wave.Format = format;

            attribute = xwave.Attribute(cWaveRate);

            if (attribute != null)
            {
                var samplerate = (attribute | -1.0f);

                if (samplerate < 0.0f)
                {
                    mareep.WriteWarning("XML: line #{0}: bad sample rate '{1:F1}'.\n", attribute.LineNumber, attribute.Value);
                    return(null);
                }

                wave.SampleRate = samplerate;
            }

            attribute = xwave.Attribute(cWaveKey);

            var keynumber = 60;

            if (attribute != null)
            {
                keynumber = attribute.AsKeyNumber();

                if (keynumber < 0 || keynumber > 127)
                {
                    mareep.WriteWarning("XML: line #{0}: bad root key '{1}'\n", attribute.LineNumber, attribute.Value);
                    return(null);
                }
            }

            wave.RootKey = keynumber;

            var xloopstart = xwave.Attribute(cWaveLoopStart);
            var xloopend   = xwave.Attribute(cWaveLoopEnd);

            if (xloopstart != null && xloopend != null)
            {
                var loopstart = (xloopstart | -1);
                var loopend   = (xloopend | -1);

                if (loopstart < 0)
                {
                    mareep.WriteWarning("XML: line #{0}: bad loop start '{0}'\n", xloopstart.LineNumber, xloopstart.Value);
                    return(null);
                }
                else if (loopend < 0)
                {
                    mareep.WriteWarning("XML: line #{0}: bad loop end '{0}'\n", xloopend.LineNumber, xloopend.Value);
                    return(null);
                }

                wave.Loop      = true;
                wave.LoopStart = loopstart;
                wave.LoopEnd   = loopend;
            }
            else if ((xloopstart == null) != (xloopend == null))
            {
                mareep.WriteWarning("XML: line #{0}: only one loop specified.\n", (xloopstart ?? xloopend).LineNumber);
                return(null);
            }

            return(wave);
        }
Esempio n. 3
0
 void ExtractRaw(Wave wave, byte[] data, Stream stream)
 {
     stream.Write(data, 0, data.Length);
 }