/[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 832 by persson, Sun Feb 5 10:24:05 2006 UTC revision 1037 by schoenebeck, Tue Jan 23 20:03:22 2007 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
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, 2006 Christian Schoenebeck                        *   *   Copyright (C) 2005-2007 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 97  namespace LinuxSampler { namespace gig { Line 97  namespace LinuxSampler { namespace gig {
97          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
98          pDiskThread        = NULL;          pDiskThread        = NULL;
99          pEventGenerator    = NULL;          pEventGenerator    = NULL;
100          pSysexBuffer       = new RingBuffer<uint8_t>(CONFIG_SYSEX_BUFFER_SIZE, 0);          pSysexBuffer       = new RingBuffer<uint8_t,false>(CONFIG_SYSEX_BUFFER_SIZE, 0);
101          pEventQueue        = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);          pEventQueue        = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
102          pEventPool         = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT);          pEventPool         = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT);
103          pVoicePool         = new Pool<Voice>(CONFIG_MAX_VOICES);          pVoicePool         = new Pool<Voice>(CONFIG_MAX_VOICES);
104          pVoiceStealingQueue = new RTList<Event>(pEventPool);          pVoiceStealingQueue = new RTList<Event>(pEventPool);
# Line 116  namespace LinuxSampler { namespace gig { Line 116  namespace LinuxSampler { namespace gig {
116       * Destructor       * Destructor
117       */       */
118      Engine::~Engine() {      Engine::~Engine() {
119            MidiInputPort::RemoveSysexListener(this);
120          if (pDiskThread) {          if (pDiskThread) {
121              dmsg(1,("Stopping disk thread..."));              dmsg(1,("Stopping disk thread..."));
122              pDiskThread->StopThread();              pDiskThread->StopThread();
# Line 131  namespace LinuxSampler { namespace gig { Line 132  namespace LinuxSampler { namespace gig {
132          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
133          if (pVoiceStealingQueue) delete pVoiceStealingQueue;          if (pVoiceStealingQueue) delete pVoiceStealingQueue;
134          if (pSysexBuffer) delete pSysexBuffer;          if (pSysexBuffer) delete pSysexBuffer;
135          EngineFactory::Destroy(this);          Unregister();
136      }      }
137    
138      void Engine::Enable() {      void Engine::Enable() {
# Line 165  namespace LinuxSampler { namespace gig { Line 166  namespace LinuxSampler { namespace gig {
166    
167      /**      /**
168       *  Reset all voices and disk thread and clear input event queue and all       *  Reset all voices and disk thread and clear input event queue and all
169       *  control and status variables. This method is not thread safe!       *  control and status variables. This method is protected by a mutex.
170       */       */
171      void Engine::ResetInternal() {      void Engine::ResetInternal() {
172            ResetInternalMutex.Lock();
173    
174            // make sure that the engine does not get any sysex messages
175            // while it's reseting
176            bool sysexDisabled = MidiInputPort::RemoveSysexListener(this);
177          ActiveVoiceCount    = 0;          ActiveVoiceCount    = 0;
178          ActiveVoiceCountMax = 0;          ActiveVoiceCountMax = 0;
179    
# Line 191  namespace LinuxSampler { namespace gig { Line 197  namespace LinuxSampler { namespace gig {
197          // delete all input events          // delete all input events
198          pEventQueue->init();          pEventQueue->init();
199          pSysexBuffer->init();          pSysexBuffer->init();
200            if (sysexDisabled) MidiInputPort::AddSysexListener(this);
201            ResetInternalMutex.Unlock();
202      }      }
203    
204      /**      /**
# Line 220  namespace LinuxSampler { namespace gig { Line 228  namespace LinuxSampler { namespace gig {
228          }          }
229          catch (AudioOutputException e) {          catch (AudioOutputException e) {
230              String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message();              String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message();
231              throw LinuxSamplerException(msg);              throw Exception(msg);
232          }          }
233    
234          this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle();          this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle();
# Line 298  namespace LinuxSampler { namespace gig { Line 306  namespace LinuxSampler { namespace gig {
306       *                  current audio cycle       *                  current audio cycle
307       */       */
308      void Engine::ImportEvents(uint Samples) {      void Engine::ImportEvents(uint Samples) {
309          RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();          RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
310          Event* pEvent;          Event* pEvent;
311          while (true) {          while (true) {
312              // get next event from input event queue              // get next event from input event queue
# Line 321  namespace LinuxSampler { namespace gig { Line 329  namespace LinuxSampler { namespace gig {
329      }      }
330    
331      /**      /**
332       *  Let this engine proceed to render the given amount of sample points. The       * Let this engine proceed to render the given amount of sample points.
333       *  calculated audio data of all voices of this engine will be placed into       * The engine will iterate through all engine channels and render audio
334       *  the engine's audio sum buffer which has to be copied and eventually be       * for each engine channel independently. The calculated audio data of
335       *  converted to the appropriate value range by the audio output class (e.g.       * all voices of each engine channel will be placed into the audio sum
336       *  AlsaIO or JackIO) right after.       * buffers of the respective audio output device, connected to the
337         * respective engine channel.
338       *       *
339       *  @param Samples - number of sample points to be rendered       *  @param Samples - number of sample points to be rendered
340       *  @returns       0 on success       *  @returns       0 on success
341       */       */
342      int Engine::RenderAudio(uint Samples) {      int Engine::RenderAudio(uint Samples) {
343          dmsg(5,("RenderAudio(Samples=%d)\n", Samples));          dmsg(7,("RenderAudio(Samples=%d)\n", Samples));
344    
345          // return if engine disabled          // return if engine disabled
346          if (EngineDisabled.Pop()) {          if (EngineDisabled.Pop()) {
# Line 383  namespace LinuxSampler { namespace gig { Line 392  namespace LinuxSampler { namespace gig {
392          // now that all ordinary voices on ALL engine channels are rendered, render new stolen voices          // now that all ordinary voices on ALL engine channels are rendered, render new stolen voices
393          RenderStolenVoices(Samples);          RenderStolenVoices(Samples);
394    
395            // handle audio routing for engine channels with FX sends
396            for (int i = 0; i < engineChannels.size(); i++) {
397                if (engineChannels[i]->fxSends.empty()) continue; // ignore if no FX sends
398                RouteAudio(engineChannels[i], Samples);
399            }
400    
401          // handle cleanup on all engine channels for the next audio fragment          // handle cleanup on all engine channels for the next audio fragment
402          for (int i = 0; i < engineChannels.size(); i++) {          for (int i = 0; i < engineChannels.size(); i++) {
403              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded
# Line 523  namespace LinuxSampler { namespace gig { Line 538  namespace LinuxSampler { namespace gig {
538      }      }
539    
540      /**      /**
541         * Will be called in case the respective engine channel sports FX send
542         * channels. In this particular case, engine channel local buffers are
543         * used to render and mix all voices to. This method is responsible for
544         * copying the audio data from those local buffers to the master audio
545         * output channels as well as to the FX send audio output channels with
546         * their respective FX send levels.
547         *
548         * @param pEngineChannel - engine channel from which audio should be
549         *                         routed
550         * @param Samples        - amount of sample points to be routed in
551         *                         this audio fragment cycle
552         */
553        void Engine::RouteAudio(EngineChannel* pEngineChannel, uint Samples) {
554            // route master signal
555            {
556                AudioChannel* pDstL = pAudioOutputDevice->Channel(pEngineChannel->AudioDeviceChannelLeft);
557                AudioChannel* pDstR = pAudioOutputDevice->Channel(pEngineChannel->AudioDeviceChannelRight);
558                pEngineChannel->pChannelLeft->MixTo(pDstL, Samples);
559                pEngineChannel->pChannelRight->MixTo(pDstR, Samples);
560            }
561            // route FX send signal
562            {
563                for (int iFxSend = 0; iFxSend < pEngineChannel->GetFxSendCount(); iFxSend++) {
564                    FxSend* pFxSend = pEngineChannel->GetFxSend(iFxSend);
565                    // left channel
566                    const int iDstL = pFxSend->DestinationChannel(0);
567                    if (iDstL < 0) {
568                        dmsg(1,("Engine::RouteAudio() Error: invalid FX send (L) destination channel"));
569                    } else {
570                        AudioChannel* pDstL = pAudioOutputDevice->Channel(iDstL);
571                        if (!pDstL) {
572                            dmsg(1,("Engine::RouteAudio() Error: invalid FX send (L) destination channel"));
573                        } else pEngineChannel->pChannelLeft->MixTo(pDstL, Samples, pFxSend->Level());
574                    }
575                    // right channel
576                    const int iDstR = pFxSend->DestinationChannel(1);
577                    if (iDstR < 0) {
578                        dmsg(1,("Engine::RouteAudio() Error: invalid FX send (R) destination channel"));
579                    } else {
580                        AudioChannel* pDstR = pAudioOutputDevice->Channel(iDstR);
581                        if (!pDstR) {
582                            dmsg(1,("Engine::RouteAudio() Error: invalid FX send (R) destination channel"));
583                        } else pEngineChannel->pChannelRight->MixTo(pDstR, Samples, pFxSend->Level());
584                    }
585                }
586            }
587            // reset buffers with silence (zero out) for the next audio cycle
588            pEngineChannel->pChannelLeft->Clear();
589            pEngineChannel->pChannelRight->Clear();
590        }
591    
592        /**
593       * Free all keys which have turned inactive in this audio fragment, from       * Free all keys which have turned inactive in this audio fragment, from
594       * the list of active keys and clear all event lists on that engine       * the list of active keys and clear all event lists on that engine
595       * channel.       * channel.
# Line 634  namespace LinuxSampler { namespace gig { Line 701  namespace LinuxSampler { namespace gig {
701          {          {
702              const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument;              const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument;
703              if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)              if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)
704                  pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) /                  pEngineChannel->CurrentKeyDimension = float(key - pInstrument->DimensionKeyRange.low) /
705                      (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);                      (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);
706          }          }
707    
# Line 862  namespace LinuxSampler { namespace gig { Line 929  namespace LinuxSampler { namespace gig {
929                      DimValues[i] = itNoteOnEvent->Param.Note.Velocity;                      DimValues[i] = itNoteOnEvent->Param.Note.Velocity;
930                      break;                      break;
931                  case ::gig::dimension_channelaftertouch:                  case ::gig::dimension_channelaftertouch:
932                      DimValues[i] = 0; //TODO: we currently ignore this dimension                      DimValues[i] = pEngineChannel->ControllerTable[128];
933                      break;                      break;
934                  case ::gig::dimension_releasetrigger:                  case ::gig::dimension_releasetrigger:
935                      VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;                      VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
936                      DimValues[i] = (uint) ReleaseTriggerVoice;                      DimValues[i] = (uint) ReleaseTriggerVoice;
937                      break;                      break;
938                  case ::gig::dimension_keyboard:                  case ::gig::dimension_keyboard:
939                      DimValues[i] = (uint) pEngineChannel->CurrentKeyDimension;                      DimValues[i] = (uint) (pEngineChannel->CurrentKeyDimension * pRegion->pDimensionDefinitions[i].zones);
940                      break;                      break;
941                  case ::gig::dimension_roundrobin:                  case ::gig::dimension_roundrobin:
942                      DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on                      DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on
# Line 1227  namespace LinuxSampler { namespace gig { Line 1294  namespace LinuxSampler { namespace gig {
1294          // update controller value in the engine channel's controller table          // update controller value in the engine channel's controller table
1295          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
1296    
1297            // handle hard coded MIDI controllers
1298          switch (itControlChangeEvent->Param.CC.Controller) {          switch (itControlChangeEvent->Param.CC.Controller) {
1299              case 5: { // portamento time              case 5: { // portamento time
1300                  pEngineChannel->PortamentoTime = (float) itControlChangeEvent->Param.CC.Value / 127.0f * (float) CONFIG_PORTAMENTO_TIME_MAX + (float) CONFIG_PORTAMENTO_TIME_MIN;                  pEngineChannel->PortamentoTime = (float) itControlChangeEvent->Param.CC.Value / 127.0f * (float) CONFIG_PORTAMENTO_TIME_MAX + (float) CONFIG_PORTAMENTO_TIME_MIN;
# Line 1234  namespace LinuxSampler { namespace gig { Line 1302  namespace LinuxSampler { namespace gig {
1302              }              }
1303              case 7: { // volume              case 7: { // volume
1304                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1305                  pEngineChannel->GlobalVolume = VolumeCurve[itControlChangeEvent->Param.CC.Value] *  CONFIG_GLOBAL_ATTENUATION;                  pEngineChannel->MidiVolume = VolumeCurve[itControlChangeEvent->Param.CC.Value];
1306                  pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag                  pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag
1307                  break;                  break;
1308              }              }
# Line 1349  namespace LinuxSampler { namespace gig { Line 1417  namespace LinuxSampler { namespace gig {
1417                  break;                  break;
1418              }              }
1419              case 123: { // all notes off              case 123: { // all notes off
1420                    #if CONFIG_PROCESS_ALL_NOTES_OFF
1421                  ReleaseAllVoices(pEngineChannel, itControlChangeEvent);                  ReleaseAllVoices(pEngineChannel, itControlChangeEvent);
1422                    #endif // CONFIG_PROCESS_ALL_NOTES_OFF
1423                  break;                  break;
1424              }              }
1425              case 126: { // mono mode on              case 126: { // mono mode on
# Line 1363  namespace LinuxSampler { namespace gig { Line 1433  namespace LinuxSampler { namespace gig {
1433                  break;                  break;
1434              }              }
1435          }          }
1436    
1437            // handle FX send controllers
1438            if (!pEngineChannel->fxSends.empty()) {
1439                for (int iFxSend = 0; iFxSend < pEngineChannel->GetFxSendCount(); iFxSend++) {
1440                    FxSend* pFxSend = pEngineChannel->GetFxSend(iFxSend);
1441                    if (pFxSend->MidiController() == itControlChangeEvent->Param.CC.Controller)
1442                        pFxSend->SetLevel(itControlChangeEvent->Param.CC.Value);
1443                }
1444            }
1445      }      }
1446    
1447      /**      /**
# Line 1371  namespace LinuxSampler { namespace gig { Line 1450  namespace LinuxSampler { namespace gig {
1450       *  @param itSysexEvent - sysex data size and time stamp of the sysex event       *  @param itSysexEvent - sysex data size and time stamp of the sysex event
1451       */       */
1452      void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) {      void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) {
1453          RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();          RingBuffer<uint8_t,false>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader();
1454    
1455          uint8_t exclusive_status, id;          uint8_t exclusive_status, id;
1456          if (!reader.pop(&exclusive_status)) goto free_sysex_data;          if (!reader.pop(&exclusive_status)) goto free_sysex_data;
# Line 1390  namespace LinuxSampler { namespace gig { Line 1469  namespace LinuxSampler { namespace gig {
1469    
1470                  // command address                  // command address
1471                  uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB)                  uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB)
1472                  const RingBuffer<uint8_t>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later                  const RingBuffer<uint8_t,false>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later
1473                  if (reader.read(&addr[0], 3) != 3) goto free_sysex_data;                  if (reader.read(&addr[0], 3) != 3) goto free_sysex_data;
1474                  if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters                  if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters
1475                      dmsg(3,("\tSystem Parameter\n"));                      dmsg(3,("\tSystem Parameter\n"));
# Line 1437  namespace LinuxSampler { namespace gig { Line 1516  namespace LinuxSampler { namespace gig {
1516       *                     question       *                     question
1517       * @param DataSize   - size of the GS message data (in bytes)       * @param DataSize   - size of the GS message data (in bytes)
1518       */       */
1519      uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t>::NonVolatileReader AddrReader, uint DataSize) {      uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize) {
1520          RingBuffer<uint8_t>::NonVolatileReader reader = AddrReader;          RingBuffer<uint8_t,false>::NonVolatileReader reader = AddrReader;
1521          uint bytes = 3 /*addr*/ + DataSize;          uint bytes = 3 /*addr*/ + DataSize;
1522          uint8_t addr_and_data[bytes];          uint8_t addr_and_data[bytes];
1523          reader.read(&addr_and_data[0], bytes);          reader.read(&addr_and_data[0], bytes);
# Line 1557  namespace LinuxSampler { namespace gig { Line 1636  namespace LinuxSampler { namespace gig {
1636      }      }
1637    
1638      String Engine::Version() {      String Engine::Version() {
1639          String s = "$Revision: 1.59 $";          String s = "$Revision: 1.70 $";
1640          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
1641      }      }
1642    
1643        InstrumentManager* Engine::GetInstrumentManager() {
1644            return &instruments;
1645        }
1646    
1647      // static constant initializers      // static constant initializers
1648      const float* Engine::VolumeCurve(InitVolumeCurve());      const float* Engine::VolumeCurve(InitVolumeCurve());
1649      const float* Engine::PanCurve(InitPanCurve());      const float* Engine::PanCurve(InitPanCurve());

Legend:
Removed from v.832  
changed lines
  Added in v.1037

  ViewVC Help
Powered by ViewVC