--- linuxsampler/trunk/src/linuxsampler.cpp 2003/11/05 14:47:10 9 +++ linuxsampler/trunk/src/linuxsampler.cpp 2005/02/09 01:22:18 361 @@ -2,7 +2,7 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -20,142 +20,187 @@ * MA 02111-1307 USA * ***************************************************************************/ -#include -#include -#include #include #include -#include "global.h" -#include "audioio.h" -#include "diskthread.h" -#include "audiothread.h" -#include "midiin.h" -#include "stream.h" -#include "RIFF.h" -#include "gig.h" - -#define AUDIO_CHANNELS 2 // stereo -#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 -#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames -#define AUDIO_SAMPLERATE 44100 // Hz - -enum patch_format_t { - patch_format_unknown, - patch_format_gig, - patch_format_dls -} patch_format; - -AudioIO* pAudioIO; -DiskThread* pDiskThread; -AudioThread* pAudioThread; -MidiIn* pMidiInThread; - -RIFF::File* pRIFF; -gig::File* pGig; -gig::Instrument* pInstrument; +#include "Sampler.h" +#include "drivers/midi/MidiInputDeviceFactory.h" +#include "drivers/audio/AudioOutputDeviceFactory.h" +#include "engines/gig/Profiler.h" +#include "network/lscpserver.h" +#include "common/stacktrace.h" +#include "common/Features.h" + +using namespace LinuxSampler; + +Sampler* pSampler = NULL; +LSCPServer* pLSCPServer = NULL; +pthread_t main_thread; +bool profile = false; +bool tune = true; 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]; +void kill_app(); int main(int argc, char **argv) { - pAudioIO = NULL; - pRIFF = NULL; - pGig = NULL; + + // initialize the stack trace mechanism with our binary file + StackTraceInit(argv[0], -1); + + main_thread = pthread_self(); // setting signal handler for catching SIGINT (thus e.g. ) 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"); + // register signal handler for all unusual signals + // (we will print the stack trace and exit) + struct sigaction sact; + sigemptyset(&sact.sa_mask); + sact.sa_flags = 0; + sact.sa_handler = signal_handler; + sigaction(SIGSEGV, &sact, NULL); + sigaction(SIGBUS, &sact, NULL); + sigaction(SIGILL, &sact, NULL); + sigaction(SIGFPE, &sact, NULL); + sigaction(SIGUSR1, &sact, NULL); + sigaction(SIGUSR2, &sact, NULL); // 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"); - return EXIT_FAILURE; - } + dmsg(1,("LinuxSampler %s\n", VERSION)); + dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n")); - 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")); - - // 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; + if (tune) + { + // detect and print system / CPU specific features + String sFeatures; + Features::detect(); +#if ARCH_X86 + if (Features::supportsMMX()) sFeatures += " MMX"; + if (Features::supportsSSE()) sFeatures += " SSE"; +#endif // ARCH_X86 + if (!sFeatures.size()) sFeatures = " None"; + dmsg(1,("Detected features:%s\n",sFeatures.c_str())); } - 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...")); - pMidiInThread->StartThread(); - dmsg(("OK\n")); - - sleep(1); - dmsg(("Starting audio thread...")); - pAudioThread->StartThread(); - dmsg(("OK\n")); + // create LinuxSampler instance + dmsg(1,("Creating Sampler...")); + pSampler = new Sampler; + dmsg(1,("OK\n")); + + dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str())); + dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str())); + + // start LSCP network server + dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT)); + pLSCPServer = new LSCPServer(pSampler); + pLSCPServer->StartThread(); + pLSCPServer->WaitUntilInitialized(); + dmsg(1,("OK\n")); + + if (profile) + { + dmsg(1,("Calibrating profiler...")); + gig::Profiler::Calibrate(); + gig::Profiler::Reset(); + dmsg(1,("OK\n")); + } printf("LinuxSampler initialization completed.\n"); - while(true) sleep(1000); + std::list rtEvents; + rtEvents.push_back(LSCPEvent::event_voice_count); + rtEvents.push_back(LSCPEvent::event_stream_count); + rtEvents.push_back(LSCPEvent::event_buffer_fill); + + while(true) + { + /*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);*/ + sleep(1); + if (profile) + { + unsigned int samplingFreq = 48000; //FIXME: hardcoded for now + unsigned int bv = gig::Profiler::GetBogoVoices(samplingFreq); + if (bv != 0) + { + printf(" BogoVoices: %i \r", bv); + fflush(stdout); + } + } + + if (LSCPServer::EventSubscribers(rtEvents)) + { + LSCPServer::LockRTNotify(); + std::map channels = pSampler->GetSamplerChannels(); + std::map::iterator iter = channels.begin(); + for (; iter != channels.end(); iter++) { + SamplerChannel* pSamplerChannel = iter->second; + Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pEngine) continue; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, iter->first, pEngine->VoiceCount())); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, iter->first, pEngine->DiskStreamCount())); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, iter->first, pEngine->DiskStreamBufferFillPercentage())); + } + LSCPServer::UnlockRTNotify(); + } + + } + return EXIT_SUCCESS; } -void signal_handler(int signal) { - if (signal == SIGINT) { - // stop all threads - 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 (pAudioIO) delete pAudioIO; - - printf("LinuxSampler stopped due to SIGINT\n"); - exit(EXIT_SUCCESS); +void signal_handler(int iSignal) { + switch (iSignal) { + case SIGINT: { + if (pthread_equal(pthread_self(), main_thread)) { + if (pLSCPServer) { + pLSCPServer->StopThread(); + delete pLSCPServer; + } + if (pSampler) delete pSampler; + printf("LinuxSampler stopped due to SIGINT.\n"); + exit(EXIT_SUCCESS); + } + return; + } + case SIGSEGV: + std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush; + break; + case SIGBUS: + std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush; + break; + case SIGILL: + std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush; + break; + case SIGFPE: + std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush; + break; + case SIGUSR1: + std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush; + break; + case SIGUSR2: + std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush; + break; + default: { // this should never happen, as we register for the signals we want + std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush; + break; + } } + signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions + std::cerr << "Showing stack trace...\n" << std::flush; + StackTrace(); + sleep(2); + std::cerr << "Killing LinuxSampler...\n" << std::flush; + kill_app(); // Use abort() if we want to generate a core dump. +} + +void kill_app() { + kill(main_thread, SIGKILL); } void parse_options(int argc, char **argv) { @@ -163,38 +208,39 @@ int option_index = 0; static struct option long_options[] = { - {"numfragments",1,0,0}, - {"fragmentsize",1,0,0}, - {"dls",0,0,0}, - {"gig",0,0,0}, {"help",0,0,0}, + {"version",0,0,0}, + {"profile",0,0,0}, + {"no-tune",0,0,0}, {0,0,0,0} }; while (true) { - res = getopt_long_only(argc, argv, "", long_options, &option_index); + /* + Stephane Letz : letz@grame.fr + getopt_long_only does not exist on OSX : replaced by getopt_long for now. + */ + res = getopt_long(argc, argv, "", long_options, &option_index); if(res == -1) break; if (res == 0) { switch(option_index) { - case 0: - num_fragments = atoi(optarg); - break; - case 1: - fragmentsize = atoi(optarg); + case 0: // --help + printf("usage: linuxsampler [OPTIONS]\n\n"); + printf("--help prints this message\n"); + printf("--version prints version information\n"); + printf("--profile profile synthesis algorithms\n"); + printf("--no-tune disable assembly optimization\n"); + exit(EXIT_SUCCESS); break; - case 2: - patch_format = patch_format_dls; + case 1: // --version + printf("LinuxSampler %s\n", VERSION); + exit(EXIT_SUCCESS); break; - case 3: - patch_format = patch_format_gig; + case 2: // --profile + profile = true; break; - case 4: - printf("usage: linuxsampler [OPTIONS] \n\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); + case 3: // --no-tune + tune = false; break; } }