/[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 9 by schoenebeck, Wed Nov 5 14:47:10 2003 UTC revision 411 by schoenebeck, Sat Feb 26 02:01:14 2005 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     *   Copyright (C) 2005 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 20  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
 #include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
24  #include <getopt.h>  #include <getopt.h>
25  #include <signal.h>  #include <signal.h>
26    
27  #include "global.h"  #include "Sampler.h"
28  #include "audioio.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
29  #include "diskthread.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
30  #include "audiothread.h"  #include "engines/gig/Profiler.h"
31  #include "midiin.h"  #include "network/lscpserver.h"
32  #include "stream.h"  #include "common/stacktrace.h"
33  #include "RIFF.h"  #include "common/Features.h"
34  #include "gig.h"  
35    using namespace LinuxSampler;
36  #define AUDIO_CHANNELS          2     // stereo  
37  #define AUDIO_FRAGMENTS         3     // 3 fragments, if it does not work set it to 2  Sampler*    pSampler    = NULL;
38  #define AUDIO_FRAGMENTSIZE      512   // each fragment has 512 frames  LSCPServer* pLSCPServer = NULL;
39  #define AUDIO_SAMPLERATE        44100 // Hz  pthread_t   main_thread;
40    bool profile = false;
41  enum patch_format_t {  bool tune = true;
     patch_format_unknown,  
     patch_format_gig,  
     patch_format_dls  
 } patch_format;  
   
 AudioIO*         pAudioIO;  
 DiskThread*      pDiskThread;  
 AudioThread*     pAudioThread;  
 MidiIn*          pMidiInThread;  
   
 RIFF::File*      pRIFF;  
 gig::File*       pGig;  
 gig::Instrument* pInstrument;  
42    
43  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
44  void signal_handler(int signal);  void signal_handler(int signal);
45    void kill_app();
 int midi_non_blocking;  
 int num_fragments;  
 int fragmentsize;  
 bool instrument_is_DLS;  
 bool instruemtn_is_gig;  
 char midi_device[40];  
46    
47  int main(int argc, char **argv) {  int main(int argc, char **argv) {
48      pAudioIO = NULL;  
49      pRIFF    = NULL;      // initialize the stack trace mechanism with our binary file
50      pGig     = NULL;      StackTraceInit(argv[0], -1);
51    
52        main_thread = pthread_self();
53    
54      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
55      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
56    
57      patch_format      = patch_format_unknown;      // register signal handler for all unusual signals
58      midi_non_blocking = 1;      // (we will print the stack trace and exit)
59      num_fragments     = AUDIO_FRAGMENTS;      struct sigaction sact;
60      fragmentsize      = AUDIO_FRAGMENTSIZE;      sigemptyset(&sact.sa_mask);
61      strcpy(midi_device, "/dev/midi00");      sact.sa_flags   = 0;
62        sact.sa_handler = signal_handler;
63        sigaction(SIGSEGV, &sact, NULL);
64        sigaction(SIGBUS,  &sact, NULL);
65        sigaction(SIGILL,  &sact, NULL);
66        sigaction(SIGFPE,  &sact, NULL);
67        sigaction(SIGUSR1, &sact, NULL);
68        sigaction(SIGUSR2, &sact, NULL);
69    
70      // parse and assign command line options      // parse and assign command line options
71      parse_options(argc, argv);      parse_options(argc, argv);
72    
73      if (patch_format != patch_format_gig) {      dmsg(1,("LinuxSampler %s\n", VERSION));
74          printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig\n");      dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n"));
75          printf("to load a .gig file!\n");      dmsg(1,("Copyright (C) 2005 Christian Schoenebeck\n"));
76          return EXIT_FAILURE;  
77        if (tune)
78        {
79                // detect and print system / CPU specific features
80                String sFeatures;
81                Features::detect();
82    #if ARCH_X86
83                if (Features::supportsMMX()) sFeatures += " MMX";
84                if (Features::supportsSSE()) sFeatures += " SSE";
85    #endif // ARCH_X86
86                if (!sFeatures.size()) sFeatures = " None";
87                dmsg(1,("Detected features:%s\n",sFeatures.c_str()));
88      }      }
89    
90      dmsg(("Initializing audio output..."));      // create LinuxSampler instance
91      pAudioIO = new AudioIO();      dmsg(1,("Creating Sampler..."));
92      int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize);      pSampler = new Sampler;
93      if (error) return EXIT_FAILURE;      dmsg(1,("OK\n"));
94      dmsg(("OK\n"));  
95        dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
96      // Loading gig file      dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
97      try {  
98          printf("Loading gig file...");      // start LSCP network server
99          fflush(stdout);      dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT));
100          pRIFF       = new RIFF::File(argv[argc - 1]);      pLSCPServer = new LSCPServer(pSampler);
101          pGig        = new gig::File(pRIFF);      pLSCPServer->StartThread();
102          pInstrument = pGig->GetFirstInstrument();      pLSCPServer->WaitUntilInitialized();
103          pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part      dmsg(1,("OK\n"));
104          printf("OK\n");  
105          fflush(stdout);      if (profile)
106      }      {
107      catch (RIFF::Exception e) {              dmsg(1,("Calibrating profiler..."));
108          e.PrintMessage();              gig::Profiler::Calibrate();
109          return EXIT_FAILURE;              gig::Profiler::Reset();
110                dmsg(1,("OK\n"));
111      }      }
     catch (...) {  
         printf("Unknown exception while trying to parse gig file.\n");  
         return EXIT_FAILURE;  
     }  
   
     DiskThread*  pDiskThread   = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo  
     AudioThread* pAudioThread  = new AudioThread(pAudioIO, pDiskThread, pInstrument);  
     MidiIn*      pMidiInThread = new MidiIn(pAudioThread);  
   
     dmsg(("Starting disk thread..."));  
     pDiskThread->StartThread();  
     dmsg(("OK\n"));  
     dmsg(("Starting MIDI in thread..."));  
     pMidiInThread->StartThread();  
     dmsg(("OK\n"));  
   
     sleep(1);  
     dmsg(("Starting audio thread..."));  
     pAudioThread->StartThread();  
     dmsg(("OK\n"));  
