/[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 221 by schoenebeck, Fri Aug 20 17:25:19 2004 UTC revision 900 by schoenebeck, Wed Jul 5 17:53:22 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, 2006 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        = 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              MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);              Engine* engine = pEngineChannel->GetEngine();
52              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
53              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);  
54              delete pEngine;              MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
55                if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
56                if (pEngineChannel) {
57                    if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
58                    EngineChannelFactory::Destroy(pEngineChannel);
59    
60                    // reconnect engine if it still exists
61                    const std::set<Engine*>& engines = EngineFactory::EngineInstances();
62                    if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
63                }
64          }          }
65      }      }
66    
67      void SamplerChannel::LoadEngine(Engine::type_t EngineType) {      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
68          dmsg(2,("SamplerChannel: Loading engine..."));          dmsg(2,("SamplerChannel: Assigning engine type..."));
69    
70          // create new engine          // create new engine channel
71          Engine* pNewEngine = NULL;          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
72          switch (EngineType) {          if (!pNewEngineChannel) throw Exception("Unknown engine type");
73              case Engine::type_gig:  
74                  pNewEngine = new gig::Engine;          //FIXME: hack to allow fast retrieval of engine channel's sampler channel index
75                  break;          pNewEngineChannel->iSamplerChannelIndex = Index();
             default:  
                 throw LinuxSamplerException("Unknown engine type");  
         }  
76    
77          // dereference midi input port.          // dereference midi input port.
78          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
79          // disconnect old engine          // disconnect old engine channel
80          if (pEngine) {          if (pEngineChannel) {
81              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              Engine* engine = pEngineChannel->GetEngine();
82              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
83              delete pEngine;  
84          }              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
85                if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
86                EngineChannelFactory::Destroy(pEngineChannel);
87    
88                // reconnect engine if it still exists
89                const std::set<Engine*>& engines = EngineFactory::EngineInstances();
90                if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
91            }
92    
93            // connect new engine channel
94            if (pAudioOutputDevice) {
95                pNewEngineChannel->Connect(pAudioOutputDevice);
96                pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
97            }
98            if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
99            pEngineChannel = pNewEngineChannel;
100    
101            // from now on get MIDI device and port from EngineChannel object
102            this->pMidiInputDevice = NULL;
103            this->iMidiPort        = 0;
104    
105            pEngineChannel->StatusChanged(true);
106    
         // connect new engine  
         pEngine = pNewEngine;  
         if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel);  
         if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);  
107          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
108      }      }
109    
110      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
111          // disconnect old device          // disconnect old device
112          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngineChannel) {
113                Engine* engine = pEngineChannel->GetEngine();
114                pAudioOutputDevice->Disconnect(engine);
115    
116                pEngineChannel->DisconnectAudioOutputDevice();
117    
118                // reconnect engine if it still exists
119                const std::set<Engine*>& engines = EngineFactory::EngineInstances();
120                if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
121            }
122    
123          // connect new device          // connect new device
124          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
125          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
126                pEngineChannel->Connect(pAudioOutputDevice);
127                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
128            }
129      }      }
130    
131      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
132         SetMidiInput(pDevice, this->midiPort, this->midiChannel);         SetMidiInput(pDevice, 0, GetMidiInputChannel());
133      }      }
134    
135      void SamplerChannel::SetMidiInputPort(int MidiPort) {      void SamplerChannel::SetMidiInputPort(int MidiPort) {
136         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
137      }      }
138    
139      void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
140         SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
141      }      }
142    
143      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
144          // dereference old midi input port.          if (!pDevice) throw Exception("No MIDI input device assigned.");
145          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);  
146            // get old and new midi input port
147            MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
148            MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
149    
150          // disconnect old device port          // disconnect old device port
151          if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);          if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);
152          // new device, port and channel          // remember new device, port and channel if not engine channel yet created
153          pMidiInputDevice = pDevice;          if (!pEngineChannel) {
154          this->midiPort = iMidiPort;              this->pMidiInputDevice = pDevice;
155          this->midiChannel = MidiChannel;              this->iMidiPort        = iMidiPort;
156                this->midiChannel      = MidiChannel;
157            }
158    
159          // connect new device port          // connect new device port
160          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
         if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);  
