Example #1
0
int main(int argc, char **argv) {

    // initialize the stack trace mechanism with our binary file
    StackTraceInit(argv[0], -1);

    main_pid = getpid();
    main_thread = pthread_self();

    // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
    signal(SIGINT, signal_handler);

    // 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);

    lscp_addr = htonl(LSCP_ADDR);
    lscp_port = htons(LSCP_PORT);

    // parse and assign command line options
    parse_options(argc, argv);

    dmsg(1,("LinuxSampler %s\n", VERSION));
    dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));
    dmsg(1,("Copyright (C) 2005-2007 Christian Schoenebeck\n"));

    if (tune) {
        // detect and print system / CPU specific features
        Features::detect();
        dmsg(1,("Detected features: %s\n", Features::featuresAsString().c_str()));
        // prevent slow denormal FPU modes
        Features::enableDenormalsAreZeroMode();
    }

    // create LinuxSampler instance
    dmsg(1,("Creating Sampler..."));
    pSampler = new Sampler;
    dmsg(1,("OK\n"));

    dmsg(1,("Registered sampler engines: %s\n", EngineFactory::AvailableEngineTypesAsString().c_str()));
    dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
    dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
    dmsg(1,("Registered instrument editors: %s\n", InstrumentEditorFactory::AvailableEditorsAsString().c_str()));

    // start LSCP network server
    struct in_addr addr;
    addr.s_addr = lscp_addr;
    dmsg(1,("Starting LSCP network server (%s:%d)...", inet_ntoa(addr), ntohs(lscp_port)));
    pLSCPServer = new LSCPServer(pSampler, lscp_addr, lscp_port);
    pLSCPServer->StartThread();
    pLSCPServer->WaitUntilInitialized();
    dmsg(1,("OK\n"));

    if (profile)
    {
        dmsg(1,("Calibrating profiler..."));
        LinuxSampler::gig::Profiler::Calibrate();
        LinuxSampler::gig::Profiler::Reset();
        LinuxSampler::gig::Profiler::enable();
        dmsg(1,("OK\n"));
    }

    printf("LinuxSampler initialization completed. :-)\n\n");

    std::list<LSCPEvent::event_t> rtEvents;
    rtEvents.push_back(LSCPEvent::event_voice_count);
    rtEvents.push_back(LSCPEvent::event_stream_count);
    rtEvents.push_back(LSCPEvent::event_buffer_fill);
    rtEvents.push_back(LSCPEvent::event_total_voice_count);

    while (true) {
        if (bPrintStatistics) {
            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
            std::set<Engine*>::iterator itEngine = engines.begin();
            for (int i = 0; itEngine != engines.end(); itEngine++, i++) {
                Engine* pEngine = *itEngine;
                printf("Engine %d) Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d)\n", i,
                    pEngine->VoiceCount(), pEngine->VoiceCountMax(),
                    pEngine->DiskStreamCount(), pEngine->DiskStreamCountMax()
                );
                fflush(stdout);
            }
        }

      sleep(1);
      if (profile)
      {
          unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
          unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);
          if (bv != 0)
          {
              printf("       BogoVoices: %i         \r", bv);
              fflush(stdout);
          }
      }

      if (LSCPServer::EventSubscribers(rtEvents))
      {
          LSCPServer::LockRTNotify();
          std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
          std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
          for (; iter != channels.end(); iter++) {
              SamplerChannel* pSamplerChannel = iter->second;
              EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
              if (!pEngineChannel) continue;
              Engine* pEngine = pEngineChannel->GetEngine();
              if (!pEngine) continue;
              pSampler->fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
              pSampler->fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
              pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
              pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount());
          }
          LSCPServer::UnlockRTNotify();
      }

    }

    return EXIT_SUCCESS;
}