/[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 670 by schoenebeck, Tue Jun 21 18:00:52 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>(CONFIG_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;
50          AudioDeviceChannelLeft  = -1;          AudioDeviceChannelLeft  = -1;
51          AudioDeviceChannelRight = -1;          AudioDeviceChannelRight = -1;
52            ResetControllers();
53      }      }
54    
55      EngineChannel::~EngineChannel() {      EngineChannel::~EngineChannel() {
56            DisconnectAudioOutputDevice();
57          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;  
             }  
         }  
58          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
59          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
60          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
61      }      }
62    
63      /**      /**
64         * Implementation of virtual method from abstract EngineChannel interface.
65         * This method will periodically be polled (e.g. by the LSCP server) to
66         * check if some engine channel parameter has changed since the last
67         * StatusChanged() call.
68         *
69         * This method can also be used to mark the engine channel as changed
70         * from outside, e.g. by a MIDI input device. The optional argument
71         * \a nNewStatus can be used for this.
72         *
73         * TODO: This "poll method" is just a lazy solution and might be
74         *       replaced in future.
75         * @param bNewStatus - (optional, default: false) sets the new status flag
76         * @returns true if engine channel status has changed since last
77         *          StatusChanged() call
78         */
79        bool EngineChannel::StatusChanged(bool bNewStatus) {
80            bool b = bStatusChanged;
81            bStatusChanged = bNewStatus;
82            return b;
83        }
84    
85        void EngineChannel::Reset() {
86            if (pEngine) pEngine->DisableAndLock();
87            ResetInternal();
88            ResetControllers();
89            if (pEngine) {
90                pEngine->Enable();
91                pEngine->Reset();
92            }
93        }
94    
95        /**
96       * This method is not thread safe!       * This method is not thread safe!
97       */       */
98      void EngineChannel::ResetInternal() {      void EngineChannel::ResetInternal() {
         Pitch               = 0;  
         SustainPedal        = false;  
         GlobalVolume        = 1.0;  
         GlobalPanLeft       = 1.0f;  
         GlobalPanRight      = 1.0f;  
99          CurrentKeyDimension = 0;          CurrentKeyDimension = 0;
100    
         // set all MIDI controller values to zero  
         memset(ControllerTable, 0x00, 128);  
   
         // reset voice stealing parameters  
         itLastStolenVoice = RTList<Voice>::Iterator();  
         iuiLastStolenKey  = RTList<uint>::Iterator();  
   
101          // reset key info          // reset key info
102          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
103              if (pMIDIKeyInfo[i].pActiveVoices)              if (pMIDIKeyInfo[i].pActiveVoices)
# Line 89  namespace LinuxSampler { namespace gig { Line 108  namespace LinuxSampler { namespace gig {
108              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
109              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
110              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();
111                pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
112          }          }
113    
114          // reset all key groups          // reset all key groups
# Line 102  namespace LinuxSampler { namespace gig { Line 122  namespace LinuxSampler { namespace gig {
122          pEventQueue->init();          pEventQueue->init();
123    
124          if (pEngine) pEngine->ResetInternal();          if (pEngine) pEngine->ResetInternal();
125    
126            // status of engine channel has changed, so set notify flag
127            bStatusChanged = true;
128      }      }
129    
130      LinuxSampler::Engine* EngineChannel::GetEngine() {      LinuxSampler::Engine* EngineChannel::GetEngine() {
# Line 136  namespace LinuxSampler { namespace gig { Line 159  namespace LinuxSampler { namespace gig {
159      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
160    
161          if (pEngine) pEngine->DisableAndLock();          if (pEngine) pEngine->DisableAndLock();
162            
163          ResetInternal();          ResetInternal();
164            
165          // free old instrument          // free old instrument
166          if (pInstrument) {          if (pInstrument) {
167              // give old instrument back to instrument manager              // give old instrument back to instrument manager
# Line 197  namespace LinuxSampler { namespace gig { Line 220  namespace LinuxSampler { namespace gig {
220    
221      /**      /**
222       * Will be called by the InstrumentResourceManager when the instrument       * Will be called by the InstrumentResourceManager when the instrument
223       * 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,
224       * can stop playback before that happens.       * so we can stop playback before that happens.
225       */       */
226      void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {      void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
227          dmsg(3,("gig::Engine: Received instrument update message.\n"));          dmsg(3,("gig::Engine: Received instrument update message.\n"));
# Line 214  namespace LinuxSampler { namespace gig { Line 237  namespace LinuxSampler { namespace gig {
237      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
238          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())
239          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
240            bStatusChanged = true; // status of engine has changed, so set notify flag
241        }
242    
243        /**
244         * Will be called by the InstrumentResourceManager on progress changes
245         * while loading or realoading an instrument for this EngineChannel.
246         *
247         * @param fProgress - current progress as value between 0.0 and 1.0
248         */
249        void EngineChannel::OnResourceProgress(float fProgress) {
250            this->InstrumentStat = int(fProgress * 100.0f);
251            dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));
252            bStatusChanged = true; // status of engine has changed, so set notify flag
253      }      }
254    
255      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
256          if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) {          if (pEngine) {
257                if (pEngine->pAudioOutputDevice == pAudioOut) return;
258              DisconnectAudioOutputDevice();              DisconnectAudioOutputDevice();
259          }          }
260          pEngine = Engine::AcquireEngine(this, pAudioOut);          pEngine = Engine::AcquireEngine(this, pAudioOut);
261          ResetInternal();                  ResetInternal();
262            pEvents   = new RTList<Event>(pEngine->pEventPool);
263            pCCEvents = new RTList<Event>(pEngine->pEventPool);
264            for (uint i = 0; i < Event::destination_count; i++) {
265                pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool);
266            }
267          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
268              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
269              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
# Line 235  namespace LinuxSampler { namespace gig { Line 277  namespace LinuxSampler { namespace gig {
277      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
278          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
279              ResetInternal();              ResetInternal();
280                if (pEvents) {
281                    delete pEvents;
282                    pEvents = NULL;
283                }
284                if (pCCEvents) {
285                    delete pCCEvents;
286                    pCCEvents = NULL;
287                }
288              for (uint i = 0; i < 128; i++) {              for (uint i = 0; i < 128; i++) {
289                  if (pMIDIKeyInfo[i].pActiveVoices) {                  if (pMIDIKeyInfo[i].pActiveVoices) {
290                      delete pMIDIKeyInfo[i].pActiveVoices;                      delete pMIDIKeyInfo[i].pActiveVoices;
# Line 245  namespace LinuxSampler { namespace gig { Line 295  namespace LinuxSampler { namespace gig {
295                      pMIDIKeyInfo[i].pEvents = NULL;                      pMIDIKeyInfo[i].pEvents = NULL;
296                  }                  }
297              }              }
298                for (uint i = 0; i < Event::destination_count; i++) {
299                    if (pSynthesisEvents[i]) {
300                        delete pSynthesisEvents[i];
301                        pSynthesisEvents[i] = NULL;
302                    }
303                }
304              Engine* oldEngine = pEngine;              Engine* oldEngine = pEngine;
305              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
306              pEngine = NULL;              pEngine = NULL;
307              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
308              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
309              AudioDeviceChannelRight = -1;                          AudioDeviceChannelRight = -1;
310          }          }
311      }      }
312    
313      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
314          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
315            
316          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
317          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
318          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
# Line 297  namespace LinuxSampler { namespace gig { Line 353  namespace LinuxSampler { namespace gig {
353              event.Type                = Event::type_note_on;              event.Type                = Event::type_note_on;
354              event.Param.Note.Key      = Key;              event.Param.Note.Key      = Key;
355              event.Param.Note.Velocity = Velocity;              event.Param.Note.Velocity = Velocity;
356              event.pEngineChannel      = this;                          event.pEngineChannel      = this;
357              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
358              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
359          }          }
# Line 329  namespace LinuxSampler { namespace gig { Line 385  namespace LinuxSampler { namespace gig {
385       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param Pitch - MIDI pitch value (-8192 ... +8191)
386       */       */
387      void EngineChannel::SendPitchbend(int Pitch) {      void EngineChannel::SendPitchbend(int Pitch) {
388          if (pEngine) {                  if (pEngine) {
389              Event event             = pEngine->pEventGenerator->CreateEvent();              Event event             = pEngine->pEventGenerator->CreateEvent();
390              event.Type              = Event::type_pitchbend;              event.Type              = Event::type_pitchbend;
391              event.Param.Pitch.Pitch = Pitch;              event.Param.Pitch.Pitch = Pitch;
# Line 358  namespace LinuxSampler { namespace gig { Line 414  namespace LinuxSampler { namespace gig {
414          }          }
415      }      }
416    
417        void EngineChannel::ClearEventLists() {
418            pEvents->clear();
419            pCCEvents->clear();
420            for (uint i = 0; i < Event::destination_count; i++) {
421                pSynthesisEvents[i]->clear();
422            }
423            // empty MIDI key specific event lists
424            {
425                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
426                RTList<uint>::Iterator end    = pActiveKeys->end();
427                for(; iuiKey != end; ++iuiKey) {
428                    pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
429                }
430            }
431        }
432    
433        void EngineChannel::ResetControllers() {
434            Pitch          = 0;
435            SustainPedal   = false;
436            GlobalVolume   = 1.0;
437            GlobalPanLeft  = 1.0f;
438            GlobalPanRight = 1.0f;
439            // set all MIDI controller values to zero
440            memset(ControllerTable, 0x00, 128);
441        }
442    
443        /**
444         * Copy all events from the engine channel's input event queue buffer to
445         * the internal event list. This will be done at the beginning of each
446         * audio cycle (that is each RenderAudio() call) to distinguish all
447         * events which have to be processed in the current audio cycle. Each
448         * EngineChannel has it's own input event queue for the common channel
449         * specific events (like NoteOn, NoteOff and ControlChange events).
450         * Beside that, the engine also has a input event queue for global
451         * events (usually SysEx messages).
452         *
453         * @param Samples - number of sample points to be processed in the
454         *                  current audio cycle
455         */
456        void EngineChannel::ImportEvents(uint Samples) {
457            RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
458            Event* pEvent;
459            while (true) {
460                // get next event from input event queue
461                if (!(pEvent = eventQueueReader.pop())) break;
462                // if younger event reached, ignore that and all subsequent ones for now
463                if (pEvent->FragmentPos() >= Samples) {
464                    eventQueueReader--;
465                    dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
466                    pEvent->ResetFragmentPos();
467                    break;
468                }
469                // copy event to internal event list
470                if (pEvents->poolIsEmpty()) {
471                    dmsg(1,("Event pool emtpy!\n"));
472                    break;
473                }
474                *pEvents->allocAppend() = *pEvent;
475            }
476            eventQueueReader.free(); // free all copied events from input queue
477        }
478    
479      float EngineChannel::Volume() {      float EngineChannel::Volume() {
480          return GlobalVolume;          return GlobalVolume;
481      }      }
482    
483      void EngineChannel::Volume(float f) {      void EngineChannel::Volume(float f) {
484          GlobalVolume = f;          GlobalVolume = f;
485            bStatusChanged = true; // status of engine channel has changed, so set notify flag
486      }      }
487    
488      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
# Line 384  namespace LinuxSampler { namespace gig { Line 503  namespace LinuxSampler { namespace gig {
503    
504      int EngineChannel::InstrumentStatus() {      int EngineChannel::InstrumentStatus() {
505          return InstrumentStat;          return InstrumentStat;
506      }          }
507    
508        String EngineChannel::EngineName() {
509            return LS_GIG_ENGINE_NAME;
510        }
511    
512  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

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

  ViewVC Help
Powered by ViewVC