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

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

  ViewVC Help
Powered by ViewVC