161          // Ooops.          // Ooops.
162          if (pMidiInputPort == NULL)          if (pNewMidiInputPort == NULL)
163              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");              throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
164      }      }
165    
166      Engine* SamplerChannel::GetEngine() {      EngineChannel* SamplerChannel::GetEngineChannel() {
167          return pEngine;          return pEngineChannel;
168      }      }
169    
170      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {      midi_chan_t SamplerChannel::GetMidiInputChannel() {
171            if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
172          return this->midiChannel;          return this->midiChannel;
173      }      }
174    
175      int SamplerChannel::GetMidiInputPort() {      int SamplerChannel::GetMidiInputPort() {
176          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
177          return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);          if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
178            return iMidiPort;
179      }      }
180    
181      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
# Line 136  namespace LinuxSampler { Line 183  namespace LinuxSampler {
183      }      }
184    
185      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
186            if (pEngineChannel)
187                pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
188          return pMidiInputDevice;          return pMidiInputDevice;
189      }      }
190    
# Line 150  namespace LinuxSampler { Line 199  namespace LinuxSampler {
199              }              }
200          }          }
201    
202          throw LinuxSamplerException("Internal error: SamplerChannel index not found");          throw Exception("Internal error: SamplerChannel index not found");
203      }      }
204    
205      MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) {      MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
206          MidiInputPort* pMidiInputPort = NULL;          MidiInputPort* pMidiInputPort = NULL;
207            MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
208          if (pMidiInputDevice)          if (pMidiInputDevice)
209              pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);              pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
210          return pMidiInputPort;          return pMidiInputPort;
# Line 181  namespace LinuxSampler { Line 231  namespace LinuxSampler {
231          if (!mSamplerChannels.size()) {          if (!mSamplerChannels.size()) {
232              SamplerChannel* pChannel = new SamplerChannel(this);              SamplerChannel* pChannel = new SamplerChannel(this);
233              mSamplerChannels[0] = pChannel;              mSamplerChannels[0] = pChannel;
234                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));
235              return pChannel;              return pChannel;
236          }          }
237    
# Line 195  namespace LinuxSampler { Line 246  namespace LinuxSampler {
246                  // we found an unused index, so insert the new channel there                  // we found an unused index, so insert the new channel there
247                  SamplerChannel* pChannel = new SamplerChannel(this);                  SamplerChannel* pChannel = new SamplerChannel(this);
248                  mSamplerChannels[i] = pChannel;                  mSamplerChannels[i] = pChannel;
249                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));
250                  return pChannel;                  return pChannel;
251              }              }
252              throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");              throw Exception("Internal error: could not find unoccupied sampler channel index.");
253          }          }
254    
255          // 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
256          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
257          mSamplerChannels[lastIndex + 1] = pChannel;          mSamplerChannels[lastIndex + 1] = pChannel;
258            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));
259          return pChannel;          return pChannel;
260      }      }
261    
# Line 220  namespace LinuxSampler { Line 273  namespace LinuxSampler {
273              if (iterChan->second == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
274                  mSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
275                  delete pSamplerChannel;                  delete pSamplerChannel;
276                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));
277                  return;                  return;
278              }              }
279          }          }
# Line 235  namespace LinuxSampler { Line 289  namespace LinuxSampler {
289          return AudioOutputDeviceFactory::AvailableDrivers();          return AudioOutputDeviceFactory::AvailableDrivers();
290      }      }
291    
292      AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {      std::vector<String> Sampler::AvailableMidiInputDrivers() {
293            return MidiInputDeviceFactory::AvailableDrivers();
294        }
295    
296        std::vector<String> Sampler::AvailableEngineTypes() {
297            return EngineFactory::AvailableEngineTypes();
298        }
299    
300        AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
301          // create new device          // create new device
302          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
303    
# Line 266  namespace LinuxSampler { Line 328  namespace LinuxSampler {
328          return mMidiInputDevices;          return mMidiInputDevices;
329      }      }
330    
331      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
332          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
333          for (; iter != mAudioOutputDevices.end(); iter++) {          for (; iter != mAudioOutputDevices.end(); iter++) {
334              if (iter->second == pDevice) {              if (iter->second == pDevice) {
335                  // check if there are still sampler engines connected to this device                  // check if there are still sampler engines connected to this device
336                  for (uint i = 0; i < SamplerChannels(); i++)                  for (uint i = 0; i < SamplerChannels(); i++)
337                      if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device.");                      if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
338    
339                  // disable device                  // disable device
340                  pDevice->Stop();                  pDevice->Stop();
# Line 282  namespace LinuxSampler { Line 344  namespace LinuxSampler {
344    
345                  // destroy and free device from memory                  // destroy and free device from memory
346                  delete pDevice;                  delete pDevice;
347    
348                    break;
349              }              }
350          }          }
351      }      }
352    
353      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
354          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
355          for (; iter != mMidiInputDevices.end(); iter++) {          for (; iter != mMidiInputDevices.end(); iter++) {
356              if (iter->second == pDevice) {              if (iter->second == pDevice) {
357                  // check if there are still sampler engines connected to this device                  // check if there are still sampler engines connected to this device
358                  for (uint i = 0; i < SamplerChannels(); i++)                  for (uint i = 0; i < SamplerChannels(); i++)
359                      if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("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.");
360    
361                  // disable device                  // disable device
362                  pDevice->StopListen();                  pDevice->StopListen();
# Line 302  namespace LinuxSampler { Line 366  namespace LinuxSampler {
366    
367                  // destroy and free device from memory                  // destroy and free device from memory
368                  delete pDevice;                  delete pDevice;
369    
370                    break;
371              }              }
372          }          }
373      }      }
374    
375      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
376          // create new device          // create new device
377          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
378    
379          // add new device to the midi device list          // add new device to the midi device list
380          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 321  namespace LinuxSampler { Line 387  namespace LinuxSampler {
387          return pDevice;          return pDevice;
388      }      }
389    
390        int Sampler::GetVoiceCount() {
391            int count = 0;
392            std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
393    
394            for(; it != EngineFactory::EngineInstances().end(); it++) {
395                count += (*it)->VoiceCount();
396            }
397    
398            return count;
399        }
400    
401      void Sampler::Reset() {      void Sampler::Reset() {
402          // delete sampler channels          // delete sampler channels
403          try {          try {
404              SamplerChannelMap::iterator iter = mSamplerChannels.begin();              while (true) {
405              for (; iter != mSamplerChannels.end(); iter++) {                      SamplerChannelMap::iterator iter = mSamplerChannels.begin();
406                  RemoveSamplerChannel(iter->second);                      if (iter == mSamplerChannels.end()) break;
407                        RemoveSamplerChannel(iter->second);
408              }              }
409          }          }
410          catch(...) {          catch(...) {
# Line 336  namespace LinuxSampler { Line 414  namespace LinuxSampler {
414    
415          // delete midi input devices          // delete midi input devices
416          try {          try {
417              MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();              while (true) {
418              for (; iter != mMidiInputDevices.end(); iter++) {                      MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
419                  DestroyMidiInputDevice(iter->second);                      if (iter == mMidiInputDevices.end()) break;
420                        DestroyMidiInputDevice(iter->second);
421              }              }
422          }          }
423          catch(...) {          catch(...) {
# Line 348  namespace LinuxSampler { Line 427  namespace LinuxSampler {
427    
428          // delete audio output devices          // delete audio output devices
429          try {          try {
430              AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();              while (true) {
431              for (; iter != mAudioOutputDevices.end(); iter++) {                      AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
432                  DestroyAudioOutputDevice(iter->second);                      if (iter == mAudioOutputDevices.end()) break;
433                        DestroyAudioOutputDevice(iter->second);
434              }              }
435          }          }
436          catch(...) {          catch(...) {

Legend:
Removed from v.221  
changed lines
  Added in v.900

  ViewVC Help
Powered by ViewVC