/[svn]/linuxsampler/trunk/src/Sampler.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/Sampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 123 by schoenebeck, Mon Jun 14 19:33:16 2004 UTC revision 209 by schoenebeck, Sun Jul 18 00:29:39 2004 UTC
# Line 24  Line 24 
24    
25  #include "Sampler.h"  #include "Sampler.h"
26    
27  #include "audiodriver/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
28  #include "mididriver/MidiInputDeviceAlsa.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
29  #include "engines/gig/Engine.h"  #include "engines/gig/Engine.h"
30    
31  namespace LinuxSampler {  namespace LinuxSampler {
# Line 38  namespace LinuxSampler { Line 38  namespace LinuxSampler {
38          pEngine            = NULL;          pEngine            = NULL;
39          pMidiInputDevice   = NULL;          pMidiInputDevice   = NULL;
40          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
41            midiPort           = 0;
42            midiChannel        = MidiInputDevice::MidiInputPort::midi_chan_all;
43          iIndex             = -1;          iIndex             = -1;
44      }      }
45    
46      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
47          if (pEngine) {          if (pEngine) {
48              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);              MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
49                if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
50              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
51              delete pEngine;              delete pEngine;
52          }          }
# Line 62  namespace LinuxSampler { Line 65  namespace LinuxSampler {
65                  throw LinuxSamplerException("Unknown engine type");                  throw LinuxSamplerException("Unknown engine type");
66          }          }
67    
68            // dereference midi input port.
69            MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
70          // disconnect old engine          // disconnect old engine
71          if (pEngine) {          if (pEngine) {
72              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
73              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
74              delete pEngine;              delete pEngine;
75          }          }
76    
77          // connect new engine          // connect new engine
78          pEngine = pNewEngine;          pEngine = pNewEngine;
79          if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index());          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel);
80          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);
81          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
82      }      }
# Line 79  namespace LinuxSampler { Line 84  namespace LinuxSampler {
84      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
85          // disconnect old device          // disconnect old device
86          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);
   
87          // connect new device          // connect new device
88          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
89          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngine) pAudioOutputDevice->Connect(pEngine);
90      }      }
91    
92      void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
93          // get / create desired midi device         SetMidiInput(pDevice, this->midiPort, this->midiChannel);
94          MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType);      }
         if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType);  
95    
96          // disconnect old device      void SamplerChannel::SetMidiInputPort(int MidiPort) {
97          if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine);         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);
98        }
99    
100          // connect new device      void SamplerChannel::SetMidiInputChannel(MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {
101           SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);
102        }
103    
104        void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int MidiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {
105            // dereference old midi input port.
106            MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
107            // disconnect old device port
108            if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);
109            // new device, port and channel
110          pMidiInputDevice = pDevice;          pMidiInputDevice = pDevice;
111          if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel);          this->midiPort = MidiPort;
112            this->midiChannel = MidiChannel;
113            // connect new device port
114            pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
115            if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);
116            // Ooops.
117            if (pMidiInputPort == NULL)
118                throw LinuxSamplerException("There is no MIDI input port with index " + ToString(MidiPort) + ".");
119      }      }
120    
121      Engine* SamplerChannel::GetEngine() {      Engine* SamplerChannel::GetEngine() {
122          return pEngine;          return pEngine;
123      }      }
124    
125      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      MidiInputDevice::MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {
126          return pMidiInputDevice;          return this->midiChannel;
127        }
128    
129        int SamplerChannel::GetMidiInputPort() {
130            MidiInputDevice::MidiInputPort *pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
131            return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);
132      }      }
133    
134      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
135          return pAudioOutputDevice;          return pAudioOutputDevice;
136      }      }
137    
138        MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
139            return pMidiInputDevice;
140        }
141    
142      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
143          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
144    
145          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
146          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
147              if (*iter == this) {              if (iter->second == this) {
148                  iIndex = i;                  iIndex = iter->first;
149                  return i;                  return iIndex;
150              }              }
151          }          }
152    
153          throw LinuxSamplerException("SamplerChannel index not found");          throw LinuxSamplerException("Internal error: SamplerChannel index not found");
154      }      }
155    
156        MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int MidiPort) {
157            MidiInputDevice::MidiInputPort *pMidiInputPort = NULL;
158            if (pMidiInputDevice)
159                pMidiInputPort = pMidiInputDevice->GetPort(MidiPort);
160            return pMidiInputPort;
161        }
162    
163      // ******************************************************************      // ******************************************************************
164      // * Sampler      // * Sampler
# Line 134  namespace LinuxSampler { Line 169  namespace LinuxSampler {
169      Sampler::~Sampler() {      Sampler::~Sampler() {
170          // delete sampler channels          // delete sampler channels
171          {          {
172              std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin();              SamplerChannelMap::iterator iter = mSamplerChannels.begin();
173              for (; iter != vSamplerChannels.end(); iter++) delete *iter;              for (; iter != mSamplerChannels.end(); iter++) delete iter->second;
174          }          }
175    
176          // delete midi input devices          // delete midi input devices
177          {          {
178              MidiInputDeviceMap::iterator iter = MidiInputDevices.begin();              MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
179              for (; iter != MidiInputDevices.end(); iter++) {              for (; iter != mMidiInputDevices.end(); iter++) {
180                  MidiInputDevice* pDevice = iter->second;                  MidiInputDevice* pDevice = iter->second;
181                  pDevice->StopListen();                  pDevice->StopListen();
182                  delete pDevice;                  delete pDevice;
# Line 160  namespace LinuxSampler { Line 195  namespace LinuxSampler {
195      }      }
196    
197      uint Sampler::SamplerChannels() {      uint Sampler::SamplerChannels() {
198          return vSamplerChannels.size();          return mSamplerChannels.size();
199      }      }
200    
201      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
202            // if there's no sampler channel yet
203            if (!mSamplerChannels.size()) {
204                SamplerChannel* pChannel = new SamplerChannel(this);
205                mSamplerChannels[0] = pChannel;
206                return pChannel;
207            }
208    
209            // get the highest used sampler channel index
210            uint lastIndex = (--(mSamplerChannels.end()))->first;
211    
212            // check if we reached the index limit
213            if (lastIndex + 1 < lastIndex) {
214                // search for an unoccupied sampler channel index starting from 0
215                for (uint i = 0; i < lastIndex; i++) {
216                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
217                    // we found an unused index, so insert the new channel there
218                    SamplerChannel* pChannel = new SamplerChannel(this);
219                    mSamplerChannels[i] = pChannel;
220                    return pChannel;
221                }
222                throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
223            }
224    
225            // we have not reached the index limit so we just add the channel past the highest index
226          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
227          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
228          return pChannel;          return pChannel;
229      }      }
230    
231      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
232          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
233          return vSamplerChannels[uiSamplerChannel];      }
234    
235        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
236            return mSamplerChannels;
237      }      }
238    
239      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
240          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
241          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
242              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
243                  vSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
244                  delete pSamplerChannel;                  delete pSamplerChannel;
245                  return;                  return;
246              }              }
# Line 199  namespace LinuxSampler { Line 261  namespace LinuxSampler {
261          // create new device          // create new device
262          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
263    
         // activate device  
         pDevice->Play();  
   
