/[svn]/linuxsampler/trunk/src/engines/gig/Engine.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/gig/Engine.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 123 by schoenebeck, Mon Jun 14 19:33:16 2004 UTC revision 438 by persson, Wed Mar 9 22:12:15 2005 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                              *
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 24 
24  #include <sstream>  #include <sstream>
25  #include "DiskThread.h"  #include "DiskThread.h"
26  #include "Voice.h"  #include "Voice.h"
27    #include "EGADSR.h"
28    #include "../EngineFactory.h"
29    
30  #include "Engine.h"  #include "Engine.h"
31    
32    #if defined(__APPLE__)
33    # include <stdlib.h>
34    #else
35    # include <malloc.h>
36    #endif
37    
38  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
39    
40      InstrumentResourceManager Engine::Instruments;      InstrumentResourceManager Engine::instruments;
41    
42        std::map<AudioOutputDevice*,Engine*> Engine::engines;
43    
44        /**
45         * Get a gig::Engine object for the given gig::EngineChannel and the
46         * given AudioOutputDevice. All engine channels which are connected to
47         * the same audio output device will use the same engine instance. This
48         * method will be called by a gig::EngineChannel whenever it's
49         * connecting to a audio output device.
50         *
51         * @param pChannel - engine channel which acquires an engine object
52         * @param pDevice  - the audio output device \a pChannel is connected to
53         */
54        Engine* Engine::AcquireEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) {
55            Engine* pEngine = NULL;
56            // check if there's already an engine for the given audio output device
57            if (engines.count(pDevice)) {
58                dmsg(4,("Using existing gig::Engine.\n"));
59                pEngine = engines[pDevice];
60            } else { // create a new engine (and disk thread) instance for the given audio output device
61                dmsg(4,("Creating new gig::Engine.\n"));
62                pEngine = (Engine*) EngineFactory::Create("gig");
63                pEngine->Connect(pDevice);
64                engines[pDevice] = pEngine;
65            }
66            // register engine channel to the engine instance
67            pEngine->engineChannels.push_back(pChannel);
68            dmsg(4,("This gig::Engine has now %d EngineChannels.\n",pEngine->engineChannels.size()));
69            return pEngine;
70        }
71    
72        /**
73         * Once an engine channel is disconnected from an audio output device,
74         * it wil immediately call this method to unregister itself from the
75         * engine instance and if that engine instance is not used by any other
76         * engine channel anymore, then that engine instance will be destroyed.
77         *
78         * @param pChannel - engine channel which wants to disconnect from it's
79         *                   engine instance
80         * @param pDevice  - audio output device \a pChannel was connected to
81         */
82        void Engine::FreeEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) {
83            dmsg(4,("Disconnecting EngineChannel from gig::Engine.\n"));
84            Engine* pEngine = engines[pDevice];
85            // unregister EngineChannel from the Engine instance
86            pEngine->engineChannels.remove(pChannel);
87            // if the used Engine instance is not used anymore, then destroy it
88            if (pEngine->engineChannels.empty()) {
89                pDevice->Disconnect(pEngine);
90                engines.erase(pDevice);
91                delete pEngine;
92                dmsg(4,("Destroying gig::Engine.\n"));
93            }
94            else dmsg(4,("This gig::Engine has now %d EngineChannels.\n",pEngine->engineChannels.size()));
95        }
96    
97      Engine::Engine() {      Engine::Engine() {
         pRIFF              = NULL;  
         pGig               = NULL;  
         pInstrument        = NULL;  
98          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
99          pDiskThread        = NULL;          pDiskThread        = NULL;
100          pEventGenerator    = NULL;          pEventGenerator    = NULL;
101          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT);          pSysexBuffer       = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0);
102          pEventPool         = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT);          pEventQueue        = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0);
103          pVoicePool         = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES);          pEventPool         = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT);
104          pActiveKeys        = new RTELMemoryPool<uint>(128);          pVoicePool         = new Pool<Voice>(MAX_AUDIO_VOICES);
105          pEvents            = new RTEList<Event>(pEventPool);          pVoiceStealingQueue = new RTList<Event>(pEventPool);
106          pCCEvents          = new RTEList<Event>(pEventPool);          pEvents            = new RTList<Event>(pEventPool);
107            pCCEvents          = new RTList<Event>(pEventPool);
108    
109          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
110              pSynthesisEvents[i] = new RTEList<Event>(pEventPool);              pSynthesisEvents[i] = new RTList<Event>(pEventPool);
111          }          }
112          for (uint i = 0; i < 128; i++) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
113              pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice>(pVoicePool);              iterVoice->SetEngine(this);
             pMIDIKeyInfo[i].KeyPressed    = false;  
             pMIDIKeyInfo[i].Active        = false;  
             pMIDIKeyInfo[i].pSelf         = NULL;  
             pMIDIKeyInfo[i].pEvents       = new RTEList<Event>(pEventPool);  
         }  
         for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {  
             pVoice->SetEngine(this);  
114          }          }
115          pVoicePool->clear();          pVoicePool->clear();
116    
# Line 62  namespace LinuxSampler { namespace gig { Line 118  namespace LinuxSampler { namespace gig {
118          pBasicFilterParameters  = NULL;          pBasicFilterParameters  = NULL;
119          pMainFilterParameters   = NULL;          pMainFilterParameters   = NULL;
120    
         InstrumentIdx = -1;  
   
