home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / linux / mikmod-3.000 / mikmod-3 / mikmod-3.1.2 / loaders / load_amf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-07  |  11.4 KB  |  481 lines

  1. /*    MikMod sound library
  2.     (c) 1998 Miodrag Vallat and others - see file AUTHORS for complete list
  3.  
  4.     This library is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU Library General Public License as
  6.     published by the Free Software Foundation; either version 2 of
  7.     the License, or (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public
  15.     License along with this library; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /*==============================================================================
  20.  
  21.   $Id: load_amf.c,v 1.16 1998/12/07 06:00:11 miod Exp $
  22.  
  23.   DMP Advanced Module Format loader
  24.  
  25. ==============================================================================*/
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30.  
  31. #include <string.h>
  32.  
  33. #include <mikmod_internals.h>
  34.  
  35. /*========== Module structure */
  36.  
  37. typedef struct AMFHEADER {
  38.     UBYTE id[3];            /* AMF file marker */
  39.     UBYTE version;            /* upper major, lower nibble minor version number */
  40.     CHAR  songname[32];        /* ASCIIZ songname */
  41.     UBYTE numsamples;        /* number of samples saved */
  42.     UBYTE numorders;
  43.     UWORD numtracks;        /* number of tracks saved */
  44.     UBYTE numchannels;        /* number of channels used  */
  45.     SBYTE panpos[32];        /* voice pan positions */
  46.     UBYTE songbpm;
  47.     UBYTE songspd;
  48. } AMFHEADER;
  49.  
  50. typedef struct AMFSAMPLE {
  51.     UBYTE type;
  52.     CHAR  samplename[32];
  53.     CHAR  filename[13];
  54.     ULONG offset;
  55.     ULONG length;
  56.     UWORD c2spd;
  57.     UBYTE volume;
  58.     ULONG reppos;
  59.     ULONG repend;
  60. } AMFSAMPLE;
  61.  
  62. typedef struct AMFNOTE {
  63.     UBYTE note,instr,volume,fxcnt;
  64.     UBYTE effect[3];
  65.     SBYTE parameter[3];
  66. } AMFNOTE;
  67.  
  68. /*========== Loader variables */
  69.  
  70. static AMFHEADER *mh = NULL;
  71. #define AMFTEXTLEN 22
  72. static CHAR AMF_Version[AMFTEXTLEN+1] = "DSMI Module Format 0.0";
  73. static AMFNOTE *track = NULL;
  74.  
  75. /*========== Loader code */
  76.  
  77. BOOL AMF_Test(void)
  78. {
  79.     UBYTE id[3],ver;
  80.  
  81.     if(!_mm_read_UBYTES(id,3,modfp)) return 0;
  82.     if(memcmp(id,"AMF",3)) return 0;
  83.  
  84.     ver=_mm_read_UBYTE(modfp);
  85.     if((ver>=10)&&(ver<=14)) return 1;
  86.     return 0;
  87. }
  88.  
  89. BOOL AMF_Init(void)
  90. {
  91.     if(!(mh=(AMFHEADER*)_mm_malloc(sizeof(AMFHEADER)))) return 0;
  92.     if(!(track=(AMFNOTE*)_mm_calloc(64,sizeof(AMFNOTE)))) return 0;
  93.  
  94.     return 1;
  95. }
  96.  
  97. void AMF_Cleanup(void)
  98. {
  99.     if(mh) free(mh);
  100.     if(track) free(track);
  101.  
  102.     mh = NULL;
  103.     track = NULL;
  104. }
  105.  
  106. BOOL AMF_UnpackTrack(FILE* modfp)
  107. {
  108.     ULONG tracksize;
  109.     UBYTE row,cmd;
  110.     SBYTE arg;
  111.  
  112.     /* empty track */
  113.     memset(track,0,64*sizeof(AMFNOTE));
  114.  
  115.     /* read packed track */
  116.     if (modfp) {
  117.         tracksize=_mm_read_I_UWORD(modfp);
  118.         tracksize+=((ULONG)_mm_read_UBYTE(modfp))<<16;
  119.         if (tracksize)
  120.             while(tracksize--) {
  121.                 row=_mm_read_UBYTE(modfp);
  122.                 cmd=_mm_read_UBYTE(modfp);
  123.                 arg=_mm_read_SBYTE(modfp);
  124.                 /* unexpected end of track */
  125.                 if(!tracksize) {
  126.                     if((row==0xff)&&(cmd==0xff)&&(arg==-1))
  127.                         break;
  128.                     /* the last triplet should be FF FF FF, but this is not
  129.                        always the case... maybe a bug in m2amf ? 
  130.                     else
  131.                         return 0;
  132.                     */
  133.  
  134.                 }
  135.                 /* invalid row (probably unexpected end of row) */
  136.                 if (row>=64)
  137.                     return 0;
  138.                 if (cmd<0x7f) {
  139.                     /* note, vol */
  140.                     track[row].note=cmd;
  141.                     track[row].volume=(UBYTE)arg+1;
  142.                 } else
  143.                   if (cmd==0x7f) {
  144.                     /* duplicate row */
  145.                     if ((arg<0)&&(row+arg>=0)) {
  146.                         memcpy(track+row,track+(row+arg),sizeof(AMFNOTE));
  147.                     }
  148.                 } else
  149.                   if (cmd==0x80) {
  150.                     /* instr */
  151.                     track[row].instr=arg+1;
  152.                 } else
  153.                   if (cmd==0x83) {
  154.                     /* volume without note */
  155.                     track[row].volume=(UBYTE)arg+1;
  156.                 } else 
  157.                   if(track[row].fxcnt<3) {
  158.                     /* effect, param */
  159.                     if(cmd>0x97)
  160.                         return 0;
  161.                     track[row].effect[track[row].fxcnt]=cmd&0x7f;
  162.                     track[row].parameter[track[row].fxcnt]=arg;
  163.                     track[row].fxcnt++;
  164.                 } else
  165.                     return 0;
  166.             }
  167.     }
  168.     return 1;
  169. }
  170.  
  171. UBYTE* AMF_ConvertTrack(void)
  172. {
  173.     int row;
  174.  
  175.     /* convert track */
  176.     UniReset();
  177.     for (row=0;row<64;row++) {
  178.         if (track[row].instr)  UniInstrument(track[row].instr-1);
  179.         if (track[row].note>OCTAVE) UniNote(track[row].note-OCTAVE);
  180.         if (track[row].volume) UniVolEffect(VOL_VOLUME,track[row].volume-1);
  181.  
  182.         /* AMF effects */
  183.         while(track[row].fxcnt--) {
  184.             SBYTE inf=track[row].parameter[track[row].fxcnt];
  185.  
  186.             switch(track[row].effect[track[row].fxcnt]) {
  187.                 case 1: /* Set speed */
  188.                     UniWrite(UNI_S3MEFFECTA);
  189.                     UniWrite(inf);
  190.                     break;
  191.                 case 2: /* Volume slide */
  192.                     UniWrite(UNI_S3MEFFECTD);
  193.                     if (inf>=0)
  194.                         UniWrite((inf&0xf)<<4);
  195.                     else
  196.                         UniWrite((-inf)&0xf);
  197.                     break;
  198.                 /* effect 3, set channel volume, done in UnpackTrack */
  199.                 case 4: /* Porta up/down */
  200.                     UniWrite(inf>=0?UNI_S3MEFFECTE:UNI_S3MEFFECTF);
  201.                     UniWrite(inf>=0?inf:-inf);
  202.                     break;
  203.                 /* effect 5, "Porta abs", not supported */
  204.                 case 6: /* Porta to note */
  205.                     UniWrite(UNI_ITEFFECTG);
  206.                     UniWrite(inf);
  207.                     break;
  208.                 case 7: /* Tremor */
  209.                     UniWrite(UNI_S3MEFFECTI);
  210.                     UniWrite(inf);
  211.                     break;
  212.                 case 8: /* Arpeggio */
  213.                     UniPTEffect(0x0,inf);
  214.                     break;
  215.                 case 9: /* Vibrato */
  216.                     UniPTEffect(0x4,inf);
  217.                     break;
  218.                 case 0xa: /* Porta + Volume slide */
  219.                     UniPTEffect(0x3,0);
  220.                     UniWrite(UNI_S3MEFFECTD);
  221.                     if (inf>=0)
  222.                         UniWrite((inf&0xf)<<4);
  223.                     else
  224.                         UniWrite((-inf)&0xf);
  225.                     break;
  226.                 case 0xb: /* Vibrato + Volume slide */
  227.                     UniPTEffect(0x4,0);
  228.                     UniWrite(UNI_S3MEFFECTD);
  229.                     if (inf>=0)
  230.                         UniWrite((inf&0xf)<<4);
  231.                     else
  232.                         UniWrite((-inf)&0xf);
  233.                     break;
  234.                 case 0xc: /* Pattern break (in hex) */
  235.                     UniPTEffect(0xd,inf);
  236.                     break;
  237.                 case 0xd: /* Pattern jump */
  238.                     UniPTEffect(0xb,inf);
  239.                     break;
  240.                 /* effect 0xe, "Sync", not supported */
  241.                 case 0xf: /* Retrig */
  242.                     UniWrite(UNI_S3MEFFECTQ);
  243.                     UniWrite(inf&0xf);
  244.                     break;
  245.                 case 0x10: /* Sample offset */
  246.                     UniPTEffect(0x9,inf);
  247.                     break;
  248.                 case 0x11: /* Fine volume slide */
  249.                     UniWrite(UNI_S3MEFFECTD);
  250.                     if (inf>=0)
  251.                         UniWrite((inf&0xf)<<4|0xf);
  252.                     else
  253.                         UniWrite(0xf0|((-inf)&0xf));
  254.                     break;
  255.                 case 0x12: /* Fine portamento */
  256.                     UniWrite(inf>=0?UNI_S3MEFFECTE:UNI_S3MEFFECTF);
  257.                     UniWrite(0xf0|((inf>=0?inf:-inf)&0xf));
  258.                     break;
  259.                 case 0x13: /* Delay note */
  260.                     UniPTEffect(0xe,0xd0|(inf&0xf));
  261.                     break;
  262.                 case 0x14: /* Note cut */
  263.                     UniPTEffect(0xc,0);
  264.                     break;
  265.                 case 0x15: /* Set tempo */
  266.                     UniWrite(UNI_S3MEFFECTT);
  267.                     UniWrite(inf);
  268.                     break;
  269.                 case 0x16: /* Extra fine portamento */
  270.                     UniWrite(inf>=0?UNI_S3MEFFECTE:UNI_S3MEFFECTF);
  271.                     UniWrite(0xe0|(((inf>=0?inf:-inf)>>2)&0xf));
  272.                     break;
  273.                 case 0x17: /* Panning */
  274.                     if (inf==100) {
  275.                         /* surround */
  276.                         UniWrite(UNI_ITEFFECTS0);
  277.                         UniWrite(0x91);
  278.                     } else
  279.                         UniPTEffect(0x8,(inf==64)?255:(inf+64)<<1);
  280.                     break;
  281.             }
  282.             
  283.         }
  284.         UniNewline();
  285.     }
  286.     return UniDup();
  287. }
  288.  
  289. BOOL AMF_Load(BOOL curious)
  290. {
  291.     int t,realtrackcnt,realsmpcnt;
  292.     AMFSAMPLE s;
  293.     SAMPLE *q;
  294.     UWORD *track_remap;
  295.     ULONG samplepos;
  296.  
  297.     /* try to read module header  */
  298.     _mm_read_UBYTES(mh->id,3,modfp);
  299.     mh->version     =_mm_read_UBYTE(modfp);
  300.     _mm_read_string(mh->songname,32,modfp);
  301.     mh->numsamples  =_mm_read_UBYTE(modfp);
  302.     mh->numorders   =_mm_read_UBYTE(modfp);
  303.     mh->numtracks   =_mm_read_I_UWORD(modfp);
  304.     mh->numchannels =_mm_read_UBYTE(modfp);
  305.     if((!mh->numchannels)||(mh->numchannels>(mh->version>=14?32:16))) {
  306.         _mm_errno=MMERR_NOT_A_MODULE;
  307.         return 0;
  308.     }
  309.     memset(mh->panpos,0,32);
  310.     _mm_read_SBYTES(mh->panpos,(mh->version>=14)?32:16,modfp);
  311.     if (mh->version>=14) {
  312.         mh->songbpm=_mm_read_UBYTE(modfp);
  313.         if(mh->songbpm<32) {
  314.             _mm_errno=MMERR_NOT_A_MODULE;
  315.             return 0;
  316.         }
  317.         mh->songspd=_mm_read_UBYTE(modfp);
  318.         if(mh->songspd>32) {
  319.             _mm_errno=MMERR_NOT_A_MODULE;
  320.             return 0;
  321.         }
  322.     } else {
  323.         mh->songbpm=125;
  324.         mh->songspd=6;
  325.     }
  326.  
  327.     if(feof(modfp)) {
  328.         _mm_errno = MMERR_LOADING_HEADER;
  329.         return 0;
  330.     }
  331.  
  332.     /* set module variables */
  333.     of.initspeed = mh->songspd;
  334.     of.inittempo = mh->songbpm;
  335.     AMF_Version[AMFTEXTLEN-3]='0'+(mh->version/10);
  336.     AMF_Version[AMFTEXTLEN-1]='0'+(mh->version%10);
  337.     of.modtype   = strdup(AMF_Version);
  338.     of.numchn    = mh->numchannels;
  339.     of.numtrk    = mh->numorders*mh->numchannels;
  340.     if(mh->numtracks>of.numtrk) of.numtrk=mh->numtracks;
  341.     of.songname  = DupStr(mh->songname,32);
  342.     of.numpos    = mh->numorders;
  343.     of.numpat    = mh->numorders;
  344.     of.reppos    = 0;
  345.     of.flags    |= UF_S3MSLIDES;
  346.     for(t=0;t<32;t++) {
  347.         if(mh->panpos[t]==100) of.panning[t]=PAN_SURROUND;
  348.         else if(mh->panpos[t]==64) of.panning[t]=255;
  349.         else of.panning[t]=(mh->panpos[t]+64)<<1;
  350.     }
  351.     of.numins=of.numsmp=mh->numsamples;
  352.  
  353.     if(!AllocPositions(of.numpos)) return 0;
  354.     for(t=0;t<of.numpos;t++)
  355.         of.positions[t]=t;
  356.  
  357.     if(!AllocTracks()) return 0;
  358.     if(!AllocPatterns()) return 0;
  359.  
  360.     /* read AMF order table */
  361.     for (t=0;t<of.numpat;t++) {
  362.         if (mh->version>=14)
  363.             /* track size */
  364.             of.pattrows[t]=_mm_read_I_UWORD(modfp);
  365.         _mm_read_I_UWORDS(of.patterns+(t*of.numchn),of.numchn,modfp);
  366.     }
  367.     if(feof(modfp)) {
  368.         _mm_errno = MMERR_LOADING_HEADER;
  369.         return 0;
  370.     }
  371.  
  372.     /* read sample information */
  373.     if(!AllocSamples()) return 0;
  374.     q=of.samples;
  375.     for(t=0;t<of.numins;t++) {
  376.         /* try to read sample info */
  377.         s.type=_mm_read_UBYTE(modfp);
  378.         _mm_read_string(s.samplename,32,modfp);
  379.         _mm_read_string(s.filename,13,modfp);
  380.         s.offset    =_mm_read_I_ULONG(modfp);
  381.         s.length    =_mm_read_I_ULONG(modfp);
  382.         s.c2spd     =_mm_read_I_UWORD(modfp);
  383.         if(s.c2spd==8368) s.c2spd=8363;
  384.         s.volume    =_mm_read_UBYTE(modfp);
  385.         s.reppos    =_mm_read_I_ULONG(modfp);
  386.         s.repend    =_mm_read_I_ULONG(modfp);
  387.  
  388.         if(feof(modfp)) {
  389.             _mm_errno = MMERR_LOADING_SAMPLEINFO; 
  390.             return 0;
  391.         }
  392.  
  393.         q->samplename = DupStr(s.samplename,32);
  394.         q->speed     = s.c2spd;
  395.         q->volume    = s.volume;
  396.         if (s.type) {
  397.             q->seekpos   = s.offset;
  398.             q->length    = s.length;
  399.             q->loopstart = s.reppos;
  400.             q->loopend   = s.repend;
  401.             if((s.repend-s.reppos)>2) q->flags |= SF_LOOP;
  402.         }
  403.         q++;
  404.     }
  405.  
  406.     /* read track table */
  407.     if(!(track_remap=_mm_calloc(mh->numtracks+1,sizeof(UWORD))))
  408.         return 0;
  409.     _mm_read_I_UWORDS(track_remap+1,mh->numtracks,modfp);
  410.     if(feof(modfp)) {
  411.         free(track_remap);
  412.         _mm_errno=MMERR_LOADING_TRACK;
  413.         return 0;
  414.     }
  415.  
  416.     for(realtrackcnt=t=0;t<=mh->numtracks;t++)
  417.         if (realtrackcnt<track_remap[t])
  418.             realtrackcnt=track_remap[t];
  419.     for(t=0;t<of.numpat*of.numchn;t++)
  420.         of.patterns[t]=(of.patterns[t]<=mh->numtracks)?
  421.                        track_remap[of.patterns[t]]-1:realtrackcnt;
  422.  
  423.     free(track_remap);
  424.  
  425.     /* unpack tracks */
  426.     for(t=0;t<realtrackcnt;t++) {
  427.         if(feof(modfp)) {
  428.             _mm_errno = MMERR_LOADING_TRACK;
  429.             return 0;
  430.         }
  431.         if (!AMF_UnpackTrack(modfp)) {
  432.             _mm_errno = MMERR_LOADING_TRACK;
  433.             return 0;
  434.         }
  435.         if(!(of.tracks[t]=AMF_ConvertTrack()))
  436.             return 0;
  437.     }
  438.     /* add en extra void track */
  439.     UniReset();
  440.     for(t=0;t<64;t++) UniNewline();
  441.     of.tracks[realtrackcnt++]=UniDup();
  442.     for(t=realtrackcnt;t<of.numtrk;t++) of.tracks[t]=NULL;
  443.  
  444.     /* compute sample offsets */
  445.     samplepos=_mm_ftell(modfp);
  446.     for(realsmpcnt=t=0;t<of.numsmp;t++)
  447.         if(realsmpcnt<of.samples[t].seekpos)
  448.             realsmpcnt=of.samples[t].seekpos;
  449.     for(t=1;t<=realsmpcnt;t++) {
  450.         q=of.samples;
  451.         while(q->seekpos!=t) q++;
  452.         q->seekpos=samplepos;
  453.         samplepos+=q->length;
  454.     }
  455.         
  456.     return 1;
  457. }
  458.  
  459. CHAR *AMF_LoadTitle(void)
  460. {
  461.     CHAR s[32];
  462.  
  463.     _mm_fseek(modfp,4,SEEK_SET);
  464.     if(!fread(s,32,1,modfp)) return NULL;
  465.  
  466.     return(DupStr(s,32));
  467. }
  468.  
  469. /*========== Loader information */
  470.  
  471. MLOADER load_amf={
  472.     NULL,
  473.     "AMF",
  474.     "DSMI AMF loader v0.9",
  475.     AMF_Init,
  476.     AMF_Test,
  477.     AMF_Load,
  478.     AMF_Cleanup,
  479.     AMF_LoadTitle
  480. };
  481.