/[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 675 by schoenebeck, Wed Jun 22 22:09:28 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            pMidiInputPort = NULL;
53            midiChannel = midi_chan_all;
54            ResetControllers();
55      }      }
56    
57      EngineChannel::~EngineChannel() {      EngineChannel::~EngineChannel() {
58            DisconnectAudioOutputDevice();
59          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;  
             }  
         }  
60          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
61          if (pActiveKeys) delete pActiveKeys;          if (pActiveKeys) delete pActiveKeys;
62          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;          if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
63      }      }
64    
65      /**      /**
66         * Implementation of virtual method from abstract EngineChannel interface.
67         * This method will periodically be polled (e.g. by the LSCP server) to
68         * check if some engine channel parameter has changed since the last
69         * StatusChanged() call.
70         *
71         * This method can also be used to mark the engine channel as changed
72         * from outside, e.g. by a MIDI input device. The optional argument
73         * \a nNewStatus can be used for this.
74         *
75         * TODO: This "poll method" is just a lazy solution and might be
76         *       replaced in future.
77         * @param bNewStatus - (optional, default: false) sets the new status flag
78         * @returns true if engine channel status has changed since last
79         *          StatusChanged() call
80         */
81        bool EngineChannel::StatusChanged(bool bNewStatus) {
82            bool b = bStatusChanged;
83            bStatusChanged = bNewStatus;
84            return b;
85        }
86    
87        void EngineChannel::Reset() {
88            if (pEngine) pEngine->DisableAndLock();
89            ResetInternal();
90            ResetControllers();
91            if (pEngine) {
92                pEngine->Enable();
93                pEngine->Reset();
94            }
95        }
96    
97        /**
98       * This method is not thread safe!       * This method is not thread safe!
99       */       */
100      void EngineChannel::ResetInternal() {      void EngineChannel::ResetInternal() {
         Pitch               = 0;  
         SustainPedal        = false;  
         GlobalVolume        = 1.0;  
101          CurrentKeyDimension = 0;          CurrentKeyDimension = 0;
102    
         // set all MIDI controller values to zero  
         memset(ControllerTable, 0x00, 128);  
   
         // reset voice stealing parameters  
         itLastStolenVoice = RTList<Voice>::Iterator();  
         iuiLastStolenKey  = RTList<uint>::Iterator();  
   
103          // reset key info          // reset key info
104          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
105              if (pMIDIKeyInfo[i].pActiveVoices)              if (pMIDIKeyInfo[i].pActiveVoices)
# Line 87  namespace LinuxSampler { namespace gig { Line 110  namespace LinuxSampler { namespace gig {
110              pMIDIKeyInfo[i].Active         = false;              pMIDIKeyInfo[i].Active         = false;
111              pMIDIKeyInfo[i].ReleaseTrigger = false;              pMIDIKeyInfo[i].ReleaseTrigger = false;
112              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();              pMIDIKeyInfo[i].itSelf         = Pool<uint>::Iterator();
113                pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
114          }          }
115    
116          // reset all key groups          // reset all key groups
# Line 100  namespace LinuxSampler { namespace gig { Line 124  namespace LinuxSampler { namespace gig {
124          pEventQueue->init();          pEventQueue->init();
125    
126          if (pEngine) pEngine->ResetInternal();          if (pEngine) pEngine->ResetInternal();
127    
128            // status of engine channel has changed, so set notify flag
129            bStatusChanged = true;
130      }      }
131    
132      LinuxSampler::Engine* EngineChannel::GetEngine() {      LinuxSampler::Engine* EngineChannel::GetEngine() {
# Line 134  namespace LinuxSampler { namespace gig { Line 161  namespace LinuxSampler { namespace gig {
161      void EngineChannel::LoadInstrument() {      void EngineChannel::LoadInstrument() {
162    
163          if (pEngine) pEngine->DisableAndLock();          if (pEngine) pEngine->DisableAndLock();
164            
165          ResetInternal();          ResetInternal();
166            
167          // free old instrument          // free old instrument
168          if (pInstrument) {          if (pInstrument) {
169              // give old instrument back to instrument manager              // give old instrument back to instrument manager
# Line 195  namespace LinuxSampler { namespace gig { Line 222  namespace LinuxSampler { namespace gig {
222    
223      /**      /**
224       * Will be called by the InstrumentResourceManager when the instrument       * Will be called by the InstrumentResourceManager when the instrument
225       * 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,
226       * can stop playback before that happens.       * so we can stop playback before that happens.
227       */       */
228      void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {      void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
229          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 239  namespace LinuxSampler { namespace gig {
239      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {      void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
240          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())
241          if (pEngine) pEngine->Enable();          if (pEngine) pEngine->Enable();
242            bStatusChanged = true; // status of engine has changed, so set notify flag
243        }
244    
245        /**
246         * Will be called by the InstrumentResourceManager on progress changes
247         * while loading or realoading an instrument for this EngineChannel.
248         *
249         * @param fProgress - current progress as value between 0.0 and 1.0
250         */
251        void EngineChannel::OnResourceProgress(float fProgress) {
252            this->InstrumentStat = int(fProgress * 100.0f);
253            dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));
254            bStatusChanged = true; // status of engine has changed, so set notify flag
255      }      }
256    
257      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {      void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
258          if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) {          if (pEngine) {
259                if (pEngine->pAudioOutputDevice == pAudioOut) return;
260              DisconnectAudioOutputDevice();              DisconnectAudioOutputDevice();
261          }          }
262          pEngine = Engine::AcquireEngine(this, pAudioOut);          pEngine = Engine::AcquireEngine(this, pAudioOut);
263          ResetInternal();                  ResetInternal();
264            pEvents   = new RTList<Event>(pEngine->pEventPool);
265            pCCEvents = new RTList<Event>(pEngine->pEventPool);
266            for (uint i = 0; i < Event::destination_count; i++) {
267                pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool);
268            }
269          for (uint i = 0; i < 128; i++) {          for (uint i = 0; i < 128; i++) {
270              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);              pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
271              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);              pMIDIKeyInfo[i].pEvents       = new RTList<Event>(pEngine->pEventPool);
# Line 233  namespace LinuxSampler { namespace gig { Line 279  namespace LinuxSampler { namespace gig {
279      void EngineChannel::DisconnectAudioOutputDevice() {      void EngineChannel::DisconnectAudioOutputDevice() {
280          if (pEngine) { // if clause to prevent disconnect loops          if (pEngine) { // if clause to prevent disconnect loops
281              ResetInternal();              ResetInternal();
282                if (pEvents) {
283                    delete pEvents;
284                    pEvents = NULL;
285                }
286                if (pCCEvents) {
287                    delete pCCEvents;
288                    pCCEvents = NULL;
289                }
290              for (uint i = 0; i < 128; i++) {              for (uint i = 0; i < 128; i++) {
291                  if (pMIDIKeyInfo[i].pActiveVoices) delete pMIDIKeyInfo[i].pActiveVoices;                  if (pMIDIKeyInfo[i].pActiveVoices) {
292                  if (pMIDIKeyInfo[i].pEvents)       delete pMIDIKeyInfo[i].pEvents;                      delete pMIDIKeyInfo[i].pActiveVoices;
293                        pMIDIKeyInfo[i].pActiveVoices = NULL;
294                    }
295                    if (pMIDIKeyInfo[i].pEvents) {
296                        delete pMIDIKeyInfo[i].pEvents;
297                        pMIDIKeyInfo[i].pEvents = NULL;
298                    }
299                }
300                for (uint i = 0; i < Event::destination_count; i++) {
301                    if (pSynthesisEvents[i]) {
302                        delete pSynthesisEvents[i];
303                        pSynthesisEvents[i] = NULL;
304                    }
305              }              }
306              Engine* oldEngine = pEngine;              Engine* oldEngine = pEngine;
307              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;              AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
308              pEngine = NULL;              pEngine = NULL;
309              Engine::FreeEngine(this, oldAudioDevice);              Engine::FreeEngine(this, oldAudioDevice);
310              AudioDeviceChannelLeft  = -1;              AudioDeviceChannelLeft  = -1;
311              AudioDeviceChannelRight = -1;                          AudioDeviceChannelRight = -1;
312          }          }
313      }      }
314    
315      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {      void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
316          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");          if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
317            
318          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);          AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
319          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));          if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
320          switch (EngineAudioChannel) {          switch (EngineAudioChannel) {
# Line 276  namespace LinuxSampler { namespace gig { Line 342  namespace LinuxSampler { namespace gig {
342          }          }
343      }      }
344    
345        void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) {
346            if (!pMidiPort || pMidiPort == this->pMidiInputPort) return;
347            DisconnectMidiInputPort();
348            this->pMidiInputPort = pMidiPort;
349            this->midiChannel    = MidiChannel;
350            pMidiPort->Connect(this, MidiChannel);
351        }
352    
353        void EngineChannel::DisconnectMidiInputPort() {
354            MidiInputPort* pOldPort = this->pMidiInputPort;
355            this->pMidiInputPort = NULL;
356            if (pOldPort) pOldPort->Disconnect(this);
357        }
358    
359        MidiInputPort* EngineChannel::GetMidiInputPort() {
360            return pMidiInputPort;
361        }
362    
363        midi_chan_t EngineChannel::MidiChannel() {
364            return midiChannel;
365        }
366    
367      /**      /**
368       *  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
369       *  voice for the given key.       *  voice for the given key.
# Line 289  namespace LinuxSampler { namespace gig { Line 377  namespace LinuxSampler { namespace gig {
377              event.Type                = Event::type_note_on;              event.Type                = Event::type_note_on;
378              event.Param.Note.Key      = Key;              event.Param.Note.Key      = Key;
379              event.Param.Note.Velocity = Velocity;              event.Param.Note.Velocity = Velocity;
380              event.pEngineChannel      = this;                          event.pEngineChannel      = this;
381              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);              if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
382              else dmsg(1,("EngineChannel: Input event queue full!"));              else dmsg(1,("EngineChannel: Input event queue full!"));
383          }          }
# Line 321  namespace LinuxSampler { namespace gig { Line 409  namespace LinuxSampler { namespace gig {
409       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param Pitch - MIDI pitch value (-8192 ... +8191)
410       */       */
411      void EngineChannel::SendPitchbend(int Pitch) {      void EngineChannel::SendPitchbend(int Pitch) {
412          if (pEngine) {                  if (pEngine) {
413              Event event             = pEngine->pEventGenerator->CreateEvent();              Event event             = pEngine->pEventGenerator->CreateEvent();
414              event.Type              = Event::type_pitchbend;              event.Type              = Event::type_pitchbend;
415              event.Param.Pitch.Pitch = Pitch;              event.Param.Pitch.Pitch = Pitch;
# Line 350  namespace LinuxSampler { namespace gig { Line 438  namespace LinuxSampler { namespace gig {
438          }          }
439      }      }
440    
441        void EngineChannel::ClearEventLists() {
442            pEvents->clear();
443            pCCEvents->clear();
444            for (uint i = 0; i < Event::destination_count; i++) {
445                pSynthesisEvents[i]->clear();
446            }
447            // empty MIDI key specific event lists
448            {
449                RTList<uint>::Iterator iuiKey = pActiveKeys->first();
450                RTList<uint>::Iterator end    = pActiveKeys->end();
451                for(; iuiKey != end; ++iuiKey) {
452                    pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
453                }
454            }
455        }
456    
457        void EngineChannel::ResetControllers() {
458            Pitch          = 0;
459            SustainPedal   = false;
460            GlobalVolume   = 1.0;
461            GlobalPanLeft  = 1.0f;
462            GlobalPanRight = 1.0f;
463            // set all MIDI controller values to zero
464            memset(ControllerTable, 0x00, 128);
465        }
466    
467        /**
468         * Copy all events from the engine channel's input event queue buffer to
469         * the internal event list. This will be done at the beginning of each
470         * audio cycle (that is each RenderAudio() call) to distinguish all
471         * events which have to be processed in the current audio cycle. Each
472         * EngineChannel has it's own input event queue for the common channel
473         * specific events (like NoteOn, NoteOff and ControlChange events).
474         * Beside that, the engine also has a input event queue for global
475         * events (usually SysEx messages).
476         *
477         * @param Samples - number of sample points to be processed in the
478         *                  current audio cycle
479         */
480        void EngineChannel::ImportEvents(uint Samples) {
481            RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
482            Event* pEvent;
483            while (true) {
484                // get next event from input event queue
485                if (!(pEvent = eventQueueReader.pop())) break;
486                // if younger event reached, ignore that and all subsequent ones for now
487                if (pEvent->FragmentPos() >= Samples) {
488                    eventQueueReader--;
489                    dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
490                    pEvent->ResetFragmentPos();
491                    break;
492                }
493                // copy event to internal event list
494                if (pEvents->poolIsEmpty()) {
495                    dmsg(1,("Event pool emtpy!\n"));
496                    break;
497                }
498                *pEvents->allocAppend() = *pEvent;
499            }
500            eventQueueReader.free(); // free all copied events from input queue
501        }
502    
503      float EngineChannel::Volume() {      float EngineChannel::Volume() {
504          return GlobalVolume;          return GlobalVolume;
505      }      }
506    
507      void EngineChannel::Volume(float f) {      void EngineChannel::Volume(float f) {
508          GlobalVolume = f;          GlobalVolume = f;
509            bStatusChanged = true; // status of engine channel has changed, so set notify flag
510      }      }
511    
512      uint EngineChannel::Channels() {      uint EngineChannel::Channels() {
# Line 376  namespace LinuxSampler { namespace gig { Line 527  namespace LinuxSampler { namespace gig {
527    
528      int EngineChannel::InstrumentStatus() {      int EngineChannel::InstrumentStatus() {
529          return InstrumentStat;          return InstrumentStat;
530      }          }
531    
532        String EngineChannel::EngineName() {
533            return LS_GIG_ENGINE_NAME;
534        }
535    
536  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

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

  ViewVC Help
Powered by ViewVC