/[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 947 by schoenebeck, Mon Nov 27 21:34:55 2006 UTC revision 1843 by iliev, Sat Feb 21 17:08:18 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, 2006 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;
39          pEvents      = NULL; // we allocate when we retrieve the right Engine object          pEvents      = NULL; // we allocate when we retrieve the right Engine object
40          pEventQueue  = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue  = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
41          pActiveKeys  = new Pool<uint>(128);          pActiveKeys  = new Pool<uint>(128);
42          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
43              pMIDIKeyInfo[i].pActiveVoices  = NULL; // we allocate when we retrieve the right Engine object              pMIDIKeyInfo[i].pActiveVoices  = NULL; // we allocate when we retrieve the right Engine object
# Line 43  namespace LinuxSampler { namespace gig { Line 50  namespace LinuxSampler { namespace gig {
50          }          }
51          InstrumentIdx  = -1;          InstrumentIdx  = -1;
52          InstrumentStat = -1;          InstrumentStat = -1;
53            pChannelLeft  = NULL;
54            pChannelRight = NULL;
55          AudioDeviceChannelLeft  = -1;          AudioDeviceChannelLeft  = -1;
56          AudioDeviceChannelRight = -1;          AudioDeviceChannelRight = -1;
57          pMidiInputPort = NULL;          pMidiInputPort = NULL;
# Line 51  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          if (pInstrument) Engine::instruments.HandBack(pInstrument, this);  
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;
94            RemoveAllFxSends();
95      }      }
96    
97      /**      /**
# Line 156  namespace LinuxSampler { namespace gig { Line 189  namespace LinuxSampler { namespace gig {
189       * This method will then actually start to load the instrument and block       * This method will then actually start to load the instrument and block
190       * the calling thread until loading was completed.       * the calling thread until loading was completed.
191       *       *
      * @returns detailed description of the method call result  
192       * @see PrepareLoadInstrument()       * @see PrepareLoadInstrument()
193       */       */
194      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
195            // make sure we don't trigger any new notes with an old
196          if (pEngine) pEngine->DisableAndLock();          // instrument
197            instrument_change_command_t& cmd = ChangeInstrument(0);
198          ResetInternal();          if (cmd.pInstrument) {
199                // give old instrument back to instrument manager, but
200          // free old instrument              // keep the dimension regions and samples that are in use
201          if (pInstrument) {              Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse);
             // give old instrument back to instrument manager  
             Engine::instruments.HandBack(pInstrument, this);  
202          }          }
203            cmd.pDimRegionsInUse->clear();
204    
205          // delete all key groups          // delete all key groups
206          ActiveKeyGroups.clear();          ActiveKeyGroups.clear();
207    
208          // request gig instrument from instrument manager          // request gig instrument from instrument manager
209            ::gig::Instrument* newInstrument;
210          try {          try {
211              InstrumentManager::instrument_id_t instrid;              InstrumentManager::instrument_id_t instrid;
212              instrid.FileName  = InstrumentFile;              instrid.FileName  = InstrumentFile;
213              instrid.Index     = InstrumentIdx;              instrid.Index     = InstrumentIdx;
214              pInstrument = Engine::instruments.Borrow(instrid, this);              newInstrument = Engine::instruments.Borrow(instrid, this);
215              if (!pInstrument) {              if (!newInstrument) {
216                  InstrumentStat = -1;                  throw InstrumentManagerException("resource was not created");
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
217              }              }
218          }          }
219          catch (RIFF::Exception e) {          catch (RIFF::Exception e) {
220              InstrumentStat = -2;              InstrumentStat = -2;
221                StatusChanged(true);
222              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
223              throw Exception(msg);              throw Exception(msg);
224          }          }
225          catch (InstrumentResourceManagerException e) {          catch (InstrumentManagerException e) {
226              InstrumentStat = -3;              InstrumentStat = -3;
227                StatusChanged(true);
228              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
229              throw Exception(msg);              throw Exception(msg);
230          }          }
231          catch (...) {          catch (...) {
232              InstrumentStat = -4;              InstrumentStat = -4;
233                StatusChanged(true);
234              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.");
235          }          }
236    
237          // rebuild ActiveKeyGroups map with key groups of current instrument          // rebuild ActiveKeyGroups map with key groups of current instrument
238          for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion())          for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
239              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
240    
241          InstrumentIdxName = pInstrument->pInfo->Name;          InstrumentIdxName = newInstrument->pInfo->Name;
242          InstrumentStat = 100;          InstrumentStat = 100;
243    
244          // inform audio driver for the need of two channels          ChangeInstrument(newInstrument);
         try {  
             if (pEngine && pEngine->pAudioOutputDevice)  
                 pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo  
         }  
         catch (AudioOutputException e) {  
             String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message();  
             throw Exception(msg);  
         }  
