Ejemplo n.º 1
0
        public bool Load()
        {
            try
            {
                //INSTRUMENT *d;
                //SAMPLE *q;
                int inst_num;
                int t, u, v, p;
                int next;
                int i;

                /* try to read module header */

                //MmIO.Instance.ReadStringSBytes(mh.id, 17, this.ModStream);
                mh.id = Reader.ReadString(17);
                //MmIO.Instance.ReadStringSBytes(mh.songname, 21, this.ModStream);
                mh.songname = Reader.ReadString(21);
                //MmIO.Instance.ReadStringSBytes(mh.trackername, 20, this.ModStream);
                mh.trackername = Reader.ReadString(20);

                /*mh.version = MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.headersize = MmIO.Instance._mm_read_I_ULONG(this.ModStream);
                 * mh.songlength = (short)MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.restart = MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.numchn = (short)MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.numpat = (short)MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.numins = (short)MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.flags = (short)MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.tempo = MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * mh.bpm = MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                 * MmIO.Instance.ReadUByteS2(mh.orders, 256, this.ModStream);*/

                mh.version    = Reader.ReadIntelUWord();
                mh.headersize = Reader.ReadIntelULong();
                mh.songlength = (short)Reader.ReadIntelUWord();
                mh.restart    = Reader.ReadIntelUWord();
                mh.numchn     = (short)Reader.ReadIntelUWord();
                mh.numpat     = (short)Reader.ReadIntelUWord();
                mh.numins     = (short)Reader.ReadIntelUWord();
                mh.flags      = (short)Reader.ReadIntelUWord();
                mh.tempo      = Reader.ReadIntelUWord();
                mh.bpm        = Reader.ReadIntelUWord();
                Reader.ReadUBytes(mh.orders, 256);


                //if(feof(this.ModStream)){
                //if (this.ModStream.FilePointer >= this.ModStream.length())
                if (Reader.isEOF())
                {
                    /*MmIO.Instance.myerr = m_.ERROR_LOADING_HEADER;
                     * return false;*/
                    throw new SharpModException(SharpModExceptionResources.ERROR_LOADING_HEADER);
                }

                /* set module variables */

                this._module.InitialSpeed = (short)mh.tempo;
                this._module.InitialTempo = (short)mh.bpm;
                //this.UniModule.ModType = m_.MLoader.DupStr(mh.trackername, 20);
                this._module.ModType       = mh.trackername;// System.Text.UTF8Encoding.UTF8.GetString((byte[])(Array)mh.trackername, 0, 20);
                this._module.ChannelsCount = mh.numchn;
                //this._module.NumPat = mh.numpat;
                //this._module.NumTrk = (short)(this._module.NumPat * this._module.NumChn); /* get number of channels */
                //this.UniModule.SongName = m_.MLoader.DupStr(mh.songname, 20); /* make a cstr of songname */
                this._module.SongName = mh.songname; //System.Text.UTF8Encoding.UTF8.GetString((byte[])(Array)mh.songname, 0, 20); /* make a cstr of songname */
                //this._module.NumPos = mh.songlength; /* copy the songlength */
                this._module.RepPos = (short)mh.restart;
                //this._module.NumIns = mh.numins;
                this._module.Flags |= UniModFlags.UF_XMPERIODS;
                if ((mh.flags & 1) != 0)
                {
                    this._module.Flags |= UniModFlags.UF_LINEAR;
                }

                //memcpy(this.UniModule.positions,mh.orders,256);po
                _module.Positions = new System.Collections.Generic.List <int>(mh.songlength);
                for (t = 0; t < 256; t++)
                {
                    if (t >= mh.songlength)
                    {
                        break;
                    }
                    else
                    {
                        this._module.Positions.Add(mh.orders[t]);
                    }
                }
                //    this.UniModule.Positions[t] = mh.orders[t];


                /*
                 * WHY THIS CODE HERE?? I CAN'T REMEMBER!
                 *
                 * this.UniModule.numpat=0;
                 * for(t=0;t<this.UniModule.numpos;t++){
                 * if(this.UniModule.positions[t]>this.UniModule.numpat) this.UniModule.numpat=this.UniModule.positions[t];
                 * }
                 * this.UniModule.numpat++;*/

                /*if (AllocTracks != null && !AllocTracks())
                 *  return false;
                 * if (AllocPatterns != null && !AllocPatterns())
                 *  return false;*/

                //int numtrk = 0;



                for (t = 0; t < mh.numpat; t++)
                {
                    XMPATHEADER ph = new XMPATHEADER();

                    /*		printf("Reading pattern %d\n",t); */

                    ph.size     = Reader.ReadIntelULong();        //MmIO.Instance._mm_read_I_ULONG(this.ModStream);
                    ph.packing  = Reader.ReadUByte();             //MmIO.Instance.ReadUByte(this.ModStream);
                    ph.numrows  = (short)Reader.ReadIntelUWord(); //MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                    ph.packsize = Reader.ReadIntelUWord();        // MmIO.Instance._mm_read_I_UWORD(this.ModStream);

                    /*		printf("headln:  %ld\n",ph.size); */
                    /*		printf("numrows: %d\n",ph.numrows); */
                    /*		printf("packsize:%d\n",ph.packsize); */

                    //TODO this.UniModule.PattRows[t] = ph.numrows;
                    if (this.AllocPatterns != null && !AllocPatterns(_module, t, ph.numrows))
                    {
                        return(false);
                    }
                    if (this.AllocTracks != null && !AllocTracks(_module.Patterns[t], _module.ChannelsCount))
                    {
                        return(false);
                    }

                    /*
                     * Gr8.. when packsize is 0, don't try to load a pattern.. it's empty.
                     * This bug was discovered thanks to Khyron's module..
                     */

                    //if(!(xmpat=(XMNOTE *)m_.MLoader.MyCalloc(ph.numrows*this.UniModule.numchn,sizeof(XMNOTE)))) return false;
                    xmpat = new XMNOTE[ph.numrows * this._module.ChannelsCount];
                    for (i = 0; i < ph.numrows * this._module.ChannelsCount; i++)
                    {
                        xmpat[i] = new XMNOTE();
                    }

                    for (i = 0; i < ph.numrows * this._module.ChannelsCount; i++)
                    {
                        xmpat[i].note = (short)(xmpat[i].ins = (short)(xmpat[i].vol = (short)(xmpat[i].eff = (short)(xmpat[i].dat = 0))));
                    }


                    if (ph.packsize > 0)
                    {
                        for (u = 0; u < ph.numrows; u++)
                        {
                            for (v = 0; v < this._module.ChannelsCount; v++)
                            {
                                XM_ReadNote(xmpat[(v * ph.numrows) + u]);
                            }
                        }
                    }

                    for (v = 0; v < this._module.ChannelsCount; v++)
                    {
                        //this.UniModule.Tracks[numtrk++] = XM_Convert(xmpat, v * ph.numrows, ph.numrows);
                        this._module.Patterns[t].Tracks[v].Cells    = new System.Collections.Generic.List <PatternCell>(new PatternCell[ph.numrows]);
                        this._module.Patterns[t].Tracks[v].UniTrack = XM_Convert(xmpat, v * ph.numrows, ph.numrows);
                    }

                    xmpat = null;
                }

                if (AllocInstruments != null && !AllocInstruments(_module, mh.numins))
                {
                    return(false);
                }

                //d=this.UniModule.instruments;
                inst_num = 0;

                for (t = 0; t < this._module.Instruments.Count; t++)
                {
                    XMINSTHEADER ih = new XMINSTHEADER();

                    /* read instrument header */

                    ih.size = Reader.ReadIntelULong(); //MmIO.Instance._mm_read_I_ULONG(this.ModStream);
                    //MmIO.Instance.ReadStringSBytes(ih.name, 22, this.ModStream);
                    ih.name   = Reader.ReadString(22);
                    ih.type   = Reader.ReadUByte();             //MmIO.Instance.ReadUByte(this.ModStream);
                    ih.numsmp = (short)Reader.ReadIntelUWord(); //MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                    ih.ssize  = Reader.ReadIntelULong();        //MmIO.Instance._mm_read_I_ULONG(this.ModStream);

                    /*      printf("Size: %ld\n",ih.size);
                     * printf("Name:    %22.22s\n",ih.name);
                     * printf("Samples:%d\n",ih.numsmp);
                     * printf("sampleheadersize:%ld\n",ih.ssize);*/
                    //this.UniModule.Instruments[inst_num].InsName = m_.MLoader.DupStr(ih.name, 22);
                    this._module.Instruments[inst_num].InsName = ih.name;//System.Text.UTF8Encoding.UTF8.GetString((byte[])(Array)ih.name, 0, 22);
                    this._module.Instruments[inst_num].NumSmp  = ih.numsmp;

                    /* if (AllocSamples != null && !AllocSamples((this.UniModule.Instruments[inst_num])))
                     *   return false;*/

                    if (ih.numsmp > 0)
                    {
                        XMPATCHHEADER pth = new XMPATCHHEADER();
                        XMWAVHEADER   wh  = new XMWAVHEADER();

                        Reader.ReadUBytes(pth.what, 96);          //MmIO.Instance.ReadUByteS2(pth.what, 96, this.ModStream);
                        Reader.ReadUBytes(pth.volenv, 48);        //MmIO.Instance.ReadUByteS2(pth.volenv, 48, this.ModStream);
                        Reader.ReadUBytes(pth.panenv, 48);        //MmIO.Instance.ReadUByteS2(pth.panenv, 48, this.ModStream);
                        pth.volpts   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.panpts   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.volsus   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.volbeg   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.volend   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.pansus   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.panbeg   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.panend   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.volflg   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.panflg   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.vibflg   = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.vibsweep = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.vibdepth = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.vibrate  = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                        pth.volfade  = Reader.ReadIntelUWord();   //MmIO.Instance._mm_read_I_UWORD(this.ModStream);
                        Reader.readIntelSWords(pth.reserved, 11); //MmIO.Instance._mm_read_I_SWORDS(pth.reserved, 11, this.ModStream);

                        //memcpy(this.UniModule.instruments[inst_num].samplenumber,pth.what,96);
                        for (i = 0; i < 96; i++)
                        {
                            this._module.Instruments[inst_num].SampleNumber[i] = pth.what[i];
                        }

                        this._module.Instruments[inst_num].VolFade = pth.volfade;

                        /*			printf("Volfade %x\n",this.UniModule.instruments[inst_num].volfade); */

                        //memcpy(this.UniModule.instruments[inst_num].volenv,pth.volenv,24);
                        for (i = 0; i < 6; i++)
                        {
                            this._module.Instruments[inst_num].VolEnv[i].Pos = (short)(pth.volenv[i * 4] + (pth.volenv[i * 4 + 1] << 8));
                            this._module.Instruments[inst_num].VolEnv[i].Val = (short)(pth.volenv[i * 4 + 2] + (pth.volenv[i * 4 + 3] << 8));
                        }

                        /*
                         * for (i = 0; i < 12; i++)
                         * {
                         * byte tmp = ((byte*)this.UniModule.instruments[inst_num].volenv)[i*2];
                         *
                         * ((byte*)this.UniModule.instruments[inst_num].volenv)[i*2] = ((byte*)this.UniModule.instruments[inst_num].volenv)[i*2+1];
                         * ((byte*)this.UniModule.instruments[inst_num].volenv)[i*2+1] = tmp;
                         * }*/

                        this._module.Instruments[inst_num].VolFlg = pth.volflg;
                        this._module.Instruments[inst_num].VolSus = pth.volsus;
                        this._module.Instruments[inst_num].VolBeg = pth.volbeg;
                        this._module.Instruments[inst_num].VolEnd = pth.volend;
                        this._module.Instruments[inst_num].VolPts = pth.volpts;

                        /*			printf("volume points	: %d\n"
                         * "volflg			: %d\n"
                         * "volbeg			: %d\n"
                         * "volend			: %d\n"
                         * "volsus			: %d\n",
                         * this.UniModule.instruments[inst_num].volpts,
                         * this.UniModule.instruments[inst_num].volflg,
                         * this.UniModule.instruments[inst_num].volbeg,
                         * this.UniModule.instruments[inst_num].volend,
                         * this.UniModule.instruments[inst_num].volsus);*/
                        /* scale volume envelope: */

                        for (p = 0; p < 12; p++)
                        {
                            this._module.Instruments[inst_num].VolEnv[p].Val <<= 2;
                            /*				printf("%d,%d,",this.UniModule.instruments[inst_num].volenv[p].pos,this.UniModule.instruments[inst_num].volenv[p].val); */
                        }

                        //memcpy(this.UniModule.instruments[inst_num].panenv,pth.panenv,24);
                        for (i = 0; i < 6; i++)
                        {
                            this._module.Instruments[inst_num].PanEnv[i].Pos = (short)(pth.panenv[i * 4] + (pth.panenv[i * 4 + 1] << 8));
                            this._module.Instruments[inst_num].PanEnv[i].Val = (short)(pth.panenv[i * 4 + 2] + (pth.panenv[i * 4 + 3] << 8));
                        }


                        /*
                         * for (i = 0; i < 12; i++)
                         * {
                         * short tmp = ((byte*)this.UniModule.instruments[inst_num].panenv)[i*2];
                         *
                         * ((byte*)this.UniModule.instruments[inst_num].panenv)[i*2] = ((byte*)this.UniModule.instruments[inst_num].panenv)[i*2+1];
                         * ((byte*)this.UniModule.instruments[inst_num].panenv)[i*2+1] = tmp;
                         * }*/
                        this._module.Instruments[inst_num].PanFlg = pth.panflg;
                        this._module.Instruments[inst_num].PanSus = pth.pansus;
                        this._module.Instruments[inst_num].PanBeg = pth.panbeg;
                        this._module.Instruments[inst_num].PanEnd = pth.panend;
                        this._module.Instruments[inst_num].PanPts = pth.panpts;

                        /*					  printf("Panning points	: %d\n"
                         * "panflg			: %d\n"
                         * "panbeg			: %d\n"
                         * "panend			: %d\n"
                         * "pansus			: %d\n",
                         * this.UniModule.instruments[inst_num].panpts,
                         * this.UniModule.instruments[inst_num].panflg,
                         * this.UniModule.instruments[inst_num].panbeg,
                         * this.UniModule.instruments[inst_num].panend,
                         * this.UniModule.instruments[inst_num].pansus);*/
                        /* scale panning envelope: */

                        for (p = 0; p < 12; p++)
                        {
                            this._module.Instruments[inst_num].PanEnv[p].Val <<= 2;
                            /*				printf("%d,%d,",this.UniModule.instruments[inst_num].panenv[p].pos,this.UniModule.instruments[inst_num].panenv[p].val); */
                        }

                        /*                      for(u=0;u<256;u++){ */
                        /*                              printf("%2.2x ",fgetc(this.ModStream)); */
                        /*                      } */

                        next = 0;

                        for (u = 0; u < ih.numsmp; u++)
                        {
                            //q=&this.UniModule.instruments[inst_num].samples[u];

                            wh.length     = Reader.ReadIntelULong();   //MmIO.Instance._mm_read_I_ULONG(this.ModStream);
                            wh.loopstart  = Reader.ReadIntelULong();   //MmIO.Instance._mm_read_I_ULONG(this.ModStream);
                            wh.looplength = Reader.ReadIntelULong();   //MmIO.Instance._mm_read_I_ULONG(this.ModStream);
                            wh.volume     = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                            wh.finetune   = Reader.ReadSByte();        //MmIO.Instance.ReadSByte(this.ModStream);
                            wh.type       = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                            wh.panning    = Reader.ReadUByte();        //MmIO.Instance.ReadUByte(this.ModStream);
                            wh.relnote    = Reader.ReadSByte();        //MmIO.Instance.ReadSByte(this.ModStream);
                            wh.reserved   = (sbyte)Reader.ReadUByte(); //(sbyte)MmIO.Instance.ReadUByte(this.ModStream);
                            wh.samplename = Reader.ReadString(22);     //MmIO.Instance.ReadStringSBytes(wh.samplename, 22, this.ModStream);

                            /*printf("wav %d:%22.22s\n",u,wh.samplename);*/

                            //this.UniModule.instruments[t].samples[u].samplename = new String(wh.samplename, 0, 0, 22);
                            if (u == this._module.Instruments[t].Samples.Count)
                            {
                                this._module.Instruments[t].Samples.Add(new Sample());
                            }
                            this._module.Instruments[t].Samples[u].SampleName = wh.samplename;//System.Text.UTF8Encoding.UTF8.GetString((byte[])(Array)wh.samplename, 0, wh.samplename.Length).Trim();// SupportClass.SbyteToString(wh.samplename);
                            this._module.Instruments[t].Samples[u].Length     = wh.length;
                            this._module.Instruments[t].Samples[u].LoopStart  = wh.loopstart;
                            this._module.Instruments[t].Samples[u].LoopEnd    = wh.loopstart + wh.looplength;
                            this._module.Instruments[t].Samples[u].Volume     = wh.volume;
                            this._module.Instruments[t].Samples[u].C2Spd      = wh.finetune + 128;
                            this._module.Instruments[t].Samples[u].Transpose  = wh.relnote;
                            this._module.Instruments[t].Samples[u].Panning    = wh.panning;
                            this._module.Instruments[t].Samples[u].SeekPos    = next;
                            this._module.Instruments[t].Samples[u].SampleRate = 22050; // Seems to be good...

                            if ((wh.type & 0x10) != 0)
                            {
                                this._module.Instruments[t].Samples[u].Length    >>= 1;
                                this._module.Instruments[t].Samples[u].LoopStart >>= 1;
                                this._module.Instruments[t].Samples[u].LoopEnd   >>= 1;
                            }

                            next += wh.length;

                            /*                              printf("Type %u\n",wh.type); */
                            /*				printf("Trans %d\n",wh.relnote); */

                            this._module.Instruments[t].Samples[u].Flags |= (SampleFormatFlags.SF_OWNPAN);
                            if ((wh.type & 0x3) != 0)
                            {
                                this._module.Instruments[t].Samples[u].Flags |= (SampleFormatFlags.SF_LOOP);
                            }
                            if ((wh.type & 0x2) != 0)
                            {
                                this._module.Instruments[t].Samples[u].Flags |= (SampleFormatFlags.SF_BIDI);
                            }

                            if ((wh.type & 0x10) != 0)
                            {
                                this._module.Instruments[t].Samples[u].Flags |= (SampleFormatFlags.SF_16BITS);
                            }

                            this._module.Instruments[t].Samples[u].Flags |= (SampleFormatFlags.SF_DELTA);
                            this._module.Instruments[t].Samples[u].Flags |= (SampleFormatFlags.SF_SIGNED);
                        }

                        for (u = 0; u < ih.numsmp; u++)
                        {
                            this._module.Instruments[inst_num].Samples[u].SeekPos += Reader.Tell();//MmIO.Instance.Tell(this.ModStream);
                        }
                        //MmIO.Instance.Seek(this.ModStream, next, SeekEnum.SEEK_CUR);
                        Reader.Seek(next, SeekOrigin.Current);
                    }

                    //d++;
                    inst_num++;
                }


                return(true);
            }
            catch (System.IO.IOException)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public virtual void XM_ReadNote(XMNOTE n)
        {
            try
            {
                short cmp;
                //memset(n,0,sizeof(XMNOTE));
                n.note = (short)(n.ins = (short)(n.vol = (short)(n.eff = (short)(n.dat = 0))));

                //cmp=fgetc(this.ModStream);
                //cmp = (short) this.ModStream.read();
                cmp = Reader.ReadByte();//(short)this.ModStream.ReadByte(); //SupportClass.BulkReader.read(this.ModStream);

                if ((cmp & 0x80) != 0)
                {
                    //					if ((cmp & 1) != 0)
                    //						n.note = (short) this.ModStream.read();
                    //					if ((cmp & 2) != 0)
                    //						n.ins = (short) this.ModStream.read();
                    //					if ((cmp & 4) != 0)
                    //						n.vol = (short) this.ModStream.read();
                    //					if ((cmp & 8) != 0)
                    //						n.eff = (short) this.ModStream.read();
                    //					if ((cmp & 16) != 0)
                    //						n.dat = (short) this.ModStream.read();
                    if ((cmp & 1) != 0)
                    {
                        n.note = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    }
                    if ((cmp & 2) != 0)
                    {
                        n.ins = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    }
                    if ((cmp & 4) != 0)
                    {
                        n.vol = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    }
                    if ((cmp & 8) != 0)
                    {
                        n.eff = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    }
                    if ((cmp & 16) != 0)
                    {
                        n.dat = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    }
                }
                else
                {
                    n.note = cmp;
                    //					n.ins = (short) this.ModStream.read();
                    //					n.vol = (short) this.ModStream.read();
                    //					n.eff = (short) this.ModStream.read();
                    //					n.dat = (short) this.ModStream.read();
                    n.ins = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    n.vol = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    n.eff = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                    n.dat = Reader.ReadByte(); //(short)this.ModStream.ReadByte();//SupportClass.BulkReader.read(this.ModStream);
                }
                if (n.note == -1)
                {
                    n.note = 255;
                }
                if (n.ins == -1)
                {
                    n.ins = 255;
                }
                if (n.vol == -1)
                {
                    n.vol = 255;
                }
                if (n.eff == -1)
                {
                    n.eff = 255;
                }
                if (n.dat == -1)
                {
                    n.dat = 255;
                }
            }
            catch (System.IO.IOException)
            {
            }
        }