home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
-
- PROGRAM: DLLMidi.c
-
- PURPOSE: DLLMidi template for Sound Blaster applications
-
- FUNCTIONS:
-
- WinMain () - calls initialization function, processes message loop
- InitApplication () - initializes window data and registers window
- InitInstance () - saves instance handle and creates main window
- MainWndProc () - processes messages
- About () - processes messages for "About" dialog box
-
- COMMENTS:
-
- Windows can have several copies of your application running at the
- same time. The variable hInst keeps track of which instance this
- application is so that processing will be to the correct window.
-
- ****************************************************************************/
-
- #include "windows.h" /* required for all Windows applications */
- #include "dllmidi.h" /* specific to this program */
- #include <string.h>
- #include <sndblst.h>
-
- WORD wDLLVersion; /* verion number of DLL */
- WORD wSoundBlasterMsg; /* Sound Blaster message */
- LPSTR lpCardName; /* name of Sound Blaster card */
- HANDLE hInst; /* current instance */
- HWND hWnd; /* Main window handle. */
- BYTE cNoteNum[8]={60, 62, 64, 65, 67, 69, 71, 72};
-
- HWND hEditWnd; /* */
- RECT Rect; /* Variables */
- unsigned no_of_lines; /* for */
- unsigned count=0; /* display */
- char szEditBuf[1024]; /* purposes */
- BOOL fScroll=FALSE; /* */
-
- /****************************************************************************
-
- FUNCTION: WinMain (HANDLE, HANDLE, LPSTR, int)
-
- PURPOSE: calls initialization function, processes message loop
-
- COMMENTS:
-
- Windows recognizes this function by name as the initial entry point
- for the program. This function calls the application initialization
- routine, if no other instance of the program is running, and always
- calls the instance initialization routine. It then executes a message
- retrieval and dispatch loop that is the top-level control structure
- for the remainder of execution. The loop is terminated when a WM_QUIT
- message is received, at which time this function exits the application
- instance by returning the value passed by PostQuitMessage().
-
- If this function must abort before entering the message loop, it
- returns the conventional value NULL.
-
- ****************************************************************************/
-
- int PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
- HANDLE hInstance; /* current instance */
- HANDLE hPrevInstance; /* previous instance */
- LPSTR lpCmdLine; /* command line */
- int nCmdShow; /* show-window type (open/icon) */
- {
- MSG msg; /* message */
-
- if (!hPrevInstance) /* Other instances of app running? */
- if (!InitApplication (hInstance)) /* Initialize shared things */
- return (FALSE); /* Exits if unable to initialize */
-
- /* Perform initializations that apply to a specific instance */
-
- if (!InitInstance (hInstance, nCmdShow))
- return (FALSE);
-
- if ((wDLLVersion = sbcGetDLLVersion ()) == NULL)
- return (FALSE);
-
- if ((lpCardName = sbcGetCardName ()) == NULL)
- return (FALSE);
-
- wSoundBlasterMsg = RegisterWindowMessage ((LPSTR) "SoundBlaster");
-
- /* Acquire and dispatch messages until a WM_QUIT message is received. */
-
- while (GetMessage (&msg, /* message structure */
- NULL, /* handle of window receiving the message */
- NULL, /* lowest message to examine */
- NULL)) /* highest message to examine */
- {
- TranslateMessage (&msg); /* Translates virtual key codes */
- DispatchMessage (&msg); /* Dispatches message to window */
- }
-
- sbcTerminateDLL ();
-
- return (msg.wParam); /* Returns the value from PostQuitMessage */
- }
-
-
- /****************************************************************************
-
- FUNCTION: InitApplication (HANDLE)
-
- PURPOSE: Initializes window data and registers window class
-
- COMMENTS:
-
- This function is called at initialization time only if no other
- instances of the application are running. This function performs
- initialization tasks that can be done once for any number of running
- instances.
-
- In this case, we initialize a window class by filling out a data
- structure of type WNDCLASS and calling the Windows RegisterClass()
- function. Since all instances of this application use the same window
- class, we only need to do this when the first instance is initialized.
-
- ****************************************************************************/
-
- BOOL InitApplication (hInstance)
- HANDLE hInstance; /* current instance */
- {
- WNDCLASS wc;
-
- /* Fill in window class structure with parameters that describe the */
- /* main window. */
-
- wc.style = NULL; /* Class style(s). */
- wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */
- /* windows of this class. */
- wc.cbClsExtra = 0; /* No per-class extra data. */
- wc.cbWndExtra = 0; /* No per-window extra data. */
- wc.hInstance = hInstance; /* Application that owns the class. */
- wc.hIcon = LoadIcon (hInstance, "dllmidi");
- wc.hCursor = LoadCursor (NULL, IDC_ARROW);
- wc.hbrBackground = GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = "DLLMidiMenu"; /* Name of menu resource in .RC file. */
- wc.lpszClassName = "DLLMidiWClass"; /* Name used in call to CreateWindow. */
-
- /* Register the window class and return success/failure code. */
-
- return (RegisterClass (&wc));
- }
-
-
- /****************************************************************************
-
- FUNCTION: InitInstance (HANDLE, int)
-
- PURPOSE: Saves instance handle and creates main window
-
- COMMENTS:
-
- This function is called at initialization time for every instance of
- this application. This function performs initialization tasks that
- cannot be shared by multiple instances.
-
- In this case, we save the instance handle in a static variable and
- create and display the main program window.
-
- ****************************************************************************/
-
- BOOL InitInstance (hInstance, nCmdShow)
- HANDLE hInstance; /* Current instance identifier. */
- int nCmdShow; /* Param for first ShowWindow() call. */
- {
- /* Save the instance handle in static variable, which will be used in */
- /* many subsequence calls from this application to Windows. */
-
- hInst = hInstance;
-
- /* Create a main window for this application instance. */
-
- hWnd = CreateWindow
- (
- "DLLMidiWClass", /* See RegisterClass() call. */
- "DLLMIDI Sample Application", /* Text for window title bar. */
- WS_OVERLAPPEDWINDOW, /* Window style. */
- CW_USEDEFAULT, /* Default horizontal position. */
- CW_USEDEFAULT, /* Default vertical position. */
- CW_USEDEFAULT, /* Default width. */
- CW_USEDEFAULT, /* Default height. */
- NULL, /* Overlapped windows have no parent. */
- NULL, /* Use the window class menu. */
- hInstance, /* This instance owns this window. */
- NULL /* Pointer not needed. */
- );
-
- /* If window could not be created, return "failure" */
-
- if (!hWnd)
- return (FALSE);
-
- *szEditBuf = '\0';
-
- /* Create a child window */
-
- hEditWnd = CreateWindow("Edit",
- NULL,
- WS_CHILD | WS_VISIBLE |
- ES_MULTILINE |
- ES_AUTOHSCROLL | ES_AUTOVSCROLL,
- 0,
- 0,
- (Rect.right-Rect.left),
- (Rect.bottom-Rect.top),
- hWnd,
- IDC_EDIT, /* Child control i.d. */
- hInst,
- NULL);
-
- if (!hEditWnd)
- {
- DestroyWindow(hWnd);
- return (NULL);
- }
-
- strcpy (szEditBuf, " Time Received Midi Code");
- SetDlgItemText (hWnd, IDC_EDIT, szEditBuf);
-
- /* Make the window visible; update its client area; and return "success" */
-
- ShowWindow (hWnd, nCmdShow); /* Show the window */
- UpdateWindow (hWnd); /* Sends WM_PAINT message */
- return (TRUE); /* Returns the value from PostQuitMessage */
- }
-
- /*****************************************************************************
-
- FUNCTION: DisplayMidiCode (LONG)
-
- PURPOSE: Display MIDI code and time-stamp received
-
- *****************************************************************************/
-
- void DisplayMidiCode (wMidiCode, dwTimeStamp)
- WORD wMidiCode;
- DWORD dwTimeStamp;
- {
- char szTmpBuf[64];
- char *ptr;
-
- wsprintf (szTmpBuf, "\r %06lx %02x", dwTimeStamp, wMidiCode);
- strcat (szEditBuf, szTmpBuf);
- SetDlgItemText (hWnd, IDC_EDIT, szEditBuf);
-
- if (fScroll)
- {
- ptr = strchr (szEditBuf+28, '\r');
- strcpy (szEditBuf+27, ptr);
- }
- else
- {
- count++;
- if (count > no_of_lines) fScroll = TRUE;
- }
- }
-
- /****************************************************************************
-
- FUNCTION: MainWndProc (HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages
-
- MESSAGES:
-
- WM_COMMAND - application menu (About dialog box)
- WM_DESTROY - destroy window
-
- COMMENTS:
-
- To process the IDM_ABOUT message, call MakeProcInstance() to get the
- current instance address of the About() function. Then call Dialog
- box which will create the box according to the information in your
- dllmidi.rc file and turn control over to the About() function. When
- it returns, free the intance address.
-
- ****************************************************************************/
-
- long FAR PASCAL MainWndProc (hWnd, message, wParam, lParam)
- HWND hWnd; /* window handle */
- unsigned message; /* type of message */
- WORD wParam; /* additional information */
- LONG lParam; /* additional information */
- {
- unsigned i;
- char *ptr;
- HDC hDC;
- FARPROC lpProcAbout; /* pointer to the "About" function */
- TEXTMETRIC textmetric;
-
- switch (message)
- {
- case WM_COMMAND: /* message: command from application menu */
- switch (wParam)
- {
- case IDM_ABOUT:
- lpProcAbout = MakeProcInstance (About, hInst);
-
- DialogBox(hInst, /* current instance */
- "AboutBox", /* resource to use */
- hWnd, /* parent handle */
- lpProcAbout); /* About() instance address */
-
- FreeProcInstance (lpProcAbout);
- break;
- case IDM_INPUT:
- StartInputMidi ();
- break;
- case IDM_STOP:
- StopInputMidi ();
- break;
- case IDM_OUTPUT:
- OutputMidi ();
- break;
- default:
- return (DefWindowProc (hWnd, message, wParam, lParam));
- }
- break;
- case WM_SIZE:
- MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
- hDC = GetDC (hWnd);
- GetTextMetrics (hDC, &textmetric);
- ReleaseDC (hWnd, hDC);
- GetClientRect(hWnd, &Rect);
- if (fScroll)
- count = no_of_lines;
- no_of_lines = (Rect.bottom-Rect.top-textmetric.tmExternalLeading)/
- (textmetric.tmExternalLeading+textmetric.tmHeight)-3;
- if (fScroll)
- {
- for (i=no_of_lines-1; i<count; i++)
- {
- ptr = strchr (szEditBuf+28, '\r');
- strcpy (szEditBuf+27, ptr);
- }
- SetDlgItemText (hWnd, IDC_EDIT, szEditBuf);
- fScroll = FALSE;
- count++;
- }
- break;
- case WM_DESTROY: /* message: window being destroyed */
- PostQuitMessage(0);
- break;
-
- default:
- if (message == wSoundBlasterMsg)
- {
- switch (wParam)
- {
- case SBM_CALLBACK:
- sbcCallBack (lParam);
- break;
- case SBM_MIDI_IN:
- DisplayMidiCode ((WORD) lParam & 0xff, (DWORD) lParam >> 8);
- break;
- }
- }
- else /* Passes it on if unproccessed */
- {
- return (DefWindowProc(hWnd, message, wParam, lParam));
- }
- }
- return (NULL);
- }
-
- /*****************************************************************************
-
- FUNCTION: StartInputMidi (void)
-
- PURPOSE: Starts to input MIDI code
-
- *****************************************************************************/
-
- void StartInputMidi (void)
- {
- WORD wReturnCode;
-
- if (wReturnCode = midiOpenInputDevice (hWnd))
- DisplayErrorCode (wReturnCode);
- else
- {
- if (wReturnCode = midiStartInput ())
- DisplayErrorCode (wReturnCode);
- }
- }
-
- /*****************************************************************************
-
- FUNCTION: StopInputMidi (void)
-
- PURPOSE: Cease to input MIDI code
-
- *****************************************************************************/
-
- void StopInputMidi ()
- {
- WORD wReturnCode;
-
- if (wReturnCode = midiStopInput ())
- DisplayErrorCode (wReturnCode);
- else
- midiCloseInputDevice ();
- }
-
- /*****************************************************************************
-
- FUNCTION: OutputMidi (void)
-
- PURPOSE: Output an octave of notes starting from middle "C"
-
- *****************************************************************************/
-
- void OutputMidi ()
- {
- WORD wReturnCode;
- WORD wNote, wDelayTime;
-
- if (wReturnCode = midiOpenOutputDevice (hWnd))
- DisplayErrorCode (wReturnCode);
- else
- {
- midiOutputShortMessage (0xC0, 0x8, 0);
- midiOutputShortMessage (0xC1, 0x88, 0);
-
- for (wNote=0; wNote<8; wNote++)
- {
- midiOutputShortMessage (0x90, cNoteNum[wNote], 0x40);
- midiOutputShortMessage (0x91, cNoteNum[wNote], 0x40);
-
- for (wDelayTime=0; wDelayTime<30000; wDelayTime++);
-
- midiOutputShortMessage (0x80, cNoteNum[wNote], 0x40);
- midiOutputShortMessage (0x81, cNoteNum[wNote], 0x40);
- }
- midiCloseOutputDevice ();
- }
- }
-
- /****************************************************************************
-
- FUNCTION: DisplayErrorCode (WORD)
-
- PURPOSE: Display error code
-
- ****************************************************************************/
-
- void DisplayErrorCode (error)
- WORD error;
- {
- char szTmpBuf[20];
-
- wsprintf (szTmpBuf, "Error code = %d", error);
- MessageBeep (0);
- MessageBox (NULL, szTmpBuf, "Error...", MB_ICONINFORMATION | MB_OK);
- }
-
- /****************************************************************************
-
- FUNCTION: About (HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages for "About" dialog box
-
- MESSAGES:
-
- WM_INITDIALOG - initialize dialog box
- WM_COMMAND - Input received
-
- COMMENTS:
-
- No initialization is needed for this particular dialog box, but TRUE
- must be returned to Windows.
-
- Wait for user to click on "Ok" button, then close the dialog box.
-
- ****************************************************************************/
-
- BOOL FAR PASCAL About (hDlg, message, wParam, lParam)
- HWND hDlg; /* window handle of the dialog box */
- unsigned message; /* type of message */
- WORD wParam; /* message-specific information */
- LONG lParam;
- {
- HDC hDC;
- HDC hMemoryDC;
- char szTmpBuf[128];
- BITMAP Bitmap;
- HBITMAP hBmSBLogo;
- HBITMAP hOldBitmap;
-
- switch (message)
- {
- case WM_INITDIALOG: /* message: initialize dialog box */
- wsprintf (szTmpBuf, "Hardware Card: %s", lpCardName);
- SetDlgItemText (hDlg, IDT_CARDNAME, szTmpBuf);
- wsprintf (szTmpBuf, "DLL Version: %d.%02d", HIBYTE (wDLLVersion),
- LOBYTE (wDLLVersion));
- SetDlgItemText (hDlg, IDT_DLLVERSION, szTmpBuf);
- return (TRUE);
-
- case WM_COMMAND: /* message: received a command */
- if (wParam == IDOK /* "OK" box selected? */
- || wParam == IDCANCEL) /* System menu close command? */
- {
- EndDialog (hDlg, TRUE); /* Exits the dialog box */
- return (TRUE);
- }
- break;
- case WM_PAINT:
- hBmSBLogo = LoadBitmap (hInst, "sbct");
- GetObject (hBmSBLogo, sizeof (BITMAP), (LPSTR) &Bitmap);
- hDC = GetDC (hDlg);
- hMemoryDC = CreateCompatibleDC (hDC);
- hOldBitmap = SelectObject (hMemoryDC, hBmSBLogo);
- if (hOldBitmap)
- {
- BitBlt (hDC, 3, 3, Bitmap.bmWidth, Bitmap.bmHeight,
- hMemoryDC, 0, 0, SRCCOPY);
- SelectObject (hMemoryDC, hOldBitmap);
- }
- DeleteDC (hMemoryDC);
- ReleaseDC (hDlg, hDC);
- DeleteObject (hBmSBLogo);
- break;
- }
- return (FALSE); /* Didn't process a message */
- }
-