264          // add new audio device to the audio device list          // add new audio device to the audio device list
265          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
266              if (!mAudioOutputDevices[i]) {              if (!mAudioOutputDevices[i]) {
# Line 217  namespace LinuxSampler { Line 276  namespace LinuxSampler {
276          return mAudioOutputDevices.size();          return mAudioOutputDevices.size();
277      }      }
278    
279        uint Sampler::MidiInputDevices() {
280            return mMidiInputDevices.size();
281        }
282    
283      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
284          return mAudioOutputDevices;          return mAudioOutputDevices;
285      }      }
286    
287        std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
288            return mMidiInputDevices;
289        }
290    
291      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {
292          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
293          for (; iter != mAudioOutputDevices.end(); iter++) {          for (; iter != mAudioOutputDevices.end(); iter++) {
# Line 241  namespace LinuxSampler { Line 308  namespace LinuxSampler {
308          }          }
309      }      }
310    
311      MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) {      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {
312          // check if device already created          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
313          MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);          for (; iter != mMidiInputDevices.end(); iter++) {
314          if (pDevice) return pDevice;              if (iter->second == pDevice) {
315                    // check if there are still sampler engines connected to this device
316                    for (uint i = 0; i < SamplerChannels(); i++)
317                        if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
318    
319          // create new device                  // disable device
320          switch (MidiType) {                  pDevice->StopListen();
321              case MidiInputDevice::type_alsa:  
322                  pDevice = new MidiInputDeviceAlsa;                  // remove device from the device list
323                  break;                  mMidiInputDevices.erase(iter);
324              default:  
325                  throw LinuxSamplerException("Unknown audio output device type");                  // destroy and free device from memory
326                    delete pDevice;
327                }
328          }          }
329        }
330    
331          // activate device      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
332          pDevice->Listen();          // create new device
333            MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);
334    
335          // add new MIDI device to the MIDI device list          // add new device to the midi device list
336          MidiInputDevices[MidiType] = pDevice;          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
337                    if (!mMidiInputDevices[i]) {
338                            mMidiInputDevices[i] = pDevice;
339                            break;
340                    }
341            }
342    
343          return pDevice;          return pDevice;
344      }      }
345    
     MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) {  
         MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);  
         return (iter != MidiInputDevices.end()) ? iter->second : NULL;  
     }  
   
346  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.123  
changed lines
  Added in v.209

  ViewVC Help
Powered by ViewVC