/[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 209 by schoenebeck, Sun Jul 18 00:29:39 2004 UTC revision 837 by persson, Thu Feb 9 20:03:03 2006 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 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 24  Line 25 
25    
26  #include "Sampler.h"  #include "Sampler.h"
27    
28    #include "engines/EngineFactory.h"
29    #include "engines/EngineChannelFactory.h"
30  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
31  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
32  #include "engines/gig/Engine.h"  #include "network/lscpserver.h"
33    
34  namespace LinuxSampler {  namespace LinuxSampler {
35    
# Line 35  namespace LinuxSampler { Line 38  namespace LinuxSampler {
38    
39      SamplerChannel::SamplerChannel(Sampler* pS) {      SamplerChannel::SamplerChannel(Sampler* pS) {
40          pSampler           = pS;          pSampler           = pS;
41          pEngine            = NULL;          pEngineChannel     = NULL;
         pMidiInputDevice   = NULL;  
42          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
43          midiPort           = 0;          pMidiInputDevice   = NULL;
44          midiChannel        = MidiInputDevice::MidiInputPort::midi_chan_all;          iMidiPort          = 0;
45            midiChannel        = midi_chan_all;
46          iIndex             = -1;          iIndex             = -1;
47      }      }
48    
49      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
50          if (pEngine) {          if (pEngineChannel) {
51              MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);              MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
52              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
53              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pEngineChannel) {
54              delete pEngine;                  if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
55                    EngineChannelFactory::Destroy(pEngineChannel);
56                }
57          }          }
58      }      }
59    
60      void SamplerChannel::LoadEngine(Engine::type_t EngineType) {      void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) {
61          dmsg(2,("SamplerChannel: Loading engine..."));          dmsg(2,("SamplerChannel: Assigning engine type..."));
62    
63          // create new engine          // create new engine channel
64          Engine* pNewEngine = NULL;          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
65          switch (EngineType) {          if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type");
66              case Engine::type_gig:  
67                  pNewEngine = new gig::Engine;          //FIXME: hack to allow fast retrieval of engine channel's sampler channel index
68                  break;          pNewEngineChannel->iSamplerChannelIndex = Index();
             default:  
                 throw LinuxSamplerException("Unknown engine type");  
         }  
69    
70          // dereference midi input port.          // dereference midi input port.
71          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
72          // disconnect old engine          // disconnect old engine channel
73          if (pEngine) {          if (pEngineChannel) {
74              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
75              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
76              delete pEngine;              EngineChannelFactory::Destroy(pEngineChannel);
77          }          }
78    
79            // connect new engine channel
80            if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
81            if (pAudioOutputDevice) {
82                pNewEngineChannel->Connect(pAudioOutputDevice);
83                pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
84            }
85            pEngineChannel = pNewEngineChannel;
86    
87            // from now on get MIDI device and port from EngineChannel object
88            this->pMidiInputDevice = NULL;
89            this->iMidiPort        = 0;
90    
91            pEngineChannel->StatusChanged(true);
92    
         // connect new engine  
         pEngine = pNewEngine;  
         if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel);  
         if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);  
93          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
94      }      }
95    
96      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
97          // disconnect old device          // disconnect old device
98          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice();
99          // connect new device          // connect new device
100          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
101          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
102                pEngineChannel->Connect(pAudioOutputDevice);
103                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
104            }
105      }      }
106    
107      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
108         SetMidiInput(pDevice, this->midiPort, this->midiChannel);         SetMidiInput(pDevice, 0, GetMidiInputChannel());
109      }      }
110    
111      void SamplerChannel::SetMidiInputPort(int MidiPort) {      void SamplerChannel::SetMidiInputPort(int MidiPort) {
112         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
113      }      }
114    
115      void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
116         SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
117      }      }
118    
119      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
120          // dereference old midi input port.          if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned.");
121          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);  
122            // get old and new midi input port
123            MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
124            MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
125    
126          // disconnect old device port          // disconnect old device port
127          if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);          if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);
128          // new device, port and channel          // remember new device, port and channel if not engine channel yet created
129          pMidiInputDevice = pDevice;          if (!pEngineChannel) {
130          this->midiPort = MidiPort;              this->pMidiInputDevice = pDevice;
131          this->midiChannel = MidiChannel;              this->iMidiPort        = iMidiPort;
132                this->midiChannel      = MidiChannel;
133            }
134    
135          // connect new device port          // connect new device port
136          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
         if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);  
