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

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

  ViewVC Help
Powered by ViewVC