/[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 554 by schoenebeck, Thu May 19 19:25:14 2005 UTC revision 1646 by persson, Sun Jan 20 15:04:51 2008 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 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    
28  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
29    
30      EngineChannel::EngineChannel() {      EngineChannel::EngineChannel() :
31            InstrumentChangeCommandReader(InstrumentChangeCommand) {
32          pMIDIKeyInfo = new midi_key_info_t[128];          pMIDIKeyInfo = new midi_key_info_t[128];
33          pEngine      = NULL;          pEngine      = NULL;
34          pInstrument  = NULL;          pInstrument  = NULL;
35          pEvents      = NULL; // we allocate when we retrieve the right Engine object          pEvents      = NULL; // we allocate when we retrieve the right Engine object
36          pCCEvents    = NULL; // we allocate when we retrieve the right Engine object          pEventQueue  = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
         pEventQueue  = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);  
37          pActiveKeys  = new Pool<uint>(128);          pActiveKeys  = new Pool<uint>(128);
38          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
39              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 42  namespace LinuxSampler { namespace gig { Line 44  namespace LinuxSampler { namespace gig {
44              pMIDIKeyInfo[i].VoiceTheftsQueued = 0;              pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
45              pMIDIKeyInfo[i].RoundRobinIndex = 0;              pMIDIKeyInfo[i].RoundRobinIndex = 0;
46          }          }
         for (uint i = 0; i < Event::destination_count; i++) {  
             pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object  
         }  
47          InstrumentIdx  = -1;          InstrumentIdx  = -1;
48          InstrumentStat = -1;          InstrumentStat = -1;
49            pChannelLeft  = NULL;
50            pChannelRight = NULL;
51          AudioDeviceChannelLeft  = -1;          AudioDeviceChannelLeft  = -1;
52          AudioDeviceChannelRight = -1;          AudioDeviceChannelRight = -1;
53            pMidiInputPort = NULL;
54            midiChannel = midi_chan_all;
55            ResetControllers();
56            SoloMode       = false;
57            PortamentoMode = false;
58            PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT;
59      }      }
60    
61      EngineChannel::~EngineChannel() {      EngineChannel::~EngineChannel() {
62          DisconnectAudioOutputDevice();          DisconnectAudioOutputDevice();
         if (pInstrument) Engine::instruments.HandBack(pInstrument, this);  
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        /**
70         * Implementation of virtual method from abstract EngineChannel interface.
71         * This method will periodically be polled (e.g. by the LSCP server) to
72         * check if some engine channel parameter has changed since the last
73         * StatusChanged() call.
74         *
75         * This method can also be used to mark the engine channel as changed
76         * from outside, e.g. by a MIDI input device. The optional argument
77         * \a nNewStatus can be used for this.
78         *
79         * TODO: This "poll method" is just a lazy solution and might be
80         *       replaced in future.
81         * @param bNewStatus - (optional, default: false) sets the new status flag
82         * @returns true if engine channel status has changed since last
83         *          StatusChanged() call
84         */
85        bool EngineChannel::StatusChanged(bool bNewStatus) {
86            bool b = bStatusChanged;
87            bStatusChanged = bNewStatus;
88            return b;
89        }
90    
91        void EngineChannel::Reset() {
92            if (pEngine) pEngine->DisableAndLock();
93            ResetInternal();
94            ResetControllers();
95            if (pEngine) {
96                pEngine->Enable();
97                pEngine->Reset();
98            }
99      }      }
100    
101      /**      /**
102       * This method is not thread safe!       * This method is not thread safe!
103       */       */
104      void EngineChannel::ResetInternal() {      void EngineChannel::ResetInternal() {
         Pitch               = 0;  
         SustainPedal        = false;  
         GlobalVolume        = 1.0;  
         GlobalPanLeft       = 1.0f;  
         GlobalPanRight      = 1.0f;  
105          CurrentKeyDimension = 0;          CurrentKeyDimension = 0;
106    
         ResetControllers();  
   
107          // reset key info          // reset key info
108          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
109              if (pMIDIKeyInfo[i].pActiveVoices)              if (pMIDIKeyInfo[i].pActiveVoices)
# Line 84  namespace LinuxSampler { namespace gig { Line 116  namespace LinuxSampler { namespace gig {
116              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();
117              pMIDIKeyInfo[i].VoiceTheftsQueued = 0;              pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
118          }          }
119            SoloKey       = -1;    // no solo key active yet
120            PortamentoPos = -1.0f; // no portamento active yet
121    
122          // reset all key groups          // reset all key groups
123          std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();          std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();
# Line 96  namespace LinuxSampler { namespace gig { Line 130  namespace LinuxSampler { namespace gig {
130          pEventQueue->init();          pEventQueue->init();
131    
132          if (pEngine) pEngine->ResetInternal();          if (pEngine) pEngine->ResetInternal();
133    
134            // status of engine channel has changed, so set notify flag
135            bStatusChanged = true;
136      }      }
137    
138      LinuxSampler::Engine* EngineChannel::GetEngine() {      LinuxSampler::Engine* EngineChannel::GetEngine() {
# Line 124  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            // make sure we don't trigger any new notes with an old
168          if (pEngine) pEngine->DisableAndLock();          // instrument
169            instrument_change_command_t& cmd = ChangeInstrument(0);
170          ResetInternal();          if (cmd.pInstrument) {
171                // give old instrument back to instrument manager, but
172          // free old instrument              // keep the dimension regions and samples that are in use
173          if (pInstrument) {              Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse);
             // give old instrument back to instrument manager  
             Engine::instruments.HandBack(pInstrument, this);  
174          }          }
175            cmd.pDimRegionsInUse->clear();
176    
177          // delete all key groups          // delete all key groups
178          ActiveKeyGroups.clear();          ActiveKeyGroups.clear();
179    
180          // request gig instrument from instrument manager          // request gig instrument from instrument manager
181            ::gig::Instrument* newInstrument;
182          try {          try {
183              instrument_id_t instrid;              InstrumentManager::instrument_id_t instrid;
184              instrid.FileName    = InstrumentFile;              instrid.FileName  = InstrumentFile;
185              instrid.iInstrument = InstrumentIdx;              instrid.Index     = InstrumentIdx;
186              pInstrument = Engine::instruments.Borrow(instrid, this);              newInstrument = Engine::instruments.Borrow(instrid, this);
187              if (!pInstrument) {              if (!newInstrument) {
188                  InstrumentStat = -1;                  throw InstrumentManagerException("resource was not created");
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
189              }              }
190          }          }
191          catch (RIFF::Exception e) {          catch (RIFF::Exception e) {
192              InstrumentStat = -2;              InstrumentStat = -2;
193                StatusChanged(true);
194              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
195              throw LinuxSamplerException(msg);              throw Exception(msg);
196          }          }
197          catch (InstrumentResourceManagerException e) {          catch (InstrumentManagerException e) {
198              InstrumentStat = -3;              InstrumentStat = -3;
199                StatusChanged(true);
200              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
201              throw LinuxSamplerException(msg);              throw Exception(msg);
202          }          }
203          catch (...) {          catch (...) {
204              InstrumentStat = -4;              InstrumentStat = -4;
205              throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");              StatusChanged(true);
206                throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");
207          }          }
208    
209          // rebuild ActiveKeyGroups map with key groups of current instrument          // rebuild ActiveKeyGroups map with key groups of current instrument
210          for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion())          for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
211              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
212    
213          InstrumentIdxName = pInstrument->pInfo->Name;          InstrumentIdxName = newInstrument->pInfo->Name;
214          InstrumentStat = 100;          InstrumentStat = 100;
215    
216          // 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 LinuxSamplerException(msg);  
         }  
