/[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 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 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 (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
53                delete pEngineChannel;
54          }          }
55      }      }
56    
57      void SamplerChannel::LoadEngine(engine_type_t EngineType) {      void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) {
58          dmsg(1,("SamplerChannel: Loading engine\n"));          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) pEngineChannel->DisconnectAudioOutputDevice();
70              delete pEngine;              delete pEngineChannel;
71          }          }
   
         // 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);  
72    
73          // disconnect old device          // connect new engine channel
74          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          pEngineChannel = pNewEngineChannel;
75            if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel);
76            if (pAudioOutputDevice) {
77                pNewEngineChannel->Connect(pAudioOutputDevice);
78                pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
79            }
80            dmsg(2,("OK\n"));
81        }
82    
83        void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
84            // disconnect old device
85            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(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
95          // get / create desired midi device         SetMidiInput(pDevice, this->midiPort, this->midiChannel);
96          MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType);      }
         if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType);  
97    
98          // disconnect old device      void SamplerChannel::SetMidiInputPort(int MidiPort) {
99          if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine);         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);
100        }
101    
102          // connect new device      void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) {
103           SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);
104        }
105    
106        void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) {
107            // dereference old midi input port.
108            MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
109            // disconnect old device port
110            if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel);
111            // new device, port and channel
112          pMidiInputDevice = pDevice;          pMidiInputDevice = pDevice;
113          if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel);          this->midiPort = iMidiPort;
114            this->midiChannel = MidiChannel;
115            // connect new device port
116            pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
117            if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel);
118            // Ooops.
119            if (pMidiInputPort == NULL)
120                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* SamplerChannel::GetMidiInputDevice() {      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {
128          return pMidiInputDevice;          return this->midiChannel;
129        }
130    
131        int SamplerChannel::GetMidiInputPort() {
132            MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
133            return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);
134      }      }
135    
136      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
137          return pAudioOutputDevice;          return pAudioOutputDevice;
138      }      }
139    
140        MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
141            return pMidiInputDevice;
142        }
143    
144      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
145          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
146    
147          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
148          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
149              if (*iter == this) {              if (iter->second == this) {
150                  iIndex = i;                  iIndex = iter->first;
151                  return i;                  return iIndex;
152              }              }
153          }          }
154    
155          throw LinuxSamplerException("SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
156      }      }
157    
158        MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) {
159            MidiInputPort* pMidiInputPort = NULL;
160            if (pMidiInputDevice)
161                pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
162            return pMidiInputPort;
163        }
164    
165    
166    
167      // ******************************************************************      // ******************************************************************
168      // * Sampler      // * Sampler
# Line 135  namespace LinuxSampler { Line 171  namespace LinuxSampler {
171      }      }
172    
173      Sampler::~Sampler() {      Sampler::~Sampler() {
174          // 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;  
             }  
         }  
175      }      }
176    
177      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
178          vSamplerChannels.size();          return mSamplerChannels.size();
179      }      }
180    
181      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
182            // if there's no sampler channel yet
183            if (!mSamplerChannels.size()) {
184                SamplerChannel* pChannel = new SamplerChannel(this);
185                mSamplerChannels[0] = pChannel;
186                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, 1));
187                return pChannel;
188            }
189    
190            // get the highest used sampler channel index
191            uint lastIndex = (--(mSamplerChannels.end()))->first;
192    
193            // check if we reached the index limit
194            if (lastIndex + 1 < lastIndex) {
195                // search for an unoccupied sampler channel index starting from 0
196                for (uint i = 0; i < lastIndex; i++) {
197                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
198                    // we found an unused index, so insert the new channel there
199                    SamplerChannel* pChannel = new SamplerChannel(this);
200                    mSamplerChannels[i] = pChannel;
201                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, i));
202                    return pChannel;
203                }
204                throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
205            }
206    
207            // 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          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
210            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, lastIndex + 1));
211          return pChannel;          return pChannel;
212      }      }
213    
214      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
215          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
216          return vSamplerChannels[uiSamplerChannel];      }
217    
218        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
219            return mSamplerChannels;
220      }      }
221    
222      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
223          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
224          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
225              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
226                  vSamplerChannels.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 194  namespace LinuxSampler { Line 237  namespace LinuxSampler {
237          RemoveSamplerChannel(pChannel);          RemoveSamplerChannel(pChannel);
238      }      }
239    
240      AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) {      std::vector<String> Sampler::AvailableAudioOutputDrivers() {
241          // check if device already created          return AudioOutputDeviceFactory::AvailableDrivers();
242          AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType);      }
         if (pDevice) return pDevice;  
243    
244        AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
245          // create new device          // create new device
246          switch (AudioType) {          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
247              case audio_output_type_alsa:  
248                  pDevice = new AudioOutputDeviceAlsa;          // add new audio device to the audio device list
249                  break;          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
250              case audio_output_type_jack:              if (!mAudioOutputDevices[i]) {
251                  pDevice = new AudioOutputDeviceJack;                  mAudioOutputDevices[i] = pDevice;
252                  break;                  break;
253              default:              }
                 throw LinuxSamplerException("Unknown audio output device type");  
254          }          }
255    
         // activate device  
         pDevice->Play();  
   
256          return pDevice;          return pDevice;
257      }      }
258    
259      AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) {      uint Sampler::AudioOutputDevices() {
260          AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType);          return mAudioOutputDevices.size();
         return (iter != AudioOutputDevices.end()) ? iter->second : NULL;  
261      }      }
262    
263      MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) {      uint Sampler::MidiInputDevices() {
264          // check if device already created          return mMidiInputDevices.size();
265          MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);      }
         if (pDevice) return pDevice;  
266    
267          // create new device      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
268          switch (MidiType) {          return mAudioOutputDevices;
269              case midi_input_type_alsa:      }
270                  pDevice = new MidiInputDeviceAlsa;  
271                  break;      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
272              default:          return mMidiInputDevices;
273                  throw LinuxSamplerException("Unknown audio output device type");      }
274    
275        void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {
276            AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
277            for (; iter != mAudioOutputDevices.end(); iter++) {
278                if (iter->second == pDevice) {
279                    // check if there are still sampler engines connected to this device
280                    for (uint i = 0; i < SamplerChannels(); i++)
281                        if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
282    
283                    // disable device
284                    pDevice->Stop();
285    
286                    // remove device from the device list
287                    mAudioOutputDevices.erase(iter);
288    
289                    // destroy and free device from memory
290                    delete pDevice;
291                }
292            }
293        }
294    
295        void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {
296            MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
297            for (; iter != mMidiInputDevices.end(); iter++) {
298                if (iter->second == pDevice) {
299                    // check if there are still sampler engines connected to this device
300                    for (uint i = 0; i < SamplerChannels(); i++)
301                        if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
302    
303                    // disable device
304                    pDevice->StopListen();
305    
306                    // remove device from the device list
307                    mMidiInputDevices.erase(iter);
308    
309                    // destroy and free device from memory
310                    delete pDevice;
311                }
312          }          }
313        }
314    
315        MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
316            // create new device
317            MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);
318    
319          // activate device          // add new device to the midi device list
320          pDevice->Listen();          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
321                    if (!mMidiInputDevices[i]) {
322                            mMidiInputDevices[i] = pDevice;
323                            break;
324                    }
325            }
326    
327          return pDevice;          return pDevice;
328      }      }
329    
330      MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) {      void Sampler::Reset() {
331          MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);          // delete sampler channels
332          return (iter != MidiInputDevices.end()) ? iter->second : NULL;          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.61  
changed lines
  Added in v.412

  ViewVC Help
Powered by ViewVC