/[svn]/linuxsampler/trunk/src/drivers/audio/AudioOutputDeviceFactory.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/drivers/audio/AudioOutputDeviceFactory.cpp

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

revision 1933 by persson, Sun Apr 26 12:19:00 2009 UTC revision 1934 by schoenebeck, Sun Jul 12 10:35:55 2009 UTC
# Line 104  namespace LinuxSampler { Line 104  namespace LinuxSampler {
104      REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceCoreAudio, ParameterBufferSize);      REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceCoreAudio, ParameterBufferSize);
105  #endif // HAVE_COREAUDIO  #endif // HAVE_COREAUDIO
106    
107        AudioOutputDeviceFactory::AudioOutputDeviceMap AudioOutputDeviceFactory::mAudioOutputDevices;
108    
109        /**
110         * Creates a new audio output device for the given driver name and
111         * parameters. Note, this method will also check whether it is actually
112         * allowed to create an instance of the given driver on its own and throw
113         * an Exception in case it is not allowed (this is the case for host plugin
114         * drivers like VST, AU, DSSI, LV2).
115         *
116         * @param DriverName - name of the driver of which a new device shall be created
117         * @param Parameters - device creation parameters which shall be passed to the driver's constructor
118         *
119         * @returns pointer to the new device instance
120         * @throws Exception - in case the device could not be created
121         *
122         * @see CreatePrivate()
123         */
124      AudioOutputDevice* AudioOutputDeviceFactory::Create(String DriverName, std::map<String,String> Parameters) throw (Exception) {      AudioOutputDevice* AudioOutputDeviceFactory::Create(String DriverName, std::map<String,String> Parameters) throw (Exception) {
125            if (!InnerFactories.count(DriverName))
126                throw Exception("There is no audio output driver '" + DriverName + "'.");
127            if (!InnerFactories[DriverName]->isAutonomousDriver())
128                throw Exception("You cannot directly create a new audio output device of the '" + DriverName + "' driver!");
129    
130            return CreatePrivate(DriverName, Parameters);
131        }
132    
133        /**
134         * Same as Create(), but this method won't check whether it is allowed to
135         * create an instance of the given driver on its own. This method is
136         * usually called by host plugins (e.g. VST, AU, DSSI, LV2) to actually
137         * create their respective audio output devices for the sampler. Usually
138         * one shouldn't call this method directly, but call Create() instead.
139         */
140        AudioOutputDevice* AudioOutputDeviceFactory::CreatePrivate(String DriverName, std::map<String,String> Parameters) throw (Exception) {
141          if (!InnerFactories.count(DriverName)) throw Exception("There is no audio output driver '" + DriverName + "'.");          if (!InnerFactories.count(DriverName)) throw Exception("There is no audio output driver '" + DriverName + "'.");
142          //Let's see if we need to create parameters          // let's see if we need to create parameters
143          std::map<String,DeviceCreationParameter*> thisDeviceParams;          std::map<String,DeviceCreationParameter*> thisDeviceParams;
144          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];
145          if (pParamFactory) {          if (pParamFactory) {
146                  thisDeviceParams = pParamFactory->CreateAllParams(Parameters);              thisDeviceParams = pParamFactory->CreateAllParams(Parameters);
147          } else {          } else {
148                  //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.
149                  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.");
150          }          }
151          //Now create the device using those parameters  
152            // get a free device id
153            int iDeviceId = -1;
154            for (int i = 0; i >= 0; i++) { // seek for a free place starting from the beginning
155                if (!mAudioOutputDevices[i]) {
156                    iDeviceId = i;
157                    break;
158                }
159            }
160            if (iDeviceId < 0)
161                throw Exception("Could not retrieve free device ID!");
162    
163            // now create the device using those parameters
164          AudioOutputDevice* pDevice = InnerFactories[DriverName]->Create(thisDeviceParams);          AudioOutputDevice* pDevice = InnerFactories[DriverName]->Create(thisDeviceParams);
165          //Now attach all parameters to the newely created device.          pDevice->setDeviceId(iDeviceId);
166          for (std::map<String,DeviceCreationParameter*>::iterator iter = thisDeviceParams.begin(); iter != thisDeviceParams.end(); iter++) {          // now attach all parameters to the newely created device.
167                  iter->second->Attach(pDevice);          for (std::map<String,DeviceCreationParameter*>::iterator iter = thisDeviceParams.begin(); iter != thisDeviceParams.end(); iter++) {
168          }              iter->second->Attach(pDevice);
169          return pDevice;          }
170    
171            // add new audio device to the audio device list
172            mAudioOutputDevices[iDeviceId] = pDevice;
173    
174            return pDevice;
175      }      }
176    
177      std::vector<String> AudioOutputDeviceFactory::AvailableDrivers() {      std::vector<String> AudioOutputDeviceFactory::AvailableDrivers() {
# Line 147  namespace LinuxSampler { Line 197  namespace LinuxSampler {
197    
198      std::map<String,DeviceCreationParameter*> AudioOutputDeviceFactory::GetAvailableDriverParameters(String DriverName) throw (Exception) {      std::map<String,DeviceCreationParameter*> AudioOutputDeviceFactory::GetAvailableDriverParameters(String DriverName) throw (Exception) {
199          if (!InnerFactories.count(DriverName)) throw Exception("There is no audio output driver '" + DriverName + "'.");          if (!InnerFactories.count(DriverName)) throw Exception("There is no audio output driver '" + DriverName + "'.");
200          std::map<String,DeviceCreationParameter*> thisDeviceParams;          std::map<String,DeviceCreationParameter*> thisDeviceParams;
201          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];          DeviceParameterFactory* pParamFactory = ParameterFactories[DriverName];
202          if (pParamFactory) {          if (pParamFactory) {
203                  thisDeviceParams = pParamFactory->CreateAllParams();              thisDeviceParams = pParamFactory->CreateAllParams();
204          }          }
205          return thisDeviceParams;          return thisDeviceParams;
206      }      }
207    
# Line 189  namespace LinuxSampler { Line 239  namespace LinuxSampler {
239          }          }
240      }      }
241    
242        std::map<uint, AudioOutputDevice*> AudioOutputDeviceFactory::Devices() {
243            return mAudioOutputDevices;
244        }
245    
246        /**
247         * Destroys the given device. Usually this method shouldn't be called
248         * directly, Sampler::DestroyAudioOutputDevice should be called instead,
249         * since it also takes care whether some sampler channel is still using
250         * the device, etc.
251         */
252        void AudioOutputDeviceFactory::Destroy(AudioOutputDevice* pDevice) throw (Exception) {
253            if (pDevice && !pDevice->isAutonomousDevice())
254                throw Exception("You cannot directly destroy this '" + pDevice->Driver() + "' device!");
255    
256            DestroyPrivate(pDevice);
257        }
258    
259        void AudioOutputDeviceFactory::DestroyPrivate(AudioOutputDevice* pDevice) throw (Exception) {
260            AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
261            for (; iter != mAudioOutputDevices.end(); iter++) {
262                if (iter->second == pDevice) {
263                    // disable device
264                    pDevice->Stop();
265                    // remove device from the device list
266                    mAudioOutputDevices.erase(iter);
267                    // destroy and free device from memory
268                    delete pDevice;
269    
270                    break;
271                }
272            }
273        }
274    
275  } // namespace LinuxSampler  } // namespace LinuxSampler

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

  ViewVC Help
Powered by ViewVC