/[svn]/linuxsampler/trunk/src/drivers/midi/MidiInputDeviceFactory.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/drivers/midi/MidiInputDeviceFactory.cpp

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

revision 1933 by iliev, Mon Feb 16 17:56:50 2009 UTC revision 1934 by schoenebeck, Sun Jul 12 10:35:55 2009 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                       *   *   Copyright (C) 2005 - 2009 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 87  namespace LinuxSampler { Line 87  namespace LinuxSampler {
87      REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDeviceJack, ParameterName);      REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDeviceJack, ParameterName);
88  #endif // HAVE_JACK_MIDI  #endif // HAVE_JACK_MIDI
89    
90        MidiInputDeviceFactory::MidiInputDeviceMap MidiInputDeviceFactory::mMidiInputDevices;
91    
92        /**
93         * Creates a new MIDI input device for the given driver name and
94         * parameters. Note, this method will also check whether it is actually
95         * allowed to create an instance of the given driver on its own and throw
96         * an Exception in case it is not allowed (this is the case for host plugin
97         * drivers like VST, AU, DSSI, LV2).
98         *
99         * @param DriverName - name of the driver of which a new device shall be created
100         * @param Parameters - device creation parameters which shall be passed to the driver's constructor
101         * @param pSampler - the sampler instance
102         *
103         * @returns pointer to the new device instance
104         * @throws Exception - in case the device could not be created
105         *
106         * @see CreatePrivate()
107         */
108      MidiInputDevice* MidiInputDeviceFactory::Create(String DriverName, std::map<String,String> Parameters, Sampler* pSampler) throw (Exception) {      MidiInputDevice* MidiInputDeviceFactory::Create(String DriverName, std::map<String,String> Parameters, Sampler* pSampler) throw (Exception) {
109            if (!InnerFactories.count(DriverName))
110                throw Exception("There is no midi input driver '" + DriverName + "'.");
111            if (!InnerFactories[DriverName]->isAutonomousDriver())
112                throw Exception("You cannot directly create a new MIDI input device of the '" + DriverName + "' driver!");
113    
114            return CreatePrivate(DriverName, Parameters, pSampler);
115        }
116    
117        /**
118         * Same as Create(), but this method won't check whether it is allowed to
119         * create an instance of the given driver on its own. This method is
120         * usually called by host plugins (e.g. VST, AU, DSSI, LV2) to actually
121         * create their respective MIDI input devices for the sampler. Usually one
122         * shouldn't call this method directly, but call Create() instead.
123         */
124        MidiInputDevice* MidiInputDeviceFactory::CreatePrivate(String DriverName, std::map<String,String> Parameters, Sampler* pSampler) throw (Exception) {
125          if (!InnerFactories.count(DriverName)) throw Exception("There is no midi input driver '" + DriverName + "'.");          if (!InnerFactories.count(DriverName)) throw Exception("There is no midi input driver '" + DriverName + "'.");
126          //Let's see if we need to create parameters          // let's see if we need to create parameters
127          std::map<String,DeviceCreationParameter*> thisDeviceParams;          std::map<String,DeviceCreationParameter*> thisDeviceParams;
128          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];
129          if (pParamFactory) {          if (pParamFactory) {
130                  thisDeviceParams = pParamFactory->CreateAllParams(Parameters);              thisDeviceParams = pParamFactory->CreateAllParams(Parameters);
131          } else {          } else {
132                  //No parameters are registered by the driver. Throw if any parameters were specified.              // no parameters are registered by the driver. Throw if any parameters were specified.
133                  if (Parameters.size() != 0) throw Exception("Driver '" + DriverName + "' does not have any parameters.");              if (Parameters.size() != 0) throw Exception("Driver '" + DriverName + "' does not have any parameters.");
134          }          }
135          //Now create the device using those parameters  
136          MidiInputDevice* pDevice = InnerFactories[DriverName]->Create(thisDeviceParams, pSampler);          // get a free device id
137          //Now attach all parameters to the newely created device.          int iDeviceId = -1;
138          for (std::map<String,DeviceCreationParameter*>::iterator iter = thisDeviceParams.begin(); iter != thisDeviceParams.end(); iter++) {          for (int i = 0; i >= 0; i++) { // seek for a free place starting from the beginning
139                  iter->second->Attach(pDevice);              if (!mMidiInputDevices[i]) {
140          }                  iDeviceId = i;
141          return pDevice;                  break;
142                }
143            }
144            if (iDeviceId < 0)
145                throw Exception("Could not retrieve free device ID!");
146    
147            // now create the device using those parameters
148            MidiInputDevice* pDevice = InnerFactories[DriverName]->Create(thisDeviceParams, pSampler);
149            pDevice->setDeviceId(iDeviceId);
150            // now attach all parameters to the newely created device.
151            for (std::map<String,DeviceCreationParameter*>::iterator iter = thisDeviceParams.begin(); iter != thisDeviceParams.end(); iter++) {
152                iter->second->Attach(pDevice);
153            }
154    
155            // add new audio device to the audio device list
156            mMidiInputDevices[iDeviceId] = pDevice;
157    
158            return pDevice;
159      }      }
160    
161      std::vector<String> MidiInputDeviceFactory::AvailableDrivers() {      std::vector<String> MidiInputDeviceFactory::AvailableDrivers() {
# Line 130  namespace LinuxSampler { Line 181  namespace LinuxSampler {
181    
182      std::map<String,DeviceCreationParameter*> MidiInputDeviceFactory::GetAvailableDriverParameters(String DriverName) throw (Exception) {      std::map<String,DeviceCreationParameter*> MidiInputDeviceFactory::GetAvailableDriverParameters(String DriverName) throw (Exception) {
183          if (!InnerFactories.count(DriverName)) throw Exception("There is no midi input driver '" + DriverName + "'.");          if (!InnerFactories.count(DriverName)) throw Exception("There is no midi input driver '" + DriverName + "'.");
184          std::map<String,DeviceCreationParameter*> thisDeviceParams;          std::map<String,DeviceCreationParameter*> thisDeviceParams;
185          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];
186          if (pParamFactory) {          if (pParamFactory) {
187                  thisDeviceParams = pParamFactory->CreateAllParams();              thisDeviceParams = pParamFactory->CreateAllParams();
188          }          }
189          return thisDeviceParams;          return thisDeviceParams;
190      }      }
191    
192      DeviceCreationParameter* MidiInputDeviceFactory::GetDriverParameter(String DriverName, String ParameterName) throw (Exception) {      DeviceCreationParameter* MidiInputDeviceFactory::GetDriverParameter(String DriverName, String ParameterName) throw (Exception) {
# Line 158  namespace LinuxSampler { Line 209  namespace LinuxSampler {
209          return InnerFactories[DriverName]->Version();          return InnerFactories[DriverName]->Version();
210      }      }
211    
212        std::map<uint, MidiInputDevice*> MidiInputDeviceFactory::Devices() {
213            return mMidiInputDevices;
214        }
215    
216        /**
217         * Destroys the given device. Usually this method shouldn't be called
218         * directly, Sampler::DestroyMidiInputDevice should be called instead,
219         * since it also takes care whether some sampler channel is still using
220         * the device, etc.
221         */
222        void MidiInputDeviceFactory::Destroy(MidiInputDevice* pDevice) throw (Exception) {
223            if (pDevice && !pDevice->isAutonomousDevice())
224                throw Exception("You cannot directly destroy this '" + pDevice->Driver() + "' device!");
225    
226            DestroyPrivate(pDevice);
227        }
228    
229        void MidiInputDeviceFactory::DestroyPrivate(MidiInputDevice* pDevice) throw (Exception) {
230            MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
231            for (; iter != mMidiInputDevices.end(); iter++) {
232                if (iter->second == pDevice) {
233                    // disable device
234                    pDevice->StopListen();
235    
236                    // remove device from the device list
237                    mMidiInputDevices.erase(iter);
238    
239                    // destroy and free device from memory
240                    delete pDevice;
241    
242                    break;
243                }
244            }
245        }
246    
247  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1933  
changed lines
  Added in v.1934

  ViewVC Help
Powered by ViewVC