/[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 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>
 #include <pthread.h>  
25    
26  #include <string>  #include "Sampler.h"
27    #include "audiodriver/AudioOutputDeviceFactory.h"
28  #include "global.h"  #include "network/lscpserver.h"
 #include "diskthread.h"  
 #include "audiothread.h"  
 #include "alsaio.h"  
 #include "jackio.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    using namespace LinuxSampler;
38    
39  enum patch_format_t {  /*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;  
 uint             instrument_index;  
 double           volume;  
 int              num_fragments;  
 int              fragmentsize;  
 std::string      input_client;  
 pthread_t        signalhandlerthread;  
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    
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();      signalhandlerthread = pthread_self();
68      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
69    
     patch_format      = patch_format_unknown;  
     instrument_index  = 0;  
     num_fragments     = AUDIO_FRAGMENTS;  
     fragmentsize      = AUDIO_FRAGMENTSIZE;  
     volume            = 0.25; // default volume  
   
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 to load a .gig file!\n");          printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n");
75          printf("Use 'linuxsampler --help' to see all available options.\n");          printf("Use 'linuxsampler --help' to see all available options.\n");
76          return EXIT_FAILURE;          return EXIT_FAILURE;
77      }      }*/
78    
79  #if HAVE_JACK  
80      dmsg(1,("Initializing audio output (Jack)..."));      // create LinuxSampler instance
81      pAudioIO = new JackIO();      dmsg(1,("Creating Sampler..."));
82      int error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS);      pSampler = new Sampler;
     if (error) {  
         dmsg(1,("Trying Alsa output instead.\n"));  
         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;  
83      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
 #endif // HAVE_JACK  
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->GetInstrument(instrument_index);          dmsg(1,("Creating audio output device (Jack)..."));
92          if (!pInstrument) {          try {
93              printf("there's no instrument with index %d.\n", instrument_index);              pSampler->CreateAudioOutputDevice(audio_output_type_jack);
94              exit(EXIT_FAILURE);              no_jack = false;
95          }          }
96          pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part          catch (AudioOutputException aoe) {
97          printf("OK\n");              aoe.PrintMessage();
98          fflush(stdout);              dmsg(1,("Trying to create Alsa output device instead.\n"));
99      }          }
     catch (RIFF::Exception e) {  
         e.PrintMessage();  
         return EXIT_FAILURE;  
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->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //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();
     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();  
119      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
120    
121      printf("LinuxSampler initialization completed.\n");      printf("LinuxSampler initialization completed.\n");
122    
123      while(true)  {      while(true)  {
124        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",
125              pAudioThread->ActiveVoiceCount, pAudioThread->ActiveVoiceCountMax,              pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,
126              pDiskThread->ActiveStreamCount, pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());              pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
127        fflush(stdout);        fflush(stdout);*/
128        usleep(500000);        usleep(500000);
129      }      }
130    
# Line 169  int main(int argc, char **argv) { Line 133  int main(int argc, char **argv) {
133    
134  void signal_handler(int signal) {  void signal_handler(int signal) {
135      if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) {      if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) {
136          // stop all threads          if (pLSCPServer) {
137          if (pAudioIO)      pAudioIO->Close();              pLSCPServer->StopThread();
138          if (pMidiInThread) pMidiInThread->StopThread();              delete pLSCPServer;
139          if (pDiskThread)   pDiskThread->StopThread();          }
140            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;  
   
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[] =
# Line 199  void parse_options(int argc, char **argv Line 155  void parse_options(int argc, char **argv
155              {"gig",0,0,0},              {"gig",0,0,0},
156              {"instrument",1,0,0},              {"instrument",1,0,0},
157              {"inputclient",1,0,0},              {"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 208  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);                      volume = atof(optarg);
179                      break;                      break;
180                  case 3:                  case 3: // --dls
181                      patch_format = patch_format_dls;                      patch_format = patch_format_dls;
182                      break;                      break;
183                  case 4:                  case 4: // --gig
184                      patch_format = patch_format_gig;                      patch_format = patch_format_gig;
185                      break;                      break;
186                  case 5:                  case 5: // --instrument
187                      instrument_index = atoi(optarg);                      instrument_index = atoi(optarg);
188                      break;                      break;
189                  case 6:                  case 6: // --inputclient
190                      input_client = optarg;                      input_client = optarg;
191                      break;                      break;
192                  case 7:                  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");                      printf("--gig              loads a Gigasampler instrument\n");
225                      printf("--dls              loads a DLS instrument\n");                      printf("--dls              loads a DLS instrument\n");
# Line 242  void parse_options(int argc, char **argv Line 232  void parse_options(int argc, char **argv
232                      printf("                   default: 0.25)\n");                      printf("                   default: 0.25)\n");
233                      printf("--inputclient      connects to an Alsa sequencer input client on startup\n");                      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");                      printf("                   (e.g. 64:0 to connect to a client with ID 64 and port 0)\n");
235                      exit(0);                      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.31  
changed lines
  Added in v.123

  ViewVC Help
Powered by ViewVC