/[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 123 by schoenebeck, Mon Jun 14 19:33:16 2004 UTC revision 411 by schoenebeck, Sat Feb 26 02:01:14 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 "audiodriver/AudioOutputDeviceFactory.h"  #include "engines/EngineChannelFactory.h"
29  #include "mididriver/MidiInputDeviceAlsa.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
30  #include "engines/gig/Engine.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
31    #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;
44            midiChannel        = MidiInputPort::midi_chan_all;
45          iIndex             = -1;          iIndex             = -1;
46      }      }
47    
48      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
49          if (pEngine) {          if (pEngineChannel) {
50              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);              MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
51              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
52              delete pEngine;              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngineChannel);
53                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.
65            MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
66          // disconnect old engine          // disconnect old engine
67          if (pEngine) {          if (pEngineChannel) {
68              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
69              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngineChannel);
70              delete pEngine;              delete pEngineChannel;
71          }          }
72    
73          // connect new engine          // connect new engine channel
74          pEngine = pNewEngine;          pEngineChannel = pNewEngineChannel;
75          if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index());          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel);
76          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngineChannel);
77          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
78      }      }
79    
80      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
81          // disconnect old device          // disconnect old device
82          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngineChannel) pAudioOutputDevice->Disconnect(pEngineChannel);
   
83          // connect new device          // connect new device
84          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
85          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) pAudioOutputDevice->Connect(pEngineChannel);
86      }      }
87    
88      void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
89          // get / create desired midi device         SetMidiInput(pDevice, this->midiPort, this->midiChannel);
90          MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType);      }
         if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType);  
91    
92          // disconnect old device      void SamplerChannel::SetMidiInputPort(int MidiPort) {
93          if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine);         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);
94        }
95    
96          // connect new device      void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) {
97           SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);
98        }
99    
100        void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) {
101            // dereference old midi input port.
102            MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
103            // disconnect old device port
104            if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel);
105            // new device, port and channel
106          pMidiInputDevice = pDevice;          pMidiInputDevice = pDevice;
107          if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel);          this->midiPort = iMidiPort;
108            this->midiChannel = MidiChannel;
109            // connect new device port
110            pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
111            if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel);
112            // Ooops.
113            if (pMidiInputPort == NULL)
114                throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
115      }      }
116    
117      Engine* SamplerChannel::GetEngine() {      EngineChannel* SamplerChannel::GetEngineChannel() {
118          return pEngine;          return pEngineChannel;
119      }      }
120    
121      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {
122          return pMidiInputDevice;          return this->midiChannel;
123        }
124    
125        int SamplerChannel::GetMidiInputPort() {
126            MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
127            return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);
128      }      }
129    
130      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
131          return pAudioOutputDevice;          return pAudioOutputDevice;
132      }      }
133    
134        MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
135            return pMidiInputDevice;
136        }
137    
138      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
139          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
140    
141          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
142          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
143              if (*iter == this) {              if (iter->second == this) {
144                  iIndex = i;                  iIndex = iter->first;
145                  return i;                  return iIndex;
146              }              }
147          }          }
148    
149          throw LinuxSamplerException("SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
150        }
151    
152        MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) {
153            MidiInputPort* pMidiInputPort = NULL;
154            if (pMidiInputDevice)
155                pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
156            return pMidiInputPort;
157      }      }
158    
159    
160    
161      // ******************************************************************      // ******************************************************************
162      // * Sampler      // * Sampler
163    
# Line 132  namespace LinuxSampler { Line 165  namespace LinuxSampler {
165      }      }
166    
167      Sampler::~Sampler() {      Sampler::~Sampler() {
168          // 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 = MidiInputDevices.begin();  
             for (; iter != MidiInputDevices.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;  
             }  
         }  
169      }      }
170    
171      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
172          return vSamplerChannels.size();          return mSamplerChannels.size();
173      }      }
174    
175      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
176            // if there's no sampler channel yet
177            if (!mSamplerChannels.size()) {
178                SamplerChannel* pChannel = new SamplerChannel(this);
179                mSamplerChannels[0] = pChannel;
180                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, 1));
181                return pChannel;
182            }
183    
184            // get the highest used sampler channel index
185            uint lastIndex = (--(mSamplerChannels.end()))->first;
186    
187            // check if we reached the index limit
188            if (lastIndex + 1 < lastIndex) {
189                // search for an unoccupied sampler channel index starting from 0
190                for (uint i = 0; i < lastIndex; i++) {
191                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
192                    // we found an unused index, so insert the new channel there
193                    SamplerChannel* pChannel = new SamplerChannel(this);
194                    mSamplerChannels[i] = pChannel;
195                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, i));
196                    return pChannel;
197                }
198                throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
199            }
200    
201            // we have not reached the index limit so we just add the channel past the highest index
202          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
203          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
204            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, lastIndex + 1));
205          return pChannel;          return pChannel;
206      }      }
207    
208      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
209          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
210          return vSamplerChannels[uiSamplerChannel];      }
211    
212        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
213            return mSamplerChannels;
214      }      }
215    
216      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
217          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
218          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
219              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
220                  vSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
221                  delete pSamplerChannel;                  delete pSamplerChannel;
222                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, mSamplerChannels.size()));
223                  return;                  return;
224              }              }
225          }          }
# Line 199  namespace LinuxSampler { Line 239  namespace LinuxSampler {
239          // create new device          // create new device
240          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
241    
         // activate device  
         pDevice->Play();  
   
