home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / audio / modulemanipulation / mod2midi.lha / source / mod2midi.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  39.4 KB  |  1,625 lines

  1. /*
  2.  * MIDIMOD.C - Amiga Module to MIDI file converter
  3.  * (c)opyright Andrew Scott 1993
  4.  *
  5.  * Turbo C 2.0
  6.  *
  7.  * Description: Takes a .mod file and has a good go at converting it to
  8.  *              a .mid file. Equivalents to certain .mod samples can be
  9.  *              set to default to particular MIDI instruments. Multiple
  10.  *              MIDI instrument tables should be supported. Note that .mod
  11.  *              and .mid extensions are at the end of the filename.
  12.  *
  13.  * Author: Andrew Scott (Adrenalin Software)
  14.  *
  15.  * Date: 14/3/1993 ver 0.1
  16.  *       20/4/1993 ver 0.2
  17.  *
  18.  **********************************************************************
  19.  *
  20.  * SAS/C 6.3
  21.  *
  22.  * UpDated: 23/Jan/1994 Raul Sobon (Amiga Version  ooooooooooh Yeah!)
  23.  *
  24.  *
  25.  */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <math.h>   /* don't forget to link in appropriate libraries */
  31.  
  32. #include "textwin.h" /* Simple text-windowing environment */
  33. #include "midimod.h" /* Dialog/Info box messages and definitions */
  34.  
  35. bfile MidFile, ModFile;
  36. char SongName[21];
  37. samps Samples;
  38. unsigned long PosLog[64];
  39. int DrumChann = 9, TempoType = 1, PosI = 0;
  40. string MidFN, ModFN;
  41.  
  42. void OutByte(bfile f, char b)
  43. /* Post: The byte b has been written to the buffer of the file f */
  44. {
  45.     if (f->o == BUFFSIZE) {
  46.         fwrite(f->b, 1, BUFFSIZE, f->f);
  47.         f->o = 0;
  48.   }
  49.     f->b[f->o++] = b;
  50. }
  51.  
  52. void FlushOut(bfile f)
  53. /* Pre: f was opened for writing */
  54. /* Post: The file f has has its buffer flushed */
  55. {
  56.     if (f->o > 0)
  57.         fwrite(f->b, 1, f->o, f->f);
  58.     f->o = 0;
  59. }
  60.  
  61. void CloseOut(bfile f)
  62. /* Pre: f was opened for writing */
  63. /* Post: The file f has been flushed and is now closed */
  64. {
  65.     FlushOut(f);
  66.     fclose(f->f);
  67.     f->f = NULL;
  68. }
  69.  
  70. int OpenOut(bfile f, string fn)
  71. /* Returns: NZ if the file f has been opened for writing with the name fn */
  72. {
  73.     if (f->f != NULL)
  74.         CloseOut(f);
  75.     f->f = fopen(fn, "wb");
  76.     f->o = 0;
  77.     return f->f != NULL;
  78. }
  79.  
  80. unsigned long Beatle(bfile f)
  81. /* Returns: bfile-tell (btell=beatle). The offset from the start */
  82. {
  83.     return ftell(f->f) + f->o;
  84. }
  85.  
  86. unsigned char InByte(bfile f)
  87. /* Pre: f was opened for reading */
  88. /* Returns: The next byte from the file f */
  89. {
  90.     if (f->o == BUFFSIZE && !feof(f->f)) {
  91.         f->r = fread(f->b, 1, BUFFSIZE, f->f);
  92.         if (f->r < BUFFSIZE)
  93.             f->b[f->r] = 0;
  94.         f->o = 0;
  95.     }
  96.     if (f->o < f->r)
  97.          return f->b[f->o++];
  98.     return f->b[f->o];
  99. }
  100.  
  101. void CloseIn(bfile f)
  102. /* Post: The file f is now closed */
  103. {
  104.     fclose(f->f);
  105.     f->f = NULL;
  106. }
  107.  
  108. int OpenIn(bfile f, string fn)
  109. /* Returns: NZ if the file f has been opened for reading with the name fn */
  110. {
  111.     if (f->f != NULL)
  112.         CloseIn(f);
  113.     f->f = fopen(fn, "rb");
  114.     f->o = f->r = BUFFSIZE;
  115.     return f->f != NULL;
  116. }
  117.  
  118. void Inskipp(bfile f, unsigned long n) /* Stainless-steel rat for Pres */
  119. /* Pre: f was opened for reading */
  120. /* Post: f's file pointer has skipped forward n bytes */
  121. {
  122.     n += f->o;
  123.     while (n >= BUFFSIZE && !feof(f->f)) {
  124.         f->r = fread(f->b, 1, BUFFSIZE, f->f);
  125.         if (f->r < BUFFSIZE)
  126.             f->b[f->r] = 0;
  127.         n -= BUFFSIZE;
  128.     }
  129.     f->o = n; /* hmmm.. may cause an error if was eof.. X-fingers */
  130. }
  131.  
  132. struct bpos FPos(bfile f)
  133. /* Returns: All necessary information regarding file f's status */
  134. {
  135.     struct bpos x;
  136.  
  137.     x.d = *f;
  138.     x.p = ftell(f->f);
  139.     return x;
  140. }
  141.  
  142. void FGoto(bfile f, struct bpos x)
  143. /* Pre: x was the status of f previously */
  144. /* Post: File f has had its status changed to x */
  145. {
  146.     fseek(f->f, x.p, SEEK_SET);
  147.     *f = x.d;
  148. }
  149.  
  150. int WriteVLQ(bfile f, unsigned long i)
  151. /*
  152.  * Returns: # of bytes written after a variable-length-quantity equivalent
  153.  *    of i has been written to the file f.
  154.  */
  155. {
  156.     int x = 0;
  157.     unsigned long buffer;
  158.  
  159.     buffer = i & 127;
  160.     while ((i >>= 7) > 0)
  161.         buffer = ((buffer << 8) | 128) + (i & 127);
  162.     while (1) {
  163.         OutByte(f, buffer & 255);
  164.         x++;
  165.         if (buffer & 128)
  166.             buffer >>= 8;
  167.         else
  168.             return x;
  169.     }
  170. }
  171.  
  172. string InitFile(bfile f, string deffn, string t, int inpm)
  173. /* Returns: NULL if file is unacceptable, the filename otherwise. The
  174.  *    filename corresponds to file f, type t, with default filename deffn.
  175.  *    Will open an input file if inpm is NZ. deffn will be freed.
  176.  */
  177. {
  178.     int r = 0;
  179.     string newfn;
  180.  
  181.     _IF[1] = t;
  182.     if (deffn==NULL)
  183.         *(deffn = (string) malloc(1)) = 0;
  184.     newfn = FileSelect(_IF, deffn);
  185.     free(deffn);
  186.     if (! *newfn) {
  187.         free(newfn);
  188.         return NULL;
  189.     }
  190.     if (inpm)
  191.         r = OpenIn(f, newfn);
  192.     else if (fclose(fopen(newfn, "r"))==EOF || InfoBox(_OUTE)=='y')
  193.         r = OpenOut(f, newfn);
  194.     if (!r) {
  195.         free(newfn);
  196.         return NULL;
  197.     }
  198.     return newfn;
  199. }
  200.  
  201. int MKTest(bfile f)
  202. /* Returns: The number of samples in the Module file f */
  203. {
  204.     unsigned long offset;
  205.     int i = 15;
  206.     char s[4];
  207.  
  208.     offset = ftell(f->f);
  209.     if (!fseek(f->f, 1080, SEEK_SET)) {
  210.         fread(s, 1, 4, f->f);
  211.         if (!memcmp(s,"M.K.",4) || !memcmp(s,"M!K!",4) || !memcmp(s,"FLT4",4))
  212.             i = 31;
  213.         else if (!memcmp(s,"FLT8",4))
  214.             i = 0;
  215.     }
  216.     fseek(f->f, offset, SEEK_SET);
  217.     return i;
  218. }
  219.  
  220. string SimplifyName(string s)
  221. /*
  222.  * Returns: A string similar to s, but has had any nasty headers removed
  223.  *    any leading spaces or trailing spaces, and all made lower-case with
  224.  *    any intermediate spaces replaced by underbar-characters
  225.  */
  226. {
  227.     string t, r, x;
  228.  
  229.     x = strchr(s, ':');
  230.     if (x != NULL && x-s == 5 && tolower(s[0])=='s' && tolower(s[1])=='t' &&
  231.      s[2]=='-') {
  232.         r = x = (string) malloc(18);
  233.         t = s + 6;
  234.         while (*(x++) = *(t++)); /* remove soundtracker header */
  235.     } else
  236.         strcpy(r = (string) malloc(1+strlen(s)), s); /* strdup? */
  237.     for (t = r; *t == ' '; t++);
  238.     x = r;
  239.     while (*(x++) = *(t++)); /* remove leading spaces */
  240.     if (*r) {
  241.         x--;
  242.         while (*(--x) == ' ') /* remove trailing spaces */
  243.             *x = 0;
  244.     }
  245.     for (x = r; *x; x++) /* fill intermediate spaces with _ */
  246.         if (*x == ' ')
  247.             *x = '_';
  248.     t = r = (string) realloc(r, strlen(r) + 1);
  249.     for (; *t; t++)
  250.         *t = tolower(*t); /* make the lot lower-case */
  251.     return r;
  252. }
  253.  
  254. string GetLine(FILE *f)
  255. /* Returns: Next line from file f, NULL on error/eof */
  256. {
  257.     string s, t;
  258.     int c;
  259.  
  260.     if ((t = s = (string) malloc(MAXSTRING))==NULL) {
  261.         InfoBox(_OOME);
  262.         return NULL;
  263.     }
  264.     while ((c = fgetc(f)) != EOF && c != '\n')
  265.         *(t++) = c;
  266.     if (s == t) {
  267.         free(s);
  268.         return NULL;
  269.     }
  270.     *t = 0;
  271.     return (string) realloc(s, t-s+1);
  272. }
  273.  
  274. int ReadModSpecs(bfile f, string n, samps s)
  275. /*
  276.  * Returns: Z if f is not a supported Amiga Module, else n is set to
  277.  * be the name of the Module and s is set to hold sample information
  278.  */
  279. {
  280.     unsigned char b, c;
  281.     int i, okmodule;
  282.     string t1;
  283.     samp *t2;
  284.  
  285.     for (i = 20, t1 = n; i--; *(t1++) = InByte(f));
  286.     okmodule = !*n || isprint(*n);
  287.     *t1 = 0;
  288.     c = s->n = MKTest(f);
  289.     okmodule = okmodule && c;
  290.     for (t2 = s->s; c--; t2++) {
  291.         for (i = 22, t1 = t2->n; i--; *(t1++) = InByte(f));
  292.         okmodule = okmodule && (!*(t2->n) || isprint(*(t2->n)));
  293.         *t1 = 0;
  294.         b = InByte(f);
  295.         t2->l = 256 * b + InByte(f);
  296.         if (t2->l < 4)
  297.             t2->l = 0;    /* if the sample is this small, not worth processing */
  298.         b = InByte(f);
  299.         t2->v = InByte(f);
  300.         InByte(f);
  301.         InByte(f);
  302.         b = InByte(f);
  303.         if (256 * b + InByte(f) > 1 && t2->l)
  304.             t2->l = -1;    /* looping: plays 'forever' */
  305.         t2->m = 0;
  306.     }
  307.     return !feof(f->f) && okmodule;
  308. }
  309.  
  310. void ScanSamples(samps s)
  311. /*
  312.  * Post: Sample data for ModFile has been scanned and samples in s have
  313.  * been given "reasonable" values for volume shifting and transpositions
  314.  * I hope :)
  315.  */
  316. {
  317.     samp *sam;
  318.     unsigned int maxpat = 0, i, j, k;
  319.     unsigned char x;
  320.     signed char c[SCANSIZE];      /* look at first SCANSIZE bytes of sample */
  321.     signed char max, min, *p;
  322.     unsigned long offset, len, lenarry[31];
  323.     long sum;
  324.     double ratio, junk;
  325.  
  326.     offset = ftell(ModFile->f);
  327.     fseek(ModFile->f, 42, SEEK_SET);
  328.     for (i = 0; i < s->n; i++) {    /* need to get actual lengths */
  329.         fread(&x, 1, 1, ModFile->f);
  330.         lenarry[i] = 256 * x;
  331.         fread(&x, 1, 1, ModFile->f);
  332.         lenarry[i] += x;
  333.         lenarry[i] *= 2;
  334.         fseek(ModFile->f, 28, SEEK_CUR);
  335.     }
  336.     fseek(ModFile->f, 20 + 30 * s->n, SEEK_SET);
  337.     fread(&x, 1, 1, ModFile->f);
  338.     fread(c, 1, 129, ModFile->f);
  339.     for (; x; x--)
  340.         if (c[x] > maxpat)
  341.             maxpat = c[x];
  342.     len = 1024L * (maxpat + 1) + 30 * s->n + ((s->n > 30) ? 4 : 0) + 150;
  343.     fseek(ModFile->f, len, SEEK_SET);
  344.     for (i = 0, sam = s->s; i < s->n; i++, sam++) {
  345.         sam->t[0] = 0; /* set transposition values to 0 */
  346.         sam->t[1] = 0;
  347.         sam->t[2] = 0;
  348.         sam->a[0] = 0;
  349.         if (!sam->l) {
  350.             sam->a[1] = 1; /* no sample = no volume shifting */
  351.             sam->a[2] = 1;
  352.             fseek(ModFile->f, lenarry[i], SEEK_CUR);
  353.         } else {
  354.             len = min(lenarry[i], SCANSIZE);
  355.             if (!(j = len = fread(c, 1, len, ModFile->f)))
  356.                 len = 1;
  357.             fseek(ModFile->f, lenarry[i] - j, SEEK_CUR);
  358.             min = 127;
  359.             max = -128;
  360.             sum = 0;
  361.             for (p = c; j--; p++) {
  362.                 sum += *p;
  363.                 if (*p > max)
  364.                     max = *p;
  365.                 if (*p < min)
  366.                     min = *p;
  367.             }
  368.             ratio = 1.0 * sum / len;  /* get average .. normally ~0 */
  369.             if (fabs(max - ratio) > fabs(min - ratio))
  370.                 ratio = fabs(min - ratio) / 64.0;
  371.             else
  372.                 ratio = fabs(max - ratio) / 64.0;
  373.             j = k = 1;
  374.             while (k++ < 15)    /* now find decent rational approx. */
  375.                 if (fabs(modf(ratio * k, &junk)-.5) > fabs(modf(ratio * j, &junk)-.5))
  376.                     j = k;
  377.             if (!(sam->a[1] = ratio * j + 0.5)) {
  378.                 sam->a[1] = 1;   /* if really small scaling needed.. */
  379.                 sam->a[2] = 16;
  380.             } else
  381.                 sam->a[2] = j;
  382.         }
  383.     }
  384.     fseek(ModFile->f, offset, SEEK_SET);
  385. }
  386.  
  387. int SetDefaults(samps s, string fn)
  388. /*
  389.  * Returns: NZ if the samples in s have been sucessfully allocated default
  390.  *    values corresponding to definitions in the DEF_MAPFILE file, and from
  391.  *    a .mm file corresponding to the filename fn
  392.  */
  393. {
  394.     FILE *f;
  395.     char i, m[MAXSTRING];
  396.     int d, e[6], v;
  397.     samp *sam;
  398.     string n, t;
  399.     bfile mmf;
  400.  
  401.     t = strchr(strcpy(m, fn), '.');
  402.     if (t==NULL) {
  403.         i = strlen(m);
  404.         m[i] = '.';
  405.     } else
  406.         i = t-m;
  407.     m[++i] = 'm';
  408.     m[++i] = 'm';
  409.     m[++i] = 0;
  410.     mmf->f = NULL;
  411.     if (OpenIn(mmf, m)) {
  412.         for (i = s->n, sam = s->s; i--; sam++) {
  413.             sam->m = InByte(mmf);
  414.             sam->t[0] = (signed char) InByte(mmf);
  415.             sam->t[1] = (signed char) InByte(mmf);
  416.             sam->t[2] = (signed char) InByte(mmf);
  417.             sam->a[0] = (signed char) InByte(mmf);
  418.             sam->a[1] = (signed char) InByte(mmf);
  419.             sam->a[2] = (signed char) InByte(mmf);
  420.         }
  421.         CloseIn(mmf);
  422.     } else
  423.         ScanSamples(s);
  424.     if ((f = fopen(DEF_MAPFILE, "rt"))==NULL) {
  425.         _NOFIL[3] = DEF_MAPFILE;
  426.         InfoBox(_NOFIL);
  427.         return 0;
  428.     }
  429.     i = s->n;
  430.     for (sam = s->s; i--; sam++)
  431.         if (sam->l) {
  432.             n = SimplifyName(sam->n);
  433.             t = GetLine(f);
  434.             e[3] = 0;       /* These are the default volume constants */
  435.             e[4] = 1;
  436.             e[5] = 1;
  437.             sscanf(t, "%s %d %d %d %d %d %d %d", m, &d, &e[0], &e[1], &e[2],
  438.                 &e[3], &e[4], &e[5]);
  439.             if ((v = strcmp(m, n))>0) {
  440.                 rewind(f);
  441.                 free(t);
  442.                 t = GetLine(f);
  443.                 e[3] = 0;
  444.                 e[4] = 1;
  445.                 e[5] = 1;
  446.                 sscanf(t, "%s %d %d %d %d %d %d %d", m, &d, &e[0], &e[1], &e[2],
  447.                     &e[3], &e[4], &e[5]);
  448.                 v = strcmp(m, n);
  449.             }
  450.             free(t);
  451.             while (v < 0 && (t = GetLine(f)) != NULL) {
  452.                 e[3] = 0;
  453.                 e[4] = 1;
  454.                 e[5] = 1;
  455.                 sscanf(t, "%s %d %d %d %d %d %d %d", m, &d, &e[0], &e[1], &e[2],
  456.                     &e[3], &e[4], &e[5]);
  457.                 free(t);
  458.                 v = strcmp(m, n);
  459.             }
  460.             if (!v) {
  461.                 sam->m = d;
  462.                 sam->t[0] = e[0];
  463.                 sam->t[1] = e[1];
  464.                 sam->t[2] = e[2];
  465.                 sam->a[0] = e[3];
  466.                 sam->a[1] = e[4];
  467.                 sam->a[2] = e[5];
  468.             }
  469.             free(n);
  470.         }
  471.     fclose(f);
  472.     return 1;
  473. }
  474.  
  475. void SaveDefaults(samps s, string fn)
  476. /*
  477.  * Post: The samples attributes in s have been written to a .mm file
  478.  *   corresponding to the filename fn
  479.  */
  480. {
  481.     char m[MAXSTRING];
  482.     string t;
  483.     char i;
  484.     bfile mmf;
  485.     samp *sam;
  486.  
  487.     t = strchr(strcpy(m, fn), '.');
  488.     if (t==NULL) {
  489.         i = strlen(m);
  490.         m[i] = '.';
  491.     } else
  492.         i = t-m;
  493.     m[++i] = 'm';
  494.     m[++i] = 'm';
  495.     m[++i] = 0;
  496.     mmf->f = NULL;
  497.     if (!OpenOut(mmf, m))
  498.         return;
  499.     for (i = s->n, sam = s->s; i--; sam++) {
  500.         OutByte(mmf, sam->m);
  501.         OutByte(mmf, sam->t[0]);
  502.         OutByte(mmf, sam->t[1]);
  503.         OutByte(mmf, sam->t[2]);
  504.         OutByte(mmf, sam->a[0]);
  505.         OutByte(mmf, sam->a[1]);
  506.         OutByte(mmf, sam->a[2]);
  507.     }
  508.     CloseOut(mmf);
  509. }
  510.  
  511. void NullArryFree(string *sp)
  512. /* Post: The NULL-terminated array sp is gone */
  513. {
  514.     string *t;
  515.  
  516.     t = sp;
  517.     while (*t != NULL)
  518.         free(*(t++));
  519.     free(sp);
  520. }
  521.  
  522. int Instrument( void )
  523. /* Returns: MIDI instrument selected from file DEF_INSFILE */
  524. {
  525.     static int w = 0, doff = -1;
  526.     static string *sp = NULL;
  527.     int c;
  528.  
  529.     if (sp == NULL) {
  530.         FILE *f;
  531.         string *t, s;
  532.         int x, i = 1;
  533.  
  534.         if ((f = fopen(DEF_INSFILE, "rt"))==NULL) {
  535.             _NOFIL[3] = DEF_INSFILE;
  536.             InfoBox(_NOFIL);
  537.             return -1;
  538.         }
  539.         if ((t = sp = (string *) malloc(257 * sizeof(string)))==NULL) {
  540.             InfoBox(_OOME);
  541.             return -1;
  542.         }
  543.         while ((s = GetLine(f)) != NULL)
  544.             if (x = strlen(s)) { /* ignore blank lines */
  545.                 i++;
  546.                 if (x > w)
  547.                     w = x;
  548.                 *(t++) = s;
  549.                 if (doff < 0 && *s=='D')  /* take note of first drum position */
  550.                     sscanf(s, "D%d ", &doff);
  551.             }
  552.         *t = NULL;
  553.         sp = (string *) realloc(sp, i * sizeof(string)); /* i <= 257 */
  554.         fclose(f);
  555.     }
  556.     c = ScrollChoice("MIDI Instruments", sp, w);
  557.     if (c>127)
  558.         c += doff;
  559.     return c;
  560. }
  561.  
  562. string MIDIVal(samp *sam)
  563. /* Returns: a string representing the MIDI instrument *sam */
  564. {
  565.     string s;
  566.  
  567.     s = (string) malloc(5);
  568.     if (sam->m < 128)
  569.         sprintf(s, "%4d", sam->m);
  570.     else
  571.         sprintf(s, "D%3d", sam->m - 128);
  572.     return s;
  573. }
  574.  
  575. string TransVal(samp *sam)
  576. /* Returns: a string representing the transpose amount of *sam */
  577. {
  578.     string s;
  579.  
  580.     s = (string) malloc(15);
  581.     if (!sam->t[1])
  582.         sprintf(s, "          %4d", sam->t[0]);
  583.     else if (!sam->t[2])
  584.         sprintf(s, "     %4d,%4d", sam->t[0], sam->t[1]);
  585.     else
  586.         sprintf(s, "%4d,%4d,%4d", sam->t[0], sam->t[1], sam->t[2]);
  587.     return s;
  588. }
  589.  
  590. string VolumeVal(samp *sam)
  591. /* Returns: a string representing the volume shifting formula for *sam */
  592. {
  593.     string s;
  594.  
  595.     s = (string) malloc(17);
  596.     if (sam->a[0] < 0)
  597.         sprintf(s, "(? -%3d)*%3d/%3d", abs(sam->a[0]), sam->a[1], sam->a[2]);
  598.     else
  599.         sprintf(s, "(? +%3d)*%3d/%3d", sam->a[0], sam->a[1], sam->a[2]);
  600.     return s;
  601. }
  602.  
  603. string NullVal(samp *sam)
  604. /* Returns: an empty string */
  605. {
  606.     string s;
  607.  
  608.     *(s = (string) malloc(1)) = 0;
  609.     return s;
  610. }
  611.  
  612. int Sample(samps s, string (*MIDIVal)(samp *x), int w)
  613. /* Returns: Sample selected from list of samples, -1 on error */
  614. {
  615.     string *sp, *t, p;
  616.     samp *sam;
  617.     int i;
  618.  
  619.     if ((t = sp = (string *) malloc((s->n + 1) * sizeof(string)))==NULL) {
  620.         InfoBox(_OOME);
  621.         return -1;
  622.     }
  623.     for (i = s->n, sam = s->s; i--; sam++) {
  624.         *t = (string) malloc(25 + w);
  625.         p = MIDIVal(sam);
  626.         sprintf(*t, "%c%-22s %s", (sam->l) ? '*' : ' ', sam->n, p);
  627.         free(p);
  628.         t++;
  629.     }
  630.     *t = NULL;
  631.     i = ScrollChoice("Select Sample", sp, 24 + w);
  632.     NullArryFree(sp);
  633.     return i;
  634. }
  635.  
  636. int ChooseChannels(samps s)
  637. /*
  638.  * Returns: The number of different channels needed to play instruments. If
  639.  *    that number is not greater than 16, upto 16 channels will be allocated
  640.  *    to the samples.
  641.  */
  642. {
  643.     unsigned char c, d = 0, i[128], m, n, numchan;
  644.     samp *sam1, *sam2;
  645.  
  646.     n = s->n;
  647.     sam1 = s->s;
  648.     memset(i, 0, 128);
  649.     for (n = s->n, sam1 = s->s; n--; sam1++) {
  650.         sam1->c = -1;
  651.         if (sam1->l)
  652.             if (sam1->m > 127) {
  653.                 d = 1;
  654.                 sam1->c = DrumChann;
  655.             } else
  656.                 i[sam1->m] = 1;
  657.         else
  658.             sam1->m = 0;
  659.     }
  660.     for (numchan = d, n = 128; n--; numchan += i[n]);
  661.     if (numchan > 16)
  662.         return numchan;
  663.     /* Ok.. now we must go through and set channels appropriately */
  664.     m = s->n;
  665.     sam1 = s->s;
  666.     c = 0;
  667.     while (m--) {
  668.         if (sam1->c < 0) {
  669.             sam1->c = c;
  670.             n = m;
  671.             sam2 = sam1 + 1;
  672.             while (n--) {
  673.                 if (sam2->c < 0)
  674.                     if (sam2->m == sam1->m || ! sam2->l)
  675.                         sam2->c = c;
  676.                 sam2++;
  677.             }
  678.             if (++c == DrumChann && d)
  679.                 c++;
  680.         }
  681.         sam1++;
  682.     }
  683.     return numchan;
  684. }
  685.  
  686. void MapSamples(samps s)
  687. /* Post: The samples s have been allocated appropriate instruments, we hope */
  688. {
  689.     int i, j;
  690.  
  691.     do
  692.         if ((i = Sample(s, MIDIVal, 4))>=0 && (j = Instrument())>=0)
  693.             s->s[i].m = j;
  694.     while (i>=0);
  695. }
  696.  
  697. void SaveSamp(samp sam)
  698. /* Post: The sample sam has been updated in the DEF_MAPFILE file */
  699. {
  700.     FILE *f1, *f2;
  701.     string s, n;
  702.     int v, d;
  703.     char m[MAXSTRING];
  704.  
  705.     if (!sam.l)
  706.         return;
  707.     if ((f1 = fopen(DEF_MAPFILE, "rt")) == NULL) {
  708.         _NOFIL[3] = DEF_MAPFILE;
  709.         InfoBox(_NOFIL);
  710.         return;
  711.     }
  712.     f2 = fopen("temp.tmp", "wt");
  713.     n = SimplifyName(sam.n);
  714.     v = 1;
  715.     while (v > 0 && (s = GetLine(f1)) != NULL) {
  716.         sscanf(s, "%s %d", m, &d);
  717.         if ((v = strcmp(n, m)) <= 0) {
  718.             fprintf(f2, "%s %d %d %d %d %d %d %d\n", n, sam.m, sam.t[0], sam.t[1],
  719.                 sam.t[2], sam.a[0], sam.a[1], sam.a[2]);
  720.             free(n);
  721.             n = NULL;
  722.             if (v)
  723.                 fprintf(f2, "%s\n", s);
  724.         } else
  725.             fprintf(f2, "%s\n", s);
  726.         free(s);
  727.     }
  728.     while ((s = GetLine(f1))!=NULL) {
  729.         fprintf(f2, "%s\n", s);
  730.         free(s);
  731.     }
  732.     if (n != NULL) {
  733.         fprintf(f2, "%s %d %d %d %d %d %d %d\n", n, sam.m, sam.t[0], sam.t[1],
  734.             sam.t[2], sam.a[0], sam.a[1], sam.a[2]);
  735.         free(n);
  736.     }
  737.     fclose(f2);
  738.     fclose(f1);
  739.     if (unlink(DEF_MAPFILE)<0 || rename("temp.tmp", DEF_MAPFILE)<0)
  740.         InfoBox(_NOSAV);
  741. }
  742.  
  743. void SaveSamples(samps s)
  744. /* Post: The desired sample attributes of s have been saved to disk */
  745. {
  746.     int i;
  747.  
  748.     do
  749.         if ((i = Sample(s, NullVal, 0))>=0)
  750.             SaveSamp(s->s[i]);
  751.     while (i>=0);
  752. }
  753.  
  754. void Transpositions(samps s)
  755. /* Post: All necessary transpositions have been applied to the samples s */
  756. {
  757.     int i;
  758.  
  759.     do {
  760.         if ((i = Sample(s, TransVal, 16))>=0) {
  761.             char s1[41];
  762.             string s2;
  763.             samp *sam;
  764.  
  765.             sam = s->s + i;
  766.             if (!sam->t[1])
  767.                 sprintf(s1, "%d", sam->t[0]);
  768.             else if (!sam->t[2])
  769.                 sprintf(s1, "%d,%d", sam->t[0], sam->t[1]);
  770.             else
  771.                 sprintf(s1, "%d,%d,%d", sam->t[0], sam->t[1], sam->t[2]);
  772.             s2 = DialogBox(_TRAQ, s1);
  773.             sam->t[1] = 0;
  774.             sam->t[2] = 0;
  775.             sscanf(s2, "%d,%d,%d", &sam->t[0], &sam->t[1], &sam->t[2]);
  776.             free(s2);
  777.             if ((sam->t[0] || sam->t[1]) && sam->m > 127)
  778.                 InfoBox(_TRANWRN);
  779.             if (sam->t[0]<-128 || sam->t[0]>127 || sam->t[1]<-128 ||
  780.              sam->t[1]>127 || sam->t[2]<-128 || sam->t[2]>127) {
  781.                 InfoBox(_TRANE);
  782.                 sam->t[0] = 0;
  783.                 sam->t[1] = 0;
  784.                 sam->t[2] = 0;
  785.             }
  786.         }
  787.     } while (i>=0);
  788. }
  789.  
  790. void VolumeShifts(samps s)
  791. /* Post: All volume shifting formulae have been defined for the samples s */
  792. {
  793.     int i;
  794.  
  795.     do
  796.         if ((i = Sample(s, VolumeVal, 16))>=0) {
  797.             char s1[41];
  798.             string s2;
  799.             samp *sam;
  800.  
  801.             sam = s->s + i;
  802.             sprintf(s1, "%d,%d,%d", sam->a[0], sam->a[1], sam->a[2]);
  803.             s2 = DialogBox(_VSHQ, s1);
  804.             if (sscanf(s2, "%d,%d,%d", &sam->a[0], &sam->a[1], &sam->a[2])!=3 ||
  805.              sam->a[0] > 127 || sam->a[0] < -128 || sam->a[1] > 127 ||
  806.              sam->a[1] < 0 || sam->a[2] > 127 || sam->a[2] < 1) {
  807.                 InfoBox(_VSHE);
  808.                 sam->a[0] = 0;
  809.                 sam->a[1] = 1;
  810.                 sam->a[2] = 1;
  811.             }
  812.             free(s2);
  813.         }
  814.     while (i>=0);
  815. }
  816.  
  817. void SetDrumChannel( void )
  818. /* Post: Drum channel is set to desired value - 1 */
  819. {
  820.     string s;
  821.     char s2[3];
  822.  
  823.     sprintf(s2, "%d", DrumChann+1);
  824.     s = DialogBox(_DCQ, s2);
  825.     if (sscanf(s, "%d", &DrumChann))
  826.         DrumChann--;        /* Drum channel is stored as value - 1 */
  827. }
  828.  
  829. void SetTempoType( void )
  830. /* Post: Tempo interpretting will either be extended or normal */
  831. {
  832.     char inp;
  833.  
  834.     while ((inp = InfoBox(TempoType ? _TTQ1 : _TTQ0)) != '0' && inp != '1');
  835.     TempoType = inp - '0';
  836. }
  837.  
  838. unsigned char NoteValue(unsigned int n)
  839. /* Returns: MIDI note equivalent of MOD period-lengths */
  840. {
  841.     static unsigned int t[72] = {
  842.         1712, 1616, 1525, 1440, 1357, 1281, 1209, 1141, 1077, 1017, 961, 907,
  843.         856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
  844.         428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
  845.         214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
  846.         107, 101, 95, 90, 85, 80, 76, 71, 67, 64, 60, 57,
  847.         53, 50, 48, 45, 42, 40, 38, 36, 34, 32, 30, 28
  848.         };
  849.     signed char a = 0, m = 35, b = 71;
  850.  
  851.     if (!n)
  852.         return 0;
  853.     for (; b-a > 1; m = (a + b) / 2) /* binary search */
  854.         if (t[m] < n)
  855.             b = m;
  856.         else
  857.             a = m;
  858.     if (n - t[b] < t[a] - n) /* choose closest value */
  859.         return (unsigned char)(b + 36);
  860.     return (unsigned char)(a + 36);
  861. }
  862.  
  863. unsigned long NoteLength(unsigned char n, unsigned int l, unsigned int b)
  864. /* Returns: # of ticks to play note length l at pitch n, b beats/min */
  865. {
  866.     unsigned long  idx;
  867.  
  868.     static float t[84] = {
  869.         3.200e-3, 3.020e-3, 2.851e-3, 2.691e-3, 2.540e-3, 2.397e-3,
  870.         2.263e-3, 2.136e-3, 2.016e-3, 1.903e-3, 1.796e-3, 1.695e-3,
  871.         1.600e-3, 1.510e-3, 1.425e-3, 1.345e-3, 1.270e-3, 1.197e-3,
  872.         1.131e-3, 1.068e-3, 1.008e-3, 9.514e-4, 8.980e-4, 8.476e-4,
  873.         8.000e-4, 7.551e-4, 7.127e-4, 6.727e-4, 6.350e-4, 5.993e-4,
  874.         5.657e-4, 5.339e-4, 5.040e-4, 4.757e-4, 4.490e-4, 4.238e-4,
  875.         4.000e-4, 3.775e-4, 3.564e-4, 3.364e-4, 3.175e-4, 2.997e-4,
  876.         2.828e-4, 2.670e-4, 2.520e-4, 2.378e-4, 2.245e-4, 2.119e-4,
  877.         2.000e-4, 1.888e-4, 1.782e-4, 1.682e-4, 1.587e-4, 1.498e-4,
  878.         1.414e-4, 1.335e-4, 1.260e-4, 1.189e-4, 1.122e-4, 1.059e-4,
  879.         1.000e-4, 9.439e-5, 8.909e-5, 8.409e-5, 7.937e-5, 7.492e-5,
  880.         7.071e-5, 6.674e-5, 6.300e-5, 5.946e-5, 5.612e-5, 5.297e-5,
  881.         5.000e-5, 4.719e-5, 4.454e-5, 4.204e-5, 3.969e-5, 3.746e-5,
  882.         3.536e-5, 3.337e-5, 3.150e-5, 2.973e-5, 2.806e-5, 2.649e-5
  883.         }; /* multipliers for each pitch: 12th roots of 2 apart */
  884.  
  885.     idx = (n - 36);
  886.     if( idx < 0 ) idx = 0;
  887.     if( idx > 84 ) idx = 84;
  888.  
  889.     return ( idx * b * l); /* better not slide out of this range :( */
  890. }
  891.  
  892. void WriteHeader(bfile mf, unsigned char n)
  893. /* Post: The MIDI header has been written to mf, with #tracks = n */
  894. {
  895.     static unsigned char MIDIH[14] = {
  896.         77, 84, 104, 100, 0, 0, 0, 6, 0, 1, 0, 255, 0, 192
  897.     };
  898.     int i = 0;
  899.  
  900.     MIDIH[11] = n+1;
  901.     while (i < 14)
  902.         OutByte(mf, MIDIH[i++]);
  903. }
  904.  
  905. unsigned int Trk0Info(bfile mf, string s)
  906. /*
  907.  * Returns: the number of bytes written as track 0 so far, given mf as the
  908.  *    output MIDI file. s is the name of the tune.
  909.  */
  910. {
  911.     unsigned char name[]= {"*File Copyright (c) 1993 abcdefghi SoftWare"};
  912.  
  913.     static unsigned char TRK0I[63] = {
  914.         0, 255, 2, 42, 70, 105, 108, 101, 32, 67, 111, 112, 121, 114, 105, 103,
  915.         104, 116, 32, 40, 99, 41, 32, 49 ,57, 57, 51, 32, 65, 100, 114, 101, 110,
  916.         97, 108, 105, 110, 32, 83, 111, 102, 116, 119, 97, 114, 101,
  917.         0, 255, 88, 4, 3, 2, 24, 8,
  918.         0, 255, 89, 2, 0, 0,
  919.         0, 255, 3
  920.         }; /* standard header + copyright message */
  921.     unsigned int i = 0;
  922.  
  923.     strncpy( &TRK0I[3], name, strlen( name ) );
  924.  
  925.     while (i < 63)
  926.         OutByte(mf, TRK0I[i++]);
  927.     i = 64 + strlen(s);
  928.     OutByte(mf, strlen(s));
  929.     while (*s)
  930.         OutByte(mf, *(s++));
  931.     return i;
  932. }
  933.  
  934. void AddToLog(unsigned long a, unsigned long b)
  935. /* Post: a (file position) and b (number) have been added to the log */
  936. {
  937.     PosLog[PosI++] = b;
  938.     PosLog[PosI++] = a;
  939. }
  940.  
  941. void WriteLog(FILE *f)
  942. /* Post: PosLog has been written into file f */
  943. {
  944.     unsigned long x, y;
  945.     unsigned char c[4];
  946.  
  947.     if (!PosI)
  948.         return;
  949.     x = ftell(f);
  950.     while (PosI) {
  951.         fseek(f, PosLog[--PosI], SEEK_SET);
  952.         y = PosLog[--PosI];
  953.         c[3] = y & 255;
  954.         y >>= 8;
  955.         c[2] = y & 255;
  956.         y >>= 8;
  957.         c[1] = y & 255;
  958.         y >>= 8;
  959.         c[0] = y;
  960.         fwrite(c, 1, 4, f);
  961.     }
  962.     fseek(f, x, SEEK_SET);
  963. }
  964.  
  965. unsigned char RestrictVol(int v)
  966. /* Returns: A volume in the range 0..127 */
  967. {
  968.     if (v < 0)
  969.         return 0;
  970.     else
  971.         return (unsigned char)( (v > 127) ? 127 : v );
  972. }
  973.  
  974. void ConvertMOD(bfile f1, bfile f2, string tn, samps smp)
  975. /*
  976.  * Post: The Amiga MODfile f1 has been converted and written to MIDI file f2,
  977.  *    using instrument mappings smp. Tune has a name tn.
  978.  */
  979. {
  980.     unsigned char b, length, pattern[128];
  981.     string n, t;
  982.     unsigned int i, j, k, tempdone = 0;
  983.     samp *sam;
  984.     struct bpos p1, p2;
  985.  
  986.     p1 = FPos(f1);
  987.     length = InByte(f1);
  988.     InByte(f1);
  989.     for (i = 0; i < 128; pattern[i++] = InByte(f1));
  990.     Inskipp(f1, (smp->n > 15) ? 4 : 0);
  991.     WriteHeader(f2, smp->n);
  992.     p2 = FPos(f1);
  993.     for (i = 0, sam = smp->s - 1; i <= smp->n; sam++, i++) {
  994.         unsigned long cnt, inst, timer, delay[4];
  995.         unsigned char c;
  996.  
  997.         OutByte(f2, 77);
  998.         OutByte(f2, 84);
  999.         OutByte(f2, 114);
  1000.         OutByte(f2, 107);
  1001.         inst = Beatle(f2); /* store this position so can set AFTERWARDS - hehe */
  1002.         OutByte(f2, 0);
  1003.         OutByte(f2, 0);
  1004.         OutByte(f2, 0);
  1005.         OutByte(f2, 0);
  1006.         if (!i)
  1007.             cnt = Trk0Info(f2, tn);
  1008.         else {
  1009.             OutByte(f2, 0);
  1010.             OutByte(f2, 255);
  1011.             OutByte(f2, 3);
  1012.             b = strlen(n = sam->n);
  1013.             OutByte(f2, b);
  1014.             cnt = 7 + b;
  1015.             while (b--)
  1016.                 OutByte(f2, *(n++));
  1017.             c = sam->c;
  1018.             OutByte(f2, 0);
  1019.             OutByte(f2, 0xC0 + c); /* set channel */
  1020.             OutByte(f2, (sam->m > 127) ? 126 : sam->m);
  1021.         }
  1022.         timer = 0;
  1023.         if (sam->l || !i) {
  1024.             unsigned int bpm, ticks, l, effect, h;
  1025.             unsigned char sampnum, lastsam[4] = {0}, vol[4];
  1026.             signed char patbreak;
  1027.             unsigned long x, pause;
  1028.             int note, lastslide, slideto;
  1029.             int n[4][48][2]; /* note data for a song position */
  1030.             int lastn[4]; /* last note on a particular channel */
  1031.  
  1032.             sprintf(_CNVPOS[2]+7, "%d", i); /* some compilers hate this */
  1033.             DrawBox(_CNVPOS);
  1034.             memset(lastn, 0, 4 * sizeof(int));
  1035.             memset(vol, 0, 4);
  1036.             memset(delay, 0, 4 * sizeof(long));
  1037.             bpm = 125;
  1038.             ticks = 6;
  1039.             lastslide = slideto = 0;
  1040.             patbreak = 0;
  1041.             for (h = 48; h--; )
  1042.                 for (k = 4; k--; )
  1043.                     n[k][h][0] = 0;
  1044.             for (l = 0; l < length; l++) {
  1045.                 if (pattern[l]<=pattern[l-1] || !l) {
  1046.                     FGoto(f1, p2);
  1047.                     x = (unsigned) 1024 * pattern[l];
  1048.                 } else
  1049.                     x = (unsigned) 1024 * (pattern[l]-pattern[l-1]-1);
  1050.                 Inskipp(f1, x);
  1051.                 j = 64;
  1052.                 if (patbreak>0)
  1053.                     patbreak = 1-patbreak;
  1054.                 while (j--) {
  1055.                     pause = 0;
  1056.                     if (patbreak)
  1057.                         Inskipp(f1, 16);
  1058.                     else
  1059.                         for (k = 0; k < 4; k++) {
  1060.                             n[k][0][1] = sam->v;
  1061.                             sampnum = InByte(f1);
  1062.                             note = ((sampnum & 15) << 8) + InByte(f1);
  1063.                             effect = InByte(f1);
  1064.                             sampnum = (sampnum & ~15) + (effect >> 4);
  1065.                             if (!i)
  1066.                                 note = 0;
  1067.                             if ((note || sampnum) && delay[k]) { /* stop old note */
  1068.                                 cnt += 3 + WriteVLQ(f2, timer);
  1069.                                 timer = 0;
  1070.                                 OutByte(f2, 0x80 + c); /* note off */
  1071.                                 OutByte(f2, ENOTE(lastn[k], 0));
  1072.                                 OutByte(f2, RestrictVol(EVOL(vol[k])));
  1073.                                 if (sam->t[1]) {
  1074.                                     cnt += 4;
  1075.                                     OutByte(f2, 0);
  1076.                                     OutByte(f2, 0x80 + c);
  1077.                                     OutByte(f2, ENOTE(lastn[k], 1));
  1078.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1079.                                     if (sam->t[2]) {
  1080.                                         cnt += 4;
  1081.                                         OutByte(f2, 0);
  1082.                                         OutByte(f2, 0x80 + c);
  1083.                                         OutByte(f2, ENOTE(lastn[k], 2));
  1084.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1085.                                     }
  1086.                                 }
  1087.                                 delay[k] = 0;
  1088.                             }
  1089.                             if (!note && sampnum == i) /* check "defaults" */
  1090.                                 note = lastn[k];
  1091.                             else if (!sampnum)
  1092.                                 if (lastsam[k] == i)
  1093.                                     sampnum = i;
  1094.                                 else
  1095.                                     note = 0;
  1096.                             else {
  1097.                                 if (sampnum != i)
  1098.                                     note = 0;
  1099.                                 lastsam[k] = sampnum;
  1100.                             }
  1101.                             n[k][0][0] = note;
  1102.                             effect = ((effect & 15) << 8) + InByte(f1);
  1103.                             switch (effect & 0xF00) {
  1104.                                 case 0x000: { /* arpeggio */
  1105.                                     int nv;
  1106.  
  1107.                                     if (!i || !effect || sam->m > 127)
  1108.                                         break;
  1109.                                     if (!note)
  1110.                                         if (!delay[k])
  1111.                                             break;
  1112.                                         else {
  1113.                                             nv = NoteValue(lastn[k]);
  1114.                                             n[k][47][0] = lastn[k];
  1115.                                             n[k][47][1] = vol[k];
  1116.                                             if (effect & 0x0F0)
  1117.                                                 n[k][16][0] = -(nv + ((effect & 0x0F0) >> 4));
  1118.                                             n[k][16][1] = vol[k];
  1119.                                             if (effect & 0x00F)
  1120.                                                 n[k][32][0] = -(nv + (effect & 0x00F));
  1121.                                             n[k][32][1] = vol[k];
  1122.                                         }
  1123.                                     else {
  1124.                                         nv = NoteValue(note);
  1125.                                         n[k][47][0] = note;
  1126.                                         n[k][47][1] = sam->v;
  1127.                                         if (effect & 0x0F0)
  1128.                                             n[k][16][0] = -(nv + ((effect & 0x0F0) >> 4));
  1129.                                         n[k][16][1] = sam->v;
  1130.                                         if (effect & 0x00F)
  1131.                                             n[k][32][0] = -(nv + (effect & 0x00F));
  1132.                                         n[k][32][1] = sam->v;
  1133.                                     }
  1134.                                     break;
  1135.                                     }
  1136.                                 case 0x300: /* slide to */
  1137.                                     if (!note && !slideto || note==lastn[k] || sam->m > 127)
  1138.                                         break;
  1139.                                     if (effect & 0x0FF)
  1140.                                         lastslide = effect & 0x0FF;
  1141.                                     else
  1142.                                         lastslide = abs(lastslide);
  1143.                                     if (note)
  1144.                                         slideto = note;
  1145.                                     if (slideto > lastn[k]) {
  1146.                                         n[k][0][0] = lastn[k] + lastslide*(ticks-1);
  1147.                                         if (n[k][0][0] > 856)
  1148.                                             n[k][0][0] = 856;
  1149.                                         if (n[k][0][0] > slideto)
  1150.                                             n[k][0][0] = slideto;
  1151.                                     } else {
  1152.                                         n[k][0][0] = lastn[k] - lastslide*(ticks-1);
  1153.                                         if (n[k][0][0] < 113)
  1154.                                             n[k][0][0] = 113;
  1155.                                         if (n[k][0][0] < slideto)
  1156.                                             n[k][0][0] = slideto;
  1157.                                     }
  1158.                                     n[k][0][1] = vol[k];
  1159.                                     break;
  1160.                                 case 0x100: /* slide up */
  1161.                                 case 0x200: /* slide down */
  1162.                                     if (!(effect & 0x0FF) || sam->m > 127)
  1163.                                         break;
  1164.                                     if (effect & 0x200)
  1165.                                         lastslide = effect & 0x0FF;
  1166.                                     else
  1167.                                         lastslide = -(effect & 0x0FF);
  1168.                                     if (!note)
  1169.                                         if (!delay[k])
  1170.                                             break;
  1171.                                         else {
  1172.                                             n[k][0][0] = lastn[k] + lastslide;
  1173.                                             n[k][0][1] = vol[k];
  1174.                                         }
  1175.                                     else
  1176.                                         n[k][0][0] += lastslide;
  1177.                                     if (n[k][0][0] > 856)
  1178.                                         n[k][0][0] = 856;
  1179.                                     else if (n[k][0][0] < 113)
  1180.                                         n[k][0][0] = 113;
  1181.                                     break;
  1182.                                 case 0x400: /* vibrato */
  1183.                                 case 0x700: /* tremolo */
  1184.                                     /* ignore these effects.. not convertable */
  1185.                                     break;
  1186.                                 case 0x500: /* slide to & volume slide */
  1187.                                     if ((note || slideto) && note!=lastn[k] && sam->m < 128) {
  1188.                                         if (note)
  1189.                                             slideto = note;
  1190.                                         if (slideto > lastn[k]) {
  1191.                                             n[k][0][0] = lastn[k] + lastslide*(ticks-1);
  1192.                                             if (n[k][0][0] > 856)
  1193.                                                 n[k][0][0] = 856;
  1194.                                             if (n[k][0][0] > slideto)
  1195.                                                 n[k][0][0] = slideto;
  1196.                                         } else {
  1197.                                             n[k][0][0] = lastn[k] - lastslide*(ticks-1);
  1198.                                             if (n[k][0][0] < 113)
  1199.                                                 n[k][0][0] = 113;
  1200.                                             if (n[k][0][0] < slideto)
  1201.                                                 n[k][0][0] = slideto;
  1202.                                         }
  1203.                                     } else
  1204.                                         n[k][0][0] = 0;
  1205.                                     note = 0;
  1206.                                 case 0x600: /* vibrato & volume slide */
  1207.                                 case 0xA00: { /* volume slide */
  1208.                                     int v;
  1209.  
  1210.                                     if (!note)
  1211.                                         v = vol[k];
  1212.                                     else
  1213.                                         v = sam->v;
  1214.                                     v += (ticks-1)*(effect & 0x0F0); /* can't really slide */
  1215.                                     v -= (ticks-1)*(effect & 0x00F);
  1216.                                     if (v > 127)
  1217.                                         v = 127;
  1218.                                     else if (v < 0)
  1219.                                         v = 0;
  1220.                                     n[k][0][1] = v;
  1221.                                     break;
  1222.                                     }
  1223.                                 case 0x900: /* set offset: pretend it's retrigger */
  1224.                                     if ((!n[k][0][0] || !sampnum) && delay[k]) {
  1225.                                         n[k][0][0] = lastn[k];
  1226.                                         n[k][0][1] = vol[k];
  1227.                                     }
  1228.                                     break;
  1229.                                 case 0xB00: /* position jump: ignore, but break anyway */
  1230.                                     patbreak = 1;
  1231.                                     break;
  1232.                                 case 0xD00: /* pattern break */
  1233.                                     patbreak = 1 + 10 * (effect & 0x0F0) + (effect & 0x00F);
  1234.                                     break;
  1235.                                 case 0xC00: /* set volume */
  1236.                                     n[k][0][1] = effect & 0x0FF;
  1237.                                     break;
  1238.                                 case 0xF00: { /* set tempo */
  1239.                                     int temp;
  1240.  
  1241.                                     temp = effect & 0x0FF;
  1242.                                     if (!temp)
  1243.                                         temp = 1;
  1244.                                     if (temp < 32) {
  1245.                                         ticks = temp;
  1246.                                         if (TempoType) {  /* Tempos act strangely so .. */
  1247.                                             bpm = 750 / temp;
  1248.                                             x = 80000 * temp;
  1249.                                         }
  1250.                                     } else {
  1251.                                         bpm = temp;
  1252.                                         x = 60000000 / temp;
  1253.                                     }
  1254.                                     if (i)
  1255.                                         break; /* only write tempo on track 0 */
  1256.                                     cnt += 6 + WriteVLQ(f2, timer);
  1257.                                     timer = 0;
  1258.                                     OutByte(f2, 255); /* meta-event */
  1259.                                     OutByte(f2, 81); /* set tempo */
  1260.                                     OutByte(f2, 3);
  1261.                                     OutByte(f2, x >> 16);
  1262.                                     OutByte(f2, (x >> 8) & 0xFF);
  1263.                                     OutByte(f2, x & 0xFF);
  1264.                                     tempdone = 1;
  1265.                                     break;
  1266.                                     }
  1267.                                 case 0xE00: /* extended effects */
  1268.                                     switch (effect & 0x0F0) {
  1269.                                         case 0x010: /* fine slide up */
  1270.                                             if (!(effect & 0x00F) || sam->m > 127)
  1271.                                                 break;
  1272.                                             if (!note)
  1273.                                                 if (!delay[k])
  1274.                                                     break;
  1275.                                                 else {
  1276.                                                     n[k][h][0] = lastn[k] + (effect & 0x00F);
  1277.                                                     n[k][h][1] = vol[k];
  1278.                                                 }
  1279.                                             else
  1280.                                                 n[k][h][0] += effect & 0x00F;
  1281.                                             break;
  1282.                                         case 0x020: /* fine slide down */
  1283.                                             if (!(effect & 0x00F) || sam->m > 127)
  1284.                                                 break;
  1285.                                             if (!note)
  1286.                                                 if (!delay[k])
  1287.                                                     break;
  1288.                                                 else {
  1289.                                                     n[k][h][0] = lastn[k] - (effect & 0x00F);
  1290.                                                     n[k][h][1] = vol[k];
  1291.                                                 }
  1292.                                             else
  1293.                                                 n[k][h][0] -= effect & 0x00F;
  1294.                                             break;
  1295.                                         case 0x000: /* set filter on/off */
  1296.                                         case 0x030: /* glissando on/off */
  1297.                                         case 0x040: /* set vibrato wave */
  1298.                                         case 0x050: /* set finetune */
  1299.                                         case 0x060: /* pattern loop */
  1300.                                         case 0x070: /* set tremolo wave */
  1301.                                         case 0x080: /* un-used */
  1302.                                         case 0x0F0: /* invert loop */
  1303.                                             /* can't do these in MIDI.. ignore */
  1304.                                             break;
  1305.                                         case 0x0A0: /* fine volume slide up */
  1306.                                         case 0x0B0: { /* fine volume slide down */
  1307.                                             int v;
  1308.  
  1309.                                             v = sam->v;
  1310.                                             if (effect & 0x0A0)
  1311.                                                 v += effect & 0x00F;
  1312.                                             else
  1313.                                                 v -= effect & 0x00F;
  1314.                                             if (v<0)
  1315.                                                 v = 0;
  1316.                                             else if (v>127)
  1317.                                                 v = 127;
  1318.                                             n[k][0][1] = v;
  1319.                                             break;
  1320.                                             }
  1321.                                         case 0x090: { /* retrigger sample */
  1322.                                             int a, b, c;
  1323.  
  1324.                                             if (!note && !delay[k] || !(effect & 0x00F))
  1325.                                                 break;
  1326.                                             a = effect & 0x00F;
  1327.                                             if (!(ticks / a))
  1328.                                                 break;
  1329.                                             if (!note) {
  1330.                                                 n[k][0][0] = lastn[k];
  1331.                                                 n[k][0][1] = vol[k];
  1332.                                             }
  1333.                                             c = 0;
  1334.                                             b = 1;
  1335.                                             a *= 48;
  1336.                                             while (c < 48) {
  1337.                                                 n[k][c][0] = note;
  1338.                                                 n[k][c][1] = n[k][0][1];
  1339.                                                 c = b * a / ticks;
  1340.                                                 b++;
  1341.                                             }
  1342.                                             break;
  1343.                                             }
  1344.                                         case 0x0C0: { /* cut sample */
  1345.                                             int a;
  1346.  
  1347.                                             if (!note && !delay[k])
  1348.                                                 break;
  1349.                                             a = 48 * (effect & 0x00F) / ticks;
  1350.                                             if (a > 47)
  1351.                                                 break;
  1352.                                             if (note)
  1353.                                                 n[k][a][0] = note;
  1354.                                             else
  1355.                                                 n[k][a][0] = lastn[k];
  1356.                                             n[k][a][1] = 0;
  1357.                                             break;
  1358.                                             }
  1359.                                         case 0x0D0: { /* delay sample */
  1360.                                             int a;
  1361.  
  1362.                                             if (!note || !(effect & 0x00F))
  1363.                                                 break;
  1364.                                             a = 48 * (effect & 0x00F) / ticks;
  1365.                                             n[k][0][0] = 0;
  1366.                                             if (a > 47)
  1367.                                                 break;
  1368.                                             n[k][a][0] = note;
  1369.                                             n[k][a][1] = n[k][a][0];
  1370.                                             break;
  1371.                                             }
  1372.                                         case 0x0E0: /* pattern pause */
  1373.                                             pause = 48 * (effect & 0x00F);
  1374.                                             break;
  1375.                                     }
  1376.                                     break;
  1377.                                 /* else dunno what it does.. disbelieve it ;) */
  1378.                             }
  1379.                         }
  1380.                     for (h = 0; h<48; h++) {
  1381.                         for (k = 0; k < 4; k++)
  1382.                             if (n[k][h][0]) {
  1383.                                 if (delay[k]) { /* turn off old note on same channel */
  1384.                                     cnt += 3 + WriteVLQ(f2, timer);
  1385.                                     timer = 0;
  1386.                                     OutByte(f2, 0x80 + c); /* note off */
  1387.                                     OutByte(f2, ENOTE(lastn[k], 0));
  1388.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1389.                                     if (sam->t[1]) {
  1390.                                         cnt += 4;
  1391.                                         OutByte(f2, 0);
  1392.                                         OutByte(f2, 0x80 + c);
  1393.                                         OutByte(f2, ENOTE(lastn[k], 1));
  1394.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1395.                                         if (sam->t[2]) {
  1396.                                             cnt += 4;
  1397.                                             OutByte(f2, 0);
  1398.                                             OutByte(f2, 0x80 + c);
  1399.                                             OutByte(f2, ENOTE(lastn[k], 2));
  1400.                                             OutByte(f2, RestrictVol(EVOL(vol[k])));
  1401.                                         }
  1402.                                     }
  1403.                                     delay[k] = 0;
  1404.                                 }
  1405.                                 lastn[k] = n[k][h][0];
  1406.                                 n[k][h][0] = 0;
  1407.                                 vol[k] = n[k][h][1];
  1408.                                 cnt += 3 + WriteVLQ(f2, timer);
  1409.                                 timer = 0;
  1410.                                 OutByte(f2, 0x90 + c); /* note on */
  1411.                                 OutByte(f2, ENOTE(lastn[k], 0));
  1412.                                 OutByte(f2, RestrictVol(EVOL(vol[k])));
  1413.                                 if (sam->t[1]) {
  1414.                                     cnt += 4;
  1415.                                     OutByte(f2, 0);
  1416.                                     OutByte(f2, 0x90 + c);
  1417.                                     OutByte(f2, ENOTE(lastn[k], 1));
  1418.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1419.                                     if (sam->t[2]) {
  1420.                                         cnt += 4;
  1421.                                         OutByte(f2, 0);
  1422.                                         OutByte(f2, 0x90 + c);
  1423.                                         OutByte(f2, ENOTE(lastn[k], 2));
  1424.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1425.                                     }
  1426.                                 }
  1427.                                 delay[k] = NoteLength(ANOTE(lastn[k]), sam->l, bpm);
  1428.                             } else if (delay[k]==1) {
  1429.                                 delay[k] = 0;
  1430.                                 cnt += 3 + WriteVLQ(f2, timer);
  1431.                                 timer = 0;
  1432.                                 OutByte(f2, 0x80 + c); /* note off */
  1433.                                 OutByte(f2, ENOTE(lastn[k], 0));
  1434.                                 OutByte(f2, RestrictVol(EVOL(vol[k])));
  1435.                                 if (sam->t[1]) {
  1436.                                     cnt += 4;
  1437.                                     OutByte(f2, 0);
  1438.                                     OutByte(f2, 0x80 + c);
  1439.                                     OutByte(f2, ENOTE(lastn[k], 1));
  1440.                                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1441.                                     if (sam->t[2]) {
  1442.                                         cnt += 4;
  1443.                                         OutByte(f2, 0);
  1444.                                         OutByte(f2, 0x80 + c);
  1445.                                         OutByte(f2, ENOTE(lastn[k], 2));
  1446.                                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1447.                                     }
  1448.                                 }
  1449.                             } else if (delay[k]>0)
  1450.                                 delay[k]--;
  1451.                         timer++;
  1452.                     }
  1453.                     timer += pause;
  1454.                     if (patbreak<0)
  1455.                         patbreak++;
  1456.                     else if (patbreak>0) {
  1457.                         patbreak = 1 - patbreak;
  1458.                         while (j) {
  1459.                             j--;
  1460.                             Inskipp(f1, 16); /* 16 bytes / song position */
  1461.                         }
  1462.                     }
  1463.                 }
  1464.             }
  1465.             for (k = 0; k < 4; k++)
  1466.                 if (delay[k]) {
  1467.                     cnt += 3 + WriteVLQ(f2, timer);
  1468.                     timer = 0;
  1469.                     OutByte(f2, 0x80 + c); /* note off */
  1470.                     OutByte(f2, ENOTE(lastn[k], 0));
  1471.                     OutByte(f2, RestrictVol(EVOL(vol[k])));
  1472.                     if (sam->t[1]) {
  1473.                         cnt += 4;
  1474.                         OutByte(f2, 0);
  1475.                         OutByte(f2, 0x80 + c);
  1476.                         OutByte(f2, ENOTE(lastn[k], 1));
  1477.                         OutByte(f2, RestrictVol(EVOL(vol[k])));
  1478.                         if (sam->t[2]) {
  1479.                             cnt += 4;
  1480.                             OutByte(f2, 0);
  1481.                             OutByte(f2, 0x80 + c);
  1482.                             OutByte(f2, ENOTE(lastn[k], 2));
  1483.                             OutByte(f2, RestrictVol(EVOL(vol[k])));
  1484.                         }
  1485.                     }
  1486.                 }
  1487.         }
  1488.         if (!i && !tempdone) {
  1489.             cnt += 7;
  1490.             OutByte(f2, 0); /* give the default 128 bpm if none done yet */
  1491.             OutByte(f2, 255);
  1492.             OutByte(f2, 81);
  1493.             OutByte(f2, 3);
  1494.             OutByte(f2, 7);
  1495.             OutByte(f2, 39);
  1496.             OutByte(f2, 14);
  1497.         }
  1498.         cnt += 3 + WriteVLQ(f2, timer);
  1499.         OutByte(f2, 255);
  1500.         OutByte(f2, 47);
  1501.         OutByte(f2, 0);
  1502.         AddToLog(inst, cnt); /* process this later */
  1503.         FGoto(f1, p2);
  1504.     }
  1505.     ClearWin();
  1506.     FlushOut(f2);
  1507.     WriteLog(f2->f);
  1508.     FGoto(f1, p1);
  1509. }
  1510.  
  1511. int main(int argc, char *argv[])
  1512. {
  1513.     int c;
  1514.  
  1515.     MainWindow("MIDIMOD - Amiga Noise/Sound/Protracker to MIDI converter",
  1516.         4, "File", 'f', "Samples", 's', "Options", 'o', "Help", 'h');
  1517.  
  1518. /*    SetMenu(0, 6, "Dest. midi",'d',0, "Source mod",'s',1, "------------",-1,-1,
  1519.         "Convert",'c',2, "------------",-1,-1, "Quit",'q',99);
  1520.     SetMenu(1, 5, "Map samples",'m',3, "Transposing",'t',6, "Volume shift",'v',9,
  1521.         "------------",-1,-1, "Save info",'s',4);
  1522.     SetMenu(2, 2, "Drum channel",'d',7, "Tempo type",'t',8);
  1523.     SetMenu(3, 1, "About",'a',5);
  1524. */
  1525.     MidFile->f = ModFile->f = NULL;
  1526.     MidFN = ModFN = NULL;
  1527.     if (argc>2) {
  1528.         strcpy(MidFN = (string) malloc(strlen(argv[2])+1), argv[2]);
  1529.         if (fclose(fopen(MidFN, "r"))==EOF || InfoBox(_OUTE)=='y')
  1530.             OpenOut(MidFile, MidFN);
  1531.         if (MidFile->f==NULL) {
  1532.             free(MidFN);
  1533.             MidFN = NULL;
  1534.         }
  1535.     }
  1536.     if (argc>1) {
  1537.         strcpy(ModFN = (string) malloc(strlen(argv[1])+1), argv[1]);
  1538.         if (OpenIn(ModFile, ModFN))
  1539.             if (!ReadModSpecs(ModFile, SongName, Samples))
  1540.                 InfoBox(_MODE);
  1541.             else if (!SetDefaults(Samples, ModFN))
  1542.                 CloseIn(ModFile);
  1543.         if (ModFile->f==NULL) {
  1544.             free(ModFN);
  1545.             ModFN = NULL;
  1546.         }
  1547.     }
  1548.  
  1549.     c = Choice();
  1550.  
  1551.     InfoBox(_ABOUT);
  1552.  
  1553.     do {
  1554.         c = Choice();
  1555.         switch(c) {
  1556.             case 0:
  1557.                 MidFN = InitFile(MidFile, MidFN, "MIDI file below.", 0);
  1558.                 break;
  1559.             case 1:
  1560.                 if (ModFile->f != NULL)
  1561.                     SaveDefaults(Samples, ModFN); /* make sure defaults are saved */
  1562.                 ModFN = InitFile(ModFile, ModFN, "MOD file below.", 1);
  1563.                 if (ModFN != NULL)
  1564.                     if (!ReadModSpecs(ModFile, SongName, Samples))
  1565.                         InfoBox(_MODE);
  1566.                     else if (!SetDefaults(Samples, ModFN))
  1567.                         CloseIn(ModFile);
  1568.                 break;
  1569.             case 2:
  1570.                 if (MidFile->f != NULL && ModFile->f != NULL)
  1571.                     if (ChooseChannels(Samples) <= 16)
  1572.                         ConvertMOD(ModFile, MidFile, SongName, Samples);
  1573.                     else
  1574.                         InfoBox(_TMC);
  1575.                 else
  1576.                     InfoBox(_CNVE);
  1577.                 break;
  1578.             case 3:
  1579.                 if (ModFile->f != NULL)
  1580.                     MapSamples(Samples);
  1581.                 else
  1582.                     InfoBox(_MODM);
  1583.                 break;
  1584.             case 4:
  1585.                 if (ModFile->f != NULL)
  1586.                     SaveSamples(Samples);
  1587.                 else
  1588.                     InfoBox(_MODM);
  1589.                 break;
  1590.             case 5:
  1591.                 InfoBox(_ABOUT);
  1592.                 break;
  1593.             case 6:
  1594.                 if (ModFile->f != NULL)
  1595.                     Transpositions(Samples);
  1596.                 else
  1597.                     InfoBox(_MODM);
  1598.                 break;
  1599.             case 7:
  1600.                 SetDrumChannel();
  1601.                 break;
  1602.             case 8:
  1603.                 SetTempoType();
  1604.                 break;
  1605.             case 9:
  1606.                 if (ModFile->f != NULL)
  1607.                     VolumeShifts(Samples);
  1608.                 else
  1609.                     InfoBox(_MODM);
  1610.                 break;
  1611.         }
  1612.     } while (c != 99);
  1613.     if (ModFile->f != NULL) {
  1614.         CloseIn(ModFile);
  1615.         SaveDefaults(Samples, ModFN);
  1616.     }
  1617.     free(ModFN);
  1618.     if (MidFile->f != NULL)
  1619.         CloseOut(MidFile);
  1620.     free(MidFN);
  1621.     EndWindows();
  1622.     exit(0);
  1623. }
  1624.  
  1625.