/[svn]/linuxsampler/trunk/src/linuxsampler.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/linuxsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1502 - (hide annotations) (download)
Wed Nov 21 07:29:52 2007 UTC (16 years, 5 months ago) by senoner
File size: 18278 byte(s)
* win32 port, work in progress: added autoconf checks for Windows
 - win32 additions in linuxsampler.cpp

1 schoenebeck 9 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 capela 1012 * Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck *
6     * Copyright (C) 2005-2007 Christian Schoenebeck *
7 schoenebeck 9 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #include <getopt.h>
25     #include <signal.h>
26    
27 senoner 1502 #if defined(WIN32)
28     // require at least Windows 2000 for the GlobalMemoryStatusEx() call
29     #define _WIN32_WINNT 0x0500
30     #endif
31    
32 schoenebeck 53 #include "Sampler.h"
33 schoenebeck 1424 #include "common/global_private.h"
34 schoenebeck 420 #include "engines/EngineFactory.h"
35 schoenebeck 1375 #include "plugins/InstrumentEditorFactory.h"
36 schoenebeck 207 #include "drivers/midi/MidiInputDeviceFactory.h"
37 schoenebeck 203 #include "drivers/audio/AudioOutputDeviceFactory.h"
38 senkov 325 #include "engines/gig/Profiler.h"
39 schoenebeck 35 #include "network/lscpserver.h"
40 schoenebeck 271 #include "common/stacktrace.h"
41 schoenebeck 319 #include "common/Features.h"
42 schoenebeck 9
43 schoenebeck 53 using namespace LinuxSampler;
44    
45 schoenebeck 211 Sampler* pSampler = NULL;
46     LSCPServer* pLSCPServer = NULL;
47 senoner 1502 #if defined(WIN32)
48     // inet_aton seems missing under WIN32
49     #ifndef INADDR_NONE
50     #define INADDR_NONE 0xffffffff
51     #endif
52    
53     int inet_aton(const char *cp, struct in_addr *addr)
54     {
55     addr->s_addr = inet_addr(cp);
56     return (addr->s_addr == INADDR_NONE) ? 0 : 1;
57     }
58    
59     DWORD main_thread;
60     #else
61 schoenebeck 271 pthread_t main_thread;
62 wylder 816 pid_t main_pid;
63 senoner 1502 #endif
64 schoenebeck 420 bool bPrintStatistics = false;
65 senkov 325 bool profile = false;
66     bool tune = true;
67 senkov 667 unsigned long int lscp_addr;
68     unsigned short int lscp_port;
69 schoenebeck 9
70     void parse_options(int argc, char **argv);
71     void signal_handler(int signal);
72 schoenebeck 271 void kill_app();
73 schoenebeck 9
74     int main(int argc, char **argv) {
75    
76 schoenebeck 271 // initialize the stack trace mechanism with our binary file
77     StackTraceInit(argv[0], -1);
78    
79 senoner 1502 #if defined(WIN32)
80     // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes)
81     SYSTEM_INFO siSysInfo;
82     long physical_memory;
83     GetSystemInfo(&siSysInfo);
84     dmsg(1,("page size=%d\n", siSysInfo.dwPageSize));
85    
86     MEMORYSTATUSEX statex;
87     statex.dwLength = sizeof (statex);
88     GlobalMemoryStatusEx (&statex);
89     dmsg(1, ("There are %*I64d total Kbytes of physical memory.\n",
90     8, statex.ullTotalPhys));
91     dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n",
92     8, statex.ullAvailPhys));
93     physical_memory = statex.ullTotalPhys;
94    
95     HANDLE hProcess = GetCurrentProcess();
96    
97     unsigned long MinimumWorkingSetSize, MaximumWorkingSetSize;
98     unsigned long DefaultMinimumWorkingSetSize, DefaultMaximumWorkingSetSize;
99     unsigned long RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize;
100     int res;
101    
102     res = GetProcessWorkingSetSize(hProcess, &DefaultMinimumWorkingSetSize, &DefaultMaximumWorkingSetSize);
103    
104     RequestedMaximumWorkingSetSize = physical_memory - 2*1024*1024;
105     RequestedMinimumWorkingSetSize = RequestedMaximumWorkingSetSize;
106    
107     for(;;) {
108     dmsg(2,("TRYING VALUES RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
109     res = SetProcessWorkingSetSize(hProcess, RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize);
110     dmsg(2,("AFTER SET: res = %d RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", res,RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
111    
112     res = GetProcessWorkingSetSize(hProcess, &MinimumWorkingSetSize, &MaximumWorkingSetSize);
113     dmsg(2,("AFTER GET: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
114    
115     if( RequestedMinimumWorkingSetSize == MinimumWorkingSetSize ) {
116     dmsg(2,("RequestedMinimumWorkingSetSize == MinimumWorkingSetSize. OK !\n"));
117     break;
118     }
119    
120     RequestedMinimumWorkingSetSize -= 10*1024*1024;
121     if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break;
122     }
123    
124     dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
125     #endif
126    
127     #if defined(WIN32)
128     main_thread = GetCurrentThreadId();
129     #else
130 wylder 816 main_pid = getpid();
131 schoenebeck 271 main_thread = pthread_self();
132 senoner 1502 #endif
133 schoenebeck 271
134 schoenebeck 9 // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
135     signal(SIGINT, signal_handler);
136    
137 senoner 1502 #if defined(WIN32)
138     // FIXME: sigaction() not supported on WIN32, we ignore it for now
139     #else
140 schoenebeck 271 // register signal handler for all unusual signals
141     // (we will print the stack trace and exit)
142     struct sigaction sact;
143     sigemptyset(&sact.sa_mask);
144     sact.sa_flags = 0;
145     sact.sa_handler = signal_handler;
146     sigaction(SIGSEGV, &sact, NULL);
147     sigaction(SIGBUS, &sact, NULL);
148     sigaction(SIGILL, &sact, NULL);
149     sigaction(SIGFPE, &sact, NULL);
150     sigaction(SIGUSR1, &sact, NULL);
151     sigaction(SIGUSR2, &sact, NULL);
152 senoner 1502 #endif
153 schoenebeck 271
154 senkov 667 lscp_addr = htonl(LSCP_ADDR);
155     lscp_port = htons(LSCP_PORT);
156    
157 schoenebeck 9 // parse and assign command line options
158 senkov 325 parse_options(argc, argv);
159 schoenebeck 9
160 schoenebeck 207 dmsg(1,("LinuxSampler %s\n", VERSION));
161 capela 1012 dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));
162     dmsg(1,("Copyright (C) 2005-2007 Christian Schoenebeck\n"));
163 schoenebeck 123
164 schoenebeck 579 if (tune) {
165 persson 425 // detect and print system / CPU specific features
166     Features::detect();
167 schoenebeck 579 dmsg(1,("Detected features: %s\n", Features::featuresAsString().c_str()));
168     // prevent slow denormal FPU modes
169     Features::enableDenormalsAreZeroMode();
170 senkov 325 }
171 schoenebeck 319
172 schoenebeck 53 // create LinuxSampler instance
173 schoenebeck 123 dmsg(1,("Creating Sampler..."));
174 schoenebeck 53 pSampler = new Sampler;
175 schoenebeck 123 dmsg(1,("OK\n"));
176 schoenebeck 53
177 schoenebeck 900 dmsg(1,("Registered sampler engines: %s\n", EngineFactory::AvailableEngineTypesAsString().c_str()));
178 schoenebeck 207 dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
179 schoenebeck 123 dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
180 schoenebeck 1212 dmsg(1,("Registered instrument editors: %s\n", InstrumentEditorFactory::AvailableEditorsAsString().c_str()));
181 schoenebeck 123
182 schoenebeck 53 // start LSCP network server
183 senkov 667 struct in_addr addr;
184     addr.s_addr = lscp_addr;
185     dmsg(1,("Starting LSCP network server (%s:%d)...", inet_ntoa(addr), ntohs(lscp_port)));
186     pLSCPServer = new LSCPServer(pSampler, lscp_addr, lscp_port);
187 schoenebeck 53 pLSCPServer->StartThread();
188 schoenebeck 211 pLSCPServer->WaitUntilInitialized();
189 schoenebeck 12 dmsg(1,("OK\n"));
190 schoenebeck 9
191 senkov 325 if (profile)
192     {
193 persson 425 dmsg(1,("Calibrating profiler..."));
194 iliev 1161 LinuxSampler::gig::Profiler::Calibrate();
195     LinuxSampler::gig::Profiler::Reset();
196     LinuxSampler::gig::Profiler::enable();
197 persson 425 dmsg(1,("OK\n"));
198 senkov 325 }
199    
200 schoenebeck 563 printf("LinuxSampler initialization completed. :-)\n\n");
201 schoenebeck 9
202 senkov 360 std::list<LSCPEvent::event_t> rtEvents;
203     rtEvents.push_back(LSCPEvent::event_voice_count);
204     rtEvents.push_back(LSCPEvent::event_stream_count);
205     rtEvents.push_back(LSCPEvent::event_buffer_fill);
206 iliev 778 rtEvents.push_back(LSCPEvent::event_total_voice_count);
207 senkov 360
208 schoenebeck 420 while (true) {
209     if (bPrintStatistics) {
210 persson 840 const std::set<Engine*>& engines = EngineFactory::EngineInstances();
211 schoenebeck 420 std::set<Engine*>::iterator itEngine = engines.begin();
212     for (int i = 0; itEngine != engines.end(); itEngine++, i++) {
213     Engine* pEngine = *itEngine;
214     printf("Engine %d) Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d)\n", i,
215     pEngine->VoiceCount(), pEngine->VoiceCountMax(),
216     pEngine->DiskStreamCount(), pEngine->DiskStreamCountMax()
217     );
218     fflush(stdout);
219     }
220     }
221 persson 425
222 senkov 325 sleep(1);
223     if (profile)
224     {
225 persson 425 unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
226 iliev 1161 unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);
227 persson 425 if (bv != 0)
228     {
229     printf(" BogoVoices: %i \r", bv);
230     fflush(stdout);
231     }
232 senkov 325 }
233 persson 425
234 senkov 360 if (LSCPServer::EventSubscribers(rtEvents))
235     {
236 persson 425 LSCPServer::LockRTNotify();
237     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
238     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
239     for (; iter != channels.end(); iter++) {
240     SamplerChannel* pSamplerChannel = iter->second;
241     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
242     if (!pEngineChannel) continue;
243     Engine* pEngine = pEngineChannel->GetEngine();
244     if (!pEngine) continue;
245 iliev 1297 pSampler->fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
246     pSampler->fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
247 iliev 1130 pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
248     pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount());
249 persson 425 }
250     LSCPServer::UnlockRTNotify();
251 senkov 360 }
252    
253 senoner 10 }
254    
255 schoenebeck 9 return EXIT_SUCCESS;
256     }
257    
258 schoenebeck 271 void signal_handler(int iSignal) {
259     switch (iSignal) {
260     case SIGINT: {
261 senoner 1502 #if defined(WIN32)
262     if( GetCurrentThreadId() == main_thread ) {
263     #else
264 schoenebeck 271 if (pthread_equal(pthread_self(), main_thread)) {
265 senoner 1502 #endif
266 persson 1248 if (pLSCPServer) pLSCPServer->StopThread();
267     // the delete order here is important: the Sampler
268     // destructor sends notifications to the lscpserver
269 schoenebeck 271 if (pSampler) delete pSampler;
270 persson 1248 if (pLSCPServer) delete pLSCPServer;
271 iliev 1161 #if HAVE_SQLITE3
272     InstrumentsDb::Destroy();
273     #endif
274 schoenebeck 271 printf("LinuxSampler stopped due to SIGINT.\n");
275     exit(EXIT_SUCCESS);
276     }
277     return;
278 schoenebeck 53 }
279 senoner 1502 #if defined(WIN32)
280     // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call
281     #else
282 schoenebeck 271 case SIGSEGV:
283     std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
284     break;
285     case SIGBUS:
286     std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush;
287     break;
288     case SIGILL:
289     std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush;
290     break;
291     case SIGFPE:
292     std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush;
293     break;
294     case SIGUSR1:
295     std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush;
296     break;
297     case SIGUSR2:
298     std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
299     break;
300 senoner 1502 #endif
301 schoenebeck 271 default: { // this should never happen, as we register for the signals we want
302     std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
303     break;
304     }
305 schoenebeck 9 }
306 schoenebeck 271 signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
307     std::cerr << "Showing stack trace...\n" << std::flush;
308     StackTrace();
309     sleep(2);
310     std::cerr << "Killing LinuxSampler...\n" << std::flush;
311     kill_app(); // Use abort() if we want to generate a core dump.
312 schoenebeck 9 }
313    
314 schoenebeck 271 void kill_app() {
315 senoner 1502 #if defined(WIN32)
316     // FIXME: do we need to do anything at this point under WIN32 ? is exit(0) ok ?
317     exit(0);
318     #else
319 wylder 816 kill(main_pid, SIGKILL);
320 senoner 1502 #endif
321 schoenebeck 271 }
322    
323 senkov 325 void parse_options(int argc, char **argv) {
324 schoenebeck 9 int res;
325     int option_index = 0;
326     static struct option long_options[] =
327     {
328     {"help",0,0,0},
329 senkov 325 {"version",0,0,0},
330     {"profile",0,0,0},
331     {"no-tune",0,0,0},
332 schoenebeck 420 {"statistics",0,0,0},
333 iliev 1208 {"instruments-db-location",1,0,0},
334 iliev 1187 {"create-instruments-db",1,0,0},
335 senkov 667 {"lscp-addr",1,0,0},
336     {"lscp-port",1,0,0},
337 schoenebeck 9 {0,0,0,0}
338     };
339    
340     while (true) {
341 schoenebeck 361 /*
342     Stephane Letz : letz@grame.fr
343     getopt_long_only does not exist on OSX : replaced by getopt_long for now.
344     */
345     res = getopt_long(argc, argv, "", long_options, &option_index);
346 schoenebeck 9 if(res == -1) break;
347     if (res == 0) {
348     switch(option_index) {
349 senkov 325 case 0: // --help
350     printf("usage: linuxsampler [OPTIONS]\n\n");
351 iliev 1208 printf("--help prints this message\n");
352     printf("--version prints version information\n");
353     printf("--profile profile synthesis algorithms\n");
354     printf("--no-tune disable assembly optimization\n");
355     printf("--statistics periodically prints statistics\n");
356     printf("--lscp-addr set LSCP address (default: any)\n");
357     printf("--lscp-port set LSCP port (default: 8888)\n");
358     printf("--create-instruments-db creates an instruments DB\n");
359     printf("--instruments-db-location specifies the instruments DB file\n");
360 senkov 325 exit(EXIT_SUCCESS);
361 schoenebeck 9 break;
362 senkov 325 case 1: // --version
363 schoenebeck 328 printf("LinuxSampler %s\n", VERSION);
364     exit(EXIT_SUCCESS);
365 schoenebeck 9 break;
366 senkov 325 case 2: // --profile
367 persson 425 profile = true;
368 schoenebeck 20 break;
369 senkov 325 case 3: // --no-tune
370 persson 425 tune = false;
371 schoenebeck 9 break;
372 schoenebeck 420 case 4: // --statistics
373     bPrintStatistics = true;
374     break;
375 iliev 1208 case 5: // --instruments-db-location
376 iliev 1187 #if HAVE_SQLITE3
377     try {
378     if (optarg) {
379 iliev 1208 struct stat statBuf;
380     int res = stat(optarg, &statBuf);
381    
382     if (res) {
383     std::stringstream ss;
384 schoenebeck 1364 ss << "Failed to stat `" << optarg << "`: " << strerror(errno);
385 iliev 1208 throw Exception(ss.str());
386     }
387    
388     if (!S_ISREG(statBuf.st_mode)) {
389     std::stringstream ss;
390     ss << "`" << optarg << "` is not a regular file";
391     throw Exception(ss.str());
392     }
393    
394     InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg));
395     }
396     } catch(Exception e) {
397 schoenebeck 1364 std::cerr << "Could not open instruments DB file: "
398     << e.Message() << std::endl;
399     exit(EXIT_FAILURE);
400 iliev 1208 }
401 schoenebeck 1364 break;
402 iliev 1208 #else
403     std::cerr << "LinuxSampler was not build with ";
404 schoenebeck 1364 std::cerr << "instruments database support!\n";
405     exit(EXIT_FAILURE);
406     break;
407     #endif
408 iliev 1208 case 6: // --create-instruments-db
409     #if HAVE_SQLITE3
410     try {
411     if (optarg) {
412 iliev 1187 std::cout << "Creating instruments database..." << std::endl;
413     InstrumentsDb::CreateInstrumentsDb(String(optarg));
414     InstrumentsDb::Destroy();
415     std::cout << "Done" << std::endl;
416     }
417     } catch(Exception e) {
418     std::cerr << e.Message() << std::endl;
419     exit(EXIT_FAILURE);
420     return;
421     }
422    
423     exit(EXIT_SUCCESS);
424     return;
425     #else
426     std::cerr << "Failed to create the database. LinuxSampler was ";
427 schoenebeck 1364 std::cerr << "not build with instruments database support!\n";
428 iliev 1187 exit(EXIT_FAILURE);
429     return;
430 schoenebeck 1364 #endif
431 iliev 1208 case 7: // --lscp-addr
432 iliev 1187 struct in_addr addr;
433     if (inet_aton(optarg, &addr) == 0)
434     printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");
435     else
436     lscp_addr = addr.s_addr;
437 senkov 667 break;
438 iliev 1208 case 8: // --lscp-port
439 iliev 1187 long unsigned int port = 0;
440     if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))
441     printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");
442     else
443     lscp_port = htons(port);
444 senkov 667 break;
445 schoenebeck 9 }
446     }
447     }
448 senkov 325 }

  ViewVC Help
Powered by ViewVC