/[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 271 by schoenebeck, Fri Oct 8 20:51:39 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"  
32  #include "RIFF.h"  using namespace LinuxSampler;
33  #include "gig.h"  
34    Sampler*    pSampler    = NULL;
35  #define AUDIO_CHANNELS          2     // stereo  LSCPServer* pLSCPServer = NULL;
36  #define AUDIO_FRAGMENTS         3     // 3 fragments, if it does not work set it to 2  pthread_t   main_thread;
 #define AUDIO_FRAGMENTSIZE      512   // each fragment has 512 frames  
 #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;  
37    
38  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
39  void signal_handler(int signal);  void signal_handler(int signal);
40    void kill_app();
 int midi_non_blocking;  
 int num_fragments;  
 int fragmentsize;  
 bool instrument_is_DLS;  
 bool instruemtn_is_gig;  
 char midi_device[40];  
41    
42  int main(int argc, char **argv) {  int main(int argc, char **argv) {
43      pAudioIO = NULL;  
44      pRIFF    = NULL;      // initialize the stack trace mechanism with our binary file
45      pGig     = NULL;      StackTraceInit(argv[0], -1);
46    
47        main_thread = pthread_self();
48    
49      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
50      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
51    
52      patch_format      = patch_format_unknown;      // register signal handler for all unusual signals
53      midi_non_blocking = 1;      // (we will print the stack trace and exit)
54      num_fragments     = AUDIO_FRAGMENTS;      struct sigaction sact;
55      fragmentsize      = AUDIO_FRAGMENTSIZE;      sigemptyset(&sact.sa_mask);
56      strcpy(midi_device, "/dev/midi00");      sact.sa_flags   = 0;
57        sact.sa_handler = signal_handler;
58        sigaction(SIGSEGV, &sact, NULL);
59        sigaction(SIGBUS,  &sact, NULL);
60        sigaction(SIGILL,  &sact, NULL);
61        sigaction(SIGFPE,  &sact, NULL);
62        sigaction(SIGUSR1, &sact, NULL);
63        sigaction(SIGUSR2, &sact, NULL);
64    
65      // parse and assign command line options      // parse and assign command line options
66      parse_options(argc, argv);      //parse_options(argc, argv);
67    
68      if (patch_format != patch_format_gig) {      dmsg(1,("LinuxSampler %s\n", VERSION));
69          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"));
         printf("to load a .gig file!\n");  
         return EXIT_FAILURE;  
     }  
   
     dmsg(("Initializing audio output..."));  
     pAudioIO = new AudioIO();  
     int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize);  
     if (error) return EXIT_FAILURE;  
     dmsg(("OK\n"));  
   
     // Loading gig file  
     try {  
         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;  
     }  
70    
71      DiskThread*  pDiskThread   = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo      // create LinuxSampler instance
72      AudioThread* pAudioThread  = new AudioThread(pAudioIO, pDiskThread, pInstrument);      dmsg(1,("Creating Sampler..."));
73      MidiIn*      pMidiInThread = new MidiIn(pAudioThread);      pSampler = new Sampler;
74        dmsg(1,("OK\n"));
75      dmsg(("Starting disk thread..."));  
76      pDiskThread->StartThread();      dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
77      dmsg(("OK\n"));      dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
78      dmsg(("Starting MIDI in thread..."));  
79      pMidiInThread->StartThread();      // start LSCP network server
80      dmsg(("OK\n"));      dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT));
81        pLSCPServer = new LSCPServer(pSampler);
82      sleep(1);      pLSCPServer->StartThread();
83      dmsg(("Starting audio thread..."));      pLSCPServer->WaitUntilInitialized();
84      pAudioThread->StartThread();      dmsg(1,("OK\n"));
     dmsg(("OK\n"));  
