home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / amiga / midi / med210.lhw / in.adf / Source / med210src.lzh / med-mod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-18  |  5.9 KB  |  175 lines

  1. /* MED - music editor ⌐ 1989, 1990 by Teijo Kinnunen */
  2. /* med-mod.c: module loading/saving */
  3. #include "med.h"
  4. #include "medproto.h"
  5. #define MMD0ID ((long)'M'<<24|(long)'M'<<16|(long)'D'<<8|(long)'0')
  6. extern UBYTE blocks;
  7. extern BPTR fh;
  8. extern struct Kappale far song;
  9. extern struct Lohko *lohko[];
  10. extern struct Soitin *sample[];
  11. static void ReadMMD0song(struct MMD0song *);
  12. static BOOL FWrite(char *,LONG);
  13. static BOOL FRead(char *,LONG);
  14.  
  15. static BOOL FWrite(char *p,LONG len)
  16. {
  17.     if(Write(fh,p,len) == len) return(FALSE); else return(TRUE);
  18. }
  19.  
  20. static BOOL FRead(char *p,LONG len)
  21. {
  22.     if(Read(fh,p,len) == len) return(FALSE); else return(TRUE);
  23. }
  24.  
  25. struct MMD0song *MakeMMD0song()
  26. {
  27.     struct MMD0song *sng = (struct MMD0song *)AllocMem(
  28.         sizeof(struct MMD0song),MEMF_CLEAR);
  29.     UWORD cnt;
  30.     if(!sng) return(0L);
  31.     for(cnt = 0; cnt < 63; cnt++) {
  32.         sng->sample[cnt].rep = song.sample[cnt].rep;
  33.         sng->sample[cnt].replen = song.sample[cnt].replen;
  34.         sng->sample[cnt].midich = song.sample[cnt].midich;
  35.         sng->sample[cnt].midipreset = song.sample[cnt].midipreset;
  36.         sng->sample[cnt].svol = song.sample[cnt].svol;
  37.         sng->sample[cnt].strans = song.sample[cnt].strans;
  38.     }
  39.     memcpy((void *)(&sng->songlen),(void *)(&song.songlen),264);
  40.     for(cnt = 0; cnt < 16; cnt++) sng->trkvol[cnt] = song.trkvol[cnt];
  41.     sng->mastervol = song.mastervol;
  42.     sng->numblocks = blocks;
  43.     return(sng);
  44. }
  45.  
  46. char *SaveMod(char *name)
  47. {
  48.     UBYTE oldexists = 0,cnt,lastsample = 0;
  49.     LONG array[100];
  50.     LONG currpos = 0; /* current file position (no Seek() required) */
  51.     LONG blkarrpos,smplarrpos,len;
  52.     struct MMD0 mmd0 = {á0 };
  53.     struct MMD0song *msng;
  54.     if(*name == '\0') return(AskName());
  55.     if(IsHere(name)) {
  56.         Ilmoita(fileex);
  57.         oldexists = 1;
  58.         if(!Continue()) return(notsaved);
  59.     }
  60.     for(cnt = 0; cnt < 63; cnt++)
  61.         if(sample[cnt]) lastsample = cnt;
  62.     if(!(fh = Open2(name,MODE_NEWFILE))) return(DISKERR);
  63.     Ilmoita("Saving module...");
  64.     mmd0.id = MMD0ID;
  65.     mmd0.actplayline = -1;
  66.     if(FWrite((char *)(&mmd0),sizeof(struct MMD0))) return(DISKERR);
  67.     currpos += sizeof(struct MMD0);
  68.     if(!(msng = MakeMMD0song())) return(nomem);
  69.     msng->numsamples = lastsample + 1;
  70.     mmd0.song = (struct MMD0song *)currpos;
  71.     if(FWrite((char *)msng,sizeof(struct MMD0song))) {
  72.         FreeMem((void *)msng,sizeof(struct MMD0song));
  73.         return(DISKERR);
  74.     }
  75.     currpos += sizeof(struct MMD0song);
  76.     FreeMem((void *)msng,sizeof(struct MMD0song));
  77.     blkarrpos = currpos; /* remember... */
  78.     if(FWrite((char *)array,4 * blocks)) return(DISKERR);
  79.     currpos += 4 * blocks; /* the array doesn't yet contain */
  80.     smplarrpos = currpos; /* anything useful */
  81.     if(FWrite((char *)array,4 * (lastsample + 1))) return(DISKERR);
  82.     currpos += 4 * (lastsample + 1);
  83.     for(cnt = 0; cnt < blocks; cnt++) {
  84.         array[cnt] = currpos;
  85.         len = lohko[cnt]->numtracks * 3 * (lohko[cnt]
  86.             ->lines + 1);
  87.         if(FWrite((char *)lohko[cnt],MMDBLKHDRSZ)) return(DISKERR);
  88.         if(FWrite(lohko[cnt]->music,len)) return(DISKERR);
  89.         currpos += len + MMDBLKHDRSZ;
  90.     }
  91.     Seek(fh,blkarrpos,OFFSET_BEGINNING); /* write the block ptrs */
  92.     if(FWrite((char *)array,4 * blocks)) return(DISKERR);
  93.     Seek(fh,0,OFFSET_END); /* all right, and now back to the end */
  94.     for(cnt = 0; cnt <= lastsample; cnt++)
  95.         if(sample[cnt]) {
  96.             array[cnt] = currpos;
  97.             len = sample[cnt]->length + sizeof(struct Soitin);
  98.             if(FWrite((char *)sample[cnt],len)) return(DISKERR);
  99.             currpos += len;
  100.         } else array[cnt] = 0L;
  101.     Seek(fh,smplarrpos,OFFSET_BEGINNING); /* and the sample ptrs */
  102.     if(FWrite((char *)array,4 * (lastsample + 1))) return(DISKERR);
  103.     Seek(fh,0,OFFSET_END);
  104.     mmd0.blockarr = (struct Lohko **)blkarrpos;
  105.     mmd0.smplarr = (struct Soitin **)smplarrpos;
  106.     mmd0.modlen = Seek(fh,0,OFFSET_BEGINNING);
  107.     if(FWrite((char *)(&mmd0),sizeof(struct MMD0))) return(DISKERR);
  108.     Close(fh); fh = 0L;
  109.     if(!oldexists) InsertSavedFile(name);
  110.     Ilmoita("Module saved.");
  111.     return(NOERR);
  112. }
  113.  
  114. static void ReadMMD0song(struct MMD0song *sng)
  115. {
  116.     UWORD cnt;
  117.     for(cnt = 0; cnt < 63; cnt++) {
  118.         song.sample[cnt].rep = sng->sample[cnt].rep;
  119.         song.sample[cnt].replen = sng->sample[cnt].replen;
  120.         song.sample[cnt].midich = sng->sample[cnt].midich;
  121.         song.sample[cnt].midipreset = sng->sample[cnt].midipreset;
  122.         song.sample[cnt].svol = sng->sample[cnt].svol;
  123.         song.sample[cnt].strans = sng->sample[cnt].strans;
  124.     }
  125.     memcpy((void *)(&song.songlen),(void *)(&sng->songlen),264);
  126.     for(cnt = 0; cnt < 16; cnt++) song.trkvol[cnt] = sng->trkvol[cnt];
  127.     song.mastervol = sng->mastervol;
  128. }
  129.  
  130. char *LoadMod()
  131. {
  132.     struct MMD0 mmd0;
  133.     union { struct MMD0song sng; LONG array[100]; } sd; /* some data */
  134.     UWORD cnt;
  135.     UBYTE blks,samples;
  136.     Ilmoita("Loading module...");
  137.     VapautaSoittimet();
  138.     if(FRead((char *)(&mmd0),sizeof(struct MMD0))) return(DISKERR);
  139.     Seek(fh,(LONG)(mmd0.song),OFFSET_BEGINNING);
  140.     if(FRead((char *)(&sd.sng),sizeof(struct MMD0song))) return(DISKERR);
  141.     ReadMMD0song(&sd.sng);
  142.     blks = sd.sng.numblocks;
  143.     samples = sd.sng.numsamples;
  144.     Seek(fh,(LONG)(mmd0.blockarr),OFFSET_BEGINNING);
  145.     if(FRead((char *)(&sd.array),blks * 4)) return(DISKERR);
  146.     blocks = 0;
  147.     for(cnt = 0; cnt < blks; cnt++) {
  148.         struct MMD0Block blkhdr; /* just a header */
  149.         Seek(fh,sd.array[cnt],OFFSET_BEGINNING);
  150.         if(FRead((char *)(&blkhdr),MMDBLKHDRSZ)) return(DISKERR);
  151.         if(AllocBlock((UWORD)blocks,blkhdr.numtracks,
  152.             (UWORD)((UWORD)blkhdr.lines + 1))) return(nomem);
  153.         blocks++;
  154.         if(FRead(lohko[cnt]->music,3 * ((UWORD)blkhdr.lines + 1) *
  155.             blkhdr.numtracks)) return(DISKERR);
  156.     }
  157.     Seek(fh,(LONG)(mmd0.smplarr),OFFSET_BEGINNING);
  158.     if(FRead((char *)(&sd.array),samples * 4)) return(DISKERR);
  159.     for(cnt = 0; cnt < samples; cnt++) {
  160.         struct Soitin shdr; /* header again */
  161.         if(sd.array[cnt]) {
  162.             stcu_d(song.sample[cnt].sname,cnt + 1);
  163.             Seek(fh,sd.array[cnt],OFFSET_BEGINNING);
  164.             if(FRead((char *)(&shdr),sizeof(struct Soitin))) return(DISKERR);
  165.             if(NewInstrument(shdr.length,(UBYTE)cnt)) return(nomem);
  166.             if(FRead((char *)sample[cnt] + sizeof(struct Soitin),
  167.                 shdr.length)) return(DISKERR);
  168.             sample[cnt]->type = shdr.type;
  169.         }
  170.     }
  171.     song.flags &= ~FLAG_INSTRSATT;
  172.     Ilmoita("Module loaded.");
  173.     return(NOERR);
  174. }
  175.