jdkmidi class library documentation

Copyright © 2004 J.D. Koftinoff Software, Ltd.

Released under the GNU General Public License (GPL)




Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

src/jdkmidi_driverwin32.cpp

Go to the documentation of this file.
00001 /*
00002  *  libjdkmidi-2004 C++ Class Library for MIDI
00003  *
00004  *  Copyright (C) 2004  J.D. Koftinoff Software, Ltd.
00005  *  www.jdkoftinoff.com
00006  *  jeffk@jdkoftinoff.com
00007  *
00008  *  *** RELEASED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) April 27, 2004 ***
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 #include "jdkmidi/world.h"
00026 
00027 #ifdef WIN32
00028 #include "jdkmidi/driverwin32.h"
00029 
00030 namespace jdkmidi
00031 {
00032   
00033   
00034   MIDISequencerGUIEventNotifierWin32::MIDISequencerGUIEventNotifierWin32(
00035     HWND w,
00036     DWORD msg,
00037     WPARAM wparam_value_
00038     )
00039     :
00040     dest_window(w),
00041     window_msg(msg),
00042     wparam_value( wparam_value_ ),
00043     en(true)
00044   {
00045     
00046   }
00047   
00048   MIDISequencerGUIEventNotifierWin32::~MIDISequencerGUIEventNotifierWin32()
00049   {
00050   }
00051   
00052   
00053   void MIDISequencerGUIEventNotifierWin32::Notify(
00054     const MIDISequencer *seq,
00055     MIDISequencerGUIEvent e
00056     )
00057   {
00058     if( en )
00059     {
00060       PostMessage(
00061         dest_window,
00062         window_msg,
00063         wparam_value,
00064         (unsigned long)e
00065         );
00066     }
00067   }
00068   
00069   bool MIDISequencerGUIEventNotifierWin32::GetEnable() const
00070   {
00071     return en;
00072   }
00073   
00074   void MIDISequencerGUIEventNotifierWin32::SetEnable( bool f )
00075   {
00076     en = f;
00077   }
00078   
00079   
00080   
00081   MIDIDriverWin32::MIDIDriverWin32( int queue_size)
00082     :
00083     MIDIDriver( queue_size ),
00084     in_handle(0),
00085     out_handle(0),
00086     in_open(false),
00087     out_open(false),
00088     timer_open(false)
00089   {
00090   }
00091   
00092   MIDIDriverWin32::~MIDIDriverWin32()
00093   {
00094     StopTimer();
00095     CloseMIDIInPort();
00096     CloseMIDIOutPort();
00097   }
00098   
00099   void MIDIDriverWin32::ResetMIDIOut()
00100   {
00101     if( out_open )
00102     {
00103       midiOutReset(out_handle);
00104     }
00105   }
00106   
00107   bool MIDIDriverWin32::StartTimer( int res )
00108   {
00109     if( !timer_open )
00110     {
00111       TIMECAPS tc;
00112       
00113       if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR)
00114       {
00115         return false;
00116       }
00117       
00118       timer_res = res;
00119       
00120       if( timer_res < (int)tc.wPeriodMin )
00121         timer_res = (int)tc.wPeriodMin;
00122       
00123       if( timer_res > (int)tc.wPeriodMax )
00124         timer_res = (int)tc.wPeriodMax;
00125       
00126       
00127       timeBeginPeriod(timer_res);
00128       
00129       timer_id = timeSetEvent(
00130         res,
00131         res,
00132         win32_timer,
00133         (DWORD)this,
00134         TIME_PERIODIC
00135         );
00136       
00137       if( timer_id )
00138       {
00139         timer_open=true;
00140       }
00141     }
00142     return true;
00143   }
00144   
00145   void MIDIDriverWin32::StopTimer()
00146   {
00147     if( timer_open )
00148     {
00149       timeKillEvent( timer_id );
00150       timeEndPeriod( timer_res );
00151       timer_open = false;
00152     }
00153   }
00154   
00155   bool MIDIDriverWin32::OpenMIDIInPort( int id )
00156   {
00157     if( !in_open )
00158     {
00159       if( midiInOpen(
00160             &in_handle,
00161             id,
00162             (DWORD)win32_midi_in,
00163             (DWORD)this,
00164             CALLBACK_FUNCTION )!=0
00165         )
00166       {
00167         return false;
00168       }
00169       
00170       midiInStart( in_handle );
00171       
00172       in_open=true;
00173     }
00174     return true;
00175   }
00176   
00177   bool MIDIDriverWin32::OpenMIDIOutPort( int id )
00178   {
00179     if( !out_open )
00180     {
00181       int e = midiOutOpen(
00182         &out_handle,
00183         id,
00184         0,
00185         0,
00186         CALLBACK_NULL
00187         );
00188       
00189       if( e!=0 )
00190       {
00191         return false;
00192       }
00193       out_open=true;
00194     }
00195     return true;
00196     
00197   }
00198   
00199   void MIDIDriverWin32::CloseMIDIInPort()
00200   {
00201     if( in_open )
00202     {
00203       midiInStop( in_handle );
00204       midiInClose( in_handle );
00205       in_open = false;
00206     }
00207   }
00208   
00209   void MIDIDriverWin32::CloseMIDIOutPort()
00210   {
00211     if( out_open )
00212     {
00213       midiOutClose( out_handle );
00214       out_open=false;
00215       Reset();
00216     }
00217   }
00218   
00219   bool MIDIDriverWin32::HardwareMsgOut( const MIDITimedBigMessage &msg )
00220   {
00221     if( out_open )
00222     {
00223       // dont send sysex or meta-events
00224       
00225       if( msg.GetStatus()<0xff && msg.GetStatus()!=0xf0 )
00226       {
00227         DWORD winmsg;
00228         
00229         
00230         winmsg =
00231           (((DWORD)msg.GetStatus()&0xff)<<0)
00232           |   (((DWORD)msg.GetByte1()&0xff)<<8)
00233           | (((DWORD)msg.GetByte2()&0xff)<<16);
00234         
00235         
00236         if( midiOutShortMsg( out_handle, winmsg )!=0 )
00237         {
00238           return false;
00239         }
00240       }
00241       
00242       return true;
00243     }
00244     return false;
00245   }
00246   
00247   void CALLBACK MIDIDriverWin32::win32_timer(
00248     UINT wTimerID,
00249     UINT msg,
00250     DWORD dwUser,
00251     DWORD dw1,
00252     DWORD dw2
00253     )
00254   {
00255     MIDIDriverWin32 *self = (MIDIDriverWin32 *)dwUser;
00256     
00257     self->TimeTick( timeGetTime() );
00258   }
00259   
00260   void CALLBACK MIDIDriverWin32::win32_midi_in(
00261     HMIDIIN hMidiIn,
00262     UINT wMsg,
00263     DWORD dwInstance,
00264     DWORD dwParam1,
00265     DWORD dwParam2
00266     )
00267   {
00268     MIDIDriverWin32 *self = (MIDIDriverWin32 *)dwInstance;
00269     
00270     if( wMsg==MIM_DATA )
00271     {
00272       MIDITimedBigMessage msg;
00273       
00274       msg.SetStatus( (unsigned char)(dwParam1&0xff) );
00275       msg.SetByte1( (unsigned char)((dwParam1>>8)&0xff) );
00276       msg.SetByte2( (unsigned char)((dwParam1>>16)&0xff) );
00277       
00278       msg.SetTime( timeGetTime() );
00279       
00280       self->HardwareMsgIn( msg );
00281     }
00282   }
00283   
00284 }  
00285 #endif
00286