/[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 1942 - (hide annotations) (download)
Tue Jul 14 18:20:41 2009 UTC (14 years, 9 months ago) by senoner
File size: 10959 byte(s)
* bugfix: LinuxSamplerVst::resume() did not call AudioEffectX::resume().
This caused LinuxSampler VST not appearing in the out: dropdown menu
under Cubase. Thanks to Arne Scheffler for the tip!

1 persson 1777 /***************************************************************************
2     * *
3 persson 1908 * Copyright (C) 2008 - 2009 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     #endif
28    
29     #include "PluginVst.h"
30    
31 persson 1908 #ifndef CHANNELS
32 persson 1917 #define CHANNELS 32
33 persson 1908 #endif
34    
35 persson 1777 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 persson 1895 #ifdef WIN32
74     ProcessHandle = INVALID_HANDLE_VALUE;
75     #endif
76 persson 1777 }
77    
78 persson 1895 LinuxSamplerEditor::~LinuxSamplerEditor() {
79     close();
80     }
81    
82 persson 1777 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 persson 1908 // 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 persson 1777
100 schoenebeck 1928 // assume Fantasia is in the same directory or one directory above
101     // the liblinuxsampler dll
102 persson 1897 String lspath = LinuxSampler::Sampler::GetInstallDir();
103     if (!lspath.empty()) {
104     lspath += "\\";
105 persson 1777 WIN32_FIND_DATA fd;
106     HANDLE hFind = FindFirstFile((lspath + "Fantasia*.jar").c_str(), &fd);
107 schoenebeck 1928 if (hFind == INVALID_HANDLE_VALUE) {
108     lspath += "..\\";
109     hFind = FindFirstFile((lspath + "Fantasia*.jar").c_str(), &fd);
110     }
111 persson 1777 if (hFind != INVALID_HANDLE_VALUE) {
112     String fantasia(fd.cFileName);
113     FindClose(hFind);
114    
115     // start a java process
116     STARTUPINFO si;
117     PROCESS_INFORMATION pi;
118     ZeroMemory(&si, sizeof(si));
119     si.cb = sizeof(si);
120     ZeroMemory(&pi, sizeof(pi));
121    
122 senoner 1920 Command = _tcsdup(TEXT((String("javaw -jar \"") + lspath + fantasia + "\"").c_str()));
123 persson 1777 CreateProcess(NULL, Command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
124     ProcessHandle = pi.hProcess;
125 persson 1908 CloseHandle(pi.hThread);
126 persson 1777 }
127     }
128     #endif
129     dmsg(2, ("<--LinuxSamplerEditor::open\n"));
130     return true;
131     }
132    
133     void LinuxSamplerEditor::close() {
134     systemWindow = 0;
135     #ifdef WIN32
136     if (ProcessHandle != INVALID_HANDLE_VALUE) {
137     TerminateProcess(ProcessHandle, 0);
138     free(Command);
139 persson 1908 CloseHandle(ProcessHandle);
140 persson 1895 ProcessHandle = INVALID_HANDLE_VALUE;
141 persson 1777 }
142     #endif
143     }
144    
145     bool LinuxSamplerEditor::getRect(ERect** rect) {
146     ERect* r = new ERect();
147     r->top = 0;
148     r->left = 0;
149     r->bottom = 0;
150     r->right = 400;
151     *rect = r;
152     return true;
153     }
154    
155    
156     // *************** LinuxSamplerVst ***************
157     // *
158    
159     LinuxSamplerVst::LinuxSamplerVst(audioMasterCallback audioMaster) :
160     AudioEffectX(audioMaster, NbPrograms, 0),
161     StateBuf(0)
162     {
163     dmsg(2, ("-->constructor\n"));
164    
165     Programs = new LinuxSamplerVstProgram[NbPrograms];
166     setProgram(0);
167     setNumInputs(0);
168 persson 1908 setNumOutputs(CHANNELS);
169 persson 1777 canProcessReplacing();
170     isSynth();
171     programsAreChunks();
172     setUniqueID(CCONST('L', 'S', 'm', 'p')); // (registred at Steinberg)
173     setEditor(new LinuxSamplerEditor(this));
174    
175     suspend();
176     dmsg(2, ("<--constructor\n"));
177     }
178    
179    
180     void LinuxSamplerVst::resume() {
181     dmsg(2, ("-->resume\n"));
182     if (!pAudioDevice) {
183 persson 1908 Init(int(sampleRate), blockSize, CHANNELS);
184 persson 1777
185 persson 1895 if (!SavedChunk.empty()) {
186 persson 1777 SetState(SavedChunk);
187 persson 1895 SavedChunk.clear();
188 persson 1777 } else {
189     InitState();
190     }
191 persson 1895 } else {
192     Init(int(sampleRate), blockSize);
193 persson 1777 }
194 senoner 1942 AudioEffectX::resume();
195 persson 1777 dmsg(2, ("<--resume\n"));
196     }
197    
198     LinuxSamplerVst::~LinuxSamplerVst() {
199     dmsg(2, ("-->destructor\n"));
200     delete[] Programs;
201     if (StateBuf) free(StateBuf);
202     dmsg(2, ("<--destructor\n"));
203     }
204    
205    
206     void LinuxSamplerVst::setProgram(VstInt32 program) {
207     if (program < 0 || program >= NbPrograms) return;
208    
209     curProgram = program;
210     }
211    
212    
213     void LinuxSamplerVst::setProgramName(char* name) {
214     vst_strncpy(Programs[curProgram].name, name, kVstMaxProgNameLen);
215     }
216    
217    
218     void LinuxSamplerVst::getProgramName(char* name) {
219     vst_strncpy(name, Programs[curProgram].name, kVstMaxProgNameLen);
220     }
221    
222    
223     bool LinuxSamplerVst::getOutputProperties(VstInt32 index,
224     VstPinProperties* properties) {
225 persson 1908 if (index < CHANNELS) {
226 persson 1777 sprintf(properties->label, "LS %d", index + 1);
227     properties->flags = kVstPinIsActive | kVstPinIsStereo;
228     return true;
229     }
230     return false;
231     }
232    
233    
234     bool LinuxSamplerVst::getProgramNameIndexed(VstInt32 category, VstInt32 index,
235     char* text) {
236     if (index < NbPrograms) {
237     vst_strncpy(text, Programs[index].name, kVstMaxProgNameLen);
238     return true;
239     }
240     return false;
241     }
242    
243    
244     bool LinuxSamplerVst::getEffectName(char* name) {
245     vst_strncpy(name, "LinuxSampler", kVstMaxEffectNameLen);
246     return true;
247     }
248    
249    
250     bool LinuxSamplerVst::getVendorString(char* text) {
251     vst_strncpy(text, "linuxsampler.org", kVstMaxVendorStrLen);
252     return true;
253     }
254    
255    
256     bool LinuxSamplerVst::getProductString(char* text) {
257     vst_strncpy(text, "LinuxSampler VST", kVstMaxProductStrLen);
258     return true;
259     }
260    
261    
262     VstInt32 LinuxSamplerVst::getVendorVersion() {
263     return 1000;
264     }
265    
266    
267     VstInt32 LinuxSamplerVst::canDo(char* text) {
268     dmsg(2, ("canDo %s\n", text));
269     if (strcmp(text, "receiveVstEvents") == 0 ||
270     strcmp(text, "receiveVstMidiEvent") == 0) return 1;
271     return -1;
272     }
273    
274     void LinuxSamplerVst::setSampleRate(float sampleRate) {
275     dmsg(2, ("linuxsampler: setSampleRate %f\n", sampleRate));
276     AudioEffectX::setSampleRate(sampleRate);
277     }
278    
279     void LinuxSamplerVst::setBlockSize(VstInt32 blockSize) {
280     dmsg(2, ("linuxsampler: setBlockSize %d\n", blockSize));
281     AudioEffectX::setBlockSize(blockSize);
282     }
283    
284    
285     void LinuxSamplerVst::processReplacing(float** inputs, float** outputs,
286     VstInt32 sampleFrames) {
287     if (pAudioDevice) {
288 persson 1908 for (int i = 0 ; i < CHANNELS ; i++) {
289     pAudioDevice->Channel(i)->SetBuffer(outputs[i]);
290     }
291 persson 1777 pAudioDevice->Render(sampleFrames);
292     } else {
293 persson 1908 for (int i = 0 ; i < CHANNELS ; i++) {
294     memset(outputs[i], 0, sampleFrames * sizeof(float));
295     }
296 persson 1777 }
297     }
298    
299    
300     VstInt32 LinuxSamplerVst::processEvents(VstEvents* ev) {
301     if (pMidiDevice) {
302     for (int i = 0; i < ev->numEvents; i++)
303     {
304     if (ev->events[i]->type == kVstMidiType) {
305     VstMidiEvent* event = reinterpret_cast<VstMidiEvent*>(ev->events[i]);
306     pMidiDevice->Port()->DispatchRaw(reinterpret_cast<uint8_t*>(event->midiData),
307     event->deltaFrames);
308     }
309     }
310     }
311     return 1;
312     }
313    
314    
315     VstInt32 LinuxSamplerVst::getChunk(void** data, bool isPreset) {
316     dmsg(2, ("-->getChunk\n"));
317    
318     String state = GetState();
319     dmsg(2, ("state=\"%s\"\n", state.c_str()));;
320     if (StateBuf) free(StateBuf);
321     StateBuf = strdup(state.c_str());
322     *data = StateBuf;
323     dmsg(2, ("<--getChunk\n"));
324     return state.length() + 1;
325     }
326    
327    
328     VstInt32 LinuxSamplerVst::setChunk(void* data, VstInt32 byteSize,
329     bool isPreset) {
330     dmsg(2, ("-->setChunk byteSize=%d isPreset=%d\n", byteSize, isPreset));
331    
332     const char* state = static_cast<const char*>(data);
333     VstInt32 res = 0;
334     if (byteSize > 0 && state[byteSize - 1] == '\0') {
335     dmsg(2, ("state=\"%s\"\n", state));
336     if (pAudioDevice) {
337     res = SetState(state);
338     } else {
339     SavedChunk = state;
340     }
341     }
342     dmsg(2, ("<--setChunk\n"));
343     return res;
344     }
345     }
346    
347    
348     // *************** VST main function ***************
349     // *
350    
351     AudioEffect* createEffectInstance(audioMasterCallback audioMaster)
352     {
353     return new LinuxSamplerVst(audioMaster);
354     }

  ViewVC Help
Powered by ViewVC