/[svn]/linuxsampler/trunk/src/drivers/midi/MidiInputPort.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/drivers/midi/MidiInputPort.cpp

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

revision 922 by persson, Mon Oct 2 18:40:10 2006 UTC revision 973 by schoenebeck, Fri Dec 15 21:40:27 2006 UTC
# Line 23  Line 23 
23    
24  #include "MidiInputPort.h"  #include "MidiInputPort.h"
25    
26    #include "MidiInstrumentMapper.h"
27  #include "../../Sampler.h"  #include "../../Sampler.h"
28  #include "../../engines/EngineFactory.h"  #include "../../engines/EngineFactory.h"
29    
# Line 75  namespace LinuxSampler { Line 76  namespace LinuxSampler {
76          this->pDevice = pDevice;          this->pDevice = pDevice;
77          this->portNumber = portNumber;          this->portNumber = portNumber;
78          Parameters["NAME"] = new ParameterName(this);          Parameters["NAME"] = new ParameterName(this);
         pPreviousProgramChangeEngineChannel = NULL;  
79      }      }
80    
81      MidiInputDevice* MidiInputPort::GetDevice() {      MidiInputDevice* MidiInputPort::GetDevice() {
# Line 244  namespace LinuxSampler { Line 244  namespace LinuxSampler {
244      }      }
245    
246      void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) {      void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) {
247            dmsg(1,("Received MIDI program change (prog=%d,ch=%d)\n",Program,MidiChannel));
248          if (Program > 127 || MidiChannel > 16) return;          if (Program > 127 || MidiChannel > 16) return;
249          if (!pDevice || !pDevice->pSampler) {          if (!pDevice || !pDevice->pSampler) {
250              std::cerr << "MidiInputPort: ERROR, no sampler instance to handle program change."              std::cerr << "MidiInputPort: ERROR, no sampler instance to handle program change."
251                        << "This is a bug, please report it!\n" << std::flush;                        << "This is a bug, please report it!\n" << std::flush;
252              return;              return;
253          }          }
254            std::vector<int> maps = MidiInstrumentMapper::Maps();
255            if (maps.empty()) return;
256    
257          Sampler*        pSampler        = (Sampler*) pDevice->pSampler;          const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
258          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(Program);          // dispatch event for engines listening to the same MIDI channel
259          if (!pSamplerChannel) return;          {
260                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
261          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();              std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
262          if (!pEngineChannel) return;              for (; engineiter != end; engineiter++) {
263                    (*engineiter)->SetMidiProgram(Program);
264          // disconnect from the engine channel which was connected by the last PC event                  if ((*engineiter)->UsesNoMidiInstrumentMap()) continue;
265          if (pPreviousProgramChangeEngineChannel)                  // retrieve the MIDI instrument map this engine channel is assigned to
266              Disconnect(pPreviousProgramChangeEngineChannel);                  int iMapID = ((*engineiter)->UsesDefaultMidiInstrumentMap())
267                        ? maps[0] /*default*/ : (*engineiter)->GetMidiInstrumentMap();
268          // now connect to the new engine channel and remember it                  // is there an entry for this MIDI bank&prog pair in that map?
269          try {                  midi_prog_index_t midiIndex;
270              Connect(pEngineChannel, (midi_chan_t) MidiChannel);                  midiIndex.midi_bank_msb = (*engineiter)->GetMidiBankMsb();
271              pPreviousProgramChangeEngineChannel = pEngineChannel;                  midiIndex.midi_bank_lsb = (*engineiter)->GetMidiBankLsb();
272                    midiIndex.midi_prog     = (*engineiter)->GetMidiProgram();
273                    optional<MidiInstrumentMapper::entry_t> mapping =
274                        MidiInstrumentMapper::GetEntry(iMapID, midiIndex);
275                    if (mapping) { // if mapping exists ...
276                        InstrumentManager::instrument_id_t id;
277                        id.FileName = mapping->InstrumentFile;
278                        id.Index    = mapping->InstrumentIndex;
279                        //TODO: we should switch the engine type here
280                        InstrumentManager::LoadInstrumentInBackground(id, *engineiter);
281                        (*engineiter)->Volume(mapping->Volume);
282                    }
283                }
284            }
285            // dispatch event for engines listening to ALL MIDI channels
286            {
287                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
288                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
289                for (; engineiter != end; engineiter++) {
290                    (*engineiter)->SetMidiProgram(Program);
291                    if ((*engineiter)->UsesNoMidiInstrumentMap()) continue;
292                    // retrieve the MIDI instrument map this engine channel is assigned to
293                    int iMapID = ((*engineiter)->UsesDefaultMidiInstrumentMap())
294                        ? maps[0] /*default*/ : (*engineiter)->GetMidiInstrumentMap();
295                    // is there an entry for this MIDI bank&prog pair in that map?
296                    midi_prog_index_t midiIndex;
297                    midiIndex.midi_bank_msb = (*engineiter)->GetMidiBankMsb();
298                    midiIndex.midi_bank_lsb = (*engineiter)->GetMidiBankLsb();
299                    midiIndex.midi_prog     = (*engineiter)->GetMidiProgram();
300                    optional<MidiInstrumentMapper::entry_t> mapping =
301                        MidiInstrumentMapper::GetEntry(iMapID, midiIndex);
302                    if (mapping) { // if mapping exists ...
303                        InstrumentManager::instrument_id_t id;
304                        id.FileName = mapping->InstrumentFile;
305                        id.Index    = mapping->InstrumentIndex;
306                        //TODO: we should switch the engine type here
307                        InstrumentManager::LoadInstrumentInBackground(id, *engineiter);
308                        (*engineiter)->Volume(mapping->Volume);
309                    }
310                }
311            }
312            MidiChannelMapReader.Unlock();
313        }
314    
315        void MidiInputPort::DispatchBankSelectMsb(uint8_t BankMSB, uint MidiChannel) {
316            if (BankMSB > 127 || MidiChannel > 16) return;
317            if (!pDevice || !pDevice->pSampler) {
318                std::cerr << "MidiInputPort: ERROR, no sampler instance to handle bank select MSB."
319                          << "This is a bug, please report it!\n" << std::flush;
320                return;
321            }
322            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
323            // dispatch event for engines listening to the same MIDI channel
324            {
325                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
326                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
327                // according to the MIDI specs, a bank select should not alter the patch
328                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankMsb(BankMSB);
329          }          }
330          catch (...) { /* NOOP */ }          // dispatch event for engines listening to ALL MIDI channels
331            {
332                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
333                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
334                // according to the MIDI specs, a bank select should not alter the patch
335                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankMsb(BankMSB);
336            }
337            MidiChannelMapReader.Unlock();
338        }
339    
340        void MidiInputPort::DispatchBankSelectLsb(uint8_t BankLSB, uint MidiChannel) {
341            if (BankLSB > 127 || MidiChannel > 16) return;
342            if (!pDevice || !pDevice->pSampler) {
343                std::cerr << "MidiInputPort: ERROR, no sampler instance to handle bank select LSB."
344                          << "This is a bug, please report it!\n" << std::flush;
345                return;
346            }
347            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
348            // dispatch event for engines listening to the same MIDI channel
349            {
350                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
351                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
352                // according to the MIDI specs, a bank select should not alter the patch
353                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankLsb(BankLSB);
354            }
355            // dispatch event for engines listening to ALL MIDI channels
356            {
357                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
358                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
359                // according to the MIDI specs, a bank select should not alter the patch
360                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankLsb(BankLSB);
361            }
362            MidiChannelMapReader.Unlock();
363      }      }
364    
365      void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) {      void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) {

Legend:
Removed from v.922  
changed lines
  Added in v.973

  ViewVC Help
Powered by ViewVC