/[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 155 by senkov, Mon Jun 28 04:30:11 2004 UTC revision 730 by iliev, Sat Jul 30 08:18:08 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/MidiInputDeviceFactory.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;
         pMidiInputPort     = NULL;  
41          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
42            pMidiInputDevice   = NULL;
43            iMidiPort          = 0;
44            midiChannel        = midi_chan_all;
45          iIndex             = -1;          iIndex             = -1;
         midiChannel        = MidiInputDevice::MidiInputPort::midi_chan_all;  
46      }      }
47    
48      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
49          if (pEngine) {          if (pEngineChannel) {
50              if (pMidiInputPort) pMidiInputPort->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(2,("SamplerChannel: Loading engine..."));          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            // connect new engine channel
79            if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
80            if (pAudioOutputDevice) {
81                pNewEngineChannel->Connect(pAudioOutputDevice);
82                pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
83            }
84            pEngineChannel = pNewEngineChannel;
85    
86            // from now on get MIDI device and port from EngineChannel object
87            this->pMidiInputDevice = NULL;
88            this->iMidiPort        = 0;
89    
90          // disconnect old engine          pEngineChannel->StatusChanged(true);
         if (pEngine) {  
             if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);  
             if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);  
             delete pEngine;  
         }  
91    
         // connect new engine  
         pEngine = pNewEngine;  
         if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, (MidiInputDevice::MidiInputPort::midi_chan_t) Index());  
         if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);  
92          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
93      }      }
94    
95      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
96          // disconnect old device          // disconnect old device
97          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice();
   
98          // connect new device          // connect new device
99          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
100          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
101                pEngineChannel->Connect(pAudioOutputDevice);
102                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
103            }
104      }      }
105    
106      void SamplerChannel::SetMidiInputPort(MidiInputDevice* pDevice, int midiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
107          // disconnect old port         SetMidiInput(pDevice, 0, GetMidiInputChannel());
         if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);  
   
         // connect new port  
         pMidiInputPort = pDevice->GetPort(midiPort);  
         if (!pMidiInputPort)  
                 throw LinuxSamplerException("MIDI port doesn't exist");  
         if (pEngine) {  
                 pMidiInputPort->Connect(pEngine, MidiChannel);  
                 this->midiChannel = MidiChannel;  
         }  
108      }      }
109    
110      Engine* SamplerChannel::GetEngine() {      void SamplerChannel::SetMidiInputPort(int MidiPort) {
111          return pEngine;         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
112      }      }
113    
114      MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputPort() {      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
115          return pMidiInputPort;         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
116        }
117    
118        void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
119            if (!pDevice) throw LinuxSamplerException("No MIDI input device assigned.");
120    
121            // get old and new midi input port
122            MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
123            MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
124    
125            // disconnect old device port
126            if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);
127            // remember new device, port and channel if not engine channel yet created
128            if (!pEngineChannel) {
129                this->pMidiInputDevice = pDevice;
130                this->iMidiPort        = iMidiPort;
131                this->midiChannel      = MidiChannel;
132            }
133    
134            // connect new device port
135            if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
136            // Ooops.
137            if (pNewMidiInputPort == NULL)
138                throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
139        }
140    
141        EngineChannel* SamplerChannel::GetEngineChannel() {
142            return pEngineChannel;
143        }
144    
145        midi_chan_t SamplerChannel::GetMidiInputChannel() {
146            if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
147            return this->midiChannel;
148        }
149    
150        int SamplerChannel::GetMidiInputPort() {
151            MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
152            if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
153            return iMidiPort;
154      }      }
155    
156      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
# Line 113  namespace LinuxSampler { Line 158  namespace LinuxSampler {
158      }      }
159    
160      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
161          MidiInputDevice::MidiInputPort* port = GetMidiInputPort();          if (pEngineChannel)
162          if (!port)              pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
163                  return NULL;          return pMidiInputDevice;
         return port->GetDevice();  