245    
246          if (pEngine) pEngine->Enable();          StatusChanged(true);
247        }
248    
249    
250        /**
251         * Changes the instrument for an engine channel.
252         *
253         * @param pInstrument - new instrument
254         * @returns the resulting instrument change command after the
255         *          command switch, containing the old instrument and
256         *          the dimregions it is using
257         */
258        EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) {
259            instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
260            cmd.pInstrument = pInstrument;
261            cmd.bChangeInstrument = true;
262    
263            return InstrumentChangeCommand.SwitchConfig();
264      }      }
265    
266      /**      /**
# Line 263  namespace LinuxSampler { namespace gig { Line 305  namespace LinuxSampler { namespace gig {
305          pEngine = Engine::AcquireEngine(this, pAudioOut);          pEngine = Engine::AcquireEngine(this, pAudioOut);
306          ResetInternal();          ResetInternal();
307          pEvents = new RTList<Event>(pEngine->pEventPool);          pEvents = new RTList<Event>(pEngine->pEventPool);
308    
309            // reset the instrument change command struct (need to be done
310            // twice, as it is double buffered)
311            {
312                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
313                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
314                cmd.pInstrument = 0;
315                cmd.bChangeInstrument = false;
316            }
317            {
318                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
319                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
320                cmd.pInstrument = 0;
321                cmd.bChangeInstrument = false;
322            }
323    
324          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
325              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
326              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
327          }          }
328          AudioDeviceChannelLeft  = 0;          AudioDeviceChannelLeft  = 0;
329          AudioDeviceChannelRight = 1;          AudioDeviceChannelRight = 1;
330          pOutputLeft             = pAudioOut->Channel(0)->Buffer();          if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
331          pOutputRight            = pAudioOut->Channel(1)->Buffer();              pChannelLeft  = pAudioOut->Channel(AudioDeviceChannelLeft);
332                pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
333            } else { // use local buffers for rendering and copy later
334                // ensure the local buffers have the correct size
335                if (pChannelLeft)  delete pChannelLeft;
336                if (pChannelRight) delete pChannelRight;
337                pChannelLeft  = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
338                pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
339            }
340            if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
341          MidiInputPort::AddSysexListener(pEngine);          MidiInputPort::AddSysexListener(pEngine);
342      }      }
343    
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                // delete the structures used for instrument change
348                RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;
349                if (d) delete d;
350                EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
351                d = cmd.pDimRegionsInUse;
352    
353                if (cmd.pInstrument) {
354                    // release the currently loaded instrument
355                    Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d);
356                }
357    
358                if (d) delete d;
359    
360                // release all active dimension regions to resource
361                // manager
362                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
363                RTList<uint>::Iterator end    = pActiveKeys->end();
364                while (iuiKey != end) { // iterate through all active keys
365                    midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
366                    ++iuiKey;
367    
368                    RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
369                    RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
370                    for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
371                        Engine::instruments.HandBackDimReg(itVoice->pDimRgn);
372                    }
373                }
374    
375              ResetInternal();              ResetInternal();
376              if (pEvents) {              if (pEvents) {
377                  delete pEvents;                  delete pEvents;
# Line 297  namespace LinuxSampler { namespace gig { Line 393  namespace LinuxSampler { namespace gig {
393              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
394              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
395              AudioDeviceChannelRight = -1;              AudioDeviceChannelRight = -1;
396                if (!fxSends.empty()) { // free the local rendering buffers
397                    if (pChannelLeft)  delete pChannelLeft;
398                    if (pChannelRight) delete pChannelRight;
399                }
400                pChannelLeft  = NULL;
401                pChannelRight = NULL;
402          }          }
403      }      }
404    
405        AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
406            return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
407        }
408    
409      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
410          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
411    
# Line 307  namespace LinuxSampler { namespace gig { Line 413  namespace LinuxSampler { namespace gig {
413          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
414          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
415              case 0: // left output channel              case 0: // left output channel
416                  pOutputLeft = pChannel->Buffer();                  if (fxSends.empty()) pChannelLeft = pChannel;
417                  AudioDeviceChannelLeft = AudioDeviceChannel;                  AudioDeviceChannelLeft = AudioDeviceChannel;
418                  break;                  break;
419              case 1: // right output channel              case 1: // right output channel
420                  pOutputRight = pChannel->Buffer();                  if (fxSends.empty()) pChannelRight = pChannel;
421                  AudioDeviceChannelRight = AudioDeviceChannel;                  AudioDeviceChannelRight = AudioDeviceChannel;
422                  break;                  break;
423              default:              default:
424                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
425          }          }
426    
427            bStatusChanged = true;
428      }      }
429    
430      int EngineChannel::OutputChannel(uint EngineAudioChannel) {      int EngineChannel::OutputChannel(uint EngineAudioChannel) {
# Line 352  namespace LinuxSampler { namespace gig { Line 460  namespace LinuxSampler { namespace gig {
460          return midiChannel;          return midiChannel;
461      }      }
462    
463        FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
464            if (pEngine) pEngine->DisableAndLock();
465            FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
466            if (fxSends.empty()) {
467                if (pEngine && pEngine->pAudioOutputDevice) {
468                    AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
469                    // create local render buffers
470                    pChannelLeft  = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
471                    pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
472                } else {
473                    // postpone local render buffer creation until audio device is assigned
474                    pChannelLeft  = NULL;
475                    pChannelRight = NULL;
476                }
477            }
478            fxSends.push_back(pFxSend);
479            if (pEngine) pEngine->Enable();
480            fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
481    
482            return pFxSend;
483        }
484    
485        FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
486            return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
487        }
488    
489        uint EngineChannel::GetFxSendCount() {
490            return fxSends.size();
491        }
492    
493        void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
494            if (pEngine) pEngine->DisableAndLock();
495            for (
496                std::vector<FxSend*>::iterator iter = fxSends.begin();
497                iter != fxSends.end(); iter++
498            ) {
499                if (*iter == pFxSend) {
500                    delete pFxSend;
501                    fxSends.erase(iter);
502                    if (fxSends.empty()) {
503                        // destroy local render buffers
504                        if (pChannelLeft)  delete pChannelLeft;
505                        if (pChannelRight) delete pChannelRight;
506                        // fallback to render directly into AudioOutputDevice's buffers
507                        if (pEngine && pEngine->pAudioOutputDevice) {
508                            pChannelLeft  = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
509                            pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
510                        } else { // we update the pointers later
511                            pChannelLeft  = NULL;
512                            pChannelRight = NULL;
513                        }
514                    }
515                    break;
516                }
517            }
518            if (pEngine) pEngine->Enable();
519            fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
520        }
521    
522      /**      /**
523       *  Will be called by the MIDIIn Thread to let the audio thread trigger a new       *  Will be called by the MIDIIn Thread to let the audio thread trigger a new
524       *  voice for the given key. This method is meant for real time rendering,       *  voice for the given key. This method is meant for real time rendering,
# Line 370  namespace LinuxSampler { namespace gig { Line 537  namespace LinuxSampler { namespace gig {
537              event.pEngineChannel      = this;              event.pEngineChannel      = this;
538              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
539              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
540                // inform connected virtual MIDI devices if any ...
541                // (e.g. virtual MIDI keyboard in instrument editor(s))
542                ArrayList<VirtualMidiDevice*>& devices =
543                    const_cast<ArrayList<VirtualMidiDevice*>&>(
544                        virtualMidiDevicesReader_MidiThread.Lock()
545                    );
546                for (int i = 0; i < devices.size(); i++) {
547                    devices[i]->SendNoteOnToDevice(Key, Velocity);
548                }
549                virtualMidiDevicesReader_MidiThread.Unlock();
550          }          }
551      }      }
552    
# Line 396  namespace LinuxSampler { namespace gig { Line 573  namespace LinuxSampler { namespace gig {
573              event.pEngineChannel      = this;              event.pEngineChannel      = this;
574              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
575              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
576                // inform connected virtual MIDI devices if any ...
577                // (e.g. virtual MIDI keyboard in instrument editor(s))
578                ArrayList<VirtualMidiDevice*>& devices =
579                    const_cast<ArrayList<VirtualMidiDevice*>&>(
580                        virtualMidiDevicesReader_MidiThread.Lock()
581                    );
582                for (int i = 0; i < devices.size(); i++) {
583                    devices[i]->SendNoteOnToDevice(Key, Velocity);
584                }
585                virtualMidiDevicesReader_MidiThread.Unlock();
586          }          }
587      }      }
588    
# Line 417  namespace LinuxSampler { namespace gig { Line 604  namespace LinuxSampler { namespace gig {
604              event.pEngineChannel      = this;              event.pEngineChannel      = this;
605              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
606              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
607                // inform connected virtual MIDI devices if any ...
608                // (e.g. virtual MIDI keyboard in instrument editor(s))
609                ArrayList<VirtualMidiDevice*>& devices =
610                    const_cast<ArrayList<VirtualMidiDevice*>&>(
611                        virtualMidiDevicesReader_MidiThread.Lock()
612                    );
613                for (int i = 0; i < devices.size(); i++) {
614                    devices[i]->SendNoteOffToDevice(Key, Velocity);
615                }
616                virtualMidiDevicesReader_MidiThread.Unlock();
617          }          }
618      }      }
619    
# Line 443  namespace LinuxSampler { namespace gig { Line 640  namespace LinuxSampler { namespace gig {
640              event.pEngineChannel      = this;              event.pEngineChannel      = this;
641              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
642              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
643                // inform connected virtual MIDI devices if any ...
644                // (e.g. virtual MIDI keyboard in instrument editor(s))
645                ArrayList<VirtualMidiDevice*>& devices =
646                    const_cast<ArrayList<VirtualMidiDevice*>&>(
647                        virtualMidiDevicesReader_MidiThread.Lock()
648                    );
649                for (int i = 0; i < devices.size(); i++) {
650                    devices[i]->SendNoteOffToDevice(Key, Velocity);
651                }
652                virtualMidiDevicesReader_MidiThread.Unlock();
653          }          }
654      }      }
655    
# Line 552  namespace LinuxSampler { namespace gig { Line 759  namespace LinuxSampler { namespace gig {
759          Pitch          = 0;          Pitch          = 0;
760          SustainPedal   = false;          SustainPedal   = false;
761          SostenutoPedal = false;          SostenutoPedal = false;
762          GlobalVolume   = CONFIG_GLOBAL_ATTENUATION;          GlobalVolume   = 1.0f;
763          MidiVolume     = 1.0;          MidiVolume     = 1.0;
764          GlobalPanLeft  = 1.0f;          GlobalPanLeft  = 1.0f;
765          GlobalPanRight = 1.0f;          GlobalPanRight = 1.0f;
766            iLastPanRequest = 64;
767            GlobalTranspose = 0;
768          // set all MIDI controller values to zero          // set all MIDI controller values to zero
769          memset(ControllerTable, 0x00, 129);          memset(ControllerTable, 0x00, 129);
770            // reset all FX Send levels
771            for (
772                std::vector<FxSend*>::iterator iter = fxSends.begin();
773                iter != fxSends.end(); iter++
774            ) {
775                (*iter)->Reset();
776            }
777      }      }
778    
779      /**      /**
# Line 574  namespace LinuxSampler { namespace gig { Line 790  namespace LinuxSampler { namespace gig {
790       *                  current audio cycle       *                  current audio cycle
791       */       */
792      void EngineChannel::ImportEvents(uint Samples) {      void EngineChannel::ImportEvents(uint Samples) {
793          RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          // import events from pure software MIDI "devices"
794            // (e.g. virtual keyboard in instrument editor)
795            {
796                const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices
797                Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
798                VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device
799                // as we're going to (carefully) write some status to the
800                // synchronized struct, we cast away the const
801                ArrayList<VirtualMidiDevice*>& devices =
802                    const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock());
803                // iterate through all virtual MIDI devices
804                for (int i = 0; i < devices.size(); i++) {
805                    VirtualMidiDevice* pDev = devices[i];
806                    // I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-)
807                    while (pDev->GetMidiEventFromDevice(devEvent)) {
808                        event.Type =
809                            (devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ?
810                                Event::type_note_on : Event::type_note_off;
811                        event.Param.Note.Key      = devEvent.Key;
812                        event.Param.Note.Velocity = devEvent.Velocity;
813                        event.pEngineChannel      = this;
814                        // copy event to internal event list
815                        if (pEvents->poolIsEmpty()) {
816                            dmsg(1,("Event pool emtpy!\n"));
817                            goto exitVirtualDevicesLoop;
818                        }
819                        *pEvents->allocAppend() = event;
820                    }
821                }
822            }
823            exitVirtualDevicesLoop:
824            virtualMidiDevicesReader_AudioThread.Unlock();
825    
826            // import events from the regular MIDI devices
827            RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
828          Event* pEvent;          Event* pEvent;
829          while (true) {          while (true) {
830              // get next event from input event queue              // get next event from input event queue
# Line 596  namespace LinuxSampler { namespace gig { Line 846  namespace LinuxSampler { namespace gig {
846          eventQueueReader.free(); // free all copied events from input queue          eventQueueReader.free(); // free all copied events from input queue
847      }      }
848    
849        void EngineChannel::RemoveAllFxSends() {
850            if (pEngine) pEngine->DisableAndLock();
851            if (!fxSends.empty()) { // free local render buffers
852                if (pChannelLeft) {
853                    delete pChannelLeft;
854                    if (pEngine && pEngine->pAudioOutputDevice) {
855                        // fallback to render directly to the AudioOutputDevice's buffer
856                        pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
857                    } else pChannelLeft = NULL;
858                }
859                if (pChannelRight) {
860                    delete pChannelRight;
861                    if (pEngine && pEngine->pAudioOutputDevice) {
862                        // fallback to render directly to the AudioOutputDevice's buffer
863                        pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
864                    } else pChannelRight = NULL;
865                }
866            }
867            for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
868            fxSends.clear();
869            if (pEngine) pEngine->Enable();
870        }
871    
872        void EngineChannel::Connect(VirtualMidiDevice* pDevice) {
873            // double buffer ... double work ...
874            {
875                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
876                devices.add(pDevice);
877            }
878            {
879                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
880                devices.add(pDevice);
881            }
882        }
883    
884        void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) {
885            // double buffer ... double work ...
886            {
887                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
888                devices.remove(pDevice);
889            }
890            {
891                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
892                devices.remove(pDevice);
893            }
894        }
895    
896      float EngineChannel::Volume() {      float EngineChannel::Volume() {
897          return GlobalVolume;          return GlobalVolume;
898      }      }
# Line 605  namespace LinuxSampler { namespace gig { Line 902  namespace LinuxSampler { namespace gig {
902          bStatusChanged = true; // status of engine channel has changed, so set notify flag          bStatusChanged = true; // status of engine channel has changed, so set notify flag
903      }      }
904    
905        float EngineChannel::Pan() {
906            return float(iLastPanRequest - 64) / 64.0f;
907        }
908    
909        void EngineChannel::Pan(float f) {
910            int iMidiPan = int(f * 64.0f) + 64;
911            if (iMidiPan > 127) iMidiPan = 127;
912            else if (iMidiPan < 0) iMidiPan = 0;
913            GlobalPanLeft  = Engine::PanCurve[128 - iMidiPan];
914            GlobalPanRight = Engine::PanCurve[iMidiPan];
915            iLastPanRequest = iMidiPan;
916        }
917    
918      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
919          return 2;          return 2;
920      }      }
# Line 629  namespace LinuxSampler { namespace gig { Line 939  namespace LinuxSampler { namespace gig {
939          return LS_GIG_ENGINE_NAME;          return LS_GIG_ENGINE_NAME;
940      }      }
941    
942        void EngineChannel::ClearDimRegionsInUse() {
943            {
944                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
945                if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear();
946            }
947            {
948                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
949                if(cmd.pDimRegionsInUse != NULL) cmd.pDimRegionsInUse->clear();
950            }
951        }
952    
953        void EngineChannel::ResetDimRegionsInUse() {
954            {
955                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
956                if(cmd.pDimRegionsInUse != NULL) {
957                    delete cmd.pDimRegionsInUse;
958                    cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
959                }
960            }
961            {
962                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
963                if(cmd.pDimRegionsInUse != NULL) {
964                    delete cmd.pDimRegionsInUse;
965                    cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
966                }
967            }
968        }
969    
970  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

Legend:
Removed from v.947  
changed lines
  Added in v.1843

  ViewVC Help
Powered by ViewVC