home *** CD-ROM | disk | FTP | other *** search
/ Sound, Music & MIDI Collection 2 / SMMVOL2.bin / MIDI_PAT / MTOOLS.ZIP / PLAYRDEV.EXE / MLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-07  |  7.2 KB  |  393 lines

  1. /*
  2.    Play/R interface module.
  3.    Copyright 1992, Kevin Weiner, All rights reserved. 
  4. */
  5.  
  6. #include <dos.h>
  7. #include <string.h>
  8.  
  9. #if defined(__BORLANDC__)
  10. #include <mem.h>
  11. #else
  12. #define asm _asm
  13. #endif
  14.  
  15. #define LOCAL
  16. #include "mlib.h"
  17.  
  18. typedef char *cptr;
  19.  
  20. #define fcnPause     0
  21. #define fcnPlay      1
  22. #define fcnPopup     2
  23. #define fcnRewind    3
  24. #define fcnLoadPlay  4
  25. #define fcnPlayStat  5
  26. #define fcnLoadStat  6
  27. #define fcnDoneStat  7
  28. #define fcnLoad      8
  29. #define fcnQuiet     9
  30. #define fcnPopEna    10
  31. #define fcnVolume    11
  32. #define fcnReset     12
  33. #define fcnTimeMode  13
  34. #define fcnGetChan   14
  35. #define fcnSetChan   15
  36. #define fcnSetPos    16
  37. #define fcnSkipSong  17
  38. #define fcnLoopMode  18
  39.  
  40. #define fcnGetName   20
  41. #define fcnSendShort 21
  42. #define fcnSendLong  22
  43. #define fcnCheckInp  23
  44. #define fcnGetInput  24
  45. #define fcnGetTime   25
  46. #define fcnGetCID    26
  47. #define fcnGetEntry  27
  48. #define fcnRemove    99
  49.  
  50. char MidiDriverLoaded;
  51. int MID;
  52.  
  53. ftype2 midiPutByte;
  54. ftype4 midiInputReady;
  55. ftype5 midiGetByte;
  56. ftype1 midiClearInput;
  57. ftype3 msTimer;
  58. ftype6 midiGetMessage;
  59. ftype7 midiResend;
  60. ftype8 midiPutMessage;
  61.  
  62. int tessmpx = 0x5453;                   /* TesSeRact multiplex id */
  63. int idnum;                              /* Play/R id number */
  64. #if defined(__BORLANDC__)
  65. char playrid[8] = "Play/R  ";           /* PlayR id string */
  66. char param [64];                        /* Parameter block */
  67. #else
  68. char far playrid[8] = "Play/R  ";       /* PlayR id string */
  69. char far param [64];                    /* Parameter block */
  70. #endif
  71. union REGS reg;                         /* Machine registers */
  72. struct SREGS sreg;
  73.  
  74.  
  75. void CallPlayR(char fcn)
  76.  
  77.   /*  Call Play/R using the contents of the byte array "param".
  78.       Byte 0, passed in fcn, the function code, and the rest is
  79.       any required data.  */
  80.  
  81.  
  82.   {
  83.     long temp;
  84.  
  85.     param[0] = fcn;
  86.     reg.x.ax = tessmpx;                /* ax = Tess multiplex id */
  87. #if defined(__BORLANDC__)
  88.     sreg.es = FP_SEG(param);           /* es = parameter segment */
  89.     reg.x.di = FP_OFF(param);          /* di = parameter offset */
  90. #else
  91.     temp = (long) param;
  92.     sreg.es = temp >> 16;              /* es = parameter segment */
  93.     reg.x.di = temp & 0xffff;          /* di = parameter offset */
  94. #endif
  95.     reg.x.cx = idnum;                  /* cx = PlayR id number */
  96.     reg.x.bx = 0x20;                   /* bx = Tess call user function */
  97.  
  98.     asm {
  99.       push di
  100.       push si
  101.     }
  102.  
  103.     int86x(0x2f, ®, ®, &sreg);   /* Call int 2fh */
  104.  
  105.     asm {
  106.       pop si
  107.       pop di
  108.     }
  109.   }
  110.  
  111.  
  112. void mfPause()
  113.  
  114.   {
  115.     CallPlayR(fcnPause);
  116.   }
  117.  
  118.  
  119. void mfContinue()
  120.  
  121.   {
  122.     CallPlayR(fcnPlay);
  123.   }
  124.  
  125.  
  126. void mfPopup()
  127.  
  128.   {
  129.     CallPlayR(fcnPopup);
  130.   }
  131.  
  132.  
  133. void mfRewind()
  134.  
  135.   {
  136.     CallPlayR(fcnRewind);
  137.   }
  138.  
  139.  
  140. byte mfPlay(char *name)
  141.  
  142.   {
  143.     strcpy(¶m[1], name);
  144.     CallPlayR(fcnLoadPlay);
  145.     return param[0];
  146.   }
  147.  
  148.  
  149. void mfSongStat(char *playing, char *done, long *position,
  150.                 byte *songcount, byte *cursong)
  151.  
  152.   {
  153.     CallPlayR(fcnPlayStat);
  154.     *playing = param[0];
  155.     *done = param[1];
  156.     memcpy(position, ¶m[2],  4);
  157.     *songcount = param[6];
  158.     *cursong = param[7];
  159.   }
  160.  
  161.  
  162. void mfFileStat(byte *stat, char *name)
  163.  
  164.   {
  165.     CallPlayR(fcnLoadStat);
  166.     *stat = param[0];
  167.     strcpy(name, ¶m[1]);
  168.   }
  169.  
  170.  
  171. byte mfLoad(char *name)
  172.  
  173.   {
  174.     strcpy(¶m[1], name);
  175.     CallPlayR(fcnLoad);
  176.     return param[0];
  177.   }
  178.  
  179.  
  180. void mfQuiet()
  181.  
  182.   {
  183.     CallPlayR(fcnQuiet);
  184.   }
  185.  
  186.  
  187. void mfPopEnable(char stat)
  188.  
  189.   {
  190.     param[1] = stat;
  191.     CallPlayR(fcnPopEna);
  192.   }
  193.  
  194.  
  195. void mfVolume(int adjust)
  196.  
  197.   {
  198.     param[1] = (char) adjust;
  199.     CallPlayR(fcnVolume);
  200.   }
  201.  
  202.  
  203. void midiReset(int dev)
  204.  
  205.   {
  206.     param[1] = dev;
  207.     CallPlayR(fcnReset);
  208.   }
  209.  
  210.  
  211. void mfTimeMode(byte mode)
  212.  
  213.   {
  214.     param[1] = mode;
  215.     CallPlayR(fcnTimeMode);
  216.   }
  217.  
  218.  
  219. void mfGetChan(int datatype, channels *chan)
  220.  
  221.   {
  222.     param[1] = datatype;
  223.     CallPlayR(fcnGetChan);
  224.     memcpy(chan, ¶m[2], 16);
  225.   }
  226.  
  227.  
  228. void mfSetChan(int datatype, channels *chan)
  229.  
  230.   {
  231.     param[1] = datatype;
  232.     memcpy(¶m[2], chan, 16);
  233.     CallPlayR(fcnSetChan);
  234.   }
  235.  
  236. void mfSetPos(long time)
  237.  
  238.   {
  239.     memcpy(¶m[1], &time, 4);
  240.     CallPlayR(fcnSetPos);
  241.   }
  242.  
  243. void mfSkipSong(byte n)
  244.  
  245.   {
  246.     param[1] = n;
  247.     CallPlayR(fcnSkipSong);
  248.   }
  249.  
  250. void mfLoopMode(byte n)
  251.  
  252.   {
  253.     param[1] = n;
  254.     CallPlayR(fcnLoopMode);
  255.   }
  256.  
  257. int midiDevName(int dev, char *devname, char *devdesc)
  258.  
  259.   {
  260.     param[1] = dev;
  261.     CallPlayR(fcnGetName);
  262.     if (param[1] == 0)
  263.       {
  264.     *devname = NULL;
  265.     *devdesc = NULL;
  266.       }
  267.     else
  268.       {
  269.     memcpy(devname, ¶m[2], 3);
  270.     devname[3] = 0;
  271.     memcpy(devdesc, ¶m[5], 20);
  272.     devdesc[20] = 0;
  273.       }
  274.     return param[1];
  275.   }
  276.  
  277.  
  278. void midiPutBuffer(int dev, char far *buf, unsigned int len)
  279.  
  280.   {
  281.     int i;
  282.  
  283.     param[1] = dev;
  284.     memcpy(¶m[2], &len, 2);
  285.     memcpy(¶m[4], &buf, 4);
  286.     for (i = 8; i < 12; i++) param[i] = 0;
  287.     CallPlayR(fcnSendLong);
  288.   }
  289.  
  290.  
  291. void midiPutBuffer1(int dev, char *buf, unsigned int len)
  292.  
  293.   {
  294.     word  i;
  295.     int   x;
  296.  
  297.     for (i = 0; i < len; i++)
  298.     {
  299.       x = buf[i];
  300.       midiPutByte(dev, x);
  301.     };
  302.   }
  303.  
  304.  
  305. word midiGetBuffer(int dev, char *buf, word max)
  306.  
  307.   {
  308.     word  i;
  309.     int   x;
  310.  
  311.     for (i=0; midiInputReady(dev) && (i < max); i++)
  312.     {
  313.       midiGetByte(dev, &x);
  314.       buf[i] = x;
  315.     };
  316.     return i;
  317.   }
  318.  
  319.  
  320. void midiRemove()
  321.  
  322.   {
  323.     CallPlayR(fcnRemove);
  324.   }
  325.  
  326.  
  327. void BindDriver()
  328.  
  329.   {
  330.     CallPlayR(fcnGetEntry);
  331.     memcpy(&midiPutByte, ¶m[1], 4);
  332.     memcpy(&midiInputReady, ¶m[5], 4);
  333.     memcpy(&midiGetByte, ¶m[9], 4);
  334.     memcpy(&midiClearInput, ¶m[13], 4);
  335.     memcpy(&msTimer, ¶m[17], 4);
  336.     memcpy(&midiGetMessage, ¶m[21], 4);
  337.     memcpy(&midiResend, ¶m[25], 4);
  338.     memcpy(&midiPutMessage, ¶m[29], 4);
  339.   }
  340.  
  341.  
  342. int CheckRes()
  343.  
  344. /*  Check Play/R is loaded - returns id number if found, else -1  */
  345.  
  346.   {
  347.     long temp;
  348.  
  349.     reg.x.ax = tessmpx;                /* ax = Tess int 2fh muliplex id */
  350. #if defined(__BORLANDC__)
  351.     sreg.ds = FP_SEG(playrid);         /* ds = id string segment */
  352.     reg.x.si = FP_OFF(playrid);        /* si = id string offset */
  353. #else
  354.     temp = (long) playrid;
  355.     sreg.ds = temp >> 16;              /* es = parameter segment */
  356.     reg.x.si = temp & 0xffff;          /* di = parameter offset */
  357. #endif
  358.     reg.x.cx = 0;                      /* cx = Tess id counter - must be 0 */
  359.     reg.x.bx = 0;                      /* bx = Tess check resident function */
  360.  
  361.     asm {
  362.       push di
  363.       push si
  364.     }
  365.  
  366.     int86x(0x2f, ®, ®, &sreg);   /* Call int 2fh */
  367.  
  368.     asm {
  369.       pop si
  370.       pop di
  371.     }
  372.  
  373.     if (reg.x.ax == 0xffff)
  374.       idnum = reg.x.cx;                /* Found - return tsr id */
  375.     else
  376.       idnum = -1;                      /* Not loaded */
  377.     return idnum;
  378.   }
  379.  
  380.  
  381. char midiInit()
  382.  
  383.   {
  384.     MidiDriverLoaded = CheckRes() >= 0;
  385.     if (MidiDriverLoaded)
  386.       {
  387.     CallPlayR(fcnGetCID);
  388.     MID = param[1];
  389.     BindDriver();
  390.       }
  391.     return MidiDriverLoaded;
  392.   }
  393.