164      }      }
165    
166      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
167          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
168    
169          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
170          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
171              if (*iter == this) {              if (iter->second == this) {
172                  iIndex = i;                  iIndex = iter->first;
173                  return i;                  return iIndex;
174              }              }
175          }          }
176    
177          throw LinuxSamplerException("SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
178      }      }
179    
180        MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
181            MidiInputPort* pMidiInputPort = NULL;
182            MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
183            if (pMidiInputDevice)
184                pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
185            return pMidiInputPort;
186        }
187    
188    
189    
190      // ******************************************************************      // ******************************************************************
191      // * Sampler      // * Sampler
# Line 141  namespace LinuxSampler { Line 194  namespace LinuxSampler {
194      }      }
195    
196      Sampler::~Sampler() {      Sampler::~Sampler() {
197          // delete sampler channels          Reset();
         {  
             std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin();  
             for (; iter != vSamplerChannels.end(); iter++) delete *iter;  
         }  
   
         // delete midi input devices  
         {  
             MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();  
             for (; iter != mMidiInputDevices.end(); iter++) {  
                 MidiInputDevice* pDevice = iter->second;  
                 pDevice->StopListen();  
                 delete pDevice;  
             }  
         }  
   
         // delete audio output devices  
         {  
             AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();  
             for (; iter != mAudioOutputDevices.end(); iter++) {  
                 AudioOutputDevice* pDevice = iter->second;  
                 pDevice->Stop();  
                 delete pDevice;  
             }  
         }  
198      }      }
199    
200      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
201          return vSamplerChannels.size();          return mSamplerChannels.size();
202      }      }
203    
204      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
205            // if there's no sampler channel yet
206            if (!mSamplerChannels.size()) {
207                SamplerChannel* pChannel = new SamplerChannel(this);
208                mSamplerChannels[0] = pChannel;
209                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));
210                return pChannel;
211            }
212    
213            // get the highest used sampler channel index
214            uint lastIndex = (--(mSamplerChannels.end()))->first;
215    
216            // check if we reached the index limit
217            if (lastIndex + 1 < lastIndex) {
218                // search for an unoccupied sampler channel index starting from 0
219                for (uint i = 0; i < lastIndex; i++) {
220                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
221                    // we found an unused index, so insert the new channel there
222                    SamplerChannel* pChannel = new SamplerChannel(this);
223                    mSamplerChannels[i] = pChannel;
224                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));
225                    return pChannel;
226                }
227                throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
228            }
229    
230            // we have not reached the index limit so we just add the channel past the highest index
231          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
232          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
233            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));
234          return pChannel;          return pChannel;
235      }      }
236    
237      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
238          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
239          return vSamplerChannels[uiSamplerChannel];      }
240    
241        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
242            return mSamplerChannels;
243      }      }
244    
245      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
246          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
247          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
248              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
249                  vSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
250                  delete pSamplerChannel;                  delete pSamplerChannel;
251                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));
252                  return;                  return;
253              }              }
254          }          }
# Line 208  namespace LinuxSampler { Line 268  namespace LinuxSampler {
268          // create new device          // create new device
269          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
270    
         // activate device  
         pDevice->Play();  
   
271          // add new audio device to the audio device list          // add new audio device to the audio device list
272          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
273              if (!mAudioOutputDevices[i]) {              if (!mAudioOutputDevices[i]) {
# Line 280  namespace LinuxSampler { Line 337  namespace LinuxSampler {
337    
338      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
339          // create new device          // create new device
340          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
   
         // activate device  
         pDevice->Listen();  
341    
342          // add new device to the midi device list          // add new device to the midi device list
343          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
# Line 296  namespace LinuxSampler { Line 350  namespace LinuxSampler {
350          return pDevice;          return pDevice;
351      }      }
352    
353        void Sampler::Reset() {
354            // delete sampler channels
355            try {
356                while (true) {
357                        SamplerChannelMap::iterator iter = mSamplerChannels.begin();
358                        if (iter == mSamplerChannels.end()) break;
359                        RemoveSamplerChannel(iter->second);
360                }
361            }
362            catch(...) {
363                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
364                exit(EXIT_FAILURE);
365            }
366    
367            // delete midi input devices
368            try {
369                while (true) {
370                        MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
371                        if (iter == mMidiInputDevices.end()) break;
372                        DestroyMidiInputDevice(iter->second);
373                }
374            }
375            catch(...) {
376                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
377                exit(EXIT_FAILURE);
378            }
379    
380            // delete audio output devices
381            try {
382                while (true) {
383                        AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
384                        if (iter == mAudioOutputDevices.end()) break;
385                        DestroyAudioOutputDevice(iter->second);
386                }
387            }
388            catch(...) {
389                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
390                exit(EXIT_FAILURE);
391            }
392        }
393    
394  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.155  
changed lines
  Added in v.730

  ViewVC Help
Powered by ViewVC