/[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 1653 by schoenebeck, Wed Jan 30 01:51:46 2008 UTC revision 1844 by persson, Mon Feb 23 18:29:50 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_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;
38          pInstrument  = NULL;          pInstrument  = NULL;
# Line 56  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 316  namespace LinuxSampler { namespace gig { Line 344  namespace LinuxSampler { namespace gig {
344      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
345          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
346    
347                ResetInternal();
348    
349              // delete the structures used for instrument change              // delete the structures used for instrument change
350              RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;              RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;
351              if (d) delete d;              if (d) delete d;
352              EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();              EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
353              d = cmd.pDimRegionsInUse;              d = cmd.pDimRegionsInUse;
354                if (d) delete d;
355    
356              if (cmd.pInstrument) {              if (cmd.pInstrument) {
357                  // release the currently loaded instrument                  // release the currently loaded instrument
358                  Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d);                  Engine::instruments.HandBack(cmd.pInstrument, this);
359              }              }
             if (d) delete d;  
360    
             // 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();  
361              if (pEvents) {              if (pEvents) {
362                  delete pEvents;                  delete pEvents;
363                  pEvents = NULL;                  pEvents = NULL;
# Line 358  namespace LinuxSampler { namespace gig { Line 372  namespace LinuxSampler { namespace gig {
372                      pMIDIKeyInfo[i].pEvents = NULL;                      pMIDIKeyInfo[i].pEvents = NULL;
373                  }                  }
374              }              }
             Engine* oldEngine = pEngine;  
