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