/[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 840 by persson, Sun Feb 26 13:00:08 2006 UTC revision 947 by schoenebeck, Mon Nov 27 21:34:55 2006 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                              *   *   Copyright (C) 2005, 2006 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 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 51  namespace LinuxSampler { Line 52  namespace LinuxSampler {
52          return std::vector<String>();          return std::vector<String>();
53      }      }
54    
55      void MidiInputPort::ParameterName::OnSetValue(String s) throw (LinuxSamplerException) {      void MidiInputPort::ParameterName::OnSetValue(String s) throw (Exception) {
56          return; /* FIXME: Nothing to do here */          return; /* FIXME: Nothing to do here */
57      }      }
58    
# Line 69  namespace LinuxSampler { Line 70  namespace LinuxSampler {
70          Parameters.clear();          Parameters.clear();
71      }      }
72    
73      MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber) {      MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber)
74            : MidiChannelMapReader(MidiChannelMap),
75              SysexListenersReader(SysexListeners) {
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 89  namespace LinuxSampler { Line 91  namespace LinuxSampler {
91      }      }
92    
93      void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) {      void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
94          const MidiChannelMap_t& midiChannelMap = MidiChannelMap.Lock();          if (Key > 127 || Velocity > 127 || MidiChannel > 16) return;
95            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
96          // dispatch event for engines listening to the same MIDI channel          // dispatch event for engines listening to the same MIDI channel
97          {          {
98              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
# Line 102  namespace LinuxSampler { Line 105  namespace LinuxSampler {
105              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
106              for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);              for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
107          }          }
108          MidiChannelMap.Unlock();          MidiChannelMapReader.Unlock();
109        }
110    
111        void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos) {
112            if (Key > 127 || Velocity > 127 || MidiChannel > 16) return;
113            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
114            // dispatch event for engines listening to the same MIDI channel
115            {
116                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
117                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
118                for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity, FragmentPos);
119            }
120            // dispatch event for engines listening to ALL MIDI channels
121            {
122                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
123                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
124                for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity, FragmentPos);
125            }
126            MidiChannelMapReader.Unlock();
127      }      }
128    
129      void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) {      void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
130          const MidiChannelMap_t& midiChannelMap = MidiChannelMap.Lock();          if (Key > 127 || Velocity > 127 || MidiChannel > 16) return;
131            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
132          // dispatch event for engines listening to the same MIDI channel          // dispatch event for engines listening to the same MIDI channel
133          {          {
134              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
# Line 119  namespace LinuxSampler { Line 141  namespace LinuxSampler {
141              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
142              for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);              for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
143          }          }
144          MidiChannelMap.Unlock();          MidiChannelMapReader.Unlock();
145        }
146    
147        void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos) {
148            if (Key > 127 || Velocity > 127 || MidiChannel > 16) return;
149            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
150            // dispatch event for engines listening to the same MIDI channel
151            {
152                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
153                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
154                for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity, FragmentPos);
155            }
156            // dispatch event for engines listening to ALL MIDI channels
157            {
158                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
159                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
160                for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity, FragmentPos);
161            }
162            MidiChannelMapReader.Unlock();
163      }      }
164    
165      void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) {      void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) {
166          const MidiChannelMap_t& midiChannelMap = MidiChannelMap.Lock();          if (Pitch < -8192 || Pitch > 8191 || MidiChannel > 16) return;
167            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
168          // dispatch event for engines listening to the same MIDI channel          // dispatch event for engines listening to the same MIDI channel
169          {          {
170              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
# Line 136  namespace LinuxSampler { Line 177  namespace LinuxSampler {
177              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
178              for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);              for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
179          }          }
180          MidiChannelMap.Unlock();          MidiChannelMapReader.Unlock();
181        }
182    
183        void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel, int32_t FragmentPos) {
184            if (Pitch < -8192 || Pitch > 8191 || MidiChannel > 16) return;
185            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
186            // dispatch event for engines listening to the same MIDI channel
187            {
188                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
189                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
190                for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch, FragmentPos);
191            }
192            // dispatch event for engines listening to ALL MIDI channels
193            {
194                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
195                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
196                for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch, FragmentPos);
197            }
198            MidiChannelMapReader.Unlock();
199      }      }
200    
201      void MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) {      void MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) {
202          const MidiChannelMap_t& midiChannelMap = MidiChannelMap.Lock();          if (Controller > 128 || Value > 127 || MidiChannel > 16) return;
203            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
204          // dispatch event for engines listening to the same MIDI channel          // dispatch event for engines listening to the same MIDI channel
205          {          {
206              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();              std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
# Line 153  namespace LinuxSampler { Line 213  namespace LinuxSampler {
213              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();              std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
214              for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);              for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
215          }          }
216          MidiChannelMap.Unlock();          MidiChannelMapReader.Unlock();
217        }
218    
219        void MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel, int32_t FragmentPos) {
220            if (Controller > 128 || Value > 127 || MidiChannel > 16) return;
221            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
222            // dispatch event for engines listening to the same MIDI channel
223            {
224                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
225                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
226                for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value, FragmentPos);
227            }
228            // dispatch event for engines listening to ALL MIDI channels
229            {
230                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
231                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
232                for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value, FragmentPos);
233            }
234            MidiChannelMapReader.Unlock();
235      }      }
236    
237      void MidiInputPort::DispatchSysex(void* pData, uint Size) {      void MidiInputPort::DispatchSysex(void* pData, uint Size) {
238            const std::set<Engine*> allEngines = SysexListenersReader.Lock();
239          // dispatch event to all engine instances          // dispatch event to all engine instances
240          std::set<Engine*>::iterator engineiter = EngineFactory::EngineInstances().begin();          std::set<Engine*>::iterator engineiter = allEngines.begin();
241          std::set<Engine*>::iterator end        = EngineFactory::EngineInstances().end();          std::set<Engine*>::iterator end        = allEngines.end();
242          for (; engineiter != end; engineiter++) (*engineiter)->SendSysex(pData, Size);          for (; engineiter != end; engineiter++) (*engineiter)->SendSysex(pData, Size);
243            SysexListenersReader.Unlock();
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;
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            const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
255            // dispatch event for engines listening to the same MIDI channel
256            {
257                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
258                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
259                for (; engineiter != end; engineiter++) {
260                    (*engineiter)->SetMidiProgram(Program);
261                    // is there a mapping for this MIDI bank&prog pair?
262                    midi_prog_index_t midiIndex;
263                    midiIndex.midi_bank_msb = (*engineiter)->GetMidiBankMsb();
264                    midiIndex.midi_bank_lsb = (*engineiter)->GetMidiBankLsb();
265                    midiIndex.midi_prog     = (*engineiter)->GetMidiProgram();
266                    optional<MidiInstrumentMapper::entry_t> mapping =
267                        MidiInstrumentMapper::GetEntry(midiIndex);
268                    if (mapping) { // if mapping exists ...
269                        InstrumentManager::instrument_id_t id;
270                        id.FileName = mapping->InstrumentFile;
271                        id.Index    = mapping->InstrumentIndex;
272                        //TODO: we should switch the engine type here
273                        InstrumentManager::LoadInstrumentInBackground(id, *engineiter);
274                        (*engineiter)->Volume(mapping->Volume);
275                    }
276                }
277            }
278            // dispatch event for engines listening to ALL MIDI channels
279            {
280                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
281                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
282                for (; engineiter != end; engineiter++) {
283                    (*engineiter)->SetMidiProgram(Program);
284                    // is there a mapping for this MIDI bank&prog pair?
285                    midi_prog_index_t midiIndex;
286                    midiIndex.midi_bank_msb = (*engineiter)->GetMidiBankMsb();
287                    midiIndex.midi_bank_lsb = (*engineiter)->GetMidiBankLsb();
288                    midiIndex.midi_prog     = (*engineiter)->GetMidiProgram();
289                    optional<MidiInstrumentMapper::entry_t> mapping =
290                        MidiInstrumentMapper::GetEntry(midiIndex);
291                    if (mapping) { // if mapping exists ...
292                        InstrumentManager::instrument_id_t id;
293                        id.FileName = mapping->InstrumentFile;
294                        id.Index    = mapping->InstrumentIndex;
295                        //TODO: we should switch the engine type here
296                        InstrumentManager::LoadInstrumentInBackground(id, *engineiter);
297                        (*engineiter)->Volume(mapping->Volume);
298                    }
299                }
300            }
301            MidiChannelMapReader.Unlock();
302        }
303    
304          Sampler*        pSampler        = (Sampler*) pDevice->pSampler;      void MidiInputPort::DispatchBankSelectMsb(uint8_t BankMSB, uint MidiChannel) {
305          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(Program);          if (BankMSB > 127 || MidiChannel > 16) return;
306          if (!pSamplerChannel) return;          if (!pDevice || !pDevice->pSampler) {
307                std::cerr << "MidiInputPort: ERROR, no sampler instance to handle bank select MSB."
308          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();                        << "This is a bug, please report it!\n" << std::flush;
309          if (!pEngineChannel) return;              return;
310            }
311          // disconnect from the engine channel which was connected by the last PC event          const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
312          if (pPreviousProgramChangeEngineChannel)          // dispatch event for engines listening to the same MIDI channel
313              Disconnect(pPreviousProgramChangeEngineChannel);          {
314                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
315                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
316                // according to the MIDI specs, a bank select should not alter the patch
317                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankMsb(BankMSB);
318            }
319            // dispatch event for engines listening to ALL MIDI channels
320            {
321                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
322                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
323                // according to the MIDI specs, a bank select should not alter the patch
324                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankMsb(BankMSB);
325            }
326            MidiChannelMapReader.Unlock();
327        }
328    
329          // now connect to the new engine channel and remember it      void MidiInputPort::DispatchBankSelectLsb(uint8_t BankLSB, uint MidiChannel) {
330          try {          if (BankLSB > 127 || MidiChannel > 16) return;
331              Connect(pEngineChannel, (midi_chan_t) MidiChannel);          if (!pDevice || !pDevice->pSampler) {
332              pPreviousProgramChangeEngineChannel = pEngineChannel;              std::cerr << "MidiInputPort: ERROR, no sampler instance to handle bank select LSB."
333                          << "This is a bug, please report it!\n" << std::flush;
334                return;
335          }          }
336          catch (...) { /* NOOP */ }          const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock();
337            // dispatch event for engines listening to the same MIDI channel
338            {
339                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin();
340                std::set<EngineChannel*>::iterator end        = midiChannelMap[MidiChannel].end();
341                // according to the MIDI specs, a bank select should not alter the patch
342                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankLsb(BankLSB);
343            }
344            // dispatch event for engines listening to ALL MIDI channels
345            {
346                std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin();
347                std::set<EngineChannel*>::iterator end        = midiChannelMap[midi_chan_all].end();
348                // according to the MIDI specs, a bank select should not alter the patch
349                for (; engineiter != end; engineiter++) (*engineiter)->SetMidiBankLsb(BankLSB);
350            }
351            MidiChannelMapReader.Unlock();
352      }      }
353    
354      void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) {      void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) {
# Line 250  namespace LinuxSampler { Line 412  namespace LinuxSampler {
412          pEngineChannel->StatusChanged(true);          pEngineChannel->StatusChanged(true);
413      }      }
414    
415        SynchronizedConfig<std::set<LinuxSampler::Engine*> > MidiInputPort::SysexListeners;
416    
417        void MidiInputPort::AddSysexListener(Engine* engine) {
418            std::pair<std::set<Engine*>::iterator, bool> p = SysexListeners.GetConfigForUpdate().insert(engine);
419            if (p.second) SysexListeners.SwitchConfig().insert(engine);
420        }
421    
422        bool MidiInputPort::RemoveSysexListener(Engine* engine) {
423            int count = SysexListeners.GetConfigForUpdate().erase(engine);
424            if (count) SysexListeners.SwitchConfig().erase(engine);
425            return count;
426        }
427    
428  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.840  
changed lines
  Added in v.947

  ViewVC Help
Powered by ViewVC