home *** CD-ROM | disk | FTP | other *** search
/ CICA 1992 November / CICA_MS_Windows_CD-ROM_Walnut_Creek_November_1992.iso / win3 / sounds / winplay.zoo / winplay.c < prev    next >
Text File  |  1991-02-06  |  16KB  |  591 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Winplay
  4.  
  5.     PURPOSE: Windows Polyphonic Music Player 1.0
  6.  
  7.         Copyright (C) Sergey Ryzhkov
  8.                   Moscow  1991.
  9.  
  10.         Program is placed in the public domain by.
  11.         Permission is hereby granted to copy, modify,
  12.         and otherwise use this program except for profit.
  13.  
  14.         This is the windows implementation of some algorithms
  15.         designed by A.Bogatyrev, Mike Talvola and Steve Muenter
  16.         (see later).
  17.  
  18. ****************************************************************************/
  19.  
  20. #include <windows.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <dos.h>
  25. #include <conio.h>
  26. #include "winplay.h"
  27.  
  28. HANDLE hInst;
  29.  
  30. HWND hEditWnd;                                  /* handle to edit window */
  31. HWND hwnd;                                      /* handle to main window */
  32.  
  33. char FileName[128];
  34. char PathName[128];
  35. char OpenName[128];
  36. char DefPath[128];
  37. char DefSpec[13] = "*.pol";
  38. char DefExt[] = ".pol";
  39. char str[255];
  40.  
  41. char DefaultName[128];
  42.  
  43. /****************************************************************************
  44.  
  45.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  46.  
  47.     PURPOSE: calls initialization function, processes message loop
  48.  
  49. ****************************************************************************/
  50.  
  51. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  52. HANDLE hInstance;
  53. HANDLE hPrevInstance;
  54. LPSTR lpCmdLine;
  55. int nCmdShow;
  56. {
  57.     MSG msg;
  58.     int i;
  59.  
  60.     if (!hPrevInstance)
  61.         if (!InitApplication(hInstance))
  62.             return (FALSE);
  63.  
  64.     for ( i = 0 ;
  65.           (DefaultName[i] = lpCmdLine[i]) && i < sizeof(DefaultName)-1 ;
  66.           i ++ );
  67.  
  68.     if (!InitInstance(hInstance, nCmdShow))
  69.         return (FALSE);
  70.  
  71.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  72.         TranslateMessage(&msg);
  73.         DispatchMessage(&msg);
  74.     }
  75.     return (msg.wParam);
  76. }
  77.  
  78.  
  79. /****************************************************************************
  80.  
  81.     FUNCTION: InitApplication(HANDLE)
  82.  
  83.     PURPOSE: Initializes window data and registers window class
  84.  
  85. ****************************************************************************/
  86.  
  87. BOOL InitApplication(hInstance)
  88. HANDLE hInstance;
  89. {
  90.     WNDCLASS  wc;
  91.  
  92.     wc.style = NULL;
  93.     wc.lpfnWndProc = MainWndProc;
  94.     wc.cbClsExtra = 0;
  95.     wc.cbWndExtra = 0;
  96.     wc.hInstance = hInstance;
  97.     wc.hIcon = LoadIcon(hInstance, "WinPlayIcon");
  98.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  99.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  100.     wc.lpszMenuName = "WinPlayMenu";
  101.     wc.lpszClassName = "WinPlayWClass";
  102.  
  103.     return (RegisterClass(&wc));
  104. }
  105.  
  106.  
  107. /****************************************************************************
  108.  
  109.     FUNCTION:  InitInstance(HANDLE, int)
  110.  
  111.     PURPOSE:  Saves instance handle and creates main window
  112.  
  113. ****************************************************************************/
  114.  
  115. BOOL InitInstance(hInstance, nCmdShow)
  116.     HANDLE          hInstance;
  117.     int             nCmdShow;
  118. {
  119.     hInst = hInstance;
  120.  
  121.     hwnd = CreateWindow(
  122.         "WinPlayWClass",
  123.         "Windows Music Play",
  124.         WS_OVERLAPPEDWINDOW,
  125.         CW_USEDEFAULT,
  126.         CW_USEDEFAULT,
  127.         200,
  128.         50,
  129.         NULL,
  130.         NULL,
  131.         hInstance,
  132.         NULL
  133.     );
  134.  
  135.     if (!hwnd)
  136.         return (FALSE);
  137.  
  138.     if (DefaultName[0] == 0) {
  139.         ShowWindow(hwnd, nCmdShow);
  140.         UpdateWindow(hwnd);
  141.     }
  142.     return (TRUE);
  143.  
  144. }
  145.  
  146. /****************************************************************************
  147.  
  148.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  149.  
  150.     PURPOSE:  Processes messages
  151.  
  152.     MESSAGES:
  153.  
  154.         WM_COMMAND    - application menu (About dialog box)
  155.         WM_DESTROY    - destroy window
  156.  
  157. ****************************************************************************/
  158.  
  159. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  160. HWND hWnd;
  161. unsigned message;
  162. WORD wParam;
  163. LONG lParam;
  164. {
  165.     FARPROC lpProcAbout, lpOpenDlg;
  166.  
  167.     int Return;
  168.  
  169.     switch (message) {
  170.         case WM_CREATE:
  171.             if ( DefaultName[0] ) {
  172.                 play (DefaultName);
  173.                 PostQuitMessage(0);
  174.             } else
  175.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  176.             break;
  177.         case WM_COMMAND:
  178.             switch (wParam) {
  179.                 case IDM_ABOUT:
  180.                     lpProcAbout = MakeProcInstance(About, hInst);
  181.                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  182.                     FreeProcInstance(lpProcAbout);
  183.                     break;
  184.  
  185.                 case IDM_PLAY:
  186.                     /* Call OpenDlg() to get the filename */
  187.  
  188.                     lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  189.                     Return = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  190.                     FreeProcInstance(lpOpenDlg);
  191.  
  192.                     if (Return)
  193.                        /*
  194.                        MessageBox (
  195.                              GetFocus(),
  196.                              OpenName,
  197.                              "Play File",
  198.                              MB_ICONASTERISK | MB_OK);
  199.                        */
  200.                        play (OpenName);
  201.                     break;
  202.             }
  203.             break;
  204.  
  205.         case WM_SETFOCUS:
  206.             SetFocus (hEditWnd);
  207.             break;
  208.  
  209.         case WM_SIZE:
  210.             MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  211.             break;
  212.  
  213.         case WM_DESTROY:
  214.             PostQuitMessage(0);
  215.             break;
  216.  
  217.         default:
  218.             return (DefWindowProc(hWnd, message, wParam, lParam));
  219.     }
  220.     return (NULL);
  221. }
  222.  
  223. /****************************************************************************
  224.  
  225.     FUNCTION: OpenDlg(HWND, unsigned, WORD, LONG)
  226.  
  227.     PURPOSE: Let user select a file, and return.  Open code not provided.
  228.  
  229. ****************************************************************************/
  230.  
  231. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  232. HWND hDlg;
  233. unsigned message;
  234. WORD wParam;
  235. LONG lParam;
  236. {
  237.     HANDLE hFile=1;     /* Temp value for return */
  238.  
  239.     switch (message) {
  240.         case WM_COMMAND:
  241.             switch (wParam) {
  242.  
  243.                 case IDC_LISTBOX:
  244.                     switch (HIWORD(lParam)) {
  245.  
  246.                         case LBN_SELCHANGE:
  247.                             /* If item is a directory name, append "*.*" */
  248.                             if (DlgDirSelect(hDlg, str, IDC_LISTBOX))
  249.                                 strcat(str, DefSpec);
  250.  
  251.                             SetDlgItemText(hDlg, IDC_EDIT, str);
  252.                             SendDlgItemMessage(hDlg,
  253.                                 IDC_EDIT,
  254.                                 EM_SETSEL,
  255.                                 NULL,
  256.                                 MAKELONG(0, 0x7fff));
  257.                             break;
  258.  
  259.                         case LBN_DBLCLK:
  260.                             goto openfile;
  261.                     }
  262.                     return (TRUE);
  263.  
  264.                 case IDOK:
  265. openfile:
  266.                     GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  267.                     if (strchr(OpenName, '*') || strchr(OpenName, '?')) {
  268.                         SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  269.                             (LPSTR) OpenName);
  270.                         if (str[0])
  271.                             strcpy(DefPath, str);
  272.                         ChangeDefExt(DefExt, DefSpec);
  273.                         UpdateListBox(hDlg);
  274.                         return (TRUE);
  275.                     }
  276.  
  277.                     if (!OpenName[0]) {
  278.                         MessageBox(hDlg, "No filename specified.",
  279.                             NULL, MB_OK | MB_ICONHAND);
  280.                         return (TRUE);
  281.                     }
  282.  
  283.                     AddExt(OpenName, DefExt);
  284.  
  285.                     /* The routine to open the file would go here, and the */
  286.                     /* file handle would be returned instead of NULL.           */
  287.                     EndDialog(hDlg, hFile);
  288.                     return (TRUE);
  289.  
  290.                 case IDCANCEL:
  291.                     EndDialog(hDlg, NULL);
  292.                     return (FALSE);
  293.             }
  294.             break;
  295.  
  296.         case WM_INITDIALOG:                        /* message: initialize    */
  297.             UpdateListBox(hDlg);
  298.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  299.             SendDlgItemMessage(hDlg,               /* dialog handle      */
  300.                 IDC_EDIT,                          /* where to send message  */
  301.                 EM_SETSEL,                         /* select characters      */
  302.                 NULL,                              /* additional information */
  303.                 MAKELONG(0, 0x7fff));              /* entire contents      */
  304.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  305.             return (FALSE); /* Indicates the focus is set to a control */
  306.     }
  307.     return FALSE;
  308. }
  309.  
  310. /****************************************************************************
  311.  
  312.     FUNCTION: UpdateListBox(HWND);
  313.  
  314.     PURPOSE: Update the list box of OpenDlg
  315.  
  316. ****************************************************************************/
  317.  
  318. void UpdateListBox(hDlg)
  319. HWND hDlg;
  320. {
  321.     strcpy(str, DefPath);
  322.     strcat(str, DefSpec);
  323.     DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  324.  
  325.     /* To ensure that the listing is made for a subdir. of
  326.      * current drive dir...
  327.      */
  328.     if (!strchr (DefPath, ':'))
  329.     DlgDirList(hDlg, DefSpec, IDC_LISTBOX, IDC_PATH, 0x4010);
  330.  
  331.     /* Remove the '..' character from path if it exists, since this
  332.      * will make DlgDirList move us up an additional level in the tree
  333.      * when UpdateListBox() is called again.
  334.      */
  335.     if (strstr (DefPath, ".."))
  336.     DefPath[0] = '\0';
  337.  
  338.     SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  339. }
  340.  
  341. /****************************************************************************
  342.  
  343.     FUNCTION: ChangeDefExt(PSTR, PSTR);
  344.  
  345.     PURPOSE: Change the default extension
  346.  
  347. ****************************************************************************/
  348.  
  349. void ChangeDefExt(Ext, Name)
  350. PSTR Ext, Name;
  351. {
  352.     PSTR pTptr;
  353.  
  354.     pTptr = Name;
  355.     while (*pTptr && *pTptr != '.')
  356.         pTptr++;
  357.     if (*pTptr)
  358.         if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  359.             strcpy(Ext, pTptr);
  360. }
  361.  
  362. /****************************************************************************
  363.  
  364.     FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR)
  365.  
  366.     PURPOSE: Separate filename and pathname
  367.  
  368. ****************************************************************************/
  369.  
  370. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  371. HWND hDlg;
  372. LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  373. {
  374.     LPSTR lpTmp;
  375.     char  cTmp;
  376.  
  377.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  378.     while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  379.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  380.     if (*lpTmp != ':' && *lpTmp != '\\') {
  381.         lstrcpy(lpDestFileName, lpSrcFileName);
  382.         lpDestPath[0] = 0;
  383.         return;
  384.     }
  385.     lstrcpy(lpDestFileName, lpTmp + 1);
  386.     cTmp = *(lpTmp + 1);
  387.     lstrcpy(lpDestPath, lpSrcFileName);
  388.      *(lpTmp + 1) = cTmp;
  389.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  390. }
  391.  
  392. /****************************************************************************
  393.  
  394.     FUNCTION: AddExt(PSTR, PSTR);
  395.  
  396.     PURPOSE: Add default extension
  397.  
  398. /***************************************************************************/
  399.  
  400. void AddExt(Name, Ext)
  401. PSTR Name, Ext;
  402. {
  403.     PSTR pTptr;
  404.  
  405.     pTptr = Name;
  406.     while (*pTptr && *pTptr != '.')
  407.         pTptr++;
  408.     if (*pTptr != '.')
  409.         strcat(Name, Ext);
  410. }
  411.  
  412. /****************************************************************************
  413.  
  414.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  415.  
  416.     PURPOSE:  Processes messages for "About" dialog box
  417.  
  418.     MESSAGES:
  419.  
  420.         WM_INITDIALOG - initialize dialog box
  421.         WM_COMMAND    - Input received
  422.  
  423. ****************************************************************************/
  424.  
  425. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  426. HWND hDlg;
  427. unsigned message;
  428. WORD wParam;
  429. LONG lParam;
  430. {
  431.     switch (message) {
  432.         case WM_INITDIALOG:
  433.             return (TRUE);
  434.  
  435.         case WM_COMMAND:
  436.         if (wParam == IDOK
  437.                 || wParam == IDCANCEL) {
  438.                 EndDialog(hDlg, TRUE);
  439.                 return (TRUE);
  440.             }
  441.             break;
  442.     }
  443.     return (FALSE);
  444. }
  445.  
  446. /****************************************************************************
  447.  *
  448.  *      tri()    - play 3-voice music
  449.  *
  450.  *      Copyright (C) A.Bogatyrev (abs)
  451.  *                 Moscow  1990.
  452.  *      Program is placed in the public domain by: ABS.
  453.  *        Permission is hereby granted to copy, modify,
  454.  *        and otherwise use this program except for profit.
  455.  *
  456. ****************************************************************************/
  457.  
  458. #include <stdlib.h>
  459. #include <stdio.h>
  460. #include <dos.h>
  461. #include <conio.h>
  462.  
  463. # define SPEAKER_PORT   0x61
  464. # define SAY            0x48
  465. # define SHUTUP        (0x48|0x2)
  466. # define SPEAK( c )     outp(SPEAKER_PORT, ( 0x8000 & c ) ? SAY : SHUTUP)
  467. /* short must has 16 bits */
  468.  
  469. void tri ( tune )
  470.     short *tune;
  471. {
  472.     short Tempo;
  473.     register short TempoCount;
  474.     register short Duration;
  475.  
  476.     short Value, sValue;
  477.     register short Count1, Count2, Count3; /* voice period count */
  478.     register short Voice1, Voice2, Voice3; /* voice period data */
  479.  
  480.     Tempo = TempoCount = 0x1fff;
  481.     Voice1 = Voice2 = Voice3 = 0;
  482.     Count1 = Count2 = Count3 = 0;
  483.  
  484.     for( ;; ){
  485.         /*
  486.         if ( kbhit() && getch() == '\033' ) {
  487.             enable ();
  488.             return;
  489.         }
  490.         */
  491.         sValue = *tune++;
  492.         Value = sValue & 0x1FFF;
  493.  
  494.         switch( sValue & 0xE000 ){
  495.         case 0:
  496.             _enable();
  497.             return;         /* EOV */
  498.  
  499.         case 0x4000:
  500.             Tempo = TempoCount = Value;
  501.             continue;
  502.  
  503.         case 0x6000:
  504.             continue;          /* nop */
  505.  
  506.         case 0x8000:
  507.             Voice1 = Value;
  508.             continue;
  509.  
  510.         case 0xA000:
  511.             Voice2 = Value;
  512.             continue;
  513.  
  514.         case 0xC000:
  515.         case 0xE000:
  516.             Voice3 = Value;
  517.             continue;
  518.  
  519.         case 0x2000:
  520.             Duration = Value;
  521.             _disable();        /* cli */
  522.             for( ;; ){
  523.                 TempoCount --;
  524.                 if( TempoCount <= 0 ){
  525.                     TempoCount = Tempo;
  526.                     Duration --;
  527.                     if( Duration <= 0 ){
  528.                         _enable();    /* sti */
  529.                         break;        /* for(;;) */
  530.                     }
  531.                 }
  532.         /* play: */
  533.                 Count3 += Voice3;
  534.                 SPEAK( Count3 );
  535.  
  536.                 Count2 += Voice2;
  537.                 SPEAK( Count2 );
  538.  
  539.                 Count1 += Voice1;
  540.                 SPEAK( Count1 );
  541.             }
  542.             continue;
  543.         } /* endsw */
  544.     } /* endfor */
  545. }
  546.  
  547. /****************************************************************************
  548.  
  549.           Polyphonic Music Player v1.2
  550.     Written in DeSmet C and placed in the public domain
  551.         by:  Mike Talvola
  552.              Agoura Hills, CA
  553.  
  554.     Permission is hereby granted to copy, modify, and
  555.     otherwise use this program except for profit.
  556.  
  557.  
  558.     Inspired by "Polyphonic Music on the IBM PC"
  559.          by:  Steve Muenter
  560. ***************************************************************************/
  561.  
  562.  
  563. #define TRUE    1
  564. #define FALSE   0
  565.  
  566. short tune[5000];
  567.  
  568. char name[80];
  569. FILE *tfile;
  570.  
  571. void play (BYTE * szName)
  572. {
  573.     int tindex;
  574.     static char b[128];
  575.  
  576.     strcpy(name, szName);
  577.     if (strchr (name,'.') == NULL)
  578.         strcat(name, ".POL");
  579.     tfile = fopen(name, "r");
  580.     if (tfile == 0) {
  581.         sprintf(b, "Can't open song file '%s'", name);
  582.         MessageBox (GetFocus(), (LPSTR)b, (LPSTR)NULL, MB_OK);
  583.         return;
  584.     }
  585.     fgets(name, 80, tfile);
  586.     tindex = 0;
  587.     while (fscanf(tfile, "%d", &tune[tindex++]) != EOF);
  588.     fclose(tfile);
  589.     tri(&tune[1]);
  590. }
  591.