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

Legend:
Removed from v.31  
changed lines
  Added in v.207

  ViewVC Help
Powered by ViewVC