--- linuxsampler/trunk/src/drivers/midi/MidiInputDevice.h 2004/07/15 21:51:15 207 +++ linuxsampler/trunk/src/drivers/midi/MidiInputDevice.h 2014/01/10 12:20:05 2500 @@ -3,6 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005 - 2014 Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -29,14 +30,29 @@ #include #include "../../common/global.h" -#include "../../common/LinuxSamplerException.h" +#include "../../common/Exception.h" #include "../DeviceParameter.h" -#include "../../engines/common/Engine.h" +#include "MidiInputPort.h" +#include "../../engines/Engine.h" +#include "../../EventListeners.h" namespace LinuxSampler { // just symbol prototyping + class MidiInputPort; class Engine; + class MidiInputDeviceFactory; + + /** + * Midi input exception that should be thrown by the MidiInputDevice + * descendants in case initialization of the MIDI input system failed + * (which should be done in the constructor of the MidiInputDevice + * descendant). + */ + class MidiInputException : public Exception { + public: + MidiInputException(const std::string& msg) : Exception(msg) {} + }; /** Abstract base class for MIDI input drivers in LinuxSampler * @@ -53,235 +69,181 @@ ///////////////////////////////////////////////////////////////// // type definitions + /** Device Parameter 'ACTIVE' + * + * Used to activate / deactivate the MIDI input device. + */ class ParameterActive : public DeviceCreationParameterBool { - public: - ParameterActive( void ) : DeviceCreationParameterBool() { InitWithDefault(); } - ParameterActive( String active ) : DeviceCreationParameterBool(active) { } - virtual String Description() { return "Enable / disable device"; } - virtual bool Fix() { return false; } - virtual bool Mandatory() { return false; } - virtual std::map DependsAsParameters() { return std::map(); } - virtual optional DefaultAsBool(std::map Parameters) { return true; } - virtual void OnSetValue(bool b) throw (LinuxSamplerException) { if (b) ((MidiInputDevice*)pDevice)->Listen(); else ((MidiInputDevice*)pDevice)->StopListen(); } - static String Name( void ) { return "active"; } - }; - - class ParameterPorts : public DeviceCreationParameterInt { - public: - ParameterPorts( void ) : DeviceCreationParameterInt() { InitWithDefault(); } - ParameterPorts( String val ) : DeviceCreationParameterInt(val) { } - virtual String Description() { return "Number of ports"; } - virtual bool Fix() { return false; } - virtual bool Mandatory() { return false; } - virtual std::map DependsAsParameters() { return std::map(); } - virtual optional DefaultAsInt(std::map Parameters) { return 0; } - virtual optional RangeMinAsInt(std::map Parameters) { return optional::nothing; } - virtual optional RangeMaxAsInt(std::map Parameters) { return optional::nothing; } - virtual std::vector PossibilitiesAsInt(std::map Parameters) { return std::vector(); } - virtual void OnSetValue(int i) throw (LinuxSamplerException) { ((MidiInputDevice*)pDevice)->AcquirePorts(i); } - static String Name( void ) { return "ports"; } - }; - - class MidiInputPort { - - public: - /** - * MIDI channels - */ - enum midi_chan_t { - midi_chan_all = 0, - midi_chan_1 = 1, - midi_chan_2 = 2, - midi_chan_3 = 3, - midi_chan_4 = 4, - midi_chan_5 = 5, - midi_chan_6 = 6, - midi_chan_7 = 7, - midi_chan_8 = 8, - midi_chan_9 = 9, - midi_chan_10 = 10, - midi_chan_11 = 11, - midi_chan_12 = 12, - midi_chan_13 = 13, - midi_chan_14 = 14, - midi_chan_15 = 15, - midi_chan_16 = 16 - }; - - class ParameterName : public DeviceCreationParameterString { - public: - ParameterName(MidiInputPort* pPort) { this->pPort = pPort; InitWithDefault();} - ParameterName(MidiInputPort* pPort, String val) : DeviceCreationParameterString(val) { this->pPort = pPort; } - virtual String Description() { return "Name for this port"; } - virtual bool Fix() { return false; } - virtual bool Mandatory() { return false; } - virtual std::map DependsAsParameters() { return std::map(); } - virtual optional Default(std::map Parameters) { return ""; } - virtual std::vector PossibilitiesAsString(std::map Parameters) { return std::vector(); } - virtual void OnSetValue(String s) throw (LinuxSamplerException) { return; /* FIXME: Nothing to do here */ } - 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 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 Parameters; ///< All port parameters. - std::set 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 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; + public: + ParameterActive(); + ParameterActive(String active); + virtual String Description() OVERRIDE; + virtual bool Fix() OVERRIDE; + virtual bool Mandatory() OVERRIDE; + virtual std::map DependsAsParameters() OVERRIDE; + virtual optional DefaultAsBool(std::map Parameters) OVERRIDE; + virtual void OnSetValue(bool b) throw (Exception) OVERRIDE; + static String Name(); + }; + + /** Device Parameter 'PORTS' + * + * Used to increase / decrease the number of MIDI ports of the + * MIDI input device. + */ + class ParameterPorts : public DeviceCreationParameterInt { + public: + ParameterPorts(); + ParameterPorts(String val); + virtual String Description() OVERRIDE; + virtual bool Fix() OVERRIDE; + virtual bool Mandatory() OVERRIDE; + virtual std::map DependsAsParameters() OVERRIDE; + virtual optional DefaultAsInt(std::map Parameters) OVERRIDE; + virtual optional RangeMinAsInt(std::map Parameters) OVERRIDE; + virtual optional RangeMaxAsInt(std::map Parameters) OVERRIDE; + virtual std::vector PossibilitiesAsInt(std::map Parameters) OVERRIDE; + virtual void OnSetValue(int i) throw (Exception) OVERRIDE; + static String Name(); + }; + + + + ///////////////////////////////////////////////////////////////// + // 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; + + /** + * Create new Midi port + * This will be called by AcquirePorts + * Each individual device must implement this. + */ + virtual MidiInputPort* CreateMidiPort() = 0; + + + + ///////////////////////////////////////////////////////////////// + // normal methods + // (usually not to be overriden by descendant) + + /** + * Return midi port \a iPort. + * + * @throws MidiInputException if index out of bounds + */ + MidiInputPort* GetPort(uint iPort) throw (MidiInputException); + + /** + * Returns amount of MIDI ports this MIDI input device currently + * provides. + */ + uint PortCount(); + + /** + * Return all device parameter settings. + */ + std::map DeviceParameters(); + + /** + * Returns the unique ID number associated with this MIDIInputDevice + * instance. This ID number is unique among all MIDIInputDevice + * instances of the same Sampler instance and during the whole + * lifetime of the Sampler instance. + * + * @returns a value equal or larger than 0, a negative value only + * on severe internal problems + */ + int MidiInputDeviceID(); + + /** + * Registers the specified listener to be notified + * when the number of MIDI input ports is changed. + */ + void AddMidiPortCountListener(MidiPortCountListener* l); + + /** + * Removes the specified listener, to stop being notified of + * further MIDI input port count chances. + */ + void RemoveMidiPortCountListener(MidiPortCountListener* l); protected: - std::map Parameters; ///< All device parameters. - std::map Ports; + std::map Parameters; ///< All device parameters. + std::map Ports; ///< All MIDI ports. + void* pSampler; ///< Sampler instance. FIXME: should actually be of type Sampler* + ListenerList portCountListeners; - MidiInputDevice(std::map DriverParameters); + /** + * Constructor + * + * FIXME: the pointer argument \a pSapmler should actually be of type Sampler*. + * Unfortunately the bidirectional relationship between this + * header and Sampler.h would clash on header file inclusion, + * so that's why I had to make it of type void* here. This is + * an annoying constraint of C++. + */ + MidiInputDevice(std::map DriverParameters, void* pSampler); - virtual ~MidiInputDevice(); + /** + * Destructor + */ + virtual ~MidiInputDevice(); + /** + * Notifies listeners that the amount of MIDI inpurt ports have + * been changed. + * @param NewCount The new number of MIDI input ports. + */ + void fireMidiPortCountChanged(int NewCount); - friend class Sampler; // allow Sampler class to destroy midi devices + /** + * Notifies listeners that the supplied MIDI input port is + * going to be removed soon. + * @param pPort The MIDI input port that is going to be removed. + */ + void fireMidiPortToBeRemoved(MidiInputPort* pPort); + + /** + * Notifies listeners that the supplied MIDI input port has + * just been added. + * @param pPort The MIDI input port that has been added. + */ + void fireMidiPortAdded(MidiInputPort* pPort); /** * Set number of MIDI ports required by the engine - * This can either do nothing, create more ports - * or destroy ports depenging on the parameter - * and how many ports already exist on this driver. + * This can either do nothing, create more ports + * or destroy ports depenging on the parameter + * and how many ports already exist on this driver. * * @param Ports - number of ports to be left on this driver after this call. */ - void AcquirePorts(uint Ports); - }; + void AcquirePorts(uint Ports); - /** - * Midi input exception that should be thrown by the MidiInputDevice - * descendants in case initialization of the MIDI input system failed - * (which should be done in the constructor of the MidiInputDevice - * descendant). - */ - class MidiInputException : public LinuxSamplerException { - public: - MidiInputException(const std::string& msg) : LinuxSamplerException(msg) {} + friend class ParameterActive; + friend class ParameterPorts; + friend class MidiInputDeviceFactory; // allow MidiInputDeviceFactory class to destroy midi devices + friend class MidiInputPort; // allow MidiInputPort to access pSampler }; }