112    
113      printf("LinuxSampler initialization completed.\n");      printf("LinuxSampler initialization completed.\n");
114    
115      while(true) sleep(1000);      std::list<LSCPEvent::event_t> rtEvents;
116        rtEvents.push_back(LSCPEvent::event_voice_count);
117        rtEvents.push_back(LSCPEvent::event_stream_count);
118        rtEvents.push_back(LSCPEvent::event_buffer_fill);
119    
120        while(true)
121        {
122          /*printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",
123                pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,
124                pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
125          fflush(stdout);*/
126          sleep(1);
127          if (profile)
128          {
129                  unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
130                  unsigned int bv = gig::Profiler::GetBogoVoices(samplingFreq);
131                  if (bv != 0)
132                  {
133                          printf("       BogoVoices: %i         \r", bv);
134                          fflush(stdout);
135                  }
136          }
137          
138          if (LSCPServer::EventSubscribers(rtEvents))
139          {
140                  LSCPServer::LockRTNotify();
141                  std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
142                  std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
143                  for (; iter != channels.end(); iter++) {
144                          SamplerChannel* pSamplerChannel = iter->second;
145                          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
146                          if (!pEngineChannel) continue;
147                          Engine* pEngine = pEngineChannel->GetEngine();
148                          if (!pEngine) continue;
149                          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, iter->first, pEngine->VoiceCount()));
150                          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, iter->first, pEngine->DiskStreamCount()));
151                          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, iter->first, pEngine->DiskStreamBufferFillPercentage()));
152                  }
153                  LSCPServer::UnlockRTNotify();
154          }
155    
156        }
157    
158      return EXIT_SUCCESS;      return EXIT_SUCCESS;
159  }  }
160    
161  void signal_handler(int signal) {  void signal_handler(int iSignal) {
162      if (signal == SIGINT) {      switch (iSignal) {
163          // stop all threads          case SIGINT: {
164          if (pMidiInThread) pMidiInThread->StopThread();              if (pthread_equal(pthread_self(), main_thread)) {
165          if (pAudioThread)  pAudioThread->StopThread();                  if (pLSCPServer) {
166          if (pDiskThread)   pDiskThread->StopThread();                      pLSCPServer->StopThread();
167                        delete pLSCPServer;
168          sleep(1);                  }
169                    if (pSampler) delete pSampler;
170          // free all resources                  printf("LinuxSampler stopped due to SIGINT.\n");
171          if (pMidiInThread) delete pMidiInThread;                  exit(EXIT_SUCCESS);
172          if (pAudioThread)  delete pAudioThread;              }
173          if (pDiskThread)   delete pDiskThread;              return;
174          if (pGig)          delete pGig;          }
175          if (pRIFF)         delete pRIFF;          case SIGSEGV:
176          if (pAudioIO)      delete pAudioIO;              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
177                break;
178          printf("LinuxSampler stopped due to SIGINT\n");          case SIGBUS:
179          exit(EXIT_SUCCESS);              std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush;
180                break;
181            case SIGILL:
182                std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush;
183                break;
184            case SIGFPE:
185                std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush;
186                break;
187            case SIGUSR1:
188                std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush;
189                break;
190            case SIGUSR2:
191                std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
192                break;
193            default: { // this should never happen, as we register for the signals we want
194                std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
195                break;
196            }
197      }      }
198        signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
199        std::cerr << "Showing stack trace...\n" << std::flush;
200        StackTrace();
201        sleep(2);
202        std::cerr << "Killing LinuxSampler...\n" << std::flush;
203        kill_app(); // Use abort() if we want to generate a core dump.
204    }
205    
206    void kill_app() {
207        kill(main_thread, SIGKILL);
208  }  }
209    
210  void parse_options(int argc, char **argv) {  void parse_options(int argc, char **argv) {
# Line 163  void parse_options(int argc, char **argv Line 212  void parse_options(int argc, char **argv
212      int option_index = 0;      int option_index = 0;
213      static struct option long_options[] =      static struct option long_options[] =
214          {          {
             {"numfragments",1,0,0},  
             {"fragmentsize",1,0,0},  
             {"dls",0,0,0},  
             {"gig",0,0,0},  
215              {"help",0,0,0},              {"help",0,0,0},
216                {"version",0,0,0},
217                {"profile",0,0,0},
218                {"no-tune",0,0,0},
219              {0,0,0,0}              {0,0,0,0}
220          };          };
221    
222      while (true) {      while (true) {
223          res = getopt_long_only(argc, argv, "", long_options, &option_index);          /*
224              Stephane Letz : letz@grame.fr
225              getopt_long_only does not exist on OSX : replaced by getopt_long for now.
226            */
227            res = getopt_long(argc, argv, "", long_options, &option_index);
228          if(res == -1) break;          if(res == -1) break;
229          if (res == 0) {          if (res == 0) {
230              switch(option_index) {              switch(option_index) {
231                  case 0:                  case 0: // --help
232                      num_fragments = atoi(optarg);                      printf("usage: linuxsampler [OPTIONS]\n\n");
233                      break;                      printf("--help             prints this message\n");
234                  case 1:                      printf("--version          prints version information\n");
235                      fragmentsize = atoi(optarg);                      printf("--profile          profile synthesis algorithms\n");
236                        printf("--no-tune          disable assembly optimization\n");
237                        exit(EXIT_SUCCESS);
238                      break;                      break;
239                  case 2:                  case 1: // --version
240                      patch_format = patch_format_dls;                      printf("LinuxSampler %s\n", VERSION);
241                        exit(EXIT_SUCCESS);
242                      break;                      break;
243                  case 3:                  case 2: // --profile
244                      patch_format = patch_format_gig;                      profile = true;
245                      break;                      break;
246                  case 4:                  case 3: // --no-tune
247                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");                      tune = false;
                     printf("--numfragments     sets the number of audio fragments\n");  
                     printf("--fragmentsize     sets the fragment size\n");  
                     printf("--dls              loads a DLS instrument\n");  
                     printf("--gig              loads a Gigasampler instrument\n");  
                     exit(0);  
248                      break;                      break;
249              }              }
250          }          }

Legend:
Removed from v.9  
changed lines
  Added in v.411

  ViewVC Help
Powered by ViewVC