121          ResetInternal();          ResetInternal();
122      }      }
123    
124      Engine::~Engine() {      Engine::~Engine() {
125          if (pDiskThread) {          if (pDiskThread) {
126                dmsg(1,("Stopping disk thread..."));
127              pDiskThread->StopThread();              pDiskThread->StopThread();
128              delete pDiskThread;              delete pDiskThread;
129          }              dmsg(1,("OK\n"));
         if (pGig)  delete pGig;  
         if (pRIFF) delete pRIFF;  
         for (uint i = 0; i < 128; i++) {  
             if (pMIDIKeyInfo[i].pActiveVoices) delete pMIDIKeyInfo[i].pActiveVoices;  
             if (pMIDIKeyInfo[i].pEvents)       delete pMIDIKeyInfo[i].pEvents;  
130          }          }
131          for (uint i = 0; i < Event::destination_count; i++) {          for (uint i = 0; i < Event::destination_count; i++) {
132              if (pSynthesisEvents[i]) delete pSynthesisEvents[i];              if (pSynthesisEvents[i]) delete pSynthesisEvents[i];
133          }          }
         delete[] pSynthesisEvents;  
134          if (pEvents)     delete pEvents;          if (pEvents)     delete pEvents;
135          if (pCCEvents)   delete pCCEvents;          if (pCCEvents)   delete pCCEvents;
136          if (pEventQueue) delete pEventQueue;          if (pEventQueue) delete pEventQueue;
137          if (pEventPool)  delete pEventPool;          if (pEventPool)  delete pEventPool;
138          if (pVoicePool)  delete pVoicePool;          if (pVoicePool) {
139          if (pActiveKeys) delete pActiveKeys;              pVoicePool->clear();
140                delete pVoicePool;
141            }
142          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
143          if (pMainFilterParameters) delete[] pMainFilterParameters;          if (pMainFilterParameters) delete[] pMainFilterParameters;
144          if (pBasicFilterParameters) delete[] pBasicFilterParameters;          if (pBasicFilterParameters) delete[] pBasicFilterParameters;
145          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);
146            if (pVoiceStealingQueue) delete pVoiceStealingQueue;
147            if (pSysexBuffer) delete pSysexBuffer;
148            EngineFactory::Destroy(this);
149      }      }
150    
151      void Engine::Enable() {      void Engine::Enable() {
# Line 118  namespace LinuxSampler { namespace gig { Line 172  namespace LinuxSampler { namespace gig {
172       */       */
173      void Engine::Reset() {      void Engine::Reset() {
174          DisableAndLock();          DisableAndLock();
   
         //if (pAudioOutputDevice->IsPlaying()) { // if already running  
             /*  
             // signal audio thread not to enter render part anymore  
             SuspensionRequested = true;  
             // sleep until wakened by audio thread  
             pthread_mutex_lock(&__render_state_mutex);  
             pthread_cond_wait(&__render_exit_condition, &__render_state_mutex);  
             pthread_mutex_unlock(&__render_state_mutex);  
             */  
         //}  
   
         //if (wasplaying) pAudioOutputDevice->Stop();  
   
175          ResetInternal();          ResetInternal();
   
         // signal audio thread to continue with rendering  
         //SuspensionRequested = false;  
176          Enable();          Enable();
177      }      }
178    
# Line 144  namespace LinuxSampler { namespace gig { Line 181  namespace LinuxSampler { namespace gig {
181       *  control and status variables. This method is not thread safe!       *  control and status variables. This method is not thread safe!
182       */       */
183      void Engine::ResetInternal() {      void Engine::ResetInternal() {
         Pitch               = 0;  
         SustainPedal        = false;  
184          ActiveVoiceCount    = 0;          ActiveVoiceCount    = 0;
185          ActiveVoiceCountMax = 0;          ActiveVoiceCountMax = 0;
186    
187          // set all MIDI controller values to zero          // reset voice stealing parameters
188          memset(ControllerTable, 0x00, 128);          pVoiceStealingQueue->clear();
189    
190          // reset key info          // reset to normal chromatic scale (means equal temper)
191          for (uint i = 0; i < 128; i++) {          memset(&ScaleTuning[0], 0x00, 12);
             pMIDIKeyInfo[i].pActiveVoices->clear();  
             pMIDIKeyInfo[i].pEvents->clear();  
             pMIDIKeyInfo[i].KeyPressed = false;  
             pMIDIKeyInfo[i].Active     = false;  
             pMIDIKeyInfo[i].pSelf      = NULL;  
         }  
192    
193          // reset all voices          // reset all voices
194          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
195              pVoice->Reset();              iterVoice->Reset();
196          }          }
197          pVoicePool->clear();          pVoicePool->clear();
198    
         // free all active keys  
         pActiveKeys->clear();  
   
199          // reset disk thread          // reset disk thread
200          if (pDiskThread) pDiskThread->Reset();          if (pDiskThread) pDiskThread->Reset();
201    
# Line 177  namespace LinuxSampler { namespace gig { Line 203  namespace LinuxSampler { namespace gig {
203          pEventQueue->init();          pEventQueue->init();
204      }      }
205    
     /**  
      *  Load an instrument from a .gig file.  
      *  
      *  @param FileName   - file name of the Gigasampler instrument file  
      *  @param Instrument - index of the instrument in the .gig file  
      *  @throws LinuxSamplerException  on error  
      *  @returns          detailed description of the method call result  
      */  
     void Engine::LoadInstrument(const char* FileName, uint Instrument) {  
   
         DisableAndLock();  
   
         ResetInternal(); // reset engine  
   
         // free old instrument  
         if (pInstrument) {  
             // give old instrument back to instrument manager  
             Instruments.HandBack(pInstrument, this);  
         }  
   
         InstrumentIdx = -1;  
   
         // request gig instrument from instrument manager  
         try {  
             instrument_id_t instrid;  
             instrid.FileName    = FileName;  
             instrid.iInstrument = Instrument;  
             pInstrument = Instruments.Borrow(instrid, this);  
             if (!pInstrument) {  
                 dmsg(1,("no instrument loaded!!!\n"));  
                 exit(EXIT_FAILURE);  
             }  
         }  
         catch (RIFF::Exception e) {  
             String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;  
             throw LinuxSamplerException(msg);  
         }  
         catch (InstrumentResourceManagerException e) {  
             String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();  
             throw LinuxSamplerException(msg);  
         }  
         catch (...) {  
             throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");  
         }  
   
         InstrumentFile = FileName;  
         InstrumentIdx = Instrument;  
   
         // inform audio driver for the need of two channels  
         try {  
             if (pAudioOutputDevice) 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);  
         }  
   
         Enable();  
     }  
   
     /**  
      * Will be called by the InstrumentResourceManager when the instrument  
      * we are currently using in this engine is going to be updated, so we  
      * can stop playback before that happens.  
      */  
     void Engine::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {  
         dmsg(3,("gig::Engine: Received instrument update message.\n"));  
         DisableAndLock();  
         ResetInternal();  
         this->pInstrument = NULL;  
     }  
   
     /**  
      * Will be called by the InstrumentResourceManager when the instrument  
      * update process was completed, so we can continue with playback.  
      */  
     void Engine::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {  
         this->pInstrument = pNewResource;  
         Enable();  
     }  
   
