/[svn]/linuxsampler/trunk/src/Sampler.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/Sampler.cpp

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

revision 960 by iliev, Thu Nov 30 10:39:12 2006 UTC revision 1761 by iliev, Fri Aug 29 15:42:06 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, 2006 Christian Schoenebeck                        *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This library 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  *
10   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 2 of the License, or     *
11   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
12   *                                                                         *   *                                                                         *
13   *   This program is distributed in the hope that it will be useful,       *   *   This library is distributed in the hope that it will be useful,       *
14   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16   *   GNU General Public License for more details.                          *   *   GNU General Public License for more details.                          *
17   *                                                                         *   *                                                                         *
18   *   You should have received a copy of the GNU General Public License     *   *   You should have received a copy of the GNU General Public License     *
19   *   along with this program; if not, write to the Free Software           *   *   along with this library; if not, write to the Free Software           *
20   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
# Line 25  Line 25 
25    
26  #include "Sampler.h"  #include "Sampler.h"
27    
28    #include "common/global_private.h"
29  #include "engines/EngineFactory.h"  #include "engines/EngineFactory.h"
30  #include "engines/EngineChannelFactory.h"  #include "engines/EngineChannelFactory.h"
31    #include "plugins/InstrumentEditorFactory.h"
32  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
33  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
34  #include "network/lscpserver.h"  #include "drivers/midi/MidiInstrumentMapper.h"
35    #include "common/Features.h"
36    
37  namespace LinuxSampler {  namespace LinuxSampler {
38    
# Line 66  namespace LinuxSampler { Line 69  namespace LinuxSampler {
69    
70      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
71          dmsg(2,("SamplerChannel: Assigning engine type..."));          dmsg(2,("SamplerChannel: Assigning engine type..."));
72            
73            if (pEngineChannel) {
74                if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {
75                    dmsg(2,("OK\n"));
76                    return;
77                }
78            }
79    
80            fireEngineToBeChanged();
81    
82          // create new engine channel          // create new engine channel
83          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
84          if (!pNewEngineChannel) throw Exception("Unknown engine type");          if (!pNewEngineChannel) throw Exception("Unknown engine type");
85    
86          //FIXME: hack to allow fast retrieval of engine channel's sampler channel index          pNewEngineChannel->SetSamplerChannel(this);
         pNewEngineChannel->iSamplerChannelIndex = Index();  
87    
88          // dereference midi input port.          // dereference midi input port.
89          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
# Line 103  namespace LinuxSampler { Line 114  namespace LinuxSampler {
114          this->iMidiPort        = 0;          this->iMidiPort        = 0;
115    
116          pEngineChannel->StatusChanged(true);          pEngineChannel->StatusChanged(true);
117            fireEngineChanged();
118          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
119      }      }
120    
# Line 204  namespace LinuxSampler { Line 215  namespace LinuxSampler {
215          throw Exception("Internal error: SamplerChannel index not found");          throw Exception("Internal error: SamplerChannel index not found");
216      }      }
217    
218        Sampler* SamplerChannel::GetSampler() {
219            return pSampler;
220        }
221    
222        void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) {
223            llEngineChangeListeners.AddListener(l);
224        }
225    
226        void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) {
227           llEngineChangeListeners.RemoveListener(l);
228        }
229    
230        void SamplerChannel::RemoveAllEngineChangeListeners() {
231           llEngineChangeListeners.RemoveAllListeners();
232        }
233    
234        void SamplerChannel::fireEngineToBeChanged() {
235            for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
236                llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index());
237            }
238        }
239    
240        void SamplerChannel::fireEngineChanged() {
241            for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
242                llEngineChangeListeners.GetListener(i)->EngineChanged(Index());
243            }
244        }
245    
246      MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {      MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
247          MidiInputPort* pMidiInputPort = NULL;          MidiInputPort* pMidiInputPort = NULL;
248          MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();          MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
# Line 218  namespace LinuxSampler { Line 257  namespace LinuxSampler {
257      // * Sampler      // * Sampler
258    
259      Sampler::Sampler() {      Sampler::Sampler() {
260            eventHandler.SetSampler(this);
261      }      }
262    
263      Sampler::~Sampler() {      Sampler::~Sampler() {
# Line 228  namespace LinuxSampler { Line 268  namespace LinuxSampler {
268          return mSamplerChannels.size();          return mSamplerChannels.size();
269      }      }
270    
271        void Sampler::AddChannelCountListener(ChannelCountListener* l) {
272            llChannelCountListeners.AddListener(l);
273        }
274    
275        void Sampler::RemoveChannelCountListener(ChannelCountListener* l) {
276           llChannelCountListeners.RemoveListener(l);
277        }
278    
279        void Sampler::fireChannelCountChanged(int NewCount) {
280            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
281                llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount);
282            }
283        }
284    
285        void Sampler::fireChannelAdded(SamplerChannel* pChannel) {
286            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
287                llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel);
288            }
289        }
290    
291        void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) {
292            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
293                llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel);
294            }
295        }
296    
297        void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {
298            llAudioDeviceCountListeners.AddListener(l);
299        }
300    
301        void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) {
302            llAudioDeviceCountListeners.RemoveListener(l);
303        }
304    
305        void Sampler::fireAudioDeviceCountChanged(int NewCount) {
306            for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) {
307                llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount);
308            }
309        }
310    
311        void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) {
312            llMidiDeviceCountListeners.AddListener(l);
313        }
314    
315        void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) {
316            llMidiDeviceCountListeners.RemoveListener(l);
317        }
318    
319        void Sampler::fireMidiDeviceCountChanged(int NewCount) {
320            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
321                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount);
322            }
323        }
324    
325        void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
326            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
327                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice);
328            }
329        }
330    
331        void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) {
332            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
333                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice);
334            }
335        }
336    
337        void Sampler::AddVoiceCountListener(VoiceCountListener* l) {
338            llVoiceCountListeners.AddListener(l);
339        }
340    
341        void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) {
342            llVoiceCountListeners.RemoveListener(l);
343        }
344    
345        void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) {
346            for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) {
347                llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount);
348            }
349        }
350    
351        void Sampler::AddStreamCountListener(StreamCountListener* l) {
352            llStreamCountListeners.AddListener(l);
353        }
354    
355        void Sampler::RemoveStreamCountListener(StreamCountListener* l) {
356            llStreamCountListeners.RemoveListener(l);
357        }
358    
359        void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) {
360            for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) {
361                llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount);
362            }
363        }
364    
365        void Sampler::AddBufferFillListener(BufferFillListener* l) {
366            llBufferFillListeners.AddListener(l);
367        }
368    
369        void Sampler::RemoveBufferFillListener(BufferFillListener* l) {
370            llBufferFillListeners.RemoveListener(l);
371        }
372    
373        void Sampler::fireBufferFillChanged(int ChannelId, String FillData) {
374            for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) {
375                llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData);
376            }
377        }
378    
379        void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) {
380            llTotalStreamCountListeners.AddListener(l);
381        }
382    
383        void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) {
384            llTotalStreamCountListeners.RemoveListener(l);
385        }
386    
387        void Sampler::fireTotalStreamCountChanged(int NewCount) {
388            for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) {
389                llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount);
390            }
391        }
392    
393        void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) {
394            llTotalVoiceCountListeners.AddListener(l);
395        }
396    
397        void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) {
398            llTotalVoiceCountListeners.RemoveListener(l);
399        }
400    
401        void Sampler::fireTotalVoiceCountChanged(int NewCount) {
402            for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) {
403                llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount);
404            }
405        }
406    
407        void Sampler::AddFxSendCountListener(FxSendCountListener* l) {
408            llFxSendCountListeners.AddListener(l);
409        }
410    
411        void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) {
412            llFxSendCountListeners.RemoveListener(l);
413        }
414    
415        void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) {
416            for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) {
417                llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount);
418            }
419        }
420    
421        void Sampler::EventHandler::EngineToBeChanged(int ChannelId) {
422            // nothing to do here
423        }
424    
425        void Sampler::EventHandler::EngineChanged(int ChannelId) {
426            EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel();
427            if(engineChannel == NULL) return;
428            engineChannel->AddFxSendCountListener(this);
429        }
430    
431        void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
432            pSampler->fireFxSendCountChanged(ChannelId, NewCount);
433        }
434    
435    
436      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
437          // if there's no sampler channel yet          // if there's no sampler channel yet
438          if (!mSamplerChannels.size()) {          if (!mSamplerChannels.size()) {
439              SamplerChannel* pChannel = new SamplerChannel(this);              SamplerChannel* pChannel = new SamplerChannel(this);
440              mSamplerChannels[0] = pChannel;              mSamplerChannels[0] = pChannel;
441              LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));              fireChannelAdded(pChannel);
442                fireChannelCountChanged(1);
443                pChannel->AddEngineChangeListener(&eventHandler);
444              return pChannel;              return pChannel;
445          }          }
446    
# Line 248  namespace LinuxSampler { Line 455  namespace LinuxSampler {
455                  // we found an unused index, so insert the new channel there                  // we found an unused index, so insert the new channel there
456                  SamplerChannel* pChannel = new SamplerChannel(this);                  SamplerChannel* pChannel = new SamplerChannel(this);
457                  mSamplerChannels[i] = pChannel;                  mSamplerChannels[i] = pChannel;
458                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));                  fireChannelAdded(pChannel);
459                    fireChannelCountChanged(SamplerChannels());
460                    pChannel->AddEngineChangeListener(&eventHandler);
461                  return pChannel;                  return pChannel;
462              }              }
463              throw Exception("Internal error: could not find unoccupied sampler channel index.");              throw Exception("Internal error: could not find unoccupied sampler channel index.");
# Line 257  namespace LinuxSampler { Line 466  namespace LinuxSampler {
466          // we have not reached the index limit so we just add the channel past the highest index          // we have not reached the index limit so we just add the channel past the highest index
467          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
468          mSamplerChannels[lastIndex + 1] = pChannel;          mSamplerChannels[lastIndex + 1] = pChannel;
469          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));          fireChannelAdded(pChannel);
470            fireChannelCountChanged(SamplerChannels());
471            pChannel->AddEngineChangeListener(&eventHandler);
472          return pChannel;          return pChannel;
473      }      }
474    
# Line 273  namespace LinuxSampler { Line 484  namespace LinuxSampler {
484          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
485          for (; iterChan != mSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
486              if (iterChan->second == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
487                    fireChannelToBeRemoved(pSamplerChannel);
488                    pSamplerChannel->RemoveAllEngineChangeListeners();
489                  mSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
490                  delete pSamplerChannel;                  delete pSamplerChannel;
491                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));                  fireChannelCountChanged(SamplerChannels());
492                  return;                  return;
493              }              }
494          }          }
# Line 311  namespace LinuxSampler { Line 524  namespace LinuxSampler {
524              }              }
525          }          }
526    
527            fireAudioDeviceCountChanged(AudioOutputDevices());
528          return pDevice;          return pDevice;
529      }      }
530    
# Line 347  namespace LinuxSampler { Line 561  namespace LinuxSampler {
561                  // destroy and free device from memory                  // destroy and free device from memory
562                  delete pDevice;                  delete pDevice;
563    
564                    fireAudioDeviceCountChanged(AudioOutputDevices());
565                  break;                  break;
566              }              }
567          }          }
# Line 360  namespace LinuxSampler { Line 575  namespace LinuxSampler {
575                  for (uint i = 0; i < SamplerChannels(); i++)                  for (uint i = 0; i < SamplerChannels(); i++)
576                      if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");                      if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
577    
578                    fireMidiDeviceToBeDestroyed(pDevice);
579    
580                  // disable device                  // disable device
581                  pDevice->StopListen();                  pDevice->StopListen();
582    
# Line 369  namespace LinuxSampler { Line 586  namespace LinuxSampler {
586                  // destroy and free device from memory                  // destroy and free device from memory
587                  delete pDevice;                  delete pDevice;
588    
589                    fireMidiDeviceCountChanged(MidiInputDevices());
590                  break;                  break;
591              }              }
592          }          }
# Line 386  namespace LinuxSampler { Line 604  namespace LinuxSampler {
604                  }                  }
605          }          }
606    
607            fireMidiDeviceCreated(pDevice);
608            fireMidiDeviceCountChanged(MidiInputDevices());
609          return pDevice;          return pDevice;
610      }      }
611    
612        int Sampler::GetDiskStreamCount() {
613            int count = 0;
614            std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
615    
616            for(; it != EngineFactory::EngineInstances().end(); it++) {
617                count += (*it)->DiskStreamCount();
618            }
619    
620            return count;
621        }
622    
623      int Sampler::GetVoiceCount() {      int Sampler::GetVoiceCount() {
624          int count = 0;          int count = 0;
625          std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();          std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
# Line 439  namespace LinuxSampler { Line 670  namespace LinuxSampler {
670              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
671              exit(EXIT_FAILURE);              exit(EXIT_FAILURE);
672          }          }
673    
674            // delete MIDI instrument maps
675            try {
676                MidiInstrumentMapper::RemoveAllMaps();
677            }
678            catch(...) {
679                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush;
680                exit(EXIT_FAILURE);
681            }
682    
683            // unload all instrument editor DLLs
684            InstrumentEditorFactory::ClosePlugins();
685        }
686    
687        bool Sampler::EnableDenormalsAreZeroMode() {
688            Features::detect();
689            return Features::enableDenormalsAreZeroMode();
690      }      }
691    
692  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.960  
changed lines
  Added in v.1761

  ViewVC Help
Powered by ViewVC