/[svn]/linuxsampler/trunk/src/engines/gig/EngineChannel.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/gig/EngineChannel.cpp

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

revision 1659 by schoenebeck, Sun Feb 3 00:13:27 2008 UTC revision 1847 by iliev, Fri Feb 27 17:12:40 2009 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 - 2008 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2009 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 24 
24  #include "EngineChannel.h"  #include "EngineChannel.h"
25    
26  #include "../../common/global_private.h"  #include "../../common/global_private.h"
27    #include "../../Sampler.h"
28    
29  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
30    
31      EngineChannel::EngineChannel() :      EngineChannel::EngineChannel() :
32          InstrumentChangeCommandReader(InstrumentChangeCommand),          InstrumentChangeCommandReader(InstrumentChangeCommand),
33          virtualMidiDevicesReader(virtualMidiDevices)          virtualMidiDevicesReader_AudioThread(virtualMidiDevices),
34            virtualMidiDevicesReader_MidiThread(virtualMidiDevices)
35      {      {
36          pMIDIKeyInfo = new midi_key_info_t[128];          pMIDIKeyInfo = new midi_key_info_t[128];
37          pEngine      = NULL;          pEngine      = NULL;
# Line 58  namespace LinuxSampler { namespace gig { Line 60  namespace LinuxSampler { namespace gig {
60          SoloMode       = false;          SoloMode       = false;
61          PortamentoMode = false;          PortamentoMode = false;
62          PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT;          PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT;
63    
64            // reset the instrument change command struct (need to be done
65            // twice, as it is double buffered)
66            {
67                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
68                cmd.pDimRegionsInUse = NULL;
69                cmd.pInstrument = NULL;
70                cmd.bChangeInstrument = false;
71            }
72            {
73                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
74                cmd.pDimRegionsInUse = NULL;
75                cmd.pInstrument = NULL;
76                cmd.bChangeInstrument = false;
77            }
78      }      }
79    
80      EngineChannel::~EngineChannel() {      EngineChannel::~EngineChannel() {
81          DisconnectAudioOutputDevice();          DisconnectAudioOutputDevice();
82    
83            // In case the channel was removed before the instrument was
84            // fully loaded, try to give back instrument again (see bug #113)
85            instrument_change_command_t& cmd = ChangeInstrument(NULL);
86            if (cmd.pInstrument) {
87                    Engine::instruments.HandBack(cmd.pInstrument, this);
88            }
89            ///////
90    
91          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
92          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
93          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
# Line 295  namespace LinuxSampler { namespace gig { Line 321  namespace LinuxSampler { namespace gig {
321              cmd.bChangeInstrument = false;              cmd.bChangeInstrument = false;
322          }          }
323    
324            if (pInstrument != NULL) {
325                pInstrument = NULL;
326                InstrumentStat = -1;
327                InstrumentIdx  = -1;
328                InstrumentIdxName = "";
329                InstrumentFile = "";
330                bStatusChanged = true;
331            }
332    
333          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
334              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
335              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
# Line 318  namespace LinuxSampler { namespace gig { Line 353  namespace LinuxSampler { namespace gig {
353      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
354          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
355    
356                ResetInternal();
357    
358              // delete the structures used for instrument change              // delete the structures used for instrument change
359              RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;              RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;
360              if (d) delete d;              if (d) delete d;
361              EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();              EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
362              d = cmd.pDimRegionsInUse;              d = cmd.pDimRegionsInUse;
363                if (d) delete d;
364    
365              if (cmd.pInstrument) {              if (cmd.pInstrument) {
366                  // release the currently loaded instrument                  // release the currently loaded instrument
367                  Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d);                  Engine::instruments.HandBack(cmd.pInstrument, this);
368              }              }
             if (d) delete d;  
