/// <summary>
        /// Write the file.
        /// </summary>
        /// <param name="w2">The file writer.</param>
        public override void Write(FileWriter w2)
        {
            //Use a RIFF writer.
            using (RiffWriter w = new RiffWriter(w2.BaseStream)) {
                //Init file.
                w.InitFile("DLS ");

                //Instrument count.
                w.StartChunk("colh");
                w.Write((uint)Instruments.Count);
                w.EndChunk();

                //Instruments.
                w.StartListChunk("lins");
                foreach (var inst in Instruments)
                {
                    w.StartListChunk("ins ");
                    w.StartChunk("insh");
                    w.Write((uint)inst.Regions.Count);
                    w.Write(inst.BankId);
                    w.Write(inst.InstrumentId);
                    w.EndChunk();
                    w.StartListChunk("lrgn");
                    foreach (Region r in inst.Regions)
                    {
                        w.StartListChunk("rgn2");
                        w.StartChunk("rgnh");
                        w.Write(r.NoteLow);
                        w.Write(r.NoteHigh);
                        w.Write(r.VelocityLow);
                        w.Write(r.VelocityHigh);
                        w.Write((ushort)(r.DoublePlayback ? 1 : 0));
                        w.Write((ushort)r.KeyGroup);
                        w.Write(r.Layer);
                        w.EndChunk();
                        w.StartChunk("wsmp");
                        w.Write((uint)0x14);
                        w.Write((ushort)r.RootNote);
                        w.Write(r.Tuning);
                        w.Write(r.Gain);
                        uint flags = 0;
                        if (r.NoTruncation)
                        {
                            flags |= 0b1;
                        }
                        if (r.NoCompression)
                        {
                            flags |= 0b10;
                        }
                        w.Write(flags);
                        w.Write((uint)(r.Loops ? 1 : 0));
                        if (r.Loops)
                        {
                            w.Write((uint)0x10);
                            w.Write((uint)(r.LoopAndRelease ? 1 : 0));
                            w.Write(r.LoopStart);
                            w.Write(r.LoopLength);
                        }
                        w.EndChunk();
                        w.StartChunk("wlnk");
                        ushort flg = 0;
                        if (r.PhaseMaster)
                        {
                            flg |= 0b1;
                        }
                        if (r.MultiChannel)
                        {
                            flg |= 0b10;
                        }
                        w.Write(flg);
                        w.Write(r.PhaseGroup);
                        w.Write(r.ChannelFlags);
                        w.Write(r.WaveId);
                        w.EndChunk();
                        w.StartListChunk("lar2");
                        foreach (var a in r.Articulators)
                        {
                            w.StartChunk("art2");
                            w.Write((uint)8);
                            w.Write((uint)a.Connections.Count);
                            foreach (var c in a.Connections)
                            {
                                w.Write((ushort)c.SourceConnection);
                                w.Write(c.ControlConnection);
                                w.Write((ushort)c.DestinationConnection);
                                w.Write((ushort)c.TransformConnection);
                                w.Write(c.Scale);
                            }
                            w.EndChunk();
                        }
                        w.EndChunk();
                        w.EndChunk();
                    }
                    w.EndChunk();
                    if (inst.Name != "")
                    {
                        w.StartListChunk("INFO");
                        w.StartChunk("INAM");
                        w.Write(inst.Name.ToCharArray());
                        w.Write("\0".ToCharArray());
                        int len = inst.Name.Length + 1;
                        while (len % 4 != 0)
                        {
                            w.Write((byte)0);
                            len++;
                        }
                        w.EndChunk();
                        w.EndChunk();
                    }
                    w.EndChunk();
                }
                w.EndChunk();

                //Pointer table initializing.
                w.StartChunk("ptbl");
                w.Write((uint)8);
                w.Write((uint)Waves.Count);
                long ptblStart = w.BaseStream.Position;
                w.Write(new byte[Waves.Count * 4]);
                w.EndChunk();

                //Write waves.
                w.StartListChunk("wvpl");
                long waveTableStart = w.BaseStream.Position;
                int  waveNum        = 0;
                foreach (var wav in Waves)
                {
                    long wBak = w.BaseStream.Position;
                    w.BaseStream.Position = ptblStart + waveNum++ *4;
                    w.Write((uint)(wBak - waveTableStart));
                    w.BaseStream.Position = wBak;
                    w.WriteWave(wav);
                }
                w.EndChunk();

                //Write info.
                w.StartListChunk("INFO");
                w.StartChunk("INAM");
                w.Write("Instrument Set".ToCharArray());
                w.EndChunk();
                w.EndChunk();

                //Close file.
                w.CloseFile();
            }
        }