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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1895 by persson, Sun May 3 12:15:40 2009 UTC revision 2347 by persson, Sun Jun 10 15:47:32 2012 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2008 Andreas Persson                                    *   *   Copyright (C) 2008 - 2012 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  *
# Line 24  Line 24 
24    
25  #ifdef WIN32  #ifdef WIN32
26  #include <tchar.h>  #include <tchar.h>
27    #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  #endif  #endif
35    
36  #include "PluginVst.h"  #include "PluginVst.h"
37    
38    #ifndef CHANNELS
39    #define CHANNELS 32
40    #endif
41    
42  namespace {  namespace {
43    
44  #if defined(WIN32) && CONFIG_DEBUG_LEVEL >= 2  #if defined(WIN32) && CONFIG_DEBUG_LEVEL >= 2
# Line 39  namespace { Line 50  namespace {
50    
51      void log(const char* fmt, ...) {      void log(const char* fmt, ...) {
52          logmutex.Lock();          logmutex.Lock();
53          FILE* f = fopen("C:\\linuxsamplervst.log", "a");          FILE* f = fopen((String(getenv("TEMP")) + "\\linuxsamplervst.log").c_str(), "a");
54          va_list ap;          va_list ap;
55          va_start(ap, fmt);          va_start(ap, fmt);
56          vfprintf(f, fmt, ap);          vfprintf(f, fmt, ap);
# Line 68  namespace { Line 79  namespace {
79          dmsg(2, ("-->LinuxSamplerEditor constructor\n"));          dmsg(2, ("-->LinuxSamplerEditor constructor\n"));
80  #ifdef WIN32  #ifdef WIN32
81          ProcessHandle = INVALID_HANDLE_VALUE;          ProcessHandle = INVALID_HANDLE_VALUE;
82    #else
83            pid = 0;
84  #endif  #endif
85      }      }
86    
# Line 75  namespace { Line 88  namespace {
88          close();          close();
89      }      }
90    
91        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      bool LinuxSamplerEditor::open(void* ptr) {      bool LinuxSamplerEditor::open(void* ptr) {
142          dmsg(2, ("-->LinuxSamplerEditor::open\n"));          dmsg(2, ("-->LinuxSamplerEditor::open\n"));
143          AEffEditor::open(ptr);          AEffEditor::open(ptr);
144    
145            // try to start the JSampler Fantasia GUI as a separate process
146  #ifdef WIN32  #ifdef WIN32
147          // try to start the JSample Fantasia GUI as a separate process          // first check if it's already running
148            if (ProcessHandle != INVALID_HANDLE_VALUE) {
149          ProcessHandle = INVALID_HANDLE_VALUE;              DWORD exitCode;
150                if (GetExitCodeProcess(ProcessHandle, &exitCode)) {
151          // look for a Fantasia jar file in %programfiles%\LinuxSampler                  if (exitCode == STILL_ACTIVE) return true;
152          if (const char* programfiles = getenv("PROGRAMFILES")) {              }
153              String lspath = String(programfiles) + "\\LinuxSampler\\";              free(Command);
154              WIN32_FIND_DATA fd;              CloseHandle(ProcessHandle);
155              HANDLE hFind = FindFirstFile((lspath + "Fantasia*.jar").c_str(), &fd);              ProcessHandle = INVALID_HANDLE_VALUE;
156              if (hFind != INVALID_HANDLE_VALUE) {          }
                 String fantasia(fd.cFileName);  
                 FindClose(hFind);  
157    
158                  // start a java process          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;                  STARTUPINFO si;
164                  PROCESS_INFORMATION pi;                  PROCESS_INFORMATION pi;
165                  ZeroMemory(&si, sizeof(si));                  ZeroMemory(&si, sizeof(si));
166                  si.cb = sizeof(si);                  si.cb = sizeof(si);
167                  ZeroMemory(&pi, sizeof(pi));                  ZeroMemory(&pi, sizeof(pi));
168    
169                  Command = _tcsdup(TEXT((String("javaw -jar \"") + lspath + fantasia + "\" &").c_str()));                  Command = _tcsdup(TEXT((String("\"") + path +
170                  CreateProcess(NULL, Command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);                                          "javaw\" -jar \"" + fantasia +
171                  ProcessHandle = pi.hProcess;                                          "\"").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    #ifdef _WIN64
185                            // LS plugin is 64 bit - look for 32 bit java
186                            path = String(windir) + "\\SysWOW64\\";
187    #else
188                            // LS plugin is 32 bit - look for 64 bit java
189                            path = String(windir) + "\\Sysnative\\";
190    #endif
191                        }
192                    }
193                }
194            }
195    #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    #endif
213                    dmsg(1, ("exec failed %d %s\n", errno, strerror(errno)));
214    
215                    // make sure somthing is executed, so the static
216                    // destructors copied from the parent don't run
217                    execl("/usr/bin/true", "/usr/bin/true", (char*)0);
218              }              }
219          }          }
220  #endif  #endif
# Line 116  namespace { Line 228  namespace {
228          if (ProcessHandle != INVALID_HANDLE_VALUE) {          if (ProcessHandle != INVALID_HANDLE_VALUE) {
229              TerminateProcess(ProcessHandle, 0);              TerminateProcess(ProcessHandle, 0);
230              free(Command);              free(Command);
231                CloseHandle(ProcessHandle);
232              ProcessHandle = INVALID_HANDLE_VALUE;              ProcessHandle = INVALID_HANDLE_VALUE;
233          }          }
234    #else
235            if (pid) {
236                kill(pid, SIGTERM);
237                waitpid(pid, 0, 0);
238                pid = 0;
239            }
240  #endif  #endif
241      }      }
242    
# Line 125  namespace { Line 244  namespace {
244          ERect* r = new ERect();          ERect* r = new ERect();
245          r->top = 0;          r->top = 0;
246          r->left = 0;          r->left = 0;
247          r->bottom = 0;          r->bottom = 1; // 0 makes EnergyXT and Ardour on Linux crash
248          r->right = 400;          r->right = 400;
249          *rect = r;          *rect = r;
250          return true;          return true;
# Line 144  namespace { Line 263  namespace {
263          Programs = new LinuxSamplerVstProgram[NbPrograms];          Programs = new LinuxSamplerVstProgram[NbPrograms];
264          setProgram(0);          setProgram(0);
265          setNumInputs(0);          setNumInputs(0);
266          setNumOutputs(2);          setNumOutputs(CHANNELS);
267          canProcessReplacing();          canProcessReplacing();
268          isSynth();          isSynth();
269          programsAreChunks();          programsAreChunks();
# Line 158  namespace { Line 277  namespace {
277    
278      void LinuxSamplerVst::resume() {      void LinuxSamplerVst::resume() {
279          dmsg(2, ("-->resume\n"));          dmsg(2, ("-->resume\n"));
         if (!pAudioDevice) {  
             Init(int(sampleRate), blockSize);  
280    
281              if (!SavedChunk.empty()) {          // Ardour initially sets blockSize to zero - we postpone the
282                  SetState(SavedChunk);          // initialization until we have a blockSize
283                  SavedChunk.clear();          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              } else {              } else {
294                  InitState();                  Init(int(sampleRate), blockSize, CHANNELS);
295              }              }
         } else {  
             Init(int(sampleRate), blockSize);  
296          }          }
297            AudioEffectX::resume();
298          dmsg(2, ("<--resume\n"));          dmsg(2, ("<--resume\n"));
299      }      }
300    
# Line 200  namespace { Line 325  namespace {
325    
326      bool LinuxSamplerVst::getOutputProperties(VstInt32 index,      bool LinuxSamplerVst::getOutputProperties(VstInt32 index,
327                                                VstPinProperties* properties) {                                                VstPinProperties* properties) {
328          if (index < 2) {          if (index < CHANNELS) {
329              sprintf(properties->label, "LS %d", index + 1);              sprintf(properties->label, "LS %d", index + 1);
330              properties->flags = kVstPinIsActive | kVstPinIsStereo;              properties->flags = kVstPinIsActive | kVstPinIsStereo;
331              return true;              return true;
# Line 263  namespace { Line 388  namespace {
388      void LinuxSamplerVst::processReplacing(float** inputs, float** outputs,      void LinuxSamplerVst::processReplacing(float** inputs, float** outputs,
389                                             VstInt32 sampleFrames) {                                             VstInt32 sampleFrames) {
390          if (pAudioDevice) {          if (pAudioDevice) {
391              pAudioDevice->Channel(0)->SetBuffer(outputs[0]);              for (int i = 0 ; i < CHANNELS ; i++) {
392              pAudioDevice->Channel(1)->SetBuffer(outputs[1]);                  pAudioDevice->Channel(i)->SetBuffer(outputs[i]);
393                }
394              pAudioDevice->Render(sampleFrames);              pAudioDevice->Render(sampleFrames);
395          } else {          } else {
396              memset(outputs[0], 0, sampleFrames * sizeof(float));              for (int i = 0 ; i < CHANNELS ; i++) {
397              memset(outputs[1], 0, sampleFrames * sizeof(float));                  memset(outputs[i], 0, sampleFrames * sizeof(float));
398                }
399          }          }
400      }      }
401    

Legend:
Removed from v.1895  
changed lines
  Added in v.2347

  ViewVC Help
Powered by ViewVC