206      void Engine::Connect(AudioOutputDevice* pAudioOut) {      void Engine::Connect(AudioOutputDevice* pAudioOut) {
207          pAudioOutputDevice = pAudioOut;          pAudioOutputDevice = pAudioOut;
208    
# Line 272  namespace LinuxSampler { namespace gig { Line 217  namespace LinuxSampler { namespace gig {
217              throw LinuxSamplerException(msg);              throw LinuxSamplerException(msg);
218          }          }
219    
220            this->MaxSamplesPerCycle      = pAudioOutputDevice->MaxSamplesPerCycle();
221            this->SampleRate              = pAudioOutputDevice->SampleRate();
222    
223            // FIXME: audio drivers with varying fragment sizes might be a problem here
224            MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1;
225            if (MaxFadeOutPos < 0)
226                throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h too big for current audio fragment size / sampling rate!");
227    
228          // (re)create disk thread          // (re)create disk thread
229          if (this->pDiskThread) {          if (this->pDiskThread) {
230                dmsg(1,("Stopping disk thread..."));
231              this->pDiskThread->StopThread();              this->pDiskThread->StopThread();
232              delete this->pDiskThread;              delete this->pDiskThread;
233                dmsg(1,("OK\n"));
234          }          }
235          this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo          this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo
236          if (!pDiskThread) {          if (!pDiskThread) {
# Line 283  namespace LinuxSampler { namespace gig { Line 238  namespace LinuxSampler { namespace gig {
238              exit(EXIT_FAILURE);              exit(EXIT_FAILURE);
239          }          }
240    
241          for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
242              pVoice->pDiskThread = this->pDiskThread;              iterVoice->pDiskThread = this->pDiskThread;
             pVoice->SetOutput(pAudioOut);  
