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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2559 - (show annotations) (download) (as text)
Sun May 18 17:38:25 2014 UTC (9 years, 10 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 26005 byte(s)
* Aftertouch: extended API to explicitly handle channel pressure and
  polyphonic key pressure events (so far polyphonic pressure was not
  supported at all, and channel pressure was rerouted as CC128 but not
  used so far).
* Gig Engine: Fixed support for 'aftertouch' attenuation controller.
* Bumped version (1.0.0.svn39).

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2014 Christian Schoenebeck *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #ifndef __LS_MIDIINPUTPORT_H__
25 #define __LS_MIDIINPUTPORT_H__
26
27 #include "../../common/global.h"
28 #include "../../common/Mutex.h"
29 #include "../../common/Exception.h"
30 #include "../DeviceParameter.h"
31 #include "midi.h"
32 #include "MidiInputDevice.h"
33 #include "../../engines/EngineChannel.h"
34 #include "../../common/SynchronizedConfig.h"
35
36 namespace LinuxSampler {
37
38 // just symbol prototyping
39 class MidiInputDevice;
40 class EngineChannel;
41 class VirtualMidiDevice;
42
43 class MidiInputPort {
44 public:
45
46 /////////////////////////////////////////////////////////////////
47 // type definitions
48
49 /** MIDI Port Parameter 'NAME'
50 *
51 * Used to assign an arbitrary name to the MIDI port.
52 */
53 class ParameterName : public DeviceRuntimeParameterString {
54 public:
55 ParameterName(MidiInputPort* pPort);
56 ParameterName(MidiInputPort* pPort, String val);
57 virtual String Description() OVERRIDE;
58 virtual bool Fix() OVERRIDE;
59 virtual std::vector<String> PossibilitiesAsString() OVERRIDE;
60 virtual void OnSetValue(String s) throw (Exception) OVERRIDE;
61 protected:
62 MidiInputPort* pPort;
63 };
64
65
66
67 /////////////////////////////////////////////////////////////////
68 // normal methods
69 // (usually not to be overriden by descendant)
70
71 /**
72 * Connect given sampler engine channel with this MIDI input
73 * device. The engine channel can either be connected to one
74 * specific MIDI channel or all MIDI channels. If an engine
75 * channel gets connected twice to this MIDI input device, then
76 * the engine's old connection will be detached (no matter on
77 * which MIDI channel).
78 *
79 * @param pEngineChannel - sampler engine
80 * @param MidiChannel - MIDI channel to connect to
81 * @throws MidiInputException if MidiChannel argument invalid
82 */
83 void Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel);
84
85 /**
86 * Disconnect given sampler engine channel from this MIDI input
87 * device. If the given engine channel was not connected with
88 * this device, nothing happens.
89 *
90 * @param pEngineChannel - sampler engine
91 */
92 void Disconnect(EngineChannel* pEngineChannel);
93
94 /**
95 * Return MIDI device where this MIDI port belongs to.
96 */
97 MidiInputDevice* GetDevice();
98
99 /**
100 * Return port number with which this MIDI port is registered to
101 * the MIDI device.
102 */
103 uint GetPortNumber();
104
105 /**
106 * Return all parameter settings of this MIDI port.
107 */
108 std::map<String,DeviceRuntimeParameter*> PortParameters();
109
110 /**
111 * Registers that an engine wants to have sysex messages.
112 */
113 static void AddSysexListener(Engine* engine);
114
115 /**
116 * Removes engine from list of engines getting sysex
117 * messages.
118 *
119 * @returns true if engine was removed, false if it wasn't
120 * present in the list.
121 */
122 static bool RemoveSysexListener(Engine* engine);
123
124 /**
125 * Connects the given virtual MIDI device to this MIDI input
126 * device. This can be used to listen to MIDI data arriving on
127 * the MIDI input device's MIDI ports, e.g. to show an MIDI
128 * activity indicator somewhere.
129 */
130 void Connect(VirtualMidiDevice* pDevice);
131
132 /**
133 * Disconnect the previously connected virtual MIDI device.
134 */
135 void Disconnect(VirtualMidiDevice* pDevice);
136
137 /**
138 * Registers the given @a filter to be applied against all note on
139 * events, adjusting their MIDI velocity data. The vector supplied
140 * here must exactly be either of size 128 or 0. In case the given
141 * vector is of size 128, it will be used as lookup table by this
142 * MIDI inpurt port to remap any incoming MIDI note on velocity
143 * data to the respective value in that vector, or exactly:
144 * @code
145 * velocity = filter[velocity];
146 * @endcode
147 * Accordingly the values in the vector have to be in the range
148 * 0..127, however note that a value 0 should actually never be
149 * used, since by MIDI specification, a note on with velocity 0 is
150 * interpreted as note off event instead!
151 *
152 * If a vector of size 0 is supplied, this note on velocity filter
153 * mechanism will be disabled.
154 *
155 * @param filter - lookup table in the format described above
156 * @throws MidiInputException - if filter is in invalid format
157 */
158 void SetNoteOnVelocityFilter(const std::vector<uint8_t>& filter);
159
160
161 /////////////////////////////////////////////////////////////////
162 // dispatch methods
163 // (should be called by the MidiInputDevice descendant on events)
164
165 /**
166 * Should be called by the implementing MIDI input device
167 * whenever a note on event arrived, this will cause the note on
168 * event to be forwarded to all connected engines on the
169 * corresponding MIDI channel.
170 *
171 * This method is meant for realtime rendering, this way an event
172 * is immediately created with the current system time as time
173 * stamp.
174 *
175 * @param Key - MIDI key number of the triggered key
176 * @param Velocity - MIDI velocity of the triggered key
177 * @param MidiChannel - MIDI channel on which event occured on
178 * (low level indexing, means 0..15)
179 */
180 void DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel);
181
182 /**
183 * Should be called by the implementing MIDI input device
184 * whenever a note on event arrived, this will cause the note on
185 * event to be forwarded to all connected engines on the
186 * corresponding MIDI channel.
187 *
188 * This method is meant for offline rendering and / or in case the
189 * exact fragment position of the event is already known.
190 *
191 * @param Key - MIDI key number of the triggered key
192 * @param Velocity - MIDI velocity of the triggered key
193 * @param MidiChannel - MIDI channel on which event occured on
194 * (low level indexing, means 0..15)
195 * @param FragmentPos - event's sample point position in the
196 * current audio fragment
197 */
198 void DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos);
199
200 /**
201 * Should be called by the implementing MIDI input device
202 * whenever a note off event arrived, this will cause the note
203 * off event to be forwarded to all connected engines on the
204 * corresponding MIDI channel.
205 *
206 * This method is meant for realtime rendering, this way an event
207 * is immediately created with the current system time as time
208 * stamp.
209 *
210 * @param Key - MIDI key number of the released key
211 * @param Velocity - MIDI velocity of the released key
212 * @param MidiChannel - MIDI channel on which event occured on
213 * (low level indexing, means 0..15)
214 */
215 void DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel);
216
217 /**
218 * Should be called by the implementing MIDI input device
219 * whenever a note off event arrived, this will cause the note
220 * off event to be forwarded to all connected engines on the
221 * corresponding MIDI channel.
222 *
223 * This method is meant for offline rendering and / or in case the
224 * exact fragment position of the event is already known.
225 *
226 * @param Key - MIDI key number of the released key
227 * @param Velocity - MIDI velocity of the released key
228 * @param MidiChannel - MIDI channel on which event occured on
229 * (low level indexing, means 0..15)
230 * @param FragmentPos - event's sample point position in the
231 * current audio fragment
232 */
233 void DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos);
234
235 /**
236 * Should be called by the implementing MIDI input device
237 * whenever a pitchbend event arrived, this will cause the
238 * pitchbend event to be forwarded to all connected engines.
239 *
240 * This method is meant for realtime rendering, this way an event
241 * is immediately created with the current system time as time
242 * stamp.
243 *
244 * @param Pitch - MIDI pitch value
245 * @param MidiChannel - MIDI channel on which event occured on
246 * (low level indexing, means 0..15)
247 */
248 void DispatchPitchbend(int Pitch, uint MidiChannel);
249
250 /**
251 * Should be called by the implementing MIDI input device
252 * whenever a pitchbend event arrived, this will cause the
253 * pitchbend event to be forwarded to all connected engines.
254 *
255 * This method is meant for offline rendering and / or in case the
256 * exact fragment position of the event is already known.
257 *
258 * @param Pitch - MIDI pitch value
259 * @param MidiChannel - MIDI channel on which event occured on
260 * (low level indexing, means 0..15)
261 * @param FragmentPos - event's sample point position in the
262 * current audio fragment
263 */
264 void DispatchPitchbend(int Pitch, uint MidiChannel, int32_t FragmentPos);
265
266 /**
267 * Should be called by the implementing MIDI input device
268 * whenever a control change event arrived, this will cause the
269 * control change event to be forwarded to all engines on the
270 * corresponding MIDI channel.
271 *
272 * This method is meant for realtime rendering, this way an event
273 * is immediately created with the current system time as time
274 * stamp.
275 *
276 * @param Controller - MIDI controller number
277 * @param Value - MIDI control change value
278 * @param MidiChannel - MIDI channel on which event occured on
279 * (low level indexing, means 0..15)
280 */
281 void DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel);
282
283 /**
284 * Should be called by the implementing MIDI input device
285 * whenever a control change event arrived, this will cause the
286 * control change event to be forwarded to all engines on the
287 * corresponding MIDI channel.
288 *
289 * This method is meant for offline rendering and / or in case the
290 * exact fragment position of the event is already known.
291 *
292 * @param Controller - MIDI controller number
293 * @param Value - MIDI control change value
294 * @param MidiChannel - MIDI channel on which event occured on
295 * (low level indexing, means 0..15)
296 * @param FragmentPos - event's sample point position in the
297 * current audio fragment
298 */
299 void DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel, int32_t FragmentPos);
300
301 /**
302 * Should be called by the implementing MIDI input device whenever
303 * a channel pressure event arrived (a.k.a. aftertouch), this will
304 * cause the channel pressure event to be forwarded to all engines
305 * on the corresponding MIDI channel.
306 *
307 * This method is meant for realtime rendering, this way an event
308 * is immediately created with the current system time as time
309 * stamp.
310 *
311 * @param Value - MIDI channel pressure value (0..127)
312 * @param MidiChannel - MIDI channel on which event occured on
313 * (low level indexing, means 0..15)
314 * @see DispatchPolyphonicKeyPressure()
315 */
316 void DispatchChannelPressure(uint8_t Value, uint MidiChannel);
317
318 /**
319 * Should be called by the implementing MIDI input device whenever
320 * a channel pressure event arrived (a.k.a. aftertouch), this will
321 * cause the channel pressure event to be forwarded to all engines
322 * on the corresponding MIDI channel.
323 *
324 * This method is meant for offline rendering and / or in case the
325 * exact fragment position of the event is already known.
326 *
327 * @param Value - MIDI channel pressure value (0..127)
328 * @param MidiChannel - MIDI channel on which event occured on
329 * (low level indexing, means 0..15)
330 * @param FragmentPos - event's sample point position in the
331 * current audio fragment
332 * @see DispatchPolyphonicKeyPressure()
333 */
334 void DispatchChannelPressure(uint8_t Value, uint MidiChannel, int32_t FragmentPos);
335
336 /**
337 * Should be called by the implementing MIDI input device whenever
338 * a polyphonic key pressure event arrived (a.k.a. polyphonic
339 * aftertouch), this will cause the polyphonic key pressure event
340 * to be forwarded to all engines on the corresponding MIDI channel.
341 *
342 * This method is meant for realtime rendering, this way an event
343 * is immediately created with the current system time as time
344 * stamp.
345 *
346 * @param Key - MIDI key number of the key where pressure changed
347 * @param Value - MIDI key pressure value (0..127)
348 * @param MidiChannel - MIDI channel on which event occured on
349 * (low level indexing, means 0..15)
350 * @see DispatchChannelPressure()
351 */
352 void DispatchPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint MidiChannel);
353
354 /**
355 * Should be called by the implementing MIDI input device whenever
356 * a polyphonic key pressure event arrived (a.k.a. polyphonic
357 * aftertouch), this will cause the polyphonic key pressure event
358 * to be forwarded to all engines on the corresponding MIDI channel.
359 *
360 * This method is meant for offline rendering and / or in case the
361 * exact fragment position of the event is already known.
362 *
363 * @param Key - MIDI key number of the key where pressure changed
364 * @param Value - MIDI key pressure value (0..127)
365 * @param MidiChannel - MIDI channel on which event occured on
366 * (low level indexing, means 0..15)
367 * @param FragmentPos - event's sample point position in the
368 * current audio fragment
369 * @see DispatchChannelPressure()
370 */
371 void DispatchPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint MidiChannel, int32_t FragmentPos);
372
373 /**
374 * Should be called by the implementing MIDI input device
375 * whenever a program change event arrived. In case the
376 * respective sampler channel(s) are enabled for MIDI
377 * instrument mapping, the respective sampler engine and
378 * instrument will be loaded on the connected sampler
379 * channel(s) as defined by the respective entry in the
380 * MIDI instrument map.
381 *
382 * @e Note: the MIDI instrument map is empty by default on
383 * sampler startup. It has to be explicitly filled with
384 * entries and the sampler channel(s) have to be enabled for
385 * a certain MIDI instrument table, otherwise program change
386 * messages are ignored!
387 *
388 * @param Program - MIDI program change number
389 * @param MidiChannel - MIDI channel on which this program
390 * change occured
391 * @see MidiInstrumentMapper
392 */
393 void DispatchProgramChange(uint8_t Program, uint MidiChannel);
394
395 /**
396 * Should be called by the implementing MIDI input device whenever
397 * a MIDI bank select MSB (Most Significant Byte) event arrived.
398 *
399 * In case the respective sampler channel(s) are enabled for "MIDI
400 * instrument mapping", a subsequent MIDI program change event can
401 * be used to let the respective sampler engine load another
402 * instrument according to the respective entry in the MIDI
403 * instrument map.
404 *
405 * MIDI sources can either a) just send bank select MSB events, or
406 * b) just send bank select LSB events or c) they can send both bank
407 * select MSB and LSB events. The sampler will automatically detect
408 * which style the MIDI source is using and appropriately interpret
409 * it for selecting the appropriate bank.
410 *
411 * @param BankMsb - Most Significant Byte of the bank number
412 * @param MidiChannel - MIDI channel on which this bank select
413 * occurred
414 */
415 void DispatchBankSelectMsb(uint8_t BankMsb, uint MidiChannel);
416
417 /**
418 * Should be called by the implementing MIDI input device whenever
419 * a MIDI bank select LSB (Least Significant Byte) event arrived.
420 *
421 * In case the respective sampler channel(s) are enabled for "MIDI
422 * instrument mapping", a subsequent MIDI program change event can
423 * be used to let the respective sampler engine load another
424 * instrument according to the respective entry in the MIDI
425 * instrument map.
426 *
427 * MIDI sources can either a) just send bank select MSB events, or
428 * b) just send bank select LSB events or c) they can send both bank
429 * select MSB and LSB events. The sampler will automatically detect
430 * which style the MIDI source is using and appropriately interpret
431 * it for selecting the appropriate bank.
432 *
433 * @param BankMsb - Most Significant Byte of the bank number
434 * @param MidiChannel - MIDI channel on which this bank select
435 * occurred
436 */
437 void DispatchBankSelectLsb(uint8_t BankLsb, uint MidiChannel);
438
439 /**
440 * Should be called by the implementing MIDI input device
441 * whenever a system exclusive message arrived, this will cause
442 * the message to be forwarded to all connected engines.
443 *
444 * @param pData - pointer to the sysex data
445 * @param Size - length of the sysex data (in bytes)
446 */
447 void DispatchSysex(void* pData, uint Size);
448
449 /**
450 * Helper function for MIDI input devices that have the
451 * MIDI data as raw bytes.
452 *
453 * @param pData - pointer to the raw MIDI data
454 */
455 void DispatchRaw(uint8_t* pData);
456
457 /**
458 * Helper function for MIDI input devices that have the
459 * MIDI data as raw bytes.
460 *
461 * @param pData - pointer to the raw MIDI data
462 * @param FragmentPos - event's sample point position in the
463 * current audio fragment
464 */
465 void DispatchRaw(uint8_t* pData, int32_t FragmentPos);
466
467 protected:
468 MidiInputDevice* pDevice;
469 int portNumber;
470 std::map<String,DeviceRuntimeParameter*> Parameters; ///< All port parameters.
471 typedef std::set<EngineChannel*> MidiChannelMap_t[17];
472 SynchronizedConfig<MidiChannelMap_t> MidiChannelMap; ///< 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.
473 SynchronizedConfig<MidiChannelMap_t>::Reader MidiChannelMapReader; ///< MIDI thread access to MidiChannelMap
474 Mutex MidiChannelMapMutex; ///< Used to protect the MidiChannelMap from being used at the same time by different threads.
475 SynchronizedConfig<std::set<Engine*> >::Reader SysexListenersReader; ///< MIDI thread access to SysexListeners
476 SynchronizedConfig<std::vector<VirtualMidiDevice*> > virtualMidiDevices;
477 SynchronizedConfig<std::vector<VirtualMidiDevice*> >::Reader virtualMidiDevicesReader;
478 Mutex virtualMidiDevicesMutex;
479 SynchronizedConfig<std::vector<uint8_t> > noteOnVelocityFilter;
480 SynchronizedConfig<std::vector<uint8_t> >::Reader noteOnVelocityFilterReader;
481 Mutex noteOnVelocityFilterMutex;
482 uint8_t runningStatusBuf[3];
483
484 /**
485 * Constructor
486 */
487 MidiInputPort(MidiInputDevice* pDevice, int portNumber);
488
489 /**
490 * Destructor
491 */
492 virtual ~MidiInputPort();
493
494 /**
495 * Takes a MIDI status byte (the first byte of each MIDI event) as
496 * argument and returns the expected size of the associated MIDI
497 * event according to the MIDI protocol. Returns -1 on invalid
498 * status bytes AND on variable size events (SysEx events).
499 *
500 * This method can be used for drivers which have to deal with raw
501 * MIDI data, like the CoreMIDI driver, which can receive MIDI
502 * packets with more than one event per packet.
503 *
504 * This method handles "MIDI running status" as well. That is, in
505 * case the supplied byte is not a status byte but a data byte,
506 * it expects the event to be in "running status" and accordingly
507 * uses the status byte of the previous event (processed by the
508 * Dispatch*() methods).
509 */
510 int expectedEventSize(unsigned char byte);
511
512 friend class MidiInputDevice;
513
514 private:
515 static SynchronizedConfig<std::set<Engine*> > SysexListeners; ///< All engines that are listening to sysex messages.
516
517 uint8_t* handleRunningStatus(uint8_t* pData);
518 };
519
520 } // namsepace LinuxSampler
521
522 #endif // __LS_MIDIINPUTPORT_H__

  ViewVC Help
Powered by ViewVC