/[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 41 by schoenebeck, Wed Mar 31 10:28:42 2004 UTC revision 319 by schoenebeck, Mon Dec 13 00:46:42 2004 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck         *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *                                                                         *   *                                                                         *
7   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
8   *   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  #include <getopt.h>  #include <getopt.h>
24  #include <signal.h>  #include <signal.h>
25    
26  #include "global.h"  #include "Sampler.h"
27  #include "diskthread.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
28  #include "audiothread.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
 #include "alsaio.h"  
 #include "jackio.h"  
 #include "midiin.h"  
 #include "stream.h"  
 #include "RIFF.h"  
 #include "gig.h"  
29  #include "network/lscpserver.h"  #include "network/lscpserver.h"
30    #include "common/stacktrace.h"
31    #include "common/Features.h"
32    
33  #define AUDIO_CHANNELS          2     // stereo  using namespace LinuxSampler;
34  #define AUDIO_FRAGMENTS         3     // 3 fragments, if it does not work set it to 2  
35  #define AUDIO_FRAGMENTSIZE      512   // each fragment has 512 frames  Sampler*    pSampler    = NULL;
36  #define AUDIO_SAMPLERATE        44100 // Hz  LSCPServer* pLSCPServer = NULL;
37    pthread_t   main_thread;
 enum patch_format_t {  
     patch_format_unknown,  
     patch_format_gig,  
     patch_format_dls  
 } patch_format = patch_format_unknown;  
   
 AudioIO*     pAudioIO         = NULL;  
 MidiIn*      pMidiInThread    = NULL;  
 LSCPServer*  pLSCPServer      = NULL;  
 AudioThread* pEngine          = NULL;  
 uint         instrument_index = 0;  
 double       volume           = 0.25;  
 int          num_fragments    = AUDIO_FRAGMENTS;  
 int          fragmentsize     = AUDIO_FRAGMENTSIZE;  
 uint         samplerate       = AUDIO_SAMPLERATE;  
 String       input_client;  
 String       alsaout          = "0,0"; // default card  
 String       jack_playback[2] = { "", "" };  
 bool         use_jack         = true;  
 bool         run_server       = false;  
 pthread_t    signalhandlerthread;  
38    
39  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
40  void signal_handler(int signal);  void signal_handler(int signal);
41    void kill_app();
42    
43  int main(int argc, char **argv) {  int main(int argc, char **argv) {
44    
45        // initialize the stack trace mechanism with our binary file
46        StackTraceInit(argv[0], -1);
47    
48        main_thread = pthread_self();
49    
50      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
     signalhandlerthread = pthread_self();  
51      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
52    
53      // parse and assign command line options      // register signal handler for all unusual signals
54      parse_options(argc, argv);      // (we will print the stack trace and exit)
55        struct sigaction sact;
56      if (patch_format != patch_format_gig) {      sigemptyset(&sact.sa_mask);
57          printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n");      sact.sa_flags   = 0;
58          printf("Use 'linuxsampler --help' to see all available options.\n");      sact.sa_handler = signal_handler;
59          return EXIT_FAILURE;      sigaction(SIGSEGV, &sact, NULL);
60      }      sigaction(SIGBUS,  &sact, NULL);
61        sigaction(SIGILL,  &sact, NULL);
62        sigaction(SIGFPE,  &sact, NULL);
63        sigaction(SIGUSR1, &sact, NULL);
64        sigaction(SIGUSR2, &sact, NULL);
65    
66      int error = 1;      // parse and assign command line options
67  #if HAVE_JACK      //parse_options(argc, argv);
     if (use_jack) {  
         dmsg(1,("Initializing audio output (Jack)..."));  
         pAudioIO = new JackIO();  
         error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback);  
         if (error) dmsg(1,("Trying Alsa output instead.\n"));  
     }  
 #endif // HAVE_JACK  
     if (error) {  
         dmsg(1,("Initializing audio output (Alsa)..."));  
         pAudioIO = new AlsaIO();  
         int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout);  
         if (error) return EXIT_FAILURE;  
     }  
     dmsg(1,("OK\n"));  
68    
69      AudioThread* pEngine       = new AudioThread(pAudioIO);      dmsg(1,("LinuxSampler %s\n", VERSION));
70      MidiIn*      pMidiInThread = new MidiIn(pEngine);      dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n"));
71    
72      // Loading gig file      // detect and print system / CPU specific features
73      result_t result = pEngine->LoadInstrument(argv[argc - 1], instrument_index);      String sFeatures;
74      if (result.type == result_type_error) return EXIT_FAILURE;      Features::detect();
75      pEngine->Volume = volume;      #if ARCH_X86
76        if (Features::supportsMMX()) sFeatures += " MMX";
77      dmsg(1,("Starting MIDI in thread..."));      if (Features::supportsSSE()) sFeatures += " SSE";
78      if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str());      #endif // ARCH_X86
79      pMidiInThread->StartThread();      if (!sFeatures.size()) sFeatures = " None";
80        dmsg(1,("Detected features:%s\n",sFeatures.c_str()));
81    
82        // create LinuxSampler instance
83        dmsg(1,("Creating Sampler..."));
84        pSampler = new Sampler;
85      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
86    
87      sleep(1);      dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
88        dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
89    
90      dmsg(1,("Starting audio thread..."));      // start LSCP network server
91      pAudioIO->AssignEngine(pEngine);      dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT));
92      pAudioIO->Activate();      pLSCPServer = new LSCPServer(pSampler);
93        pLSCPServer->StartThread();
94        pLSCPServer->WaitUntilInitialized();
95      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
96    
     if (run_server) {  
         dmsg(1,("Starting network server..."));  
         pLSCPServer = new LSCPServer(pEngine);  
         pLSCPServer->StartThread();  
         dmsg(1,("OK\n"));  
     }  
   
