/[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 61 by schoenebeck, Mon May 3 19:29:44 2004 UTC revision 675 by schoenebeck, Wed Jun 22 22:09:28 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 20  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24    #include <sstream>
25    
26  #include "Sampler.h"  #include "Sampler.h"
27    
28  #include "audiodriver/AudioOutputDeviceAlsa.h"  #include "engines/EngineChannelFactory.h"
29  #include "audiodriver/AudioOutputDeviceJack.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
30  #include "mididriver/MidiInputDeviceAlsa.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
31  #include "engines/gig/Engine.h"  #include "network/lscpserver.h"
32    
33  namespace LinuxSampler {  namespace LinuxSampler {
34    
# Line 34  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;
         pMidiInputDevice   = NULL;  
41          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
42            pMidiInputDevice   = NULL;
43            iMidiPort          = 0;
44            midiChannel        = 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 = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
51              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
52              delete pEngine;              if (pEngineChannel) {
53                    if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
54                    EngineChannelFactory::Destroy(pEngineChannel);
55                }
56          }          }
57      }      }
58    
59      void SamplerChannel::LoadEngine(engine_type_t EngineType) {      void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) {
60          dmsg(1,("SamplerChannel: Loading engine\n"));          dmsg(2,("SamplerChannel: Assigning engine type..."));
61    
62          // create new engine          // create new engine channel
63          Engine* pNewEngine = NULL;          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
64          switch (EngineType) {          if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type");
65              case engine_type_gig:  
66                  pNewEngine = new gig::Engine;          //FIXME: hack to allow fast retrieval of engine channel's sampler channel index
67                  break;          pNewEngineChannel->iSamplerChannelIndex = Index();
68              default:  
69                  throw LinuxSamplerException("Unknown engine type");          // dereference midi input port.
70            MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
71            // disconnect old engine channel
72            if (pEngineChannel) {
73                if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
74                if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
75                EngineChannelFactory::Destroy(pEngineChannel);
76          }          }
77    
78          // disconnect old engine          // connect new engine channel
79          if (pEngine) {          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
80              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);          if (pAudioOutputDevice) {
81              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              pNewEngineChannel->Connect(pAudioOutputDevice);
82              delete pEngine;              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
83          }          }
84            pEngineChannel = pNewEngineChannel;
85    
86          // connect new engine          // from now on get MIDI device and port from EngineChannel object
87          pEngine = pNewEngine;          this->pMidiInputDevice = NULL;
88          if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index());          this->iMidiPort        = 0;
         if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);  
         dmsg(1,("SamplerChannel: Engine loaded.\n"));  
     }  
89    
90      void SamplerChannel::SetAudioOutputDevice(audio_output_type_t AudioType) {          dmsg(2,("OK\n"));
91          // get / create desired audio device      }
         AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType);  
         if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType);  
92    
93        void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
94          // disconnect old device          // disconnect old device
95          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice();
   
96          // connect new device          // connect new device
97          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
98          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
99                pEngineChannel->Connect(pAudioOutputDevice);
100                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
101            }
102      }      }
103    
104      void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
105          // get / create desired midi device         SetMidiInput(pDevice, 0, GetMidiInputChannel());
106          MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType);      }
         if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType);  
107    
108          // disconnect old device      void SamplerChannel::SetMidiInputPort(int MidiPort) {
109          if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine);         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
110        }
111    
112          // connect new device      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
113          pMidiInputDevice = pDevice;         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
         if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel);  