137          // Ooops.          // Ooops.
138          if (pMidiInputPort == NULL)          if (pNewMidiInputPort == NULL)
139              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + ".");              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
140      }      }
141    
142      Engine* SamplerChannel::GetEngine() {      EngineChannel* SamplerChannel::GetEngineChannel() {
143          return pEngine;          return pEngineChannel;
144      }      }
145    
146      MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {      midi_chan_t SamplerChannel::GetMidiInputChannel() {
147            if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
148          return this->midiChannel;          return this->midiChannel;
149      }      }
150    
151      int SamplerChannel::GetMidiInputPort() {      int SamplerChannel::GetMidiInputPort() {
152          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
153          return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);          if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
154            return iMidiPort;
155      }      }
156    
157      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
# Line 136  namespace LinuxSampler { Line 159  namespace LinuxSampler {
159      }      }
160    
161      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
162            if (pEngineChannel)
163                pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
164          return pMidiInputDevice;          return pMidiInputDevice;
165      }      }
166    
# Line 153  namespace LinuxSampler { Line 178  namespace LinuxSampler {
178          throw LinuxSamplerException("Internal error: SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
179      }      }
180    
181      MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) {      MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
182          MidiInputDevice::MidiInputPort *pMidiInputPort = NULL;          MidiInputPort* pMidiInputPort = NULL;
183            MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
184          if (pMidiInputDevice)          if (pMidiInputDevice)
185              pMidiInputPort = pMidiInputDevice->GetPort(MidiPort);              pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
186          return pMidiInputPort;          return pMidiInputPort;
187      }      }
188    
189    
190    
191      // ******************************************************************      // ******************************************************************
192      // * Sampler      // * Sampler
193    
# Line 167  namespace LinuxSampler { Line 195  namespace LinuxSampler {
195      }      }
196    
197      Sampler::~Sampler() {      Sampler::~Sampler() {
198          // delete sampler channels          Reset();
         {  
             SamplerChannelMap::iterator iter = mSamplerChannels.begin();  
             for (; iter != mSamplerChannels.end(); iter++) delete iter->second;  
         }  
   
         // delete midi input devices  
         {  
             MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();  
             for (; iter != mMidiInputDevices.end(); iter++) {  
                 MidiInputDevice* pDevice = iter->second;  
                 pDevice->StopListen();  
                 delete pDevice;  
             }  
         }  
   
         // delete audio output devices  
         {  
             AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();  
             for (; iter != mAudioOutputDevices.end(); iter++) {  
                 AudioOutputDevice* pDevice = iter->second;  
                 pDevice->Stop();  
                 delete pDevice;  
             }  
         }  
199      }      }
200    
201      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
# Line 203  namespace LinuxSampler { Line 207  namespace LinuxSampler {
207          if (!mSamplerChannels.size()) {          if (!mSamplerChannels.size()) {
208              SamplerChannel* pChannel = new SamplerChannel(this);              SamplerChannel* pChannel = new SamplerChannel(this);
209              mSamplerChannels[0] = pChannel;              mSamplerChannels[0] = pChannel;
210                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));
211              return pChannel;              return pChannel;
212          }          }
213    
# Line 217  namespace LinuxSampler { Line 222  namespace LinuxSampler {
222                  // we found an unused index, so insert the new channel there                  // we found an unused index, so insert the new channel there
223                  SamplerChannel* pChannel = new SamplerChannel(this);                  SamplerChannel* pChannel = new SamplerChannel(this);
224                  mSamplerChannels[i] = pChannel;                  mSamplerChannels[i] = pChannel;
225                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));
226                  return pChannel;                  return pChannel;
227              }              }
228              throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");              throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
# Line 225  namespace LinuxSampler { Line 231  namespace LinuxSampler {
231          // 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
232          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
233          mSamplerChannels[lastIndex + 1] = pChannel;          mSamplerChannels[lastIndex + 1] = pChannel;
234            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));
235          return pChannel;          return pChannel;
236      }      }
237    
# Line 242  namespace LinuxSampler { Line 249  namespace LinuxSampler {
249              if (iterChan->second == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
250                  mSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
251                  delete pSamplerChannel;                  delete pSamplerChannel;
252                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));
253                  return;                  return;
254              }              }
255          }          }
# Line 304  namespace LinuxSampler { Line 312  namespace LinuxSampler {
312    
313                  // destroy and free device from memory                  // destroy and free device from memory
314                  delete pDevice;                  delete pDevice;
315    
316                    break;
317              }              }
318          }          }
319      }      }
# Line 324  namespace LinuxSampler { Line 334  namespace LinuxSampler {
334    
335                  // destroy and free device from memory                  // destroy and free device from memory
336                  delete pDevice;                  delete pDevice;
337    
338                    break;
339              }              }
340          }          }
341      }      }
342    
343      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
344          // create new device          // create new device
345          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
346    
347          // add new device to the midi device list          // add new device to the midi device list
348          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
# Line 343  namespace LinuxSampler { Line 355  namespace LinuxSampler {
355          return pDevice;          return pDevice;
356      }      }
357    
358        int Sampler::GetVoiceCount() {
359            int count = 0;
360            std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
361    
362            for(; it != EngineFactory::EngineInstances().end(); it++) {
363                count += (*it)->VoiceCount();
364            }
365    
366            return count;
367        }
368    
369        void Sampler::Reset() {
370            // delete sampler channels
371            try {
372                while (true) {
373                        SamplerChannelMap::iterator iter = mSamplerChannels.begin();
374                        if (iter == mSamplerChannels.end()) break;
375                        RemoveSamplerChannel(iter->second);
376                }
377            }
378            catch(...) {
379                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
380                exit(EXIT_FAILURE);
381            }
382    
383            // delete midi input devices
384            try {
385                while (true) {
386                        MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
387                        if (iter == mMidiInputDevices.end()) break;
388                        DestroyMidiInputDevice(iter->second);
389                }
390            }
391            catch(...) {
392                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
393                exit(EXIT_FAILURE);
394            }
395    
396            // delete audio output devices
397            try {
398                while (true) {
399                        AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
400                        if (iter == mAudioOutputDevices.end()) break;
401                        DestroyAudioOutputDevice(iter->second);
402                }
403            }
404            catch(...) {
405                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
406                exit(EXIT_FAILURE);
407            }
408        }
409    
410  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.209  
changed lines
  Added in v.837

  ViewVC Help
Powered by ViewVC