/[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 970 by schoenebeck, Wed Dec 6 22:28:17 2006 UTC revision 1298 by iliev, Fri Aug 17 12:55:37 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 43  namespace LinuxSampler { namespace gig { Line 43  namespace LinuxSampler { namespace gig {
43          }          }
44          InstrumentIdx  = -1;          InstrumentIdx  = -1;
45          InstrumentStat = -1;          InstrumentStat = -1;
46            pChannelLeft  = NULL;
47            pChannelRight = NULL;
48          AudioDeviceChannelLeft  = -1;          AudioDeviceChannelLeft  = -1;
49          AudioDeviceChannelRight = -1;          AudioDeviceChannelRight = -1;
50          pMidiInputPort = NULL;          pMidiInputPort = NULL;
# Line 59  namespace LinuxSampler { namespace gig { Line 61  namespace LinuxSampler { namespace gig {
61          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
62          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
63          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
64            RemoveAllFxSends();
65      }      }
66    
67      /**      /**
# Line 156  namespace LinuxSampler { namespace gig { Line 159  namespace LinuxSampler { namespace gig {
159       * This method will then actually start to load the instrument and block       * This method will then actually start to load the instrument and block
160       * the calling thread until loading was completed.       * the calling thread until loading was completed.
161       *       *
      * @returns detailed description of the method call result  
162       * @see PrepareLoadInstrument()       * @see PrepareLoadInstrument()
163       */       */
164      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
165            ::gig::Instrument* oldInstrument = pInstrument;
         if (pEngine) pEngine->DisableAndLock();  
   
         ResetInternal();  
166    
167          // free old instrument          // free old instrument
168          if (pInstrument) {          if (oldInstrument) {
169              // give old instrument back to instrument manager              if (pEngine) {
170              Engine::instruments.HandBack(pInstrument, this);                  // make sure we don't trigger any new notes with the
171                    // old instrument
172                    ::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0);
173    
174                    // give old instrument back to instrument manager, but
175                    // keep the dimension regions and samples that are in
176                    // use
177                    Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse);
178                } else {
179                    Engine::instruments.HandBack(oldInstrument, this);
180                }
181          }          }
182    
183          // delete all key groups          // delete all key groups
184          ActiveKeyGroups.clear();          ActiveKeyGroups.clear();
185    
186          // request gig instrument from instrument manager          // request gig instrument from instrument manager
187            ::gig::Instrument* newInstrument;
188          try {          try {
189              InstrumentManager::instrument_id_t instrid;              InstrumentManager::instrument_id_t instrid;
190              instrid.FileName  = InstrumentFile;              instrid.FileName  = InstrumentFile;
191              instrid.Index     = InstrumentIdx;              instrid.Index     = InstrumentIdx;
192              pInstrument = Engine::instruments.Borrow(instrid, this);              newInstrument = Engine::instruments.Borrow(instrid, this);
193              if (!pInstrument) {              if (!newInstrument) {
194                  InstrumentStat = -1;                  throw InstrumentManagerException("resource was not created");
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
195              }              }
196          }          }
197          catch (RIFF::Exception e) {          catch (RIFF::Exception e) {
# Line 191  namespace LinuxSampler { namespace gig { Line 199  namespace LinuxSampler { namespace gig {
199              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
200              throw Exception(msg);              throw Exception(msg);
201          }          }
202          catch (InstrumentResourceManagerException e) {          catch (InstrumentManagerException e) {
203              InstrumentStat = -3;              InstrumentStat = -3;
204              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
205              throw Exception(msg);              throw Exception(msg);
# Line 202  namespace LinuxSampler { namespace gig { Line 210  namespace LinuxSampler { namespace gig {
210          }          }
211    
212          // rebuild ActiveKeyGroups map with key groups of current instrument          // rebuild ActiveKeyGroups map with key groups of current instrument
213          for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion())          for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
214              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
215    
216          InstrumentIdxName = pInstrument->pInfo->Name;          InstrumentIdxName = newInstrument->pInfo->Name;
217          InstrumentStat = 100;          InstrumentStat = 100;
218    
219          // inform audio driver for the need of two channels          if (pEngine) pEngine->ChangeInstrument(this, newInstrument);
220          try {          else pInstrument = newInstrument;
221              if (pEngine && pEngine->pAudioOutputDevice)          
222                  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();  
223      }      }
224    
225      /**      /**
# Line 269  namespace LinuxSampler { namespace gig { Line 270  namespace LinuxSampler { namespace gig {
270          }          }
271          AudioDeviceChannelLeft  = 0;          AudioDeviceChannelLeft  = 0;
272          AudioDeviceChannelRight = 1;          AudioDeviceChannelRight = 1;
273          pOutputLeft             = pAudioOut->Channel(0)->Buffer();          if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
274          pOutputRight            = pAudioOut->Channel(1)->Buffer();              pChannelLeft  = pAudioOut->Channel(AudioDeviceChannelLeft);
275                pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
276            } else { // use local buffers for rendering and copy later
277                // ensure the local buffers have the correct size
278                if (pChannelLeft)  delete pChannelLeft;
279                if (pChannelRight) delete pChannelRight;
280                pChannelLeft  = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
281                pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
282            }
283            if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
284          MidiInputPort::AddSysexListener(pEngine);          MidiInputPort::AddSysexListener(pEngine);
285      }      }
286    
# Line 297  namespace LinuxSampler { namespace gig { Line 307  namespace LinuxSampler { namespace gig {
307              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
308              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
309              AudioDeviceChannelRight = -1;              AudioDeviceChannelRight = -1;
310                if (!fxSends.empty()) { // free the local rendering buffers
311                    if (pChannelLeft)  delete pChannelLeft;
312                    if (pChannelRight) delete pChannelRight;
313                }
314                pChannelLeft  = NULL;
315                pChannelRight = NULL;
316          }          }
317      }      }
318    
319        AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
320            return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
321        }
322    
323      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
324          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
325    
# Line 307  namespace LinuxSampler { namespace gig { Line 327  namespace LinuxSampler { namespace gig {
327          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
328          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
329              case 0: // left output channel              case 0: // left output channel
330                  pOutputLeft = pChannel->Buffer();                  if (fxSends.empty()) pChannelLeft = pChannel;
331                  AudioDeviceChannelLeft = AudioDeviceChannel;                  AudioDeviceChannelLeft = AudioDeviceChannel;
332                  break;                  break;
333              case 1: // right output channel              case 1: // right output channel
334                  pOutputRight = pChannel->Buffer();                  if (fxSends.empty()) pChannelRight = pChannel;
335                  AudioDeviceChannelRight = AudioDeviceChannel;                  AudioDeviceChannelRight = AudioDeviceChannel;
336                  break;                  break;
337              default:              default:
338                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
339          }          }
340    
341            bStatusChanged = true;
342      }      }
343    
344      int EngineChannel::OutputChannel(uint EngineAudioChannel) {      int EngineChannel::OutputChannel(uint EngineAudioChannel) {
# Line 352  namespace LinuxSampler { namespace gig { Line 374  namespace LinuxSampler { namespace gig {
374          return midiChannel;          return midiChannel;
375      }      }
376    
377        FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
378            if (pEngine) pEngine->DisableAndLock();
379            FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
380            if (fxSends.empty()) {
381                if (pEngine && pEngine->pAudioOutputDevice) {
382                    AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
383                    // create local render buffers
384                    pChannelLeft  = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
385                    pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
386                } else {
387                    // postpone local render buffer creation until audio device is assigned
388                    pChannelLeft  = NULL;
389                    pChannelRight = NULL;
390                }
391            }
392            fxSends.push_back(pFxSend);
393            if (pEngine) pEngine->Enable();
394            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
395            
396            return pFxSend;
397        }
398    
399        FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
400            return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
401        }
402    
403        uint EngineChannel::GetFxSendCount() {
404            return fxSends.size();
405        }
406    
407        void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
408            if (pEngine) pEngine->DisableAndLock();
409            for (
410                std::vector<FxSend*>::iterator iter = fxSends.begin();
411                iter != fxSends.end(); iter++
412            ) {
413                if (*iter == pFxSend) {
414                    delete pFxSend;
415                    fxSends.erase(iter);
416                    if (fxSends.empty()) {
417                        // destroy local render buffers
418                        if (pChannelLeft)  delete pChannelLeft;
419                        if (pChannelRight) delete pChannelRight;
420                        // fallback to render directly into AudioOutputDevice's buffers
421                        if (pEngine && pEngine->pAudioOutputDevice) {
422                            pChannelLeft  = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
423                            pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
424                        } else { // we update the pointers later
425                            pChannelLeft  = NULL;
426                            pChannelRight = NULL;
427                        }
428                    }
429                    break;
430                }
431            }
432            if (pEngine) pEngine->Enable();
433            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
434        }
435    
436      /**      /**
437       *  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
438       *  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 633  namespace LinuxSampler { namespace gig {
633          Pitch          = 0;          Pitch          = 0;
634          SustainPedal   = false;          SustainPedal   = false;
635          SostenutoPedal = false;          SostenutoPedal = false;
636          GlobalVolume   = CONFIG_GLOBAL_ATTENUATION;          GlobalVolume   = 1.0f;
637          MidiVolume     = 1.0;          MidiVolume     = 1.0;
638          GlobalPanLeft  = 1.0f;          GlobalPanLeft  = 1.0f;
639          GlobalPanRight = 1.0f;          GlobalPanRight = 1.0f;
640            GlobalTranspose = 0;
641          // set all MIDI controller values to zero          // set all MIDI controller values to zero
642          memset(ControllerTable, 0x00, 129);          memset(ControllerTable, 0x00, 129);
643            // reset all FX Send levels
644            for (
645                std::vector<FxSend*>::iterator iter = fxSends.begin();
646                iter != fxSends.end(); iter++
647            ) {
648                (*iter)->Reset();
649            }
650      }      }
651    
652      /**      /**
# Line 596  namespace LinuxSampler { namespace gig { Line 685  namespace LinuxSampler { namespace gig {
685          eventQueueReader.free(); // free all copied events from input queue          eventQueueReader.free(); // free all copied events from input queue
686      }      }
687    
688        void EngineChannel::RemoveAllFxSends() {
689            if (pEngine) pEngine->DisableAndLock();
690            if (!fxSends.empty()) { // free local render buffers
691                if (pChannelLeft) {
692                    delete pChannelLeft;
693                    if (pEngine && pEngine->pAudioOutputDevice) {
694                        // fallback to render directly to the AudioOutputDevice's buffer
695                        pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
696                    } else pChannelLeft = NULL;
697                }
698                if (pChannelRight) {
699                    delete pChannelRight;
700                    if (pEngine && pEngine->pAudioOutputDevice) {
701                        // fallback to render directly to the AudioOutputDevice's buffer
702                        pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
703                    } else pChannelRight = NULL;
704                }
705            }
706            for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
707            fxSends.clear();
708            if (pEngine) pEngine->Enable();
709        }
710    
711      float EngineChannel::Volume() {      float EngineChannel::Volume() {
712          return GlobalVolume;          return GlobalVolume;
713      }      }

Legend:
Removed from v.970  
changed lines
  Added in v.1298

  ViewVC Help
Powered by ViewVC