114      }      }
115    
116      Engine* SamplerChannel::GetEngine() {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
117          return pEngine;          if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned.");
118    
119            // get old and new midi input port
120            MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
121            MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
122    
123            // disconnect old device port
124            if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);
125            // remember new device, port and channel if not engine channel yet created
126            if (!pEngineChannel) {
127                this->pMidiInputDevice = pDevice;
128                this->iMidiPort        = iMidiPort;
129                this->midiChannel      = MidiChannel;
130            }
131    
132            // connect new device port
133            if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
134            // Ooops.
135            if (pNewMidiInputPort == NULL)
136                throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
137      }      }
138    
139      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      EngineChannel* SamplerChannel::GetEngineChannel() {
140          return pMidiInputDevice;          return pEngineChannel;
141        }
142    
143        midi_chan_t SamplerChannel::GetMidiInputChannel() {
144            if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
145            return this->midiChannel;
146        }
147    
148        int SamplerChannel::GetMidiInputPort() {
149            MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
150            if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
151            return iMidiPort;
152      }      }
153    
154      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
155          return pAudioOutputDevice;          return pAudioOutputDevice;
156      }      }
157    
158        MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
159            if (pEngineChannel)
160                pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
161            return pMidiInputDevice;
162        }
163    
164      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
165          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
166    
167          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
168          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
169              if (*iter == this) {              if (iter->second == this) {
170                  iIndex = i;                  iIndex = iter->first;
171                  return i;                  return iIndex;
172              }              }
173          }          }
174    
175          throw LinuxSamplerException("SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
176      }      }
177    
178        MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
179            MidiInputPort* pMidiInputPort = NULL;
180            MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
181            if (pMidiInputDevice)
182                pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
183            return pMidiInputPort;
184        }
185    
186    
187    
188      // ******************************************************************      // ******************************************************************
189      // * Sampler      // * Sampler
# Line 135  namespace LinuxSampler { Line 192  namespace LinuxSampler {
192      }      }
193    
194      Sampler::~Sampler() {      Sampler::~Sampler() {
195          // 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 = AudioOutputDevices.begin();  
             for (; iter != AudioOutputDevices.end(); iter++) {  
                 AudioOutputDevice* pDevice = iter->second;  
                 pDevice->Stop();  
                 delete pDevice;  
             }  
         }  
196      }      }
197    
198      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
199          vSamplerChannels.size();          return mSamplerChannels.size();
200      }      }
201    
202      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
203            // if there's no sampler channel yet
204            if (!mSamplerChannels.size()) {
205                SamplerChannel* pChannel = new SamplerChannel(this);
206                mSamplerChannels[0] = pChannel;
207                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));
208                return pChannel;
209            }
210    
211            // get the highest used sampler channel index
212            uint lastIndex = (--(mSamplerChannels.end()))->first;
213    
214            // check if we reached the index limit
215            if (lastIndex + 1 < lastIndex) {
216                // search for an unoccupied sampler channel index starting from 0
217                for (uint i = 0; i < lastIndex; i++) {
218                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
219                    // we found an unused index, so insert the new channel there
220                    SamplerChannel* pChannel = new SamplerChannel(this);
221                    mSamplerChannels[i] = pChannel;
222                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));
223                    return pChannel;
224                }
225                throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
226            }
227    
228            // we have not reached the index limit so we just add the channel past the highest index
229          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
230          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
231            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));
232          return pChannel;          return pChannel;
233      }      }
234    
235      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
236          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
237          return vSamplerChannels[uiSamplerChannel];      }
238    
239        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
240            return mSamplerChannels;
241      }      }
242    
243      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
244          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
245          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
246              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
247                  vSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
248                  delete pSamplerChannel;                  delete pSamplerChannel;
249                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));
250                  return;                  return;
251              }              }
252          }          }
# Line 194  namespace LinuxSampler { Line 258  namespace LinuxSampler {
258          RemoveSamplerChannel(pChannel);          RemoveSamplerChannel(pChannel);
259      }      }
260    
261      AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) {      std::vector<String> Sampler::AvailableAudioOutputDrivers() {
262          // check if device already created          return AudioOutputDeviceFactory::AvailableDrivers();
263          AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType);      }
         if (pDevice) return pDevice;  
