home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / sonido / mod-0.000 / mod-0 / mod / load_mod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-16  |  9.8 KB  |  430 lines

  1. /*
  2.  *  load_mod.c - Loads standard Protracker modules.
  3.  *
  4.  *  (C) 1994 Mikael Nordqvist (d91mn@efd.lth.se, mech@df.lth.se)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <sys/types.h>
  11. #include <fcntl.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/soundcard.h>
  14. #include <sys/ultrasound.h>
  15. #include <limits.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18.  
  19. #include "mod.h"
  20.  
  21. /* External global variables */
  22.  
  23. SEQ_DECLAREBUF();
  24. extern int seqfd, gus_dev;
  25.  
  26. extern struct mod_info M;
  27. extern struct options opt;
  28.  
  29. extern char quit;
  30.  
  31. extern int periodtable[NR_OCTAVES*12];
  32. extern char effect_used[NR_EFX];
  33.  
  34. /* Local functions */
  35. static int process_header(char *);
  36. static int read_patterndata(int);
  37.  
  38. /* Defines and variables used while loading module */
  39.  
  40. #define MOD31_NAME              0
  41. #define MOD31_SAMPLEINFO       20
  42. #define MOD31_SONGLENGTH      950
  43. #define MOD31_RESTARTPOS      951
  44. #define MOD31_PATTERNTABLE    952
  45. #define MOD31_MAGIC          1080
  46. #define MOD31_PATTERNDATA    1084
  47.  
  48. #define MOD31_SAMPLEINFO_SIZE         30
  49.  
  50. static int expected_length, OS15;
  51.  
  52.  
  53. int load_mod(int fd)
  54. {
  55.     unsigned char buf[MOD31_PATTERNDATA];
  56.     char tmpbuf[80];
  57.  
  58.     int i, diff, fatalerr, tmp;
  59.  
  60.     /* The following might fail if the total length of a 15 instrument
  61.      * module is less than MOD31_PATTERNDATA, but that is very
  62.      * unlikely.
  63.      */
  64.     if(read(fd, buf, MOD31_PATTERNDATA) != MOD31_PATTERNDATA) {
  65.     info("Unable to read header from file.\n");
  66.     return 0;
  67.     }
  68.  
  69.     if(!process_header(buf))
  70.     return 0;
  71.  
  72.     /* As a 15 instrument modules has no tag, a -4 offset is needed in
  73.      * that case.
  74.      */
  75.     if(lseek(fd, MOD31_PATTERNDATA+OS15+(OS15 ? -4 : 0), SEEK_SET)
  76.        == -1) {
  77.     info("Unable to read patterndata.\n");
  78.     return 0;
  79.     }
  80.     
  81.     if(!read_patterndata(fd)) {
  82.     info("Unable to read patterndata.\n");
  83.     return 0;
  84.     }
  85.  
  86.     ioctl(seqfd, SNDCTL_SEQ_RESETSAMPLES, &gus_dev);
  87.  
  88.     tmp=0;
  89.     for(i=1; i <=M.nr_samples; ++i)
  90.     if(M.sample[i].length > 4)
  91.         tmp++;
  92.  
  93.     info("\nUploading instruments to GUS...\n");
  94.     
  95.     fatalerr=0;
  96.     for(i=1; i <= M.nr_samples; ++i) {
  97.     if(M.sample[i].length > 4) { /* Only load non-silent samples */
  98.         if(!read_and_upload_sample(fd, i)) {
  99.         fatalerr=1;
  100.         break;
  101.         }
  102.     }
  103.     else { /* Skip sample */
  104.         if(lseek(fd, M.sample[i].length, SEEK_CUR) == -1){
  105.         fatalerr=1;
  106.         break;
  107.         }
  108.     }
  109.     if(quit == QUIT_SIGNAL) /* Bail out if we got QUIT_SIGNAL */
  110.         return 0;
  111.     }
  112.     
  113.     if(fatalerr) {
  114.     diff=expected_length-lseek(fd, 0, SEEK_CUR);
  115.     sprintf(tmpbuf, "File %d bytes short", diff);
  116.     print_status(tmpbuf);
  117.     info("\nShort file (%d bytes missing).\n", diff);
  118.     sleep(1);
  119.     return 0;
  120.     }
  121.     
  122.     /* Check if filelength was as expected. This is done with two 
  123.      * statements to make sure we don't move the filepointer to the 
  124.      * end before checking current position.
  125.      */
  126.  
  127.     diff=-lseek(fd, 0, SEEK_CUR);
  128.     diff+=lseek(fd, 0, SEEK_END);
  129.  
  130.     if(diff) {
  131.     if(diff > 1024*64) {
  132.         print_status("Too many garbagebytes");
  133.         sleep(1);
  134.         return 0;
  135.     }
  136.     else {
  137.         sprintf(tmpbuf, "Found %d garbagebytes", diff);
  138.         print_status(tmpbuf);
  139.         sleep(1);
  140.     }
  141.  
  142.     info("\nFound %d garbagebytes after samples. Ignoring.\n", diff);
  143.     }
  144.  
  145.     print_songname(M.name);
  146.     print_samples(1);
  147.     print_songinfo(M.nr_voices, tmp, "MOD", expected_length,
  148.            M.songlength, M.nr_patterns);
  149.     
  150.     return 1;
  151. }
  152.  
  153.  
  154. static int process_header(char *buf)
  155. {
  156.     char tmpch, *p, typebuf[16], tmpbuf[80];
  157.     int i, tmp;
  158.     struct sample_info *s;
  159.  
  160.     M.nr_tracks=0;
  161.     M.nr_voices=4;     /* Default to 4-channel 31 sample module */
  162.     M.nr_samples=31;
  163.     OS15=0;
  164.  
  165.  
  166.     if(!strncmp((char *)&buf[MOD31_MAGIC], "M.K.", 4)) {
  167.     sprintf(typebuf, "M.K.");
  168.     }
  169.     else if(!strncmp((char *)&buf[MOD31_MAGIC], "M!K!", 4)) {
  170.     sprintf(typebuf, "M!K!");
  171.     }
  172.     else if(!strncmp((char *)&buf[MOD31_MAGIC], "FLT4", 4)) {
  173.     sprintf(typebuf, "FLT4");
  174.     }
  175.     else if(!strncmp((char *)&buf[MOD31_MAGIC], "4CHN", 4)) {
  176.     sprintf(typebuf, "4CHN");
  177.     }
  178.     else if(!strncmp((char *)&buf[MOD31_MAGIC], "6CHN", 4)) {
  179.     sprintf(typebuf, "6CHN");
  180.     M.nr_voices=6;
  181.     }
  182.     else if(!strncmp((char *)&buf[MOD31_MAGIC], "8CHN", 4)) {
  183.     sprintf(typebuf, "8CHN");
  184.     M.nr_voices=8;
  185.     }
  186.     else if(!strncmp((char *)&buf[MOD31_MAGIC], "16CH", 4)) {
  187.     sprintf(typebuf, "16CH"); /* Take Tracker */
  188.     M.nr_voices=16;
  189.     }
  190.     else if(!strncmp((char *)&buf[MOD31_MAGIC], "32CH", 4)) {
  191.     sprintf(typebuf, "32CH"); /* Take Tracker */
  192.     M.nr_voices=32;
  193.     }
  194.     else {
  195.     sprintf(typebuf, "15 sample");
  196.     info("No known tag found, trying 15 instrument module.\n");
  197.         M.nr_samples=15;
  198.     OS15=-16*MOD31_SAMPLEINFO_SIZE; /* Modifier for 15 instrument module */
  199.     }
  200.     
  201.     sprintf(tmpbuf, "Loading MOD (%s)", typebuf);
  202.     print_status(tmpbuf);
  203.     info("Type: '%s'\n", typebuf);
  204.     
  205.     M.sample=(struct sample_info *)malloc((1+M.nr_samples)*
  206.                       sizeof(struct sample_info));
  207.  
  208.     strncpy(M.name, &buf[MOD31_NAME], 20);
  209.     M.name[20]=0;
  210.     fix_string(M.name);
  211.  
  212.     M.songlength=buf[MOD31_SONGLENGTH+OS15];
  213.     M.restartpos=buf[MOD31_RESTARTPOS+OS15];
  214.  
  215.     if(!M.songlength || M.songlength > 127)
  216.     return 0;
  217.     
  218.     memcpy(M.patterntable, &buf[MOD31_PATTERNTABLE+OS15], 128);
  219.     
  220.     print_sample_info_header();
  221.  
  222.     expected_length=0; /* Start counting expected filesize */
  223.     p=&buf[MOD31_SAMPLEINFO];
  224.     
  225.     
  226.     /* Here we make extra checks for 15 instrument modules, as this is where
  227.      * we get if we try to load something not supported, or something not
  228.      * a module.
  229.      */
  230.     
  231.     for(i=1; i <= M.nr_samples; ++i) {
  232.     s=&M.sample[i];
  233.     s->valid=0;              /* Valid isn't set to TRUE until it's
  234.                   * sampledata has been read.
  235.                   */
  236.     strncpy(s->name, p, 22);
  237.     s->name[22]=0;
  238.     p+=22;
  239.     fix_string(s->name);
  240.     
  241.     s->length=SWAPSHORT(p)*2;
  242.     p+=2;
  243.     
  244.     tmpch=(*p++&0x0f);
  245.     s->finetune=(tmpch > 7 ? tmpch|0xf0 : tmpch);
  246.     
  247.     s->volume=*(unsigned char *)p++;
  248.  
  249.     if(s->volume > 64 && M.nr_samples == 15 && !opt.tolerant)
  250.         return 0;  /* Bail */
  251.     
  252.     tmp=SWAPSHORT(p)*2; /* Repeat start */
  253.     p+=2;
  254.     if(s->length && (tmp < 0 || tmp >= s->length)) {
  255.         tmp=0;
  256.     }
  257.     s->repeat_start=tmp;
  258.     
  259.     /* -1 as the sample at repeat_end is played */
  260.     tmp+=SWAPSHORT(p)*2-1;
  261.     p+=2;
  262.  
  263.     if(tmp < s->repeat_start+2)
  264.         tmp=s->repeat_start+1; /* turn off looping */
  265.     else if(tmp >= s->length)
  266.         tmp=s->length-1;       /* truncate loop-end */
  267.  
  268.     s->repeat_end=tmp;
  269.     
  270.     if(s->repeat_end > s->repeat_start+1)
  271.         s->looped=LOOP_FORWARD;
  272.     else
  273.         s->looped=0;
  274.  
  275.     s->bits_16=0;
  276.     s->unsigned_data=0;
  277.     
  278.     expected_length+=s->length;
  279.     
  280.     s->c2freq=-1; /* Use PAL/NTSC */
  281.  
  282.     print_sample_info(i);
  283.     }
  284.     
  285.     p=&buf[MOD31_PATTERNTABLE+OS15];
  286.     for(i=0, tmp=0; i <= 127; ++i) {
  287.     M.patterntable[i]=p[i];
  288.     if(p[i] > tmp)
  289.         tmp=p[i];
  290.     }
  291.     M.nr_patterns=++tmp;
  292.  
  293.     if(M.nr_patterns > 127)
  294.     return 0;
  295.  
  296.     if(M.restartpos >= M.songlength)
  297.     M.restartpos=0;
  298.     
  299.     info("\nVoices: %d  Patterns: %d  Songlength: %d",
  300.      M.nr_voices, M.nr_patterns, M.songlength);
  301.     if(M.restartpos)
  302.     info("  Restartpos: %d\n", M.restartpos);
  303.     else
  304.     info("\n");
  305.     
  306.     for(i=0; i < M.nr_voices; ++i)
  307.     M.panning[i]=get_voice_balance(i);
  308.     
  309.     expected_length+=MOD31_PATTERNDATA+OS15+(OS15 ? -4 : 0)+
  310.     M.nr_patterns*1024;
  311.     
  312.     M.volrange=64;
  313.  
  314.     if(expected_length > 2*1024*1024) /* Sanity check */
  315.     return 0;
  316.  
  317.     return 1;
  318. }
  319.  
  320.  
  321. static int read_patterndata(int fd)
  322. {
  323.     unsigned char buf[4*64*32]; /* 4 bytes, 64 lines, MAX 32 channels */
  324.     unsigned char tmp;
  325.     unsigned int period;
  326.     struct event *e;
  327.     int p, l, v, pos, hinote, lonote;
  328.     struct event events[64*32]; /* 64 lines, 32 channels = 1 pattern */
  329.     struct event *track;
  330.     
  331.     hinote=0;
  332.     lonote=255;
  333.     
  334.     for(v=0; v < NR_EFX; ++v)
  335.     effect_used[v]=0;
  336.  
  337.     for(v=0; v < M.nr_voices; ++v)
  338.     M.track_idx[v]=(int *)malloc(M.nr_patterns*sizeof(int));
  339.  
  340.     /* Allocate for worst case (no identical tracks) */
  341.     M.tracks=(struct event **)malloc(M.nr_patterns*M.nr_voices*
  342.                     sizeof(struct event *));
  343.  
  344.     for(p=0; p < M.nr_patterns; ++p) {
  345.     if(read(fd, buf, 4*64*M.nr_voices) != 4*64*M.nr_voices)
  346.         return 0;
  347.     pos=0;
  348.     track=events;
  349.     for(l=0; l < 64; ++l, track++) {
  350.         for(v=0; v < M.nr_voices; ++v) {
  351.         e=&track[v*64];
  352.         
  353.         e->sample=
  354.             (buf[pos]&0xf0)|((buf[pos+2]>>4)&0x0f);
  355.         tmp=buf[pos+2]&0x0f;
  356.         if(tmp != 0x0e) {
  357.             e->arg=buf[pos+3];
  358.         }
  359.         else {
  360.             tmp=0x10|((buf[pos+3]>>4)&0x0f);
  361.             e->arg=buf[pos+3]&0x0f;
  362.         }
  363.         e->effect=tmp;
  364.  
  365.         if(tmp || e->arg)
  366.             effect_used[tmp]=1;
  367.         
  368.         period=((buf[pos]&0x0f)<<8)|buf[pos+1];
  369.         /* period*4 as we convert X-2 >= X-4 */
  370.         e->note=
  371.             (period ? period2note(period*4) : 0);
  372.         
  373.         if(e->note < BASE_NOTE+2*12 || e->note >= BASE_NOTE+7*12)
  374.             e->note=0; /* Catch any garbage notes */
  375.         
  376.         /* Clear the second effect */
  377.         e->effect2=e->arg2=0;
  378.  
  379.         if(e->note) {
  380.             hinote=MAX(hinote, e->note);
  381.             lonote=MIN(lonote, e->note);
  382.         }
  383.         pos+=4;
  384.         }
  385.     }
  386.     /* Convert pattern to tracks */
  387.     for(v=0; v < M.nr_voices; ++v)
  388.         M.track_idx[v][p]=get_track_idx(&events[v*64]);
  389.     }
  390.  
  391.     if(!opt.low_note) { /* Determine if we should allow octave 0 and 4 */
  392.     if(lonote < BASE_NOTE+3*12 || hinote > BASE_NOTE+6*12-1) {
  393.         opt.low_note=BASE_NOTE+2*12;
  394.         opt.high_note=BASE_NOTE+7*12-1;
  395.     }
  396.     else {
  397.         opt.low_note=BASE_NOTE+3*12;
  398.         opt.high_note=BASE_NOTE+6*12-1;
  399.     }
  400.     }
  401.     else { /* Warn user if he made an invalid choice */
  402.     if(hinote > opt.high_note || lonote < opt.low_note)
  403.         info("Warning: Module contains notes in other octaves than"
  404.          " the allowed.\n         It might be played"
  405.          " incorrectly.\n");
  406.     }
  407.     
  408.     print_used_effects();
  409.  
  410.     return 1;
  411. }
  412.  
  413.  
  414. /*  Balance: 0 is left, 15 is right.
  415.  */
  416.  
  417. int get_voice_balance(int v)
  418. {
  419.     static char bal[]={1, 14, 14, 1};
  420.  
  421.     if(M.format == MODFORMAT_ULT)
  422.     return 7;
  423.     else if(M.format == MODFORMAT_MTM)
  424.     return (v%2 ? 12 : 4);
  425.     else if(opt.mono || (M.nr_voices%4))
  426.     return 7; /* Default to centered */
  427.     else
  428.     return bal[v%4];
  429. }
  430.