/[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 412 by schoenebeck, Sat Feb 26 22:44:51 2005 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/EngineChannelFactory.h"
29  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
30  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
31  #include "engines/gig/Engine.h"  #include "network/lscpserver.h"
32    
33  namespace LinuxSampler {  namespace LinuxSampler {
34    
# Line 35  namespace LinuxSampler { Line 37  namespace LinuxSampler {
37    
38      SamplerChannel::SamplerChannel(Sampler* pS) {      SamplerChannel::SamplerChannel(Sampler* pS) {
39          pSampler           = pS;          pSampler           = pS;
40          pEngine            = NULL;          pEngineChannel     = NULL;
41          pMidiInputDevice   = NULL;          pMidiInputDevice   = NULL;
42          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
43          midiPort           = 0;          midiPort           = 0;
44          midiChannel        = MidiInputDevice::MidiInputPort::midi_chan_all;          midiChannel        = MidiInputPort::midi_chan_all;
45          iIndex             = -1;          iIndex             = -1;
46      }      }
47    
48      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
49          if (pEngine) {          if (pEngineChannel) {
50              MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);              MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
51              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
52              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
53              delete pEngine;              delete pEngineChannel;
54          }          }
55      }      }
56    
57      void SamplerChannel::LoadEngine(Engine::type_t EngineType) {      void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) {
58          dmsg(2,("SamplerChannel: Loading engine..."));          dmsg(2,("SamplerChannel: Assigning engine type..."));
59    
60          // create new engine          // create new engine channel
61          Engine* pNewEngine = NULL;          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
62          switch (EngineType) {          if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type");
             case Engine::type_gig:  
                 pNewEngine = new gig::Engine;  
                 break;  
             default:  
                 throw LinuxSamplerException("Unknown engine type");  
         }  
63    
64          // dereference midi input port.          // dereference midi input port.
65          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
66          // disconnect old engine          // disconnect old engine
67          if (pEngine) {          if (pEngineChannel) {
68              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
69              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
70              delete pEngine;              delete pEngineChannel;
71          }          }
72    
73          // connect new engine          // connect new engine channel
74          pEngine = pNewEngine;          pEngineChannel = pNewEngineChannel;
75          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel);          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel);
76          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);          if (pAudioOutputDevice) {
77                pNewEngineChannel->Connect(pAudioOutputDevice);
78                pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
79            }
80          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
81      }      }
82    
83      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
84          // disconnect old device          // disconnect old device
85          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice();
86          // connect new device          // connect new device
87          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
88          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
89                pEngineChannel->Connect(pAudioOutputDevice);
90                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
91            }
92      }      }
93    
94      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
# Line 97  namespace LinuxSampler { Line 99  namespace LinuxSampler {
99         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);
100      }      }
101    
102      void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) {
103         SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);         SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);
104      }      }
105    
106      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) {
107          // dereference old midi input port.          // dereference old midi input port.
108          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
109          // disconnect old device port          // disconnect old device port
110          if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);          if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel);
111          // new device, port and channel          // new device, port and channel
112          pMidiInputDevice = pDevice;          pMidiInputDevice = pDevice;
113          this->midiPort = MidiPort;          this->midiPort = iMidiPort;
114          this->midiChannel = MidiChannel;          this->midiChannel = MidiChannel;
115          // connect new device port          // connect new device port
116          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
117          if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);          if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel);
118          // Ooops.          // Ooops.
119          if (pMidiInputPort == NULL)          if (pMidiInputPort == NULL)
120              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + ".");              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
121      }      }
122    
123      Engine* SamplerChannel::GetEngine() {      EngineChannel* SamplerChannel::GetEngineChannel() {
124          return pEngine;          return pEngineChannel;
125      }      }
126    
127      MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {
128          return this->midiChannel;          return this->midiChannel;
129      }      }
130    
131      int SamplerChannel::GetMidiInputPort() {      int SamplerChannel::GetMidiInputPort() {
132          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
133          return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);          return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);
134      }      }
135    
# Line 153  namespace LinuxSampler { Line 155  namespace LinuxSampler {
155          throw LinuxSamplerException("Internal error: SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
156      }      }
157    
158      MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) {      MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) {
159          MidiInputDevice::MidiInputPort *pMidiInputPort = NULL;          MidiInputPort* pMidiInputPort = NULL;
160          if (pMidiInputDevice)          if (pMidiInputDevice)
161              pMidiInputPort = pMidiInputDevice->GetPort(MidiPort);              pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
162          return pMidiInputPort;          return pMidiInputPort;
163      }      }
164    
165    
166    
167      // ******************************************************************      // ******************************************************************
168      // * Sampler      // * Sampler
169    
# Line 167  namespace LinuxSampler { Line 171  namespace LinuxSampler {
171      }      }
172    
173      Sampler::~Sampler() {      Sampler::~Sampler() {
174          // 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;  
             }  
         }  
