home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / MSWSRC35 / MMWIND.CPP < prev    next >
C/C++ Source or Header  |  1993-10-12  |  10KB  |  416 lines

  1. #include "allwind.h"
  2.  
  3. #ifdef ISWIN31
  4. HMIDIOUT hMidiOut = 0;
  5. #endif
  6.  
  7. NODE *lsound(NODE *arg)
  8.    {
  9.    int duration;
  10.    int hertz;
  11.    int frac;
  12.    int i;
  13.    int odd;
  14.    
  15.    NODE *args;
  16.    
  17.    long frequency;
  18.    
  19.    /* open sound and get arg list */
  20.    
  21.    OpenSound();
  22.    
  23.    args = car(arg);
  24.    
  25.    /* must be a list that contains something */
  26.    
  27.    if (is_list(args) && (args != NIL))
  28.       {
  29.       
  30.       /* count items in list and check that they are pairs */
  31.       
  32.       arg = args;
  33.       i=0;
  34.       odd=0;
  35.       
  36.       while (arg != NIL)
  37.          {
  38.          if (arg != NIL) arg=cdr(arg);
  39.          if (arg != NIL) arg=cdr(arg); else odd = 1;
  40.          i++;
  41.          }
  42.       
  43.       /* if sound creation ok and we have pairs continue */
  44.       
  45.       if (!SetVoiceQueueSize(1,i*6) && (!odd))
  46.          {
  47.          
  48.          arg = args;
  49.          
  50.          /* fill queue with freq/duration pairs */
  51.          
  52.          while (arg != NIL)
  53.             {
  54.             hertz = int_arg(arg);
  55.             if (cdr(arg) != NIL) duration = int_arg(arg=cdr(arg));
  56.             frequency = hertz*0x10000L;
  57.             SetVoiceSound(1, frequency, duration);
  58.             if (arg != NIL) arg=cdr(arg);
  59.             }
  60.          
  61.          /* play it */
  62.          
  63.          StartSound();
  64.          WaitSoundState(S_QUEUEEMPTY);
  65.          StopSound();           
  66.          CloseSound();
  67.          }
  68.       else
  69.          {
  70.          MessageBox(MainHWindow, "Too long or Not paired", "Sound Error", MB_OK | MB_ICONEXCLAMATION);
  71.          }
  72.       }
  73.    else
  74.       {
  75.       MessageBox(MainHWindow, "Bad argument", "Sound Error", MB_OK | MB_ICONEXCLAMATION);
  76.       }
  77.    
  78.    return (UNBOUND);
  79.    }
  80.  
  81. NODE *lmidiopen(NODE *args)
  82.    {
  83. #ifdef ISWIN31
  84.    
  85.    UINT id;
  86.    UINT MidiError;
  87.    
  88.    MIDIOUTCAPS moc;
  89.    
  90.    NODE *targ;
  91.    NODE *val = UNBOUND;
  92.    
  93.    char MidiErrorBuffer[MAX_BUFFER_SIZE];
  94.    
  95.    /* if not open open it */
  96.    
  97.    if (!hMidiOut)
  98.       {
  99.       
  100.       id = MIDIMAPPER;
  101.       
  102.       if (args != NIL)
  103.          {
  104.          id = int_arg(args);
  105.          if (id > midiOutGetNumDevs())
  106.             {
  107.             MessageBox(MainHWindow, "Invalid Midi device", "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  108.             }
  109.          }
  110.       
  111.       MidiError = midiOutGetDevCaps(id, &moc, sizeof(moc));
  112.       
  113.       if (!MidiError) MidiError = midiOutOpen(&hMidiOut, id, NULL, 0L, 0L);
  114.       
  115.       if (MidiError) 
  116.          {
  117.          midiOutGetErrorText(MidiError, MidiErrorBuffer, MAX_BUFFER_SIZE);
  118.          MessageBox(MainHWindow, MidiErrorBuffer, "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  119.          }
  120.       else
  121.          {
  122.      targ = make_strnode(moc.szPname, NULL, strlen(moc.szPname), STRING, strnzcpy);
  123.          val = parser(targ, FALSE);
  124.          return(val);
  125.          }
  126.       }
  127.    else
  128.       {
  129.       MessageBox(MainHWindow, "Already Open", "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  130.       }
  131.    
  132. #else
  133.    MessageBox(MainHWindow, "Only supported on Windows 3.1", "Sorry", MB_OK);
  134. #endif
  135.    
  136.    return (UNBOUND);
  137.    }
  138.  
  139. NODE *lmidiclose(NODE *args)
  140.    {
  141. #ifdef ISWIN31
  142.    
  143.    UINT MidiError;
  144.    char MidiErrorBuffer[MAX_BUFFER_SIZE];
  145.    
  146.    /* if open close it */
  147.    
  148.    if (hMidiOut)
  149.       {
  150.       
  151.       MidiError = midiOutClose(hMidiOut);
  152.       
  153.       hMidiOut = 0;
  154.       
  155.       if (MidiError) 
  156.          {
  157.          midiOutGetErrorText(MidiError, MidiErrorBuffer, MAX_BUFFER_SIZE);
  158.          MessageBox(MainHWindow, MidiErrorBuffer, "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  159.          }
  160.       }
  161.    else
  162.       {
  163.       MessageBox(MainHWindow, "Already closed", "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  164.       }
  165.    
  166. #else
  167.    MessageBox(MainHWindow, "Only supported on Windows 3.1", "Sorry", MB_OK);
  168. #endif
  169.    
  170.    return (UNBOUND);
  171.    }
  172.  
  173. NODE *lmidimessage(NODE *arg)
  174.    {
  175. #ifdef ISWIN31
  176.    
  177.    NODE *args;
  178.    UINT MidiError;
  179.    char MidiErrorBuffer[MAX_BUFFER_SIZE];
  180.    
  181.    int byte0;
  182.    int byte1;
  183.    int byte2;
  184.    int i;
  185.    int j;
  186.    
  187.    union
  188.       {
  189.       long mylong;
  190.       char mybyte[4];
  191.       } bytetolong;
  192.    
  193.    MIDIHDR *MidiOutHdr;
  194.    BYTE *MidiOutData;
  195.    
  196.    HANDLE DataHandle;  
  197.    HANDLE HdrHandle;  
  198.    
  199.    /* if midi open continue */
  200.    
  201.    if (hMidiOut)
  202.       {
  203.       args = car(arg);
  204.       
  205.       /* if a list with something in it continue */
  206.       
  207.       if (is_list(args) && (args != NIL))
  208.          {
  209.          
  210.          /* if not system exclusive then use shortmsg else use longmsg */
  211.          
  212.          if (int_arg(args) != 0xF0)
  213.             {
  214.             
  215.             /* pack 3 bytes at a time and send them as short messages */
  216.             
  217.             arg = args;
  218.             
  219.             while (arg != NIL)
  220.                {
  221.                bytetolong.mylong = 0L;
  222.                bytetolong.mybyte[0] = int_arg(arg);
  223.                if (cdr(arg) != NIL) bytetolong.mybyte[1] = int_arg(arg=cdr(arg));
  224.                if (cdr(arg) != NIL) bytetolong.mybyte[2] = int_arg(arg=cdr(arg));
  225.                MidiError = midiOutShortMsg(hMidiOut, bytetolong.mylong);
  226.                if (MidiError) break;
  227.                if (arg != NIL) arg=cdr(arg);
  228.                }
  229.             
  230.             }
  231.          else
  232.             {
  233.             
  234.             /* count elements in list so we can allocate buffer */
  235.             
  236.             i=0;
  237.             arg = args;
  238.             
  239.             while (arg != NIL)
  240.                {
  241.                arg=cdr(arg);
  242.                i++;
  243.                }
  244.             
  245.             /* allocate structure buffer */
  246.             
  247.             HdrHandle = (HANDLE)GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT, sizeof(MIDIHDR));
  248.             MidiOutHdr = (MIDIHDR *)GlobalLock((HGLOBAL)HdrHandle);
  249.             
  250.             DataHandle = (HANDLE)GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT, i);
  251.             MidiOutData = (BYTE *)GlobalLock((HGLOBAL)DataHandle);
  252.             
  253.             /* pack the buffer array and set size */
  254.             
  255.             arg = args;
  256.             
  257.             for (j=0;j<i;j++)
  258.                {
  259.                MidiOutData[j] = int_arg(arg);
  260.                arg=cdr(arg);
  261.                }
  262.             
  263.             MidiOutHdr->dwBufferLength = i;
  264.             
  265.             MidiOutHdr->lpData = MidiOutData;
  266.             
  267.             /* prepare it, send it out, and unprepare it */
  268.             
  269.             MidiError = midiOutPrepareHeader(hMidiOut, MidiOutHdr, sizeof(MIDIHDR));
  270.             if (!MidiError) MidiError = midiOutLongMsg(hMidiOut, MidiOutHdr, sizeof(MIDIHDR));
  271.             if (!MidiError) MidiError = midiOutUnprepareHeader(hMidiOut, MidiOutHdr, sizeof(MIDIHDR));
  272.             
  273.             /* free buffer and struct */
  274.             
  275.             GlobalUnlock(DataHandle);
  276.             GlobalFree(DataHandle);
  277.             
  278.             GlobalUnlock(HdrHandle);
  279.             GlobalFree(HdrHandle);
  280.             }
  281.          
  282.          /* if midi error let 'em know */
  283.          
  284.          if (MidiError) 
  285.             {
  286.             midiOutGetErrorText(MidiError, MidiErrorBuffer, MAX_BUFFER_SIZE);
  287.             MessageBox(MainHWindow, MidiErrorBuffer, "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  288.             }
  289.          }
  290.       else
  291.          {
  292.          MessageBox(MainHWindow, "Bad argument", "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  293.          }
  294.       }
  295.    else
  296.       {
  297.       MessageBox(MainHWindow, "Not Open", "Midi Error", MB_OK | MB_ICONEXCLAMATION);
  298.       }
  299.    
  300. #else
  301.    MessageBox(MainHWindow, "Only supported on Windows 3.1", "Sorry", MB_OK);
  302. #endif
  303.    
  304.    return (UNBOUND);
  305.    }
  306.  
  307. NODE *lmci(NODE *args)
  308.    {
  309. #ifdef ISWIN31
  310.    
  311.    NODE *targ;
  312.    NODE *val = UNBOUND;
  313.    
  314.    DWORD MciError;
  315.    
  316.    char textbuf[MAX_BUFFER_SIZE];
  317.    char MciReturnBuffer[MAX_BUFFER_SIZE];
  318.    char MciErrorBuffer[MAX_BUFFER_SIZE];
  319.    char callback[MAX_BUFFER_SIZE];
  320.    
  321.    /* get mci command string */
  322.    
  323.    cnv_strnode_string(textbuf,args);
  324.    
  325.    /* check for optional callback routine */
  326.    
  327.    if (cdr(args) != NIL)
  328.       {
  329.       cnv_strnode_string(callback, cdr(args));
  330.       strcpy(mci_callback,callback);
  331.       }
  332.    
  333.    MciReturnBuffer[0] = '\0';
  334.    
  335.    /* send out command */
  336.    
  337.    MciError = mciSendString(textbuf, MciReturnBuffer, MAX_BUFFER_SIZE, MainHWindow);
  338.    
  339.    /* if error let user know else continue */
  340.    
  341.    if (MciError) 
  342.       {
  343.       mciGetErrorString(MciError, MciErrorBuffer, MAX_BUFFER_SIZE);
  344.       MessageBox(MainHWindow, MciErrorBuffer, "MCI Error", MB_OK | MB_ICONEXCLAMATION);
  345.       }
  346.    else
  347.       {
  348.       
  349.       /* if something was returned then return it to user */
  350.       
  351.       if (strlen(MciReturnBuffer))
  352.          {
  353.          targ = make_strnode(MciReturnBuffer, NULL, strlen(MciReturnBuffer), STRING, strnzcpy);
  354.          val = parser(targ, FALSE);
  355.          return(val);
  356.          }  
  357.       }
  358. #else
  359.    MessageBox(MainHWindow, "Only supported on Windows 3.1", "Sorry", MB_OK);
  360. #endif
  361.    
  362.    return (UNBOUND);
  363.    }
  364.  
  365. NODE *lsettimer(NODE *args)
  366.    {
  367.    char callback[MAX_BUFFER_SIZE];
  368.    WORD delay;
  369.    int id;   
  370.    
  371.    // get id and if valid continue
  372.    
  373.    id = int_arg(args);
  374.    
  375.    if ((id > 0) && (id < 32))
  376.       {
  377.       
  378.       // get delay callback
  379.       
  380.       delay = int_arg(args=cdr(args));
  381.       
  382.       cnv_strnode_string(callback, args=cdr(args));
  383.       
  384.       if (timer_callback[id] == NULL) timer_callback[id] = (char *)malloc(MAX_BUFFER_SIZE);
  385.       strcpy(timer_callback[id],callback);
  386.       
  387.       // if not set sucessfully error
  388.       
  389.       if (!SetTimer(MainHWindow, id, delay, NULL))
  390.          {
  391.          MessageBox(MainHWindow, "Too Many Timers", "Error", MB_ICONEXCLAMATION | MB_OK);
  392.          }
  393.       }
  394.    else
  395.       {
  396.       MessageBox(MainHWindow, "Bad Timer Id", "Error", MB_ICONEXCLAMATION | MB_OK);
  397.       }   
  398.    return (UNBOUND);
  399.    }
  400.  
  401. NODE *lcleartimer(NODE *args)
  402.    {
  403.    int id;   
  404.    
  405.    // get args
  406.    
  407.    id = int_arg(args);
  408.    
  409.    // if timer was not set let user know
  410.    
  411.    if (!KillTimer(MainHWindow, id))
  412.    MessageBox(MainHWindow, "Timer not found", "Error", MB_ICONEXCLAMATION | MB_OK);
  413.    
  414.    return (UNBOUND);
  415.    }
  416.