/[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 12 by schoenebeck, Sun Nov 16 19:01:50 2003 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 20  Line 20 
20   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
21   ***************************************************************************/   ***************************************************************************/
22    
 #include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
23  #include <getopt.h>  #include <getopt.h>
24  #include <signal.h>  #include <signal.h>
25    
26  #include "global.h"  #include "Sampler.h"
27  #include "audioio.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
28  #include "diskthread.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
29  #include "audiothread.h"  #include "network/lscpserver.h"
30  #include "midiin.h"  #include "common/stacktrace.h"
31  #include "stream.h"  #include "common/Features.h"
32  #include "RIFF.h"  
33  #include "gig.h"  using namespace LinuxSampler;
34    
35  #define AUDIO_CHANNELS          2     // stereo  Sampler*    pSampler    = NULL;
36  #define AUDIO_FRAGMENTS         3     // 3 fragments, if it does not work set it to 2  LSCPServer* pLSCPServer = NULL;
37  #define AUDIO_FRAGMENTSIZE      512   // each fragment has 512 frames  pthread_t   main_thread;
 #define AUDIO_SAMPLERATE        44100 // Hz  
   
 enum patch_format_t {  
     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;  
 int              num_fragments;  
 int              fragmentsize;  
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      pAudioIO = NULL;  
45      pRIFF    = NULL;      // initialize the stack trace mechanism with our binary file
46      pGig     = NULL;      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>)
51      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
52    
53      patch_format      = patch_format_unknown;      // register signal handler for all unusual signals
54      num_fragments     = AUDIO_FRAGMENTS;      // (we will print the stack trace and exit)
55      fragmentsize      = AUDIO_FRAGMENTSIZE;      struct sigaction sact;
56        sigemptyset(&sact.sa_mask);
57        sact.sa_flags   = 0;
58        sact.sa_handler = signal_handler;
59        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      // parse and assign command line options      // parse and assign command line options
67      parse_options(argc, argv);      //parse_options(argc, argv);
68    
69      if (patch_format != patch_format_gig) {      dmsg(1,("LinuxSampler %s\n", VERSION));
70          printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n");      dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n"));
         return EXIT_FAILURE;  
     }  
71    
72      dmsg(1,("Initializing audio output..."));      // detect and print system / CPU specific features
73      pAudioIO = new AudioIO();      String sFeatures;
74      int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize);      Features::detect();
75      if (error) return EXIT_FAILURE;      #if ARCH_X86
76        if (Features::supportsMMX()) sFeatures += " MMX";
77        if (Features::supportsSSE()) sFeatures += " SSE";
78        #endif // ARCH_X86
79        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      // Loading gig file      dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
88      try {      dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
         printf("Loading gig file...");  
         fflush(stdout);  
         pRIFF       = new RIFF::File(argv[argc - 1]);  
         pGig        = new gig::File(pRIFF);  
         pInstrument = pGig->GetFirstInstrument();  
         pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part  
         printf("OK\n");  
         fflush(stdout);  
     }  
     catch (RIFF::Exception e) {  
         e.PrintMessage();  
         return EXIT_FAILURE;  
     }  
     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);  
89    
90      dmsg(1,("Starting disk thread..."));      // start LSCP network server
91      pDiskThread->StartThread();      dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT));
92      dmsg(1,("OK\n"));      pLSCPServer = new LSCPServer(pSampler);
93      dmsg(1,("Starting MIDI in thread..."));      pLSCPServer->StartThread();
94      pMidiInThread->StartThread();      pLSCPServer->WaitUntilInitialized();
     dmsg(1,("OK\n"));  
   
     sleep(1);  
     dmsg(1,("Starting audio thread..."));  
     pAudioThread->StartThread();  
