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

Diff of /linuxsampler/trunk/src/linuxsampler.cpp

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

revision 1765 by persson, Sat Sep 6 16:44:42 2008 UTC revision 3058 by schoenebeck, Fri Dec 16 13:08:00 2016 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck    *   *   Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck    *
6   *   Copyright (C) 2005-2008 Christian Schoenebeck                         *   *   Copyright (C) 2005-2016 Christian Schoenebeck                         *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 23  Line 23 
23    
24  #include <getopt.h>  #include <getopt.h>
25  #include <signal.h>  #include <signal.h>
26    #include <sys/stat.h>
27    
28  #if defined(WIN32)  #if defined(WIN32)
29  // require at least Windows 2000 for the GlobalMemoryStatusEx() call  // 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  #define _WIN32_WINNT 0x0500
35  #endif  #endif
36    #endif
37    
38  #include "Sampler.h"  #include "Sampler.h"
39  #include "common/global_private.h"  #include "common/global_private.h"
# Line 35  Line 41 
41  #include "plugins/InstrumentEditorFactory.h"  #include "plugins/InstrumentEditorFactory.h"
42  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
43  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
44    #include "effects/EffectFactory.h"
45  #include "engines/gig/Profiler.h"  #include "engines/gig/Profiler.h"
46  #include "network/lscpserver.h"  #include "network/lscpserver.h"
47  #include "common/stacktrace.h"  #include "common/stacktrace.h"
# Line 51  LSCPServer* pLSCPServer = NULL; Line 58  LSCPServer* pLSCPServer = NULL;
58  #define INADDR_NONE 0xffffffff  #define INADDR_NONE 0xffffffff
59  #endif  #endif
60    
61    typedef unsigned long in_addr_t;
62    
63  int inet_aton(const char *cp, struct in_addr *addr)  int inet_aton(const char *cp, struct in_addr *addr)
64  {  {
65      addr->s_addr = inet_addr(cp);      addr->s_addr = inet_addr(cp);
# Line 63  pid_t       main_pid; Line 72  pid_t       main_pid;
72  bool bPrintStatistics = false;  bool bPrintStatistics = false;
73  bool profile = false;  bool profile = false;
74  bool tune = true;  bool tune = true;
75    static bool bShowStackTrace = false;
76  unsigned long int lscp_addr;  unsigned long int lscp_addr;
77  unsigned short int lscp_port;  unsigned short int lscp_port;
78    String ExecAfterInit;
79    
80  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
81  void signal_handler(int signal);  void signal_handler(int signal);
# Line 73  static atomic_t running = ATOMIC_INIT(1) Line 84  static atomic_t running = ATOMIC_INIT(1)
84    
85  int main(int argc, char **argv) {  int main(int argc, char **argv) {
86    
87        lscp_addr = htonl(LSCP_ADDR);
88        lscp_port = htons(LSCP_PORT);
89    
90        #if !defined(WIN32)
91        main_pid = getpid();
92        #endif
93    
94        // parse and assign command line options
95        parse_options(argc, argv);
96    
97        // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
98        signal(SIGINT, signal_handler);
99    
100      // initialize the stack trace mechanism with our binary file      // initialize the stack trace mechanism with our binary file
101      StackTraceInit(argv[0], -1);      // (if requested by command line option)
102        if (bShowStackTrace) {
103            #if defined(WIN32)
104            // FIXME: sigaction() not supported on WIN32, we ignore it for now
105            #elif AC_APPLE_UNIVERSAL_BUILD
106            // not used for Xcode
107            #else
108            StackTraceInit(argv[0], -1);
109            // register signal handler for all unusual signals
110            // (we will print the stack trace and exit)
111            struct sigaction sact;
112            sigemptyset(&sact.sa_mask);
113            sact.sa_flags   = 0;
114            sact.sa_handler = signal_handler;
115            sigaction(SIGSEGV, &sact, NULL);
116            sigaction(SIGBUS,  &sact, NULL);
117            sigaction(SIGILL,  &sact, NULL);
118            sigaction(SIGFPE,  &sact, NULL);
119            sigaction(SIGUSR1, &sact, NULL);
120            sigaction(SIGUSR2, &sact, NULL);
121            #endif
122        }
123    
124        dmsg(1,("LinuxSampler %s\n", VERSION));
125        dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));
126        dmsg(1,("Copyright (C) 2005-2016 Christian Schoenebeck\n"));
127        dmsg(1,("Binary built: " __DATE__ "\n"))
128    
129      #if defined(WIN32)      #if defined(WIN32)
130        #if 0
131      // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes)      // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes)
132      SYSTEM_INFO siSysInfo;      SYSTEM_INFO siSysInfo;
133      long physical_memory;      long physical_memory;
134      GetSystemInfo(&siSysInfo);      GetSystemInfo(&siSysInfo);
135      dmsg(1,("page size=%d\n", siSysInfo.dwPageSize));      dmsg(2,("page size=%d\n", siSysInfo.dwPageSize));
136    
137      MEMORYSTATUSEX statex;      MEMORYSTATUSEX statex;
138          statex.dwLength = sizeof (statex);          statex.dwLength = sizeof (statex);
139      GlobalMemoryStatusEx (&statex);      GlobalMemoryStatusEx (&statex);
140      dmsg(1, ("There are %*I64d total Kbytes of physical memory.\n",      dmsg(2, ("There are %*I64d total Kbytes of physical memory.\n",
141            8, statex.ullTotalPhys));            8, statex.ullTotalPhys));
142      dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n",      dmsg(2, ("There are %*I64d free Kbytes of physical memory.\n",
143            8, statex.ullAvailPhys));            8, statex.ullAvailPhys));
144      physical_memory = statex.ullTotalPhys;      physical_memory = statex.ullTotalPhys;
145    
# Line 121  int main(int argc, char **argv) { Line 172  int main(int argc, char **argv) {
172          if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break;          if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break;
173      }      }
174    
175      dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));      dmsg(2,("AFTER GetProcessWorkingSetSize: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
176      #endif      #endif
177        #endif // WIN32
     #if !defined(WIN32)  
     main_pid = getpid();  
     #endif  
   
     // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)  
     signal(SIGINT, signal_handler);  
   
     #if defined(WIN32)  
     // FIXME: sigaction() not supported on WIN32, we ignore it for now  
     #else  
     // register signal handler for all unusual signals  
     // (we will print the stack trace and exit)  
     struct sigaction sact;  
     sigemptyset(&sact.sa_mask);  
     sact.sa_flags   = 0;  
     sact.sa_handler = signal_handler;  
     sigaction(SIGSEGV, &sact, NULL);  
     sigaction(SIGBUS,  &sact, NULL);  
     sigaction(SIGILL,  &sact, NULL);  
     sigaction(SIGFPE,  &sact, NULL);  
     sigaction(SIGUSR1, &sact, NULL);  
     sigaction(SIGUSR2, &sact, NULL);  
     #endif  
   
     lscp_addr = htonl(LSCP_ADDR);  
     lscp_port = htons(LSCP_PORT);  
   
     // parse and assign command line options  
     parse_options(argc, argv);  
   
     dmsg(1,("LinuxSampler %s\n", VERSION));  
     dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));  
     dmsg(1,("Copyright (C) 2005-2008 Christian Schoenebeck\n"));  