243              dmsg(3,("d"));              dmsg(3,("d"));
244          }          }
245          pVoicePool->clear();          pVoicePool->clear();
# Line 295  namespace LinuxSampler { namespace gig { Line 249  namespace LinuxSampler { namespace gig {
249          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());
250    
251          // (re)allocate synthesis parameter matrix          // (re)allocate synthesis parameter matrix
252          if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0];          if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);
253          pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()];  
254            #if defined(__APPLE__)
255            pSynthesisParameters[0] = (float *) malloc(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle());
256            #else
257            pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()));
258            #endif
259          for (int dst = 1; dst < Event::destination_count; dst++)          for (int dst = 1; dst < Event::destination_count; dst++)
260              pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();              pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();
261    
# Line 310  namespace LinuxSampler { namespace gig { Line 269  namespace LinuxSampler { namespace gig {
269          pDiskThread->StartThread();          pDiskThread->StartThread();
270          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
271    
272          for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
273              if (!pVoice->pDiskThread) {              if (!iterVoice->pDiskThread) {
274                  dmsg(0,("Engine -> voice::trigger: !pDiskThread\n"));                  dmsg(0,("Engine -> voice::trigger: !pDiskThread\n"));
275                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
276              }              }
277          }          }
278      }      }
279    
280      void Engine::DisconnectAudioOutputDevice() {      void Engine::ClearEventLists() {
281          if (pAudioOutputDevice) { // if clause to prevent disconnect loops          pEvents->clear();
282              AudioOutputDevice* olddevice = pAudioOutputDevice;          pCCEvents->clear();
283              pAudioOutputDevice = NULL;          for (uint i = 0; i < Event::destination_count; i++) {
284              olddevice->Disconnect(this);              pSynthesisEvents[i]->clear();
285            }
286        }
287    
288        /**
289         * Copy all events from the given input queue buffer to the engine's
290         * internal event list. This will be done at the beginning of each audio
291         * cycle (that is each RenderAudio() call) to get all events which have
292         * to be processed in the current audio cycle. Each EngineChannel has
293         * it's own input event queue for the common channel specific events
294         * (like NoteOn, NoteOff and ControlChange events). Beside that, the
295         * engine also has a input event queue for global events (usually SysEx
296         * message).
297         *
298         * @param pEventQueue - input event buffer to read from
299         * @param Samples     - number of sample points to be processed in the
300         *                      current audio cycle
301         */
302        void Engine::ImportEvents(RingBuffer<Event>* pEventQueue, uint Samples) {
303            RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
304            Event* pEvent;
305            while (true) {
306                // get next event from input event queue
307                if (!(pEvent = eventQueueReader.pop())) break;
308                // if younger event reached, ignore that and all subsequent ones for now
309                if (pEvent->FragmentPos() >= Samples) {
310                    eventQueueReader--;
311                    dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
312                    pEvent->ResetFragmentPos();
313                    break;
314                }
315                // copy event to internal event list
316                if (pEvents->poolIsEmpty()) {
317                    dmsg(1,("Event pool emtpy!\n"));
318                    break;
319                }
320                *pEvents->allocAppend() = *pEvent;
321          }          }
322            eventQueueReader.free(); // free all copied events from input queue
323      }      }
324    
325      /**      /**
# Line 339  namespace LinuxSampler { namespace gig { Line 335  namespace LinuxSampler { namespace gig {
335      int Engine::RenderAudio(uint Samples) {      int Engine::RenderAudio(uint Samples) {
336          dmsg(5,("RenderAudio(Samples=%d)\n", Samples));          dmsg(5,("RenderAudio(Samples=%d)\n", Samples));
337    
338          // return if no instrument loaded or engine disabled          // return if engine disabled
339          if (EngineDisabled.Pop()) {          if (EngineDisabled.Pop()) {
340              dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe()));              dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe()));
341              return 0;              return 0;
342          }          }
343          if (!pInstrument) {  
344              dmsg(5,("gig::Engine: no instrument loaded\n"));          // update time of start and end of this audio fragment (as events' time stamps relate to this)
345              return 0;          pEventGenerator->UpdateFragmentTime(Samples);
346    
347            // empty the engine's event lists for the new fragment
348            ClearEventLists();
349    
350            // get all events from the engine's global input event queue which belong to the current fragment
351            // (these are usually just SysEx messages)
352            ImportEvents(this->pEventQueue, Samples);
353    
354            // process engine global events (these are currently only MIDI System Exclusive messages)
355            {
356                RTList<Event>::Iterator itEvent = pEvents->first();
357                RTList<Event>::Iterator end     = pEvents->end();
358                for (; itEvent != end; ++itEvent) {
359                    switch (itEvent->Type) {
360                        case Event::type_sysex:
361                            dmsg(5,("Engine: Sysex received\n"));
362                            ProcessSysex(itEvent);
363                            break;
364                    }
365                }
366          }          }
367    
368            // reset internal voice counter (just for statistic of active voices)
369            ActiveVoiceCountTemp = 0;
370    
371          // empty the event lists for the new fragment          // render audio for all engine channels
372          pEvents->clear();          // TODO: should we make voice stealing engine globally? unfortunately this would mean other disadvantages so I left voice stealing in the engine channel space for now
373          pCCEvents->clear();          {
374          for (uint i = 0; i < Event::destination_count; i++) {              std::list<EngineChannel*>::iterator itChannel = engineChannels.begin();
375              pSynthesisEvents[i]->clear();              std::list<EngineChannel*>::iterator end       = engineChannels.end();
376                for (; itChannel != end; itChannel++) {
377                    if (!(*itChannel)->pInstrument) continue; // ignore if no instrument loaded
378                    RenderAudio(*itChannel, Samples);
379                }
380          }          }
381    
382          // read and copy events from input queue          // just some statistics about this engine instance
383          Event event = pEventGenerator->CreateEvent();          ActiveVoiceCount = ActiveVoiceCountTemp;
384          while (true) {          if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount;
385              if (!pEventQueue->pop(&event)) break;  
386              pEvents->alloc_assign(event);          return 0;
387        }
388    
389        void Engine::RenderAudio(EngineChannel* pEngineChannel, uint Samples) {
390            // empty the engine's event lists for the new fragment
391            ClearEventLists();
392            // empty the engine channel's, MIDI key specific event lists
393            {
394                RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
395                RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
396                for(; iuiKey != end; ++iuiKey) {
397                    pEngineChannel->pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
398                }
399          }          }
400    
401    
402          // update time of start and end of this audio fragment (as events' time stamps relate to this)          // get all events from the engine channels's input event queue which belong to the current fragment
403          pEventGenerator->UpdateFragmentTime(Samples);          // (these are the common events like NoteOn, NoteOff, ControlChange, etc.)
404            ImportEvents(pEngineChannel->pEventQueue, Samples);
405    
406    
407          // process events          // process events
408          Event* pNextEvent = pEvents->first();          {
409          while (pNextEvent) {              RTList<Event>::Iterator itEvent = pEvents->first();
410              Event* pEvent = pNextEvent;              RTList<Event>::Iterator end     = pEvents->end();
411              pEvents->set_current(pEvent);              for (; itEvent != end; ++itEvent) {
412              pNextEvent = pEvents->next();                  switch (itEvent->Type) {
413              switch (pEvent->Type) {                      case Event::type_note_on:
414                  case Event::type_note_on:                          dmsg(5,("Engine: Note on received\n"));
415                      dmsg(5,("Audio Thread: Note on received\n"));                          ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent);
416                      ProcessNoteOn(pEvent);                          break;
417                      break;                      case Event::type_note_off:
418                  case Event::type_note_off:                          dmsg(5,("Engine: Note off received\n"));
419                      dmsg(5,("Audio Thread: Note off received\n"));                          ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent);
420                      ProcessNoteOff(pEvent);                          break;
421                      break;                      case Event::type_control_change:
422                  case Event::type_control_change:                          dmsg(5,("Engine: MIDI CC received\n"));
423                      dmsg(5,("Audio Thread: MIDI CC received\n"));                          ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent);
424                      ProcessControlChange(pEvent);                          break;
425                      break;                      case Event::type_pitchbend:
426                  case Event::type_pitchbend:                          dmsg(5,("Engine: Pitchbend received\n"));
427                      dmsg(5,("Audio Thread: Pitchbend received\n"));                          ProcessPitchbend((EngineChannel*)itEvent->pEngineChannel, itEvent);
428                      ProcessPitchbend(pEvent);                          break;
429                      break;                  }
430              }              }
431          }          }
432    
433    
434          // render audio from all active voices          // render audio from all active voices
435          int active_voices = 0;          {
436          uint* piKey = pActiveKeys->first();              RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
437          while (piKey) { // iterate through all active keys              RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
438              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];              while (iuiKey != end) { // iterate through all active keys
439              pActiveKeys->set_current(piKey);                  midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
440              piKey = pActiveKeys->next();                  ++iuiKey;
441    
442              Voice* pVoiceNext = pKey->pActiveVoices->first();                  RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
443              while (pVoiceNext) { // iterate through all voices on this key                  RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
444                  // already get next voice on key                  for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
445                  Voice* pVoice = pVoiceNext;                      // now render current voice
446                  pKey->pActiveVoices->set_current(pVoice);                      itVoice->Render(Samples);
447                  pVoiceNext = pKey->pActiveVoices->next();                      if (itVoice->IsActive()) ActiveVoiceCountTemp++; // still active
448                        else { // voice reached end, is now inactive
449                  // now render current voice                          FreeVoice(pEngineChannel, itVoice); // remove voice from the list of active voices
450                  pVoice->Render(Samples);                      }
451                  if (pVoice->IsActive()) active_voices++; // still active                  }
452                  else { // voice reached end, is now inactive              }
                     KillVoice(pVoice); // remove voice from the list of active voices  
                 }  
             }  
             pKey->pEvents->clear(); // free all events on the key  
453          }          }
454    
455    
456          // write that to the disk thread class so that it can print it          // now render all postponed voices from voice stealing
457          // on the console for debugging purposes          {
458          ActiveVoiceCount = active_voices;              RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first();
459          if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount;              RTList<Event>::Iterator end               = pVoiceStealingQueue->end();
460                for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
461                    Pool<Voice>::Iterator itNewVoice =
462          return 0;                      LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);
463                    if (itNewVoice) {
464                        for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) {
465                            itNewVoice->Render(Samples);
466                            if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active
467                            else { // voice reached end, is now inactive
468                                FreeVoice(pEngineChannel, itNewVoice); // remove voice from the list of active voices
469                            }
470                        }
471                    }
472                    else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n"));
473                }
474            }
475            // reset voice stealing for the new fragment
476            pVoiceStealingQueue->clear();
477            pEngineChannel->itLastStolenVoice = RTList<Voice>::Iterator();
478            pEngineChannel->iuiLastStolenKey  = RTList<uint>::Iterator();
479    
480    
481            // free all keys which have no active voices left
482            {
483                RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
484                RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
485                while (iuiKey != end) { // iterate through all active keys
486                    midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
487                    ++iuiKey;
488                    if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey);
489                    #if DEVMODE
490                    else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
491                        RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
492                        RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
493                        for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
494                            if (itVoice->itKillEvent) {
495                                dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n"));
496                            }
497                        }
498                    }
499                    #endif // DEVMODE
500                }
501            }
502      }      }
503    
504      /**      /**
505       *  Will be called by the MIDIIn Thread to let the audio thread trigger a new       *  Will be called by the MIDI input device whenever a MIDI system
506       *  voice for the given key.       *  exclusive message has arrived.
507       *       *
508       *  @param Key      - MIDI key number of the triggered key       *  @param pData - pointer to sysex data
509       *  @param Velocity - MIDI velocity value of the triggered key       *  @param Size  - lenght of sysex data (in bytes)
510       */       */
511      void Engine::SendNoteOn(uint8_t Key, uint8_t Velocity) {      void Engine::SendSysex(void* pData, uint Size) {
512          Event event    = pEventGenerator->CreateEvent();          Event event             = pEventGenerator->CreateEvent();
513          event.Type     = Event::type_note_on;          event.Type              = Event::type_sysex;
514          event.Key      = Key;          event.Param.Sysex.Size  = Size;
515          event.Velocity = Velocity;          event.pEngineChannel    = NULL; // as Engine global event
516          if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);          if (pEventQueue->write_space() > 0) {
517          else dmsg(1,("Engine: Input event queue full!"));              if (pSysexBuffer->write_space() >= Size) {
518      }                  // copy sysex data to input buffer
519                    uint toWrite = Size;
520                    uint8_t* pPos = (uint8_t*) pData;
521                    while (toWrite) {
522                        const uint writeNow = RTMath::Min(toWrite, pSysexBuffer->write_space_to_end());
523                        pSysexBuffer->write(pPos, writeNow);
524                        toWrite -= writeNow;
525                        pPos    += writeNow;
526    
527      /**                  }
528       *  Will be called by the MIDIIn Thread to signal the audio thread to release                  // finally place sysex event into input event queue
529       *  voice(s) on the given key.                  pEventQueue->push(&event);
530       *              }
531       *  @param Key      - MIDI key number of the released key              else dmsg(1,("Engine: Sysex message too large (%d byte) for input buffer (%d byte)!",Size,SYSEX_BUFFER_SIZE));
532       *  @param Velocity - MIDI release velocity value of the released key          }
      */  
     void Engine::SendNoteOff(uint8_t Key, uint8_t Velocity) {  
         Event event    = pEventGenerator->CreateEvent();  
         event.Type     = Event::type_note_off;  
         event.Key      = Key;  
         event.Velocity = Velocity;  
         if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);  
533          else dmsg(1,("Engine: Input event queue full!"));          else dmsg(1,("Engine: Input event queue full!"));
534      }      }
535    
536      /**      /**
537       *  Will be called by the MIDIIn Thread to signal the audio thread to change       *  Assigns and triggers a new voice for the respective MIDI key.
      *  the pitch value for all voices.  
538       *       *
539       *  @param Pitch - MIDI pitch value (-8192 ... +8191)       *  @param pEngineChannel - engine channel on which this event occured on
540         *  @param itNoteOnEvent - key, velocity and time stamp of the event
541       */       */
542      void Engine::SendPitchbend(int Pitch) {      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
         Event event = pEventGenerator->CreateEvent();  
         event.Type  = Event::type_pitchbend;  
         event.Pitch = Pitch;  
         if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);  
         else dmsg(1,("Engine: Input event queue full!"));  
     }  
