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 - 2009 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 * |
23 |
|
|
24 |
#include "MidiInputPort.h" |
#include "MidiInputPort.h" |
25 |
|
|
26 |
|
#include "../../common/global_private.h" |
27 |
#include "MidiInstrumentMapper.h" |
#include "MidiInstrumentMapper.h" |
28 |
#include "../../Sampler.h" |
#include "../../Sampler.h" |
29 |
#include "../../engines/EngineFactory.h" |
#include "../../engines/EngineFactory.h" |
30 |
|
#include "VirtualMidiDevice.h" |
31 |
|
|
32 |
|
#include <algorithm> |
33 |
|
|
34 |
namespace LinuxSampler { |
namespace LinuxSampler { |
35 |
|
|
76 |
|
|
77 |
MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber) |
MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber) |
78 |
: MidiChannelMapReader(MidiChannelMap), |
: MidiChannelMapReader(MidiChannelMap), |
79 |
SysexListenersReader(SysexListeners) { |
SysexListenersReader(SysexListeners), |
80 |
|
virtualMidiDevicesReader(virtualMidiDevices) { |
81 |
this->pDevice = pDevice; |
this->pDevice = pDevice; |
82 |
this->portNumber = portNumber; |
this->portNumber = portNumber; |
83 |
Parameters["NAME"] = new ParameterName(this); |
Parameters["NAME"] = new ParameterName(this); |
111 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity); |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity); |
112 |
} |
} |
113 |
MidiChannelMapReader.Unlock(); |
MidiChannelMapReader.Unlock(); |
114 |
|
|
115 |
|
// dispatch event to all low priority MIDI listeners |
116 |
|
const std::vector<VirtualMidiDevice*>& listeners = |
117 |
|
virtualMidiDevicesReader.Lock(); |
118 |
|
for (int i = 0; i < listeners.size(); ++i) |
119 |
|
listeners[i]->SendNoteOnToDevice(Key, Velocity); |
120 |
|
virtualMidiDevicesReader.Unlock(); |
121 |
} |
} |
122 |
|
|
123 |
void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos) { |
void MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos) { |
136 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity, FragmentPos); |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity, FragmentPos); |
137 |
} |
} |
138 |
MidiChannelMapReader.Unlock(); |
MidiChannelMapReader.Unlock(); |
139 |
|
|
140 |
|
// dispatch event to all low priority MIDI listeners |
141 |
|
const std::vector<VirtualMidiDevice*>& listeners = |
142 |
|
virtualMidiDevicesReader.Lock(); |
143 |
|
for (int i = 0; i < listeners.size(); ++i) |
144 |
|
listeners[i]->SendNoteOnToDevice(Key, Velocity); |
145 |
|
virtualMidiDevicesReader.Unlock(); |
146 |
} |
} |
147 |
|
|
148 |
void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) { |
void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) { |
161 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity); |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity); |
162 |
} |
} |
163 |
MidiChannelMapReader.Unlock(); |
MidiChannelMapReader.Unlock(); |
164 |
|
|
165 |
|
// dispatch event to all low priority MIDI listeners |
166 |
|
const std::vector<VirtualMidiDevice*>& listeners = |
167 |
|
virtualMidiDevicesReader.Lock(); |
168 |
|
for (int i = 0; i < listeners.size(); ++i) |
169 |
|
listeners[i]->SendNoteOffToDevice(Key, Velocity); |
170 |
|
virtualMidiDevicesReader.Unlock(); |
171 |
} |
} |
172 |
|
|
173 |
void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos) { |
void MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel, int32_t FragmentPos) { |
186 |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity, FragmentPos); |
for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity, FragmentPos); |
187 |
} |
} |
188 |
MidiChannelMapReader.Unlock(); |
MidiChannelMapReader.Unlock(); |
189 |
|
|
190 |
|
// dispatch event to all low priority MIDI listeners |
191 |
|
const std::vector<VirtualMidiDevice*>& listeners = |
192 |
|
virtualMidiDevicesReader.Lock(); |
193 |
|
for (int i = 0; i < listeners.size(); ++i) |
194 |
|
listeners[i]->SendNoteOffToDevice(Key, Velocity); |
195 |
|
virtualMidiDevicesReader.Unlock(); |
196 |
} |
} |
197 |
|
|
198 |
void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) { |
void MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) { |
272 |
// dispatch event to all engine instances |
// dispatch event to all engine instances |
273 |
std::set<Engine*>::iterator engineiter = allEngines.begin(); |
std::set<Engine*>::iterator engineiter = allEngines.begin(); |
274 |
std::set<Engine*>::iterator end = allEngines.end(); |
std::set<Engine*>::iterator end = allEngines.end(); |
275 |
for (; engineiter != end; engineiter++) (*engineiter)->SendSysex(pData, Size); |
for (; engineiter != end; engineiter++) (*engineiter)->SendSysex(pData, Size, this); |
276 |
SysexListenersReader.Unlock(); |
SysexListenersReader.Unlock(); |
277 |
} |
} |
278 |
|
|
279 |
void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) { |
void MidiInputPort::DispatchProgramChange(uint8_t Program, uint MidiChannel) { |
|
dmsg(1,("Received MIDI program change (prog=%d,ch=%d)\n",Program,MidiChannel)); |
|
280 |
if (Program > 127 || MidiChannel > 16) return; |
if (Program > 127 || MidiChannel > 16) return; |
281 |
if (!pDevice || !pDevice->pSampler) { |
if (!pDevice || !pDevice->pSampler) { |
282 |
std::cerr << "MidiInputPort: ERROR, no sampler instance to handle program change." |
std::cerr << "MidiInputPort: ERROR, no sampler instance to handle program change." |
283 |
<< "This is a bug, please report it!\n" << std::flush; |
<< "This is a bug, please report it!\n" << std::flush; |
284 |
return; |
return; |
285 |
} |
} |
|
std::vector<int> maps = MidiInstrumentMapper::Maps(); |
|
|
if (maps.empty()) return; |
|
286 |
|
|
287 |
const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock(); |
const MidiChannelMap_t& midiChannelMap = MidiChannelMapReader.Lock(); |
288 |
// dispatch event for engines listening to the same MIDI channel |
// dispatch event for engines listening to the same MIDI channel |
289 |
{ |
{ |
290 |
std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin(); |
std::set<EngineChannel*>::iterator engineiter = midiChannelMap[MidiChannel].begin(); |
291 |
std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end(); |
std::set<EngineChannel*>::iterator end = midiChannelMap[MidiChannel].end(); |
292 |
for (; engineiter != end; engineiter++) { |
for (; engineiter != end; engineiter++) (*engineiter)->SendProgramChange(Program); |
|
(*engineiter)->SetMidiProgram(Program); |
|
|
if ((*engineiter)->UsesNoMidiInstrumentMap()) continue; |
|
|
if (MidiInstrumentMapper::GetMapCount() == 0) continue; |
|
|
// retrieve the MIDI instrument map this engine channel is assigned to |
|
|
int iMapID = ((*engineiter)->UsesDefaultMidiInstrumentMap()) |
|
|
? MidiInstrumentMapper::GetDefaultMap() /*default*/ : (*engineiter)->GetMidiInstrumentMap(); |
|
|
// is there an entry for this MIDI bank&prog pair in that map? |
|
|
midi_prog_index_t midiIndex; |
|
|
midiIndex.midi_bank_msb = (*engineiter)->GetMidiBankMsb(); |
|
|
midiIndex.midi_bank_lsb = (*engineiter)->GetMidiBankLsb(); |
|
|
midiIndex.midi_prog = (*engineiter)->GetMidiProgram(); |
|
|
optional<MidiInstrumentMapper::entry_t> mapping = |
|
|
MidiInstrumentMapper::GetEntry(iMapID, midiIndex); |
|
|
if (mapping) { // if mapping exists ... |
|
|
InstrumentManager::instrument_id_t id; |
|
|
id.FileName = mapping->InstrumentFile; |
|
|
id.Index = mapping->InstrumentIndex; |
|
|
//TODO: we should switch the engine type here |
|
|
InstrumentManager::LoadInstrumentInBackground(id, *engineiter); |
|
|
(*engineiter)->Volume(mapping->Volume); |
|
|
} |
|
|
} |
|
293 |
} |
} |
294 |
// dispatch event for engines listening to ALL MIDI channels |
// dispatch event for engines listening to ALL MIDI channels |
295 |
{ |
{ |
296 |
std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin(); |
std::set<EngineChannel*>::iterator engineiter = midiChannelMap[midi_chan_all].begin(); |
297 |
std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end(); |
std::set<EngineChannel*>::iterator end = midiChannelMap[midi_chan_all].end(); |
298 |
for (; engineiter != end; engineiter++) { |
for (; engineiter != end; engineiter++) (*engineiter)->SendProgramChange(Program); |
|
(*engineiter)->SetMidiProgram(Program); |
|
|
if ((*engineiter)->UsesNoMidiInstrumentMap()) continue; |
|
|
if (MidiInstrumentMapper::GetMapCount() == 0) continue; |
|
|
// retrieve the MIDI instrument map this engine channel is assigned to |
|
|
int iMapID = ((*engineiter)->UsesDefaultMidiInstrumentMap()) |
|
|
? MidiInstrumentMapper::GetDefaultMap() /*default*/ : (*engineiter)->GetMidiInstrumentMap(); |
|
|
// is there an entry for this MIDI bank&prog pair in that map? |
|
|
midi_prog_index_t midiIndex; |
|
|
midiIndex.midi_bank_msb = (*engineiter)->GetMidiBankMsb(); |
|
|
midiIndex.midi_bank_lsb = (*engineiter)->GetMidiBankLsb(); |
|
|
midiIndex.midi_prog = (*engineiter)->GetMidiProgram(); |
|
|
optional<MidiInstrumentMapper::entry_t> mapping = |
|
|
MidiInstrumentMapper::GetEntry(iMapID, midiIndex); |
|
|
if (mapping) { // if mapping exists ... |
|
|
InstrumentManager::instrument_id_t id; |
|
|
id.FileName = mapping->InstrumentFile; |
|
|
id.Index = mapping->InstrumentIndex; |
|
|
//TODO: we should switch the engine type here |
|
|
InstrumentManager::LoadInstrumentInBackground(id, *engineiter); |
|
|
(*engineiter)->Volume(mapping->Volume); |
|
|
} |
|
|
} |
|
299 |
} |
} |
300 |
MidiChannelMapReader.Unlock(); |
MidiChannelMapReader.Unlock(); |
301 |
} |
} |
350 |
MidiChannelMapReader.Unlock(); |
MidiChannelMapReader.Unlock(); |
351 |
} |
} |
352 |
|
|
353 |
|
void MidiInputPort::DispatchRaw(uint8_t* pData) { |
354 |
|
uint8_t channel = pData[0] & 0x0f; |
355 |
|
switch (pData[0] & 0xf0) { |
356 |
|
case 0x80: |
357 |
|
DispatchNoteOff(pData[1], pData[2], channel); |
358 |
|
break; |
359 |
|
case 0x90: |
360 |
|
if (pData[2]) { |
361 |
|
DispatchNoteOn(pData[1], pData[2], channel); |
362 |
|
} else { |
363 |
|
DispatchNoteOff(pData[1], pData[2], channel); |
364 |
|
} |
365 |
|
break; |
366 |
|
case 0xb0: |
367 |
|
if (pData[1] == 0) { |
368 |
|
DispatchBankSelectMsb(pData[2], channel); |
369 |
|
} else if (pData[1] == 32) { |
370 |
|
DispatchBankSelectLsb(pData[2], channel); |
371 |
|
} |
372 |
|
DispatchControlChange(pData[1], pData[2], channel); |
373 |
|
break; |
374 |
|
case 0xc0: |
375 |
|
DispatchProgramChange(pData[1], channel); |
376 |
|
break; |
377 |
|
case 0xd0: |
378 |
|
DispatchControlChange(128, pData[1], channel); |
379 |
|
break; |
380 |
|
case 0xe0: |
381 |
|
DispatchPitchbend((pData[1] | pData[2] << 7) - 8192, channel); |
382 |
|
break; |
383 |
|
} |
384 |
|
} |
385 |
|
|
386 |
|
void MidiInputPort::DispatchRaw(uint8_t* pData, int32_t FragmentPos) { |
387 |
|
uint8_t channel = pData[0] & 0x0f; |
388 |
|
switch (pData[0] & 0xf0) { |
389 |
|
case 0x80: |
390 |
|
DispatchNoteOff(pData[1], pData[2], channel, FragmentPos); |
391 |
|
break; |
392 |
|
case 0x90: |
393 |
|
if (pData[2]) { |
394 |
|
DispatchNoteOn(pData[1], pData[2], channel, FragmentPos); |
395 |
|
} else { |
396 |
|
DispatchNoteOff(pData[1], pData[2], channel, FragmentPos); |
397 |
|
} |
398 |
|
break; |
399 |
|
case 0xb0: |
400 |
|
if (pData[1] == 0) { |
401 |
|
DispatchBankSelectMsb(pData[2], channel); |
402 |
|
} else if (pData[1] == 32) { |
403 |
|
DispatchBankSelectLsb(pData[2], channel); |
404 |
|
} |
405 |
|
DispatchControlChange(pData[1], pData[2], channel, FragmentPos); |
406 |
|
break; |
407 |
|
case 0xc0: |
408 |
|
DispatchProgramChange(pData[1], channel); |
409 |
|
break; |
410 |
|
case 0xd0: |
411 |
|
DispatchControlChange(128, pData[1], channel, FragmentPos); |
412 |
|
break; |
413 |
|
case 0xe0: |
414 |
|
DispatchPitchbend((pData[1] | pData[2] << 7) - 8192, channel, FragmentPos); |
415 |
|
break; |
416 |
|
} |
417 |
|
} |
418 |
|
|
419 |
void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) { |
void MidiInputPort::Connect(EngineChannel* pEngineChannel, midi_chan_t MidiChannel) { |
420 |
if (MidiChannel < 0 || MidiChannel > 16) |
if (MidiChannel < 0 || MidiChannel > 16) |
421 |
throw MidiInputException("MIDI channel index out of bounds"); |
throw MidiInputException("MIDI channel index out of bounds"); |
490 |
return count; |
return count; |
491 |
} |
} |
492 |
|
|
493 |
|
void MidiInputPort::Connect(VirtualMidiDevice* pDevice) { |
494 |
|
virtualMidiDevicesMutex.Lock(); |
495 |
|
// double buffer ... double work ... |
496 |
|
{ |
497 |
|
std::vector<VirtualMidiDevice*>& devices = |
498 |
|
virtualMidiDevices.GetConfigForUpdate(); |
499 |
|
devices.push_back(pDevice); |
500 |
|
} |
501 |
|
{ |
502 |
|
std::vector<VirtualMidiDevice*>& devices = |
503 |
|
virtualMidiDevices.SwitchConfig(); |
504 |
|
devices.push_back(pDevice); |
505 |
|
} |
506 |
|
virtualMidiDevicesMutex.Unlock(); |
507 |
|
} |
508 |
|
|
509 |
|
void MidiInputPort::Disconnect(VirtualMidiDevice* pDevice) { |
510 |
|
virtualMidiDevicesMutex.Lock(); |
511 |
|
// double buffer ... double work ... |
512 |
|
{ |
513 |
|
std::vector<VirtualMidiDevice*>& devices = |
514 |
|
virtualMidiDevices.GetConfigForUpdate(); |
515 |
|
devices.erase(std::find(devices.begin(), devices.end(), pDevice)); |
516 |
|
} |
517 |
|
{ |
518 |
|
std::vector<VirtualMidiDevice*>& devices = |
519 |
|
virtualMidiDevices.SwitchConfig(); |
520 |
|
devices.erase(std::find(devices.begin(), devices.end(), pDevice)); |
521 |
|
} |
522 |
|
virtualMidiDevicesMutex.Unlock(); |
523 |
|
} |
524 |
|
|
525 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |