예제 #1
0
        /// <summary>
        /// Write a bank file.
        /// </summary>
        /// <param name="writeMode">Write mode.</param>
        /// <param name="bw">The writer.</param>
        public void Write(WriteMode writeMode, BinaryDataWriter bw)
        {
            //Set the write mode.
            this.writeMode = writeMode;

            //Init the bank file.
            FileWriter FileWriter = new FileWriter();

            FileWriter.InitFile(bw, writeMode, "BNK", 1, Version);

            //Init info block.
            FileWriter.InitBlock(bw, ReferenceTypes.BNK_Block_Info, "INFO");

            //Init the reference to the wave id table.
            FileWriter.InitReference(bw, "WaveIdTableRef");

            //Init the reference to the instrument reference table.
            FileWriter.InitReference(bw, "InstrumentReferenceTableRef");

            //Write the instrument reference table.
            if (Instruments != null)
            {
                //Write the reference.
                FileWriter.CloseReference(bw, ReferenceTypes.Table_Reference, "InstrumentReferenceTableRef");

                //Reference table is a structure.
                FileWriter.StartStructure(bw);

                //Init the instrument reference table.
                FileWriter.InitReferenceTable(bw, Instruments.Count, "InstrumentReferenceTable");

                //Write each instrument.
                for (int i = 0; i < Instruments.Count; i++)
                {
                    //If instrument is null.
                    if (Instruments[i] == null)
                    {
                        //Write the null reference.
                        FileWriter.AddReferenceTableNullReferenceWithType(ReferenceTypes.BNK_Info_Null, "InstrumentReferenceTable");
                    }

                    //Instrument is not null.
                    else
                    {
                        //Add reference.
                        FileWriter.AddReferenceTableReference(bw, ReferenceTypes.BNK_Info_Instrument, "InstrumentReferenceTable");

                        //Instrument is a new structure.
                        FileWriter.StartStructure(bw);

                        //Instrument type reference.
                        FileWriter.InitReference(bw, "InstType");

                        //Get the type of reference.
                        switch (Instruments[i].GetInstrumentType())
                        {
                        //Direct.
                        case InstrumentType.Direct:
                            FileWriter.CloseReference(bw, ReferenceTypes.BNK_RefTable_Direct, "InstType");
                            break;

                        //Index.
                        case InstrumentType.Index:
                            FileWriter.CloseReference(bw, ReferenceTypes.BNK_RefTable_Index, "InstType");
                            break;

                        //Range.
                        case InstrumentType.Range:
                            FileWriter.CloseReference(bw, ReferenceTypes.BNK_RefTable_Range, "InstType");
                            break;
                        }

                        //Instrument is a new structure.
                        FileWriter.StartStructure(bw);

                        //Switch the type of reference.
                        switch (Instruments[i].GetInstrumentType())
                        {
                        //Direct.
                        case InstrumentType.Direct:

                            //Direct.
                            DirectInstrument d = ((DirectInstrument)Instruments[i]);

                            //Write key region reference.
                            FileWriter.InitReference(bw, "KeyRegion");

                            //Null region.
                            if (d.KeyRegion == null)
                            {
                                FileWriter.CloseNullReference(bw, "KeyRegion");
                            }

                            //Not null.
                            else
                            {
                                //Close the reference.
                                FileWriter.CloseReference(bw, ReferenceTypes.BNK_Info_KeyRegion, "KeyRegion");

                                //Write key region.
                                WriteKeyRegion(bw, d.KeyRegion, FileWriter);
                            }
                            break;

                        //Ranged.
                        case InstrumentType.Range:

                            //Set range info.
                            RangeInstrument ran = (RangeInstrument)Instruments[i];

                            //Write stuff.
                            bw.Write((byte)ran.StartNote);
                            bw.Write((byte)ran.EndNote);
                            bw.Write((UInt16)0);

                            //Init each reference.
                            for (int j = 0; j < ran.NoteCount; j++)
                            {
                                FileWriter.InitReference(bw, "Ran" + j);
                            }

                            //Write each key region.
                            for (int j = 0; j < ran.NoteCount; j++)
                            {
                                //Null region.
                                if (ran[j] == null)
                                {
                                    FileWriter.CloseNullReference(bw, "Ran" + j);
                                }

                                //Write key region.
                                else
                                {
                                    //Key region reference.
                                    FileWriter.CloseReference(bw, ReferenceTypes.BNK_Info_KeyRegion, "Ran" + j);

                                    //Write the key region.
                                    WriteKeyRegion(bw, ran[j], FileWriter);
                                }
                            }
                            break;

                        //Index.
                        case InstrumentType.Index:

                            //Set index data.
                            IndexInstrument ind = (IndexInstrument)Instruments[i];

                            //Write the table of indices.
                            bw.Write((uint)ind.Count);
                            for (int j = 0; j < ind.Count; j++)
                            {
                                bw.Write((byte)ind.Keys.ElementAt(j));
                            }

                            //Write padding.
                            FileWriter.Align(bw, 4);

                            //Init each index reference.
                            for (int j = 0; j < ind.Count; j++)
                            {
                                FileWriter.InitReference(bw, "Ind" + j);
                            }

                            //Write each index.
                            for (int j = 0; j < ind.Count; j++)
                            {
                                //Null index.
                                if (ind.Values.ElementAt(j) == null)
                                {
                                    FileWriter.CloseNullReference(bw, "Ind" + j);
                                }

                                //Index exist.
                                else
                                {
                                    //Close reference.
                                    FileWriter.CloseReference(bw, ReferenceTypes.BNK_Info_KeyRegion, "Ind" + j);

                                    //Write the key region.
                                    WriteKeyRegion(bw, ind.Values.ElementAt(j), FileWriter);
                                }
                            }
                            break;
                        }

                        //End the instrument.
                        FileWriter.EndStructure();

                        //End the instrument structure.
                        FileWriter.EndStructure();
                    }
                }

                //Close the instrument reference table.
                FileWriter.CloseReferenceTable(bw, "InstrumentReferenceTable");

                //End the instrument reference table structure.
                FileWriter.EndStructure();
            }
            else
            {
                //Write null offset.
                FileWriter.CloseNullReference(bw, "InstrumentReferenceTableRef");
            }

            //Write the wave id table.
            if (Waves != null)
            {
                //Write the reference.
                FileWriter.CloseReference(bw, ReferenceTypes.Tables, "WaveIdTableRef");

                //Write the wave id table.
                bw.Write((uint)Waves.Count);
                for (int i = 0; i < Waves.Count; i++)
                {
                    Id warId = new Id(SoundTypes.WaveArchive, (uint)Waves[i].WarIndex);
                    warId.Write(ref bw);
                    bw.Write((uint)Waves[i].WaveIndex);
                }
            }

            //Null wave ids.
            else
            {
                FileWriter.CloseNullReference(bw, "WaveIdTableRef");
            }

            //Close the info block.
            FileWriter.CloseBlock(bw);

            //Close the bank file.
            FileWriter.CloseFile(bw);
        }
