/[svn]/linuxsampler/trunk/src/hostplugins/vst/PluginVst.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/hostplugins/vst/PluginVst.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2360 - (hide annotations) (download)
Thu Aug 16 17:01:35 2012 UTC (11 years, 8 months ago) by persson
File size: 14574 byte(s)
* LV2: fixed save/restore of SFZ state (patch by David Robillard)
* LV2: made LV2 plugin buildable on Windows and Mac

1 persson 1777 /***************************************************************************
2     * *
3 persson 2347 * Copyright (C) 2008 - 2012 Andreas Persson *
4 persson 1777 * *
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 persson 2347 #else
28     #include <cerrno>
29     #include <signal.h>
30     #include <sys/wait.h>
31     #include <unistd.h>
32     #include <dirent.h>
33     #include <sys/stat.h>
34 persson 1777 #endif
35    
36     #include "PluginVst.h"
37    
38 persson 1908 #ifndef CHANNELS
39 persson 1917 #define CHANNELS 32
40 persson 1908 #endif
41    
42 persson 1777 namespace {
43    
44     #if defined(WIN32) && CONFIG_DEBUG_LEVEL >= 2
45     // on windows, send debug logging to a file instead of a non-existent
46     // console
47     #include <cstdarg>
48     #include "../../common/Mutex.h"
49     LinuxSampler::Mutex logmutex;
50    
51     void log(const char* fmt, ...) {
52     logmutex.Lock();
53 persson 2165 FILE* f = fopen((String(getenv("TEMP")) + "\\linuxsamplervst.log").c_str(), "a");
54 persson 1777 va_list ap;
55     va_start(ap, fmt);
56     vfprintf(f, fmt, ap);
57     va_end(ap);
58     fclose(f);
59     logmutex.Unlock();
60     }
61     #undef dmsg
62     #define dmsg(debuglevel,x) log x;
63     #endif
64    
65    
66     // *************** LinuxSamplerVstProgram ***************
67     // *
68    
69     LinuxSamplerVstProgram::LinuxSamplerVstProgram() {
70     vst_strncpy(name, "Basic", kVstMaxProgNameLen);
71     }
72    
73    
74     // *************** LinuxSamplerEditor ***************
75     // *
76    
77     LinuxSamplerEditor::LinuxSamplerEditor(AudioEffect* effect) :
78     AEffEditor(effect) {
79     dmsg(2, ("-->LinuxSamplerEditor constructor\n"));
80 persson 1895 #ifdef WIN32
81     ProcessHandle = INVALID_HANDLE_VALUE;
82 persson 2347 #else
83     pid = 0;
84 persson 1895 #endif
85 persson 1777 }
86    
87 persson 1895 LinuxSamplerEditor::~LinuxSamplerEditor() {
88     close();
89     }
90    
91 persson 2347 String LinuxSamplerEditor::FindFantasia() {
92     String fantasia;
93     #ifdef WIN32
94     // assume Fantasia is in the same directory or one directory above
95     // the liblinuxsampler dll
96     String lspath = LinuxSampler::Sampler::GetInstallDir();
97     if (!lspath.empty()) {
98     lspath += "\\";
99     WIN32_FIND_DATA fd;
100     HANDLE hFind =
101     FindFirstFile((lspath + "Fantasia*.jar").c_str(), &fd);
102     if (hFind == INVALID_HANDLE_VALUE) {
103     lspath += "..\\";
104     hFind = FindFirstFile((lspath + "Fantasia*.jar").c_str(), &fd);
105     }
106     if (hFind != INVALID_HANDLE_VALUE) {
107     fantasia = fd.cFileName;
108     FindClose(hFind);
109     return lspath + fantasia;
110     }
111     }
112     #elif defined(__APPLE__)
113     // look for the java application wrapper
114     const char* cmd =
115     "/Applications/LinuxSampler/Fantasia.app"
116     "/Contents/MacOS/JavaApplicationStub";
117     fantasia = String(getenv("HOME")) + cmd;
118     struct stat buf;
119     if (stat(fantasia.c_str(), &buf) != 0) {
120     fantasia = stat(cmd, &buf) == 0 ? cmd : "";
121     }
122     #else
123     // search in <datadir>/java (default: /usr/local/share/java)
124     String path(DATADIR);
125     path += *(path.end() - 1) == '/' ? "java" : "/java";
126     DIR* dir = opendir(path.c_str());
127     if (dir) {
128     while (dirent* d = readdir(dir)) {
129     const char* name = d->d_name;
130     if (strncmp("Fantasia", name, 8) == 0 &&
131     strcmp(name + strlen(name) - 4, ".jar") == 0) {
132     fantasia = path + "/" + name;
133     }
134     }
135     closedir(dir);
136     }
137     #endif
138     return fantasia;
139     }
140    
141 persson 1777 bool LinuxSamplerEditor::open(void* ptr) {
142     dmsg(2, ("-->LinuxSamplerEditor::open\n"));
143     AEffEditor::open(ptr);
144    
145 persson 2347 // try to start the JSampler Fantasia GUI as a separate process
146 persson 1777 #ifdef WIN32
147 persson 1908 // first check if it's already running
148     if (ProcessHandle != INVALID_HANDLE_VALUE) {
149     DWORD exitCode;
150     if (GetExitCodeProcess(ProcessHandle, &exitCode)) {
151     if (exitCode == STILL_ACTIVE) return true;
152     }
153     free(Command);
154     CloseHandle(ProcessHandle);
155     ProcessHandle = INVALID_HANDLE_VALUE;
156     }
157 persson 1777
158 persson 2347 String fantasia = FindFantasia();
159     if (!fantasia.empty()) {
160     // start a java process
161     String path; // look in PATH first
162     for (int i = 0 ; i < 2 ; i++) { // two tries
163     STARTUPINFO si;
164     PROCESS_INFORMATION pi;
165     ZeroMemory(&si, sizeof(si));
166     si.cb = sizeof(si);
167     ZeroMemory(&pi, sizeof(pi));
168 persson 1777
169 persson 2347 Command = _tcsdup(TEXT((String("\"") + path +
170     "javaw\" -jar \"" + fantasia +
171     "\"").c_str()));
172     if (CreateProcess(NULL, Command, NULL, NULL, FALSE, 0, NULL,
173     NULL, &si, &pi)) {
174     ProcessHandle = pi.hProcess;
175     CloseHandle(pi.hThread);
176     break;
177     } else {
178     free(Command);
179     if (path.empty()) {
180     // java wasn't found in PATH, try again
181     // with an alternative directory
182     char* windir = getenv("windir");
183     if (!windir) break;
184 persson 2165 #ifdef _WIN64
185 persson 2347 // LS plugin is 64 bit - look for 32 bit java
186     path = String(windir) + "\\SysWOW64\\";
187 persson 2165 #else
188 persson 2347 // LS plugin is 32 bit - look for 64 bit java
189     path = String(windir) + "\\Sysnative\\";
190 persson 2165 #endif
191     }
192     }
193 persson 1777 }
194     }
195 persson 2347 #else
196     // first check if it's already running
197     if (pid && waitpid(pid, 0, WNOHANG) == 0) return true;
198    
199     String fantasia = FindFantasia();
200     if (!fantasia.empty())
201     {
202     // start a java process
203     pid = fork();
204     if (pid == -1) {
205     dmsg(1, ("fork failed %d %s\n", errno, strerror(errno)));
206     pid = 0;
207     } else if (pid == 0) {
208     #ifdef __APPLE__
209     execl(fantasia.c_str(), fantasia.c_str(), (char*)0);
210     #else
211     execlp("java", "java", "-jar", fantasia.c_str(), (char*)0);
212 persson 1777 #endif
213 persson 2347 dmsg(1, ("exec failed %d %s\n", errno, strerror(errno)));
214    
215 persson 2360 // make sure something is executed, so the static
216 persson 2347 // destructors copied from the parent don't run
217     execl("/usr/bin/true", "/usr/bin/true", (char*)0);
218     }
219     }
220     #endif
221 persson 1777 dmsg(2, ("<--LinuxSamplerEditor::open\n"));
222     return true;
223     }
224    
225     void LinuxSamplerEditor::close() {
226     systemWindow = 0;
227     #ifdef WIN32
228     if (ProcessHandle != INVALID_HANDLE_VALUE) {
229     TerminateProcess(ProcessHandle, 0);
230     free(Command);
231 persson 1908 CloseHandle(ProcessHandle);
232 persson 1895 ProcessHandle = INVALID_HANDLE_VALUE;
233 persson 1777 }
234 persson 2347 #else
235     if (pid) {
236     kill(pid, SIGTERM);
237     waitpid(pid, 0, 0);
238     pid = 0;
239     }
240 persson 1777 #endif
241     }
242    
243     bool LinuxSamplerEditor::getRect(ERect** rect) {
244     ERect* r = new ERect();
245     r->top = 0;
246     r->left = 0;
247 persson 2347 r->bottom = 1; // 0 makes EnergyXT and Ardour on Linux crash
248 persson 1777 r->right = 400;
249     *rect = r;
250     return true;
251     }
252    
253    
254     // *************** LinuxSamplerVst ***************
255     // *
256    
257     LinuxSamplerVst::LinuxSamplerVst(audioMasterCallback audioMaster) :
258     AudioEffectX(audioMaster, NbPrograms, 0),
259     StateBuf(0)
260     {
261     dmsg(2, ("-->constructor\n"));
262    
263     Programs = new LinuxSamplerVstProgram[NbPrograms];
264     setProgram(0);
265     setNumInputs(0);
266 persson 1908 setNumOutputs(CHANNELS);
267 persson 1777 canProcessReplacing();
268     isSynth();
269     programsAreChunks();
270     setUniqueID(CCONST('L', 'S', 'm', 'p')); // (registred at Steinberg)
271     setEditor(new LinuxSamplerEditor(this));
272    
273     suspend();
274     dmsg(2, ("<--constructor\n"));
275     }
276    
277    
278     void LinuxSamplerVst::resume() {
279     dmsg(2, ("-->resume\n"));
280    
281 persson 2347 // Ardour initially sets blockSize to zero - we postpone the
282     // initialization until we have a blockSize
283     if (blockSize != 0) {
284     if (!pAudioDevice) {
285     Init(int(sampleRate), blockSize, CHANNELS);
286    
287     if (!SavedChunk.empty()) {
288     SetState(SavedChunk);
289     SavedChunk.clear();
290     } else {
291     InitState();
292     }
293 persson 1777 } else {
294 persson 2347 Init(int(sampleRate), blockSize, CHANNELS);
295 persson 1777 }
296     }
297 senoner 1942 AudioEffectX::resume();
298 persson 1777 dmsg(2, ("<--resume\n"));
299     }
300    
301     LinuxSamplerVst::~LinuxSamplerVst() {
302     dmsg(2, ("-->destructor\n"));
303     delete[] Programs;
304     if (StateBuf) free(StateBuf);
305     dmsg(2, ("<--destructor\n"));
306     }
307    
308    
309     void LinuxSamplerVst::setProgram(VstInt32 program) {
310     if (program < 0 || program >= NbPrograms) return;
311    
312     curProgram = program;
313     }
314    
315    
316     void LinuxSamplerVst::setProgramName(char* name) {
317     vst_strncpy(Programs[curProgram].name, name, kVstMaxProgNameLen);
318     }
319    
320    
321     void LinuxSamplerVst::getProgramName(char* name) {
322     vst_strncpy(name, Programs[curProgram].name, kVstMaxProgNameLen);
323     }
324    
325    
326     bool LinuxSamplerVst::getOutputProperties(VstInt32 index,
327     VstPinProperties* properties) {
328 persson 1908 if (index < CHANNELS) {
329 persson 1777 sprintf(properties->label, "LS %d", index + 1);
330     properties->flags = kVstPinIsActive | kVstPinIsStereo;
331     return true;
332     }
333     return false;
334     }
335    
336    
337     bool LinuxSamplerVst::getProgramNameIndexed(VstInt32 category, VstInt32 index,
338     char* text) {
339     if (index < NbPrograms) {
340     vst_strncpy(text, Programs[index].name, kVstMaxProgNameLen);
341     return true;
342     }
343     return false;
344     }
345    
346    
347     bool LinuxSamplerVst::getEffectName(char* name) {
348     vst_strncpy(name, "LinuxSampler", kVstMaxEffectNameLen);
349     return true;
350     }
351    
352    
353     bool LinuxSamplerVst::getVendorString(char* text) {
354     vst_strncpy(text, "linuxsampler.org", kVstMaxVendorStrLen);
355     return true;
356     }
357    
358    
359     bool LinuxSamplerVst::getProductString(char* text) {
360     vst_strncpy(text, "LinuxSampler VST", kVstMaxProductStrLen);
361     return true;
362     }
363    
364    
365     VstInt32 LinuxSamplerVst::getVendorVersion() {
366     return 1000;
367     }
368    
369    
370     VstInt32 LinuxSamplerVst::canDo(char* text) {
371     dmsg(2, ("canDo %s\n", text));
372     if (strcmp(text, "receiveVstEvents") == 0 ||
373     strcmp(text, "receiveVstMidiEvent") == 0) return 1;
374     return -1;
375     }
376    
377     void LinuxSamplerVst::setSampleRate(float sampleRate) {
378     dmsg(2, ("linuxsampler: setSampleRate %f\n", sampleRate));
379     AudioEffectX::setSampleRate(sampleRate);
380     }
381    
382     void LinuxSamplerVst::setBlockSize(VstInt32 blockSize) {
383     dmsg(2, ("linuxsampler: setBlockSize %d\n", blockSize));
384     AudioEffectX::setBlockSize(blockSize);
385     }
386    
387    
388     void LinuxSamplerVst::processReplacing(float** inputs, float** outputs,
389     VstInt32 sampleFrames) {
390     if (pAudioDevice) {
391 persson 1908 for (int i = 0 ; i < CHANNELS ; i++) {
392     pAudioDevice->Channel(i)->SetBuffer(outputs[i]);
393     }
394 persson 1777 pAudioDevice->Render(sampleFrames);
395     } else {
396 persson 1908 for (int i = 0 ; i < CHANNELS ; i++) {
397     memset(outputs[i], 0, sampleFrames * sizeof(float));
398     }
399 persson 1777 }
400     }
401    
402    
403     VstInt32 LinuxSamplerVst::processEvents(VstEvents* ev) {
404     if (pMidiDevice) {
405     for (int i = 0; i < ev->numEvents; i++)
406     {
407     if (ev->events[i]->type == kVstMidiType) {
408     VstMidiEvent* event = reinterpret_cast<VstMidiEvent*>(ev->events[i]);
409     pMidiDevice->Port()->DispatchRaw(reinterpret_cast<uint8_t*>(event->midiData),
410     event->deltaFrames);
411     }
412     }
413     }
414     return 1;
415     }
416    
417    
418     VstInt32 LinuxSamplerVst::getChunk(void** data, bool isPreset) {
419     dmsg(2, ("-->getChunk\n"));
420    
421     String state = GetState();
422     dmsg(2, ("state=\"%s\"\n", state.c_str()));;
423     if (StateBuf) free(StateBuf);
424     StateBuf = strdup(state.c_str());
425     *data = StateBuf;
426     dmsg(2, ("<--getChunk\n"));
427     return state.length() + 1;
428     }
429    
430    
431     VstInt32 LinuxSamplerVst::setChunk(void* data, VstInt32 byteSize,
432     bool isPreset) {
433     dmsg(2, ("-->setChunk byteSize=%d isPreset=%d\n", byteSize, isPreset));
434    
435     const char* state = static_cast<const char*>(data);
436     VstInt32 res = 0;
437     if (byteSize > 0 && state[byteSize - 1] == '\0') {
438     dmsg(2, ("state=\"%s\"\n", state));
439     if (pAudioDevice) {
440     res = SetState(state);
441     } else {
442     SavedChunk = state;
443     }
444     }
445     dmsg(2, ("<--setChunk\n"));
446     return res;
447     }
448     }
449    
450    
451     // *************** VST main function ***************
452     // *
453    
454     AudioEffect* createEffectInstance(audioMasterCallback audioMaster)
455     {
456     return new LinuxSamplerVst(audioMaster);
457     }

  ViewVC Help
Powered by ViewVC