/[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 1212 by schoenebeck, Tue May 29 23:59:36 2007 UTC revision 1826 by iliev, Sat Jan 24 14:32:35 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 - 2007 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2008 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 23  Line 23 
23    
24  #include "EngineChannel.h"  #include "EngineChannel.h"
25    
26    #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),
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 57  namespace LinuxSampler { namespace gig { Line 64  namespace LinuxSampler { namespace gig {
64    
65      EngineChannel::~EngineChannel() {      EngineChannel::~EngineChannel() {
66          DisconnectAudioOutputDevice();          DisconnectAudioOutputDevice();
67          if (pInstrument) Engine::instruments.HandBack(pInstrument, this);  
68            // In case the channel was removed before the instrument was
69            // fully loaded, try to give back instrument again (see bug #113)
70            instrument_change_command_t& cmd = ChangeInstrument(NULL);
71            if (cmd.pInstrument) {
72                    Engine::instruments.HandBack(cmd.pInstrument, this);
73            }
74            ///////
75    
76          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
77          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
78          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
# Line 162  namespace LinuxSampler { namespace gig { Line 177  namespace LinuxSampler { namespace gig {
177       * @see PrepareLoadInstrument()       * @see PrepareLoadInstrument()
178       */       */
179      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
180          ::gig::Instrument* oldInstrument = pInstrument;          // make sure we don't trigger any new notes with an old
181            // instrument
182          // free old instrument          instrument_change_command_t& cmd = ChangeInstrument(0);
183          if (oldInstrument) {          if (cmd.pInstrument) {
184              if (pEngine) {              // give old instrument back to instrument manager, but
185                  // make sure we don't trigger any new notes with the              // keep the dimension regions and samples that are in use
186                  // old instrument              Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse);
                 ::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0);  
   
                 // give old instrument back to instrument manager, but  
                 // keep the dimension regions and samples that are in  
                 // use  
                 Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse);  
             } else {  
                 Engine::instruments.HandBack(oldInstrument, this);  
             }  
187          }          }
188            cmd.pDimRegionsInUse->clear();
189    
190          // delete all key groups          // delete all key groups
191          ActiveKeyGroups.clear();          ActiveKeyGroups.clear();
# Line 196  namespace LinuxSampler { namespace gig { Line 203  namespace LinuxSampler { namespace gig {
203          }          }
204          catch (RIFF::Exception e) {          catch (RIFF::Exception e) {
205              InstrumentStat = -2;              InstrumentStat = -2;
206                StatusChanged(true);
207              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
208              throw Exception(msg);              throw Exception(msg);
209          }          }
210          catch (InstrumentManagerException e) {          catch (InstrumentManagerException e) {
211              InstrumentStat = -3;              InstrumentStat = -3;
212                StatusChanged(true);
213              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
214              throw Exception(msg);              throw Exception(msg);
215          }          }
216          catch (...) {          catch (...) {
217              InstrumentStat = -4;              InstrumentStat = -4;
218                StatusChanged(true);
219              throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");              throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");
220          }          }
221    
# Line 216  namespace LinuxSampler { namespace gig { Line 226  namespace LinuxSampler { namespace gig {
226          InstrumentIdxName = newInstrument->pInfo->Name;          InstrumentIdxName = newInstrument->pInfo->Name;
227          InstrumentStat = 100;          InstrumentStat = 100;
228    
229          if (pEngine) pEngine->ChangeInstrument(this, newInstrument);          ChangeInstrument(newInstrument);
230          else pInstrument = newInstrument;  
231            StatusChanged(true);
232        }
233    
234    
235        /**
236         * Changes the instrument for an engine channel.
237         *
238         * @param pInstrument - new instrument
239         * @returns the resulting instrument change command after the
240         *          command switch, containing the old instrument and
241         *          the dimregions it is using
242         */
243        EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) {
244            instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
245            cmd.pInstrument = pInstrument;
246            cmd.bChangeInstrument = true;
247    
248            return InstrumentChangeCommand.SwitchConfig();
249      }      }
250    
251      /**      /**
# Line 262  namespace LinuxSampler { namespace gig { Line 290  namespace LinuxSampler { namespace gig {
290          pEngine = Engine::AcquireEngine(this, pAudioOut);          pEngine = Engine::AcquireEngine(this, pAudioOut);
291          ResetInternal();          ResetInternal();
292          pEvents = new RTList<Event>(pEngine->pEventPool);          pEvents = new RTList<Event>(pEngine->pEventPool);
293    
294            // reset the instrument change command struct (need to be done
295            // twice, as it is double buffered)
296            {
297                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
298                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
299                cmd.pInstrument = 0;
300                cmd.bChangeInstrument = false;
301            }
302            {
303                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
304                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
305                cmd.pInstrument = 0;
306                cmd.bChangeInstrument = false;
307            }
308    
309          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
310              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
311              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
# Line 284  namespace LinuxSampler { namespace gig { Line 328  namespace LinuxSampler { namespace gig {
328    
329      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
330          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
331    
332                // delete the structures used for instrument change
333                RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;
334                if (d) delete d;
335                EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
336                d = cmd.pDimRegionsInUse;
337    
338                if (cmd.pInstrument) {
339                    // release the currently loaded instrument
340                    Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d);
341                }
342    
343                if (d) delete d;
344    
345                // release all active dimension regions to resource
346                // manager
347                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
348                RTList<uint>::Iterator end    = pActiveKeys->end();
349                while (iuiKey != end) { // iterate through all active keys
350                    midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
351                    ++iuiKey;
352    
353                    RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
354                    RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
355                    for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
356                        Engine::instruments.HandBackDimReg(itVoice->pDimRgn);
357                    }
358                }
359    
360              ResetInternal();              ResetInternal();
361              if (pEvents) {              if (pEvents) {
362                  delete pEvents;                  delete pEvents;
# Line 335  namespace LinuxSampler { namespace gig { Line 408  namespace LinuxSampler { namespace gig {
408              default:              default:
409                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
410          }          }
411    
412            bStatusChanged = true;
413      }      }
414    
415      int EngineChannel::OutputChannel(uint EngineAudioChannel) {      int EngineChannel::OutputChannel(uint EngineAudioChannel) {
# Line 387  namespace LinuxSampler { namespace gig { Line 462  namespace LinuxSampler { namespace gig {
462          }          }
463          fxSends.push_back(pFxSend);          fxSends.push_back(pFxSend);
464          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
465          fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());          fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
466            
467          return pFxSend;          return pFxSend;
468      }      }
469    
# Line 426  namespace LinuxSampler { namespace gig { Line 501  namespace LinuxSampler { namespace gig {
501              }              }
502          }          }
503          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
504          fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());          fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
505      }      }
506    
507      /**      /**
# Line 447  namespace LinuxSampler { namespace gig { Line 522  namespace LinuxSampler { namespace gig {
522              event.pEngineChannel      = this;              event.pEngineChannel      = this;
523              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
524              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
525                // inform connected virtual MIDI devices if any ...
526                // (e.g. virtual MIDI keyboard in instrument editor(s))
527                ArrayList<VirtualMidiDevice*>& devices =
528                    const_cast<ArrayList<VirtualMidiDevice*>&>(
529                        virtualMidiDevicesReader_MidiThread.Lock()
530                    );
531                for (int i = 0; i < devices.size(); i++) {
532                    devices[i]->SendNoteOnToDevice(Key, Velocity);
533                }
534                virtualMidiDevicesReader_MidiThread.Unlock();
535          }          }
536      }      }
537    
# Line 473  namespace LinuxSampler { namespace gig { Line 558  namespace LinuxSampler { namespace gig {
558              event.pEngineChannel      = this;              event.pEngineChannel      = this;
559              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
560              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
561                // inform connected virtual MIDI devices if any ...
562                // (e.g. virtual MIDI keyboard in instrument editor(s))
563                ArrayList<VirtualMidiDevice*>& devices =
564                    const_cast<ArrayList<VirtualMidiDevice*>&>(
565                        virtualMidiDevicesReader_MidiThread.Lock()
566                    );
567                for (int i = 0; i < devices.size(); i++) {
568                    devices[i]->SendNoteOnToDevice(Key, Velocity);
569                }
570                virtualMidiDevicesReader_MidiThread.Unlock();
571          }          }
572      }      }
573    
# Line 494  namespace LinuxSampler { namespace gig { Line 589  namespace LinuxSampler { namespace gig {
589              event.pEngineChannel      = this;              event.pEngineChannel      = this;
590              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
591              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
592                // inform connected virtual MIDI devices if any ...
593                // (e.g. virtual MIDI keyboard in instrument editor(s))
594                ArrayList<VirtualMidiDevice*>& devices =
595                    const_cast<ArrayList<VirtualMidiDevice*>&>(
596                        virtualMidiDevicesReader_MidiThread.Lock()
597                    );
598                for (int i = 0; i < devices.size(); i++) {
599                    devices[i]->SendNoteOffToDevice(Key, Velocity);
600                }
601                virtualMidiDevicesReader_MidiThread.Unlock();
602          }          }
603      }      }
604    
# Line 520  namespace LinuxSampler { namespace gig { Line 625  namespace LinuxSampler { namespace gig {
625              event.pEngineChannel      = this;              event.pEngineChannel      = this;
626              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
627              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
628                // inform connected virtual MIDI devices if any ...
629                // (e.g. virtual MIDI keyboard in instrument editor(s))
630                ArrayList<VirtualMidiDevice*>& devices =
631                    const_cast<ArrayList<VirtualMidiDevice*>&>(
632                        virtualMidiDevicesReader_MidiThread.Lock()
633                    );
634                for (int i = 0; i < devices.size(); i++) {
635                    devices[i]->SendNoteOffToDevice(Key, Velocity);
636                }
637                virtualMidiDevicesReader_MidiThread.Unlock();
638          }          }
639      }      }
640    
# Line 633  namespace LinuxSampler { namespace gig { Line 748  namespace LinuxSampler { namespace gig {
748          MidiVolume     = 1.0;          MidiVolume     = 1.0;
749          GlobalPanLeft  = 1.0f;          GlobalPanLeft  = 1.0f;
750          GlobalPanRight = 1.0f;          GlobalPanRight = 1.0f;
751            iLastPanRequest = 64;
752          GlobalTranspose = 0;          GlobalTranspose = 0;
753          // set all MIDI controller values to zero          // set all MIDI controller values to zero
754          memset(ControllerTable, 0x00, 129);          memset(ControllerTable, 0x00, 129);
# Line 659  namespace LinuxSampler { namespace gig { Line 775  namespace LinuxSampler { namespace gig {
775       *                  current audio cycle       *                  current audio cycle
776       */       */
777      void EngineChannel::ImportEvents(uint Samples) {      void EngineChannel::ImportEvents(uint Samples) {
778            // import events from pure software MIDI "devices"
779            // (e.g. virtual keyboard in instrument editor)
780            {
781                const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices
782                Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
783                VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device
784                // as we're going to (carefully) write some status to the
785                // synchronized struct, we cast away the const
786                ArrayList<VirtualMidiDevice*>& devices =
787                    const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock());
788                // iterate through all virtual MIDI devices
789                for (int i = 0; i < devices.size(); i++) {
790                    VirtualMidiDevice* pDev = devices[i];
791                    // I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-)
792                    while (pDev->GetMidiEventFromDevice(devEvent)) {
793                        event.Type =
794                            (devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ?
795                                Event::type_note_on : Event::type_note_off;
796                        event.Param.Note.Key      = devEvent.Key;
797                        event.Param.Note.Velocity = devEvent.Velocity;
798                        event.pEngineChannel      = this;
799                        // copy event to internal event list
800                        if (pEvents->poolIsEmpty()) {
801                            dmsg(1,("Event pool emtpy!\n"));
802                            goto exitVirtualDevicesLoop;
803                        }
804                        *pEvents->allocAppend() = event;
805                    }
806                }
807            }
808            exitVirtualDevicesLoop:
809            virtualMidiDevicesReader_AudioThread.Unlock();
810    
811            // import events from the regular MIDI devices
812          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
813          Event* pEvent;          Event* pEvent;
814          while (true) {          while (true) {
# Line 696  namespace LinuxSampler { namespace gig { Line 846  namespace LinuxSampler { namespace gig {
846                  if (pEngine && pEngine->pAudioOutputDevice) {                  if (pEngine && pEngine->pAudioOutputDevice) {
847                      // fallback to render directly to the AudioOutputDevice's buffer                      // fallback to render directly to the AudioOutputDevice's buffer
848                      pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);                      pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
849                  } else pChannelRight = NULL;                  } else pChannelRight = NULL;
850              }              }
851          }          }
852          for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];          for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
# Line 704  namespace LinuxSampler { namespace gig { Line 854  namespace LinuxSampler { namespace gig {
854          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
855      }      }
856    
857        void EngineChannel::Connect(VirtualMidiDevice* pDevice) {
858            // double buffer ... double work ...
859            {
860                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
861                devices.add(pDevice);
862            }
863            {
864                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
865                devices.add(pDevice);
866            }
867        }
868    
869        void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) {
870            // double buffer ... double work ...
871            {
872                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
873                devices.remove(pDevice);
874            }
875            {
876                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
877                devices.remove(pDevice);
878            }
879        }
880    
881      float EngineChannel::Volume() {      float EngineChannel::Volume() {
882          return GlobalVolume;          return GlobalVolume;
883      }      }
# Line 713  namespace LinuxSampler { namespace gig { Line 887  namespace LinuxSampler { namespace gig {
887          bStatusChanged = true; // status of engine channel has changed, so set notify flag          bStatusChanged = true; // status of engine channel has changed, so set notify flag
888      }      }
889    
890        float EngineChannel::Pan() {
891            return float(iLastPanRequest - 64) / 64.0f;
892        }
893    
894        void EngineChannel::Pan(float f) {
895            int iMidiPan = int(f * 64.0f) + 64;
896            if (iMidiPan > 127) iMidiPan = 127;
897            else if (iMidiPan < 0) iMidiPan = 0;
898            GlobalPanLeft  = Engine::PanCurve[128 - iMidiPan];
899            GlobalPanRight = Engine::PanCurve[iMidiPan];
900            iLastPanRequest = iMidiPan;
901        }
902    
903      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
904          return 2;          return 2;
905      }      }

Legend:
Removed from v.1212  
changed lines
  Added in v.1826

  ViewVC Help
Powered by ViewVC