| 1 | /* |
|---|
| 2 | * libjdkmidi-2004 C++ Class Library for MIDI |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004 J.D. Koftinoff Software, Ltd. |
|---|
| 5 | * www.jdkoftinoff.com |
|---|
| 6 | * jeffk@jdkoftinoff.com |
|---|
| 7 | * |
|---|
| 8 | * *** RELEASED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) April 27, 2004 *** |
|---|
| 9 | * |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU General Public License as published by |
|---|
| 12 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | * (at your option) any later version. |
|---|
| 14 | * |
|---|
| 15 | * This program is distributed in the hope that it will be useful, |
|---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | * GNU General Public License for more details. |
|---|
| 19 | * |
|---|
| 20 | * You should have received a copy of the GNU General Public License |
|---|
| 21 | * along with this program; if not, write to the Free Software |
|---|
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | */ |
|---|
| 24 | #ifndef JDKMIDI_DRIVER_H |
|---|
| 25 | #define JDKMIDI_DRIVER_H |
|---|
| 26 | |
|---|
| 27 | #include "jdkmidi/msg.h" |
|---|
| 28 | #include "jdkmidi/sysex.h" |
|---|
| 29 | #include "jdkmidi/matrix.h" |
|---|
| 30 | #include "jdkmidi/process.h" |
|---|
| 31 | #include "jdkmidi/queue.h" |
|---|
| 32 | #include "jdkmidi/tick.h" |
|---|
| 33 | |
|---|
| 34 | namespace jdkmidi |
|---|
| 35 | { |
|---|
| 36 | class MIDIDriver : public MIDITick |
|---|
| 37 | { |
|---|
| 38 | public: |
|---|
| 39 | MIDIDriver ( int queue_size ); |
|---|
| 40 | virtual ~MIDIDriver(); |
|---|
| 41 | |
|---|
| 42 | virtual void Reset(); |
|---|
| 43 | |
|---|
| 44 | // to get the midi in queue |
|---|
| 45 | MIDIQueue * InputQueue() |
|---|
| 46 | { |
|---|
| 47 | return &in_queue; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | const MIDIQueue * InputQueue() const |
|---|
| 51 | { |
|---|
| 52 | return &in_queue; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | // to get the midi out queue |
|---|
| 56 | MIDIQueue * OutputQueue() |
|---|
| 57 | { |
|---|
| 58 | return &out_queue; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | const MIDIQueue * OutputQueue() const |
|---|
| 62 | { |
|---|
| 63 | return &out_queue; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | // |
|---|
| 68 | // returns true if the output queue is not full |
|---|
| 69 | bool CanOutputMessage() const |
|---|
| 70 | { |
|---|
| 71 | return out_queue.CanPut(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | // processes message with the OutProcessor and then |
|---|
| 76 | // puts the message in the out_queue |
|---|
| 77 | void OutputMessage ( MIDITimedBigMessage &msg ) |
|---|
| 78 | { |
|---|
| 79 | if ( ( out_proc && out_proc->Process ( &msg ) ) || !out_proc ) |
|---|
| 80 | { |
|---|
| 81 | out_matrix.Process ( msg ); |
|---|
| 82 | out_queue.Put ( msg ); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void SetThruEnable ( bool f ) |
|---|
| 87 | { |
|---|
| 88 | thru_enable = f; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | bool GetThruEnable() const |
|---|
| 92 | { |
|---|
| 93 | return thru_enable; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // to set the midi processors used for thru, out, and in |
|---|
| 97 | void SetThruProcessor ( MIDIProcessor *proc ) |
|---|
| 98 | { |
|---|
| 99 | thru_proc = proc; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void SetOutProcessor ( MIDIProcessor *proc ) |
|---|
| 103 | { |
|---|
| 104 | out_proc = proc; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | void SetInProcessor ( MIDIProcessor *proc ) |
|---|
| 108 | { |
|---|
| 109 | in_proc = proc; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void SetTickProc ( MIDITick *tick ) |
|---|
| 113 | { |
|---|
| 114 | tick_proc = tick; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | // to send all notes off on selected midi chanel |
|---|
| 118 | void AllNotesOff ( int chan ); |
|---|
| 119 | |
|---|
| 120 | // to send all notes off on all midi channels |
|---|
| 121 | void AllNotesOff(); |
|---|
| 122 | |
|---|
| 123 | // call handle midi in when a parsed midi message |
|---|
| 124 | // comes in to the system. Can be called by a callback function |
|---|
| 125 | // or by your TimeTick() function. |
|---|
| 126 | |
|---|
| 127 | virtual bool HardwareMsgIn ( MIDITimedBigMessage &msg ); |
|---|
| 128 | |
|---|
| 129 | // HardwareMsgOut() must be overriden by a subclass - It must |
|---|
| 130 | // take |
|---|
| 131 | |
|---|
| 132 | virtual bool HardwareMsgOut ( const MIDITimedBigMessage &msg ) = 0; |
|---|
| 133 | |
|---|
| 134 | // the time tick procedure: |
|---|
| 135 | // manages in/out/thru to hardware |
|---|
| 136 | // inherited from MIDITick. |
|---|
| 137 | // |
|---|
| 138 | // if you need to poll midi in hardware, |
|---|
| 139 | // you can override this method - Call MIDIDriver::TimeTick(t) |
|---|
| 140 | // first, You may then poll the midi in |
|---|
| 141 | // hardware, parse the bytes, form a message, and give the |
|---|
| 142 | // resulting message to HandleMsgIn to process it and put it in |
|---|
| 143 | // the in_queue. |
|---|
| 144 | |
|---|
| 145 | virtual void TimeTick ( unsigned long sys_time ); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | protected: |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | // the in and out queues |
|---|
| 152 | MIDIQueue in_queue; |
|---|
| 153 | MIDIQueue out_queue; |
|---|
| 154 | |
|---|
| 155 | // the processors |
|---|
| 156 | MIDIProcessor *in_proc; |
|---|
| 157 | MIDIProcessor *out_proc; |
|---|
| 158 | MIDIProcessor *thru_proc; |
|---|
| 159 | |
|---|
| 160 | bool thru_enable; |
|---|
| 161 | |
|---|
| 162 | // additional TimeTick procedure |
|---|
| 163 | |
|---|
| 164 | MIDITick *tick_proc; |
|---|
| 165 | |
|---|
| 166 | // to keep track of notes on going to MIDI out |
|---|
| 167 | |
|---|
| 168 | MIDIMatrix out_matrix; |
|---|
| 169 | }; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | #endif |
|---|