/[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 412 by schoenebeck, Sat Feb 26 22:44:51 2005 UTC revision 1659 by schoenebeck, Sun Feb 3 00:13:27 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  namespace LinuxSampler { namespace gig {      #include "../../common/global_private.h"
27    
28      EngineChannel::EngineChannel() {  namespace LinuxSampler { namespace gig {
29    
30        EngineChannel::EngineChannel() :
31            InstrumentChangeCommandReader(InstrumentChangeCommand),
32            virtualMidiDevicesReader(virtualMidiDevices)
33        {
34          pMIDIKeyInfo = new midi_key_info_t[128];          pMIDIKeyInfo = new midi_key_info_t[128];
35          pEngine      = NULL;          pEngine      = NULL;
36          pInstrument  = NULL;          pInstrument  = NULL;
37          pEventQueue  = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);                  pEvents      = NULL; // we allocate when we retrieve the right Engine object
38            pEventQueue  = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
39          pActiveKeys  = new Pool<uint>(128);          pActiveKeys  = new Pool<uint>(128);
40          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
41              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 37  namespace LinuxSampler { namespace gig { Line 43  namespace LinuxSampler { namespace gig {
43              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
44              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
45              pMIDIKeyInfo[i].pEvents        = NULL; // we allocate when we retrieve the right Engine object              pMIDIKeyInfo[i].pEvents        = NULL; // we allocate when we retrieve the right Engine object
46                pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
47                pMIDIKeyInfo[i].RoundRobinIndex = 0;
48          }          }
49          InstrumentIdx  = -1;          InstrumentIdx  = -1;
50          InstrumentStat = -1;          InstrumentStat = -1;
51            pChannelLeft  = NULL;
52            pChannelRight = NULL;
53          AudioDeviceChannelLeft  = -1;          AudioDeviceChannelLeft  = -1;
54          AudioDeviceChannelRight = -1;          AudioDeviceChannelRight = -1;
55            pMidiInputPort = NULL;
56            midiChannel = midi_chan_all;
57            ResetControllers();
58            SoloMode       = false;
59            PortamentoMode = false;
60            PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT;
61      }      }
62    
63      EngineChannel::~EngineChannel() {      EngineChannel::~EngineChannel() {
64          if (pInstrument) Engine::instruments.HandBack(pInstrument, this);          DisconnectAudioOutputDevice();
         for (uint i = 0; i < 128; i++) {  
             if (pMIDIKeyInfo[i].pActiveVoices) {  
                 pMIDIKeyInfo[i].pActiveVoices->clear();  
                 delete pMIDIKeyInfo[i].pActiveVoices;  
             }  
             if (pMIDIKeyInfo[i].pEvents) {  
                 pMIDIKeyInfo[i].pEvents->clear();  
                 delete pMIDIKeyInfo[i].pEvents;  
             }  
         }  
65          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
66          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
67          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
68            RemoveAllFxSends();
69        }
70    
71        /**
72         * Implementation of virtual method from abstract EngineChannel interface.
73         * This method will periodically be polled (e.g. by the LSCP server) to
74         * check if some engine channel parameter has changed since the last
75         * StatusChanged() call.
76         *
77         * This method can also be used to mark the engine channel as changed
78         * from outside, e.g. by a MIDI input device. The optional argument
79         * \a nNewStatus can be used for this.
80         *
81         * TODO: This "poll method" is just a lazy solution and might be
82         *       replaced in future.
83         * @param bNewStatus - (optional, default: false) sets the new status flag
84         * @returns true if engine channel status has changed since last
85         *          StatusChanged() call
86         */
87        bool EngineChannel::StatusChanged(bool bNewStatus) {
88            bool b = bStatusChanged;
89            bStatusChanged = bNewStatus;
90            return b;
91        }
92    
93        void EngineChannel::Reset() {
94            if (pEngine) pEngine->DisableAndLock();
95            ResetInternal();
96            ResetControllers();
97            if (pEngine) {
98                pEngine->Enable();
99                pEngine->Reset();
100            }
101      }      }
102    
103      /**      /**
104       * This method is not thread safe!       * This method is not thread safe!
105       */       */
106      void EngineChannel::ResetInternal() {      void EngineChannel::ResetInternal() {
         Pitch               = 0;  
         SustainPedal        = false;  
         GlobalVolume        = 1.0;  
107          CurrentKeyDimension = 0;          CurrentKeyDimension = 0;
108    
         // set all MIDI controller values to zero  
         memset(ControllerTable, 0x00, 128);  
   
         // reset voice stealing parameters  
         itLastStolenVoice = RTList<Voice>::Iterator();  
         iuiLastStolenKey  = RTList<uint>::Iterator();  
   
109          // reset key info          // reset key info
110          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
111              if (pMIDIKeyInfo[i].pActiveVoices)              if (pMIDIKeyInfo[i].pActiveVoices)
# Line 87  namespace LinuxSampler { namespace gig { Line 116  namespace LinuxSampler { namespace gig {
116              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
117              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
118              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();
119                pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
120          }          }
121            SoloKey       = -1;    // no solo key active yet
122            PortamentoPos = -1.0f; // no portamento active yet
123    
124          // reset all key groups          // reset all key groups
125          std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();          std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();
# Line 100  namespace LinuxSampler { namespace gig { Line 132  namespace LinuxSampler { namespace gig {
132          pEventQueue->init();          pEventQueue->init();
133    
134          if (pEngine) pEngine->ResetInternal();          if (pEngine) pEngine->ResetInternal();
135    
136            // status of engine channel has changed, so set notify flag
137            bStatusChanged = true;
138      }      }
139    
140      LinuxSampler::Engine* EngineChannel::GetEngine() {      LinuxSampler::Engine* EngineChannel::GetEngine() {
# Line 128  namespace LinuxSampler { namespace gig { Line 163  namespace LinuxSampler { namespace gig {
163       * This method will then actually start to load the instrument and block       * This method will then actually start to load the instrument and block
164       * the calling thread until loading was completed.       * the calling thread until loading was completed.
165       *       *
      * @returns detailed description of the method call result  
166       * @see PrepareLoadInstrument()       * @see PrepareLoadInstrument()
167       */       */
168      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
169            // make sure we don't trigger any new notes with an old
170          if (pEngine) pEngine->DisableAndLock();          // instrument
171                    instrument_change_command_t& cmd = ChangeInstrument(0);
172          ResetInternal();          if (cmd.pInstrument) {
173                        // give old instrument back to instrument manager, but
174          // free old instrument              // keep the dimension regions and samples that are in use
175          if (pInstrument) {              Engine::instruments.HandBackInstrument(cmd.pInstrument, this, cmd.pDimRegionsInUse);
             // give old instrument back to instrument manager  
             Engine::instruments.HandBack(pInstrument, this);  
176          }          }
177            cmd.pDimRegionsInUse->clear();
178    
179          // delete all key groups          // delete all key groups
180          ActiveKeyGroups.clear();          ActiveKeyGroups.clear();
181    
182          // request gig instrument from instrument manager          // request gig instrument from instrument manager
183            ::gig::Instrument* newInstrument;
184          try {          try {
185              instrument_id_t instrid;              InstrumentManager::instrument_id_t instrid;
186              instrid.FileName    = InstrumentFile;              instrid.FileName  = InstrumentFile;
187              instrid.iInstrument = InstrumentIdx;              instrid.Index     = InstrumentIdx;
188              pInstrument = Engine::instruments.Borrow(instrid, this);              newInstrument = Engine::instruments.Borrow(instrid, this);
189              if (!pInstrument) {              if (!newInstrument) {
190                  InstrumentStat = -1;                  throw InstrumentManagerException("resource was not created");
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
191              }              }
192          }          }
193          catch (RIFF::Exception e) {          catch (RIFF::Exception e) {
194              InstrumentStat = -2;              InstrumentStat = -2;
195                StatusChanged(true);
196              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;              String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
197              throw LinuxSamplerException(msg);              throw Exception(msg);
198          }          }
199          catch (InstrumentResourceManagerException e) {          catch (InstrumentManagerException e) {
200              InstrumentStat = -3;              InstrumentStat = -3;
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 LinuxSamplerException(msg);              throw Exception(msg);
204          }          }
205          catch (...) {          catch (...) {
206              InstrumentStat = -4;              InstrumentStat = -4;
207              throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");              StatusChanged(true);
208                throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");
209          }          }
210    
211          // rebuild ActiveKeyGroups map with key groups of current instrument          // rebuild ActiveKeyGroups map with key groups of current instrument
212          for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion())          for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
213              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;              if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
214    
215          InstrumentIdxName = pInstrument->pInfo->Name;          InstrumentIdxName = newInstrument->pInfo->Name;
216          InstrumentStat = 100;          InstrumentStat = 100;
217    
218          // 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);  
         }  
219    
220          if (pEngine) pEngine->Enable();          StatusChanged(true);
221        }
222    
223    
224        /**
225         * Changes the instrument for an engine channel.
226         *
227         * @param pInstrument - new instrument
228         * @returns the resulting instrument change command after the
229         *          command switch, containing the old instrument and
230         *          the dimregions it is using
231         */
232        EngineChannel::instrument_change_command_t& EngineChannel::ChangeInstrument(::gig::Instrument* pInstrument) {
233            instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
234            cmd.pInstrument = pInstrument;
235            cmd.bChangeInstrument = true;
236    
237            return InstrumentChangeCommand.SwitchConfig();
238      }      }
239    
240      /**      /**
241       * Will be called by the InstrumentResourceManager when the instrument       * Will be called by the InstrumentResourceManager when the instrument
242       * we are currently using in this engine is going to be updated, so we       * we are currently using on this EngineChannel is going to be updated,
243       * can stop playback before that happens.       * so we can stop playback before that happens.
244       */       */
245      void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {      void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
246          dmsg(3,("gig::Engine: Received instrument update message.\n"));          dmsg(3,("gig::Engine: Received instrument update message.\n"));
# Line 212  namespace LinuxSampler { namespace gig { Line 256  namespace LinuxSampler { namespace gig {
256      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
257          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())
258          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
259            bStatusChanged = true; // status of engine has changed, so set notify flag
260        }
261    
262        /**
263         * Will be called by the InstrumentResourceManager on progress changes
264         * while loading or realoading an instrument for this EngineChannel.
265         *
266         * @param fProgress - current progress as value between 0.0 and 1.0
267         */
268        void EngineChannel::OnResourceProgress(float fProgress) {
269            this->InstrumentStat = int(fProgress * 100.0f);
270            dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));
271            bStatusChanged = true; // status of engine has changed, so set notify flag
272      }      }
273    
274      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
275          if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) {          if (pEngine) {
276                if (pEngine->pAudioOutputDevice == pAudioOut) return;
277              DisconnectAudioOutputDevice();              DisconnectAudioOutputDevice();
278          }          }
279          pEngine = Engine::AcquireEngine(this, pAudioOut);          pEngine = Engine::AcquireEngine(this, pAudioOut);
280          ResetInternal();                  ResetInternal();
281            pEvents = new RTList<Event>(pEngine->pEventPool);
282    
283            // reset the instrument change command struct (need to be done
284            // twice, as it is double buffered)
285            {
286                instrument_change_command_t& cmd = InstrumentChangeCommand.GetConfigForUpdate();
287                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[0]);
288                cmd.pInstrument = 0;
289                cmd.bChangeInstrument = false;
290            }
291            {
292                instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
293                cmd.pDimRegionsInUse = new RTList< ::gig::DimensionRegion*>(pEngine->pDimRegionPool[1]);
294                cmd.pInstrument = 0;
295                cmd.bChangeInstrument = false;
296            }
297    
298          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
299              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
300              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
301          }          }
302          AudioDeviceChannelLeft  = 0;          AudioDeviceChannelLeft  = 0;
303          AudioDeviceChannelRight = 1;          AudioDeviceChannelRight = 1;
304          pOutputLeft             = pAudioOut->Channel(0)->Buffer();          if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
305          pOutputRight            = pAudioOut->Channel(1)->Buffer();              pChannelLeft  = pAudioOut->Channel(AudioDeviceChannelLeft);
306                pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
307            } else { // use local buffers for rendering and copy later
308                // ensure the local buffers have the correct size
309                if (pChannelLeft)  delete pChannelLeft;
310                if (pChannelRight) delete pChannelRight;
311                pChannelLeft  = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
312                pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
313            }
314            if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
315            MidiInputPort::AddSysexListener(pEngine);
316      }      }
317    
318      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
319          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
320    
321                // delete the structures used for instrument change
322                RTList< ::gig::DimensionRegion*>* d = InstrumentChangeCommand.GetConfigForUpdate().pDimRegionsInUse;
323                if (d) delete d;
324                EngineChannel::instrument_change_command_t& cmd = InstrumentChangeCommand.SwitchConfig();
325                d = cmd.pDimRegionsInUse;
326    
327                if (cmd.pInstrument) {
328                    // release the currently loaded instrument
329                    Engine::instruments.HandBackInstrument(cmd.pInstrument, this, d);
330                }
331                if (d) delete d;
332    
333                // release all active dimension regions to resource
334                // manager
335                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
336                RTList<uint>::Iterator end    = pActiveKeys->end();
337                while (iuiKey != end) { // iterate through all active keys
338                    midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey];
339                    ++iuiKey;
340    
341                    RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
342                    RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
343                    for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
344                        Engine::instruments.HandBackDimReg(itVoice->pDimRgn);
345                    }
346                }
347    
348              ResetInternal();              ResetInternal();
349                if (pEvents) {
350                    delete pEvents;
351                    pEvents = NULL;
352                }
353              for (uint i = 0; i < 128; i++) {              for (uint i = 0; i < 128; i++) {
354                  if (pMIDIKeyInfo[i].pActiveVoices) delete pMIDIKeyInfo[i].pActiveVoices;                  if (pMIDIKeyInfo[i].pActiveVoices) {
355                  if (pMIDIKeyInfo[i].pEvents)       delete pMIDIKeyInfo[i].pEvents;                      delete pMIDIKeyInfo[i].pActiveVoices;
356                        pMIDIKeyInfo[i].pActiveVoices = NULL;
357                    }
358                    if (pMIDIKeyInfo[i].pEvents) {
359                        delete pMIDIKeyInfo[i].pEvents;
360                        pMIDIKeyInfo[i].pEvents = NULL;
361                    }
362              }              }
363              Engine* oldEngine = pEngine;              Engine* oldEngine = pEngine;
364              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
365              pEngine = NULL;              pEngine = NULL;
366              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
367              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
368              AudioDeviceChannelRight = -1;                          AudioDeviceChannelRight = -1;
369                if (!fxSends.empty()) { // free the local rendering buffers
370                    if (pChannelLeft)  delete pChannelLeft;
371                    if (pChannelRight) delete pChannelRight;
372                }
373                pChannelLeft  = NULL;
374                pChannelRight = NULL;
375          }          }
376      }      }
377    
378        AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
379            return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
380        }
381    
382      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
383          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
384            
385          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
386          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
387          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
388              case 0: // left output channel              case 0: // left output channel
389                  pOutputLeft = pChannel->Buffer();                  if (fxSends.empty()) pChannelLeft = pChannel;
390                  AudioDeviceChannelLeft = AudioDeviceChannel;                  AudioDeviceChannelLeft = AudioDeviceChannel;
391                  break;                  break;
392              case 1: // right output channel              case 1: // right output channel
393                  pOutputRight = pChannel->Buffer();                  if (fxSends.empty()) pChannelRight = pChannel;
394                  AudioDeviceChannelRight = AudioDeviceChannel;                  AudioDeviceChannelRight = AudioDeviceChannel;
395                  break;                  break;
396              default:              default:
397                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));                  throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
398          }          }
399    
400            bStatusChanged = true;
401      }      }
402    
403      int EngineChannel::OutputChannel(uint EngineAudioChannel) {      int EngineChannel::OutputChannel(uint EngineAudioChannel) {
# Line 276  namespace LinuxSampler { namespace gig { Line 411  namespace LinuxSampler { namespace gig {
411          }          }
412      }      }
413    
414        void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) {
415            if (!pMidiPort || pMidiPort == this->pMidiInputPort) return;
416            DisconnectMidiInputPort();
417            this->pMidiInputPort = pMidiPort;
418            this->midiChannel    = MidiChannel;
419            pMidiPort->Connect(this, MidiChannel);
420        }
421    
422        void EngineChannel::DisconnectMidiInputPort() {
423            MidiInputPort* pOldPort = this->pMidiInputPort;
424            this->pMidiInputPort = NULL;
425            if (pOldPort) pOldPort->Disconnect(this);
426        }
427    
428        MidiInputPort* EngineChannel::GetMidiInputPort() {
429            return pMidiInputPort;
430        }
431    
432        midi_chan_t EngineChannel::MidiChannel() {
433            return midiChannel;
434        }
435    
436        FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
437            if (pEngine) pEngine->DisableAndLock();
438            FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
439            if (fxSends.empty()) {
440                if (pEngine && pEngine->pAudioOutputDevice) {
441                    AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
442                    // create local render buffers
443                    pChannelLeft  = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
444                    pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
445                } else {
446                    // postpone local render buffer creation until audio device is assigned
447                    pChannelLeft  = NULL;
448                    pChannelRight = NULL;
449                }
450            }
451            fxSends.push_back(pFxSend);
452            if (pEngine) pEngine->Enable();
453            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
454    
455            return pFxSend;
456        }
457    
458        FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
459            return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
460        }
461    
462        uint EngineChannel::GetFxSendCount() {
463            return fxSends.size();
464        }
465    
466        void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
467            if (pEngine) pEngine->DisableAndLock();
468            for (
469                std::vector<FxSend*>::iterator iter = fxSends.begin();
470                iter != fxSends.end(); iter++
471            ) {
472                if (*iter == pFxSend) {
473                    delete pFxSend;
474                    fxSends.erase(iter);
475                    if (fxSends.empty()) {
476                        // destroy local render buffers
477                        if (pChannelLeft)  delete pChannelLeft;
478                        if (pChannelRight) delete pChannelRight;
479                        // fallback to render directly into AudioOutputDevice's buffers
480                        if (pEngine && pEngine->pAudioOutputDevice) {
481                            pChannelLeft  = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
482                            pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
483                        } else { // we update the pointers later
484                            pChannelLeft  = NULL;
485                            pChannelRight = NULL;
486                        }
487                    }
488                    break;
489                }
490            }
491            if (pEngine) pEngine->Enable();
492            fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
493        }
494    
495      /**      /**
496       *  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
497       *  voice for the given key.       *  voice for the given key. This method is meant for real time rendering,
498         *  that is an event will immediately be created with the current system
499         *  time as time stamp.
500       *       *
501       *  @param Key      - MIDI key number of the triggered key       *  @param Key      - MIDI key number of the triggered key
502       *  @param Velocity - MIDI velocity value of the triggered key       *  @param Velocity - MIDI velocity value of the triggered key
# Line 289  namespace LinuxSampler { namespace gig { Line 507  namespace LinuxSampler { namespace gig {
507              event.Type                = Event::type_note_on;              event.Type                = Event::type_note_on;
508              event.Param.Note.Key      = Key;              event.Param.Note.Key      = Key;
509              event.Param.Note.Velocity = Velocity;              event.Param.Note.Velocity = Velocity;
510              event.pEngineChannel      = this;                          event.pEngineChannel      = this;
511              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
512              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
513                // inform instrument editor(s), if any ...
514                pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);
515            }
516        }
517    
518        /**
519         *  Will be called by the MIDIIn Thread to let the audio thread trigger a new
520         *  voice for the given key. This method is meant for offline rendering
521         *  and / or for cases where the exact position of the event in the current
522         *  audio fragment is already known.
523         *
524         *  @param Key         - MIDI key number of the triggered key
525         *  @param Velocity    - MIDI velocity value of the triggered key
526         *  @param FragmentPos - sample point position in the current audio
527         *                       fragment to which this event belongs to
528         */
529        void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
530            if (FragmentPos < 0) {
531                dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!"));
532            }
533            else if (pEngine) {
534                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
535                event.Type                = Event::type_note_on;
536                event.Param.Note.Key      = Key;
537                event.Param.Note.Velocity = Velocity;
538                event.pEngineChannel      = this;
539                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
540                else dmsg(1,("EngineChannel: Input event queue full!"));
541                // inform instrument editor(s), if any ...
542                pEngine->instruments.TrySendNoteOnToEditors(Key, Velocity, pInstrument);
543          }          }
544      }      }
545    
546      /**      /**
547       *  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
548       *  voice(s) on the given key.       *  voice(s) on the given key. This method is meant for real time rendering,
549         *  that is an event will immediately be created with the current system
550         *  time as time stamp.
551       *       *
552       *  @param Key      - MIDI key number of the released key       *  @param Key      - MIDI key number of the released key
553       *  @param Velocity - MIDI release velocity value of the released key       *  @param Velocity - MIDI release velocity value of the released key
# Line 311  namespace LinuxSampler { namespace gig { Line 561  namespace LinuxSampler { namespace gig {
561              event.pEngineChannel      = this;              event.pEngineChannel      = this;
562              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
563              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
564                // inform instrument editor(s), if any ...
565                pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);
566            }
567        }
568    
569        /**
570         *  Will be called by the MIDIIn Thread to signal the audio thread to release
571         *  voice(s) on the given key. This method is meant for offline rendering
572         *  and / or for cases where the exact position of the event in the current
573         *  audio fragment is already known.
574         *
575         *  @param Key         - MIDI key number of the released key
576         *  @param Velocity    - MIDI release velocity value of the released key
577         *  @param FragmentPos - sample point position in the current audio
578         *                       fragment to which this event belongs to
579         */
580        void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
581            if (FragmentPos < 0) {
582                dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!"));
583            }
584            else if (pEngine) {
585                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
586                event.Type                = Event::type_note_off;
587                event.Param.Note.Key      = Key;
588                event.Param.Note.Velocity = Velocity;
589                event.pEngineChannel      = this;
590                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
591                else dmsg(1,("EngineChannel: Input event queue full!"));
592                // inform instrument editor(s), if any ...
593                pEngine->instruments.TrySendNoteOffToEditors(Key, Velocity, pInstrument);
594          }          }
595      }      }
596    
597      /**      /**
598       *  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
599       *  the pitch value for all voices.       *  the pitch value for all voices. This method is meant for real time
600         *  rendering, that is an event will immediately be created with the
601         *  current system time as time stamp.
602       *       *
603       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param Pitch - MIDI pitch value (-8192 ... +8191)
604       */       */
605      void EngineChannel::SendPitchbend(int Pitch) {      void EngineChannel::SendPitchbend(int Pitch) {
606          if (pEngine) {                  if (pEngine) {
607              Event event             = pEngine->pEventGenerator->CreateEvent();              Event event             = pEngine->pEventGenerator->CreateEvent();
608              event.Type              = Event::type_pitchbend;              event.Type              = Event::type_pitchbend;
609              event.Param.Pitch.Pitch = Pitch;              event.Param.Pitch.Pitch = Pitch;
# Line 332  namespace LinuxSampler { namespace gig { Line 614  namespace LinuxSampler { namespace gig {
614      }      }
615    
616      /**      /**
617         *  Will be called by the MIDIIn Thread to signal the audio thread to change
618         *  the pitch value for all voices. This method is meant for offline
619         *  rendering and / or for cases where the exact position of the event in
620         *  the current audio fragment is already known.
621         *
622         *  @param Pitch       - MIDI pitch value (-8192 ... +8191)
623         *  @param FragmentPos - sample point position in the current audio
624         *                       fragment to which this event belongs to
625         */
626        void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) {
627            if (FragmentPos < 0) {
628                dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!"));
629            }
630            else if (pEngine) {
631                Event event             = pEngine->pEventGenerator->CreateEvent(FragmentPos);
632                event.Type              = Event::type_pitchbend;
633                event.Param.Pitch.Pitch = Pitch;
634                event.pEngineChannel    = this;
635                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
636                else dmsg(1,("EngineChannel: Input event queue full!"));
637            }
638        }
639    
640        /**
641       *  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
642       *  continuous controller value has changed.       *  continuous controller value has changed. This method is meant for real
643         *  time rendering, that is an event will immediately be created with the
644         *  current system time as time stamp.
645       *       *
646       *  @param Controller - MIDI controller number of the occured control change       *  @param Controller - MIDI controller number of the occured control change
647       *  @param Value      - value of the control change       *  @param Value      - value of the control change
# Line 350  namespace LinuxSampler { namespace gig { Line 658  namespace LinuxSampler { namespace gig {
658          }          }
659      }      }
660    
661        /**
662         *  Will be called by the MIDIIn Thread to signal the audio thread that a
663         *  continuous controller value has changed. This method is meant for
664         *  offline rendering and / or for cases where the exact position of the
665         *  event in the current audio fragment is already known.
666         *
667         *  @param Controller  - MIDI controller number of the occured control change
668         *  @param Value       - value of the control change
669         *  @param FragmentPos - sample point position in the current audio
670         *                       fragment to which this event belongs to
671         */
672        void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) {
673            if (FragmentPos < 0) {
674                dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!"));
675            }
676            else if (pEngine) {
677                Event event               = pEngine->pEventGenerator->CreateEvent(FragmentPos);
678                event.Type                = Event::type_control_change;
679                event.Param.CC.Controller = Controller;
680                event.Param.CC.Value      = Value;
681                event.pEngineChannel      = this;
682                if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
683                else dmsg(1,("EngineChannel: Input event queue full!"));
684            }
685        }
686    
687        void EngineChannel::ClearEventLists() {
688            pEvents->clear();
689            // empty MIDI key specific event lists
690            {
691                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
692                RTList<uint>::Iterator end    = pActiveKeys->end();
693                for(; iuiKey != end; ++iuiKey) {
694                    pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
695                }
696            }
697        }
698    
699        void EngineChannel::ResetControllers() {
700            Pitch          = 0;
701            SustainPedal   = false;
702            SostenutoPedal = false;
703            GlobalVolume   = 1.0f;
704            MidiVolume     = 1.0;
705            GlobalPanLeft  = 1.0f;
706            GlobalPanRight = 1.0f;
707            GlobalTranspose = 0;
708            // set all MIDI controller values to zero
709            memset(ControllerTable, 0x00, 129);
710            // reset all FX Send levels
711            for (
712                std::vector<FxSend*>::iterator iter = fxSends.begin();
713                iter != fxSends.end(); iter++
714            ) {
715                (*iter)->Reset();
716            }
717        }
718    
719        /**
720         * Copy all events from the engine channel's input event queue buffer to
721         * the internal event list. This will be done at the beginning of each
722         * audio cycle (that is each RenderAudio() call) to distinguish all
723         * events which have to be processed in the current audio cycle. Each
724         * EngineChannel has it's own input event queue for the common channel
725         * specific events (like NoteOn, NoteOff and ControlChange events).
726         * Beside that, the engine also has a input event queue for global
727         * events (usually SysEx messages).
728         *
729         * @param Samples - number of sample points to be processed in the
730         *                  current audio cycle
731         */
732        void EngineChannel::ImportEvents(uint Samples) {
733            // import events from pure software MIDI "devices"
734            // (e.g. virtual keyboard in instrument editor)
735            {
736                const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices
737                Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
738                VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device
739                // as we're going to (carefully) write some status to the
740                // synchronized struct, we cast away the const
741                ArrayList<VirtualMidiDevice*>& devices =
742                    const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader.Lock());
743                // iterate through all virtual MIDI devices
744                for (int i = 0; i < devices.size(); i++) {
745                    VirtualMidiDevice* pDev = devices[i];
746                    // I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-)
747                    while (pDev->GetMidiEventFromDevice(devEvent)) {
748                        event.Type =
749                            (devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ?
750                                Event::type_note_on : Event::type_note_off;
751                        event.Param.Note.Key      = devEvent.Key;
752                        event.Param.Note.Velocity = devEvent.Velocity;
753                        event.pEngineChannel      = this;
754                        // copy event to internal event list
755                        if (pEvents->poolIsEmpty()) {
756                            dmsg(1,("Event pool emtpy!\n"));
757                            goto exitVirtualDevicesLoop;
758                        }
759                        *pEvents->allocAppend() = event;
760                    }
761                }
762            }
763            exitVirtualDevicesLoop:
764            virtualMidiDevicesReader.Unlock();
765    
766            // import events from the regular MIDI devices
767            RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
768            Event* pEvent;
769            while (true) {
770                // get next event from input event queue
771                if (!(pEvent = eventQueueReader.pop())) break;
772                // if younger event reached, ignore that and all subsequent ones for now
773                if (pEvent->FragmentPos() >= Samples) {
774                    eventQueueReader--;
775                    dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
776                    pEvent->ResetFragmentPos();
777                    break;
778                }
779                // copy event to internal event list
780                if (pEvents->poolIsEmpty()) {
781                    dmsg(1,("Event pool emtpy!\n"));
782                    break;
783                }
784                *pEvents->allocAppend() = *pEvent;
785            }
786            eventQueueReader.free(); // free all copied events from input queue
787        }
788    
789        void EngineChannel::RemoveAllFxSends() {
790            if (pEngine) pEngine->DisableAndLock();
791            if (!fxSends.empty()) { // free local render buffers
792                if (pChannelLeft) {
793                    delete pChannelLeft;
794                    if (pEngine && pEngine->pAudioOutputDevice) {
795                        // fallback to render directly to the AudioOutputDevice's buffer
796                        pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
797                    } else pChannelLeft = NULL;
798                }
799                if (pChannelRight) {
800                    delete pChannelRight;
801                    if (pEngine && pEngine->pAudioOutputDevice) {
802                        // fallback to render directly to the AudioOutputDevice's buffer
803                        pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
804                    } else pChannelRight = NULL;
805                }
806            }
807            for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
808            fxSends.clear();
809            if (pEngine) pEngine->Enable();
810        }
811    
812        void EngineChannel::Connect(VirtualMidiDevice* pDevice) {
813            // double buffer ... double work ...
814            {
815                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
816                devices.add(pDevice);
817            }
818            {
819                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
820                devices.add(pDevice);
821            }
822        }
823    
824        void EngineChannel::Disconnect(VirtualMidiDevice* pDevice) {
825            // double buffer ... double work ...
826            {
827                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
828                devices.remove(pDevice);
829            }
830            {
831                ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
832                devices.remove(pDevice);
833            }
834        }
835    
836      float EngineChannel::Volume() {      float EngineChannel::Volume() {
837          return GlobalVolume;          return GlobalVolume;
838      }      }
839    
840      void EngineChannel::Volume(float f) {      void EngineChannel::Volume(float f) {
841          GlobalVolume = f;          GlobalVolume = f;
842            bStatusChanged = true; // status of engine channel has changed, so set notify flag
843      }      }
844    
845      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
# Line 376  namespace LinuxSampler { namespace gig { Line 860  namespace LinuxSampler { namespace gig {
860    
861      int EngineChannel::InstrumentStatus() {      int EngineChannel::InstrumentStatus() {
862          return InstrumentStat;          return InstrumentStat;
863      }          }
864    
865        String EngineChannel::EngineName() {
866            return LS_GIG_ENGINE_NAME;
867        }
868    
869  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

Legend:
Removed from v.412  
changed lines
  Added in v.1659

  ViewVC Help
Powered by ViewVC