217    
218          if (pEngine) pEngine->Enable();          StatusChanged(true);
219        }
220    
221    
222        /**
223         * Changes the instrument for an engine channel.
224         *
225         * @param pInstrument - new instrument
226         * @returns the resulting instrument change command after the
227         *          command switch, containing the old instrument and
228         *          the dimregions it is using
229         */
230        EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) {
231            instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
232            cmd.pInstrument = pInstrument;
233            cmd.bChangeInstrument = true;
234    
235            return InstrumentChangeCommand.SwitchConfig();
236      }      }
237    
238      /**      /**
# Line 208  namespace LinuxSampler { namespace gig { Line 254  namespace LinuxSampler { namespace gig {
254      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
255          this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument())          this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument())
256          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
257            bStatusChanged = true; // status of engine has changed, so set notify flag
258      }      }
259    
260      /**      /**
# Line 219  namespace LinuxSampler { namespace gig { Line 266  namespace LinuxSampler { namespace gig {
266      void EngineChannel::OnResourceProgress(float fProgress) {      void EngineChannel::OnResourceProgress(float fProgress) {
267          this->InstrumentStat = int(fProgress * 100.0f);          this->InstrumentStat = int(fProgress * 100.0f);
268          dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));          dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));
269            bStatusChanged = true; // status of engine has changed, so set notify flag
270      }      }
271    
272      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
# Line 228  namespace LinuxSampler { namespace gig { Line 276  namespace LinuxSampler { namespace gig {
276          }          }
277          pEngine = Engine::AcquireEngine(this, pAudioOut);          pEngine = Engine::AcquireEngine(this, pAudioOut);
278          ResetInternal();          ResetInternal();
279          pEvents   = new RTList<Event>(pEngine->pEventPool);          pEvents = new RTList<Event>(pEngine->pEventPool);
280          pCCEvents = new RTList<Event>(pEngine->pEventPool);  
281          for (uint i = 0; i < Event::destination_count; i++) {          // reset the instrument change command struct (need to be done
282              pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool);          // twice, as it is double buffered)
283            {
284                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
285                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
286                cmd.pInstrument = 0;
287                cmd.bChangeInstrument = false;
288          }          }
289            {
290                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
291                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
292                cmd.pInstrument = 0;
293                cmd.bChangeInstrument = false;
294            }
295    
296          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
297              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
298              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
299          }          }
300          AudioDeviceChannelLeft  = 0;          AudioDeviceChannelLeft  = 0;
301          AudioDeviceChannelRight = 1;          AudioDeviceChannelRight = 1;
302          pOutputLeft             = pAudioOut->Channel(0)->Buffer();          if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
303          pOutputRight            = pAudioOut->Channel(1)->Buffer();              pChannelLeft  = pAudioOut->Channel(AudioDeviceChannelLeft);
304                pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
305            } else { // use local buffers for rendering and copy later
306                // ensure the local buffers have the correct size
307                if (pChannelLeft)  delete pChannelLeft;
308                if (pChannelRight) delete pChannelRight;
309                pChannelLeft  = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
310                pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
311            }
312            if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
313            MidiInputPort::AddSysexListener(pEngine);
314      }      }
315    
316      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
317          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
318    
319                // delete the structures used for instrument change
320                RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;
321                if (d) delete d;
322                EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
323                d = cmd.pDimRegionsInUse;
324    
325                if (cmd.pInstrument) {
326                    // release the currently loaded instrument
327                    Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d);
328                }
329                if (d) delete d;
330    
331                // release all active dimension regions to resource
332                // manager
333                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
334                RTList<uint>::Iterator end    = pActiveKeys->end();
335                while (iuiKey != end) { // iterate through all active keys
336                    midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
337                    ++iuiKey;
338    
339                    RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
340                    RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
341                    for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
342                        Engine::instruments.HandBackDimReg(itVoice->pDimRgn);
343                    }
344                }
345    
346              ResetInternal();              ResetInternal();
347              if (pEvents) {              if (pEvents) {
348                  delete pEvents;                  delete pEvents;
349                  pEvents = NULL;                  pEvents = NULL;
350              }              }
             if (pCCEvents) {  
                 delete pCCEvents;  
                 pCCEvents = NULL;  
             }  
351              for (uint i = 0; i < 128; i++) {              for (uint i = 0; i < 128; i++) {
352                  if (pMIDIKeyInfo[i].pActiveVoices) {                  if (pMIDIKeyInfo[i].pActiveVoices) {
353                      delete pMIDIKeyInfo[i].pActiveVoices;                      delete pMIDIKeyInfo[i].pActiveVoices;
# Line 264  namespace LinuxSampler { namespace gig { Line 358  namespace LinuxSampler { namespace gig {
358                      pMIDIKeyInfo[i].pEvents = NULL;                      pMIDIKeyInfo[i].pEvents = NULL;
359                  }                  }
360              }              }
             for (uint i = 0; i < Event::destination_count; i++) {  
                 if (pSynthesisEvents[i]) {  
                     delete pSynthesisEvents[i];  
                     pSynthesisEvents[i] = NULL;  
                 }  
             }  
361              Engine* oldEngine = pEngine;              Engine* oldEngine = pEngine;
362              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
363              pEngine = NULL;              pEngine = NULL;
364              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
365              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
366              AudioDeviceChannelRight = -1;              AudioDeviceChannelRight = -1;
367                if (!fxSends.empty()) { // free the local rendering buffers
368                    if (pChannelLeft)  delete pChannelLeft;
369                    if (pChannelRight) delete pChannelRight;
370                }
371                pChannelLeft  = NULL;
372                pChannelRight = NULL;
373          }          }
374      }      }
375    
376        AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
377            return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
378        }
379    
380      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
381          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
382    
# Line 286  namespace LinuxSampler { namespace gig { Line 384  namespace LinuxSampler { namespace gig {
384          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
385          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
386              case 0: // left output channel              case 0: // left output channel
387                  pOutputLeft = pChannel->Buffer();                  if (fxSends.empty()) pChannelLeft = pChannel;
388                  AudioDeviceChannelLeft = AudioDeviceChannel;                  AudioDeviceChannelLeft = AudioDeviceChannel;
389                  break;                  break;
390              case 1: // right output channel              case 1: // right output channel
391                  pOutputRight = pChannel->Buffer();                  if (fxSends.empty()) pChannelRight = pChannel;
392                  AudioDeviceChannelRight = AudioDeviceChannel;                  AudioDeviceChannelRight = AudioDeviceChannel;
393                  break;                  break;
394              default:              default:
395                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
396          }          }
397    
398            bStatusChanged = true;
399      }      }
400    
401      int EngineChannel::OutputChannel(uint EngineAudioChannel) {      int EngineChannel::OutputChannel(uint EngineAudioChannel) {
# Line 309  namespace LinuxSampler { namespace gig { Line 409  namespace LinuxSampler { namespace gig {
409          }          }
410      }      }
411    
412        void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) {
413            if (!pMidiPort || pMidiPort == this->pMidiInputPort) return;
414            DisconnectMidiInputPort();
415            this->pMidiInputPort = pMidiPort;
416            this->midiChannel    = MidiChannel;
417            pMidiPort->Connect(this, MidiChannel);
418        }
419    
420        void EngineChannel::DisconnectMidiInputPort() {
421            MidiInputPort* pOldPort = this->pMidiInputPort;
422            this->pMidiInputPort = NULL;
423            if (pOldPort) pOldPort->Disconnect(this);
424        }
425    
426        MidiInputPort* EngineChannel::GetMidiInputPort() {
427            return pMidiInputPort;
428        }
429    
430        midi_chan_t EngineChannel::MidiChannel() {
431            return midiChannel;
432        }
433    
434        FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
435            if (pEngine) pEngine->DisableAndLock();
436            FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
437            if (fxSends.empty()) {
438                if (pEngine && pEngine->pAudioOutputDevice) {
439                    AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
440                    // create local render buffers
441                    pChannelLeft  = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
442                    pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
443                } else {
444                    // postpone local render buffer creation until audio device is assigned
445                    pChannelLeft  = NULL;
446                    pChannelRight = NULL;
447                }
448            }
449            fxSends.push_back(pFxSend);
450            if (pEngine) pEngine->Enable();
451            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
452    
453            return pFxSend;
454        }
455    
456        FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
457            return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
458        }
459    
460        uint EngineChannel::GetFxSendCount() {
461            return fxSends.size();
462        }
463    
464        void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
465            if (pEngine) pEngine->DisableAndLock();
466            for (
467                std::vector<FxSend*>::iterator iter = fxSends.begin();
468                iter != fxSends.end(); iter++
469            ) {
470                if (*iter == pFxSend) {
471                    delete pFxSend;
472                    fxSends.erase(iter);
473                    if (fxSends.empty()) {
474                        // destroy local render buffers
475                        if (pChannelLeft)  delete pChannelLeft;
476                        if (pChannelRight) delete pChannelRight;
477                        // fallback to render directly into AudioOutputDevice's buffers
478                        if (pEngine && pEngine->pAudioOutputDevice) {
479                            pChannelLeft  = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
480                            pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
481                        } else { // we update the pointers later
482                            pChannelLeft  = NULL;
483                            pChannelRight = NULL;
484                        }
485                    }
486                    break;
487                }
488            }
489            if (pEngine) pEngine->Enable();
490            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
491        }
492    
493      /**      /**
494       *  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
495       *  voice for the given key.       *  voice for the given key. This method is meant for real time rendering,
496         *  that is an event will immediately be created with the current system
497         *  time as time stamp.
498       *       *
499       *  @param Key      - MIDI key number of the triggered key       *  @param Key      - MIDI key number of the triggered key
500       *  @param Velocity - MIDI velocity value of the triggered key       *  @param Velocity - MIDI velocity value of the triggered key
# Line 329  namespace LinuxSampler { namespace gig { Line 512  namespace LinuxSampler { namespace gig {
512      }      }
513    
514      /**      /**
515         *  Will be called by the MIDIIn Thread to let the audio thread trigger a new
516         *  voice for the given key. This method is meant for offline rendering
517         *  and / or for cases where the exact position of the event in the current
518         *  audio fragment is already known.
519         *
520         *  @param Key         - MIDI key number of the triggered key
521         *  @param Velocity    - MIDI velocity value of the triggered key
522         *  @param FragmentPos - sample point position in the current audio
523         *                       fragment to which this event belongs to
524         */
525        void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
526            if (FragmentPos < 0) {
527                dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!"));
528            }
529            else if (pEngine) {
530                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
531                event.Type                = Event::type_note_on;
532                event.Param.Note.Key      = Key;
533                event.Param.Note.Velocity = Velocity;
534                event.pEngineChannel      = this;
535                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
536                else dmsg(1,("EngineChannel: Input event queue full!"));
537            }
538        }
539    
540        /**
541       *  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
542       *  voice(s) on the given key.       *  voice(s) on the given key. This method is meant for real time rendering,
543         *  that is an event will immediately be created with the current system
544         *  time as time stamp.
545       *       *
546       *  @param Key      - MIDI key number of the released key       *  @param Key      - MIDI key number of the released key
547       *  @param Velocity - MIDI release velocity value of the released key       *  @param Velocity - MIDI release velocity value of the released key
# Line 348  namespace LinuxSampler { namespace gig { Line 559  namespace LinuxSampler { namespace gig {
559      }      }
560    
561      /**      /**
562         *  Will be called by the MIDIIn Thread to signal the audio thread to release
563         *  voice(s) on the given key. This method is meant for offline rendering
564         *  and / or for cases where the exact position of the event in the current
565         *  audio fragment is already known.
566         *
567         *  @param Key         - MIDI key number of the released key
568         *  @param Velocity    - MIDI release velocity value of the released key
569         *  @param FragmentPos - sample point position in the current audio
570         *                       fragment to which this event belongs to
571         */
572        void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
573            if (FragmentPos < 0) {
574                dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!"));
575            }
576            else if (pEngine) {
577                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
578                event.Type                = Event::type_note_off;
579                event.Param.Note.Key      = Key;
580                event.Param.Note.Velocity = Velocity;
581                event.pEngineChannel      = this;
582                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
583                else dmsg(1,("EngineChannel: Input event queue full!"));
584            }
585        }
586    
587        /**
588       *  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
589       *  the pitch value for all voices.       *  the pitch value for all voices. This method is meant for real time
590         *  rendering, that is an event will immediately be created with the
591         *  current system time as time stamp.
592       *       *
593       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param Pitch - MIDI pitch value (-8192 ... +8191)
594       */       */
# Line 365  namespace LinuxSampler { namespace gig { Line 604  namespace LinuxSampler { namespace gig {
604      }      }
605    
606      /**      /**
607         *  Will be called by the MIDIIn Thread to signal the audio thread to change
608         *  the pitch value for all voices. This method is meant for offline
609         *  rendering and / or for cases where the exact position of the event in
610         *  the current audio fragment is already known.
611         *
612         *  @param Pitch       - MIDI pitch value (-8192 ... +8191)
613         *  @param FragmentPos - sample point position in the current audio
614         *                       fragment to which this event belongs to
615         */
616        void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) {
617            if (FragmentPos < 0) {
618                dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!"));
619            }
620            else if (pEngine) {
621                Event event             = pEngine->pEventGenerator->CreateEvent(FragmentPos);
622                event.Type              = Event::type_pitchbend;
623                event.Param.Pitch.Pitch = Pitch;
624                event.pEngineChannel    = this;
625                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
626                else dmsg(1,("EngineChannel: Input event queue full!"));
627            }
628        }
629    
630        /**
631       *  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
632       *  continuous controller value has changed.       *  continuous controller value has changed. This method is meant for real
633         *  time rendering, that is an event will immediately be created with the
634         *  current system time as time stamp.
635       *       *
636       *  @param Controller - MIDI controller number of the occured control change       *  @param Controller - MIDI controller number of the occured control change
637       *  @param Value      - value of the control change       *  @param Value      - value of the control change
# Line 383  namespace LinuxSampler { namespace gig { Line 648  namespace LinuxSampler { namespace gig {
648          }          }
649      }      }
650    
651        /**
652         *  Will be called by the MIDIIn Thread to signal the audio thread that a
653         *  continuous controller value has changed. This method is meant for
654         *  offline rendering and / or for cases where the exact position of the
655         *  event in the current audio fragment is already known.
656         *
657         *  @param Controller  - MIDI controller number of the occured control change
658         *  @param Value       - value of the control change
659         *  @param FragmentPos - sample point position in the current audio
660         *                       fragment to which this event belongs to
661         */
662        void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) {
663            if (FragmentPos < 0) {
664                dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!"));
665            }
666            else if (pEngine) {
667                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
668                event.Type                = Event::type_control_change;
669                event.Param.CC.Controller = Controller;
670                event.Param.CC.Value      = Value;
671                event.pEngineChannel      = this;
672                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
673                else dmsg(1,("EngineChannel: Input event queue full!"));
674            }
675        }
676    
677      void EngineChannel::ClearEventLists() {      void EngineChannel::ClearEventLists() {
678          pEvents->clear();          pEvents->clear();
         pCCEvents->clear();  
         for (uint i = 0; i < Event::destination_count; i++) {  
             pSynthesisEvents[i]->clear();  
         }  
679          // empty MIDI key specific event lists          // empty MIDI key specific event lists
680          {          {
681              RTList<uint>::Iterator iuiKey = pActiveKeys->first();              RTList<uint>::Iterator iuiKey = pActiveKeys->first();
# Line 400  namespace LinuxSampler { namespace gig { Line 687  namespace LinuxSampler { namespace gig {
687      }      }
688    
689      void EngineChannel::ResetControllers() {      void EngineChannel::ResetControllers() {
690            Pitch          = 0;
691            SustainPedal   = false;
692            SostenutoPedal = false;
693            GlobalVolume   = 1.0f;
694            MidiVolume     = 1.0;
695            GlobalPanLeft  = 1.0f;
696            GlobalPanRight = 1.0f;
697            GlobalTranspose = 0;
698          // set all MIDI controller values to zero          // set all MIDI controller values to zero
699          memset(ControllerTable, 0x00, 128);          memset(ControllerTable, 0x00, 129);
700            // reset all FX Send levels
701            for (
702                std::vector<FxSend*>::iterator iter = fxSends.begin();
703                iter != fxSends.end(); iter++
704            ) {
705                (*iter)->Reset();
706            }
707      }      }
708    
709      /**      /**
# Line 418  namespace LinuxSampler { namespace gig { Line 720  namespace LinuxSampler { namespace gig {
720       *                  current audio cycle       *                  current audio cycle
721       */       */
722      void EngineChannel::ImportEvents(uint Samples) {      void EngineChannel::ImportEvents(uint Samples) {
723          RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
724          Event* pEvent;          Event* pEvent;
725          while (true) {          while (true) {
726              // get next event from input event queue              // get next event from input event queue
# Line 440  namespace LinuxSampler { namespace gig { Line 742  namespace LinuxSampler { namespace gig {
742          eventQueueReader.free(); // free all copied events from input queue          eventQueueReader.free(); // free all copied events from input queue
743      }      }
744    
745        void EngineChannel::RemoveAllFxSends() {
746            if (pEngine) pEngine->DisableAndLock();
747            if (!fxSends.empty()) { // free local render buffers
748                if (pChannelLeft) {
749                    delete pChannelLeft;
750                    if (pEngine && pEngine->pAudioOutputDevice) {
751                        // fallback to render directly to the AudioOutputDevice's buffer
752                        pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
753                    } else pChannelLeft = NULL;
754                }
755                if (pChannelRight) {
756                    delete pChannelRight;
757                    if (pEngine && pEngine->pAudioOutputDevice) {
758                        // fallback to render directly to the AudioOutputDevice's buffer
759                        pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
760                    } else pChannelRight = NULL;
761                }
762            }
763            for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
764            fxSends.clear();
765            if (pEngine) pEngine->Enable();
766        }
767    
768      float EngineChannel::Volume() {      float EngineChannel::Volume() {
769          return GlobalVolume;          return GlobalVolume;
770      }      }
771    
772      void EngineChannel::Volume(float f) {      void EngineChannel::Volume(float f) {
773          GlobalVolume = f;          GlobalVolume = f;
774            bStatusChanged = true; // status of engine channel has changed, so set notify flag
775      }      }
776    
777      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
# Line 471  namespace LinuxSampler { namespace gig { Line 797  namespace LinuxSampler { namespace gig {
797      String EngineChannel::EngineName() {      String EngineChannel::EngineName() {
798          return LS_GIG_ENGINE_NAME;          return LS_GIG_ENGINE_NAME;
799      }      }
800        
801  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

Legend:
Removed from v.554  
changed lines
  Added in v.1646

  ViewVC Help
Powered by ViewVC