175      }      }
176    
177      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
# Line 203  namespace LinuxSampler { Line 183  namespace LinuxSampler {
183          if (!mSamplerChannels.size()) {          if (!mSamplerChannels.size()) {
184              SamplerChannel* pChannel = new SamplerChannel(this);              SamplerChannel* pChannel = new SamplerChannel(this);
185              mSamplerChannels[0] = pChannel;              mSamplerChannels[0] = pChannel;
186                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, 1));
187              return pChannel;              return pChannel;
188          }          }
189    
# Line 217  namespace LinuxSampler { Line 198  namespace LinuxSampler {
198                  // we found an unused index, so insert the new channel there                  // we found an unused index, so insert the new channel there
199                  SamplerChannel* pChannel = new SamplerChannel(this);                  SamplerChannel* pChannel = new SamplerChannel(this);
200                  mSamplerChannels[i] = pChannel;                  mSamplerChannels[i] = pChannel;
201                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, i));
202                  return pChannel;                  return pChannel;
203              }              }
204              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 207  namespace LinuxSampler {
207          // 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
208          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
209          mSamplerChannels[lastIndex + 1] = pChannel;          mSamplerChannels[lastIndex + 1] = pChannel;
210            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, lastIndex + 1));
211          return pChannel;          return pChannel;
212      }      }
213    
# Line 242  namespace LinuxSampler { Line 225  namespace LinuxSampler {
225              if (iterChan->second == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
226                  mSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
227                  delete pSamplerChannel;                  delete pSamplerChannel;
228                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, mSamplerChannels.size()));
229                  return;                  return;
230              }              }
231          }          }
# Line 343  namespace LinuxSampler { Line 327  namespace LinuxSampler {
327          return pDevice;          return pDevice;
328      }      }
329    
330        void Sampler::Reset() {
331            // delete sampler channels
332            try {
333                while (true) {
334                        SamplerChannelMap::iterator iter = mSamplerChannels.begin();
335                        if (iter == mSamplerChannels.end()) break;
336                        RemoveSamplerChannel(iter->second);
337                }
338            }
339            catch(...) {
340                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
341                exit(EXIT_FAILURE);
342            }
343    
344            // delete midi input devices
345            try {
346                while (true) {
347                        MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
348                        if (iter == mMidiInputDevices.end()) break;
349                        DestroyMidiInputDevice(iter->second);
350                }
351            }
352            catch(...) {
353                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
354                exit(EXIT_FAILURE);
355            }
356    
357            // delete audio output devices
358            try {
359                while (true) {
360                        AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
361                        if (iter == mAudioOutputDevices.end()) break;
362                        DestroyAudioOutputDevice(iter->second);
363                }
364            }
365            catch(...) {
366                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
367                exit(EXIT_FAILURE);
368            }
369        }
370    
371  } // namespace LinuxSampler  } // namespace LinuxSampler

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

  ViewVC Help
Powered by ViewVC