/[svn]/linuxsampler/trunk/src/linuxsampler.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/linuxsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1895 - (hide annotations) (download)
Sun May 3 12:15:40 2009 UTC (14 years, 11 months ago) by persson
File size: 17397 byte(s)
* fixes for using large audio device buffers
* VST: added support for sample rate and buffer size changes
* VST: close editor (Fantasia) when the VST is removed
* minor fix in configure for mmsystem.h detection on MinGW
* removed warnings from gcc 4.4 and valgrind

1 schoenebeck 9 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 persson 1644 * Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 1830 * Copyright (C) 2005-2009 Christian Schoenebeck *
7 schoenebeck 9 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #include <getopt.h>
25     #include <signal.h>
26    
27 senoner 1502 #if defined(WIN32)
28     // require at least Windows 2000 for the GlobalMemoryStatusEx() call
29 persson 1895 #if _WIN32_WINNT < 0x0500
30     #ifdef _WIN32_WINNT
31     #undef _WIN32_WINNT
32     #endif
33 senoner 1502 #define _WIN32_WINNT 0x0500
34     #endif
35 persson 1895 #endif
36 senoner 1502
37 schoenebeck 53 #include "Sampler.h"
38 schoenebeck 1424 #include "common/global_private.h"
39 schoenebeck 420 #include "engines/EngineFactory.h"
40 schoenebeck 1375 #include "plugins/InstrumentEditorFactory.h"
41 schoenebeck 207 #include "drivers/midi/MidiInputDeviceFactory.h"
42 schoenebeck 203 #include "drivers/audio/AudioOutputDeviceFactory.h"
43 senkov 325 #include "engines/gig/Profiler.h"
44 schoenebeck 35 #include "network/lscpserver.h"
45 schoenebeck 271 #include "common/stacktrace.h"
46 schoenebeck 319 #include "common/Features.h"
47 persson 1534 #include "common/atomic.h"
48 schoenebeck 9
49 schoenebeck 53 using namespace LinuxSampler;
50    
51 schoenebeck 211 Sampler* pSampler = NULL;
52     LSCPServer* pLSCPServer = NULL;
53 senoner 1502 #if defined(WIN32)
54     // inet_aton seems missing under WIN32
55     #ifndef INADDR_NONE
56     #define INADDR_NONE 0xffffffff
57     #endif
58    
59     int inet_aton(const char *cp, struct in_addr *addr)
60     {
61     addr->s_addr = inet_addr(cp);
62     return (addr->s_addr == INADDR_NONE) ? 0 : 1;
63     }
64    
65     #else
66 wylder 816 pid_t main_pid;
67 senoner 1502 #endif
68 schoenebeck 420 bool bPrintStatistics = false;
69 senkov 325 bool profile = false;
70     bool tune = true;
71 schoenebeck 1830 static bool bShowStackTrace = false;
72 senkov 667 unsigned long int lscp_addr;
73     unsigned short int lscp_port;
74 schoenebeck 9
75     void parse_options(int argc, char **argv);
76     void signal_handler(int signal);
77 schoenebeck 271 void kill_app();
78 persson 1534 static atomic_t running = ATOMIC_INIT(1);
79 schoenebeck 9
80     int main(int argc, char **argv) {
81    
82 schoenebeck 1830 lscp_addr = htonl(LSCP_ADDR);
83     lscp_port = htons(LSCP_PORT);
84    
85     #if !defined(WIN32)
86     main_pid = getpid();
87     #endif
88    
89     // parse and assign command line options
90     parse_options(argc, argv);
91    
92     // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
93     signal(SIGINT, signal_handler);
94    
95 schoenebeck 271 // initialize the stack trace mechanism with our binary file
96 schoenebeck 1830 // (if requested by command line option)
97     if (bShowStackTrace) {
98     #if defined(WIN32)
99     // FIXME: sigaction() not supported on WIN32, we ignore it for now
100     #else
101     StackTraceInit(argv[0], -1);
102     // register signal handler for all unusual signals
103     // (we will print the stack trace and exit)
104     struct sigaction sact;
105     sigemptyset(&sact.sa_mask);
106     sact.sa_flags = 0;
107     sact.sa_handler = signal_handler;
108     sigaction(SIGSEGV, &sact, NULL);
109     sigaction(SIGBUS, &sact, NULL);
110     sigaction(SIGILL, &sact, NULL);
111     sigaction(SIGFPE, &sact, NULL);
112     sigaction(SIGUSR1, &sact, NULL);
113     sigaction(SIGUSR2, &sact, NULL);
114     #endif
115     }
116 schoenebeck 271
117 schoenebeck 1830 dmsg(1,("LinuxSampler %s\n", VERSION));
118     dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));
119     dmsg(1,("Copyright (C) 2005-2009 Christian Schoenebeck\n"));
120    
121 senoner 1502 #if defined(WIN32)
122 persson 1888 #if 0
123 senoner 1502 // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes)
124     SYSTEM_INFO siSysInfo;
125     long physical_memory;
126     GetSystemInfo(&siSysInfo);
127 persson 1888 dmsg(2,("page size=%d\n", siSysInfo.dwPageSize));
128 senoner 1502
129     MEMORYSTATUSEX statex;
130     statex.dwLength = sizeof (statex);
131     GlobalMemoryStatusEx (&statex);
132 persson 1888 dmsg(2, ("There are %*I64d total Kbytes of physical memory.\n",
133 senoner 1502 8, statex.ullTotalPhys));
134 persson 1888 dmsg(2, ("There are %*I64d free Kbytes of physical memory.\n",
135 senoner 1502 8, statex.ullAvailPhys));
136     physical_memory = statex.ullTotalPhys;
137    
138     HANDLE hProcess = GetCurrentProcess();
139    
140     unsigned long MinimumWorkingSetSize, MaximumWorkingSetSize;
141     unsigned long DefaultMinimumWorkingSetSize, DefaultMaximumWorkingSetSize;
142     unsigned long RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize;
143     int res;
144    
145     res = GetProcessWorkingSetSize(hProcess, &DefaultMinimumWorkingSetSize, &DefaultMaximumWorkingSetSize);
146    
147     RequestedMaximumWorkingSetSize = physical_memory - 2*1024*1024;
148     RequestedMinimumWorkingSetSize = RequestedMaximumWorkingSetSize;
149    
150     for(;;) {
151     dmsg(2,("TRYING VALUES RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
152     res = SetProcessWorkingSetSize(hProcess, RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize);
153     dmsg(2,("AFTER SET: res = %d RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", res,RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
154    
155     res = GetProcessWorkingSetSize(hProcess, &MinimumWorkingSetSize, &MaximumWorkingSetSize);
156     dmsg(2,("AFTER GET: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
157    
158     if( RequestedMinimumWorkingSetSize == MinimumWorkingSetSize ) {
159     dmsg(2,("RequestedMinimumWorkingSetSize == MinimumWorkingSetSize. OK !\n"));
160     break;
161     }
162    
163     RequestedMinimumWorkingSetSize -= 10*1024*1024;
164     if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break;
165     }
166    
167 persson 1888 dmsg(2,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
168     #endif
169 schoenebeck 1830 #endif // WIN32
170 senoner 1502
171 schoenebeck 579 if (tune) {
172 persson 425 // detect and print system / CPU specific features
173     Features::detect();
174 schoenebeck 579 dmsg(1,("Detected features: %s\n", Features::featuresAsString().c_str()));
175     // prevent slow denormal FPU modes
176     Features::enableDenormalsAreZeroMode();
177 senkov 325 }
178 schoenebeck 319
179 schoenebeck 1830 dmsg(1,("Automatic Stacktrace: %s\n", (bShowStackTrace) ? "On" : "Off"));
180    
181 schoenebeck 53 // create LinuxSampler instance
182 schoenebeck 123 dmsg(1,("Creating Sampler..."));
183 schoenebeck 53 pSampler = new Sampler;
184 schoenebeck 123 dmsg(1,("OK\n"));
185 schoenebeck 53
186 schoenebeck 900 dmsg(1,("Registered sampler engines: %s\n", EngineFactory::AvailableEngineTypesAsString().c_str()));
187 schoenebeck 207 dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
188 schoenebeck 123 dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
189 schoenebeck 1212 dmsg(1,("Registered instrument editors: %s\n", InstrumentEditorFactory::AvailableEditorsAsString().c_str()));
190 schoenebeck 123
191 schoenebeck 53 // start LSCP network server
192 senkov 667 struct in_addr addr;
193     addr.s_addr = lscp_addr;
194     dmsg(1,("Starting LSCP network server (%s:%d)...", inet_ntoa(addr), ntohs(lscp_port)));
195     pLSCPServer = new LSCPServer(pSampler, lscp_addr, lscp_port);
196 schoenebeck 53 pLSCPServer->StartThread();
197 schoenebeck 211 pLSCPServer->WaitUntilInitialized();
198 schoenebeck 12 dmsg(1,("OK\n"));
199 schoenebeck 9
200 senkov 325 if (profile)
201     {
202 persson 425 dmsg(1,("Calibrating profiler..."));
203 iliev 1161 LinuxSampler::gig::Profiler::Calibrate();
204     LinuxSampler::gig::Profiler::Reset();
205     LinuxSampler::gig::Profiler::enable();
206 persson 425 dmsg(1,("OK\n"));
207 senkov 325 }
208    
209 schoenebeck 563 printf("LinuxSampler initialization completed. :-)\n\n");
210 schoenebeck 9
211 persson 1534 while (atomic_read(&running)) {
212 schoenebeck 420 if (bPrintStatistics) {
213 persson 840 const std::set<Engine*>& engines = EngineFactory::EngineInstances();
214 schoenebeck 420 std::set<Engine*>::iterator itEngine = engines.begin();
215     for (int i = 0; itEngine != engines.end(); itEngine++, i++) {
216     Engine* pEngine = *itEngine;
217     printf("Engine %d) Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d)\n", i,
218     pEngine->VoiceCount(), pEngine->VoiceCountMax(),
219     pEngine->DiskStreamCount(), pEngine->DiskStreamCountMax()
220     );
221     fflush(stdout);
222     }
223     }
224 persson 425
225 persson 1534 sleep(1);
226     if (profile)
227     {
228     unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
229     unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);
230     if (bv != 0)
231     {
232     printf(" BogoVoices: %i \r", bv);
233     fflush(stdout);
234     }
235     }
236 persson 425
237 persson 1765 pSampler->fireStatistics();
238 senoner 10 }
239 persson 1534 if (pLSCPServer) pLSCPServer->StopThread();
240     // the delete order here is important: the Sampler
241     // destructor sends notifications to the lscpserver
242     if (pSampler) delete pSampler;
243     if (pLSCPServer) delete pLSCPServer;
244     printf("LinuxSampler stopped due to SIGINT.\n");
245 schoenebeck 9 return EXIT_SUCCESS;
246     }
247    
248 schoenebeck 271 void signal_handler(int iSignal) {
249     switch (iSignal) {
250 persson 1534 case SIGINT:
251     atomic_set(&running, 0);
252 schoenebeck 271 return;
253 senoner 1502 #if defined(WIN32)
254     // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call
255     #else
256 schoenebeck 271 case SIGSEGV:
257     std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
258     break;
259     case SIGBUS:
260     std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush;
261     break;
262     case SIGILL:
263     std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush;
264     break;
265     case SIGFPE:
266     std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush;
267     break;
268     case SIGUSR1:
269     std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush;
270     break;
271     case SIGUSR2:
272     std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
273     break;
274 senoner 1502 #endif
275 schoenebeck 271 default: { // this should never happen, as we register for the signals we want
276     std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
277     break;
278     }
279 schoenebeck 9 }
280 schoenebeck 271 signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
281 schoenebeck 1830 if (bShowStackTrace) {
282     std::cerr << "Showing stack trace...\n" << std::flush;
283     StackTrace();
284     sleep(2);
285     }
286 schoenebeck 271 std::cerr << "Killing LinuxSampler...\n" << std::flush;
287     kill_app(); // Use abort() if we want to generate a core dump.
288 schoenebeck 9 }
289    
290 schoenebeck 271 void kill_app() {
291 senoner 1502 #if defined(WIN32)
292     // FIXME: do we need to do anything at this point under WIN32 ? is exit(0) ok ?
293     exit(0);
294     #else
295 wylder 816 kill(main_pid, SIGKILL);
296 senoner 1502 #endif
297 schoenebeck 271 }
298    
299 senkov 325 void parse_options(int argc, char **argv) {
300 schoenebeck 9 int res;
301     int option_index = 0;
302     static struct option long_options[] =
303     {
304     {"help",0,0,0},
305 senkov 325 {"version",0,0,0},
306     {"profile",0,0,0},
307     {"no-tune",0,0,0},
308 schoenebeck 420 {"statistics",0,0,0},
309 iliev 1208 {"instruments-db-location",1,0,0},
310 iliev 1187 {"create-instruments-db",1,0,0},
311 senkov 667 {"lscp-addr",1,0,0},
312     {"lscp-port",1,0,0},
313 schoenebeck 1830 {"stacktrace",0,0,0},
314 schoenebeck 9 {0,0,0,0}
315     };
316    
317     while (true) {
318 schoenebeck 361 /*
319     Stephane Letz : letz@grame.fr
320     getopt_long_only does not exist on OSX : replaced by getopt_long for now.
321     */
322     res = getopt_long(argc, argv, "", long_options, &option_index);
323 schoenebeck 9 if(res == -1) break;
324     if (res == 0) {
325     switch(option_index) {
326 senkov 325 case 0: // --help
327     printf("usage: linuxsampler [OPTIONS]\n\n");
328 iliev 1208 printf("--help prints this message\n");
329     printf("--version prints version information\n");
330     printf("--profile profile synthesis algorithms\n");
331     printf("--no-tune disable assembly optimization\n");
332     printf("--statistics periodically prints statistics\n");
333     printf("--lscp-addr set LSCP address (default: any)\n");
334     printf("--lscp-port set LSCP port (default: 8888)\n");
335     printf("--create-instruments-db creates an instruments DB\n");
336     printf("--instruments-db-location specifies the instruments DB file\n");
337 schoenebeck 1830 printf("--stacktrace automatically shows stacktrace if crashes\n");
338     printf(" (broken on most systems at the moment)\n");
339 senkov 325 exit(EXIT_SUCCESS);
340 schoenebeck 9 break;
341 senkov 325 case 1: // --version
342 schoenebeck 328 printf("LinuxSampler %s\n", VERSION);
343     exit(EXIT_SUCCESS);
344 schoenebeck 9 break;
345 senkov 325 case 2: // --profile
346 persson 425 profile = true;
347 schoenebeck 20 break;
348 senkov 325 case 3: // --no-tune
349 persson 425 tune = false;
350 schoenebeck 9 break;
351 schoenebeck 420 case 4: // --statistics
352     bPrintStatistics = true;
353     break;
354 iliev 1208 case 5: // --instruments-db-location
355 iliev 1187 #if HAVE_SQLITE3
356     try {
357     if (optarg) {
358 iliev 1208 struct stat statBuf;
359     int res = stat(optarg, &statBuf);
360    
361     if (res) {
362     std::stringstream ss;
363 schoenebeck 1364 ss << "Failed to stat `" << optarg << "`: " << strerror(errno);
364 iliev 1208 throw Exception(ss.str());
365     }
366    
367     if (!S_ISREG(statBuf.st_mode)) {
368     std::stringstream ss;
369     ss << "`" << optarg << "` is not a regular file";
370     throw Exception(ss.str());
371     }
372    
373     InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg));
374     }
375     } catch(Exception e) {
376 schoenebeck 1364 std::cerr << "Could not open instruments DB file: "
377     << e.Message() << std::endl;
378     exit(EXIT_FAILURE);
379 iliev 1208 }
380 schoenebeck 1364 break;
381 iliev 1208 #else
382     std::cerr << "LinuxSampler was not build with ";
383 schoenebeck 1364 std::cerr << "instruments database support!\n";
384     exit(EXIT_FAILURE);
385     break;
386     #endif
387 iliev 1208 case 6: // --create-instruments-db
388     #if HAVE_SQLITE3
389     try {
390     if (optarg) {
391 iliev 1187 std::cout << "Creating instruments database..." << std::endl;
392     InstrumentsDb::CreateInstrumentsDb(String(optarg));
393     std::cout << "Done" << std::endl;
394     }
395     } catch(Exception e) {
396     std::cerr << e.Message() << std::endl;
397     exit(EXIT_FAILURE);
398     return;
399     }
400    
401     exit(EXIT_SUCCESS);
402     return;
403     #else
404     std::cerr << "Failed to create the database. LinuxSampler was ";
405 schoenebeck 1364 std::cerr << "not build with instruments database support!\n";
406 iliev 1187 exit(EXIT_FAILURE);
407     return;
408 schoenebeck 1364 #endif
409 schoenebeck 1830 case 7: { // --lscp-addr
410 iliev 1187 struct in_addr addr;
411     if (inet_aton(optarg, &addr) == 0)
412     printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");
413     else
414     lscp_addr = addr.s_addr;
415 senkov 667 break;
416 schoenebeck 1830 }
417     case 8: {// --lscp-port
418 iliev 1187 long unsigned int port = 0;
419     if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))
420     printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");
421     else
422     lscp_port = htons(port);
423 senkov 667 break;
424 schoenebeck 1830 }
425     case 9: // --stacktrace
426     bShowStackTrace = true;
427     break;
428 schoenebeck 9 }
429     }
430     }
431 senkov 325 }

  ViewVC Help
Powered by ViewVC