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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1888 - (hide annotations) (download)
Sat Apr 18 09:26:45 2009 UTC (14 years, 11 months ago) by persson
File size: 17317 byte(s)
* applied old fixes from Benno to the ASIO driver that were included
  in the previous binary release but accidentally never committed to
  CVS (fixes #117)

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

  ViewVC Help
Powered by ViewVC