3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
30 |
#include <vector> |
#include <vector> |
31 |
|
|
32 |
#include "../../common/global.h" |
#include "../../common/global.h" |
33 |
#include "../../common/LinuxSamplerException.h" |
#include "../../common/Exception.h" |
34 |
#include "../DeviceParameter.h" |
#include "../DeviceParameter.h" |
35 |
#include "../../engines/common/Engine.h" |
#include "MidiInputPort.h" |
36 |
|
#include "../../engines/Engine.h" |
37 |
|
|
38 |
namespace LinuxSampler { |
namespace LinuxSampler { |
39 |
|
|
40 |
// just symbol prototyping |
// just symbol prototyping |
41 |
|
class MidiInputPort; |
42 |
class Engine; |
class Engine; |
43 |
|
|
44 |
|
/** |
45 |
|
* Midi input exception that should be thrown by the MidiInputDevice |
46 |
|
* descendants in case initialization of the MIDI input system failed |
47 |
|
* (which should be done in the constructor of the MidiInputDevice |
48 |
|
* descendant). |
49 |
|
*/ |
50 |
|
class MidiInputException : public Exception { |
51 |
|
public: |
52 |
|
MidiInputException(const std::string& msg) : Exception(msg) {} |
53 |
|
}; |
54 |
|
|
55 |
/** Abstract base class for MIDI input drivers in LinuxSampler |
/** Abstract base class for MIDI input drivers in LinuxSampler |
56 |
* |
* |
57 |
* This class will be derived by specialized classes which implement the |
* This class will be derived by specialized classes which implement the |
67 |
///////////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////////// |
68 |
// type definitions |
// type definitions |
69 |
|
|
70 |
|
/** Device Parameter 'ACTIVE' |
71 |
|
* |
72 |
|
* Used to activate / deactivate the MIDI input device. |
73 |
|
*/ |
74 |
class ParameterActive : public DeviceCreationParameterBool { |
class ParameterActive : public DeviceCreationParameterBool { |
75 |
public: |
public: |
76 |
ParameterActive( void ) : DeviceCreationParameterBool() { InitWithDefault(); } |
ParameterActive(); |
77 |
ParameterActive( String active ) : DeviceCreationParameterBool(active) { } |
ParameterActive(String active); |
78 |
virtual String Description() { return "Enable / disable device"; } |
virtual String Description(); |
79 |
virtual bool Fix() { return false; } |
virtual bool Fix(); |
80 |
virtual bool Mandatory() { return false; } |
virtual bool Mandatory(); |
81 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); } |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters(); |
82 |
virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters) { return true; } |
virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters); |
83 |
virtual void OnSetValue(bool b) throw (LinuxSamplerException) { if (b) ((MidiInputDevice*)pDevice)->Listen(); else ((MidiInputDevice*)pDevice)->StopListen(); } |
virtual void OnSetValue(bool b) throw (Exception); |
84 |
static String Name( void ) { return "active"; } |
static String Name(); |
85 |
}; |
}; |
86 |
|
|
87 |
class ParameterPorts : public DeviceCreationParameterInt { |
/** Device Parameter 'PORTS' |
88 |
public: |
* |
89 |
ParameterPorts( void ) : DeviceCreationParameterInt() { InitWithDefault(); } |
* Used to increase / decrease the number of MIDI ports of the |
90 |
ParameterPorts( String val ) : DeviceCreationParameterInt(val) { } |
* MIDI input device. |
91 |
virtual String Description() { return "Number of ports"; } |
*/ |
92 |
virtual bool Fix() { return false; } |
class ParameterPorts : public DeviceCreationParameterInt { |
93 |
virtual bool Mandatory() { return false; } |
public: |
94 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); } |
ParameterPorts(); |
95 |
virtual optional<int> DefaultAsInt(std::map<String,String> Parameters) { return 0; } |
ParameterPorts(String val); |
96 |
virtual optional<int> RangeMinAsInt(std::map<String,String> Parameters) { return optional<int>::nothing; } |
virtual String Description(); |
97 |
virtual optional<int> RangeMaxAsInt(std::map<String,String> Parameters) { return optional<int>::nothing; } |
virtual bool Fix(); |
98 |
virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters) { return std::vector<int>(); } |
virtual bool Mandatory(); |
99 |
virtual void OnSetValue(int i) throw (LinuxSamplerException) { ((MidiInputDevice*)pDevice)->AcquirePorts(i); } |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters(); |
100 |
static String Name( void ) { return "ports"; } |
virtual optional<int> DefaultAsInt(std::map<String,String> Parameters); |
101 |
}; |
virtual optional<int> RangeMinAsInt(std::map<String,String> Parameters); |
102 |
|
virtual optional<int> RangeMaxAsInt(std::map<String,String> Parameters); |
103 |
class MidiInputPort { |
virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters); |
104 |
|
virtual void OnSetValue(int i) throw (Exception); |
105 |
public: |
static String Name(); |
106 |
/** |
}; |
107 |
* MIDI channels |
|
108 |
*/ |
|
109 |
enum midi_chan_t { |
|
110 |
midi_chan_all = 0, |
///////////////////////////////////////////////////////////////// |
111 |
midi_chan_1 = 1, |
// abstract methods |
112 |
midi_chan_2 = 2, |
// (these have to be implemented by the descendant) |
113 |
midi_chan_3 = 3, |
|
114 |
midi_chan_4 = 4, |
/** |
115 |
midi_chan_5 = 5, |
* Start listen to MIDI input events on the MIDI input port. |
116 |
midi_chan_6 = 6, |
* The MIDIInputPort descendant should forward all MIDI input |
117 |
midi_chan_7 = 7, |
* events by calling the appropriate (protected) Dispatch* |
118 |
midi_chan_8 = 8, |
* method of class MidiInputPort. |
119 |
midi_chan_9 = 9, |
*/ |
120 |
midi_chan_10 = 10, |
virtual void Listen() = 0; |
121 |
midi_chan_11 = 11, |
|
122 |
midi_chan_12 = 12, |
/** |
123 |
midi_chan_13 = 13, |
* Stop to listen to MIDI input events on the MIDI input port. |
124 |
midi_chan_14 = 14, |
* After this method was called, the MidiInputPort descendant |
125 |
midi_chan_15 = 15, |
* should ignore all MIDI input events. |
126 |
midi_chan_16 = 16 |
*/ |
127 |
}; |
virtual void StopListen() = 0; |
128 |
|
|
129 |
class ParameterName : public DeviceCreationParameterString { |
/** |
130 |
public: |
* Return device driver name |
131 |
ParameterName(MidiInputPort* pPort) { this->pPort = pPort; InitWithDefault();} |
*/ |
132 |
ParameterName(MidiInputPort* pPort, String val) : DeviceCreationParameterString(val) { this->pPort = pPort; } |
virtual String Driver() = 0; |
133 |
virtual String Description() { return "Name for this port"; } |
|
134 |
virtual bool Fix() { return false; } |
/** |
135 |
virtual bool Mandatory() { return false; } |
* Create new Midi port |
136 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); } |
* This will be called by AcquirePorts |
137 |
virtual optional<String> DefaultAsString(std::map<String,String> Parameters) { return ""; } |
* Each individual device must implement this. |
138 |
virtual std::vector<String> PossibilitiesAsString(std::map<String,String> Parameters) { return std::vector<String>(); } |
*/ |
139 |
virtual void OnSetValue(String s) throw (LinuxSamplerException) { return; /* FIXME: Nothing to do here */ } |
virtual MidiInputPort* CreateMidiPort() = 0; |
|
protected: |
|
|
MidiInputPort * pPort; |
|
|
}; |
|
|
|
|
|
///////////////////////////////////////////////////////////////// |
|
|
// normal methods |
|
|
// (usually not to be overriden by descendant) |
|
|
|
|
|
/** |
|
|
* Connect given sampler engine with this MIDI input device. |
|
|
* The engine can either be connected to one specific MIDI |
|
|
* channel or all MIDI channels. If an engine gets connected |
|
|
* twice to this MIDI input device, then the engine's old |
|
|
* connection will be detached (no matter on which MIDI channel). |
|
|
* |
|
|
* @param pEngine - sampler engine |
|
|
* @param MidiChannel - MIDI channel to connect to |
|
|
* @throws MidiInputException if MidiChannel argument invalid |
|
|
*/ |
|
|
void Connect(Engine* pEngine, midi_chan_t MidiChannel); |
|
|
|
|
|
/** |
|
|
* Disconnect given sampler engine from this MIDI input device. |
|
|
* |
|
|
* @param pEngine - sampler engine |
|
|
*/ |
|
|
void Disconnect(Engine* pEngine); |
|
|
|
|
|
MidiInputDevice* GetDevice(); |
|
|
uint GetPortNumber(); |
|
|
std::map<String,DeviceCreationParameter*> DeviceParameters(); |
|
|
|
|
|
///////////////////////////////////////////////////////////////// |
|
|
// dispatch methods |
|
|
// (should be called by the MidiInputDevice descendant on events) |
|
|
|
|
|
/** |
|
|
* Should be called by the implementing MIDI input device |
|
|
* whenever a note on event arrived, this will cause the note on |
|
|
* event to be forwarded to all connected engines on the |
|
|
* corresponding MIDI channel. |
|
|
* |
|
|
* @param Key - MIDI key number of the triggered key |
|
|
* @param Velocity - MIDI velocity of the triggered key |
|
|
* @param MidiChannel - MIDI channel on which event occured on |
|
|
*/ |
|
|
void DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel); |
|
|
|
|
|
/** |
|
|
* Should be called by the implementing MIDI input device |
|
|
* whenever a note off event arrived, this will cause the note |
|
|
* off event to be forwarded to all connected engines on the |
|
|
* corresponding MIDI channel. |
|
|
* |
|
|
* @param Key - MIDI key number of the released key |
|
|
* @param Velocity - MIDI velocity of the released key |
|
|
* @param MidiChannel - MIDI channel on which event occured on |
|
|
*/ |
|
|
void DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel); |
|
|
|
|
|
/** |
|
|
* Should be called by the implementing MIDI input device |
|
|
* whenever a pitchbend event arrived, this will cause the |
|
|
* pitchbend event to be forwarded to all connected engines. |
|
|
* |
|
|
* @param Pitch - MIDI pitch value |
|
|
* @param MidiChannel - MIDI channel on which event occured on |
|
|
*/ |
|
|
void DispatchPitchbend(int Pitch, uint MidiChannel); |
|
|
|
|
|
/** |
|
|
* Should be called by the implementing MIDI input device |
|
|
* whenever a control change event arrived, this will cause the |
|
|
* control change event to be forwarded to all engines on the |
|
|
* corresponding MIDI channel. |
|
|
* |
|
|
* @param Controller - MIDI controller number |
|
|
* @param Value - MIDI control change value |
|
|
* @param MidiChannel - MIDI channel on which event occured on |
|
|
*/ |
|
|
void DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel); |
|
|
|
|
|
MidiInputPort(MidiInputDevice* pDevice, int portNumber); |
|
|
|
|
|
protected: |
|
|
MidiInputDevice* pDevice; |
|
|
int portNumber; |
|
|
std::map<String,DeviceCreationParameter*> Parameters; ///< All port parameters. |
|
|
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. |
|
|
virtual ~MidiInputPort(); |
|
|
|
|
|
friend class MidiInputDevice; |
|
|
}; |
|
|
|
|
|
/** |
|
|
* Return midi port |
|
|
*/ |
|
|
MidiInputPort* GetPort(int i) { return Ports[i]; } |
|
|
|
|
|
/** |
|
|
* Create new Midi port |
|
|
* This will be called by AcquirePorts |
|
|
* Each individual device must implement this. |
|
|
*/ |
|
|
virtual MidiInputPort* CreateMidiPort( void ) = 0; |
|
|
|
|
|
std::map<String,DeviceCreationParameter*> DeviceParameters(); |
|
|
|
|
|
///////////////////////////////////////////////////////////////// |
|
|
// abstract methods |
|
|
// (these have to be implemented by the descendant) |
|
|
|
|
|
/** |
|
|
* Start listen to MIDI input events on the MIDI input port. |
|
|
* The MIDIInputPort descendant should forward all MIDI input |
|
|
* events by calling the appropriate (protected) Dispatch* |
|
|
* method of class MidiInputPort. |
|
|
*/ |
|
|
virtual void Listen() = 0; |
|
|
|
|
|
/** |
|
|
* Stop to listen to MIDI input events on the MIDI input port. |
|
|
* After this method was called, the MidiInputPort descendant |
|
|
* should ignore all MIDI input events. |
|
|
*/ |
|
|
virtual void StopListen() = 0; |
|
|
|
|
|
/** |
|
|
* Return device driver name |
|
|
*/ |
|
|
virtual String Driver() = 0; |
|
140 |
|
|
|
protected: |
|
|
std::map<String,DeviceCreationParameter*> Parameters; ///< All device parameters. |
|
|
std::map<int,MidiInputPort*> Ports; |
|
141 |
|
|
|
MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters); |
|
142 |
|
|
143 |
virtual ~MidiInputDevice(); |
///////////////////////////////////////////////////////////////// |
144 |
|
// normal methods |
145 |
|
// (usually not to be overriden by descendant) |
146 |
|
|
147 |
|
/** |
148 |
|
* Return midi port \a iPort. |
149 |
|
* |
150 |
|
* @throws MidiInputException if index out of bounds |
151 |
|
*/ |
152 |
|
MidiInputPort* GetPort(uint iPort) throw (MidiInputException); |
153 |
|
|
154 |
|
/** |
155 |
|
* Return all device parameter settings. |
156 |
|
*/ |
157 |
|
std::map<String,DeviceCreationParameter*> DeviceParameters(); |
158 |
|
|
159 |
friend class Sampler; // allow Sampler class to destroy midi devices |
protected: |
160 |
|
std::map<String,DeviceCreationParameter*> Parameters; ///< All device parameters. |
161 |
|
std::map<int,MidiInputPort*> Ports; ///< All MIDI ports. |
162 |
|
void* pSampler; ///< Sampler instance. FIXME: should actually be of type Sampler* |
163 |
|
|
164 |
|
/** |
165 |
|
* Constructor |
166 |
|
* |
167 |
|
* FIXME: the pointer argument \a pSapmler should actually be of type Sampler*. |
168 |
|
* Unfortunately the bidirectional relationship between this |
169 |
|
* header and Sampler.h would clash on header file inclusion, |
170 |
|
* so that's why I had to make it of type void* here. This is |
171 |
|
* an annoying constraint of C++. |
172 |
|
*/ |
173 |
|
MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters, void* pSampler); |
174 |
|
|
175 |
|
/** |
176 |
|
* Destructor |
177 |
|
*/ |
178 |
|
virtual ~MidiInputDevice(); |
179 |
|
|
180 |
/** |
/** |
181 |
* Set number of MIDI ports required by the engine |
* Set number of MIDI ports required by the engine |
182 |
* This can either do nothing, create more ports |
* This can either do nothing, create more ports |
183 |
* or destroy ports depenging on the parameter |
* or destroy ports depenging on the parameter |
184 |
* and how many ports already exist on this driver. |
* and how many ports already exist on this driver. |
185 |
* |
* |
186 |
* @param Ports - number of ports to be left on this driver after this call. |
* @param Ports - number of ports to be left on this driver after this call. |
187 |
*/ |
*/ |
188 |
void AcquirePorts(uint Ports); |
void AcquirePorts(uint Ports); |
|
}; |
|
189 |
|
|
190 |
/** |
friend class ParameterActive; |
191 |
* Midi input exception that should be thrown by the MidiInputDevice |
friend class ParameterPorts; |
192 |
* descendants in case initialization of the MIDI input system failed |
friend class Sampler; // allow Sampler class to destroy midi devices |
193 |
* (which should be done in the constructor of the MidiInputDevice |
friend class MidiInputPort; // allow MidiInputPort to access pSampler |
|
* descendant). |
|
|
*/ |
|
|
class MidiInputException : public LinuxSamplerException { |
|
|
public: |
|
|
MidiInputException(const std::string& msg) : LinuxSamplerException(msg) {} |
|
194 |
}; |
}; |
195 |
} |
} |
196 |
|
|