97      printf("LinuxSampler initialization completed.\n");      printf("LinuxSampler initialization completed.\n");
98    
99      while(true)  {      while(true)  {
100        printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",        /*printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",
101              pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,              pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,
102              pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());              pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
103        fflush(stdout);        fflush(stdout);*/
104        usleep(500000);        usleep(500000);
105      }      }
106    
107      return EXIT_SUCCESS;      return EXIT_SUCCESS;
108  }  }
109    
110  void signal_handler(int signal) {  void signal_handler(int iSignal) {
111      if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) {      switch (iSignal) {
112          // stop all threads          case SIGINT: {
113          if (pAudioIO)      pAudioIO->Close();              if (pthread_equal(pthread_self(), main_thread)) {
114          if (pMidiInThread) pMidiInThread->StopThread();                  if (pLSCPServer) {
115                        pLSCPServer->StopThread();
116          // free all resources                      delete pLSCPServer;
117          if (pMidiInThread) delete pMidiInThread;                  }
118          if (pEngine)       delete pEngine;                  if (pSampler) delete pSampler;
119          if (pAudioIO)      delete pAudioIO;                  printf("LinuxSampler stopped due to SIGINT.\n");
120                    exit(EXIT_SUCCESS);
121          printf("LinuxSampler stopped due to SIGINT\n");              }
122          exit(EXIT_SUCCESS);              return;
123            }
124            case SIGSEGV:
125                std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
126                break;
127            case SIGBUS:
128                std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush;
129                break;
130            case SIGILL:
131                std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush;
132                break;
133            case SIGFPE:
134                std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush;
135                break;
136            case SIGUSR1:
137                std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush;
138                break;
139            case SIGUSR2:
140                std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
141                break;
142            default: { // this should never happen, as we register for the signals we want
143                std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
144                break;
145            }
146      }      }
147        signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
148        std::cerr << "Showing stack trace...\n" << std::flush;
149        StackTrace();
150        sleep(2);
151        std::cerr << "Killing LinuxSampler...\n" << std::flush;
152        kill_app(); // Use abort() if we want to generate a core dump.
153  }  }
154    
155  void parse_options(int argc, char **argv) {  void kill_app() {
156        kill(main_thread, SIGKILL);
157    }
158    
159    /*void parse_options(int argc, char **argv) {
160      int res;      int res;
161      int option_index = 0;      int option_index = 0;
162      static struct option long_options[] =      static struct option long_options[] =
# Line 254  void parse_options(int argc, char **argv Line 258  void parse_options(int argc, char **argv
258              }              }
259          }          }
260      }      }
261  }  }*/

Legend:
Removed from v.41  
changed lines
  Added in v.319

  ViewVC Help
Powered by ViewVC