242          // add new audio device to the audio device list          // add new audio device to the audio device list
243          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
244              if (!mAudioOutputDevices[i]) {              if (!mAudioOutputDevices[i]) {
# Line 217  namespace LinuxSampler { Line 254  namespace LinuxSampler {
254          return mAudioOutputDevices.size();          return mAudioOutputDevices.size();
255      }      }
256    
257        uint Sampler::MidiInputDevices() {
258            return mMidiInputDevices.size();
259        }
260    
261      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
262          return mAudioOutputDevices;          return mAudioOutputDevices;
263      }      }
264    
265        std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
266            return mMidiInputDevices;
267        }
268    
269      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {
270          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
271          for (; iter != mAudioOutputDevices.end(); iter++) {          for (; iter != mAudioOutputDevices.end(); iter++) {
# Line 241  namespace LinuxSampler { Line 286  namespace LinuxSampler {
286          }          }
287      }      }
288    
289      MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) {      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {
290          // check if device already created          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
291          MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);          for (; iter != mMidiInputDevices.end(); iter++) {
292          if (pDevice) return pDevice;              if (iter->second == pDevice) {
293                    // check if there are still sampler engines connected to this device
294                    for (uint i = 0; i < SamplerChannels(); i++)
295                        if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
296    
297                    // disable device
298                    pDevice->StopListen();
299    
300          // create new device                  // remove device from the device list
301          switch (MidiType) {                  mMidiInputDevices.erase(iter);
302              case MidiInputDevice::type_alsa:  
303                  pDevice = new MidiInputDeviceAlsa;                  // destroy and free device from memory
304                  break;                  delete pDevice;
305              default:              }
                 throw LinuxSamplerException("Unknown audio output device type");  
306          }          }
307        }
308    
309          // activate device      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
310          pDevice->Listen();          // create new device
311            MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);
312    
313          // add new MIDI device to the MIDI device list          // add new device to the midi device list
314          MidiInputDevices[MidiType] = pDevice;          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
315                    if (!mMidiInputDevices[i]) {
316                            mMidiInputDevices[i] = pDevice;
317                            break;
318                    }
319            }
320    
321          return pDevice;          return pDevice;
322      }      }
323    
324      MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) {      void Sampler::Reset() {
325          MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);          // delete sampler channels
326          return (iter != MidiInputDevices.end()) ? iter->second : NULL;          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.123  
changed lines
  Added in v.411

  ViewVC Help
Powered by ViewVC