/[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 1502 by senoner, Wed Nov 21 07:29:52 2007 UTC revision 1830 by schoenebeck, Sat Jan 31 11:31:41 2009 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
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-2007 Christian Schoenebeck                        *   *   Copyright (C) 2005-2009 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 39  Line 39 
39  #include "network/lscpserver.h"  #include "network/lscpserver.h"
40  #include "common/stacktrace.h"  #include "common/stacktrace.h"
41  #include "common/Features.h"  #include "common/Features.h"
42    #include "common/atomic.h"
43    
44  using namespace LinuxSampler;  using namespace LinuxSampler;
45    
# Line 56  int inet_aton(const char *cp, struct in_ Line 57  int inet_aton(const char *cp, struct in_
57      return (addr->s_addr == INADDR_NONE) ? 0 : 1;      return (addr->s_addr == INADDR_NONE) ? 0 : 1;
58  }  }
59    
 DWORD main_thread;  
60  #else  #else
 pthread_t   main_thread;  
61  pid_t       main_pid;  pid_t       main_pid;
62  #endif  #endif
63  bool bPrintStatistics = false;  bool bPrintStatistics = false;
64  bool profile = false;  bool profile = false;
65  bool tune = true;  bool tune = true;
66    static bool bShowStackTrace = false;
67  unsigned long int lscp_addr;  unsigned long int lscp_addr;
68  unsigned short int lscp_port;  unsigned short int lscp_port;
69    
70  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
71  void signal_handler(int signal);  void signal_handler(int signal);
72  void kill_app();  void kill_app();
73    static atomic_t running = ATOMIC_INIT(1);
74    
75  int main(int argc, char **argv) {  int main(int argc, char **argv) {
76    
77        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      // initialize the stack trace mechanism with our binary file      // initialize the stack trace mechanism with our binary file
91      StackTraceInit(argv[0], -1);      // (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    
112        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      #if defined(WIN32)      #if defined(WIN32)
117      // 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)
# Line 122  int main(int argc, char **argv) { Line 159  int main(int argc, char **argv) {
159      }      }
160    
161      dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));      dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
162      #endif      #endif // WIN32
   
     #if defined(WIN32)  
     main_thread = GetCurrentThreadId();  
     #else  
     main_pid = getpid();  
     main_thread = pthread_self();  
     #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-2007 Christian Schoenebeck\n"));  
163    
164      if (tune) {      if (tune) {
165          // detect and print system / CPU specific features          // detect and print system / CPU specific features
# Line 169  int main(int argc, char **argv) { Line 169  int main(int argc, char **argv) {
169          Features::enableDenormalsAreZeroMode();          Features::enableDenormalsAreZeroMode();
170      }      }
171    
172        dmsg(1,("Automatic Stacktrace: %s\n", (bShowStackTrace) ? "On" : "Off"));
173    
174      // create LinuxSampler instance      // create LinuxSampler instance
175      dmsg(1,("Creating Sampler..."));      dmsg(1,("Creating Sampler..."));
176      pSampler = new Sampler;      pSampler = new Sampler;
# Line 199  int main(int argc, char **argv) { Line 201  int main(int argc, char **argv) {
201    
202      printf("LinuxSampler initialization completed. :-)\n\n");      printf("LinuxSampler initialization completed. :-)\n\n");
203    
204      std::list<LSCPEvent::event_t> rtEvents;      while (atomic_read(&running)) {
     rtEvents.push_back(LSCPEvent::event_voice_count);  
     rtEvents.push_back(LSCPEvent::event_stream_count);  
     rtEvents.push_back(LSCPEvent::event_buffer_fill);  
     rtEvents.push_back(LSCPEvent::event_total_voice_count);  
   
     while (true) {  
205          if (bPrintStatistics) {          if (bPrintStatistics) {
206              const std::set<Engine*>& engines = EngineFactory::EngineInstances();              const std::set<Engine*>& engines = EngineFactory::EngineInstances();
207              std::set<Engine*>::iterator itEngine = engines.begin();              std::set<Engine*>::iterator itEngine = engines.begin();
# Line 219  int main(int argc, char **argv) { Line 215  int main(int argc, char **argv) {
215              }              }
216          }          }
217    
218        sleep(1);          sleep(1);
219        if (profile)          if (profile)
220        {          {
221            unsigned int samplingFreq = 48000; //FIXME: hardcoded for now              unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
222            unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);              unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);
223            if (bv != 0)              if (bv != 0)
224            {              {
225                printf("       BogoVoices: %i         \r", bv);                  printf("       BogoVoices: %i         \r", bv);
226                fflush(stdout);                  fflush(stdout);
227            }              }
228        }          }
   
       if (LSCPServer::EventSubscribers(rtEvents))  
       {  
           LSCPServer::LockRTNotify();  
           std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();  
           std::map<uint,SamplerChannel*>::iterator iter = channels.begin();  
           for (; iter != channels.end(); iter++) {  
               SamplerChannel* pSamplerChannel = iter->second;  
               EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
               if (!pEngineChannel) continue;  
               Engine* pEngine = pEngineChannel->GetEngine();  
               if (!pEngine) continue;  
               pSampler->fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());  
               pSampler->fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());  
               pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());  
               pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount());  
           }  
           LSCPServer::UnlockRTNotify();  
       }  