264    
265        AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
266          // create new device          // create new device
267          switch (AudioType) {          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
268              case audio_output_type_alsa:  
269                  pDevice = new AudioOutputDeviceAlsa;          // add new audio device to the audio device list
270                  break;          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
271              case audio_output_type_jack:              if (!mAudioOutputDevices[i]) {
272                  pDevice = new AudioOutputDeviceJack;                  mAudioOutputDevices[i] = pDevice;
273                  break;                  break;
274              default:              }
                 throw LinuxSamplerException("Unknown audio output device type");  
275          }          }
276    
         // activate device  
         pDevice->Play();  
   
277          return pDevice;          return pDevice;
278      }      }
279    
280      AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) {      uint Sampler::AudioOutputDevices() {
281          AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType);          return mAudioOutputDevices.size();
         return (iter != AudioOutputDevices.end()) ? iter->second : NULL;  
282      }      }
283    
284      MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) {      uint Sampler::MidiInputDevices() {
285          // check if device already created          return mMidiInputDevices.size();
286          MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);      }
         if (pDevice) return pDevice;  
287    
288          // create new device      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
289          switch (MidiType) {          return mAudioOutputDevices;
290              case midi_input_type_alsa:      }
291                  pDevice = new MidiInputDeviceAlsa;  
292                  break;      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
293              default:          return mMidiInputDevices;
294                  throw LinuxSamplerException("Unknown audio output device type");      }
295    
296        void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {
297            AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
298            for (; iter != mAudioOutputDevices.end(); iter++) {
299                if (iter->second == pDevice) {
300                    // check if there are still sampler engines connected to this device
301                    for (uint i = 0; i < SamplerChannels(); i++)
302                        if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
303    
304                    // disable device
305                    pDevice->Stop();
306    
307                    // remove device from the device list
308                    mAudioOutputDevices.erase(iter);
309    
310                    // destroy and free device from memory
311                    delete pDevice;
312                }
313            }
314        }
315    
316        void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {
317            MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
318            for (; iter != mMidiInputDevices.end(); iter++) {
319                if (iter->second == pDevice) {
320                    // check if there are still sampler engines connected to this device
321                    for (uint i = 0; i < SamplerChannels(); i++)
322                        if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
323    
324                    // disable device
325                    pDevice->StopListen();
326    
327                    // remove device from the device list
328                    mMidiInputDevices.erase(iter);
329    
330                    // destroy and free device from memory
331                    delete pDevice;
332                }
333          }          }
334        }
335    
336        MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
337            // create new device
338            MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
339    
340          // activate device          // add new device to the midi device list
341          pDevice->Listen();          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
342                    if (!mMidiInputDevices[i]) {
343                            mMidiInputDevices[i] = pDevice;
344                            break;
345                    }
346            }
347    
348          return pDevice;          return pDevice;
349      }      }
350    
351      MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) {      void Sampler::Reset() {
352          MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);          // delete sampler channels
353          return (iter != MidiInputDevices.end()) ? iter->second : NULL;          try {
354                while (true) {
355                        SamplerChannelMap::iterator iter = mSamplerChannels.begin();
356                        if (iter == mSamplerChannels.end()) break;
357                        RemoveSamplerChannel(iter->second);
358                }
359            }
360            catch(...) {
361                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
362                exit(EXIT_FAILURE);
363            }
364    
365            // delete midi input devices
366            try {
367                while (true) {
368                        MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
369                        if (iter == mMidiInputDevices.end()) break;
370                        DestroyMidiInputDevice(iter->second);
371                }
372            }
373            catch(...) {
374                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
375                exit(EXIT_FAILURE);
376            }
377    
378            // delete audio output devices
379            try {
380                while (true) {
381                        AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
382                        if (iter == mAudioOutputDevices.end()) break;
383                        DestroyAudioOutputDevice(iter->second);
384                }
385            }
386            catch(...) {
387                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
388                exit(EXIT_FAILURE);
389            }
390      }      }
391    
392  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.61  
changed lines
  Added in v.675

  ViewVC Help
Powered by ViewVC