home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / beaversweeper_v101.zip / src / MIDIInput.cpp < prev    next >
C/C++ Source or Header  |  2003-01-06  |  2KB  |  85 lines

  1. #include "stdafx.h"
  2. #include <mmsystem.h>
  3. #include "MIDIInput.h"
  4.  
  5. static DWORD WINAPI MIDIThreadProc( LPVOID lpParameter )
  6. {
  7.     MIDIInput *inp = (MIDIInput *) lpParameter;
  8.     inp->thread();
  9.     ExitThread(0);
  10.     return 0;
  11. }
  12.  
  13. MIDIInput::MIDIInput()
  14. {
  15.     m_curdevice = 0;
  16.     m_handle = 0;
  17.     m_isopen = FALSE;
  18.     m_msgdest = 0;
  19.  
  20.     // Skapa trσd    
  21.     m_thread = CreateThread( 0, 0, &MIDIThreadProc, this, 0, &m_threadid );
  22. }
  23.  
  24. MIDIInput::~MIDIInput()
  25. {
  26.     // Vσld!
  27.     PostThreadMessage( m_threadid, WM_QUIT, 0, 0 );
  28.     if (WAIT_OBJECT_0 != WaitForSingleObject( m_thread, 1000 )) 
  29.         TerminateThread( m_thread, 0 );
  30. }
  31.  
  32. void MIDIInput::thread(void)
  33. {
  34.     MSG msg;
  35.     while (GetMessage(&msg,0,0,0)) {
  36.         if (m_msgdest) 
  37.             PostMessage( m_msgdest, msg.message, msg.wParam, msg.lParam );
  38.     }
  39. }
  40.  
  41. void MIDIInput::setmsgdest( HWND hwnd )
  42. {
  43.     m_msgdest = hwnd;
  44. }
  45.  
  46. void MIDIInput::endmsgdest( HWND hwnd )
  47. {
  48.     if (m_msgdest == hwnd) 
  49.         m_msgdest = 0;
  50. }
  51.  
  52. void MIDIInput::start( UINT device )
  53. {
  54.     if (TRUE == m_isopen) {
  55.         MIDIInput::stop();
  56.     }
  57.  
  58.     m_curdevice = device;
  59.  
  60.     if (MMSYSERR_NOERROR != midiInOpen( &m_handle, m_curdevice, (DWORD)m_threadid, 0, CALLBACK_THREAD ))
  61.         MessageBox( NULL, _T("Error!"), _T("MIDI Error!"), MB_OK );
  62.     else {
  63.         if (MMSYSERR_NOERROR == midiInStart( m_handle )) {
  64.             m_isopen = TRUE;            
  65.         }
  66.     }
  67.  
  68.     
  69.  
  70.     
  71. }
  72.  
  73. void MIDIInput::stop( void )
  74. {
  75.     if (FALSE == m_isopen)
  76.         return;
  77.     
  78.     midiInStop( m_handle );
  79.     midiInClose( m_handle );
  80.  
  81.     m_isopen = FALSE;
  82. }
  83.  
  84.  
  85.