/[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 221 by schoenebeck, Fri Aug 20 17:25:19 2004 UTC revision 660 by schoenebeck, Fri Jun 17 19:49:30 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 "engines/EngineChannelFactory.h"
29  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
30  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
31  #include "engines/gig/Engine.h"  #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;
41          pMidiInputDevice   = NULL;          pMidiInputDevice   = NULL;
42          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
43          midiPort           = 0;          midiPort           = 0;
# Line 44  namespace LinuxSampler { Line 46  namespace LinuxSampler {
46      }      }
47    
48      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
49          if (pEngine) {          if (pEngineChannel) {
50              MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);              MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
51              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
52              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pEngineChannel) {
53              delete pEngine;                  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();
             default:  
                 throw LinuxSamplerException("Unknown engine type");  
         }  
68    
69          // dereference midi input port.          // dereference midi input port.
70          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
71          // disconnect old engine          // disconnect old engine
72          if (pEngine) {          if (pEngineChannel) {
73              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
74              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
75              delete pEngine;              EngineChannelFactory::Destroy(pEngineChannel);
76          }          }
77    
78          // connect new engine          // connect new engine channel
79          pEngine = pNewEngine;          pEngineChannel = pNewEngineChannel;
80          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel);          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel);
81          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);          if (pAudioOutputDevice) {
82                pNewEngineChannel->Connect(pAudioOutputDevice);
83                pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
84            }
85          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
86      }      }
87    
88      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
89          // disconnect old device          // disconnect old device
90          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice();
91          // connect new device          // connect new device
92          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
93          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
94                pEngineChannel->Connect(pAudioOutputDevice);
95                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
96            }
97      }      }
98    
99      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
# Line 105  namespace LinuxSampler { Line 112  namespace LinuxSampler {
112          // dereference old midi input port.          // dereference old midi input port.
113          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
114          // disconnect old device port          // disconnect old device port
115          if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);          if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel);
116          // new device, port and channel          // new device, port and channel
117          pMidiInputDevice = pDevice;          pMidiInputDevice = pDevice;
118          this->midiPort = iMidiPort;          this->midiPort = iMidiPort;
119          this->midiChannel = MidiChannel;          this->midiChannel = MidiChannel;
120          // connect new device port          // connect new device port
121          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
122          if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);          if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel);
123          // Ooops.          // Ooops.
124          if (pMidiInputPort == NULL)          if (pMidiInputPort == NULL)
125              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
126      }      }
127    
128      Engine* SamplerChannel::GetEngine() {      EngineChannel* SamplerChannel::GetEngineChannel() {
129          return pEngine;          return pEngineChannel;
130      }      }
131    
132      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {
# Line 181  namespace LinuxSampler { Line 188  namespace LinuxSampler {
188          if (!mSamplerChannels.size()) {          if (!mSamplerChannels.size()) {
189              SamplerChannel* pChannel = new SamplerChannel(this);              SamplerChannel* pChannel = new SamplerChannel(this);
190              mSamplerChannels[0] = pChannel;              mSamplerChannels[0] = pChannel;
191                LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));
192              return pChannel;              return pChannel;
193          }          }
194    
# Line 195  namespace LinuxSampler { Line 203  namespace LinuxSampler {
203                  // we found an unused index, so insert the new channel there                  // we found an unused index, so insert the new channel there
204                  SamplerChannel* pChannel = new SamplerChannel(this);                  SamplerChannel* pChannel = new SamplerChannel(this);
205                  mSamplerChannels[i] = pChannel;                  mSamplerChannels[i] = pChannel;
206                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));
207                  return pChannel;                  return pChannel;
208              }              }
209              throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");              throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
# Line 203  namespace LinuxSampler { Line 212  namespace LinuxSampler {
212          // we have not reached the index limit so we just add the channel past the highest index          // we have not reached the index limit so we just add the channel past the highest index
213          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
214          mSamplerChannels[lastIndex + 1] = pChannel;          mSamplerChannels[lastIndex + 1] = pChannel;
215            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));
216          return pChannel;          return pChannel;
217      }      }
218    
# Line 220  namespace LinuxSampler { Line 230  namespace LinuxSampler {
230              if (iterChan->second == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
231                  mSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
232                  delete pSamplerChannel;                  delete pSamplerChannel;
233                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));
234                  return;                  return;
235              }              }
236          }          }
# Line 308  namespace LinuxSampler { Line 319  namespace LinuxSampler {
319    
320      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
321          // create new device          // create new device
322          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
323    
324          // add new device to the midi device list          // add new device to the midi device list
325          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 324  namespace LinuxSampler { Line 335  namespace LinuxSampler {
335      void Sampler::Reset() {      void Sampler::Reset() {
336          // delete sampler channels          // delete sampler channels
337          try {          try {
338              SamplerChannelMap::iterator iter = mSamplerChannels.begin();              while (true) {
339              for (; iter != mSamplerChannels.end(); iter++) {                      SamplerChannelMap::iterator iter = mSamplerChannels.begin();
340                  RemoveSamplerChannel(iter->second);                      if (iter == mSamplerChannels.end()) break;
341                        RemoveSamplerChannel(iter->second);
342              }              }
343          }          }
344          catch(...) {          catch(...) {
# Line 336  namespace LinuxSampler { Line 348  namespace LinuxSampler {
348    
349          // delete midi input devices          // delete midi input devices
350          try {          try {
351              MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();              while (true) {
352              for (; iter != mMidiInputDevices.end(); iter++) {                      MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
353                  DestroyMidiInputDevice(iter->second);                      if (iter == mMidiInputDevices.end()) break;
354                        DestroyMidiInputDevice(iter->second);
355              }              }
356          }          }
357          catch(...) {          catch(...) {
# Line 348  namespace LinuxSampler { Line 361  namespace LinuxSampler {
361    
362          // delete audio output devices          // delete audio output devices
363          try {          try {
364              AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();              while (true) {
365              for (; iter != mAudioOutputDevices.end(); iter++) {                      AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
366                  DestroyAudioOutputDevice(iter->second);                      if (iter == mAudioOutputDevices.end()) break;
367                        DestroyAudioOutputDevice(iter->second);
368              }              }
369          }          }
370          catch(...) {          catch(...) {

Legend:
Removed from v.221  
changed lines
  Added in v.660

  ViewVC Help
Powered by ViewVC