/[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 880 by schoenebeck, Tue Jun 27 22:57:37 2006 UTC revision 1040 by schoenebeck, Wed Feb 7 15:41:31 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 30  namespace LinuxSampler { namespace gig { Line 30  namespace LinuxSampler { namespace gig {
30          pEngine      = NULL;          pEngine      = NULL;
31          pInstrument  = NULL;          pInstrument  = NULL;
32          pEvents      = NULL; // we allocate when we retrieve the right Engine object          pEvents      = NULL; // we allocate when we retrieve the right Engine object
33          pEventQueue  = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue  = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
34          pActiveKeys  = new Pool<uint>(128);          pActiveKeys  = new Pool<uint>(128);
35          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
36              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 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 160  namespace LinuxSampler { namespace gig { Line 163  namespace LinuxSampler { namespace gig {
163       * @see PrepareLoadInstrument()       * @see PrepareLoadInstrument()
164       */       */
165      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
166            ::gig::Instrument* oldInstrument = pInstrument;
         if (pEngine) pEngine->DisableAndLock();  
   
         ResetInternal();  
167    
168          // free old instrument          // free old instrument
169          if (pInstrument) {          if (oldInstrument) {
170              // give old instrument back to instrument manager              if (pEngine) {
171              Engine::instruments.HandBack(pInstrument, this);                  // make sure we don't trigger any new notes with the
172                    // old instrument
173                    ::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0);
174    
175                    // give old instrument back to instrument manager, but
176                    // keep the dimension regions and samples that are in
177                    // use
178                    Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse);
179                } else {
180                    Engine::instruments.HandBack(oldInstrument, this);
181                }
182          }          }
183    
184          // delete all key groups          // delete all key groups
185          ActiveKeyGroups.clear();          ActiveKeyGroups.clear();
186    
187          // request gig instrument from instrument manager          // request gig instrument from instrument manager
188            ::gig::Instrument* newInstrument;
189          try {          try {
190              instrument_id_t instrid;              InstrumentManager::instrument_id_t instrid;
191              instrid.FileName    = InstrumentFile;              instrid.FileName  = InstrumentFile;
192              instrid.iInstrument = InstrumentIdx;              instrid.Index     = InstrumentIdx;
193              pInstrument = Engine::instruments.Borrow(instrid, this);              newInstrument = Engine::instruments.Borrow(instrid, this);
194              if (!pInstrument) {              if (!newInstrument) {
195                  InstrumentStat = -1;                  throw InstrumentResourceManagerException("resource was not created");
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
196              }              }
197          }          }
198          catch (RIFF::Exception e) {          catch (RIFF::Exception e) {
# Line 202  namespace LinuxSampler { namespace gig { Line 211  namespace LinuxSampler { namespace gig {
211          }          }
212    
213          // rebuild ActiveKeyGroups map with key groups of current instrument          // rebuild ActiveKeyGroups map with key groups of current instrument
214          for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion())          for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
215              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
216    
217          InstrumentIdxName = pInstrument->pInfo->Name;          InstrumentIdxName = newInstrument->pInfo->Name;
218          InstrumentStat = 100;          InstrumentStat = 100;
219    
220          // inform audio driver for the need of two channels          if (pEngine) pEngine->ChangeInstrument(this, newInstrument);
221          try {          else pInstrument = newInstrument;
             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);  
         }  
   
         if (pEngine) pEngine->Enable();  
222      }      }
223    
224      /**      /**
# Line 269  namespace LinuxSampler { namespace gig { Line 269  namespace LinuxSampler { namespace gig {
269          }          }
270          AudioDeviceChannelLeft  = 0;          AudioDeviceChannelLeft  = 0;
271          AudioDeviceChannelRight = 1;          AudioDeviceChannelRight = 1;
272          pOutputLeft             = pAudioOut->Channel(0)->Buffer();          if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
273          pOutputRight            = pAudioOut->Channel(1)->Buffer();              pChannelLeft  = pAudioOut->Channel(AudioDeviceChannelLeft);
274                pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
275            } else { // use local buffers for rendering and copy later
276                // ensure the local buffers have the correct size
277                if (pChannelLeft)  delete pChannelLeft;
278                if (pChannelRight) delete pChannelRight;
279                pChannelLeft  = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
280                pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
281            }
282            if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
283          MidiInputPort::AddSysexListener(pEngine);          MidiInputPort::AddSysexListener(pEngine);
284      }      }
285    
# Line 297  namespace LinuxSampler { namespace gig { Line 306  namespace LinuxSampler { namespace gig {
306              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
307              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
308              AudioDeviceChannelRight = -1;              AudioDeviceChannelRight = -1;
309                if (!fxSends.empty()) { // free the local rendering buffers
310                    if (pChannelLeft)  delete pChannelLeft;
311                    if (pChannelRight) delete pChannelRight;
312                }
313                pChannelLeft  = NULL;
314                pChannelRight = NULL;
315          }          }
316      }      }
317    
318        AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
319            return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
320        }
321    
322      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
323          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
324    
# Line 307  namespace LinuxSampler { namespace gig { Line 326  namespace LinuxSampler { namespace gig {
326          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
327          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
328              case 0: // left output channel              case 0: // left output channel
329                  pOutputLeft = pChannel->Buffer();                  if (fxSends.empty()) pChannelLeft = pChannel;
330                  AudioDeviceChannelLeft = AudioDeviceChannel;                  AudioDeviceChannelLeft = AudioDeviceChannel;
331                  break;                  break;
332              case 1: // right output channel              case 1: // right output channel
333                  pOutputRight = pChannel->Buffer();                  if (fxSends.empty()) pChannelRight = pChannel;
334                  AudioDeviceChannelRight = AudioDeviceChannel;                  AudioDeviceChannelRight = AudioDeviceChannel;
335                  break;                  break;
336              default:              default:
# Line 352  namespace LinuxSampler { namespace gig { Line 371  namespace LinuxSampler { namespace gig {
371          return midiChannel;          return midiChannel;
372      }      }
373    
374        FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
375            if (pEngine) pEngine->DisableAndLock();
376            FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
377            if (fxSends.empty()) {
378                if (pEngine && pEngine->pAudioOutputDevice) {
379                    AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
380                    // create local render buffers
381                    pChannelLeft  = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
382                    pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
383                } else {
384                    // postpone local render buffer creation until audio device is assigned
385                    pChannelLeft  = NULL;
386                    pChannelRight = NULL;
387                }
388            }
389            fxSends.push_back(pFxSend);
390            if (pEngine) pEngine->Enable();
391            return pFxSend;
392        }
393    
394        FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
395            return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
396        }
397    
398        uint EngineChannel::GetFxSendCount() {
399            return fxSends.size();
400        }
401    
402        void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
403            if (pEngine) pEngine->DisableAndLock();
404            for (
405                std::vector<FxSend*>::iterator iter = fxSends.begin();
406                iter != fxSends.end(); iter++
407            ) {
408                if (*iter == pFxSend) {
409                    delete pFxSend;
410                    fxSends.erase(iter);
411                    if (fxSends.empty()) {
412                        // destroy local render buffers
413                        if (pChannelLeft)  delete pChannelLeft;
414                        if (pChannelRight) delete pChannelRight;
415                        // fallback to render directly into AudioOutputDevice's buffers
416                        if (pEngine && pEngine->pAudioOutputDevice) {
417                            pChannelLeft  = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
418                            pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
419                        } else { // we update the pointers later
420                            pChannelLeft  = NULL;
421                            pChannelRight = NULL;
422                        }
423                    }
424                    break;
425                }
426            }
427            if (pEngine) pEngine->Enable();
428        }
429    
430      /**      /**
431       *  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
432       *  voice for the given key.       *  voice for the given key. This method is meant for real time rendering,
433         *  that is an event will immediately be created with the current system
434         *  time as time stamp.
435       *       *
436       *  @param Key      - MIDI key number of the triggered key       *  @param Key      - MIDI key number of the triggered key
437       *  @param Velocity - MIDI velocity value of the triggered key       *  @param Velocity - MIDI velocity value of the triggered key
# Line 372  namespace LinuxSampler { namespace gig { Line 449  namespace LinuxSampler { namespace gig {
449      }      }
450    
451      /**      /**
452         *  Will be called by the MIDIIn Thread to let the audio thread trigger a new
453         *  voice for the given key. This method is meant for offline rendering
454         *  and / or for cases where the exact position of the event in the current
455         *  audio fragment is already known.
456         *
457         *  @param Key         - MIDI key number of the triggered key
458         *  @param Velocity    - MIDI velocity value of the triggered key
459         *  @param FragmentPos - sample point position in the current audio
460         *                       fragment to which this event belongs to
461         */
462        void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
463            if (FragmentPos < 0) {
464                dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!"));
465            }
466            else if (pEngine) {
467                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
468                event.Type                = Event::type_note_on;
469                event.Param.Note.Key      = Key;
470                event.Param.Note.Velocity = Velocity;
471                event.pEngineChannel      = this;
472                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
473                else dmsg(1,("EngineChannel: Input event queue full!"));
474            }
475        }
476    
477        /**
478       *  Will be called by the MIDIIn Thread to signal the audio thread to release       *  Will be called by the MIDIIn Thread to signal the audio thread to release
479       *  voice(s) on the given key.       *  voice(s) on the given key. This method is meant for real time rendering,
480         *  that is an event will immediately be created with the current system
481         *  time as time stamp.
482       *       *
483       *  @param Key      - MIDI key number of the released key       *  @param Key      - MIDI key number of the released key
484       *  @param Velocity - MIDI release velocity value of the released key       *  @param Velocity - MIDI release velocity value of the released key
# Line 391  namespace LinuxSampler { namespace gig { Line 496  namespace LinuxSampler { namespace gig {
496      }      }
497    
498      /**      /**
499         *  Will be called by the MIDIIn Thread to signal the audio thread to release
500         *  voice(s) on the given key. This method is meant for offline rendering
501         *  and / or for cases where the exact position of the event in the current
502         *  audio fragment is already known.
503         *
504         *  @param Key         - MIDI key number of the released key
505         *  @param Velocity    - MIDI release velocity value of the released key
506         *  @param FragmentPos - sample point position in the current audio
507         *                       fragment to which this event belongs to
508         */
509        void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
510            if (FragmentPos < 0) {
511                dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!"));
512            }
513            else if (pEngine) {
514                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
515                event.Type                = Event::type_note_off;
516                event.Param.Note.Key      = Key;
517                event.Param.Note.Velocity = Velocity;
518                event.pEngineChannel      = this;
519                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
520                else dmsg(1,("EngineChannel: Input event queue full!"));
521            }
522        }
523    
524        /**
525       *  Will be called by the MIDIIn Thread to signal the audio thread to change       *  Will be called by the MIDIIn Thread to signal the audio thread to change
526       *  the pitch value for all voices.       *  the pitch value for all voices. This method is meant for real time
527         *  rendering, that is an event will immediately be created with the
528         *  current system time as time stamp.
529       *       *
530       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param Pitch - MIDI pitch value (-8192 ... +8191)
531       */       */
# Line 408  namespace LinuxSampler { namespace gig { Line 541  namespace LinuxSampler { namespace gig {
541      }      }
542    
543      /**      /**
544         *  Will be called by the MIDIIn Thread to signal the audio thread to change
545         *  the pitch value for all voices. This method is meant for offline
546         *  rendering and / or for cases where the exact position of the event in
547         *  the current audio fragment is already known.
548         *
549         *  @param Pitch       - MIDI pitch value (-8192 ... +8191)
550         *  @param FragmentPos - sample point position in the current audio
551         *                       fragment to which this event belongs to
552         */
553        void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) {
554            if (FragmentPos < 0) {
555                dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!"));
556            }
557            else if (pEngine) {
558                Event event             = pEngine->pEventGenerator->CreateEvent(FragmentPos);
559                event.Type              = Event::type_pitchbend;
560                event.Param.Pitch.Pitch = Pitch;
561                event.pEngineChannel    = this;
562                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
563                else dmsg(1,("EngineChannel: Input event queue full!"));
564            }
565        }
566    
567        /**
568       *  Will be called by the MIDIIn Thread to signal the audio thread that a       *  Will be called by the MIDIIn Thread to signal the audio thread that a
569       *  continuous controller value has changed.       *  continuous controller value has changed. This method is meant for real
570         *  time rendering, that is an event will immediately be created with the
571         *  current system time as time stamp.
572       *       *
573       *  @param Controller - MIDI controller number of the occured control change       *  @param Controller - MIDI controller number of the occured control change
574       *  @param Value      - value of the control change       *  @param Value      - value of the control change
# Line 426  namespace LinuxSampler { namespace gig { Line 585  namespace LinuxSampler { namespace gig {
585          }          }
586      }      }
587    
588        /**
589         *  Will be called by the MIDIIn Thread to signal the audio thread that a
590         *  continuous controller value has changed. This method is meant for
591         *  offline rendering and / or for cases where the exact position of the
592         *  event in the current audio fragment is already known.
593         *
594         *  @param Controller  - MIDI controller number of the occured control change
595         *  @param Value       - value of the control change
596         *  @param FragmentPos - sample point position in the current audio
597         *                       fragment to which this event belongs to
598         */
599        void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) {
600            if (FragmentPos < 0) {
601                dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!"));
602            }
603            else if (pEngine) {
604                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
605                event.Type                = Event::type_control_change;
606                event.Param.CC.Controller = Controller;
607                event.Param.CC.Value      = Value;
608                event.pEngineChannel      = this;
609                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
610                else dmsg(1,("EngineChannel: Input event queue full!"));
611            }
612        }
613    
614      void EngineChannel::ClearEventLists() {      void EngineChannel::ClearEventLists() {
615          pEvents->clear();          pEvents->clear();
616          // empty MIDI key specific event lists          // empty MIDI key specific event lists
# Line 442  namespace LinuxSampler { namespace gig { Line 627  namespace LinuxSampler { namespace gig {
627          Pitch          = 0;          Pitch          = 0;
628          SustainPedal   = false;          SustainPedal   = false;
629          SostenutoPedal = false;          SostenutoPedal = false;
630          GlobalVolume   = CONFIG_GLOBAL_ATTENUATION;          GlobalVolume   = 1.0f;
631            MidiVolume     = 1.0;
632          GlobalPanLeft  = 1.0f;          GlobalPanLeft  = 1.0f;
633          GlobalPanRight = 1.0f;          GlobalPanRight = 1.0f;
634          // set all MIDI controller values to zero          // set all MIDI controller values to zero
635          memset(ControllerTable, 0x00, 128);          memset(ControllerTable, 0x00, 129);
636            // reset all FX Send levels
637            for (
638                std::vector<FxSend*>::iterator iter = fxSends.begin();
639                iter != fxSends.end(); iter++
640            ) {
641                (*iter)->Reset();
642            }
643      }      }
644    
645      /**      /**
# Line 463  namespace LinuxSampler { namespace gig { Line 656  namespace LinuxSampler { namespace gig {
656       *                  current audio cycle       *                  current audio cycle
657       */       */
658      void EngineChannel::ImportEvents(uint Samples) {      void EngineChannel::ImportEvents(uint Samples) {
659          RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
660          Event* pEvent;          Event* pEvent;
661          while (true) {          while (true) {
662              // get next event from input event queue              // get next event from input event queue
# Line 485  namespace LinuxSampler { namespace gig { Line 678  namespace LinuxSampler { namespace gig {
678          eventQueueReader.free(); // free all copied events from input queue          eventQueueReader.free(); // free all copied events from input queue
679      }      }
680    
681        void EngineChannel::RemoveAllFxSends() {
682            if (pEngine) pEngine->DisableAndLock();
683            if (!fxSends.empty()) { // free local render buffers
684                if (pChannelLeft) {
685                    delete pChannelLeft;
686                    if (pEngine && pEngine->pAudioOutputDevice) {
687                        // fallback to render directly to the AudioOutputDevice's buffer
688                        pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
689                    } else pChannelLeft = NULL;
690                }
691                if (pChannelRight) {
692                    delete pChannelRight;
693                    if (pEngine && pEngine->pAudioOutputDevice) {
694                        // fallback to render directly to the AudioOutputDevice's buffer
695                        pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
696                    } else pChannelRight = NULL;
697                }
698            }
699            for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
700            fxSends.clear();
701            if (pEngine) pEngine->Enable();
702        }
703    
704      float EngineChannel::Volume() {      float EngineChannel::Volume() {
705          return GlobalVolume;          return GlobalVolume;
706      }      }

Legend:
Removed from v.880  
changed lines
  Added in v.1040

  ViewVC Help
Powered by ViewVC