/[svn]/linuxsampler/trunk/src/drivers/midi/MidiInputDevice.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/drivers/midi/MidiInputDevice.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 201 - (show annotations) (download) (as text)
Tue Jul 13 22:10:21 2004 UTC (19 years, 9 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 12997 byte(s)
moved directory '/src/mididriver' -> '/src/drivers/midi'

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #ifndef __LS_MIDIINPUTDEVICE_H__
24 #define __LS_MIDIINPUTDEVICE_H__
25
26 #include <stdexcept>
27 #include <set>
28 #include <map>
29 #include <vector>
30
31 #include "../../common/global.h"
32 #include "../../common/LinuxSamplerException.h"
33 #include "../DeviceParameter.h"
34 #include "../../engines/common/Engine.h"
35
36 namespace LinuxSampler {
37
38 // just symbol prototyping
39 class Engine;
40
41 /** Abstract base class for MIDI input drivers in LinuxSampler
42 *
43 * This class will be derived by specialized classes which implement the
44 * connection to a specific MIDI input system (e.g. Alsa Sequencer,
45 * CoreMIDI). The MidiInputDevice desendant should just call the
46 * appropriate (protected) Dispatch* method here when an MIDI event
47 * occured. The dispatch* methods here will automatically forward the
48 * MIDI event to the appropriate, connected sampler engines.
49 */
50 class MidiInputDevice : public InputOutputDevice {
51 public:
52
53 /////////////////////////////////////////////////////////////////
54 // type definitions
55
56 class ParameterActive : public DeviceCreationParameterBool {
57 public:
58 ParameterActive( void ) : DeviceCreationParameterBool() { InitWithDefault(); }
59 ParameterActive( String active ) : DeviceCreationParameterBool(active) { }
60 virtual String Description() { return "Enable / disable device"; }
61 virtual bool Fix() { return false; }
62 virtual bool Mandatory() { return false; }
63 virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); }
64 virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters) { return true; }
65 virtual void OnSetValue(bool b) throw (LinuxSamplerException) { if (b) ((MidiInputDevice*)pDevice)->Listen(); else ((MidiInputDevice*)pDevice)->StopListen(); }
66 static String Name( void ) { return "active"; }
67 };
68
69 class ParameterPorts : public DeviceCreationParameterInt {
70 public:
71 ParameterPorts( void ) : DeviceCreationParameterInt() { InitWithDefault(); }
72 ParameterPorts( String val ) : DeviceCreationParameterInt(val) { }
73 virtual String Description() { return "Number of ports"; }
74 virtual bool Fix() { return false; }
75 virtual bool Mandatory() { return false; }
76 virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); }
77 virtual optional<int> DefaultAsInt(std::map<String,String> Parameters) { return 0; }
78 virtual optional<int> RangeMinAsInt(std::map<String,String> Parameters) { return optional<int>::nothing; }
79 virtual optional<int> RangeMaxAsInt(std::map<String,String> Parameters) { return optional<int>::nothing; }
80 virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters) { return std::vector<int>(); }
81 virtual void OnSetValue(int i) throw (LinuxSamplerException) { ((MidiInputDevice*)pDevice)->AcquirePorts(i); }
82 static String Name( void ) { return "ports"; }
83 };
84
85 class MidiInputPort {
86
87 public:
88 /**
89 * MIDI channels
90 */
91 enum midi_chan_t {
92 midi_chan_all = 0,
93 midi_chan_1 = 1,
94 midi_chan_2 = 2,
95 midi_chan_3 = 3,
96 midi_chan_4 = 4,
97 midi_chan_5 = 5,
98 midi_chan_6 = 6,
99 midi_chan_7 = 7,
100 midi_chan_8 = 8,
101 midi_chan_9 = 9,
102 midi_chan_10 = 10,
103 midi_chan_11 = 11,
104 midi_chan_12 = 12,
105 midi_chan_13 = 13,
106 midi_chan_14 = 14,
107 midi_chan_15 = 15,
108 midi_chan_16 = 16
109 };
110
111 class ParameterName : public DeviceCreationParameterString {
112 public:
113 ParameterName(MidiInputPort* pPort) { this->pPort = pPort; InitWithDefault();}
114 ParameterName(MidiInputPort* pPort, String val) : DeviceCreationParameterString(val) { this->pPort = pPort; }
115 virtual String Description() { return "Name for this port"; }
116 virtual bool Fix() { return false; }
117 virtual bool Mandatory() { return false; }
118 virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); }
119 virtual optional<String> Default(std::map<String,String> Parameters) { return ""; }
120 virtual std::vector<String> PossibilitiesAsString(std::map<String,String> Parameters) { return std::vector<String>(); }
121 virtual void OnSetValue(String s) throw (LinuxSamplerException) { return; /* FIXME: Nothing to do here */ }
122 protected:
123 MidiInputPort * pPort;
124 };
125
126 /////////////////////////////////////////////////////////////////
127 // normal methods
128 // (usually not to be overriden by descendant)
129
130 /**
131 * Connect given sampler engine with this MIDI input device.
132 * The engine can either be connected to one specific MIDI
133 * channel or all MIDI channels. If an engine gets connected
134 * twice to this MIDI input device, then the engine's old
135 * connection will be detached (no matter on which MIDI channel).
136 *
137 * @param pEngine - sampler engine
138 * @param MidiChannel - MIDI channel to connect to
139 * @throws MidiInputException if MidiChannel argument invalid
140 */
141 void Connect(Engine* pEngine, midi_chan_t MidiChannel);
142
143 /**
144 * Disconnect given sampler engine from this MIDI input device.
145 *
146 * @param pEngine - sampler engine
147 */
148 void Disconnect(Engine* pEngine);
149
150 MidiInputDevice* GetDevice();
151 uint GetPortNumber();
152 std::map<String,DeviceCreationParameter*> DeviceParameters();
153
154 /////////////////////////////////////////////////////////////////
155 // dispatch methods
156 // (should be called by the MidiInputDevice descendant on events)
157
158 /**
159 * Should be called by the implementing MIDI input device
160 * whenever a note on event arrived, this will cause the note on
161 * event to be forwarded to all connected engines on the
162 * corresponding MIDI channel.
163 *
164 * @param Key - MIDI key number of the triggered key
165 * @param Velocity - MIDI velocity of the triggered key
166 * @param MidiChannel - MIDI channel on which event occured on
167 */
168 void DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel);
169
170 /**
171 * Should be called by the implementing MIDI input device
172 * whenever a note off event arrived, this will cause the note
173 * off event to be forwarded to all connected engines on the
174 * corresponding MIDI channel.
175 *
176 * @param Key - MIDI key number of the released key
177 * @param Velocity - MIDI velocity of the released key
178 * @param MidiChannel - MIDI channel on which event occured on
179 */
180 void DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel);
181
182 /**
183 * Should be called by the implementing MIDI input device
184 * whenever a pitchbend event arrived, this will cause the
185 * pitchbend event to be forwarded to all connected engines.
186 *
187 * @param Pitch - MIDI pitch value
188 * @param MidiChannel - MIDI channel on which event occured on
189 */
190 void DispatchPitchbend(int Pitch, uint MidiChannel);
191
192 /**
193 * Should be called by the implementing MIDI input device
194 * whenever a control change event arrived, this will cause the
195 * control change event to be forwarded to all engines on the
196 * corresponding MIDI channel.
197 *
198 * @param Controller - MIDI controller number
199 * @param Value - MIDI control change value
200 * @param MidiChannel - MIDI channel on which event occured on
201 */
202 void DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel);
203
204 MidiInputPort(MidiInputDevice* pDevice, int portNumber);
205
206 protected:
207 MidiInputDevice* pDevice;
208 int portNumber;
209 std::map<String,DeviceCreationParameter*> Parameters; ///< All port parameters.
210 std::set<Engine*> MidiChannelMap[17]; ///< Contains the list of connected engines for each MIDI channel, where index 0 points to the list of engines which are connected to all MIDI channels. Usually it's not necessary for the descendant to use this map, instead it should just use the Dispatch* methods.
211 virtual ~MidiInputPort();
212
213 friend class MidiInputDevice;
214 };
215
216 /**
217 * Return midi port
218 */
219 MidiInputPort* GetPort(int i) { return Ports[i]; }
220
221 /**
222 * Create new Midi port
223 * This will be called by AcquirePorts
224 * Each individual device must implement this.
225 */
226 virtual MidiInputPort* CreateMidiPort( void ) = 0;
227
228 std::map<String,DeviceCreationParameter*> DeviceParameters();
229
230 /////////////////////////////////////////////////////////////////
231 // abstract methods
232 // (these have to be implemented by the descendant)
233
234 /**
235 * Start listen to MIDI input events on the MIDI input port.
236 * The MIDIInputPort descendant should forward all MIDI input
237 * events by calling the appropriate (protected) Dispatch*
238 * method of class MidiInputPort.
239 */
240 virtual void Listen() = 0;
241
242 /**
243 * Stop to listen to MIDI input events on the MIDI input port.
244 * After this method was called, the MidiInputPort descendant
245 * should ignore all MIDI input events.
246 */
247 virtual void StopListen() = 0;
248
249 /**
250 * Return device driver name
251 */
252 virtual String Driver() = 0;
253
254 protected:
255 std::map<String,DeviceCreationParameter*> Parameters; ///< All device parameters.
256 std::map<int,MidiInputPort*> Ports;
257
258 MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters);
259
260 virtual ~MidiInputDevice();
261
262
263 friend class Sampler; // allow Sampler class to destroy midi devices
264
265 /**
266 * Set number of MIDI ports required by the engine
267 * This can either do nothing, create more ports
268 * or destroy ports depenging on the parameter
269 * and how many ports already exist on this driver.
270 *
271 * @param Ports - number of ports to be left on this driver after this call.
272 */
273 void AcquirePorts(uint Ports);
274 };
275
276 /**
277 * Midi input exception that should be thrown by the MidiInputDevice
278 * descendants in case initialization of the MIDI input system failed
279 * (which should be done in the constructor of the MidiInputDevice
280 * descendant).
281 */
282 class MidiInputException : public LinuxSamplerException {
283 public:
284 MidiInputException(const std::string& msg) : LinuxSamplerException(msg) {}
285 };
286 }
287
288 #endif // __LS_MIDIINPUTDEVICE_H__

  ViewVC Help
Powered by ViewVC