1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2008 - 2012 Andreas Persson * |
* Copyright (C) 2008 - 2013 Andreas Persson * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
6 |
* 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 * |
34 |
#endif |
#endif |
35 |
|
|
36 |
#include "PluginVst.h" |
#include "PluginVst.h" |
37 |
|
#include "../../drivers/midi/MidiInstrumentMapper.h" |
38 |
|
|
39 |
#ifndef CHANNELS |
#ifndef CHANNELS |
40 |
#define CHANNELS 32 |
#define CHANNELS 32 |
41 |
#endif |
#endif |
42 |
|
|
43 |
|
// the sampler can actually hold up a very huge amount of programs |
44 |
|
// (2^32 [maps] * 2^16 [banks MSB + LSB] * 2^8 [programs per bank] = 2^56 [total programs] ) |
45 |
|
// however I am not sure if we might crash some VST host with a number |
46 |
|
// here that is too huge, however this should be enough in most cases ... |
47 |
|
#define MAX_VST_PROGRAMS (128*128) |
48 |
|
|
49 |
|
using namespace LinuxSampler; |
50 |
|
|
51 |
namespace { |
namespace { |
52 |
|
|
53 |
#if defined(WIN32) && CONFIG_DEBUG_LEVEL >= 2 |
#if defined(WIN32) && CONFIG_DEBUG_LEVEL >= 2 |
58 |
LinuxSampler::Mutex logmutex; |
LinuxSampler::Mutex logmutex; |
59 |
|
|
60 |
void log(const char* fmt, ...) { |
void log(const char* fmt, ...) { |
61 |
logmutex.Lock(); |
LockGuard lock(logmutex); |
62 |
FILE* f = fopen((String(getenv("TEMP")) + "\\linuxsamplervst.log").c_str(), "a"); |
FILE* f = fopen((String(getenv("TEMP")) + "\\linuxsamplervst.log").c_str(), "a"); |
63 |
va_list ap; |
va_list ap; |
64 |
va_start(ap, fmt); |
va_start(ap, fmt); |
65 |
vfprintf(f, fmt, ap); |
vfprintf(f, fmt, ap); |
66 |
va_end(ap); |
va_end(ap); |
67 |
fclose(f); |
fclose(f); |
|
logmutex.Unlock(); |
|
68 |
} |
} |
69 |
#undef dmsg |
#undef dmsg |
70 |
#define dmsg(debuglevel,x) log x; |
#define dmsg(debuglevel,x) log x; |
71 |
#endif |
#endif |
72 |
|
|
73 |
|
|
|
// *************** LinuxSamplerVstProgram *************** |
|
|
// * |
|
|
|
|
|
LinuxSamplerVstProgram::LinuxSamplerVstProgram() { |
|
|
vst_strncpy(name, "Basic", kVstMaxProgNameLen); |
|
|
} |
|
|
|
|
|
|
|
74 |
// *************** LinuxSamplerEditor *************** |
// *************** LinuxSamplerEditor *************** |
75 |
// * |
// * |
76 |
|
|
255 |
// * |
// * |
256 |
|
|
257 |
LinuxSamplerVst::LinuxSamplerVst(audioMasterCallback audioMaster) : |
LinuxSamplerVst::LinuxSamplerVst(audioMasterCallback audioMaster) : |
258 |
AudioEffectX(audioMaster, NbPrograms, 0), |
AudioEffectX(audioMaster, MAX_VST_PROGRAMS, 0), |
259 |
StateBuf(0) |
StateBuf(0) |
260 |
{ |
{ |
261 |
dmsg(2, ("-->constructor\n")); |
dmsg(2, ("-->constructor\n")); |
262 |
|
|
|
Programs = new LinuxSamplerVstProgram[NbPrograms]; |
|
263 |
setProgram(0); |
setProgram(0); |
264 |
setNumInputs(0); |
setNumInputs(0); |
265 |
setNumOutputs(CHANNELS); |
setNumOutputs(CHANNELS); |
299 |
|
|
300 |
LinuxSamplerVst::~LinuxSamplerVst() { |
LinuxSamplerVst::~LinuxSamplerVst() { |
301 |
dmsg(2, ("-->destructor\n")); |
dmsg(2, ("-->destructor\n")); |
|
delete[] Programs; |
|
302 |
if (StateBuf) free(StateBuf); |
if (StateBuf) free(StateBuf); |
303 |
dmsg(2, ("<--destructor\n")); |
dmsg(2, ("<--destructor\n")); |
304 |
} |
} |
305 |
|
|
306 |
|
|
307 |
void LinuxSamplerVst::setProgram(VstInt32 program) { |
void LinuxSamplerVst::setProgram(VstInt32 program) { |
308 |
if (program < 0 || program >= NbPrograms) return; |
if (program < 0 || program >= MAX_VST_PROGRAMS || |
309 |
|
!pMidiDevice || !pMidiDevice->Port()) return; |
310 |
curProgram = program; |
|
311 |
|
int i = 0; |
312 |
|
const std::vector<int> maps = MidiInstrumentMapper::Maps(); |
313 |
|
for (int iMap = 0; iMap < maps.size(); ++iMap) { |
314 |
|
const int mapID = maps[iMap]; |
315 |
|
const std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t> mappings = |
316 |
|
MidiInstrumentMapper::Entries(maps[mapID]); |
317 |
|
for (std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t>::const_iterator iter = mappings.begin(); |
318 |
|
iter != mappings.end(); ++iter, ++i) |
319 |
|
{ |
320 |
|
if (i == program) { |
321 |
|
//TODO: switch MIDI instrument map before sending bank select and program change |
322 |
|
|
323 |
|
const uint iMIDIChannel = 0; |
324 |
|
pMidiDevice->Port()->DispatchBankSelectMsb(iter->first.midi_bank_msb, iMIDIChannel); |
325 |
|
pMidiDevice->Port()->DispatchBankSelectLsb(iter->first.midi_bank_lsb, iMIDIChannel); |
326 |
|
pMidiDevice->Port()->DispatchProgramChange(iter->first.midi_prog, iMIDIChannel); |
327 |
|
|
328 |
|
curProgram = program; |
329 |
|
|
330 |
|
return; |
331 |
|
} |
332 |
|
} |
333 |
|
} |
334 |
} |
} |
335 |
|
|
|
|
|
336 |
void LinuxSamplerVst::setProgramName(char* name) { |
void LinuxSamplerVst::setProgramName(char* name) { |
337 |
vst_strncpy(Programs[curProgram].name, name, kVstMaxProgNameLen); |
int i = 0; |
338 |
|
const std::vector<int> maps = MidiInstrumentMapper::Maps(); |
339 |
|
for (int iMap = 0; iMap < maps.size(); ++iMap) { |
340 |
|
const int mapID = maps[iMap]; |
341 |
|
const std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t> mappings = |
342 |
|
MidiInstrumentMapper::Entries(maps[mapID]); |
343 |
|
for (std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t>::const_iterator iter = mappings.begin(); |
344 |
|
iter != mappings.end(); ++iter, ++i) |
345 |
|
{ |
346 |
|
if (i == curProgram) { |
347 |
|
char buf[kVstMaxProgNameLen + 1] = {}; |
348 |
|
vst_strncpy(buf, name, kVstMaxProgNameLen); |
349 |
|
MidiInstrumentMapper::entry_t entry = iter->second; |
350 |
|
entry.Name = buf; |
351 |
|
try { |
352 |
|
MidiInstrumentMapper::AddOrReplaceEntry( |
353 |
|
mapID, iter->first, entry, false/*bInBackground*/ |
354 |
|
); |
355 |
|
} catch (Exception e) { |
356 |
|
e.PrintMessage(); |
357 |
|
} |
358 |
|
return; |
359 |
|
} |
360 |
|
} |
361 |
|
} |
362 |
} |
} |
363 |
|
|
|
|
|
364 |
void LinuxSamplerVst::getProgramName(char* name) { |
void LinuxSamplerVst::getProgramName(char* name) { |
365 |
vst_strncpy(name, Programs[curProgram].name, kVstMaxProgNameLen); |
if (!getProgramNameIndexed(0 /*dont care*/, curProgram, name)) { |
366 |
|
vst_strncpy(name, "unknown", kVstMaxProgNameLen); |
367 |
|
} |
368 |
|
} |
369 |
|
|
370 |
|
bool LinuxSamplerVst::getProgramNameIndexed(VstInt32 /*category*/, VstInt32 index, |
371 |
|
char* text) |
372 |
|
{ |
373 |
|
//NOTE: parameter 'category' is unused in VST 2.4 |
374 |
|
|
375 |
|
int i = 0; |
376 |
|
const std::vector<int> maps = MidiInstrumentMapper::Maps(); |
377 |
|
for (int iMap = 0; iMap < maps.size(); ++iMap) { |
378 |
|
const int mapID = maps[iMap]; |
379 |
|
const std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t> mappings = |
380 |
|
MidiInstrumentMapper::Entries(maps[mapID]); |
381 |
|
for (std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t>::const_iterator iter = mappings.begin(); |
382 |
|
iter != mappings.end(); ++iter, ++i) |
383 |
|
{ |
384 |
|
if (i == index) { |
385 |
|
vst_strncpy(text, iter->second.Name.c_str(), kVstMaxProgNameLen); |
386 |
|
return true; |
387 |
|
} |
388 |
|
} |
389 |
|
} |
390 |
|
return false; |
391 |
} |
} |
392 |
|
|
393 |
|
|
402 |
} |
} |
403 |
|
|
404 |
|
|
|
bool LinuxSamplerVst::getProgramNameIndexed(VstInt32 category, VstInt32 index, |
|
|
char* text) { |
|
|
if (index < NbPrograms) { |
|
|
vst_strncpy(text, Programs[index].name, kVstMaxProgNameLen); |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
405 |
bool LinuxSamplerVst::getEffectName(char* name) { |
bool LinuxSamplerVst::getEffectName(char* name) { |
406 |
vst_strncpy(name, "LinuxSampler", kVstMaxEffectNameLen); |
vst_strncpy(name, "LinuxSampler", kVstMaxEffectNameLen); |
407 |
return true; |
return true; |
427 |
|
|
428 |
VstInt32 LinuxSamplerVst::canDo(char* text) { |
VstInt32 LinuxSamplerVst::canDo(char* text) { |
429 |
dmsg(2, ("canDo %s\n", text)); |
dmsg(2, ("canDo %s\n", text)); |
430 |
|
|
431 |
|
// supported features |
432 |
if (strcmp(text, "receiveVstEvents") == 0 || |
if (strcmp(text, "receiveVstEvents") == 0 || |
433 |
strcmp(text, "receiveVstMidiEvent") == 0) return 1; |
strcmp(text, "receiveVstMidiEvent") == 0 || |
434 |
return -1; |
strcmp(text, "midiProgramNames") == 0) return 1; |
435 |
|
|
436 |
|
// not supported features |
437 |
|
if (strcmp(text, "sendVstEvents") == 0 || |
438 |
|
strcmp(text, "sendVstMidiEvent") == 0 || |
439 |
|
strcmp(text, "receiveVstTimeInfo") == 0 || |
440 |
|
strcmp(text, "offline") == 0 || |
441 |
|
strcmp(text, "bypass") == 0) return -1; |
442 |
|
|
443 |
|
return 0; // "don't know", never heard of this feature |
444 |
} |
} |
445 |
|
|
446 |
void LinuxSamplerVst::setSampleRate(float sampleRate) { |
void LinuxSamplerVst::setSampleRate(float sampleRate) { |