1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2008 - 2009 Andreas Persson * |
4 |
* * |
5 |
* 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 * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
18 |
* MA 02110-1301 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include <cstdio> |
22 |
#include <cstdlib> |
23 |
#include <cstring> |
24 |
|
25 |
#ifdef WIN32 |
26 |
#include <tchar.h> |
27 |
#endif |
28 |
|
29 |
#include "PluginVst.h" |
30 |
|
31 |
#ifndef CHANNELS |
32 |
#define CHANNELS 32 |
33 |
#endif |
34 |
|
35 |
namespace { |
36 |
|
37 |
#if defined(WIN32) && CONFIG_DEBUG_LEVEL >= 2 |
38 |
// on windows, send debug logging to a file instead of a non-existent |
39 |
// console |
40 |
#include <cstdarg> |
41 |
#include "../../common/Mutex.h" |
42 |
LinuxSampler::Mutex logmutex; |
43 |
|
44 |
void log(const char* fmt, ...) { |
45 |
logmutex.Lock(); |
46 |
FILE* f = fopen("C:\\linuxsamplervst.log", "a"); |
47 |
va_list ap; |
48 |
va_start(ap, fmt); |
49 |
vfprintf(f, fmt, ap); |
50 |
va_end(ap); |
51 |
fclose(f); |
52 |
logmutex.Unlock(); |
53 |
} |
54 |
#undef dmsg |
55 |
#define dmsg(debuglevel,x) log x; |
56 |
#endif |
57 |
|
58 |
|
59 |
// *************** LinuxSamplerVstProgram *************** |
60 |
// * |
61 |
|
62 |
LinuxSamplerVstProgram::LinuxSamplerVstProgram() { |
63 |
vst_strncpy(name, "Basic", kVstMaxProgNameLen); |
64 |
} |
65 |
|
66 |
|
67 |
// *************** LinuxSamplerEditor *************** |
68 |
// * |
69 |
|
70 |
LinuxSamplerEditor::LinuxSamplerEditor(AudioEffect* effect) : |
71 |
AEffEditor(effect) { |
72 |
dmsg(2, ("-->LinuxSamplerEditor constructor\n")); |
73 |
#ifdef WIN32 |
74 |
ProcessHandle = INVALID_HANDLE_VALUE; |
75 |
#endif |
76 |
} |
77 |
|
78 |
LinuxSamplerEditor::~LinuxSamplerEditor() { |
79 |
close(); |
80 |
} |
81 |
|
82 |
bool LinuxSamplerEditor::open(void* ptr) { |
83 |
dmsg(2, ("-->LinuxSamplerEditor::open\n")); |
84 |
AEffEditor::open(ptr); |
85 |
|
86 |
#ifdef WIN32 |
87 |
// try to start the JSample Fantasia GUI as a separate process |
88 |
|
89 |
// first check if it's already running |
90 |
if (ProcessHandle != INVALID_HANDLE_VALUE) { |
91 |
DWORD exitCode; |
92 |
if (GetExitCodeProcess(ProcessHandle, &exitCode)) { |
93 |
if (exitCode == STILL_ACTIVE) return true; |
94 |
} |
95 |
free(Command); |
96 |
CloseHandle(ProcessHandle); |
97 |
ProcessHandle = INVALID_HANDLE_VALUE; |
98 |
} |
99 |
|
100 |
// assume Fantasia is in the same directory as the |
101 |
// liblinuxsampler dll |
102 |
String lspath = LinuxSampler::Sampler::GetInstallDir(); |
103 |
if (!lspath.empty()) { |
104 |
lspath += "\\"; |
105 |
WIN32_FIND_DATA fd; |
106 |
HANDLE hFind = FindFirstFile((lspath + "Fantasia*.jar").c_str(), &fd); |
107 |
if (hFind != INVALID_HANDLE_VALUE) { |
108 |
String fantasia(fd.cFileName); |
109 |
FindClose(hFind); |
110 |
|
111 |
// start a java process |
112 |
STARTUPINFO si; |
113 |
PROCESS_INFORMATION pi; |
114 |
ZeroMemory(&si, sizeof(si)); |
115 |
si.cb = sizeof(si); |
116 |
ZeroMemory(&pi, sizeof(pi)); |
117 |
|
118 |
Command = _tcsdup(TEXT((String("javaw -jar \"") + lspath + fantasia + "\"").c_str())); |
119 |
CreateProcess(NULL, Command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); |
120 |
ProcessHandle = pi.hProcess; |
121 |
CloseHandle(pi.hThread); |
122 |
} |
123 |
} |
124 |
#endif |
125 |
dmsg(2, ("<--LinuxSamplerEditor::open\n")); |
126 |
return true; |
127 |
} |
128 |
|
129 |
void LinuxSamplerEditor::close() { |
130 |
systemWindow = 0; |
131 |
#ifdef WIN32 |
132 |
if (ProcessHandle != INVALID_HANDLE_VALUE) { |
133 |
TerminateProcess(ProcessHandle, 0); |
134 |
free(Command); |
135 |
CloseHandle(ProcessHandle); |
136 |
ProcessHandle = INVALID_HANDLE_VALUE; |
137 |
} |
138 |
#endif |
139 |
} |
140 |
|
141 |
bool LinuxSamplerEditor::getRect(ERect** rect) { |
142 |
ERect* r = new ERect(); |
143 |
r->top = 0; |
144 |
r->left = 0; |
145 |
r->bottom = 0; |
146 |
r->right = 400; |
147 |
*rect = r; |
148 |
return true; |
149 |
} |
150 |
|
151 |
|
152 |
// *************** LinuxSamplerVst *************** |
153 |
// * |
154 |
|
155 |
LinuxSamplerVst::LinuxSamplerVst(audioMasterCallback audioMaster) : |
156 |
AudioEffectX(audioMaster, NbPrograms, 0), |
157 |
StateBuf(0) |
158 |
{ |
159 |
dmsg(2, ("-->constructor\n")); |
160 |
|
161 |
Programs = new LinuxSamplerVstProgram[NbPrograms]; |
162 |
setProgram(0); |
163 |
setNumInputs(0); |
164 |
setNumOutputs(CHANNELS); |
165 |
canProcessReplacing(); |
166 |
isSynth(); |
167 |
programsAreChunks(); |
168 |
setUniqueID(CCONST('L', 'S', 'm', 'p')); // (registred at Steinberg) |
169 |
setEditor(new LinuxSamplerEditor(this)); |
170 |
|
171 |
suspend(); |
172 |
dmsg(2, ("<--constructor\n")); |
173 |
} |
174 |
|
175 |
|
176 |
void LinuxSamplerVst::resume() { |
177 |
dmsg(2, ("-->resume\n")); |
178 |
if (!pAudioDevice) { |
179 |
Init(int(sampleRate), blockSize, CHANNELS); |
180 |
|
181 |
if (!SavedChunk.empty()) { |
182 |
SetState(SavedChunk); |
183 |
SavedChunk.clear(); |
184 |
} else { |
185 |
InitState(); |
186 |
} |
187 |
} else { |
188 |
Init(int(sampleRate), blockSize); |
189 |
} |
190 |
dmsg(2, ("<--resume\n")); |
191 |
} |
192 |
|
193 |
LinuxSamplerVst::~LinuxSamplerVst() { |
194 |
dmsg(2, ("-->destructor\n")); |
195 |
delete[] Programs; |
196 |
if (StateBuf) free(StateBuf); |
197 |
dmsg(2, ("<--destructor\n")); |
198 |
} |
199 |
|
200 |
|
201 |
void LinuxSamplerVst::setProgram(VstInt32 program) { |
202 |
if (program < 0 || program >= NbPrograms) return; |
203 |
|
204 |
curProgram = program; |
205 |
} |
206 |
|
207 |
|
208 |
void LinuxSamplerVst::setProgramName(char* name) { |
209 |
vst_strncpy(Programs[curProgram].name, name, kVstMaxProgNameLen); |
210 |
} |
211 |
|
212 |
|
213 |
void LinuxSamplerVst::getProgramName(char* name) { |
214 |
vst_strncpy(name, Programs[curProgram].name, kVstMaxProgNameLen); |
215 |
} |
216 |
|
217 |
|
218 |
bool LinuxSamplerVst::getOutputProperties(VstInt32 index, |
219 |
VstPinProperties* properties) { |
220 |
if (index < CHANNELS) { |
221 |
sprintf(properties->label, "LS %d", index + 1); |
222 |
properties->flags = kVstPinIsActive | kVstPinIsStereo; |
223 |
return true; |
224 |
} |
225 |
return false; |
226 |
} |
227 |
|
228 |
|
229 |
bool LinuxSamplerVst::getProgramNameIndexed(VstInt32 category, VstInt32 index, |
230 |
char* text) { |
231 |
if (index < NbPrograms) { |
232 |
vst_strncpy(text, Programs[index].name, kVstMaxProgNameLen); |
233 |
return true; |
234 |
} |
235 |
return false; |
236 |
} |
237 |
|
238 |
|
239 |
bool LinuxSamplerVst::getEffectName(char* name) { |
240 |
vst_strncpy(name, "LinuxSampler", kVstMaxEffectNameLen); |
241 |
return true; |
242 |
} |
243 |
|
244 |
|
245 |
bool LinuxSamplerVst::getVendorString(char* text) { |
246 |
vst_strncpy(text, "linuxsampler.org", kVstMaxVendorStrLen); |
247 |
return true; |
248 |
} |
249 |
|
250 |
|
251 |
bool LinuxSamplerVst::getProductString(char* text) { |
252 |
vst_strncpy(text, "LinuxSampler VST", kVstMaxProductStrLen); |
253 |
return true; |
254 |
} |
255 |
|
256 |
|
257 |
VstInt32 LinuxSamplerVst::getVendorVersion() { |
258 |
return 1000; |
259 |
} |
260 |
|
261 |
|
262 |
VstInt32 LinuxSamplerVst::canDo(char* text) { |
263 |
dmsg(2, ("canDo %s\n", text)); |
264 |
if (strcmp(text, "receiveVstEvents") == 0 || |
265 |
strcmp(text, "receiveVstMidiEvent") == 0) return 1; |
266 |
return -1; |
267 |
} |
268 |
|
269 |
void LinuxSamplerVst::setSampleRate(float sampleRate) { |
270 |
dmsg(2, ("linuxsampler: setSampleRate %f\n", sampleRate)); |
271 |
AudioEffectX::setSampleRate(sampleRate); |
272 |
} |
273 |
|
274 |
void LinuxSamplerVst::setBlockSize(VstInt32 blockSize) { |
275 |
dmsg(2, ("linuxsampler: setBlockSize %d\n", blockSize)); |
276 |
AudioEffectX::setBlockSize(blockSize); |
277 |
} |
278 |
|
279 |
|
280 |
void LinuxSamplerVst::processReplacing(float** inputs, float** outputs, |
281 |
VstInt32 sampleFrames) { |
282 |
if (pAudioDevice) { |
283 |
for (int i = 0 ; i < CHANNELS ; i++) { |
284 |
pAudioDevice->Channel(i)->SetBuffer(outputs[i]); |
285 |
} |
286 |
pAudioDevice->Render(sampleFrames); |
287 |
} else { |
288 |
for (int i = 0 ; i < CHANNELS ; i++) { |
289 |
memset(outputs[i], 0, sampleFrames * sizeof(float)); |
290 |
} |
291 |
} |
292 |
} |
293 |
|
294 |
|
295 |
VstInt32 LinuxSamplerVst::processEvents(VstEvents* ev) { |
296 |
if (pMidiDevice) { |
297 |
for (int i = 0; i < ev->numEvents; i++) |
298 |
{ |
299 |
if (ev->events[i]->type == kVstMidiType) { |
300 |
VstMidiEvent* event = reinterpret_cast<VstMidiEvent*>(ev->events[i]); |
301 |
pMidiDevice->Port()->DispatchRaw(reinterpret_cast<uint8_t*>(event->midiData), |
302 |
event->deltaFrames); |
303 |
} |
304 |
} |
305 |
} |
306 |
return 1; |
307 |
} |
308 |
|
309 |
|
310 |
VstInt32 LinuxSamplerVst::getChunk(void** data, bool isPreset) { |
311 |
dmsg(2, ("-->getChunk\n")); |
312 |
|
313 |
String state = GetState(); |
314 |
dmsg(2, ("state=\"%s\"\n", state.c_str()));; |
315 |
if (StateBuf) free(StateBuf); |
316 |
StateBuf = strdup(state.c_str()); |
317 |
*data = StateBuf; |
318 |
dmsg(2, ("<--getChunk\n")); |
319 |
return state.length() + 1; |
320 |
} |
321 |
|
322 |
|
323 |
VstInt32 LinuxSamplerVst::setChunk(void* data, VstInt32 byteSize, |
324 |
bool isPreset) { |
325 |
dmsg(2, ("-->setChunk byteSize=%d isPreset=%d\n", byteSize, isPreset)); |
326 |
|
327 |
const char* state = static_cast<const char*>(data); |
328 |
VstInt32 res = 0; |
329 |
if (byteSize > 0 && state[byteSize - 1] == '\0') { |
330 |
dmsg(2, ("state=\"%s\"\n", state)); |
331 |
if (pAudioDevice) { |
332 |
res = SetState(state); |
333 |
} else { |
334 |
SavedChunk = state; |
335 |
} |
336 |
} |
337 |
dmsg(2, ("<--setChunk\n")); |
338 |
return res; |
339 |
} |
340 |
} |
341 |
|
342 |
|
343 |
// *************** VST main function *************** |
344 |
// * |
345 |
|
346 |
AudioEffect* createEffectInstance(audioMasterCallback audioMaster) |
347 |
{ |
348 |
return new LinuxSamplerVst(audioMaster); |
349 |
} |