178    
179      if (tune) {      if (tune) {
180          // detect and print system / CPU specific features          // detect and print system / CPU specific features
# Line 166  int main(int argc, char **argv) { Line 184  int main(int argc, char **argv) {
184          Features::enableDenormalsAreZeroMode();          Features::enableDenormalsAreZeroMode();
185      }      }
186    
187        dmsg(1,("Automatic Stacktrace: %s\n", (bShowStackTrace) ? "On" : "Off"));
188    
189      // create LinuxSampler instance      // create LinuxSampler instance
190      dmsg(1,("Creating Sampler..."));      dmsg(1,("Creating Sampler..."));
191      pSampler = new Sampler;      pSampler = new Sampler;
# Line 175  int main(int argc, char **argv) { Line 195  int main(int argc, char **argv) {
195      dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));      dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
196      dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));      dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
197      dmsg(1,("Registered instrument editors: %s\n", InstrumentEditorFactory::AvailableEditorsAsString().c_str()));      dmsg(1,("Registered instrument editors: %s\n", InstrumentEditorFactory::AvailableEditorsAsString().c_str()));
198        dmsg(1,("Registered internal effect systems: %s\n", EffectFactory::AvailableEffectSystemsAsString().c_str()));
199        dmsg(1,("Registered internal effects: %d\n", EffectFactory::AvailableEffectsCount()));
200    
201      // start LSCP network server      // start LSCP network server
202      struct in_addr addr;      struct in_addr addr;
203      addr.s_addr = lscp_addr;      addr.s_addr = (in_addr_t)lscp_addr;
204      dmsg(1,("Starting LSCP network server (%s:%d)...", inet_ntoa(addr), ntohs(lscp_port)));      dmsg(1,("Starting LSCP network server (%s:%d)...", inet_ntoa(addr), ntohs(lscp_port)));
205      pLSCPServer = new LSCPServer(pSampler, lscp_addr, lscp_port);      pLSCPServer = new LSCPServer(pSampler, lscp_addr, lscp_port);
206      pLSCPServer->StartThread();      pLSCPServer->StartThread();
# Line 195  int main(int argc, char **argv) { Line 217  int main(int argc, char **argv) {
217      }      }
218    
219      printf("LinuxSampler initialization completed. :-)\n\n");      printf("LinuxSampler initialization completed. :-)\n\n");
220        
221        if (ExecAfterInit != "") {
222            printf("Executing command: %s\n\n", ExecAfterInit.c_str());
223            if (system(ExecAfterInit.c_str()) == -1) {
224                std::cerr << "Failed to execute the command" << std::endl;
225            }
226        }
227    
228    //TODO: (hopefully) just a temporary nasty hack for launching gigedit on the main thread on Mac (see comments in gigedit.cpp for details)
229    #if defined(__APPLE__)
230        g_mainThreadCallbackSupported = true;
231    #endif
232    
233      while (atomic_read(&running)) {      while (atomic_read(&running)) {
234          if (bPrintStatistics) {          if (bPrintStatistics) {
# Line 223  int main(int argc, char **argv) { Line 257  int main(int argc, char **argv) {
257          }          }
258    
259          pSampler->fireStatistics();          pSampler->fireStatistics();
260            
261            //TODO: (hopefully) just a temporary nasty hack for launching gigedit on the main thread on Mac (see comments in gigedit.cpp for details)
262            #if defined(__APPLE__)
263            if (g_fireMainThreadCallback && g_mainThreadCallback) {
264                void (*fn)(void* info) = g_mainThreadCallback;
265                void* info = g_mainThreadCallbackInfo;
266                g_mainThreadCallbackInfo = NULL;
267                g_mainThreadCallback     = NULL;
268                g_fireMainThreadCallback = false;
269                printf("Received main thread callback, calling now ...\n"); fflush(stdout);
270                (*fn)(info);
271                printf("Main thread callback executed.\n"); fflush(stdout);
272            }
273            #endif
274      }      }
275    //#endif
276      if (pLSCPServer) pLSCPServer->StopThread();      if (pLSCPServer) pLSCPServer->StopThread();
277      // the delete order here is important: the Sampler      // the delete order here is important: the Sampler
278      // destructor sends notifications to the lscpserver      // destructor sends notifications to the lscpserver
# Line 266  void signal_handler(int iSignal) { Line 315  void signal_handler(int iSignal) {
315          }          }
316      }      }
317      signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions      signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
318      std::cerr << "Showing stack trace...\n" << std::flush;      if (bShowStackTrace) {
319      StackTrace();          std::cerr << "Showing stack trace...\n" << std::flush;
320      sleep(2);          #if !AC_APPLE_UNIVERSAL_BUILD
321            StackTrace();
322            #endif
323            sleep(2);
324        }
325      std::cerr << "Killing LinuxSampler...\n" << std::flush;      std::cerr << "Killing LinuxSampler...\n" << std::flush;
326      kill_app(); // Use abort() if we want to generate a core dump.      kill_app(); // Use abort() if we want to generate a core dump.
327  }  }
# Line 296  void parse_options(int argc, char **argv Line 349  void parse_options(int argc, char **argv
349              {"create-instruments-db",1,0,0},              {"create-instruments-db",1,0,0},
350              {"lscp-addr",1,0,0},              {"lscp-addr",1,0,0},
351              {"lscp-port",1,0,0},              {"lscp-port",1,0,0},
352                {"stacktrace",0,0,0},
353                {"exec-after-init",1,0,0},
354              {0,0,0,0}              {0,0,0,0}
355          };          };
356    
# Line 319  void parse_options(int argc, char **argv Line 374  void parse_options(int argc, char **argv
374                      printf("--lscp-port                 set LSCP port (default: 8888)\n");                      printf("--lscp-port                 set LSCP port (default: 8888)\n");
375                      printf("--create-instruments-db     creates an instruments DB\n");                      printf("--create-instruments-db     creates an instruments DB\n");
376                      printf("--instruments-db-location   specifies the instruments DB file\n");                      printf("--instruments-db-location   specifies the instruments DB file\n");
377                        printf("--stacktrace                automatically shows stacktrace if crashes\n");
378                        printf("                            (broken on most systems at the moment)\n");
379                        printf("--exec-after-init           executes a command after initialization\n");
380                      exit(EXIT_SUCCESS);                      exit(EXIT_SUCCESS);
381                      break;                      break;
382                  case 1: // --version                  case 1: // --version
# Line 327  void parse_options(int argc, char **argv Line 385  void parse_options(int argc, char **argv
385                      break;                      break;
386                  case 2: // --profile                  case 2: // --profile
387                      profile = true;                      profile = true;
388                        //FIXME: profiling code is currently broken!
389                        std::cerr << "Option '--profile' is currently not supported, since the profiling code is currently broken!"  << std::endl;
390                        exit(EXIT_FAILURE);
391                      break;                      break;
392                  case 3: // --no-tune                  case 3: // --no-tune
393                      tune = false;                      tune = false;
# Line 389  void parse_options(int argc, char **argv Line 450  void parse_options(int argc, char **argv
450                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
451                      return;                      return;
452  #endif  #endif
453                  case 7: // --lscp-addr                  case 7: { // --lscp-addr
454                      struct in_addr addr;                      struct in_addr addr;
455                      if (inet_aton(optarg, &addr) == 0)                      if (inet_aton(optarg, &addr) == 0)
456                          printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");                          printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");
457                      else                      else
458                          lscp_addr = addr.s_addr;                          lscp_addr = addr.s_addr;
459                      break;                      break;
460                  case 8: // --lscp-port                  }
461                    case 8: {// --lscp-port
462                      long unsigned int port = 0;                      long unsigned int port = 0;
463                      if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))                      if ((sscanf(optarg, "%lu", &port) != 1) || (port == 0) || (port > 65535))
464                          printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");                          printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");
465                      else                      else
466                          lscp_port = htons(port);                          lscp_port = htons(port);
467                      break;                      break;
468                    }
469                    case 9: // --stacktrace
470                        bShowStackTrace = true;
471                        break;
472                    case 10: // --exec-after-init
473                        ExecAfterInit = optarg;
474                        break;
475              }              }
476          }          }
477      }      }

Legend:
Removed from v.1765  
changed lines
  Added in v.3058

  ViewVC Help
Powered by ViewVC