95      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
96    
97      printf("LinuxSampler initialization completed.\n");      printf("LinuxSampler initialization completed.\n");
98    
99      while(true)  {      while(true)  {
100        printf("Voices: %3.3d  Streams: %3.3d  \r",pAudioThread->ActiveVoiceCount, pDiskThread->ActiveStreamCount); fflush(stdout);        /*printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",
101                pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,
102                pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
103          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 (signal == SIGINT) {      switch (iSignal) {
112          // stop all threads          case SIGINT: {
113          if (pMidiInThread) pMidiInThread->StopThread();              if (pthread_equal(pthread_self(), main_thread)) {
114          if (pAudioThread)  pAudioThread->StopThread();                  if (pLSCPServer) {
115          if (pDiskThread)   pDiskThread->StopThread();                      pLSCPServer->StopThread();
116                        delete pLSCPServer;
117          sleep(1);                  }
118                    if (pSampler) delete pSampler;
119          // free all resources                  printf("LinuxSampler stopped due to SIGINT.\n");
120          if (pMidiInThread) delete pMidiInThread;                  exit(EXIT_SUCCESS);
121          if (pAudioThread)  delete pAudioThread;              }
122          if (pDiskThread)   delete pDiskThread;              return;
123          if (pGig)          delete pGig;          }
124          if (pRIFF)         delete pRIFF;          case SIGSEGV:
125          if (pAudioIO)      delete pAudioIO;              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
126                break;
127          printf("LinuxSampler stopped due to SIGINT\n");          case SIGBUS:
128          exit(EXIT_SUCCESS);              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[] =
163          {          {
164              {"numfragments",1,0,0},              {"numfragments",1,0,0},
165              {"fragmentsize",1,0,0},              {"fragmentsize",1,0,0},
166                {"volume",1,0,0},
167              {"dls",0,0,0},              {"dls",0,0,0},
168              {"gig",0,0,0},              {"gig",0,0,0},
169                {"instrument",1,0,0},
170                {"inputclient",1,0,0},
171                {"alsaout",1,0,0},
172                {"jackout",1,0,0},
173                {"samplerate",1,0,0},
174                {"server",0,0,0},
175              {"help",0,0,0},              {"help",0,0,0},
176              {0,0,0,0}              {0,0,0,0}
177          };          };
# Line 171  void parse_options(int argc, char **argv Line 181  void parse_options(int argc, char **argv
181          if(res == -1) break;          if(res == -1) break;
182          if (res == 0) {          if (res == 0) {
183              switch(option_index) {              switch(option_index) {
184                  case 0:                  case 0: // --numfragments
185                      num_fragments = atoi(optarg);                      num_fragments = atoi(optarg);
186                      break;                      break;
187                  case 1:                  case 1: // --fragmentsize
188                      fragmentsize = atoi(optarg);                      fragmentsize = atoi(optarg);
189                      break;                      break;
190                  case 2:                  case 2: // --volume
191                        volume = atof(optarg);
192                        break;
193                    case 3: // --dls
194                      patch_format = patch_format_dls;                      patch_format = patch_format_dls;
195                      break;                      break;
196                  case 3:                  case 4: // --gig
197                      patch_format = patch_format_gig;                      patch_format = patch_format_gig;
198                      break;                      break;
199                  case 4:                  case 5: // --instrument
200                        instrument_index = atoi(optarg);
201                        break;
202                    case 6: // --inputclient
203                        input_client = optarg;
204                        break;
205                    case 7: // --alsaout
206                        alsaout = optarg;
207                        use_jack = false; // If this option is specified do not connect to jack
208                        break;
209                    case 8: { // --jackout
210                        try {
211                            String arg(optarg);
212                            // remove outer apostrophes
213                            arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1));
214                            // split in two arguments
215                            jack_playback[0] = arg.substr(0, arg.find("\' "));
216                            jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2));
217                            // remove inner apostrophes
218                            jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\''));
219                            jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\''));
220                            // this is the default but set it up anyway in case alsa_card was also used.
221                            use_jack = true;
222                        }
223                        catch (...) {
224                            fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg);
225                            exit(EXIT_FAILURE);
226                        }
227                        break;
228                    }
229                    case 9: // --samplerate
230                        samplerate = atoi(optarg);
231                        break;
232                    case 10: // --server
233                        run_server = true;
234                        break;
235                    case 11: // --help
236                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");
237                        printf("--gig              loads a Gigasampler instrument\n");
238                        printf("--dls              loads a DLS instrument\n");
239                        printf("--instrument       index of the instrument in the instrument file if it\n");
240                        printf("                   contains more than one (default: 0)\n");
241                      printf("--numfragments     sets the number of audio fragments\n");                      printf("--numfragments     sets the number of audio fragments\n");
242                      printf("--fragmentsize     sets the fragment size\n");                      printf("--fragmentsize     sets the fragment size\n");
243                      printf("--dls              loads a DLS instrument\n");                      printf("--volume           sets global volume gain factor (a value > 1.0 means\n");
244                      printf("--gig              loads a Gigasampler instrument\n");                      printf("                   amplification, a value < 1.0 means attenuation,\n");
245                      exit(0);                      printf("                   default: 0.25)\n");
246                        printf("--inputclient      connects to an Alsa sequencer input client on startup\n");
247                        printf("                   (e.g. 64:0 to connect to a client with ID 64 and port 0)\n");
248                        printf("--alsaout          connects to the given Alsa sound device on startup\n");
249                        printf("                   (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n");
250                        printf("--jackout          connects to the given Jack playback ports on startup\n");
251                        printf("                   (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n");
252                        printf("                   in case of stereo output)\n");
253                        printf("--samplerate       sets sample rate if supported by audio output system\n");
254                        printf("                   (e.g. 44100)\n");
255                        printf("--server           launch network server for remote control\n");
256                        exit(EXIT_SUCCESS);
257                      break;                      break;
258              }              }
259          }          }
260      }      }
261  }  }*/

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

  ViewVC Help
Powered by ViewVC