/[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 175 by senkov, Tue Jul 6 04:41:23 2004 UTC revision 329 by senkov, Tue Dec 28 09:43:04 2004 UTC
# Line 24  Line 24 
24    
25  #include "Sampler.h"  #include "Sampler.h"
26    
27  #include "audiodriver/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
28  #include "mididriver/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
29  #include "engines/gig/Engine.h"  #include "engines/gig/Engine.h"
30    
31  namespace LinuxSampler {  namespace LinuxSampler {
# Line 39  namespace LinuxSampler { Line 39  namespace LinuxSampler {
39          pMidiInputDevice   = NULL;          pMidiInputDevice   = NULL;
40          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
41          midiPort           = 0;          midiPort           = 0;
42          midiChannel        = MidiInputDevice::MidiInputPort::midi_chan_all;          midiChannel        = MidiInputPort::midi_chan_all;
43          iIndex             = -1;          iIndex             = -1;
44      }      }
45    
46      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
47          if (pEngine) {          if (pEngine) {
48              MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);              MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
49              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
50              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
51              delete pEngine;              delete pEngine;
# Line 66  namespace LinuxSampler { Line 66  namespace LinuxSampler {
66          }          }
67    
68          // dereference midi input port.          // dereference midi input port.
69          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
70          // disconnect old engine          // disconnect old engine
71          if (pEngine) {          if (pEngine) {
72              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
# Line 92  namespace LinuxSampler { Line 92  namespace LinuxSampler {
92      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
93         SetMidiInput(pDevice, this->midiPort, this->midiChannel);         SetMidiInput(pDevice, this->midiPort, this->midiChannel);
94      }      }
95        
96      void SamplerChannel::SetMidiInputPort(int MidiPort) {      void SamplerChannel::SetMidiInputPort(int MidiPort) {
97         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);
98      }      }
99        
100      void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) {
101         SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);         SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);
102      }      }
103        
104      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) {
105          // dereference old midi input port.          // dereference old midi input port.
106          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
107          // disconnect old device port          // disconnect old device port
108          if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);          if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);
109          // new device, port and channel          // new device, port and channel
110          pMidiInputDevice = pDevice;          pMidiInputDevice = pDevice;
111          this->midiPort = MidiPort;          this->midiPort = iMidiPort;
112          this->midiChannel = MidiChannel;          this->midiChannel = MidiChannel;
113          // connect new device port          // connect new device port
114          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
115          if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);          if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);
116          // Ooops.          // Ooops.
117          if (pMidiInputPort == NULL)          if (pMidiInputPort == NULL)
118              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + ".");              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
119      }      }
120    
121      Engine* SamplerChannel::GetEngine() {      Engine* SamplerChannel::GetEngine() {
122          return pEngine;          return pEngine;
123      }      }
124    
125      MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {
126          return this->midiChannel;          return this->midiChannel;
127      }      }
128    
129      int SamplerChannel::GetMidiInputPort() {      int SamplerChannel::GetMidiInputPort() {
130          MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
131          return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);          return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);
132      }      }
133    
# Line 142  namespace LinuxSampler { Line 142  namespace LinuxSampler {
142      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
143          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
144    
145          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
146          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
147              if (*iter == this) {              if (iter->second == this) {
148                  iIndex = i;                  iIndex = iter->first;
149                  return i;                  return iIndex;
150              }              }
151          }          }
152    
153          throw LinuxSamplerException("SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
154      }      }
155    
156      MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) {      MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) {
157          MidiInputDevice::MidiInputPort *pMidiInputPort = NULL;          MidiInputPort* pMidiInputPort = NULL;
158          if (pMidiInputDevice)          if (pMidiInputDevice)
159              pMidiInputPort = pMidiInputDevice->GetPort(MidiPort);              pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
160          return pMidiInputPort;          return pMidiInputPort;
161      }      }
162    
163    
164    
165      // ******************************************************************      // ******************************************************************
166      // * Sampler      // * Sampler
167    
# Line 167  namespace LinuxSampler { Line 169  namespace LinuxSampler {
169      }      }
170    
171      Sampler::~Sampler() {      Sampler::~Sampler() {
172          // delete sampler channels          Reset();
         {  
             std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin();  
             for (; iter != vSamplerChannels.end(); iter++) delete *iter;  
         }  
   
         // 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;  
             }  
         }  
173      }      }
174    
175      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
176          return vSamplerChannels.size();          return mSamplerChannels.size();
177      }      }
178    
179      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
180            // if there's no sampler channel yet
181            if (!mSamplerChannels.size()) {
182                SamplerChannel* pChannel = new SamplerChannel(this);
183                mSamplerChannels[0] = pChannel;
184                return pChannel;
185            }
186    
187            // get the highest used sampler channel index
188            uint lastIndex = (--(mSamplerChannels.end()))->first;
189    
190            // check if we reached the index limit
191            if (lastIndex + 1 < lastIndex) {
192                // search for an unoccupied sampler channel index starting from 0
193                for (uint i = 0; i < lastIndex; i++) {
194                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
195                    // we found an unused index, so insert the new channel there
196                    SamplerChannel* pChannel = new SamplerChannel(this);
197                    mSamplerChannels[i] = pChannel;
198                    return pChannel;
199                }
200                throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
201            }
202    
203            // we have not reached the index limit so we just add the channel past the highest index
204          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
205          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
206          return pChannel;          return pChannel;
207      }      }
208    
209      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
210          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
211          return vSamplerChannels[uiSamplerChannel];      }
212    
213        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
214            return mSamplerChannels;
215      }      }
216    
217      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
218          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
219          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
220              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
221                  vSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
222                  delete pSamplerChannel;                  delete pSamplerChannel;
223                  return;                  return;
224              }              }
# Line 316  namespace LinuxSampler { Line 321  namespace LinuxSampler {
321          return pDevice;          return pDevice;
322      }      }
323    
324        void Sampler::Reset() {
325            // delete sampler channels
326            try {
327                while (true) {
328                        SamplerChannelMap::iterator iter = mSamplerChannels.begin();
329                        if (iter == mSamplerChannels.end()) break;
330                        RemoveSamplerChannel(iter->second);
331                }
332            }
333            catch(...) {
334                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
335                exit(EXIT_FAILURE);
336            }
337    
338            // delete midi input devices
339            try {
340                while (true) {
341                        MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
342                        if (iter == mMidiInputDevices.end()) break;
343                        DestroyMidiInputDevice(iter->second);
344                }
345            }
346            catch(...) {
347                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
348                exit(EXIT_FAILURE);
349            }
350    
351            // delete audio output devices
352            try {
353                while (true) {
354                        AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
355                        if (iter == mAudioOutputDevices.end()) break;
356                        DestroyAudioOutputDevice(iter->second);
357                }
358            }
359            catch(...) {
360                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
361                exit(EXIT_FAILURE);
362            }
363        }
364    
365  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.175  
changed lines
  Added in v.329

  ViewVC Help
Powered by ViewVC