543    
544      /**          const int key = itNoteOnEvent->Param.Note.Key;
      *  Will be called by the MIDIIn Thread to signal the audio thread that a  
      *  continuous controller value has changed.  
      *  
      *  @param Controller - MIDI controller number of the occured control change  
      *  @param Value      - value of the control change  
      */  
     void Engine::SendControlChange(uint8_t Controller, uint8_t Value) {  
         Event event      = pEventGenerator->CreateEvent();  
         event.Type       = Event::type_control_change;  
         event.Controller = Controller;  
         event.Value      = Value;  
         if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);  
         else dmsg(1,("Engine: Input event queue full!"));  
     }  
545    
546      /**          // Change key dimension value if key is in keyswitching area
547       *  Assigns and triggers a new voice for the respective MIDI key.          {
548       *              const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument;
549       *  @param pNoteOnEvent - key, velocity and time stamp of the event              if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)
550       */                  pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) /
551      void Engine::ProcessNoteOn(Event* pNoteOnEvent) {                      (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);
552          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Key];          }
553    
554            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];
555    
556          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
557    
558          // cancel release process of voices on this key if needed          // cancel release process of voices on this key if needed
559          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !pEngineChannel->SustainPedal) {
560              pNoteOnEvent->Type = Event::type_cancel_release; // transform event type              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
561              pEvents->move(pNoteOnEvent, pKey->pEvents); // move event to the key's own event list              if (itCancelReleaseEvent) {
562          }                  *itCancelReleaseEvent = *itNoteOnEvent;                  // copy event
563                    itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type
         // allocate a new voice for the key  
         Voice* pNewVoice = pKey->pActiveVoices->alloc();  
         if (pNewVoice) {  
             // launch the new voice  
             if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument) < 0) {  
                 dmsg(1,("Triggering new voice failed!\n"));  
                 pKey->pActiveVoices->free(pNewVoice);  
             }  
             else if (!pKey->Active) { // mark as active key  
                 pKey->Active = true;  
                 pKey->pSelf  = pActiveKeys->alloc();  
                 *pKey->pSelf = pNoteOnEvent->Key;  
564              }              }
565                else dmsg(1,("Event pool emtpy!\n"));
566          }          }
567          else std::cerr << "No free voice!" << std::endl << std::flush;  
568            // move note on event to the key's own event list
569            RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
570    
571            // allocate and trigger a new voice for the key
572            LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, 0, false, true);
573    
574            pKey->RoundRobinIndex++;
575      }      }
576    
577      /**      /**
# Line 532  namespace LinuxSampler { namespace gig { Line 580  namespace LinuxSampler { namespace gig {
580       *  sustain pedal will be released or voice turned inactive by itself (e.g.       *  sustain pedal will be released or voice turned inactive by itself (e.g.
581       *  due to completion of sample playback).       *  due to completion of sample playback).
582       *       *
583       *  @param pNoteOffEvent - key, velocity and time stamp of the event       *  @param pEngineChannel - engine channel on which this event occured on
584         *  @param itNoteOffEvent - key, velocity and time stamp of the event
585       */       */
586      void Engine::ProcessNoteOff(Event* pNoteOffEvent) {      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {
587          midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Key];          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
588    
589          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
590    
591          // release voices on this key if needed          // release voices on this key if needed
592          if (pKey->Active && !SustainPedal) {          if (pKey->Active && !pEngineChannel->SustainPedal) {
593              pNoteOffEvent->Type = Event::type_release; // transform event type              itNoteOffEvent->Type = Event::type_release; // transform event type
594              pEvents->move(pNoteOffEvent, pKey->pEvents); // move event to the key's own event list          }
595    
596            // move event to the key's own event list
597            RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
598    
599            // spawn release triggered voice(s) if needed
600            if (pKey->ReleaseTrigger) {
601                LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
602                pKey->ReleaseTrigger = false;
603          }          }
604      }      }
605    
# Line 550  namespace LinuxSampler { namespace gig { Line 607  namespace LinuxSampler { namespace gig {
607       *  Moves pitchbend event from the general (input) event list to the pitch       *  Moves pitchbend event from the general (input) event list to the pitch
608       *  event list.       *  event list.
609       *       *
610       *  @param pPitchbendEvent - absolute pitch value and time stamp of the event       *  @param pEngineChannel - engine channel on which this event occured on
611         *  @param itPitchbendEvent - absolute pitch value and time stamp of the event
612       */       */
613      void Engine::ProcessPitchbend(Event* pPitchbendEvent) {      void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) {
614          this->Pitch = pPitchbendEvent->Pitch; // store current pitch value          pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value
615          pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]);          itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]);
616      }      }
617    
618      /**      /**
619       *  Immediately kills the voice given with pVoice (no matter if sustain is       *  Allocates and triggers a new voice. This method will usually be
620       *  pressed or not) and removes it from the MIDI key's list of active voice.       *  called by the ProcessNoteOn() method and by the voices itself
621       *  This method will e.g. be called if a voice went inactive by itself.       *  (e.g. to spawn further voices on the same key for layered sounds).
622       *       *
623       *  @param pVoice - points to the voice to be killed       *  @param pEngineChannel      - engine channel on which this event occured on
624         *  @param itNoteOnEvent       - key, velocity and time stamp of the event
625         *  @param iLayer              - layer index for the new voice (optional - only
626         *                               in case of layered sounds of course)
627         *  @param ReleaseTriggerVoice - if new voice is a release triggered voice
628         *                               (optional, default = false)
629         *  @param VoiceStealing       - if voice stealing should be performed
630         *                               when there is no free voice
631         *                               (optional, default = true)
632         *  @returns pointer to new voice or NULL if there was no free voice or
633         *           if the voice wasn't triggered (for example when no region is
634         *           defined for the given key).
635       */       */
636      void Engine::KillVoice(Voice* pVoice) {      Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) {
637          if (pVoice) {          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
             if (pVoice->IsActive()) pVoice->Kill();  
638    
639              midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey];          // allocate a new voice for the key
640            Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
641            if (itNewVoice) {
642                // launch the new voice
643                if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {
644                    dmsg(4,("Voice not triggered\n"));
645                    pKey->pActiveVoices->free(itNewVoice);
646                }
647                else { // on success
648                    uint** ppKeyGroup = NULL;
649                    if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group
650                        ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup];
651                        if (*ppKeyGroup) { // if there's already an active key in that key group
652                            midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup];
653                            // kill all voices on the (other) key
654                            RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
655                            RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
656                            for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
657                                if (itVoiceToBeKilled->Type != Voice::type_release_trigger) itVoiceToBeKilled->Kill(itNoteOnEvent);
658                            }
659                        }
660                    }
661                    if (!pKey->Active) { // mark as active key
662                        pKey->Active = true;
663                        pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();
664                        *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
665                    }
666                    if (itNewVoice->KeyGroup) {
667                        *ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group
668                    }
669                    if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s)
670                    return itNewVoice; // success
671                }
672            }
673            else if (VoiceStealing) {
674                // first, get total amount of required voices (dependant on amount of layers)
675                ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);
676                if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed
677                int voicesRequired = pRegion->Layers;
678    
679                // now steal the (remaining) amount of voices
680                for (int i = iLayer; i < voicesRequired; i++)
681                    StealVoice(pEngineChannel, itNoteOnEvent);
682    
683                // put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died
684                RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend();
685                if (itStealEvent) {
686                    *itStealEvent = *itNoteOnEvent; // copy event
687                    itStealEvent->Param.Note.Layer = iLayer;
688                    itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice;
689                }
690                else dmsg(1,("Voice stealing queue full!\n"));
691            }
692    
693            return Pool<Voice>::Iterator(); // no free voice or error
694        }
695    
696        /**
697         *  Will be called by LaunchVoice() method in case there are no free
698         *  voices left. This method will select and kill one old voice for
699         *  voice stealing and postpone the note-on event until the selected
700         *  voice actually died.
701         *
702         *  @param pEngineChannel - engine channel on which this event occured on
703         *  @param itNoteOnEvent - key, velocity and time stamp of the event
704         */
705        void Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
706            if (!pEventPool->poolIsEmpty()) {
707    
708                RTList<uint>::Iterator  iuiOldestKey;
709                RTList<Voice>::Iterator itOldestVoice;
710    
711                // Select one voice for voice stealing
712                switch (VOICE_STEAL_ALGORITHM) {
713    
714                    // try to pick the oldest voice on the key where the new
715                    // voice should be spawned, if there is no voice on that
716                    // key, or no voice left to kill there, then procceed with
717                    // 'oldestkey' algorithm
718                    case voice_steal_algo_keymask: {
719                        midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
720                        if (pEngineChannel->itLastStolenVoice) {
721                            itOldestVoice = pEngineChannel->itLastStolenVoice;
722                            ++itOldestVoice;
723                        }
724                        else { // no voice stolen in this audio fragment cycle yet
725                            itOldestVoice = pOldestKey->pActiveVoices->first();
726                        }
727                        if (itOldestVoice) {
728                            iuiOldestKey = pOldestKey->itSelf;
729                            break; // selection succeeded
730                        }
731                    } // no break - intentional !
732    
733                    // try to pick the oldest voice on the oldest active key
734                    // (caution: must stay after 'keymask' algorithm !)
735                    case voice_steal_algo_oldestkey: {
736                        if (pEngineChannel->itLastStolenVoice) {
737                            midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*pEngineChannel->iuiLastStolenKey];
738                            itOldestVoice = pEngineChannel->itLastStolenVoice;
739                            ++itOldestVoice;
740                            if (!itOldestVoice) {
741                                iuiOldestKey = pEngineChannel->iuiLastStolenKey;
742                                ++iuiOldestKey;
743                                if (iuiOldestKey) {
744                                    midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey];
745                                    itOldestVoice = pOldestKey->pActiveVoices->first();
746                                }
747                                else {
748                                    dmsg(1,("gig::Engine: Warning, too less voices, even for voice stealing! - Better recompile with higher MAX_AUDIO_VOICES.\n"));
749                                    return;
750                                }
751                            }
752                            else iuiOldestKey = pEngineChannel->iuiLastStolenKey;
753                        }
754                        else { // no voice stolen in this audio fragment cycle yet
755                            iuiOldestKey = pEngineChannel->pActiveKeys->first();
756                            midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey];
757                            itOldestVoice = pOldestKey->pActiveVoices->first();
758                        }
759                        break;
760                    }
761    
762                    // don't steal anything
763                    case voice_steal_algo_none:
764                    default: {
765                        dmsg(1,("No free voice (voice stealing disabled)!\n"));
766                        return;
767                    }
768                }
769    
770                //FIXME: can be removed, just a sanity check for debugging
771                if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n"));
772    
773                // now kill the selected voice
774                itOldestVoice->Kill(itNoteOnEvent);
775                // remember which voice on which key we stole, so we can simply proceed for the next voice stealing
776                pEngineChannel->itLastStolenVoice = itOldestVoice;
777                pEngineChannel->iuiLastStolenKey = iuiOldestKey;
778            }
779            else dmsg(1,("Event pool emtpy!\n"));
780        }
781    
782        /**
783         *  Removes the given voice from the MIDI key's list of active voices.
784         *  This method will be called when a voice went inactive, e.g. because
785         *  it finished to playback its sample, finished its release stage or
786         *  just was killed.
787         *
788         *  @param pEngineChannel - engine channel on which this event occured on
789         *  @param itVoice - points to the voice to be freed
790         */
791        void Engine::FreeVoice(EngineChannel* pEngineChannel, Pool<Voice>::Iterator& itVoice) {
792            if (itVoice) {
793                midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itVoice->MIDIKey];
794    
795                uint keygroup = itVoice->KeyGroup;
796    
797              // free the voice object              // free the voice object
798              pVoicePool->free(pVoice);              pVoicePool->free(itVoice);
799    
800              // check if there are no voices left on the MIDI key and update the key info if so              // if no other voices left and member of a key group, remove from key group
801              if (pKey->pActiveVoices->is_empty()) {              if (pKey->pActiveVoices->isEmpty() && keygroup) {
802                  pKey->Active = false;                  uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[keygroup];
803                  pActiveKeys->free(pKey->pSelf); // remove key from list of active keys                  if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group
                 pKey->pSelf = NULL;  
                 dmsg(3,("Key has no more voices now\n"));  
804              }              }
805          }          }
806          else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush;          else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush;
807        }
808    
809        /**
810         *  Called when there's no more voice left on a key, this call will
811         *  update the key info respectively.
812         *
813         *  @param pEngineChannel - engine channel on which this event occured on
814         *  @param pKey - key which is now inactive
815         */
816        void Engine::FreeKey(EngineChannel* pEngineChannel, midi_key_info_t* pKey) {
817            if (pKey->pActiveVoices->isEmpty()) {
818                pKey->Active = false;
819                pEngineChannel->pActiveKeys->free(pKey->itSelf); // remove key from list of active keys
820                pKey->itSelf = RTList<uint>::Iterator();
821                pKey->ReleaseTrigger = false;
822                pKey->pEvents->clear();
823                dmsg(3,("Key has no more voices now\n"));
824            }
825            else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n"));
826      }      }
827    
828      /**      /**
829       *  Reacts on supported control change commands (e.g. pitch bend wheel,       *  Reacts on supported control change commands (e.g. pitch bend wheel,
830       *  modulation wheel, aftertouch).       *  modulation wheel, aftertouch).
831       *       *
832       *  @param pControlChangeEvent - controller, value and time stamp of the event       *  @param pEngineChannel - engine channel on which this event occured on
833         *  @param itControlChangeEvent - controller, value and time stamp of the event
834       */       */
835      void Engine::ProcessControlChange(Event* pControlChangeEvent) {      void Engine::ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) {
836          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", pControlChangeEvent->Controller, pControlChangeEvent->Value));          dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value));
837    
838          switch (pControlChangeEvent->Controller) {          switch (itControlChangeEvent->Param.CC.Controller) {
839              case 64: {              case 7: { // volume
840                  if (pControlChangeEvent->Value >= 64 && !SustainPedal) {                  //TODO: not sample accurate yet
841                    pEngineChannel->GlobalVolume = (float) itControlChangeEvent->Param.CC.Value / 127.0f;
842                    break;
843                }
844                case 10: { // panpot
845                    //TODO: not sample accurate yet
846                    const int pan = (int) itControlChangeEvent->Param.CC.Value - 64;
847                    pEngineChannel->GlobalPanLeft  = 1.0f - float(RTMath::Max(pan, 0)) /  63.0f;
848                    pEngineChannel->GlobalPanRight = 1.0f - float(RTMath::Min(pan, 0)) / -64.0f;
849                    break;
850                }
851                case 64: { // sustain
852                    if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {
853                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
854                      SustainPedal = true;                      pEngineChannel->SustainPedal = true;
855    
856                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
857                      uint* piKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
858                      if (piKey) {                      if (iuiKey) {
859                          pControlChangeEvent->Type = Event::type_cancel_release; // transform event type                          itControlChangeEvent->Type = Event::type_cancel_release; // transform event type
860                          while (piKey) {                          while (iuiKey) {
861                              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];                              midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
862                              pActiveKeys->set_current(piKey);                              ++iuiKey;
                             piKey = pActiveKeys->next();  
863                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
864                                  Event* pNewEvent = pKey->pEvents->alloc();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
865                                  if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list                                  if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
866                                  else dmsg(1,("Event pool emtpy!\n"));                                  else dmsg(1,("Event pool emtpy!\n"));
867                              }                              }
868                          }                          }
869                      }                      }
870                  }                  }
871                  if (pControlChangeEvent->Value < 64 && SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {
872                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
873                      SustainPedal = false;                      pEngineChannel->SustainPedal = false;
874    
875                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
876                      uint* piKey = pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
877                      if (piKey) {                      if (iuiKey) {
878                          pControlChangeEvent->Type = Event::type_release; // transform event type                          itControlChangeEvent->Type = Event::type_release; // transform event type
879                          while (piKey) {                          while (iuiKey) {
880                              midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey];                              midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
881                              pActiveKeys->set_current(piKey);                              ++iuiKey;
                             piKey = pActiveKeys->next();  
882                              if (!pKey->KeyPressed) {                              if (!pKey->KeyPressed) {
883                                  Event* pNewEvent = pKey->pEvents->alloc();                                  RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
884                                  if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list                                  if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
885                                  else dmsg(1,("Event pool emtpy!\n"));                                  else dmsg(1,("Event pool emtpy!\n"));
886                              }                              }
887                          }                          }
# Line 640  namespace LinuxSampler { namespace gig { Line 892  namespace LinuxSampler { namespace gig {
892          }          }
893    
894          // update controller value in the engine's controller table          // update controller value in the engine's controller table
895          ControllerTable[pControlChangeEvent->Controller] = pControlChangeEvent->Value;          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
896    
897          // move event from the unsorted event list to the control change event list          // move event from the unsorted event list to the control change event list
898          pEvents->move(pControlChangeEvent, pCCEvents);          itControlChangeEvent.moveToEndOf(pCCEvents);
899        }
900    
901        /**
902         *  Reacts on MIDI system exclusive messages.
903         *
904         *  @param itSysexEvent - sysex data size and time stamp of the sysex event
905         */
906        void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) {
907            RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();
908    
909            uint8_t exclusive_status, id;
910            if (!reader.pop(&exclusive_status)) goto free_sysex_data;
911            if (!reader.pop(&id))               goto free_sysex_data;
912            if (exclusive_status != 0xF0)       goto free_sysex_data;
913    
914            switch (id) {
915                case 0x41: { // Roland
916                    uint8_t device_id, model_id, cmd_id;
917                    if (!reader.pop(&device_id)) goto free_sysex_data;
918                    if (!reader.pop(&model_id))  goto free_sysex_data;
919                    if (!reader.pop(&cmd_id))    goto free_sysex_data;
920                    if (model_id != 0x42 /*GS*/) goto free_sysex_data;
921                    if (cmd_id != 0x12 /*DT1*/)  goto free_sysex_data;
922    
923                    // command address
924                    uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB)
925                    const RingBuffer<uint8_t>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later
926                    if (reader.read(&addr[0], 3) != 3) goto free_sysex_data;
927                    if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters
928                    }
929                    else if (addr[0] == 0x40 && addr[1] == 0x01) { // Common Parameters
930                    }
931                    else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x10) { // Part Parameters (1)
932                        switch (addr[3]) {
933                            case 0x40: { // scale tuning
934                                uint8_t scale_tunes[12]; // detuning of all 12 semitones of an octave
935                                if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data;
936                                uint8_t checksum;
937                                if (!reader.pop(&checksum))                      goto free_sysex_data;
938                                if (GSCheckSum(checksum_reader, 12) != checksum) goto free_sysex_data;
939                                for (int i = 0; i < 12; i++) scale_tunes[i] -= 64;
940                                AdjustScale((int8_t*) scale_tunes);
941                                break;
942                            }
943                        }
944                    }
945                    else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x20) { // Part Parameters (2)
946                    }
947                    else if (addr[0] == 0x41) { // Drum Setup Parameters
948                    }
949                    break;
950                }
951            }
952    
953            free_sysex_data: // finally free sysex data
954            pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size);
955        }
956    
957        /**
958         * Calculates the Roland GS sysex check sum.
959         *
960         * @param AddrReader - reader which currently points to the first GS
961         *                     command address byte of the GS sysex message in
962         *                     question
963         * @param DataSize   - size of the GS message data (in bytes)
964         */
965        uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t>::NonVolatileReader AddrReader, uint DataSize) {
966            RingBuffer<uint8_t>::NonVolatileReader reader = AddrReader;
967            uint bytes = 3 /*addr*/ + DataSize;
968            uint8_t addr_and_data[bytes];
969            reader.read(&addr_and_data[0], bytes);
970            uint8_t sum = 0;
971            for (uint i = 0; i < bytes; i++) sum += addr_and_data[i];
972            return 128 - sum % 128;
973        }
974    
975        /**
976         * Allows to tune each of the twelve semitones of an octave.
977         *
978         * @param ScaleTunes - detuning of all twelve semitones (in cents)
979         */
980        void Engine::AdjustScale(int8_t ScaleTunes[12]) {
981            memcpy(&this->ScaleTuning[0], &ScaleTunes[0], 12); //TODO: currently not sample accurate
982      }      }
983    
984      /**      /**
# Line 661  namespace LinuxSampler { namespace gig { Line 996  namespace LinuxSampler { namespace gig {
996          }          }
997      }      }
998    
     float Engine::Volume() {  
         return GlobalVolume;  
     }  
   
     void Engine::Volume(float f) {  
         GlobalVolume = f;  
     }  
   
999      uint Engine::VoiceCount() {      uint Engine::VoiceCount() {
1000          return ActiveVoiceCount;          return ActiveVoiceCount;
1001      }      }
# Line 701  namespace LinuxSampler { namespace gig { Line 1028  namespace LinuxSampler { namespace gig {
1028          return "GigEngine";          return "GigEngine";
1029      }      }
1030    
     String Engine::InstrumentFileName() {  
         return InstrumentFile;  
     }  
   
     int Engine::InstrumentIndex() {  
         return InstrumentIdx;  
     }  
   
1031      String Engine::Description() {      String Engine::Description() {
1032          return "Gigasampler Engine";          return "Gigasampler Engine";
1033      }      }
1034    
1035      String Engine::Version() {      String Engine::Version() {
1036          String s = "$Revision: 1.6 $";          String s = "$Revision: 1.30 $";
1037          return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword          return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
1038      }      }
1039    

Legend:
Removed from v.123  
changed lines
  Added in v.438

  ViewVC Help
Powered by ViewVC