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

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

  ViewVC Help
Powered by ViewVC