/[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 1424 by schoenebeck, Sun Oct 14 22:00:17 2007 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 - 2007 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    
28  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
29    
30      EngineChannel::EngineChannel() {      EngineChannel::EngineChannel() {
# Line 30  namespace LinuxSampler { namespace gig { Line 32  namespace LinuxSampler { namespace gig {
32          pEngine      = NULL;          pEngine      = NULL;
33          pInstrument  = NULL;          pInstrument  = NULL;
34          pEvents      = NULL; // we allocate when we retrieve the right Engine object          pEvents      = NULL; // we allocate when we retrieve the right Engine object
35          pEventQueue  = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue  = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
36          pActiveKeys  = new Pool<uint>(128);          pActiveKeys  = new Pool<uint>(128);
37          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
38              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 45  namespace LinuxSampler { namespace gig {
45          }          }
46          InstrumentIdx  = -1;          InstrumentIdx  = -1;
47          InstrumentStat = -1;          InstrumentStat = -1;
48            pChannelLeft  = NULL;
49            pChannelRight = NULL;
50          AudioDeviceChannelLeft  = -1;          AudioDeviceChannelLeft  = -1;
51          AudioDeviceChannelRight = -1;          AudioDeviceChannelRight = -1;
52          pMidiInputPort = NULL;          pMidiInputPort = NULL;
# Line 59  namespace LinuxSampler { namespace gig { Line 63  namespace LinuxSampler { namespace gig {
63          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
64          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
65          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
66            RemoveAllFxSends();
67      }      }
68    
69      /**      /**
# Line 156  namespace LinuxSampler { namespace gig { Line 161  namespace LinuxSampler { namespace gig {
161       * This method will then actually start to load the instrument and block       * This method will then actually start to load the instrument and block
162       * the calling thread until loading was completed.       * the calling thread until loading was completed.
163       *       *
      * @returns detailed description of the method call result  
164       * @see PrepareLoadInstrument()       * @see PrepareLoadInstrument()
165       */       */
166      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
167            ::gig::Instrument* oldInstrument = pInstrument;
         if (pEngine) pEngine->DisableAndLock();  
   
         ResetInternal();  
168    
169          // free old instrument          // free old instrument
170          if (pInstrument) {          if (oldInstrument) {
171              // give old instrument back to instrument manager              if (pEngine) {
172              Engine::instruments.HandBack(pInstrument, this);                  // make sure we don't trigger any new notes with the
173                    // old instrument
174                    ::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0);
175    
176                    // give old instrument back to instrument manager, but
177                    // keep the dimension regions and samples that are in
178                    // use
179                    Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse);
180                } else {
181                    Engine::instruments.HandBack(oldInstrument, this);
182                }
183          }          }
184    
185          // delete all key groups          // delete all key groups
186          ActiveKeyGroups.clear();          ActiveKeyGroups.clear();
187    
188          // request gig instrument from instrument manager          // request gig instrument from instrument manager
189            ::gig::Instrument* newInstrument;
190          try {          try {
191              InstrumentManager::instrument_id_t instrid;              InstrumentManager::instrument_id_t instrid;
192              instrid.FileName  = InstrumentFile;              instrid.FileName  = InstrumentFile;
193              instrid.Index     = InstrumentIdx;              instrid.Index     = InstrumentIdx;
194              pInstrument = Engine::instruments.Borrow(instrid, this);              newInstrument = Engine::instruments.Borrow(instrid, this);
195              if (!pInstrument) {              if (!newInstrument) {
196                  InstrumentStat = -1;                  throw InstrumentManagerException("resource was not created");
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
197              }              }
198          }          }
199          catch (RIFF::Exception e) {          catch (RIFF::Exception e) {
200              InstrumentStat = -2;              InstrumentStat = -2;
201                StatusChanged(true);
202              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
203              throw Exception(msg);              throw Exception(msg);
204          }          }
205          catch (InstrumentResourceManagerException e) {          catch (InstrumentManagerException e) {
206              InstrumentStat = -3;              InstrumentStat = -3;
207                StatusChanged(true);
208              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
209              throw Exception(msg);              throw Exception(msg);
210          }          }
211          catch (...) {          catch (...) {
212              InstrumentStat = -4;              InstrumentStat = -4;
213                StatusChanged(true);
214              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.");
215          }          }
216    
217          // rebuild ActiveKeyGroups map with key groups of current instrument          // rebuild ActiveKeyGroups map with key groups of current instrument
218          for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion())          for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
219              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
220    
221          InstrumentIdxName = pInstrument->pInfo->Name;          InstrumentIdxName = newInstrument->pInfo->Name;
222          InstrumentStat = 100;          InstrumentStat = 100;
223    
224          // inform audio driver for the need of two channels          if (pEngine) pEngine->ChangeInstrument(this, newInstrument);
225          try {          else pInstrument = newInstrument;
226              if (pEngine && pEngine->pAudioOutputDevice)          
227                  pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo          StatusChanged(true);
         }  
         catch (AudioOutputException e) {  
             String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message();  
             throw Exception(msg);  
         }  
   
         if (pEngine) pEngine->Enable();  
