/[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 424 by schoenebeck, Fri Mar 4 22:54:11 2005 UTC revision 475 by schoenebeck, Thu Mar 17 23:56:56 2005 UTC
# Line 23  Line 23 
23    
24  #include "EngineChannel.h"  #include "EngineChannel.h"
25    
26  namespace LinuxSampler { namespace gig {      namespace LinuxSampler { namespace gig {
27    
28      EngineChannel::EngineChannel() {      EngineChannel::EngineChannel() {
29          pMIDIKeyInfo = new midi_key_info_t[128];          pMIDIKeyInfo = new midi_key_info_t[128];
30          pEngine      = NULL;          pEngine      = NULL;
31          pInstrument  = NULL;          pInstrument  = NULL;
32          pEventQueue  = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);                  pEvents      = NULL; // we allocate when we retrieve the right Engine object
33            pCCEvents    = NULL; // we allocate when we retrieve the right Engine object
34            pEventQueue  = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);
35          pActiveKeys  = new Pool<uint>(128);          pActiveKeys  = new Pool<uint>(128);
36          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
37              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 39  namespace LinuxSampler { namespace gig {
39              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
40              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
41              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
42                pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
43                pMIDIKeyInfo[i].RoundRobinIndex = 0;
44            }
45            for (uint i = 0; i < Event::destination_count; i++) {
46                pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object
47          }          }
48          InstrumentIdx  = -1;          InstrumentIdx  = -1;
49          InstrumentStat = -1;          InstrumentStat = -1;
# Line 45  namespace LinuxSampler { namespace gig { Line 52  namespace LinuxSampler { namespace gig {
52      }      }
53    
54      EngineChannel::~EngineChannel() {      EngineChannel::~EngineChannel() {
55            DisconnectAudioOutputDevice();
56          if (pInstrument) Engine::instruments.HandBack(pInstrument, this);          if (pInstrument) Engine::instruments.HandBack(pInstrument, this);
         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;  
             }  
         }  
57          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
58          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
59          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
# Line 72  namespace LinuxSampler { namespace gig { Line 70  namespace LinuxSampler { namespace gig {
70          GlobalPanRight      = 1.0f;          GlobalPanRight      = 1.0f;
71          CurrentKeyDimension = 0;          CurrentKeyDimension = 0;
72    
73          // set all MIDI controller values to zero          ResetControllers();
         memset(ControllerTable, 0x00, 128);  
   
         // reset voice stealing parameters  
         itLastStolenVoice = RTList<Voice>::Iterator();  
         iuiLastStolenKey  = RTList<uint>::Iterator();  
74    
75          // reset key info          // reset key info
76          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
# Line 89  namespace LinuxSampler { namespace gig { Line 82  namespace LinuxSampler { namespace gig {
82              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
83              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
84              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();
85                pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
86          }          }
87    
88          // reset all key groups          // reset all key groups
# Line 136  namespace LinuxSampler { namespace gig { Line 130  namespace LinuxSampler { namespace gig {
130      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
131    
132          if (pEngine) pEngine->DisableAndLock();          if (pEngine) pEngine->DisableAndLock();
133            
134          ResetInternal();          ResetInternal();
135            
136          // free old instrument          // free old instrument
137          if (pInstrument) {          if (pInstrument) {
138              // give old instrument back to instrument manager              // give old instrument back to instrument manager
# Line 217  namespace LinuxSampler { namespace gig { Line 211  namespace LinuxSampler { namespace gig {
211      }      }
212    
213      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
214          if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) {          if (pEngine) {
215                if (pEngine->pAudioOutputDevice == pAudioOut) return;
216              DisconnectAudioOutputDevice();              DisconnectAudioOutputDevice();
217          }          }
218          pEngine = Engine::AcquireEngine(this, pAudioOut);          pEngine = Engine::AcquireEngine(this, pAudioOut);
219          ResetInternal();                  ResetInternal();
220            pEvents   = new RTList<Event>(pEngine->pEventPool);
221            pCCEvents = new RTList<Event>(pEngine->pEventPool);
222            for (uint i = 0; i < Event::destination_count; i++) {
223                pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool);
224            }
225          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
226              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
227              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
# Line 235  namespace LinuxSampler { namespace gig { Line 235  namespace LinuxSampler { namespace gig {
235      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
236          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
237              ResetInternal();              ResetInternal();
238                if (pEvents) {
239                    delete pEvents;
240                    pEvents = NULL;
241                }
242                if (pCCEvents) {
243                    delete pCCEvents;
244                    pCCEvents = NULL;
245                }
246              for (uint i = 0; i < 128; i++) {              for (uint i = 0; i < 128; i++) {
247                  if (pMIDIKeyInfo[i].pActiveVoices) {                  if (pMIDIKeyInfo[i].pActiveVoices) {
248                      delete pMIDIKeyInfo[i].pActiveVoices;                      delete pMIDIKeyInfo[i].pActiveVoices;
# Line 245  namespace LinuxSampler { namespace gig { Line 253  namespace LinuxSampler { namespace gig {
253                      pMIDIKeyInfo[i].pEvents = NULL;                      pMIDIKeyInfo[i].pEvents = NULL;
254                  }                  }
255              }              }
256                for (uint i = 0; i < Event::destination_count; i++) {
257                    if (pSynthesisEvents[i]) {
258                        delete pSynthesisEvents[i];
259                        pSynthesisEvents[i] = NULL;
260                    }
261                }
262              Engine* oldEngine = pEngine;              Engine* oldEngine = pEngine;
263              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
264              pEngine = NULL;              pEngine = NULL;
265              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
266              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
267              AudioDeviceChannelRight = -1;                          AudioDeviceChannelRight = -1;
268          }          }
269      }      }
270    
271      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
272          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
273            
274          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
275          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
276          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
# Line 297  namespace LinuxSampler { namespace gig { Line 311  namespace LinuxSampler { namespace gig {
311              event.Type                = Event::type_note_on;              event.Type                = Event::type_note_on;
312              event.Param.Note.Key      = Key;              event.Param.Note.Key      = Key;
313              event.Param.Note.Velocity = Velocity;              event.Param.Note.Velocity = Velocity;
314              event.pEngineChannel      = this;                          event.pEngineChannel      = this;
315              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
316              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
317          }          }
# Line 329  namespace LinuxSampler { namespace gig { Line 343  namespace LinuxSampler { namespace gig {
343       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param Pitch - MIDI pitch value (-8192 ... +8191)
344       */       */
345      void EngineChannel::SendPitchbend(int Pitch) {      void EngineChannel::SendPitchbend(int Pitch) {
346          if (pEngine) {                  if (pEngine) {
347              Event event             = pEngine->pEventGenerator->CreateEvent();              Event event             = pEngine->pEventGenerator->CreateEvent();
348              event.Type              = Event::type_pitchbend;              event.Type              = Event::type_pitchbend;
349              event.Param.Pitch.Pitch = Pitch;              event.Param.Pitch.Pitch = Pitch;
# Line 358  namespace LinuxSampler { namespace gig { Line 372  namespace LinuxSampler { namespace gig {
372          }          }
373      }      }
374    
375        void EngineChannel::ClearEventLists() {
376            pEvents->clear();
377            pCCEvents->clear();
378            for (uint i = 0; i < Event::destination_count; i++) {
379                pSynthesisEvents[i]->clear();
380            }
381            // empty MIDI key specific event lists
382            {
383                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
384                RTList<uint>::Iterator end    = pActiveKeys->end();
385                for(; iuiKey != end; ++iuiKey) {
386                    pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
387                }
388            }
389        }
390    
391        void EngineChannel::ResetControllers() {
392            // set all MIDI controller values to zero
393            memset(ControllerTable, 0x00, 128);
394        }
395    
396        /**
397         * Copy all events from the engine channel's input event queue buffer to
398         * the internal event list. This will be done at the beginning of each
399         * audio cycle (that is each RenderAudio() call) to distinguish all
400         * events which have to be processed in the current audio cycle. Each
401         * EngineChannel has it's own input event queue for the common channel
402         * specific events (like NoteOn, NoteOff and ControlChange events).
403         * Beside that, the engine also has a input event queue for global
404         * events (usually SysEx messages).
405         *
406         * @param Samples - number of sample points to be processed in the
407         *                  current audio cycle
408         */
409        void EngineChannel::ImportEvents(uint Samples) {
410            RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
411            Event* pEvent;
412            while (true) {
413                // get next event from input event queue
414                if (!(pEvent = eventQueueReader.pop())) break;
415                // if younger event reached, ignore that and all subsequent ones for now
416                if (pEvent->FragmentPos() >= Samples) {
417                    eventQueueReader--;
418                    dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
419                    pEvent->ResetFragmentPos();
420                    break;
421                }
422                // copy event to internal event list
423                if (pEvents->poolIsEmpty()) {
424                    dmsg(1,("Event pool emtpy!\n"));
425                    break;
426                }
427                *pEvents->allocAppend() = *pEvent;
428            }
429            eventQueueReader.free(); // free all copied events from input queue
430        }
431    
432      float EngineChannel::Volume() {      float EngineChannel::Volume() {
433          return GlobalVolume;          return GlobalVolume;
434      }      }
# Line 384  namespace LinuxSampler { namespace gig { Line 455  namespace LinuxSampler { namespace gig {
455    
456      int EngineChannel::InstrumentStatus() {      int EngineChannel::InstrumentStatus() {
457          return InstrumentStat;          return InstrumentStat;
458      }          }
459    
460        String EngineChannel::EngineName() {
461            return LS_GIG_ENGINE_NAME;
462        }
463        
464  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

Legend:
Removed from v.424  
changed lines
  Added in v.475

  ViewVC Help
Powered by ViewVC