369    
             // release all active dimension regions to resource  
             // manager  
             RTList<uint>::Iterator iuiKey = pActiveKeys->first();  
             RTList<uint>::Iterator end    = pActiveKeys->end();  
             while (iuiKey != end) { // iterate through all active keys  
                 midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];  
                 ++iuiKey;  
   
                 RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();  
                 RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();  
                 for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key  
                     Engine::instruments.HandBackDimReg(itVoice->pDimRgn);  
                 }  
             }  
   
             ResetInternal();  
370              if (pEvents) {              if (pEvents) {
371                  delete pEvents;                  delete pEvents;
372                  pEvents = NULL;                  pEvents = NULL;
# Line 360  namespace LinuxSampler { namespace gig { Line 381  namespace LinuxSampler { namespace gig {
381                      pMIDIKeyInfo[i].pEvents = NULL;                      pMIDIKeyInfo[i].pEvents = NULL;
382                  }                  }
383              }              }
             Engine* oldEngine = pEngine;  
384              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
385              pEngine = NULL;              pEngine = NULL;
386              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
# Line 450  namespace LinuxSampler { namespace gig { Line 470  namespace LinuxSampler { namespace gig {
470          }          }
471          fxSends.push_back(pFxSend);          fxSends.push_back(pFxSend);
472          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
473          fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());          fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
474    
475          return pFxSend;          return pFxSend;
476      }      }
# Line 489  namespace LinuxSampler { namespace gig { Line 509  namespace LinuxSampler { namespace gig {
509              }              }
510          }          }
511          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
512          fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());          fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
513      }      }
514    
515      /**      /**
# Line 510  namespace LinuxSampler { namespace gig { Line 530  namespace LinuxSampler { namespace gig {
530              event.pEngineChannel      = this;              event.pEngineChannel      = this;
531              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
532              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
533              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
534              pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
535                ArrayList<VirtualMidiDevice*>& devices =
536                    const_cast<ArrayList<VirtualMidiDevice*>&>(
537                        virtualMidiDevicesReader_MidiThread.Lock()
538                    );
539                for (int i = 0; i < devices.size(); i++) {
540                    devices[i]->SendNoteOnToDevice(Key, Velocity);
541                }
542                virtualMidiDevicesReader_MidiThread.Unlock();
543          }          }
544      }      }
545    
# Line 538  namespace LinuxSampler { namespace gig { Line 566  namespace LinuxSampler { namespace gig {
566              event.pEngineChannel      = this;              event.pEngineChannel      = this;
567              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
568              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
569              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
570              pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
571                ArrayList<VirtualMidiDevice*>& devices =
572                    const_cast<ArrayList<VirtualMidiDevice*>&>(
573                        virtualMidiDevicesReader_MidiThread.Lock()
574                    );
575                for (int i = 0; i < devices.size(); i++) {
576                    devices[i]->SendNoteOnToDevice(Key, Velocity);
577                }
578                virtualMidiDevicesReader_MidiThread.Unlock();
579          }          }
580      }      }
581    
# Line 561  namespace LinuxSampler { namespace gig { Line 597  namespace LinuxSampler { namespace gig {
597              event.pEngineChannel      = this;              event.pEngineChannel      = this;
598              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
599              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
600              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
601              pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
602                ArrayList<VirtualMidiDevice*>& devices =
603                    const_cast<ArrayList<VirtualMidiDevice*>&>(
604                        virtualMidiDevicesReader_MidiThread.Lock()
605                    );
606                for (int i = 0; i < devices.size(); i++) {
607                    devices[i]->SendNoteOffToDevice(Key, Velocity);
608                }
609                virtualMidiDevicesReader_MidiThread.Unlock();
610          }          }
611      }      }
612    
# Line 589  namespace LinuxSampler { namespace gig { Line 633  namespace LinuxSampler { namespace gig {
633              event.pEngineChannel      = this;              event.pEngineChannel      = this;
634              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
635              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
636              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
637              pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
638                ArrayList<VirtualMidiDevice*>& devices =
639                    const_cast<ArrayList<VirtualMidiDevice*>&>(
640                        virtualMidiDevicesReader_MidiThread.Lock()
641                    );
642                for (int i = 0; i < devices.size(); i++) {
643                    devices[i]->SendNoteOffToDevice(Key, Velocity);
644                }
645                virtualMidiDevicesReader_MidiThread.Unlock();
646          }          }
647      }      }
648    
# Line 704  namespace LinuxSampler { namespace gig { Line 756  namespace LinuxSampler { namespace gig {
756          MidiVolume     = 1.0;          MidiVolume     = 1.0;
757          GlobalPanLeft  = 1.0f;          GlobalPanLeft  = 1.0f;
758          GlobalPanRight = 1.0f;          GlobalPanRight = 1.0f;
759            iLastPanRequest = 64;
760          GlobalTranspose = 0;          GlobalTranspose = 0;
761          // set all MIDI controller values to zero          // set all MIDI controller values to zero
762          memset(ControllerTable, 0x00, 129);          memset(ControllerTable, 0x00, 129);
# Line 739  namespace LinuxSampler { namespace gig { Line 792  namespace LinuxSampler { namespace gig {
792              // as we're going to (carefully) write some status to the              // as we're going to (carefully) write some status to the
793              // synchronized struct, we cast away the const              // synchronized struct, we cast away the const
794              ArrayList<VirtualMidiDevice*>& devices =              ArrayList<VirtualMidiDevice*>& devices =
795                  const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader.Lock());                  const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock());
796              // iterate through all virtual MIDI devices              // iterate through all virtual MIDI devices
797              for (int i = 0; i < devices.size(); i++) {              for (int i = 0; i < devices.size(); i++) {
798                  VirtualMidiDevice* pDev = devices[i];                  VirtualMidiDevice* pDev = devices[i];
# Line 761  namespace LinuxSampler { namespace gig { Line 814  namespace LinuxSampler { namespace gig {
814              }              }
815          }          }
816          exitVirtualDevicesLoop:          exitVirtualDevicesLoop:
817          virtualMidiDevicesReader.Unlock();          virtualMidiDevicesReader_AudioThread.Unlock();
818    
819          // import events from the regular MIDI devices          // import events from the regular MIDI devices
820          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
# Line 842  namespace LinuxSampler { namespace gig { Line 895  namespace LinuxSampler { namespace gig {
895          bStatusChanged = true; // status of engine channel has changed, so set notify flag          bStatusChanged = true; // status of engine channel has changed, so set notify flag
896      }      }
897    
898        float EngineChannel::Pan() {
899            return float(iLastPanRequest - 64) / 64.0f;
900        }
901    
902        void EngineChannel::Pan(float f) {
903            int iMidiPan = int(f * 64.0f) + 64;
904            if (iMidiPan > 127) iMidiPan = 127;
905            else if (iMidiPan < 0) iMidiPan = 0;
906            GlobalPanLeft  = Engine::PanCurve[128 - iMidiPan];
907            GlobalPanRight = Engine::PanCurve[iMidiPan];
908            iLastPanRequest = iMidiPan;
909        }
910    
911      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
912          return 2;          return 2;
913      }      }
# Line 866  namespace LinuxSampler { namespace gig { Line 932  namespace LinuxSampler { namespace gig {
932          return LS_GIG_ENGINE_NAME;          return LS_GIG_ENGINE_NAME;
933      }      }
934    
935        void EngineChannel::ClearDimRegionsInUse() {
936            {
937                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
938                if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear();
939            }
940            {
941                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
942                if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear();
943            }
944        }
945    
946        void EngineChannel::ResetDimRegionsInUse() {
947            {
948                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
949                if(cmd.pDimRegionsInUse != NULL) {
950                    delete cmd.pDimRegionsInUse;
951                    cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
952                }
953            }
954            {
955                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
956                if(cmd.pDimRegionsInUse != NULL) {
957                    delete cmd.pDimRegionsInUse;
958                    cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
959                }
960            }
961        }
962    
963  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

Legend:
Removed from v.1659  
changed lines
  Added in v.1847

  ViewVC Help
Powered by ViewVC