228      }      }
229    
230      /**      /**
# Line 269  namespace LinuxSampler { namespace gig { Line 275  namespace LinuxSampler { namespace gig {
275          }          }
276          AudioDeviceChannelLeft  = 0;          AudioDeviceChannelLeft  = 0;
277          AudioDeviceChannelRight = 1;          AudioDeviceChannelRight = 1;
278          pOutputLeft             = pAudioOut->Channel(0)->Buffer();          if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
279          pOutputRight            = pAudioOut->Channel(1)->Buffer();              pChannelLeft  = pAudioOut->Channel(AudioDeviceChannelLeft);
280                pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
281            } else { // use local buffers for rendering and copy later
282                // ensure the local buffers have the correct size
283                if (pChannelLeft)  delete pChannelLeft;
284                if (pChannelRight) delete pChannelRight;
285                pChannelLeft  = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
286                pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
287            }
288            if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
289          MidiInputPort::AddSysexListener(pEngine);          MidiInputPort::AddSysexListener(pEngine);
290      }      }
291    
# Line 297  namespace LinuxSampler { namespace gig { Line 312  namespace LinuxSampler { namespace gig {
312              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
313              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
314              AudioDeviceChannelRight = -1;              AudioDeviceChannelRight = -1;
315                if (!fxSends.empty()) { // free the local rendering buffers
316                    if (pChannelLeft)  delete pChannelLeft;
317                    if (pChannelRight) delete pChannelRight;
318                }
319                pChannelLeft  = NULL;
320                pChannelRight = NULL;
321          }          }
322      }      }
323    
324        AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
325            return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
326        }
327    
328      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
329          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
330    
# Line 307  namespace LinuxSampler { namespace gig { Line 332  namespace LinuxSampler { namespace gig {
332          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
333          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
334              case 0: // left output channel              case 0: // left output channel
335                  pOutputLeft = pChannel->Buffer();                  if (fxSends.empty()) pChannelLeft = pChannel;
336                  AudioDeviceChannelLeft = AudioDeviceChannel;                  AudioDeviceChannelLeft = AudioDeviceChannel;
337                  break;                  break;
338              case 1: // right output channel              case 1: // right output channel
339                  pOutputRight = pChannel->Buffer();                  if (fxSends.empty()) pChannelRight = pChannel;
340                  AudioDeviceChannelRight = AudioDeviceChannel;                  AudioDeviceChannelRight = AudioDeviceChannel;
341                  break;                  break;
342              default:              default:
343                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
344          }          }
345    
346            bStatusChanged = true;
347      }      }
348    
349      int EngineChannel::OutputChannel(uint EngineAudioChannel) {      int EngineChannel::OutputChannel(uint EngineAudioChannel) {
# Line 352  namespace LinuxSampler { namespace gig { Line 379  namespace LinuxSampler { namespace gig {
379          return midiChannel;          return midiChannel;
380      }      }
381    
382        FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
383            if (pEngine) pEngine->DisableAndLock();
384            FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
385            if (fxSends.empty()) {
386                if (pEngine && pEngine->pAudioOutputDevice) {
387                    AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
388                    // create local render buffers
389                    pChannelLeft  = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
390                    pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
391                } else {
392                    // postpone local render buffer creation until audio device is assigned
393                    pChannelLeft  = NULL;
394                    pChannelRight = NULL;
395                }
396            }
397            fxSends.push_back(pFxSend);
398            if (pEngine) pEngine->Enable();
399            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
400            
401            return pFxSend;
402        }
403    
404        FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
405            return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
406        }
407    
408        uint EngineChannel::GetFxSendCount() {
409            return fxSends.size();
410        }
411    
412        void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
413            if (pEngine) pEngine->DisableAndLock();
414            for (
415                std::vector<FxSend*>::iterator iter = fxSends.begin();
416                iter != fxSends.end(); iter++
417            ) {
418                if (*iter == pFxSend) {
419                    delete pFxSend;
420                    fxSends.erase(iter);
421                    if (fxSends.empty()) {
422                        // destroy local render buffers
423                        if (pChannelLeft)  delete pChannelLeft;
424                        if (pChannelRight) delete pChannelRight;
425                        // fallback to render directly into AudioOutputDevice's buffers
426                        if (pEngine && pEngine->pAudioOutputDevice) {
427                            pChannelLeft  = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
428                            pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
429                        } else { // we update the pointers later
430                            pChannelLeft  = NULL;
431                            pChannelRight = NULL;
432                        }
433                    }
434                    break;
435                }
436            }
437            if (pEngine) pEngine->Enable();
438            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
439        }
440    
441      /**      /**
442       *  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
443       *  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 552  namespace LinuxSampler { namespace gig { Line 638  namespace LinuxSampler { namespace gig {
638          Pitch          = 0;          Pitch          = 0;
639          SustainPedal   = false;          SustainPedal   = false;
640          SostenutoPedal = false;          SostenutoPedal = false;
641          GlobalVolume   = CONFIG_GLOBAL_ATTENUATION;          GlobalVolume   = 1.0f;
642          MidiVolume     = 1.0;          MidiVolume     = 1.0;
643          GlobalPanLeft  = 1.0f;          GlobalPanLeft  = 1.0f;
644          GlobalPanRight = 1.0f;          GlobalPanRight = 1.0f;
645            GlobalTranspose = 0;
646          // set all MIDI controller values to zero          // set all MIDI controller values to zero
647          memset(ControllerTable, 0x00, 129);          memset(ControllerTable, 0x00, 129);
648            // reset all FX Send levels
649            for (
650                std::vector<FxSend*>::iterator iter = fxSends.begin();
651                iter != fxSends.end(); iter++
652            ) {
653                (*iter)->Reset();
654            }
655      }      }
656    
657      /**      /**
# Line 574  namespace LinuxSampler { namespace gig { Line 668  namespace LinuxSampler { namespace gig {
668       *                  current audio cycle       *                  current audio cycle
669       */       */
670      void EngineChannel::ImportEvents(uint Samples) {      void EngineChannel::ImportEvents(uint Samples) {
671          RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
672          Event* pEvent;          Event* pEvent;
673          while (true) {          while (true) {
674              // get next event from input event queue              // get next event from input event queue
# Line 596  namespace LinuxSampler { namespace gig { Line 690  namespace LinuxSampler { namespace gig {
690          eventQueueReader.free(); // free all copied events from input queue          eventQueueReader.free(); // free all copied events from input queue
691      }      }
692    
693        void EngineChannel::RemoveAllFxSends() {
694            if (pEngine) pEngine->DisableAndLock();
695            if (!fxSends.empty()) { // free local render buffers
696                if (pChannelLeft) {
697                    delete pChannelLeft;
698                    if (pEngine && pEngine->pAudioOutputDevice) {
699                        // fallback to render directly to the AudioOutputDevice's buffer
700                        pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
701                    } else pChannelLeft = NULL;
702                }
703                if (pChannelRight) {
704                    delete pChannelRight;
705                    if (pEngine && pEngine->pAudioOutputDevice) {
706                        // fallback to render directly to the AudioOutputDevice's buffer
707                        pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
708                    } else pChannelRight = NULL;
709                }
710            }
711            for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
712            fxSends.clear();
713            if (pEngine) pEngine->Enable();
714        }
715    
716      float EngineChannel::Volume() {      float EngineChannel::Volume() {
717          return GlobalVolume;          return GlobalVolume;
718      }      }

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

  ViewVC Help
Powered by ViewVC