85    
86      printf("LinuxSampler initialization completed.\n");      printf("LinuxSampler initialization completed.\n");
87    
88      while(true) sleep(1000);      while(true)  {
89          /*printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",
90                pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,
91                pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
92          fflush(stdout);*/
93          usleep(500000);
94        }
95    
96      return EXIT_SUCCESS;      return EXIT_SUCCESS;
97  }  }
98    
99  void signal_handler(int signal) {  void signal_handler(int iSignal) {
100      if (signal == SIGINT) {      switch (iSignal) {
101          // stop all threads          case SIGINT: {
102          if (pMidiInThread) pMidiInThread->StopThread();              if (pthread_equal(pthread_self(), main_thread)) {
103          if (pAudioThread)  pAudioThread->StopThread();                  if (pLSCPServer) {
104          if (pDiskThread)   pDiskThread->StopThread();                      pLSCPServer->StopThread();
105                        delete pLSCPServer;
106          sleep(1);                  }
107                    if (pSampler) delete pSampler;
108          // free all resources                  printf("LinuxSampler stopped due to SIGINT.\n");
109          if (pMidiInThread) delete pMidiInThread;                  exit(EXIT_SUCCESS);
110          if (pAudioThread)  delete pAudioThread;              }
111          if (pDiskThread)   delete pDiskThread;              return;
112          if (pGig)          delete pGig;          }
113          if (pRIFF)         delete pRIFF;          case SIGSEGV:
114          if (pAudioIO)      delete pAudioIO;              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
115                break;
116          printf("LinuxSampler stopped due to SIGINT\n");          case SIGBUS:
117          exit(EXIT_SUCCESS);              std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush;
118                break;
119            case SIGILL:
120                std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush;
121                break;
122            case SIGFPE:
123                std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush;
124                break;
125            case SIGUSR1:
126                std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush;
127                break;
128            case SIGUSR2:
129                std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
130                break;
131            default: { // this should never happen, as we register for the signals we want
132                std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
133                break;
134            }
135      }      }
136        signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
137        std::cerr << "Showing stack trace...\n" << std::flush;
138        StackTrace();
139        sleep(2);
140        std::cerr << "Killing LinuxSampler...\n" << std::flush;
141        kill_app(); // Use abort() if we want to generate a core dump.
142  }  }
143    
144  void parse_options(int argc, char **argv) {  void kill_app() {
145        kill(main_thread, SIGKILL);
146    }
147    
148    /*void parse_options(int argc, char **argv) {
149      int res;      int res;
150      int option_index = 0;      int option_index = 0;
151      static struct option long_options[] =      static struct option long_options[] =
152          {          {
153              {"numfragments",1,0,0},              {"numfragments",1,0,0},
154              {"fragmentsize",1,0,0},              {"fragmentsize",1,0,0},
155                {"volume",1,0,0},
156              {"dls",0,0,0},              {"dls",0,0,0},
157              {"gig",0,0,0},              {"gig",0,0,0},
158                {"instrument",1,0,0},
159                {"inputclient",1,0,0},
160                {"alsaout",1,0,0},
161                {"jackout",1,0,0},
162                {"samplerate",1,0,0},
163                {"server",0,0,0},
164              {"help",0,0,0},              {"help",0,0,0},
165              {0,0,0,0}              {0,0,0,0}
166          };          };
# Line 176  void parse_options(int argc, char **argv Line 170  void parse_options(int argc, char **argv
170          if(res == -1) break;          if(res == -1) break;
171          if (res == 0) {          if (res == 0) {
172              switch(option_index) {              switch(option_index) {
173                  case 0:                  case 0: // --numfragments
174                      num_fragments = atoi(optarg);                      num_fragments = atoi(optarg);
175                      break;                      break;
176                  case 1:                  case 1: // --fragmentsize
177                      fragmentsize = atoi(optarg);                      fragmentsize = atoi(optarg);
178                      break;                      break;
179                  case 2:                  case 2: // --volume
180                        volume = atof(optarg);
181                        break;
182                    case 3: // --dls
183                      patch_format = patch_format_dls;                      patch_format = patch_format_dls;
184                      break;                      break;
185                  case 3:                  case 4: // --gig
186                      patch_format = patch_format_gig;                      patch_format = patch_format_gig;
187                      break;                      break;
188                  case 4:                  case 5: // --instrument
189                        instrument_index = atoi(optarg);
190                        break;
191                    case 6: // --inputclient
192                        input_client = optarg;
193                        break;
194                    case 7: // --alsaout
195                        alsaout = optarg;
196                        use_jack = false; // If this option is specified do not connect to jack
197                        break;
198                    case 8: { // --jackout
199                        try {
200                            String arg(optarg);
201                            // remove outer apostrophes
202                            arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1));
203                            // split in two arguments
204                            jack_playback[0] = arg.substr(0, arg.find("\' "));
205                            jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2));
206                            // remove inner apostrophes
207                            jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\''));
208                            jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\''));
209                            // this is the default but set it up anyway in case alsa_card was also used.
210                            use_jack = true;
211                        }
212                        catch (...) {
213                            fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg);
214                            exit(EXIT_FAILURE);
215                        }
216                        break;
217                    }
218                    case 9: // --samplerate
219                        samplerate = atoi(optarg);
220                        break;
221                    case 10: // --server
222                        run_server = true;
223                        break;
224                    case 11: // --help
225                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");
226                        printf("--gig              loads a Gigasampler instrument\n");
227                        printf("--dls              loads a DLS instrument\n");
228                        printf("--instrument       index of the instrument in the instrument file if it\n");
229                        printf("                   contains more than one (default: 0)\n");
230                      printf("--numfragments     sets the number of audio fragments\n");                      printf("--numfragments     sets the number of audio fragments\n");
231                      printf("--fragmentsize     sets the fragment size\n");                      printf("--fragmentsize     sets the fragment size\n");
232                      printf("--dls              loads a DLS instrument\n");                      printf("--volume           sets global volume gain factor (a value > 1.0 means\n");
233                      printf("--gig              loads a Gigasampler instrument\n");                      printf("                   amplification, a value < 1.0 means attenuation,\n");
234                      exit(0);                      printf("                   default: 0.25)\n");
235                        printf("--inputclient      connects to an Alsa sequencer input client on startup\n");
236                        printf("                   (e.g. 64:0 to connect to a client with ID 64 and port 0)\n");
237                        printf("--alsaout          connects to the given Alsa sound device on startup\n");
238                        printf("                   (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n");
239                        printf("--jackout          connects to the given Jack playback ports on startup\n");
240                        printf("                   (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n");
241                        printf("                   in case of stereo output)\n");
242                        printf("--samplerate       sets sample rate if supported by audio output system\n");
243                        printf("                   (e.g. 44100)\n");
244                        printf("--server           launch network server for remote control\n");
245                        exit(EXIT_SUCCESS);
246                      break;                      break;
247              }              }
248          }          }
249      }      }
250  }  }*/

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

  ViewVC Help
Powered by ViewVC