--- linuxsampler/trunk/src/linuxsampler.cpp 2008/01/19 16:55:03 1644 +++ linuxsampler/trunk/src/linuxsampler.cpp 2016/12/16 12:57:59 3056 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005-2008 Christian Schoenebeck * + * Copyright (C) 2005-2016 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 * @@ -23,11 +23,17 @@ #include #include +#include #if defined(WIN32) // require at least Windows 2000 for the GlobalMemoryStatusEx() call +#if _WIN32_WINNT < 0x0500 +#ifdef _WIN32_WINNT +#undef _WIN32_WINNT +#endif #define _WIN32_WINNT 0x0500 #endif +#endif #include "Sampler.h" #include "common/global_private.h" @@ -35,6 +41,7 @@ #include "plugins/InstrumentEditorFactory.h" #include "drivers/midi/MidiInputDeviceFactory.h" #include "drivers/audio/AudioOutputDeviceFactory.h" +#include "effects/EffectFactory.h" #include "engines/gig/Profiler.h" #include "network/lscpserver.h" #include "common/stacktrace.h" @@ -49,6 +56,7 @@ // inet_aton seems missing under WIN32 #ifndef INADDR_NONE #define INADDR_NONE 0xffffffff +typedef unsigned long in_addr_t; #endif int inet_aton(const char *cp, struct in_addr *addr) @@ -63,8 +71,10 @@ bool bPrintStatistics = false; bool profile = false; bool tune = true; +static bool bShowStackTrace = false; unsigned long int lscp_addr; unsigned short int lscp_port; +String ExecAfterInit; void parse_options(int argc, char **argv); void signal_handler(int signal); @@ -73,22 +83,62 @@ int main(int argc, char **argv) { + lscp_addr = htonl(LSCP_ADDR); + lscp_port = htons(LSCP_PORT); + + #if !defined(WIN32) + main_pid = getpid(); + #endif + + // parse and assign command line options + parse_options(argc, argv); + + // setting signal handler for catching SIGINT (thus e.g. ) + signal(SIGINT, signal_handler); + // initialize the stack trace mechanism with our binary file - StackTraceInit(argv[0], -1); + // (if requested by command line option) + if (bShowStackTrace) { + #if defined(WIN32) + // FIXME: sigaction() not supported on WIN32, we ignore it for now + #elif AC_APPLE_UNIVERSAL_BUILD + // not used for Xcode + #else + StackTraceInit(argv[0], -1); + // 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); + #endif + } + + dmsg(1,("LinuxSampler %s\n", VERSION)); + dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n")); + dmsg(1,("Copyright (C) 2005-2016 Christian Schoenebeck\n")); + dmsg(1,("Binary built: " __DATE__ "\n")) #if defined(WIN32) + #if 0 // 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)); + dmsg(2,("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", + dmsg(2, ("There are %*I64d total Kbytes of physical memory.\n", 8, statex.ullTotalPhys)); - dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n", + dmsg(2, ("There are %*I64d free Kbytes of physical memory.\n", 8, statex.ullAvailPhys)); physical_memory = statex.ullTotalPhys; @@ -121,42 +171,9 @@ if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break; } - dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); - #endif - - #if !defined(WIN32) - main_pid = getpid(); - #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; - 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); + dmsg(2,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); #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-2007 Christian Schoenebeck\n")); + #endif // WIN32 if (tune) { // detect and print system / CPU specific features @@ -166,6 +183,8 @@ Features::enableDenormalsAreZeroMode(); } + dmsg(1,("Automatic Stacktrace: %s\n", (bShowStackTrace) ? "On" : "Off")); + // create LinuxSampler instance dmsg(1,("Creating Sampler...")); pSampler = new Sampler; @@ -175,10 +194,12 @@ 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())); + dmsg(1,("Registered internal effect systems: %s\n", EffectFactory::AvailableEffectSystemsAsString().c_str())); + dmsg(1,("Registered internal effects: %d\n", EffectFactory::AvailableEffectsCount())); // start LSCP network server struct in_addr addr; - addr.s_addr = lscp_addr; + addr.s_addr = (in_addr_t)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(); @@ -195,12 +216,18 @@ } printf("LinuxSampler initialization completed. :-)\n\n"); + + if (ExecAfterInit != "") { + printf("Executing command: %s\n\n", ExecAfterInit.c_str()); + if (system(ExecAfterInit.c_str()) == -1) { + std::cerr << "Failed to execute the command" << std::endl; + } + } - std::list 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); +//TODO: (hopefully) just a temporary nasty hack for launching gigedit on the main thread on Mac (see comments in gigedit.cpp for details) +#if defined(__APPLE__) + g_mainThreadCallbackSupported = true; +#endif while (atomic_read(&running)) { if (bPrintStatistics) { @@ -228,26 +255,23 @@ } } - 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; - 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->fireTotalStreamCountChanged(pSampler->GetDiskStreamCount()); - pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount()); - } - LSCPServer::UnlockRTNotify(); + pSampler->fireStatistics(); + + //TODO: (hopefully) just a temporary nasty hack for launching gigedit on the main thread on Mac (see comments in gigedit.cpp for details) + #if defined(__APPLE__) + if (g_fireMainThreadCallback && g_mainThreadCallback) { + void (*fn)(void* info) = g_mainThreadCallback; + void* info = g_mainThreadCallbackInfo; + g_mainThreadCallbackInfo = NULL; + g_mainThreadCallback = NULL; + g_fireMainThreadCallback = false; + printf("Received main thread callback, calling now ...\n"); fflush(stdout); + (*fn)(info); + printf("Main thread callback executed.\n"); fflush(stdout); } + #endif } +//#endif if (pLSCPServer) pLSCPServer->StopThread(); // the delete order here is important: the Sampler // destructor sends notifications to the lscpserver @@ -290,9 +314,13 @@ } } signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions - std::cerr << "Showing stack trace...\n" << std::flush; - StackTrace(); - sleep(2); + if (bShowStackTrace) { + std::cerr << "Showing stack trace...\n" << std::flush; + #if !AC_APPLE_UNIVERSAL_BUILD + StackTrace(); + #endif + sleep(2); + } std::cerr << "Killing LinuxSampler...\n" << std::flush; kill_app(); // Use abort() if we want to generate a core dump. } @@ -320,6 +348,8 @@ {"create-instruments-db",1,0,0}, {"lscp-addr",1,0,0}, {"lscp-port",1,0,0}, + {"stacktrace",0,0,0}, + {"exec-after-init",1,0,0}, {0,0,0,0} }; @@ -343,6 +373,9 @@ 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"); + printf("--stacktrace automatically shows stacktrace if crashes\n"); + printf(" (broken on most systems at the moment)\n"); + printf("--exec-after-init executes a command after initialization\n"); exit(EXIT_SUCCESS); break; case 1: // --version @@ -351,6 +384,9 @@ break; case 2: // --profile profile = true; + //FIXME: profiling code is currently broken! + std::cerr << "Option '--profile' is currently not supported, since the profiling code is currently broken!" << std::endl; + exit(EXIT_FAILURE); break; case 3: // --no-tune tune = false; @@ -413,20 +449,28 @@ exit(EXIT_FAILURE); return; #endif - case 7: // --lscp-addr + 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 + } + case 8: {// --lscp-port long unsigned int port = 0; - if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535)) + if ((sscanf(optarg, "%lu", &port) != 1) || (port == 0) || (port > 65535)) printf("WARNING: Failed to parse lscp-port argument, ignoring!\n"); else lscp_port = htons(port); break; + } + case 9: // --stacktrace + bShowStackTrace = true; + break; + case 10: // --exec-after-init + ExecAfterInit = optarg; + break; } } }