예제 #2
0
        /// <summary>
        /// Read the bank.
        /// </summary>
        /// <param name="br">The reader.</param>
        public void Read(BinaryDataReader br)
        {
            //Open the bank file.
            FileReader FileReader = new FileReader();

            FileReader.OpenFile(br, out writeMode, out Version);

            //Open info block.
            FileReader.OpenBlock(br, 0);

            //Add the reference to the wave id table.
            FileReader.OpenReference(br, "WaveIdTableRef");

            //Add the reference to the instrument reference table.
            FileReader.OpenReference(br, "InstrumentReferenceTableRef");

            //Jump to the wave id table.
            if (!FileReader.ReferenceIsNull("WaveIdTableRef"))
            {
                FileReader.JumpToReference(br, "WaveIdTableRef");

                //Read the wave id table.
                uint waveIdCount = br.ReadUInt32();
                for (int i = 0; i < waveIdCount; i++)
                {
                    Id   warId = new Id(ref br);
                    uint wavId = br.ReadUInt32();
                    Waves.Add(new WaveArchivePair((int)warId.index, (int)wavId));
                }
            }

            //Null wave ids.
            else
            {
                Waves = null;
            }

            //Close the wave id table reference.
            FileReader.CloseReference("WaveIdTableRef");

            //Jump to the instrument reference table.
            if (!FileReader.ReferenceIsNull("InstrumentReferenceTableRef"))
            {
                FileReader.JumpToReference(br, "InstrumentReferenceTableRef");

                //Reference table is a structure.
                FileReader.StartStructure(br);

                //Read the instrument reference table.
                FileReader.OpenReferenceTable(br, "InstrumentReferenceTable");

                //Read each instrument reference.
                Instruments = new List <IInstrument>();
                for (int i = 0; i < FileReader.ReferenceTableCount("InstrumentReferenceTable"); i++)
                {
                    //If reference is null.
                    if (FileReader.ReferenceTableReferenceIsNull(i, "InstrumentReferenceTable"))
                    {
                        Instruments.Add(null);
                    }

                    //Reference is not null.
                    else
                    {
                        //New instruments list.
                        IInstrument inst = null;

                        //Jump to reference.
                        FileReader.ReferenceTableJumpToReference(br, i, "InstrumentReferenceTable");

                        //Instrument is a new structure.
                        FileReader.StartStructure(br);

                        //Instrument type reference.
                        FileReader.OpenReference(br, "InstType");

                        //Jump to reference if not null.
                        if (!FileReader.ReferenceIsNull("InstType"))
                        {
                            //Jump to the reference.
                            FileReader.JumpToReference(br, "InstType");

                            //Instrument is a new structure.
                            FileReader.StartStructure(br);

                            //Switch the type of reference.
                            switch (FileReader.ReferenceGetType("InstType"))
                            {
                            //Direct.
                            case ReferenceTypes.BNK_RefTable_Direct:

                                //Create direct instrument.
                                DirectInstrument d = new DirectInstrument();

                                //Reference to key region.
                                FileReader.OpenReference(br, "KeyRegionRef");

                                //If key region is not null.
                                if (!FileReader.ReferenceIsNull("KeyRegionRef"))
                                {
                                    //Read key region.
                                    d.KeyRegion = ReadKeyRegion(br, FileReader);
                                }
                                else
                                {
                                    //Add null region.
                                    d.KeyRegion = null;
                                }

                                //Set instrument to the direct.
                                inst = d;

                                //Close reference to key region.
                                FileReader.CloseReference("KeyRegionRef");
                                break;

                            //Ranged.
                            case ReferenceTypes.BNK_RefTable_Range:

                                //New ranged instrument.
                                RangeInstrument ran = new RangeInstrument();

                                //Get count.
                                ran.StartNote = br.ReadByte();
                                ran.EndNote   = br.ReadByte();
                                br.ReadUInt16();

                                //Read each reference.
                                for (int j = 0; j < ran.NoteCount; j++)
                                {
                                    FileReader.OpenReference(br, "Ran" + j);
                                }

                                //Read each key region.
                                for (int j = 0; j < ran.NoteCount; j++)
                                {
                                    //Null region.
                                    if (FileReader.ReferenceIsNull("Ran" + j))
                                    {
                                        ran.Add(null);
                                    }

                                    //Read key region.
                                    else
                                    {
                                        //Jump to reference.
                                        FileReader.JumpToReference(br, "Ran" + j);

                                        //Read the key region.
                                        ran.Add(ReadKeyRegion(br, FileReader));
                                    }
                                }

                                //Close each reference.
                                for (int j = 0; j < ran.NoteCount; j++)
                                {
                                    FileReader.CloseReference("Ran" + j);
                                }

                                //Set the instrument to this.
                                inst = ran;
                                break;

                            //Index.
                            case ReferenceTypes.BNK_RefTable_Index:

                                //Read the table of indices.
                                uint        numInd  = br.ReadUInt32();
                                List <byte> indices = new List <byte>();
                                for (int j = 0; j < numInd; j++)
                                {
                                    indices.Add(br.ReadByte());
                                }

                                //Read padding.
                                FileReader.SeekAlign(br, 4);

                                //New index instrument.
                                IndexInstrument ind = new IndexInstrument();

                                //Read each index reference.
                                for (int j = 0; j < numInd; j++)
                                {
                                    FileReader.OpenReference(br, "Ind" + j);
                                }

                                //Read each index.
                                for (int j = 0; j < numInd; j++)
                                {
                                    //Null index.
                                    if (FileReader.ReferenceIsNull("Ind" + j))
                                    {
                                        ind.Add((sbyte)indices[j], null);
                                    }

                                    //Index exist.
                                    else
                                    {
                                        //Jump to reference.
                                        FileReader.JumpToReference(br, "Ind" + j);

                                        //Read the key region.
                                        ind.Add((sbyte)indices[j], ReadKeyRegion(br, FileReader));
                                    }
                                }

                                //Close each index reference.
                                for (int j = 0; j < numInd; j++)
                                {
                                    FileReader.CloseReference("Ind" + j);
                                }

                                //Set the instrument to this.
                                inst = ind;
                                break;
                            }

                            //End the instrument.
                            FileReader.EndStructure();
                        }

                        //Add instrument.
                        Instruments.Add(inst);

                        //Close the instrument type reference.
                        FileReader.CloseReference("InstType");

                        //End the instrument structure.
                        FileReader.EndStructure();
                    }
                }

                //Close the instrument reference table.
                FileReader.CloseReferenceTable("InstrumentReferenceTable");

                //End the instrument reference table structure.
                FileReader.EndStructure();
            }
            else
            {
                Instruments = null;
            }

            //Close the instrument reference table reference.
            FileReader.CloseReference("InstrumentReferenceTableRef");

            //Close the info block.
            FileReader.CloseBlock(br);

            //Close the bank file.
            FileReader.CloseFile(br);
        }