home *** CD-ROM | disk | FTP | other *** search
/ PC Loisirs 18 / cd.iso / sharewar / mikm202 / source / loaders / 669load.c next >
Encoding:
C/C++ Source or Header  |  1995-09-18  |  3.4 KB  |  225 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "mloader.h"
  6. #include "munitrk.h"
  7.  
  8.  
  9. // Raw 669 header struct:
  10.  
  11. typedef struct S69HEADER{
  12.     UWORD marker;
  13.     char  message[108];
  14.     UBYTE nos;
  15.     UBYTE nop;
  16.     UBYTE looporder;
  17.     UBYTE orders[0x80];
  18.     UBYTE tempos[0x80];
  19.     UBYTE breaks[0x80];
  20. } S69HEADER;
  21.  
  22.  
  23.  
  24. // Raw 669 sampleinfo struct:
  25.  
  26. typedef struct S69SAMPLE{
  27.     char  filename[13];
  28.     long  length;
  29.     long  loopbeg;
  30.     long  loopend;
  31. } S69SAMPLE;
  32.  
  33.  
  34. // Raw 669 Note struct
  35.  
  36. typedef struct S69NOTE{
  37.     UBYTE a,b,c;
  38. } S69NOTE;
  39.  
  40.  
  41.  
  42.  
  43. S69NOTE *s69pat;
  44. static S69HEADER *mh;
  45.  
  46. char *S69_Version[]={
  47.     "669",
  48.     "Extended 669"
  49. };
  50.  
  51.  
  52.  
  53. BOOL S69_Test(void)
  54. {
  55.     UWORD id;
  56.  
  57.     rewind(modfp);
  58.     if(!fread(&id,2,1,modfp)) return 0;
  59.     if(id=='if') return 1;
  60.     if(id=='JN') return 1;
  61.     return 0;
  62. }
  63.  
  64.  
  65.  
  66. BOOL S69_Init(void)
  67. {
  68.     mh=NULL;
  69.     s69pat=NULL;
  70.  
  71.     if(!(s69pat=(S69NOTE *)MyMalloc(64*8*sizeof(S69NOTE)))) return 0;
  72.     if(!(mh=(S69HEADER *)MyCalloc(1,sizeof(S69HEADER)))) return 0;
  73.     return 1;
  74. }
  75.  
  76.  
  77.  
  78. void S69_Cleanup(void)
  79. {
  80.     if(s69pat!=NULL) free(s69pat);
  81.     if(mh!=NULL) free(mh);
  82. }
  83.  
  84.  
  85.  
  86.  
  87. BOOL S69_LoadPatterns(void)
  88. {
  89.     int u,t,s,tracks=0,q;
  90.     UBYTE note,inst,vol,a,b,c;
  91.  
  92.     if(!AllocPatterns()) return 0;
  93.     if(!AllocTracks()) return 0;
  94.  
  95.     for(t=0;t<of.numpat;t++){
  96.  
  97.         of.pattrows[t]=mh->breaks[t]+1;
  98.  
  99.         /* Load the pattern into the temp buffer
  100.            and convert it into the 3-byte format */
  101.  
  102.         if(fread(s69pat,64*8*sizeof(S69NOTE),1,modfp)!=1){
  103.             myerr=ERROR_LOADING_PATTERN;
  104.             return 0;
  105.         }
  106.  
  107.         for(s=0;s<8;s++){
  108.  
  109.             UniReset();
  110.  
  111.             UniPTEffect(0xf,78);
  112.             UniPTEffect(0xf,3);
  113.  
  114.             for(q=0;q<64;q++){
  115.  
  116.                 a=s69pat[(q*8)+s].a;
  117.                 b=s69pat[(q*8)+s].b;
  118.                 c=s69pat[(q*8)+s].c;
  119.  
  120.                 note=a>>2;
  121.                 inst=((a&0x3)<<4)|((b&0xf0)>>4);
  122.                 vol=b&0xf;
  123.  
  124.                 if(note<0x3e){
  125.                     UniInstrument(inst);
  126.                     UniNote(note+24);
  127.                 }
  128.  
  129.                 if(note<0x3f){
  130.                     UniPTEffect(0xc,vol<<2);
  131.                 }
  132.  
  133.                 UniNewline();
  134.             }
  135.             if(!(of.tracks[tracks++]=UniDup())) return 0;
  136.         }
  137.     }
  138.     return 1;
  139. }
  140.  
  141.  
  142. BOOL S69_Load(void)
  143. {
  144.     int t,tracks;
  145.  
  146.     S69SAMPLE s;
  147.     INSTRUMENT *d;
  148.     SAMPLE *q;
  149.  
  150.     rewind(modfp);
  151.  
  152.     // try to read module header
  153.  
  154.     if(!fread(mh,sizeof(S69HEADER),1,modfp)){
  155.         myerr=ERROR_LOADING_HEADER;
  156.         return 0;
  157.     }
  158.  
  159.     /* set module variables */
  160.  
  161.     of.initspeed=6;
  162.     of.inittempo=125;
  163.     of.modtype=strdup(S69_Version[mh->marker=='JN']);
  164.     of.numchn=8;
  165.     of.numpat=mh->nop;
  166.     of.numins=mh->nos;
  167.     of.numtrk=of.numchn*of.numpat;
  168.  
  169.     memcpy(of.positions,mh->orders,0x80);
  170.  
  171.     for(t=0;t<128;t++){
  172.         if(of.positions[t]==0xff) break;
  173.     }
  174.     of.numpos=t;
  175.  
  176.     if(!AllocInstruments()) return 0;
  177.  
  178.     d=of.instruments;
  179.  
  180.     for(t=0;t<of.numins;t++){
  181.  
  182.         d->numsmp=1;
  183.         if(!AllocSamples(d)) return 0;
  184.         q=d->samples;
  185.  
  186.         // try to read sample info
  187.  
  188.         if(!fread(&s,sizeof(S69SAMPLE),1,modfp)){
  189.             myerr=ERROR_LOADING_SAMPLEINFO;
  190.             return 0;
  191.         }
  192.  
  193.         d->insname=DupStr(s.filename,13);
  194.  
  195.         q->seekpos=0;
  196.         q->c2spd=8363;
  197.         q->length=s.length;
  198.         q->loopstart=s.loopbeg;
  199.         q->loopend=(s.loopend<s.length) ? s.loopend : s.length;
  200.  
  201.         q->flags=(s.loopbeg<s.loopend)?SF_LOOP:0;
  202.  
  203.         q->volume=64;
  204. //              d->flags=(d->loopstart<d->loopend)?SF_LOOP:0;
  205.         d++;
  206.     }
  207.  
  208.     if(!S69_LoadPatterns()) return 0;
  209.  
  210.     return 1;
  211. }
  212.  
  213.  
  214.  
  215.  
  216. LOADER s69load={
  217.     NULL,
  218.     "669",
  219.     "669 loader v0.1",
  220.     S69_Init,
  221.     S69_Test,
  222.     S69_Load,
  223.     S69_Cleanup
  224. };
  225.