/[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 12 by schoenebeck, Sun Nov 16 19:01:50 2003 UTC revision 33 by schoenebeck, Mon Feb 16 19:30:42 2004 UTC
# Line 25  Line 25 
25  #include <unistd.h>  #include <unistd.h>
26  #include <getopt.h>  #include <getopt.h>
27  #include <signal.h>  #include <signal.h>
28    #include <pthread.h>
29    
30  #include "global.h"  #include "global.h"
 #include "audioio.h"  
31  #include "diskthread.h"  #include "diskthread.h"
32  #include "audiothread.h"  #include "audiothread.h"
33    #include "alsaio.h"
34    #include "jackio.h"
35  #include "midiin.h"  #include "midiin.h"
36  #include "stream.h"  #include "stream.h"
37  #include "RIFF.h"  #include "RIFF.h"
# Line 53  MidiIn*          pMidiInThread; Line 55  MidiIn*          pMidiInThread;
55  RIFF::File*      pRIFF;  RIFF::File*      pRIFF;
56  gig::File*       pGig;  gig::File*       pGig;
57  gig::Instrument* pInstrument;  gig::Instrument* pInstrument;
58    uint             instrument_index;
59    double           volume;
60  int              num_fragments;  int              num_fragments;
61  int              fragmentsize;  int              fragmentsize;
62    String           input_client;
63    String           alsaout;
64    String           jack_playback[2];
65    bool             use_jack;
66    pthread_t        signalhandlerthread;
67    uint             samplerate;
68    
69  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
70  void signal_handler(int signal);  void signal_handler(int signal);
# Line 65  int main(int argc, char **argv) { Line 75  int main(int argc, char **argv) {
75      pGig     = NULL;      pGig     = NULL;
76    
77      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
78        signalhandlerthread = pthread_self();
79      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
80    
81      patch_format      = patch_format_unknown;      patch_format      = patch_format_unknown;
82        instrument_index  = 0;
83      num_fragments     = AUDIO_FRAGMENTS;      num_fragments     = AUDIO_FRAGMENTS;
84      fragmentsize      = AUDIO_FRAGMENTSIZE;      fragmentsize      = AUDIO_FRAGMENTSIZE;
85        volume            = 0.25; // default volume
86        alsaout           = "0,0"; // default card
87        jack_playback[0]  = "";
88        jack_playback[1]  = "";
89        samplerate        = AUDIO_SAMPLERATE;
90        use_jack          = true;
91    
92      // parse and assign command line options      // parse and assign command line options
93      parse_options(argc, argv);      parse_options(argc, argv);
94    
95      if (patch_format != patch_format_gig) {      if (patch_format != patch_format_gig) {
96          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");
97            printf("Use 'linuxsampler --help' to see all available options.\n");
98          return EXIT_FAILURE;          return EXIT_FAILURE;
99      }      }
100    
101      dmsg(1,("Initializing audio output..."));      int error = 1;
102      pAudioIO = new AudioIO();  #if HAVE_JACK
103      int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize);      if (use_jack) {
104      if (error) return EXIT_FAILURE;          dmsg(1,("Initializing audio output (Jack)..."));
105            pAudioIO = new JackIO();
106            error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback);
107            if (error) dmsg(1,("Trying Alsa output instead.\n"));
108        }
109    #endif // HAVE_JACK
110        if (error) {
111            dmsg(1,("Initializing audio output (Alsa)..."));
112            pAudioIO = new AlsaIO();
113            int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout);
114            if (error) return EXIT_FAILURE;
115        }
116      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
117    
118      // Loading gig file      // Loading gig file
# Line 91  int main(int argc, char **argv) { Line 121  int main(int argc, char **argv) {
121          fflush(stdout);          fflush(stdout);
122          pRIFF       = new RIFF::File(argv[argc - 1]);          pRIFF       = new RIFF::File(argv[argc - 1]);
123          pGig        = new gig::File(pRIFF);          pGig        = new gig::File(pRIFF);
124          pInstrument = pGig->GetFirstInstrument();          pInstrument = pGig->GetInstrument(instrument_index);
125            if (!pInstrument) {
126                printf("there's no instrument with index %d.\n", instrument_index);
127                exit(EXIT_FAILURE);
128            }
129          pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part          pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part
130          printf("OK\n");          printf("OK\n");
131          fflush(stdout);          fflush(stdout);
# Line 105  int main(int argc, char **argv) { Line 139  int main(int argc, char **argv) {
139          return EXIT_FAILURE;          return EXIT_FAILURE;
140      }      }
141    
142      DiskThread*  pDiskThread   = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo      DiskThread*  pDiskThread   = new DiskThread(((pAudioIO->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo
143      AudioThread* pAudioThread  = new AudioThread(pAudioIO, pDiskThread, pInstrument);      AudioThread* pAudioThread  = new AudioThread(pAudioIO, pDiskThread, pInstrument);
144      MidiIn*      pMidiInThread = new MidiIn(pAudioThread);      MidiIn*      pMidiInThread = new MidiIn(pAudioThread);
145    
# Line 113  int main(int argc, char **argv) { Line 147  int main(int argc, char **argv) {
147      pDiskThread->StartThread();      pDiskThread->StartThread();
148      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
149      dmsg(1,("Starting MIDI in thread..."));      dmsg(1,("Starting MIDI in thread..."));
150        if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str());
151      pMidiInThread->StartThread();      pMidiInThread->StartThread();
152      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
153    
154      sleep(1);      sleep(1);
155      dmsg(1,("Starting audio thread..."));      dmsg(1,("Starting audio thread..."));
156      pAudioThread->StartThread();      pAudioThread->Volume = volume;
157        pAudioIO->AssignEngine(pAudioThread);
158        pAudioIO->Activate();
159      dmsg(1,("OK\n"));      dmsg(1,("OK\n"));
160    
161      printf("LinuxSampler initialization completed.\n");      printf("LinuxSampler initialization completed.\n");
162    
163      while(true)  {      while(true)  {
164        printf("Voices: %3.3d  Streams: %3.3d  \r",pAudioThread->ActiveVoiceCount, pDiskThread->ActiveStreamCount); fflush(stdout);        printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",
165                pAudioThread->ActiveVoiceCount, pAudioThread->ActiveVoiceCountMax,
166                pDiskThread->ActiveStreamCount, pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
167          fflush(stdout);
168        usleep(500000);        usleep(500000);
169      }      }
170    
# Line 132  int main(int argc, char **argv) { Line 172  int main(int argc, char **argv) {
172  }  }
173    
174  void signal_handler(int signal) {  void signal_handler(int signal) {
175      if (signal == SIGINT) {      if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) {
176          // stop all threads          // stop all threads
177            if (pAudioIO)      pAudioIO->Close();
178          if (pMidiInThread) pMidiInThread->StopThread();          if (pMidiInThread) pMidiInThread->StopThread();
         if (pAudioThread)  pAudioThread->StopThread();  
179          if (pDiskThread)   pDiskThread->StopThread();          if (pDiskThread)   pDiskThread->StopThread();
180    
         sleep(1);  
   
181          // free all resources          // free all resources
182          if (pMidiInThread) delete pMidiInThread;          if (pMidiInThread) delete pMidiInThread;
183          if (pAudioThread)  delete pAudioThread;          if (pAudioThread)  delete pAudioThread;
# Line 160  void parse_options(int argc, char **argv Line 198  void parse_options(int argc, char **argv
198          {          {
199              {"numfragments",1,0,0},              {"numfragments",1,0,0},
200              {"fragmentsize",1,0,0},              {"fragmentsize",1,0,0},
201                {"volume",1,0,0},
202              {"dls",0,0,0},              {"dls",0,0,0},
203              {"gig",0,0,0},              {"gig",0,0,0},
204                {"instrument",1,0,0},
205                {"inputclient",1,0,0},
206                {"alsaout",1,0,0},
207                {"jackout",1,0,0},
208                {"samplerate",1,0,0},
209              {"help",0,0,0},              {"help",0,0,0},
210              {0,0,0,0}              {0,0,0,0}
211          };          };
# Line 171  void parse_options(int argc, char **argv Line 215  void parse_options(int argc, char **argv
215          if(res == -1) break;          if(res == -1) break;
216          if (res == 0) {          if (res == 0) {
217              switch(option_index) {              switch(option_index) {
218                  case 0:                  case 0: // --numfragments
219                      num_fragments = atoi(optarg);                      num_fragments = atoi(optarg);
220                      break;                      break;
221                  case 1:                  case 1: // --fragmentsize
222                      fragmentsize = atoi(optarg);                      fragmentsize = atoi(optarg);
223                      break;                      break;
224                  case 2:                  case 2: // --volume
225                        volume = atof(optarg);
226                        break;
227                    case 3: // --dls
228                      patch_format = patch_format_dls;                      patch_format = patch_format_dls;
229                      break;                      break;
230                  case 3:                  case 4: // --gig
231                      patch_format = patch_format_gig;                      patch_format = patch_format_gig;
232                      break;                      break;
233                  case 4:                  case 5: // --instrument
234                        instrument_index = atoi(optarg);
235                        break;
236                    case 6: // --inputclient
237                        input_client = optarg;
238                        break;
239                    case 7: // --alsaout
240                        alsaout = optarg;
241                        use_jack = false; // If this option is specified do not connect to jack
242                        break;
243                    case 8: { // --jackout
244                        try {
245                            String arg(optarg);
246                            // remove outer apostrophes
247                            arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1));
248                            // split in two arguments
249                            jack_playback[0] = arg.substr(0, arg.find("\' "));
250                            jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2));
251                            // remove inner apostrophes
252                            jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\''));
253                            jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\''));
254                            // this is the default but set it up anyway in case alsa_card was also used.
255                            use_jack = true;
256                        }
257                        catch (...) {
258                            fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg);
259                            exit(EXIT_FAILURE);
260                        }
261                        break;
262                    }
263                    case 9: // --samplerate
264                        samplerate = atoi(optarg);
265                        break;
266                    case 10: // --help
267                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");                      printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");
268                        printf("--gig              loads a Gigasampler instrument\n");
269                        printf("--dls              loads a DLS instrument\n");
270                        printf("--instrument       index of the instrument in the instrument file if it\n");
271                        printf("                   contains more than one (default: 0)\n");
272                      printf("--numfragments     sets the number of audio fragments\n");                      printf("--numfragments     sets the number of audio fragments\n");
273                      printf("--fragmentsize     sets the fragment size\n");                      printf("--fragmentsize     sets the fragment size\n");
274                      printf("--dls              loads a DLS instrument\n");                      printf("--volume           sets global volume gain factor (a value > 1.0 means\n");
275                      printf("--gig              loads a Gigasampler instrument\n");                      printf("                   amplification, a value < 1.0 means attenuation,\n");
276                      exit(0);                      printf("                   default: 0.25)\n");
277                        printf("--inputclient      connects to an Alsa sequencer input client on startup\n");
278                        printf("                   (e.g. 64:0 to connect to a client with ID 64 and port 0)\n");
279                        printf("--alsaout          connects to the given Alsa sound device on startup\n");
280                        printf("                   (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n");
281                        printf("--jackout          connects to the given Jack playback ports on startup\n");
282                        printf("                   (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n");
283                        printf("                   in case of stereo output)\n");
284                        printf("--samplerate       sets sample rate if supported by audio output system\n");
285                        printf("                   (e.g. 44100)\n");
286                        exit(EXIT_SUCCESS);
287                      break;                      break;
288              }              }
289          }          }

Legend:
Removed from v.12  
changed lines
  Added in v.33

  ViewVC Help
Powered by ViewVC