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

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

  ViewVC Help
Powered by ViewVC