00001 #ifndef TOOL_MIDI_H
00002 #define TOOL_MIDI_H
00003
00004 #ifdef USE_MIDI
00005
00006 #include <string>
00007
00008 #ifdef __WIN32__
00009 # include <OpenTracker/tool/midiw32.h>
00010 #else
00011
00012 #endif // WIN32
00013
00014 #include <iostream>
00015 #include <sstream>
00016 #include <vector>
00017 #include <OpenTracker/core/OtException.h>
00018 namespace ot{
00019
00020 class MidiEx: public OtException{
00021 public:
00022 MidiEx(std::string e) throw():OtException(e)
00023 { };
00024
00025 };
00026
00027 class MidiInHandler {
00028 public:
00029 virtual ~MidiInHandler(){};
00030
00031 virtual void handleShortMsg( unsigned long msg, unsigned long timestamp ) {};
00032
00033 virtual void onShortMsgError( unsigned long msg, unsigned long timestamp) {};
00034
00035 virtual void handleLongMsg( unsigned long msg, unsigned long timestamp) {};
00036
00037 virtual void onLongMsgError( unsigned long msg, unsigned long timestamp) {};
00038
00039 };
00040
00041
00042 void midiFormatErrorString(unsigned long err, char * buf, unsigned long size);
00043
00044 std::string midiErrorString(unsigned long err);
00045
00046 unsigned long midiCloseIn(MIDIINHANDLE hnd);
00047
00048 unsigned long midiStartIn(MIDIINHANDLE hdl);
00049
00050 unsigned long midiStopIn(MIDIINHANDLE hdl);
00051
00052 typedef struct MIDIBUFFER{
00053 MIDIHDR hdr;
00054 unsigned long timestamp;
00055 unsigned long Hdl;
00056 }MIDIBUFFER;
00057
00058 unsigned long midiInitBuffer(unsigned long size, MIDIBUFFER * buf);
00059 unsigned long midiReleaseBuffer(MIDIBUFFER * buf);
00060
00061 unsigned long midiInPrepareBuffer(MIDIINHANDLE inHdl, MIDIBUFFER * buf);
00062 unsigned long midiInUnPrepareBuffer(MIDIBUFFER * buf);
00063 unsigned long midiInQueueBuffer(MIDIBUFFER *buf);
00064 unsigned long midiBufferGetSize(MIDIBUFFER * buf);
00065 unsigned long midiBufferGetBytesRecorded(MIDIBUFFER * buf);
00066 unsigned char * midiBufferGetPtr(MIDIBUFFER * buf);
00067
00068
00069
00070
00071 class MidiIn{
00072 protected:
00073 MIDIINHANDLE hdl;
00074 MidiInHandler * inHandler;
00075 bool recording;
00076 bool _open;
00077 public:
00078 virtual ~MidiIn();
00079 MidiIn();
00080
00081 MIDIINHANDLE getHandle();
00082
00083 static void CALLBACK handleFunc(MIDIINHANDLE hdl, unsigned long msg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 );
00084
00085 void openId(unsigned long id );
00086 void openStr(std::string devname);
00087 bool isOpen();
00088 void close();
00089
00090 bool isRecording();
00091 void startRecording();
00092 void stopRecording();
00093 void setHandler(MidiInHandler * inHdl);
00094 void addBuffer(MIDIBUFFER * buf);
00095 void queueBuffer(MIDIBUFFER * buf);
00096 };
00097
00098
00099 class MidiOut{
00100 protected:
00101 MIDIOUTHANDLE out;
00102 bool _opened;
00103
00104 public:
00105 MidiOut();
00106 virtual ~MidiOut();
00107
00108 void close ();
00109 bool isOpen();
00110 void openId(unsigned long devid);
00111 void openStr(std::string devName);
00112
00113 void sendShortMessage( unsigned long msg);
00114 void sendSysExMessage( unsigned char * buf, unsigned long size);
00115 };
00116
00117
00118
00119 class MidiMsg{
00120 public:
00121 std::vector<unsigned char> msg;
00122 unsigned long timestamp;
00123 public:
00124 unsigned long packMsg();
00125 bool isSysEx();
00126 unsigned char getCommand();
00127 unsigned char getChannel();
00128 unsigned char getStatus();
00129 static void unpackShortMsg(unsigned long msg, unsigned char & byte1, unsigned char & byte2, unsigned char & byte3);
00130 };
00131
00132 std::ostream& operator<<(std::ostream& os, MidiMsg& object);
00133 std::istream& operator>>(std::istream& is, MidiMsg& object);
00134
00135 };
00136 #endif // USE_MIDI
00137
00138 #endif // TOOL_MIDI_H