/[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 123 by schoenebeck, Mon Jun 14 19:33:16 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 "audiodriver/AudioOutputDeviceFactory.h"
28  #include "diskthread.h"  #include "network/lscpserver.h"
 #include "audiothread.h"  
 #include "midiin.h"  
 #include "stream.h"  
 #include "RIFF.h"  
 #include "gig.h"  
29    
30    #if 0
31  #define AUDIO_CHANNELS          2     // stereo  #define AUDIO_CHANNELS          2     // stereo
32  #define AUDIO_FRAGMENTS         3     // 3 fragments, if it does not work set it to 2  #define AUDIO_FRAGMENTS         3     // 3 fragments, if it does not work set it to 2
33  #define AUDIO_FRAGMENTSIZE      512   // each fragment has 512 frames  #define AUDIO_FRAGMENTSIZE      512   // each fragment has 512 frames
34  #define AUDIO_SAMPLERATE        44100 // Hz  #define AUDIO_SAMPLERATE        44100 // Hz
35    #endif
36    
37  enum patch_format_t {  using namespace LinuxSampler;
38    
39    /*enum patch_format_t {
40      patch_format_unknown,      patch_format_unknown,
41      patch_format_gig,      patch_format_gig,
42      patch_format_dls      patch_format_dls
43  } patch_format;  } patch_format = patch_format_unknown;*/
44    
45    Sampler*     pSampler         = NULL;
46    LSCPServer*  pLSCPServer      = NULL;
47    pthread_t    signalhandlerthread;
48    /*AudioThread* pEngine          = NULL;
49    uint         instrument_index = 0;
50    double       volume           = 0.25;
51    int          num_fragments    = AUDIO_FRAGMENTS;
52    int          fragmentsize     = AUDIO_FRAGMENTSIZE;
53    uint         samplerate       = AUDIO_SAMPLERATE;
54    String       input_client;
55    String       alsaout          = "0,0"; // default card
56    String       jack_playback[2] = { "", "" };
57    bool         use_jack         = true;
58    bool         run_server       = false;*/
59    
 AudioIO*         pAudioIO;  
 DiskThread*      pDiskThread;  
 AudioThread*     pAudioThread;  
 MidiIn*          pMidiInThread;  
   
 RIFF::File*      pRIFF;  
 gig::File*       pGig;  
 gig::Instrument* pInstrument;  
60    
61  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
62  void signal_handler(int signal);  void signal_handler(int signal);
63    
 int midi_non_blocking;  
 int num_fragments;  
 int fragmentsize;  
 bool instrument_is_DLS;  
 bool instruemtn_is_gig;  
 char midi_device[40];  
   
64  int main(int argc, char **argv) {  int main(int argc, char **argv) {
     pAudioIO = NULL;  
     pRIFF    = NULL;  
     pGig     = NULL;  
65    
66      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
67        signalhandlerthread = pthread_self();
68      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
69    
     patch_format      = patch_format_unknown;  
     midi_non_blocking = 1;  
     num_fragments     = AUDIO_FRAGMENTS;  
     fragmentsize      = AUDIO_FRAGMENTSIZE;  
     strcpy(midi_device, "/dev/midi00");  
   
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) {      /*if (patch_format != patch_format_gig) {
74          printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig\n");          printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n");
75          printf("to load a .gig file!\n");          printf("Use 'linuxsampler --help' to see all available options.\n");
76          return EXIT_FAILURE;          return EXIT_FAILURE;
77      }      }*/
78    
79      dmsg(("Initializing audio output..."));  
80      pAudioIO = new AudioIO();      // create LinuxSampler instance
81      int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize);      dmsg(1,("Creating Sampler..."));
82      if (error) return EXIT_FAILURE;      pSampler = new Sampler;
83      dmsg(("OK\n"));      dmsg(1,("OK\n"));
84    
85      // Loading gig file      dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
86      try {  
87          printf("Loading gig file...");      // create an audio output device
88          fflush(stdout);     /* bool no_jack = true;
89          pRIFF       = new RIFF::File(argv[argc - 1]);  #if HAVE_JACK
90          pGig        = new gig::File(pRIFF);      if (use_jack) {
91          pInstrument = pGig->GetFirstInstrument();          dmsg(1,("Creating audio output device (Jack)..."));
92          pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part          try {
93          printf("OK\n");              pSampler->CreateAudioOutputDevice(audio_output_type_jack);
94          fflush(stdout);              no_jack = false;
95      }          }
96      catch (RIFF::Exception e) {          catch (AudioOutputException aoe) {
97          e.PrintMessage();              aoe.PrintMessage();
98          return EXIT_FAILURE;              dmsg(1,("Trying to create Alsa output device instead.\n"));
99            }
100      }      }
101      catch (...) {  #endif // HAVE_JACK
102          printf("Unknown exception while trying to parse gig file.\n");      if (no_jack) {
103          return EXIT_FAILURE;          dmsg(1,("Creating audio output device (Alsa)..."));
104            try {
105                pSampler->CreateAudioOutputDevice(audio_output_type_alsa);
106            }
107            catch (AudioOutputException aoe) {
108                aoe.PrintMessage();
109                dmsg(1,("Trying to create Alsa output device instead.\n"));
110                return EXIT_FAILURE;
111            }
112      }      }
113        dmsg(1,("OK\n"));*/
114    
115      DiskThread*  pDiskThread   = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo      // start LSCP network server
116      AudioThread* pAudioThread  = new AudioThread(pAudioIO, pDiskThread, pInstrument);      dmsg(1,("Starting network server..."));
117      MidiIn*      pMidiInThread = new MidiIn(pAudioThread);      pLSCPServer = new LSCPServer(pSampler);
118        pLSCPServer->StartThread();
119      dmsg(("Starting disk thread..."));      dmsg(1,("OK\n"));
     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"));  
120    
121      printf("LinuxSampler initialization completed.\n");      printf("LinuxSampler initialization completed.\n");
122    
123      while(true) sleep(1000);      while(true)  {
124          /*printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",
125                pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,
126                pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
127          fflush(stdout);*/
128          usleep(500000);
129        }
130    
131      return EXIT_SUCCESS;      return EXIT_SUCCESS;
132  }  }
133    
134  void signal_handler(int signal) {  void signal_handler(int signal) {
135      if (signal == SIGINT) {      if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) {
136          // stop all threads          if (pLSCPServer) {
137          if (pMidiInThread) pMidiInThread->StopThread();              pLSCPServer->StopThread();
138          if (pAudioThread)  pAudioThread->StopThread();              delete pLSCPServer;
139          if (pDiskThread)   pDiskThread->StopThread();          }
140            if (pSampler) delete pSampler;
         sleep(1);  
   
         // free all resources  
         if (pMidiInThread) delete pMidiInThread;  
         if (pAudioThread)  delete pAudioThread;  
         if (pDiskThread)   delete pDiskThread;  
         if (pGig)          delete pGig;  
         if (pRIFF)         delete pRIFF;  
         if (pAudioIO)      delete pAudioIO;  
   
141          printf("LinuxSampler stopped due to SIGINT\n");          printf("LinuxSampler stopped due to SIGINT\n");
142          exit(EXIT_SUCCESS);          exit(EXIT_SUCCESS);
143      }      }
144  }  }
145    
146  void parse_options(int argc, char **argv) {  /*void parse_options(int argc, char **argv) {
147      int res;      int res;
148      int option_index = 0;      int option_index = 0;
149      static struct option long_options[] =      static struct option long_options[] =
150          {          {
151              {"numfragments",1,0,0},              {"numfragments",1,0,0},
152              {"fragmentsize",1,0,0},              {"fragmentsize",1,0,0},
153                {"volume",1,0,0},
154              {"dls",0,0,0},              {"dls",0,0,0},
155              {"gig",0,0,0},              {"gig",0,0,0},
156                {"instrument",1,0,0},
157                {"inputclient",1,0,0},
158                {"alsaout",1,0,0},
159                {"jackout",1,0,0},
160                {"samplerate",1,0,0},
161                {"server",0,0,0},
162              {"help",0,0,0},              {"help",0,0,0},
163              {0,0,0,0}              {0,0,0,0}
164          };          };
# Line 176  void parse_options(int argc, char **argv Line 168  void parse_options(int argc, char **argv
168          if(res == -1) break;          if(res == -1) break;
169          if (res == 0) {          if (res == 0) {
170              switch(option_index) {              switch(option_index) {
171                  case 0:                  case 0: // --numfragments
172                      num_fragments = atoi(optarg);                      num_fragments = atoi(optarg);
173                      break;                      break;
174                  case 1:                  case 1: // --fragmentsize
175                      fragmentsize = atoi(optarg);                      fragmentsize = atoi(optarg);
176                      break;                      break;
177                  case 2:                  case 2: // --volume
178                        volume = atof(optarg);
179                        break;
180                    case 3: // --dls
181                      patch_format = patch_format_dls;                      patch_format = patch_format_dls;
182                      break;                      break;
183                  case 3:                  case 4: // --gig
184                      patch_format = patch_format_gig;                      patch_format = patch_format_gig;
185                      break;                      break;
186                  case 4:                  case 5: // --instrument
187                        instrument_index = atoi(optarg);
188                        break;
189                    case 6: // --inputclient
190                        input_client = optarg;
191                        break;
192                    case 7: // --alsaout
193                        alsaout = optarg;
194                        use_jack = false; // If this option is specified do not connect to jack
195                        break;
196                    case 8: { // --jackout
197                        try {
198                            String arg(optarg);
199                            // remove outer apostrophes
200                            arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1));
201                            // split in two arguments
202                            jack_playback[0] = arg.substr(0, arg.find("\' "));
203                            jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2));
204                            // remove inner apostrophes
205                            jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\''));
206                            jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\''));
207                            // this is the default but set it up anyway in case alsa_card was also used.
208                            use_jack = true;
209                        }
210                        catch (...) {
211                            fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg);
212                            exit(EXIT_FAILURE);
213                        }
214                        break;
215                    }
216                    case 9: // --samplerate
217                        samplerate = atoi(optarg);
218                        break;
219                    case 10: // --server
220                        run_server = true;
221                        break;
222                    case 11: // --help
223                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");
224                        printf("--gig              loads a Gigasampler instrument\n");
225                        printf("--dls              loads a DLS instrument\n");
226                        printf("--instrument       index of the instrument in the instrument file if it\n");
227                        printf("                   contains more than one (default: 0)\n");
228                      printf("--numfragments     sets the number of audio fragments\n");                      printf("--numfragments     sets the number of audio fragments\n");
229                      printf("--fragmentsize     sets the fragment size\n");                      printf("--fragmentsize     sets the fragment size\n");
230                      printf("--dls              loads a DLS instrument\n");                      printf("--volume           sets global volume gain factor (a value > 1.0 means\n");
231                      printf("--gig              loads a Gigasampler instrument\n");                      printf("                   amplification, a value < 1.0 means attenuation,\n");
232                      exit(0);                      printf("                   default: 0.25)\n");
233                        printf("--inputclient      connects to an Alsa sequencer input client on startup\n");
234                        printf("                   (e.g. 64:0 to connect to a client with ID 64 and port 0)\n");
235                        printf("--alsaout          connects to the given Alsa sound device on startup\n");
236                        printf("                   (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n");
237                        printf("--jackout          connects to the given Jack playback ports on startup\n");
238                        printf("                   (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n");
239                        printf("                   in case of stereo output)\n");
240                        printf("--samplerate       sets sample rate if supported by audio output system\n");
241                        printf("                   (e.g. 44100)\n");
242                        printf("--server           launch network server for remote control\n");
243                        exit(EXIT_SUCCESS);
244                      break;                      break;
245              }              }
246          }          }
247      }      }
248  }  }*/

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

  ViewVC Help
Powered by ViewVC