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