/// <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); }
/// <summary> /// Read a velocity region. /// </summary> /// <param name="br">The reader.</param> /// <returns>The velocity region.</returns> public VelocityRegion ReadVelocityInfo(BinaryDataReader br, FileReader FileReader) { //Start structure. FileReader.StartStructure(br); //New velocity region. VelocityRegion r = new VelocityRegion(); //First is the wave id index. r.WaveIndex = (int)br.ReadUInt32(); //Flags. FlagParameters f = new FlagParameters(ref br); //Original key. 0x000000KK. r.OriginalKey = (sbyte)(0xFF & SoundArchiveReader.GetFlagValue(f, 0, 60)); //Volume. 0x000000VV. r.Volume = (byte)(0xFF & SoundArchiveReader.GetFlagValue(f, 1, 127)); //Pan. 0x0000SSPP. S = Span, P = Pan. r.Pan = (sbyte)(0xFF & SoundArchiveReader.GetFlagValue(f, 2, 64)); r.SurroundPan = (sbyte)((0xFF00 & SoundArchiveReader.GetFlagValue(f, 2)) >> 8); //Pitch. r.Pitch = BitConverter.ToSingle(BitConverter.GetBytes(SoundArchiveReader.GetFlagValue(f, 3, 0x3F800000)), 0); //Note parameters. 0x00TTGGII. T = Interpolation Type, G = Key group, I = Ignore note off. uint noteParam = SoundArchiveReader.GetFlagValue(f, 4, 0); r.PercussionMode = (noteParam & 0xFF) > 0; r.KeyGroup = (byte)((noteParam & 0xFF00) >> 8); r.InterPolationType = (VelocityRegion.EInterPolationType)((noteParam & 0xFF0000) >> 16); //ADSHR curve offset. if (f.isFlagEnabled[9]) { //ADSHR not null. if (f.flagValues[9] != 0xFFFFFFFF) { //Jump to offset. FileReader.JumpToOffsetManually(br, (int)f.flagValues[9]); //Start structure. FileReader.StartStructure(br); //ADSHR curve reference is here. FileReader.OpenReference(br, "ADSHR"); //Not null reference. if (!FileReader.ReferenceIsNull("ADSHR")) { //Jump to position. FileReader.JumpToReference(br, "ADSHR"); //Read ADSHR curve. r.Attack = br.ReadByte(); r.Decay = br.ReadByte(); r.Sustain = br.ReadByte(); r.Hold = br.ReadByte(); r.Release = br.ReadByte(); } //End structure. FileReader.EndStructure(); //Close reference. FileReader.CloseReference("ADSHR"); } } //End structure. FileReader.EndStructure(); //Return the velocity region. return(r); }
/// <summary> /// Read the sequence data. /// </summary> /// <param name="br">The reader.</param> public void Read(BinaryDataReader br) { //Open file. FileReader FileReader = new FileReader(); FileReader.OpenFile(br, out writeMode, out Version); //Data block. FileReader.OpenBlock(br, 0); //Get size. br.Position -= 4; uint size = br.ReadUInt32(); //Read the sequence data. BE for WiiU and 3ds, LE for NX. Must therefore write a parser. List <byte> tempSeqData = br.ReadBytes((int)size - 8).ToList(); //Trim data. bool foundEnd = false; while (!foundEnd) { //Remove padding stuff. if (tempSeqData[tempSeqData.Count - 1] == 0) { tempSeqData.RemoveAt(tempSeqData.Count - 1); } //Found end. else { foundEnd = true; } } //Close data block. FileReader.CloseBlock(br); //Label block. FileReader.OpenBlock(br, 1); //Open the reference table. FileReader.OpenReferenceTable(br, "Labels"); //Go through all the references. Labels = new List <SequenceLabel>(); for (int i = 0; i < FileReader.ReferenceTableCount("Labels"); i++) { //Reference is null. if (FileReader.ReferenceTableReferenceIsNull(i, "Labels")) { Labels.Add(null); } //Label exist. else { //Jump to label. FileReader.ReferenceTableJumpToReference(br, i, "Labels"); //Start label info structure. FileReader.StartStructure(br); //Get reference to data. FileReader.OpenReference(br, "ToSeqData"); //String size. uint stringSize = br.ReadUInt32(); //String data. string labelNow = new string(br.ReadChars((int)stringSize)); //Get label info. Labels.Add(new SequenceLabel() { Offset = FileReader.ReferenceGetOffset("ToSeqData"), Label = labelNow }); //Close reference. FileReader.CloseReference("ToSeqData"); //End label info structure. FileReader.EndStructure(); } } //Close the reference table. FileReader.CloseReferenceTable("Labels"); //Close label block. FileReader.CloseBlock(br); //Convert labels to dictionary. Dictionary <string, int> publicLabels = new Dictionary <string, int>(); foreach (var e in Labels) { if (e != null) { publicLabels.Add(e.Label, e.Offset); } } //Set sequence data. SequenceData = new SequenceData(); SequenceData.PublicLabelOffsets = publicLabels; Syroot.BinaryData.ByteOrder bo = Syroot.BinaryData.ByteOrder.BigEndian; if (writeMode == WriteMode.NX || writeMode == WriteMode.C_BE) { bo = Syroot.BinaryData.ByteOrder.LittleEndian; } SequenceData.Read(tempSeqData.ToArray(), bo); goodData = tempSeqData.ToArray(); //Close file. FileReader.CloseFile(br); }
/// <summary> /// Return a key region. /// </summary> /// <param name="br">The reader.</param> /// <returns>A key region.</returns> public IKeyRegion ReadKeyRegion(BinaryDataReader br, FileReader FileReader) { //Key region is a structure. FileReader.StartStructure(br); //Set up key region. IKeyRegion keyRegion = null; //Get reference to the velocity type. FileReader.OpenReference(br, "KeyTypeRef"); //Reference is not null. if (!FileReader.ReferenceIsNull("KeyTypeRef")) { //Jump to the reference. FileReader.JumpToReference(br, "KeyTypeRef"); //It's a new structure. FileReader.StartStructure(br); //Switch reference type. switch (FileReader.ReferenceGetType("KeyTypeRef")) { //Direct. case ReferenceTypes.BNK_RefTable_Direct: //New direct key region. DirectKeyRegion dir = new DirectKeyRegion(); //Open reference to velocity region. FileReader.OpenReference(br, "VelRegion"); //Not null velocity region. if (!FileReader.ReferenceIsNull("VelRegion")) { //Jump to the velocity region. FileReader.JumpToReference(br, "VelRegion"); //Open the velocity region. dir.VelocityRegion = ReadVelocityInfo(br, FileReader); } //Close reference to velocity region. FileReader.CloseReference("VelRegion"); //Set the key region. keyRegion = dir; break; //Range. case ReferenceTypes.BNK_RefTable_Range: //New ranged instrument. RangeKeyRegion ran = new RangeKeyRegion(); //Get count. ran.StartVelocity = br.ReadByte(); ran.EndVelocity = br.ReadByte(); br.ReadUInt16(); //Read each reference. for (int j = 0; j < ran.VelocityCount; j++) { FileReader.OpenReference(br, "RanKey" + j); } //Read each key region. for (int j = 0; j < ran.VelocityCount; j++) { //Null region. if (FileReader.ReferenceIsNull("RanKey" + j)) { ran.Add(null); } //Read key region. else { //Jump to reference. FileReader.JumpToReference(br, "RanKey" + j); //Read the velocity region. ran.Add(ReadVelocityInfo(br, FileReader)); } } //Close each reference. for (int j = 0; j < ran.VelocityCount; j++) { FileReader.CloseReference("RanKey" + j); } //Set the key region to this. keyRegion = 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 key region. IndexKeyRegion ind = new IndexKeyRegion(); //Read each index reference. for (int j = 0; j < numInd; j++) { FileReader.OpenReference(br, "IndKey" + j); } //Read each index. for (int j = 0; j < numInd; j++) { //Null index. if (FileReader.ReferenceIsNull("IndKey" + j)) { ind.Add((sbyte)indices[j], null); } //Index exist. else { //Jump to reference. FileReader.JumpToReference(br, "IndKey" + j); //Read the velocity region. ind.Add((sbyte)indices[j], ReadVelocityInfo(br, FileReader)); } } //Close each index reference. for (int j = 0; j < numInd; j++) { FileReader.CloseReference("IndKey" + j); } //Set the key region to this. keyRegion = ind; break; } //End the vel type stuff. FileReader.EndStructure(); } //Close reference to velocity type. FileReader.CloseReference("KeyTypeRef"); //End the velocity region. FileReader.EndStructure(); //Return the velocity region. return(keyRegion); }
/// <summary> /// Read the file. ID War. U32 Index /// </summary> /// <param name="br">The reader.</param> public void Read(BinaryDataReader br) { //Open the file. FileReader FileReader = new FileReader(); FileReader.OpenFile(br, out writeMode, out Version); //Open the info block. FileReader.OpenBlock(br, 0); //Wave id table reference. FileReader.OpenReference(br, "WaveIdTableRef"); //Wave sound data reference table reference. FileReader.OpenReference(br, "WaveSoundDataRefTableRef"); //If table is null. if (FileReader.ReferenceIsNull("WaveIdTableRef")) { Waves = null; } //Go to table. else { //New wave list. Waves = new List <WaveArchivePair>(); //Jump to table. FileReader.JumpToReference(br, "WaveIdTableRef"); //Table is here. UInt32 count = br.ReadUInt32(); for (int i = 0; i < count; i++) { //New id. Id war = new Id(ref br); //Add entry. Waves.Add(new WaveArchivePair((int)war.index, (int)br.ReadUInt32())); } } //Close wave id table reference. FileReader.CloseReference("WaveIdTableRef"); //If reference table is null. if (FileReader.ReferenceIsNull("WaveSoundDataRefTableRef")) { DataItems = null; } //Read reference table. else { //New sound data list. DataItems = new List <WaveSoundDataItem>(); //Jump to the reference table. FileReader.JumpToReference(br, "WaveSoundDataRefTableRef"); //This is a structure. FileReader.StartStructure(br); //Open reference table. FileReader.OpenReferenceTable(br, "WaveSoundDataRefTable"); //Read each entry. for (int i = 0; i < FileReader.ReferenceTableCount("WaveSoundDataRefTable"); i++) { //Reference is null. if (FileReader.ReferenceTableReferenceIsNull(i, "WaveSoundDataRefTable")) { DataItems.Add(null); } //Valid item. else { //Jump to position. FileReader.ReferenceTableJumpToReference(br, i, "WaveSoundDataRefTable"); //Start structure. FileReader.StartStructure(br); //New data. WaveSoundDataItem d = new WaveSoundDataItem(); //Wave sound info reference. FileReader.OpenReference(br, "WaveSoundInfoRef"); //Track info reference table reference. FileReader.OpenReference(br, "TrackInfoRefTableRef"); //Note info reference table reference. FileReader.OpenReference(br, "NoteInfoRefTableRef"); //Wave sound info. if (!FileReader.ReferenceIsNull("WaveSoundInfoRef")) { //Jump to the structure. FileReader.JumpToReference(br, "WaveSoundInfoRef"); //Start structure. FileReader.StartStructure(br); //They are just flags. FlagParameters f = new FlagParameters(ref br); //Flag list: /* * 0 - Pan info. 0x0000SSPP S-Span P-Pan. * 1 - Pitch, float. * 2 - Biquad and LPF info. 0x00VVTTLL L-LPF T-Biquad Type V-Biquad Value. * 8 - Offset to send value. * 9 - Offset to ADSHR curve offset. */ //Pan info. d.Pan = (sbyte)(SoundArchiveReader.GetFlagValue(f, 0, (uint)d.Pan) & 0xFF); d.SurroundPan = (sbyte)(SoundArchiveReader.GetFlagValue(f, 0, (uint)d.SurroundPan & 0xFF00) >> 8); //Pitch. if (f.isFlagEnabled[1]) { d.Pitch = BitConverter.ToSingle(BitConverter.GetBytes(SoundArchiveReader.GetFlagValue(f, 1)), 0); } //LPF. d.LpfFrequency = (sbyte)SoundArchiveReader.GetFlagValue(f, 2, (uint)d.LpfFrequency); //Biquad type. d.BiquadType = (WaveSoundDataItem.EBiquadType)((SoundArchiveReader.GetFlagValue(f, 2, 0) & 0xFF00) >> 8); //Biquad value. d.BiquadValue = (sbyte)((SoundArchiveReader.GetFlagValue(f, 2, 0) & 0xFF0000) >> 16); //Offset to send value. uint sendValueOff = SoundArchiveReader.GetFlagValue(f, 8, 0xFFFFFFFF); if (sendValueOff != 0xFFFFFFFF) { //Send value. All bytes: (Main, Count (3), AuxA, AuxB, AuxC), followed by padding to 8 bytes. FileReader.JumpToOffsetManually(br, (int)sendValueOff); d.SendValue[0] = br.ReadByte(); int sendCount = br.ReadByte(); for (int j = 0; j < 3 && j < sendCount; j++) { d.SendValue[j + 1] = br.ReadByte(); } } //Offset to ADSHR curve. uint adshrOff = SoundArchiveReader.GetFlagValue(f, 9, 0xFFFFFFFF); if (adshrOff != 0xFFFFFFFF) { //Go to the reference. FileReader.JumpToOffsetManually(br, (int)adshrOff); //Start structure. FileReader.StartStructure(br); //Open the reference. FileReader.OpenReference(br, "AdshrRef"); //Jump to reference if not null. if (!FileReader.ReferenceIsNull("AdshrRef")) { //Jump. FileReader.JumpToReference(br, "AdshrRef"); //ADSHR curve. d.Attack = br.ReadByte(); d.Decay = br.ReadByte(); d.Sustain = br.ReadByte(); d.Hold = br.ReadByte(); d.Release = br.ReadByte(); } //Close the reference. FileReader.CloseReference("AdshrRef"); //End structure. FileReader.EndStructure(); } //Reference to ADSHR curve. //Read ADSHR curve. //End structure. FileReader.EndStructure(); } //Close wave sound info reference. FileReader.CloseReference("WaveSoundInfoRef"); //Go to the reference table for tracks. if (!FileReader.ReferenceIsNull("TrackInfoRefTableRef")) { //New list. d.NoteEvents = new List <List <NoteEvent> >(); //Go to offset. FileReader.JumpToReference(br, "TrackInfoRefTableRef"); //Start track info. FileReader.StartStructure(br); //Read the reference table. FileReader.OpenReferenceTable(br, "TrackInfoRefTable"); //Read each track. for (int j = 0; j < FileReader.ReferenceTableCount("TrackInfoRefTable"); j++) { //New null track. List <NoteEvent> track = null; //Reference is not null. if (!FileReader.ReferenceTableReferenceIsNull(j, "TrackInfoRefTable")) { //Jump to track info. FileReader.ReferenceTableJumpToReference(br, j, "TrackInfoRefTable"); //Start structure. FileReader.StartStructure(br); //Note event table reference. FileReader.OpenReference(br, "NoteEventRefTableRef"); //Reference is not null. if (!FileReader.ReferenceIsNull("NoteEventRefTableRef")) { //Make track valid. track = new List <NoteEvent>(); //Jump to the table. FileReader.JumpToReference(br, "NoteEventRefTableRef"); //Start structure. FileReader.StartStructure(br); //Read the reference table. FileReader.OpenReferenceTable(br, "NoteEventRefTable"); //Read each note event. for (int k = 0; k < FileReader.ReferenceTableCount("NoteEventRefTable"); k++) { //Null data. if (FileReader.ReferenceTableReferenceIsNull(j, "NoteEventRefTable")) { track.Add(null); } //Valid note event. else { //Jump to note event. FileReader.ReferenceTableJumpToReference(br, k, "NoteEventRefTable"); //Read note event. NoteEvent e = new NoteEvent(); e.Position = br.ReadSingle(); e.Length = br.ReadSingle(); e.NoteIndex = (int)br.ReadUInt32(); br.ReadUInt32(); //Add note event. track.Add(e); } } //Close the reference table. FileReader.CloseReferenceTable("NoteEventRefTable"); //End structure. FileReader.EndStructure(); } //Close note event table reference. FileReader.CloseReference("NoteEventRefTableRef"); //Ends structure. FileReader.EndStructure(); } //Add track. d.NoteEvents.Add(track); } //Close the reference table. FileReader.CloseReferenceTable("TrackInfoRefTable"); //End track info. FileReader.EndStructure(); } //Null track info. else { d.NoteEvents = null; } //Close track info reference table reference. FileReader.CloseReference("TrackInfoRefTableRef"); //Close note info reference. FileReader.CloseReference("NoteEventRefTableRef"); //Go to the reference table for notes. if (!FileReader.ReferenceIsNull("NoteInfoRefTableRef")) { //New list of notes. d.Notes = new List <NoteInfo>(); //Go to offset. FileReader.JumpToReference(br, "NoteInfoRefTableRef"); //Start note reference table info. FileReader.StartStructure(br); //Open the reference table. FileReader.OpenReferenceTable(br, "NoteInfoRefTable"); //Read each note info. for (int j = 0; j < FileReader.ReferenceTableCount("NoteInfoRefTable"); j++) { //If null info. if (FileReader.ReferenceTableReferenceIsNull(j, "NoteInfoRefTable")) { d.Notes.Add(null); } //Valid note info. else { //Jump to note info. FileReader.ReferenceTableJumpToReference(br, j, "NoteInfoRefTable"); //Read data. d.Notes.Add(ReadNoteInfo(br)); } } //Close the reference table. FileReader.CloseReferenceTable("NoteInfoRefTable"); //End track info. FileReader.EndStructure(); } //Null note info. else { d.Notes = null; } //Close note info reference. FileReader.CloseReference("NoteInfoRefTableRef"); //End structure. FileReader.EndStructure(); //Add data. DataItems.Add(d); } } //Close reference table. FileReader.CloseReferenceTable("WaveSoundDataRefTable"); //End structure. FileReader.EndStructure(); } //Close wave sound data reference table reference. FileReader.CloseReference("WaveSoundDataRefTableRef"); //Close the info block. FileReader.CloseBlock(br); //Close the file. FileReader.CloseFile(br); }