1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 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_MIDIINPUTDEVICE_H__ |
25 |
#define __LS_MIDIINPUTDEVICE_H__ |
26 |
|
27 |
#include <stdexcept> |
28 |
#include <set> |
29 |
#include <map> |
30 |
#include <vector> |
31 |
|
32 |
#include "../../common/global.h" |
33 |
#include "../../common/Exception.h" |
34 |
#include "../DeviceParameter.h" |
35 |
#include "MidiInputPort.h" |
36 |
#include "../../engines/Engine.h" |
37 |
#include "../../EventListeners.h" |
38 |
|
39 |
namespace LinuxSampler { |
40 |
|
41 |
// just symbol prototyping |
42 |
class MidiInputPort; |
43 |
class Engine; |
44 |
|
45 |
/** |
46 |
* Midi input exception that should be thrown by the MidiInputDevice |
47 |
* descendants in case initialization of the MIDI input system failed |
48 |
* (which should be done in the constructor of the MidiInputDevice |
49 |
* descendant). |
50 |
*/ |
51 |
class MidiInputException : public Exception { |
52 |
public: |
53 |
MidiInputException(const std::string& msg) : Exception(msg) {} |
54 |
}; |
55 |
|
56 |
/** Abstract base class for MIDI input drivers in LinuxSampler |
57 |
* |
58 |
* This class will be derived by specialized classes which implement the |
59 |
* connection to a specific MIDI input system (e.g. Alsa Sequencer, |
60 |
* CoreMIDI). The MidiInputDevice desendant should just call the |
61 |
* appropriate (protected) Dispatch* method here when an MIDI event |
62 |
* occured. The dispatch* methods here will automatically forward the |
63 |
* MIDI event to the appropriate, connected sampler engines. |
64 |
*/ |
65 |
class MidiInputDevice : public Device { |
66 |
public: |
67 |
|
68 |
///////////////////////////////////////////////////////////////// |
69 |
// type definitions |
70 |
|
71 |
/** Device Parameter 'ACTIVE' |
72 |
* |
73 |
* Used to activate / deactivate the MIDI input device. |
74 |
*/ |
75 |
class ParameterActive : public DeviceCreationParameterBool { |
76 |
public: |
77 |
ParameterActive(); |
78 |
ParameterActive(String active); |
79 |
virtual String Description(); |
80 |
virtual bool Fix(); |
81 |
virtual bool Mandatory(); |
82 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters(); |
83 |
virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters); |
84 |
virtual void OnSetValue(bool b) throw (Exception); |
85 |
static String Name(); |
86 |
}; |
87 |
|
88 |
/** Device Parameter 'PORTS' |
89 |
* |
90 |
* Used to increase / decrease the number of MIDI ports of the |
91 |
* MIDI input device. |
92 |
*/ |
93 |
class ParameterPorts : public DeviceCreationParameterInt { |
94 |
public: |
95 |
ParameterPorts(); |
96 |
ParameterPorts(String val); |
97 |
virtual String Description(); |
98 |
virtual bool Fix(); |
99 |
virtual bool Mandatory(); |
100 |
virtual std::map<String,DeviceCreationParameter*> DependsAsParameters(); |
101 |
virtual optional<int> DefaultAsInt(std::map<String,String> Parameters); |
102 |
virtual optional<int> RangeMinAsInt(std::map<String,String> Parameters); |
103 |
virtual optional<int> RangeMaxAsInt(std::map<String,String> Parameters); |
104 |
virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters); |
105 |
virtual void OnSetValue(int i) throw (Exception); |
106 |
static String Name(); |
107 |
}; |
108 |
|
109 |
|
110 |
|
111 |
///////////////////////////////////////////////////////////////// |
112 |
// abstract methods |
113 |
// (these have to be implemented by the descendant) |
114 |
|
115 |
/** |
116 |
* Start listen to MIDI input events on the MIDI input port. |
117 |
* The MIDIInputPort descendant should forward all MIDI input |
118 |
* events by calling the appropriate (protected) Dispatch* |
119 |
* method of class MidiInputPort. |
120 |
*/ |
121 |
virtual void Listen() = 0; |
122 |
|
123 |
/** |
124 |
* Stop to listen to MIDI input events on the MIDI input port. |
125 |
* After this method was called, the MidiInputPort descendant |
126 |
* should ignore all MIDI input events. |
127 |
*/ |
128 |
virtual void StopListen() = 0; |
129 |
|
130 |
/** |
131 |
* Return device driver name |
132 |
*/ |
133 |
virtual String Driver() = 0; |
134 |
|
135 |
/** |
136 |
* Create new Midi port |
137 |
* This will be called by AcquirePorts |
138 |
* Each individual device must implement this. |
139 |
*/ |
140 |
virtual MidiInputPort* CreateMidiPort() = 0; |
141 |
|
142 |
|
143 |
|
144 |
///////////////////////////////////////////////////////////////// |
145 |
// normal methods |
146 |
// (usually not to be overriden by descendant) |
147 |
|
148 |
/** |
149 |
* Return midi port \a iPort. |
150 |
* |
151 |
* @throws MidiInputException if index out of bounds |
152 |
*/ |
153 |
MidiInputPort* GetPort(uint iPort) throw (MidiInputException); |
154 |
|
155 |
/** |
156 |
* Returns amount of MIDI ports this MIDI input device currently |
157 |
* provides. |
158 |
*/ |
159 |
uint PortCount(); |
160 |
|
161 |
/** |
162 |
* Return all device parameter settings. |
163 |
*/ |
164 |
std::map<String,DeviceCreationParameter*> DeviceParameters(); |
165 |
|
166 |
/** |
167 |
* Registers the specified listener to be notified |
168 |
* when the number of MIDI input ports is changed. |
169 |
*/ |
170 |
void AddMidiPortCountListener(MidiPortCountListener* l); |
171 |
|
172 |
/** |
173 |
* Removes the specified listener, to stop being notified of |
174 |
* further MIDI input port count chances. |
175 |
*/ |
176 |
void RemoveMidiPortCountListener(MidiPortCountListener* l); |
177 |
|
178 |
protected: |
179 |
std::map<String,DeviceCreationParameter*> Parameters; ///< All device parameters. |
180 |
std::map<int,MidiInputPort*> Ports; ///< All MIDI ports. |
181 |
void* pSampler; ///< Sampler instance. FIXME: should actually be of type Sampler* |
182 |
ListenerList<MidiPortCountListener*> portCountListeners; |
183 |
|
184 |
/** |
185 |
* Constructor |
186 |
* |
187 |
* FIXME: the pointer argument \a pSapmler should actually be of type Sampler*. |
188 |
* Unfortunately the bidirectional relationship between this |
189 |
* header and Sampler.h would clash on header file inclusion, |
190 |
* so that's why I had to make it of type void* here. This is |
191 |
* an annoying constraint of C++. |
192 |
*/ |
193 |
MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters, void* pSampler); |
194 |
|
195 |
/** |
196 |
* Destructor |
197 |
*/ |
198 |
virtual ~MidiInputDevice(); |
199 |
|
200 |
/** |
201 |
* Notifies listeners that the amount of MIDI inpurt ports have |
202 |
* been changed. |
203 |
* @param NewCount The new number of MIDI input ports. |
204 |
*/ |
205 |
void fireMidiPortCountChanged(int NewCount); |
206 |
|
207 |
/** |
208 |
* Notifies listeners that the supplied MIDI input port is |
209 |
* going to be removed soon. |
210 |
* @param pPort The MIDI input port that is going to be removed. |
211 |
*/ |
212 |
void fireMidiPortToBeRemoved(MidiInputPort* pPort); |
213 |
|
214 |
/** |
215 |
* Notifies listeners that the supplied MIDI input port has |
216 |
* just been added. |
217 |
* @param pPort The MIDI input port that has been added. |
218 |
*/ |
219 |
void fireMidiPortAdded(MidiInputPort* pPort); |
220 |
|
221 |
/** |
222 |
* Set number of MIDI ports required by the engine |
223 |
* This can either do nothing, create more ports |
224 |
* or destroy ports depenging on the parameter |
225 |
* and how many ports already exist on this driver. |
226 |
* |
227 |
* @param Ports - number of ports to be left on this driver after this call. |
228 |
*/ |
229 |
void AcquirePorts(uint Ports); |
230 |
|
231 |
friend class ParameterActive; |
232 |
friend class ParameterPorts; |
233 |
friend class Sampler; // allow Sampler class to destroy midi devices |
234 |
friend class MidiInputPort; // allow MidiInputPort to access pSampler |
235 |
}; |
236 |
} |
237 |
|
238 |
#endif // __LS_MIDIINPUTDEVICE_H__ |