375              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
376              pEngine = NULL;              pEngine = NULL;
377              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
# Line 448  namespace LinuxSampler { namespace gig { Line 461  namespace LinuxSampler { namespace gig {
461          }          }
462          fxSends.push_back(pFxSend);          fxSends.push_back(pFxSend);
463          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
464          fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());          fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
465    
466          return pFxSend;          return pFxSend;
467      }      }
# Line 487  namespace LinuxSampler { namespace gig { Line 500  namespace LinuxSampler { namespace gig {
500              }              }
501          }          }
502          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
503          fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());          fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
504      }      }
505    
506      /**      /**
# Line 508  namespace LinuxSampler { namespace gig { Line 521  namespace LinuxSampler { namespace gig {
521              event.pEngineChannel      = this;              event.pEngineChannel      = this;
522              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
523              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
524              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
525              pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
526                ArrayList<VirtualMidiDevice*>& devices =
527                    const_cast<ArrayList<VirtualMidiDevice*>&>(
528                        virtualMidiDevicesReader_MidiThread.Lock()
529                    );
530                for (int i = 0; i < devices.size(); i++) {
531                    devices[i]->SendNoteOnToDevice(Key, Velocity);
532                }
533                virtualMidiDevicesReader_MidiThread.Unlock();
534          }          }
535      }      }
536    
# Line 536  namespace LinuxSampler { namespace gig { Line 557  namespace LinuxSampler { namespace gig {
557              event.pEngineChannel      = this;              event.pEngineChannel      = this;
558              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
559              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
560              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
561              pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
562                ArrayList<VirtualMidiDevice*>& devices =
563                    const_cast<ArrayList<VirtualMidiDevice*>&>(
564                        virtualMidiDevicesReader_MidiThread.Lock()
565                    );
566                for (int i = 0; i < devices.size(); i++) {
567                    devices[i]->SendNoteOnToDevice(Key, Velocity);
568                }
569                virtualMidiDevicesReader_MidiThread.Unlock();
570          }          }
571      }      }
572    
# Line 559  namespace LinuxSampler { namespace gig { Line 588  namespace LinuxSampler { namespace gig {
588              event.pEngineChannel      = this;              event.pEngineChannel      = this;
589              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
590              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
591              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
592              pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
593                ArrayList<VirtualMidiDevice*>& devices =
594                    const_cast<ArrayList<VirtualMidiDevice*>&>(
595                        virtualMidiDevicesReader_MidiThread.Lock()
596                    );
597                for (int i = 0; i < devices.size(); i++) {
598                    devices[i]->SendNoteOffToDevice(Key, Velocity);
599                }
600                virtualMidiDevicesReader_MidiThread.Unlock();
601          }          }
602      }      }
603    
# Line 587  namespace LinuxSampler { namespace gig { Line 624  namespace LinuxSampler { namespace gig {
624              event.pEngineChannel      = this;              event.pEngineChannel      = this;
625              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
626              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
627              // inform instrument editor(s), if any ...              // inform connected virtual MIDI devices if any ...
628              pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);              // (e.g. virtual MIDI keyboard in instrument editor(s))
629                ArrayList<VirtualMidiDevice*>& devices =
630                    const_cast<ArrayList<VirtualMidiDevice*>&>(
631                        virtualMidiDevicesReader_MidiThread.Lock()
632                    );
633                for (int i = 0; i < devices.size(); i++) {
634                    devices[i]->SendNoteOffToDevice(Key, Velocity);
635                }
636                virtualMidiDevicesReader_MidiThread.Unlock();
637          }          }
638      }      }
639    
# Line 702  namespace LinuxSampler { namespace gig { Line 747  namespace LinuxSampler { namespace gig {
747          MidiVolume     = 1.0;          MidiVolume     = 1.0;
748          GlobalPanLeft  = 1.0f;          GlobalPanLeft  = 1.0f;
749          GlobalPanRight = 1.0f;          GlobalPanRight = 1.0f;
750            iLastPanRequest = 64;
751          GlobalTranspose = 0;          GlobalTranspose = 0;
752          // set all MIDI controller values to zero          // set all MIDI controller values to zero
753          memset(ControllerTable, 0x00, 129);          memset(ControllerTable, 0x00, 129);
# Line 728  namespace LinuxSampler { namespace gig { Line 774  namespace LinuxSampler { namespace gig {
774       *                  current audio cycle       *                  current audio cycle
775       */       */
776      void EngineChannel::ImportEvents(uint Samples) {      void EngineChannel::ImportEvents(uint Samples) {
777            // import events from pure software MIDI "devices"
778            // (e.g. virtual keyboard in instrument editor)
779            {
780                const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices
781                Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
782                VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device
783                // as we're going to (carefully) write some status to the
784                // synchronized struct, we cast away the const
785                ArrayList<VirtualMidiDevice*>& devices =
786                    const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock());
787                // iterate through all virtual MIDI devices
788                for (int i = 0; i < devices.size(); i++) {
789                    VirtualMidiDevice* pDev = devices[i];
790                    // I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-)
791                    while (pDev->GetMidiEventFromDevice(devEvent)) {
792                        event.Type =
793                            (devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ?
794                                Event::type_note_on : Event::type_note_off;
795                        event.Param.Note.Key      = devEvent.Key;
796                        event.Param.Note.Velocity = devEvent.Velocity;
797                        event.pEngineChannel      = this;
798                        // copy event to internal event list
799                        if (pEvents->poolIsEmpty()) {
800                            dmsg(1,("Event pool emtpy!\n"));
801                            goto exitVirtualDevicesLoop;
802                        }
803                        *pEvents->allocAppend() = event;
804                    }
805                }
806            }
807            exitVirtualDevicesLoop:
808            virtualMidiDevicesReader_AudioThread.Unlock();
809    
810            // import events from the regular MIDI devices
811          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
812          Event* pEvent;          Event* pEvent;
813          while (true) {          while (true) {
# Line 773  namespace LinuxSampler { namespace gig { Line 853  namespace LinuxSampler { namespace gig {
853          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
854      }      }
855    
856        void EngineChannel::Connect(VirtualMidiDevice* pDevice) {
857            // double buffer ... double work ...
858            {
859                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
860                devices.add(pDevice);
861            }
862            {
863                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
864                devices.add(pDevice);
865            }
866        }
867    
868        void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) {
869            // double buffer ... double work ...
870            {
871                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
872                devices.remove(pDevice);
873            }
874            {
875                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
876                devices.remove(pDevice);
877            }
878        }
879    
880      float EngineChannel::Volume() {      float EngineChannel::Volume() {
881          return GlobalVolume;          return GlobalVolume;
882      }      }
# Line 782  namespace LinuxSampler { namespace gig { Line 886  namespace LinuxSampler { namespace gig {
886          bStatusChanged = true; // status of engine channel has changed, so set notify flag          bStatusChanged = true; // status of engine channel has changed, so set notify flag
887      }      }
888    
889        float EngineChannel::Pan() {
890            return float(iLastPanRequest - 64) / 64.0f;
891        }
892    
893        void EngineChannel::Pan(float f) {
894            int iMidiPan = int(f * 64.0f) + 64;
895            if (iMidiPan > 127) iMidiPan = 127;
896            else if (iMidiPan < 0) iMidiPan = 0;
897            GlobalPanLeft  = Engine::PanCurve[128 - iMidiPan];
898            GlobalPanRight = Engine::PanCurve[iMidiPan];
899            iLastPanRequest = iMidiPan;
900        }
901    
902      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
903          return 2;          return 2;
904      }      }
# Line 806  namespace LinuxSampler { namespace gig { Line 923  namespace LinuxSampler { namespace gig {
923          return LS_GIG_ENGINE_NAME;          return LS_GIG_ENGINE_NAME;
924      }      }
925    
926        void EngineChannel::ClearDimRegionsInUse() {
927            {
928                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
929                if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear();
930            }
931            {
932                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
933                if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear();
934            }
935        }
936    
937        void EngineChannel::ResetDimRegionsInUse() {
938            {
939                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
940                if(cmd.pDimRegionsInUse != NULL) {
941                    delete cmd.pDimRegionsInUse;
942                    cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
943                }
944            }
945            {
946                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
947                if(cmd.pDimRegionsInUse != NULL) {
948                    delete cmd.pDimRegionsInUse;
949                    cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
950                }
951            }
952        }
953    
954  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

Legend:
Removed from v.1653  
changed lines
  Added in v.1844

  ViewVC Help
Powered by ViewVC