--- linuxsampler/trunk/src/linuxsampler.cpp 2005/05/22 20:43:32 563 +++ linuxsampler/trunk/src/linuxsampler.cpp 2007/11/21 07:29:52 1502 @@ -2,8 +2,8 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 Christian Schoenebeck * + * Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005-2007 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 * @@ -24,8 +24,15 @@ #include #include +#if defined(WIN32) +// require at least Windows 2000 for the GlobalMemoryStatusEx() call +#define _WIN32_WINNT 0x0500 +#endif + #include "Sampler.h" +#include "common/global_private.h" #include "engines/EngineFactory.h" +#include "plugins/InstrumentEditorFactory.h" #include "drivers/midi/MidiInputDeviceFactory.h" #include "drivers/audio/AudioOutputDeviceFactory.h" #include "engines/gig/Profiler.h" @@ -37,10 +44,28 @@ Sampler* pSampler = NULL; LSCPServer* pLSCPServer = NULL; +#if defined(WIN32) +// inet_aton seems missing under WIN32 +#ifndef INADDR_NONE +#define INADDR_NONE 0xffffffff +#endif + +int inet_aton(const char *cp, struct in_addr *addr) +{ + addr->s_addr = inet_addr(cp); + return (addr->s_addr == INADDR_NONE) ? 0 : 1; +} + +DWORD main_thread; +#else pthread_t main_thread; +pid_t main_pid; +#endif bool bPrintStatistics = false; bool profile = false; bool tune = true; +unsigned long int lscp_addr; +unsigned short int lscp_port; void parse_options(int argc, char **argv); void signal_handler(int signal); @@ -51,11 +76,67 @@ // initialize the stack trace mechanism with our binary file StackTraceInit(argv[0], -1); + #if defined(WIN32) + // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes) + SYSTEM_INFO siSysInfo; + long physical_memory; + GetSystemInfo(&siSysInfo); + dmsg(1,("page size=%d\n", siSysInfo.dwPageSize)); + + MEMORYSTATUSEX statex; + statex.dwLength = sizeof (statex); + GlobalMemoryStatusEx (&statex); + dmsg(1, ("There are %*I64d total Kbytes of physical memory.\n", + 8, statex.ullTotalPhys)); + dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n", + 8, statex.ullAvailPhys)); + physical_memory = statex.ullTotalPhys; + + HANDLE hProcess = GetCurrentProcess(); + + unsigned long MinimumWorkingSetSize, MaximumWorkingSetSize; + unsigned long DefaultMinimumWorkingSetSize, DefaultMaximumWorkingSetSize; + unsigned long RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize; + int res; + + res = GetProcessWorkingSetSize(hProcess, &DefaultMinimumWorkingSetSize, &DefaultMaximumWorkingSetSize); + + RequestedMaximumWorkingSetSize = physical_memory - 2*1024*1024; + RequestedMinimumWorkingSetSize = RequestedMaximumWorkingSetSize; + + for(;;) { + dmsg(2,("TRYING VALUES RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize)); + res = SetProcessWorkingSetSize(hProcess, RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize); + dmsg(2,("AFTER SET: res = %d RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", res,RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize)); + + res = GetProcessWorkingSetSize(hProcess, &MinimumWorkingSetSize, &MaximumWorkingSetSize); + dmsg(2,("AFTER GET: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); + + if( RequestedMinimumWorkingSetSize == MinimumWorkingSetSize ) { + dmsg(2,("RequestedMinimumWorkingSetSize == MinimumWorkingSetSize. OK !\n")); + break; + } + + RequestedMinimumWorkingSetSize -= 10*1024*1024; + if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break; + } + + dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); + #endif + + #if defined(WIN32) + main_thread = GetCurrentThreadId(); + #else + main_pid = getpid(); main_thread = pthread_self(); + #endif // setting signal handler for catching SIGINT (thus e.g. ) signal(SIGINT, signal_handler); + #if defined(WIN32) + // FIXME: sigaction() not supported on WIN32, we ignore it for now + #else // register signal handler for all unusual signals // (we will print the stack trace and exit) struct sigaction sact; @@ -68,40 +149,24 @@ sigaction(SIGFPE, &sact, NULL); sigaction(SIGUSR1, &sact, NULL); sigaction(SIGUSR2, &sact, NULL); + #endif + + 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 Christian Schoenebeck\n")); + dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n")); + dmsg(1,("Copyright (C) 2005-2007 Christian Schoenebeck\n")); - if (tune) - { + 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"; - if (Features::supportsSSE2()) { - sFeatures += " SSE2"; - - // enable denormals-are-zeros mode - int x; - __asm__ __volatile__ ( - "stmxcsr %0\n\t" - "movl %0, %%eax\n\t" - "orl $0x40, %%eax\n\t" - "movl %%eax, %0\n\t" - "ldmxcsr %0\n\t" - :: "m" (x) - : "%eax" - ); - } -#endif // ARCH_X86 - if (!sFeatures.size()) sFeatures = " None"; - dmsg(1,("Detected features:%s\n",sFeatures.c_str())); + dmsg(1,("Detected features: %s\n", Features::featuresAsString().c_str())); + // prevent slow denormal FPU modes + Features::enableDenormalsAreZeroMode(); } // create LinuxSampler instance @@ -109,12 +174,16 @@ 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 - dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT)); - pLSCPServer = new LSCPServer(pSampler); + 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")); @@ -122,8 +191,9 @@ if (profile) { dmsg(1,("Calibrating profiler...")); - gig::Profiler::Calibrate(); - gig::Profiler::Reset(); + LinuxSampler::gig::Profiler::Calibrate(); + LinuxSampler::gig::Profiler::Reset(); + LinuxSampler::gig::Profiler::enable(); dmsg(1,("OK\n")); } @@ -133,10 +203,11 @@ 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) { - std::set engines = EngineFactory::EngineInstances(); + const std::set& engines = EngineFactory::EngineInstances(); std::set::iterator itEngine = engines.begin(); for (int i = 0; itEngine != engines.end(); itEngine++, i++) { Engine* pEngine = *itEngine; @@ -152,7 +223,7 @@ if (profile) { unsigned int samplingFreq = 48000; //FIXME: hardcoded for now - unsigned int bv = gig::Profiler::GetBogoVoices(samplingFreq); + unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq); if (bv != 0) { printf(" BogoVoices: %i \r", bv); @@ -171,9 +242,10 @@ if (!pEngineChannel) continue; Engine* pEngine = pEngineChannel->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())); + pSampler->fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount()); + pSampler->fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount()); + pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage()); + pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount()); } LSCPServer::UnlockRTNotify(); } @@ -186,17 +258,27 @@ void signal_handler(int iSignal) { switch (iSignal) { case SIGINT: { + #if defined(WIN32) + if( GetCurrentThreadId() == main_thread ) { + #else if (pthread_equal(pthread_self(), main_thread)) { - if (pLSCPServer) { - pLSCPServer->StopThread(); - delete pLSCPServer; - } + #endif + if (pLSCPServer) pLSCPServer->StopThread(); + // the delete order here is important: the Sampler + // destructor sends notifications to the lscpserver if (pSampler) delete pSampler; + if (pLSCPServer) delete pLSCPServer; +#if HAVE_SQLITE3 + InstrumentsDb::Destroy(); +#endif printf("LinuxSampler stopped due to SIGINT.\n"); exit(EXIT_SUCCESS); } return; } + #if defined(WIN32) + // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call + #else case SIGSEGV: std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush; break; @@ -215,6 +297,7 @@ case SIGUSR2: std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush; break; + #endif default: { // this should never happen, as we register for the signals we want std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush; break; @@ -229,7 +312,12 @@ } void kill_app() { - kill(main_thread, SIGKILL); + #if defined(WIN32) + // FIXME: do we need to do anything at this point under WIN32 ? is exit(0) ok ? + exit(0); + #else + kill(main_pid, SIGKILL); + #endif } void parse_options(int argc, char **argv) { @@ -242,6 +330,10 @@ {"profile",0,0,0}, {"no-tune",0,0,0}, {"statistics",0,0,0}, + {"instruments-db-location",1,0,0}, + {"create-instruments-db",1,0,0}, + {"lscp-addr",1,0,0}, + {"lscp-port",1,0,0}, {0,0,0,0} }; @@ -256,11 +348,15 @@ switch(option_index) { 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"); - printf("--statistics prints periodically statistics\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"); + printf("--statistics periodically prints statistics\n"); + printf("--lscp-addr set LSCP address (default: any)\n"); + printf("--lscp-port set LSCP port (default: 8888)\n"); + printf("--create-instruments-db creates an instruments DB\n"); + printf("--instruments-db-location specifies the instruments DB file\n"); exit(EXIT_SUCCESS); break; case 1: // --version @@ -276,6 +372,76 @@ case 4: // --statistics bPrintStatistics = true; break; + case 5: // --instruments-db-location +#if HAVE_SQLITE3 + try { + if (optarg) { + struct stat statBuf; + int res = stat(optarg, &statBuf); + + if (res) { + std::stringstream ss; + ss << "Failed to stat `" << optarg << "`: " << strerror(errno); + throw Exception(ss.str()); + } + + if (!S_ISREG(statBuf.st_mode)) { + std::stringstream ss; + ss << "`" << optarg << "` is not a regular file"; + throw Exception(ss.str()); + } + + InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg)); + } + } catch(Exception e) { + std::cerr << "Could not open instruments DB file: " + << e.Message() << std::endl; + exit(EXIT_FAILURE); + } + break; +#else + std::cerr << "LinuxSampler was not build with "; + std::cerr << "instruments database support!\n"; + exit(EXIT_FAILURE); + break; +#endif + case 6: // --create-instruments-db +#if HAVE_SQLITE3 + try { + if (optarg) { + std::cout << "Creating instruments database..." << std::endl; + InstrumentsDb::CreateInstrumentsDb(String(optarg)); + InstrumentsDb::Destroy(); + std::cout << "Done" << std::endl; + } + } catch(Exception e) { + std::cerr << e.Message() << std::endl; + exit(EXIT_FAILURE); + return; + } + + exit(EXIT_SUCCESS); + return; +#else + std::cerr << "Failed to create the database. LinuxSampler was "; + std::cerr << "not build with instruments database support!\n"; + exit(EXIT_FAILURE); + return; +#endif + case 7: // --lscp-addr + struct in_addr addr; + if (inet_aton(optarg, &addr) == 0) + printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n"); + else + lscp_addr = addr.s_addr; + break; + case 8: // --lscp-port + long unsigned int port = 0; + if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535)) + printf("WARNING: Failed to parse lscp-port argument, ignoring!\n"); + else + lscp_port = htons(port); + break; } } }