229    
230            pSampler->fireStatistics();
231      }      }
232        if (pLSCPServer) pLSCPServer->StopThread();
233        // the delete order here is important: the Sampler
234        // destructor sends notifications to the lscpserver
235        if (pSampler) delete pSampler;
236        if (pLSCPServer) delete pLSCPServer;
237        printf("LinuxSampler stopped due to SIGINT.\n");
238      return EXIT_SUCCESS;      return EXIT_SUCCESS;
239  }  }
240    
241  void signal_handler(int iSignal) {  void signal_handler(int iSignal) {
242      switch (iSignal) {      switch (iSignal) {
243          case SIGINT: {          case SIGINT:
244              #if defined(WIN32)              atomic_set(&running, 0);
             if( GetCurrentThreadId() == main_thread ) {  
             #else  
             if (pthread_equal(pthread_self(), main_thread)) {  
             #endif  
                 if (pLSCPServer) pLSCPServer->StopThread();  
                 // the delete order here is important: the Sampler  
                 // destructor sends notifications to the lscpserver  
                 if (pSampler) delete pSampler;  
                 if (pLSCPServer) delete pLSCPServer;  
 #if HAVE_SQLITE3  
                 InstrumentsDb::Destroy();  
 #endif  
                 printf("LinuxSampler stopped due to SIGINT.\n");  
                 exit(EXIT_SUCCESS);  
             }  
245              return;              return;
         }  
246          #if defined(WIN32)          #if defined(WIN32)
247          // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call          // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call
248          #else          #else
# Line 304  void signal_handler(int iSignal) { Line 271  void signal_handler(int iSignal) {
271          }          }
272      }      }
273      signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions      signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
274      std::cerr << "Showing stack trace...\n" << std::flush;      if (bShowStackTrace) {
275      StackTrace();          std::cerr << "Showing stack trace...\n" << std::flush;
276      sleep(2);          StackTrace();
277            sleep(2);
278        }
279      std::cerr << "Killing LinuxSampler...\n" << std::flush;      std::cerr << "Killing LinuxSampler...\n" << std::flush;
280      kill_app(); // Use abort() if we want to generate a core dump.      kill_app(); // Use abort() if we want to generate a core dump.
281  }  }
# Line 334  void parse_options(int argc, char **argv Line 303  void parse_options(int argc, char **argv
303              {"create-instruments-db",1,0,0},              {"create-instruments-db",1,0,0},
304              {"lscp-addr",1,0,0},              {"lscp-addr",1,0,0},
305              {"lscp-port",1,0,0},              {"lscp-port",1,0,0},
306                {"stacktrace",0,0,0},
307              {0,0,0,0}              {0,0,0,0}
308          };          };
309    
# Line 357  void parse_options(int argc, char **argv Line 327  void parse_options(int argc, char **argv
327                      printf("--lscp-port                 set LSCP port (default: 8888)\n");                      printf("--lscp-port                 set LSCP port (default: 8888)\n");
328                      printf("--create-instruments-db     creates an instruments DB\n");                      printf("--create-instruments-db     creates an instruments DB\n");
329                      printf("--instruments-db-location   specifies the instruments DB file\n");                      printf("--instruments-db-location   specifies the instruments DB file\n");
330                        printf("--stacktrace                automatically shows stacktrace if crashes\n");
331                        printf("                            (broken on most systems at the moment)\n");
332                      exit(EXIT_SUCCESS);                      exit(EXIT_SUCCESS);
333                      break;                      break;
334                  case 1: // --version                  case 1: // --version
# Line 411  void parse_options(int argc, char **argv Line 383  void parse_options(int argc, char **argv
383                          if (optarg) {                          if (optarg) {
384                              std::cout << "Creating instruments database..." << std::endl;                              std::cout << "Creating instruments database..." << std::endl;
385                              InstrumentsDb::CreateInstrumentsDb(String(optarg));                              InstrumentsDb::CreateInstrumentsDb(String(optarg));
                             InstrumentsDb::Destroy();  
386                              std::cout << "Done" << std::endl;                              std::cout << "Done" << std::endl;
387                          }                          }
388                      } catch(Exception e) {                      } catch(Exception e) {
# Line 428  void parse_options(int argc, char **argv Line 399  void parse_options(int argc, char **argv
399                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
400                      return;                      return;
401  #endif  #endif
402                  case 7: // --lscp-addr                  case 7: { // --lscp-addr
403                      struct in_addr addr;                      struct in_addr addr;
404                      if (inet_aton(optarg, &addr) == 0)                      if (inet_aton(optarg, &addr) == 0)
405                          printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");                          printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");
406                      else                      else
407                          lscp_addr = addr.s_addr;                          lscp_addr = addr.s_addr;
408                      break;                      break;
409                  case 8: // --lscp-port                  }
410                    case 8: {// --lscp-port
411                      long unsigned int port = 0;                      long unsigned int port = 0;
412                      if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))                      if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))
413                          printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");                          printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");
414                      else                      else
415                          lscp_port = htons(port);                          lscp_port = htons(port);
416                      break;                      break;
417                    }
418                    case 9: // --stacktrace
419                        bShowStackTrace = true;
420                        break;
421              }              }
422          }          }
423      }      }

Legend:
Removed from v.1502  
changed lines
  Added in v.1830

  ViewVC Help
Powered by ViewVC