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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 207 by schoenebeck, Thu Jul 15 21:51:15 2004 UTC revision 2500 by schoenebeck, Fri Jan 10 12:20:05 2014 UTC
# Line 3  Line 3 
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 - 2014 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  *
# Line 29  Line 30 
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    #include "../../EventListeners.h"
38    
39  namespace LinuxSampler {  namespace LinuxSampler {
40    
41      // just symbol prototyping      // just symbol prototyping
42        class MidiInputPort;
43      class Engine;      class Engine;
44        class MidiInputDeviceFactory;
45    
46        /**
47         * Midi input exception that should be thrown by the MidiInputDevice
48         * descendants in case initialization of the MIDI input system failed
49         * (which should be done in the constructor of the MidiInputDevice
50         * descendant).
51         */
52        class MidiInputException : public Exception {
53            public:
54                MidiInputException(const std::string& msg) : Exception(msg) {}
55        };
56    
57      /** Abstract base class for MIDI input drivers in LinuxSampler      /** Abstract base class for MIDI input drivers in LinuxSampler
58       *       *
# Line 53  namespace LinuxSampler { Line 69  namespace LinuxSampler {
69              /////////////////////////////////////////////////////////////////              /////////////////////////////////////////////////////////////////
70              // type definitions              // type definitions
71    
72                /** Device Parameter 'ACTIVE'
73                 *
74                 * Used to activate / deactivate the MIDI input device.
75                 */
76              class ParameterActive : public DeviceCreationParameterBool {              class ParameterActive : public DeviceCreationParameterBool {
77                      public:                  public:
78                              ParameterActive( void ) : DeviceCreationParameterBool()                     { InitWithDefault(); }                      ParameterActive();
79                              ParameterActive( String active ) : DeviceCreationParameterBool(active)      { }                      ParameterActive(String active);
80                              virtual String Description()                                                { return "Enable / disable device";  }                      virtual String Description() OVERRIDE;
81                              virtual bool   Fix()                                                        { return false;                      }                      virtual bool   Fix() OVERRIDE;
82                              virtual bool   Mandatory()                                                  { return false;                      }                      virtual bool   Mandatory() OVERRIDE;
83                              virtual std::map<String,DeviceCreationParameter*> DependsAsParameters()     { return std::map<String,DeviceCreationParameter*>(); }                      virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() OVERRIDE;
84                              virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters)    { return true;                       }                      virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters) OVERRIDE;
85                              virtual void OnSetValue(bool b) throw (LinuxSamplerException)               { if (b) ((MidiInputDevice*)pDevice)->Listen(); else ((MidiInputDevice*)pDevice)->StopListen(); }                      virtual void OnSetValue(bool b) throw (Exception) OVERRIDE;
86                              static String Name( void ) { return "active"; }                      static String Name();
87              };              };
88    
89              class ParameterPorts : public DeviceCreationParameterInt {              /** Device Parameter 'PORTS'
90                      public:               *
91                              ParameterPorts( void ) : DeviceCreationParameterInt()                           { InitWithDefault(); }               * Used to increase / decrease the number of MIDI ports of the
92                              ParameterPorts( String val ) : DeviceCreationParameterInt(val)                  { }               * MIDI input device.
93                              virtual String Description()                                                    { return "Number of ports";   }               */
94                              virtual bool   Fix()                                                            { return false;   }              class ParameterPorts : public DeviceCreationParameterInt {
95                              virtual bool   Mandatory()                                                      { return false;   }                  public:
96                              virtual std::map<String,DeviceCreationParameter*> DependsAsParameters()         { return std::map<String,DeviceCreationParameter*>(); }                      ParameterPorts();
97                              virtual optional<int>    DefaultAsInt(std::map<String,String> Parameters)       { return 0; }                      ParameterPorts(String val);
98                              virtual optional<int>    RangeMinAsInt(std::map<String,String> Parameters)      { return optional<int>::nothing;   }                      virtual String Description() OVERRIDE;
99                              virtual optional<int>    RangeMaxAsInt(std::map<String,String> Parameters)      { return optional<int>::nothing;   }                      virtual bool   Fix() OVERRIDE;
100                              virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters) { return std::vector<int>();   }                      virtual bool   Mandatory() OVERRIDE;
101                              virtual void             OnSetValue(int i) throw (LinuxSamplerException)        { ((MidiInputDevice*)pDevice)->AcquirePorts(i); }                      virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() OVERRIDE;
102                              static String Name( void ) { return "ports"; }                      virtual optional<int>    DefaultAsInt(std::map<String,String> Parameters) OVERRIDE;
103              };                      virtual optional<int>    RangeMinAsInt(std::map<String,String> Parameters) OVERRIDE;
104                        virtual optional<int>    RangeMaxAsInt(std::map<String,String> Parameters) OVERRIDE;
105              class MidiInputPort {                      virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters) OVERRIDE;
106                        virtual void             OnSetValue(int i) throw (Exception) OVERRIDE;
107                      public:                      static String Name();
108                              /**              };
109                               * MIDI channels  
110                               */  
111                              enum midi_chan_t {  
112                                      midi_chan_all = 0,              /////////////////////////////////////////////////////////////////
113                                      midi_chan_1   = 1,              // abstract methods
114                                      midi_chan_2   = 2,              //     (these have to be implemented by the descendant)
115                                      midi_chan_3   = 3,  
116                                      midi_chan_4   = 4,              /**
117                                      midi_chan_5   = 5,               * Start listen to MIDI input events on the MIDI input port.
118                                      midi_chan_6   = 6,               * The MIDIInputPort descendant should forward all MIDI input
119                                      midi_chan_7   = 7,               * events by calling the appropriate (protected) Dispatch*
120                                      midi_chan_8   = 8,               * method of class MidiInputPort.
121                                      midi_chan_9   = 9,               */
122                                      midi_chan_10  = 10,              virtual void Listen() = 0;
123                                      midi_chan_11  = 11,  
124                                      midi_chan_12  = 12,              /**
125                                      midi_chan_13  = 13,               * Stop to listen to MIDI input events on the MIDI input port.
126                                      midi_chan_14  = 14,               * After this method was called, the MidiInputPort descendant
127                                      midi_chan_15  = 15,               * should ignore all MIDI input events.
128                                      midi_chan_16  = 16               */
129                              };              virtual void StopListen() = 0;
130    
131                              class ParameterName : public DeviceCreationParameterString {              /**
132                                      public:               * Return device driver name
133                                              ParameterName(MidiInputPort* pPort) { this->pPort = pPort; InitWithDefault();}               */
134                                              ParameterName(MidiInputPort* pPort, String val) : DeviceCreationParameterString(val) { this->pPort = pPort; }              virtual String Driver() = 0;
135                                              virtual String Description()        { return "Name for this port"; }  
136                                              virtual bool   Fix()                { return false; }              /**
137                                              virtual bool   Mandatory()          { return false; }               * Create new Midi port
138                                              virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); }               * This will be called by AcquirePorts
139                                              virtual optional<String>    Default(std::map<String,String> Parameters) { return ""; }               * Each individual device must implement this.
140                                              virtual std::vector<String> PossibilitiesAsString(std::map<String,String> Parameters) { return std::vector<String>(); }               */
141                                              virtual void OnSetValue(String s) throw (LinuxSamplerException) { return; /* FIXME: Nothing to do here */ }              virtual MidiInputPort* CreateMidiPort() = 0;
142                                      protected:  
143                                              MidiInputPort * pPort;  
144                              };  
145                /////////////////////////////////////////////////////////////////
146                              /////////////////////////////////////////////////////////////////              // normal methods
147                              // normal methods              //     (usually not to be overriden by descendant)
148                              //     (usually not to be overriden by descendant)  
149                /**
150                              /**               * Return midi port \a iPort.
151                               * Connect given sampler engine with this MIDI input device.               *
152                               * The engine can either be connected to one specific MIDI               * @throws MidiInputException  if index out of bounds
153                               * channel or all MIDI channels. If an engine gets connected               */
154                               * twice to this MIDI input device, then the engine's old              MidiInputPort* GetPort(uint iPort) throw (MidiInputException);
155                               * connection will be detached (no matter on which MIDI channel).  
156                               *              /**
157                               * @param pEngine     - sampler engine               * Returns amount of MIDI ports this MIDI input device currently
158                               * @param MidiChannel - MIDI channel to connect to               * provides.
159                               * @throws MidiInputException  if MidiChannel argument invalid               */
160                               */              uint PortCount();
161                              void Connect(Engine* pEngine, midi_chan_t MidiChannel);  
162                /**
163                              /**               * Return all device parameter settings.
164                               * Disconnect given sampler engine from this MIDI input device.               */
165                               *              std::map<String,DeviceCreationParameter*> DeviceParameters();
166                               * @param pEngine - sampler engine  
167                               */              /**
168                              void Disconnect(Engine* pEngine);               * Returns the unique ID number associated with this MIDIInputDevice
169                 * instance. This ID number is unique among all MIDIInputDevice
170                              MidiInputDevice* GetDevice();               * instances of the same Sampler instance and during the whole
171                              uint GetPortNumber();               * lifetime of the Sampler instance.
172                              std::map<String,DeviceCreationParameter*> DeviceParameters();               *
173                 * @returns a value equal or larger than 0, a negative value only
174                              /////////////////////////////////////////////////////////////////               *          on severe internal problems
175                              // dispatch methods               */
176                              //     (should be called by the MidiInputDevice descendant on events)              int MidiInputDeviceID();
177    
178                              /**              /**
179                               * Should be called by the implementing MIDI input device               * Registers the specified listener to be notified
180                               * whenever a note on event arrived, this will cause the note on               * when the number of MIDI input ports is changed.
181                               * event to be forwarded to all connected engines on the               */
182                               * corresponding MIDI channel.              void AddMidiPortCountListener(MidiPortCountListener* l);
183                               *  
184                               * @param Key         - MIDI key number of the triggered key              /**
185                               * @param Velocity    - MIDI velocity of the triggered key               * Removes the specified listener, to stop being notified of
186                               * @param MidiChannel - MIDI channel on which event occured on               * further MIDI input port count chances.
187                               */               */
188                              void DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel);              void RemoveMidiPortCountListener(MidiPortCountListener* l);
   
                             /**  
                              * 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;  
189    
190          protected:          protected:
191              std::map<String,DeviceCreationParameter*> Parameters;  ///< All device parameters.              std::map<String,DeviceCreationParameter*> Parameters;  ///< All device parameters.
192              std::map<int,MidiInputPort*> Ports;              std::map<int,MidiInputPort*> Ports;                    ///< All MIDI ports.
193                void* pSampler;                                        ///< Sampler instance. FIXME: should actually be of type Sampler*
194                ListenerList<MidiPortCountListener*> portCountListeners;
195    
196              MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters);              /**
197                 * Constructor
198                 *
199                 * FIXME: the pointer argument \a pSapmler should actually be of type Sampler*.
200                 * Unfortunately the bidirectional relationship between this
201                 * header and Sampler.h would clash on header file inclusion,
202                 * so that's why I had to make it of type void* here. This is
203                 * an annoying constraint of C++.
204                 */
205                MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters, void* pSampler);
206    
207              virtual ~MidiInputDevice();              /**
208                 * Destructor
209                 */
210                virtual ~MidiInputDevice();
211    
212                /**
213                 * Notifies listeners that the amount of MIDI inpurt ports have
214                 * been changed.
215                 * @param NewCount The new number of MIDI input ports.
216                 */
217                void fireMidiPortCountChanged(int NewCount);
218    
219              friend class Sampler; // allow Sampler class to destroy midi devices              /**
220                 * Notifies listeners that the supplied MIDI input port is
221                 * going to be removed soon.
222                 * @param pPort The MIDI input port that is going to be removed.
223                 */
224                void fireMidiPortToBeRemoved(MidiInputPort* pPort);
225    
226                /**
227                 * Notifies listeners that the supplied MIDI input port has
228                 * just been added.
229                 * @param pPort The MIDI input port that has been added.
230                 */
231                void fireMidiPortAdded(MidiInputPort* pPort);
232    
233              /**              /**
234               * Set number of MIDI ports required by the engine               * Set number of MIDI ports required by the engine
235               * This can either do nothing, create more ports               * This can either do nothing, create more ports
236               * or destroy ports depenging on the parameter               * or destroy ports depenging on the parameter
237               * and how many ports already exist on this driver.               * and how many ports already exist on this driver.
238               *               *
239               * @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.
240               */               */
241              void AcquirePorts(uint Ports);              void AcquirePorts(uint Ports);
     };  
242    
243      /**              friend class ParameterActive;
244       * Midi input exception that should be thrown by the MidiInputDevice              friend class ParameterPorts;
245       * descendants in case initialization of the MIDI input system failed              friend class MidiInputDeviceFactory; // allow MidiInputDeviceFactory class to destroy midi devices
246       * (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) {}  
247      };      };
248  }  }
249    

Legend:
Removed from v.207  
changed lines
  Added in v.2500

  ViewVC Help
Powered by ViewVC