/[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 212 by schoenebeck, Wed Jul 28 14:17:29 2004 UTC revision 1695 by schoenebeck, Sat Feb 16 01:09:33 2008 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 - 2008 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    
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      /** Abstract base class for MIDI input drivers in LinuxSampler
57       *       *
58       * This class will be derived by specialized classes which implement the       * This class will be derived by specialized classes which implement the
# Line 53  namespace LinuxSampler { Line 68  namespace LinuxSampler {
68              /////////////////////////////////////////////////////////////////              /////////////////////////////////////////////////////////////////
69              // type definitions              // type definitions
70    
71                /** Device Parameter 'ACTIVE'
72                 *
73                 * Used to activate / deactivate the MIDI input device.
74                 */
75              class ParameterActive : public DeviceCreationParameterBool {              class ParameterActive : public DeviceCreationParameterBool {
76                      public:                  public:
77                              ParameterActive( void ) : DeviceCreationParameterBool()                     { InitWithDefault(); }                      ParameterActive();
78                              ParameterActive( String active ) : DeviceCreationParameterBool(active)      { }                      ParameterActive(String active);
79                              virtual String Description()                                                { return "Enable / disable device";  }                      virtual String Description();
80                              virtual bool   Fix()                                                        { return false;                      }                      virtual bool   Fix();
81                              virtual bool   Mandatory()                                                  { return false;                      }                      virtual bool   Mandatory();
82                              virtual std::map<String,DeviceCreationParameter*> DependsAsParameters()     { return std::map<String,DeviceCreationParameter*>(); }                      virtual std::map<String,DeviceCreationParameter*> DependsAsParameters();
83                              virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters)    { return true;                       }                      virtual optional<bool> DefaultAsBool(std::map<String,String> Parameters);
84                              virtual void OnSetValue(bool b) throw (LinuxSamplerException)               { if (b) ((MidiInputDevice*)pDevice)->Listen(); else ((MidiInputDevice*)pDevice)->StopListen(); }                      virtual void OnSetValue(bool b) throw (Exception);
85                              static String Name( void ) { return "active"; }                      static String Name();
86              };              };
87    
88              class ParameterPorts : public DeviceCreationParameterInt {              /** Device Parameter 'PORTS'
89                      public:               *
90                              ParameterPorts( void ) : DeviceCreationParameterInt()                           { InitWithDefault(); }               * Used to increase / decrease the number of MIDI ports of the
91                              ParameterPorts( String val ) : DeviceCreationParameterInt(val)                  { }               * MIDI input device.
92                              virtual String Description()                                                    { return "Number of ports";   }               */
93                              virtual bool   Fix()                                                            { return false;   }              class ParameterPorts : public DeviceCreationParameterInt {
94                              virtual bool   Mandatory()                                                      { return false;   }                  public:
95                              virtual std::map<String,DeviceCreationParameter*> DependsAsParameters()         { return std::map<String,DeviceCreationParameter*>(); }                      ParameterPorts();
96                              virtual optional<int>    DefaultAsInt(std::map<String,String> Parameters)       { return 0; }                      ParameterPorts(String val);
97                              virtual optional<int>    RangeMinAsInt(std::map<String,String> Parameters)      { return optional<int>::nothing;   }                      virtual String Description();
98                              virtual optional<int>    RangeMaxAsInt(std::map<String,String> Parameters)      { return optional<int>::nothing;   }                      virtual bool   Fix();
99                              virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters) { return std::vector<int>();   }                      virtual bool   Mandatory();
100                              virtual void             OnSetValue(int i) throw (LinuxSamplerException)        { ((MidiInputDevice*)pDevice)->AcquirePorts(i); }                      virtual std::map<String,DeviceCreationParameter*> DependsAsParameters();
101                              static String Name( void ) { return "ports"; }                      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              class MidiInputPort {                      virtual std::vector<int> PossibilitiesAsInt(std::map<String,String> Parameters);
105                        virtual void             OnSetValue(int i) throw (Exception);
106                      public:                      static String Name();
107                              /**              };
108                               * MIDI channels  
109                               */  
110                              enum midi_chan_t {  
111                                      midi_chan_all = 0,              /////////////////////////////////////////////////////////////////
112                                      midi_chan_1   = 1,              // abstract methods
113                                      midi_chan_2   = 2,              //     (these have to be implemented by the descendant)
114                                      midi_chan_3   = 3,  
115                                      midi_chan_4   = 4,              /**
116                                      midi_chan_5   = 5,               * Start listen to MIDI input events on the MIDI input port.
117                                      midi_chan_6   = 6,               * The MIDIInputPort descendant should forward all MIDI input
118                                      midi_chan_7   = 7,               * events by calling the appropriate (protected) Dispatch*
119                                      midi_chan_8   = 8,               * method of class MidiInputPort.
120                                      midi_chan_9   = 9,               */
121                                      midi_chan_10  = 10,              virtual void Listen() = 0;
122                                      midi_chan_11  = 11,  
123                                      midi_chan_12  = 12,              /**
124                                      midi_chan_13  = 13,               * Stop to listen to MIDI input events on the MIDI input port.
125                                      midi_chan_14  = 14,               * After this method was called, the MidiInputPort descendant
126                                      midi_chan_15  = 15,               * should ignore all MIDI input events.
127                                      midi_chan_16  = 16               */
128                              };              virtual void StopListen() = 0;
129    
130                              class ParameterName : public DeviceCreationParameterString {              /**
131                                      public:               * Return device driver name
132                                              ParameterName(MidiInputPort* pPort) { this->pPort = pPort; InitWithDefault();}               */
133                                              ParameterName(MidiInputPort* pPort, String val) : DeviceCreationParameterString(val) { this->pPort = pPort; }              virtual String Driver() = 0;
134                                              virtual String Description()        { return "Name for this port"; }  
135                                              virtual bool   Fix()                { return false; }              /**
136                                              virtual bool   Mandatory()          { return false; }               * Create new Midi port
137                                              virtual std::map<String,DeviceCreationParameter*> DependsAsParameters() { return std::map<String,DeviceCreationParameter*>(); }               * This will be called by AcquirePorts
138                                              virtual optional<String>    DefaultAsString(std::map<String,String> Parameters) { return ""; }               * Each individual device must implement this.
139                                              virtual std::vector<String> PossibilitiesAsString(std::map<String,String> Parameters) { return std::vector<String>(); }               */
140                                              virtual void OnSetValue(String s) throw (LinuxSamplerException) { return; /* FIXME: Nothing to do here */ }              virtual MidiInputPort* CreateMidiPort() = 0;
141                                      protected:  
142                                              MidiInputPort * pPort;  
143                              };  
144                /////////////////////////////////////////////////////////////////
145                              /////////////////////////////////////////////////////////////////              // normal methods
146                              // normal methods              //     (usually not to be overriden by descendant)
147                              //     (usually not to be overriden by descendant)  
148                /**
149                              /**               * Return midi port \a iPort.
150                               * Connect given sampler engine with this MIDI input device.               *
151                               * The engine can either be connected to one specific MIDI               * @throws MidiInputException  if index out of bounds
152                               * channel or all MIDI channels. If an engine gets connected               */
153                               * twice to this MIDI input device, then the engine's old              MidiInputPort* GetPort(uint iPort) throw (MidiInputException);
154                               * connection will be detached (no matter on which MIDI channel).  
155                               *              /**
156                               * @param pEngine     - sampler engine               * Returns amount of MIDI ports this MIDI input device currently
157                               * @param MidiChannel - MIDI channel to connect to               * provides.
158                               * @throws MidiInputException  if MidiChannel argument invalid               */
159                               */              uint PortCount();
160                              void Connect(Engine* pEngine, midi_chan_t MidiChannel);  
161                /**
162                              /**               * Return all device parameter settings.
163                               * Disconnect given sampler engine from this MIDI input device.               */
164                               *              std::map<String,DeviceCreationParameter*> DeviceParameters();
165                               * @param pEngine - sampler engine  
166                               */              /**
167                              void Disconnect(Engine* pEngine);               * Registers the specified listener to be notified
168                 * when the number of MIDI input ports is changed.
169                              MidiInputDevice* GetDevice();               */
170                              uint GetPortNumber();              void AddMidiPortCountListener(MidiPortCountListener* l);
171                              std::map<String,DeviceCreationParameter*> DeviceParameters();  
172                /**
173                              /////////////////////////////////////////////////////////////////               * Removes the specified listener, to stop being notified of
174                              // dispatch methods               * further MIDI input port count chances.
175                              //     (should be called by the MidiInputDevice descendant on events)               */
176                void RemoveMidiPortCountListener(MidiPortCountListener* l);
                             /**  
                              * 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;  
177    
178          protected:          protected:
179              std::map<String,DeviceCreationParameter*> Parameters;  ///< All device parameters.              std::map<String,DeviceCreationParameter*> Parameters;  ///< All device parameters.
180              std::map<int,MidiInputPort*> Ports;              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              MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters);              /**
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              virtual ~MidiInputDevice();              /**
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              friend class Sampler; // allow Sampler class to destroy midi devices              /**
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               * Set number of MIDI ports required by the engine
223               * This can either do nothing, create more ports               * This can either do nothing, create more ports
224               * or destroy ports depenging on the parameter               * or destroy ports depenging on the parameter
225               * and how many ports already exist on this driver.               * 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.               * @param Ports - number of ports to be left on this driver after this call.
228               */               */
229              void AcquirePorts(uint Ports);              void AcquirePorts(uint Ports);
     };  
230    
231      /**              friend class ParameterActive;
232       * Midi input exception that should be thrown by the MidiInputDevice              friend class ParameterPorts;
233       * descendants in case initialization of the MIDI input system failed              friend class Sampler; // allow Sampler class to destroy midi devices
234       * (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) {}  
235      };      };
236  }  }
237    

Legend:
Removed from v.212  
changed lines
  Added in v.1695

  ViewVC Help
Powered by ViewVC