--- linuxsampler/trunk/src/linuxsampler.cpp 2003/11/11 23:30:47 10 +++ linuxsampler/trunk/src/linuxsampler.cpp 2004/03/05 13:46:15 35 @@ -25,15 +25,18 @@ #include #include #include +#include #include "global.h" -#include "audioio.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" +#include "network/lscpserver.h" #define AUDIO_CHANNELS 2 // stereo #define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 @@ -44,96 +47,93 @@ patch_format_unknown, patch_format_gig, patch_format_dls -} patch_format; +} patch_format = patch_format_unknown; -AudioIO* pAudioIO; -DiskThread* pDiskThread; -AudioThread* pAudioThread; -MidiIn* pMidiInThread; - -RIFF::File* pRIFF; -gig::File* pGig; -gig::Instrument* pInstrument; +AudioIO* pAudioIO = NULL; +MidiIn* pMidiInThread = NULL; +LSCPServer* pLSCPServer = NULL; +AudioThread* pEngine = NULL; +uint instrument_index = 0; +double volume = 0.25; +int num_fragments = AUDIO_FRAGMENTS; +int fragmentsize = AUDIO_FRAGMENTSIZE; +uint samplerate = AUDIO_SAMPLERATE; +String input_client; +String alsaout = "0,0"; // default card +String jack_playback[2] = { "", "" }; +bool use_jack = true; +bool run_server = false; +pthread_t signalhandlerthread; void parse_options(int argc, char **argv); void signal_handler(int signal); -int midi_non_blocking; -int num_fragments; -int fragmentsize; -bool instrument_is_DLS; -bool instruemtn_is_gig; -char midi_device[40]; - int main(int argc, char **argv) { - pAudioIO = NULL; - pRIFF = NULL; - pGig = NULL; // setting signal handler for catching SIGINT (thus e.g. ) + signalhandlerthread = pthread_self(); signal(SIGINT, signal_handler); - patch_format = patch_format_unknown; - midi_non_blocking = 1; - num_fragments = AUDIO_FRAGMENTS; - fragmentsize = AUDIO_FRAGMENTSIZE; - strcpy(midi_device, "/dev/midi00"); - // parse and assign command line options parse_options(argc, argv); if (patch_format != patch_format_gig) { - printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig\n"); - printf("to load a .gig file!\n"); + printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); + printf("Use 'linuxsampler --help' to see all available options.\n"); return EXIT_FAILURE; } - dmsg(("Initializing audio output...")); - pAudioIO = new AudioIO(); - int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); - if (error) return EXIT_FAILURE; - dmsg(("OK\n")); + int error = 1; +#if HAVE_JACK + if (use_jack) { + dmsg(1,("Initializing audio output (Jack)...")); + pAudioIO = new JackIO(); + error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback); + if (error) dmsg(1,("Trying Alsa output instead.\n")); + } +#endif // HAVE_JACK + if (error) { + dmsg(1,("Initializing audio output (Alsa)...")); + pAudioIO = new AlsaIO(); + int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); + if (error) return EXIT_FAILURE; + } + dmsg(1,("OK\n")); + + AudioThread* pEngine = new AudioThread(pAudioIO); + MidiIn* pMidiInThread = new MidiIn(pEngine); // Loading gig file - try { - printf("Loading gig file..."); - fflush(stdout); - pRIFF = new RIFF::File(argv[argc - 1]); - pGig = new gig::File(pRIFF); - pInstrument = pGig->GetFirstInstrument(); - pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part - printf("OK\n"); - fflush(stdout); - } - catch (RIFF::Exception e) { - e.PrintMessage(); - return EXIT_FAILURE; - } - catch (...) { - printf("Unknown exception while trying to parse gig file.\n"); - return EXIT_FAILURE; - } + result_t result = pEngine->LoadInstrument(argv[argc - 1], instrument_index); + if (result.type == result_type_error) return EXIT_FAILURE; + pEngine->Volume = volume; - DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo - AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); - MidiIn* pMidiInThread = new MidiIn(pAudioThread); - - dmsg(("Starting disk thread...")); - pDiskThread->StartThread(); - dmsg(("OK\n")); - dmsg(("Starting MIDI in thread...")); + dmsg(1,("Starting MIDI in thread...")); + if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); pMidiInThread->StartThread(); - dmsg(("OK\n")); + dmsg(1,("OK\n")); sleep(1); - dmsg(("Starting audio thread...")); - pAudioThread->StartThread(); - dmsg(("OK\n")); + + dmsg(1,("Starting audio thread...")); + pAudioIO->AssignEngine(pEngine); + pAudioIO->Activate(); + dmsg(1,("OK\n")); + + if (run_server) { + dmsg(1,("Starting network server...")); + pLSCPServer = new LSCPServer(pEngine); + pLSCPServer->StartThread(); + dmsg(1,("OK\n")); + } printf("LinuxSampler initialization completed.\n"); while(true) { - 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", + pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, + pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); + fflush(stdout); usleep(500000); } @@ -141,20 +141,14 @@ } void signal_handler(int signal) { - if (signal == SIGINT) { + if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { // stop all threads + if (pAudioIO) pAudioIO->Close(); if (pMidiInThread) pMidiInThread->StopThread(); - if (pAudioThread) pAudioThread->StopThread(); - if (pDiskThread) pDiskThread->StopThread(); - - 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 (pEngine) delete pEngine; if (pAudioIO) delete pAudioIO; printf("LinuxSampler stopped due to SIGINT\n"); @@ -169,8 +163,15 @@ { {"numfragments",1,0,0}, {"fragmentsize",1,0,0}, + {"volume",1,0,0}, {"dls",0,0,0}, {"gig",0,0,0}, + {"instrument",1,0,0}, + {"inputclient",1,0,0}, + {"alsaout",1,0,0}, + {"jackout",1,0,0}, + {"samplerate",1,0,0}, + {"server",0,0,0}, {"help",0,0,0}, {0,0,0,0} }; @@ -180,25 +181,79 @@ if(res == -1) break; if (res == 0) { switch(option_index) { - case 0: + case 0: // --numfragments num_fragments = atoi(optarg); break; - case 1: + case 1: // --fragmentsize fragmentsize = atoi(optarg); break; - case 2: + case 2: // --volume + volume = atof(optarg); + break; + case 3: // --dls patch_format = patch_format_dls; break; - case 3: + case 4: // --gig patch_format = patch_format_gig; break; - case 4: + case 5: // --instrument + instrument_index = atoi(optarg); + break; + case 6: // --inputclient + input_client = optarg; + break; + case 7: // --alsaout + alsaout = optarg; + use_jack = false; // If this option is specified do not connect to jack + break; + case 8: { // --jackout + try { + String arg(optarg); + // remove outer apostrophes + arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1)); + // split in two arguments + jack_playback[0] = arg.substr(0, arg.find("\' ")); + jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2)); + // remove inner apostrophes + jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\'')); + jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\'')); + // this is the default but set it up anyway in case alsa_card was also used. + use_jack = true; + } + catch (...) { + fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg); + exit(EXIT_FAILURE); + } + break; + } + case 9: // --samplerate + samplerate = atoi(optarg); + break; + case 10: // --server + run_server = true; + break; + case 11: // --help printf("usage: linuxsampler [OPTIONS] \n\n"); + printf("--gig loads a Gigasampler instrument\n"); + printf("--dls loads a DLS instrument\n"); + printf("--instrument index of the instrument in the instrument file if it\n"); + printf(" contains more than one (default: 0)\n"); printf("--numfragments sets the number of audio fragments\n"); printf("--fragmentsize sets the fragment size\n"); - printf("--dls loads a DLS instrument\n"); - printf("--gig loads a Gigasampler instrument\n"); - exit(0); + printf("--volume sets global volume gain factor (a value > 1.0 means\n"); + printf(" amplification, a value < 1.0 means attenuation,\n"); + printf(" default: 0.25)\n"); + printf("--inputclient connects to an Alsa sequencer input client on startup\n"); + printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); + printf("--alsaout connects to the given Alsa sound device on startup\n"); + printf(" (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n"); + printf("--jackout connects to the given Jack playback ports on startup\n"); + printf(" (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n"); + printf(" in case of stereo output)\n"); + printf("--samplerate sets sample rate if supported by audio output system\n"); + printf(" (e.g. 44100)\n"); + printf("--server launch network server for remote control\n"); + exit(EXIT_SUCCESS); break; } }