3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
* Copyright (C) 2005 - 2009 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
24 |
#include <algorithm> |
#include <algorithm> |
25 |
#include <string> |
#include <string> |
26 |
|
|
27 |
|
#include "../common/File.h" |
28 |
#include "lscpserver.h" |
#include "lscpserver.h" |
29 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
30 |
#include "lscpevent.h" |
#include "lscpevent.h" |
141 |
} |
} |
142 |
|
|
143 |
LSCPServer::~LSCPServer() { |
LSCPServer::~LSCPServer() { |
144 |
|
CloseAllConnections(); |
145 |
|
InstrumentManager::StopBackgroundThread(); |
146 |
#if defined(WIN32) |
#if defined(WIN32) |
147 |
if (hSocket >= 0) closesocket(hSocket); |
if (hSocket >= 0) closesocket(hSocket); |
148 |
#else |
#else |
337 |
} |
} |
338 |
#endif // HAVE_SQLITE3 |
#endif // HAVE_SQLITE3 |
339 |
|
|
340 |
|
void LSCPServer::RemoveListeners() { |
341 |
|
pSampler->RemoveChannelCountListener(&eventHandler); |
342 |
|
pSampler->RemoveAudioDeviceCountListener(&eventHandler); |
343 |
|
pSampler->RemoveMidiDeviceCountListener(&eventHandler); |
344 |
|
pSampler->RemoveVoiceCountListener(&eventHandler); |
345 |
|
pSampler->RemoveStreamCountListener(&eventHandler); |
346 |
|
pSampler->RemoveBufferFillListener(&eventHandler); |
347 |
|
pSampler->RemoveTotalStreamCountListener(&eventHandler); |
348 |
|
pSampler->RemoveTotalVoiceCountListener(&eventHandler); |
349 |
|
pSampler->RemoveFxSendCountListener(&eventHandler); |
350 |
|
MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler); |
351 |
|
MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler); |
352 |
|
MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler); |
353 |
|
MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler); |
354 |
|
#if HAVE_SQLITE3 |
355 |
|
InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler); |
356 |
|
#endif |
357 |
|
} |
358 |
|
|
359 |
/** |
/** |
360 |
* Blocks the calling thread until the LSCP Server is initialized and |
* Blocks the calling thread until the LSCP Server is initialized and |
528 |
|
|
529 |
int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout); |
int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout); |
530 |
|
|
531 |
if (retval == 0) |
if (retval == 0 || (retval == -1 && errno == EINTR)) |
532 |
continue; //Nothing try again |
continue; //Nothing try again |
533 |
if (retval == -1) { |
if (retval == -1) { |
534 |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
555 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
556 |
} |
} |
557 |
#else |
#else |
558 |
|
struct linger linger; |
559 |
|
linger.l_onoff = 1; |
560 |
|
linger.l_linger = 0; |
561 |
|
if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) { |
562 |
|
std::cerr << "LSCPServer: Failed to set SO_LINGER\n"; |
563 |
|
} |
564 |
|
|
565 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
566 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
567 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
633 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
634 |
} |
} |
635 |
|
|
636 |
|
void LSCPServer::CloseAllConnections() { |
637 |
|
std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); |
638 |
|
while(iter != Sessions.end()) { |
639 |
|
CloseConnection(iter); |
640 |
|
iter = Sessions.begin(); |
641 |
|
} |
642 |
|
} |
643 |
|
|
644 |
void LSCPServer::LockRTNotify() { |
void LSCPServer::LockRTNotify() { |
645 |
RTNotifyMutex.Lock(); |
RTNotifyMutex.Lock(); |
646 |
} |
} |
1295 |
for (;iter != parameters.end(); iter++) { |
for (;iter != parameters.end(); iter++) { |
1296 |
if (s != "") s += ","; |
if (s != "") s += ","; |
1297 |
s += iter->first; |
s += iter->first; |
1298 |
|
delete iter->second; |
1299 |
} |
} |
1300 |
result.Add("PARAMETERS", s); |
result.Add("PARAMETERS", s); |
1301 |
} |
} |
1320 |
for (;iter != parameters.end(); iter++) { |
for (;iter != parameters.end(); iter++) { |
1321 |
if (s != "") s += ","; |
if (s != "") s += ","; |
1322 |
s += iter->first; |
s += iter->first; |
1323 |
|
delete iter->second; |
1324 |
} |
} |
1325 |
result.Add("PARAMETERS", s); |
result.Add("PARAMETERS", s); |
1326 |
} |
} |
1351 |
if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); |
if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); |
1352 |
if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); |
if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); |
1353 |
if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); |
if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); |
1354 |
|
delete pParameter; |
1355 |
} |
} |
1356 |
catch (Exception e) { |
catch (Exception e) { |
1357 |
result.Error(e); |
result.Error(e); |
1379 |
if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); |
if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); |
1380 |
if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); |
if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); |
1381 |
if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); |
if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); |
1382 |
|
delete pParameter; |
1383 |
} |
} |
1384 |
catch (Exception e) { |
catch (Exception e) { |
1385 |
result.Error(e); |
result.Error(e); |
2491 |
if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device"); |
if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device"); |
2492 |
|
|
2493 |
if (MidiMsg == "NOTE_ON") { |
if (MidiMsg == "NOTE_ON") { |
2494 |
|
pMidiDevice->SendNoteOnToDevice(Arg1, Arg2); |
2495 |
bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2); |
bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2); |
2496 |
if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); |
if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); |
2497 |
} else if (MidiMsg == "NOTE_OFF") { |
} else if (MidiMsg == "NOTE_OFF") { |
2498 |
|
pMidiDevice->SendNoteOffToDevice(Arg1, Arg2); |
2499 |
bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2); |
bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2); |
2500 |
if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); |
if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); |
2501 |
} else { |
} else { |
2580 |
String LSCPServer::GetTotalVoiceCountMax() { |
String LSCPServer::GetTotalVoiceCountMax() { |
2581 |
dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n")); |
dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n")); |
2582 |
LSCPResultSet result; |
LSCPResultSet result; |
2583 |
result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES); |
result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES); |
2584 |
|
return result.Produce(); |
2585 |
|
} |
2586 |
|
|
2587 |
|
/** |
2588 |
|
* Will be called by the parser to return the sampler global maximum |
2589 |
|
* allowed number of voices. |
2590 |
|
*/ |
2591 |
|
String LSCPServer::GetGlobalMaxVoices() { |
2592 |
|
dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n")); |
2593 |
|
LSCPResultSet result; |
2594 |
|
result.Add(GLOBAL_MAX_VOICES); |
2595 |
|
return result.Produce(); |
2596 |
|
} |
2597 |
|
|
2598 |
|
/** |
2599 |
|
* Will be called by the parser to set the sampler global maximum number of |
2600 |
|
* voices. |
2601 |
|
*/ |
2602 |
|
String LSCPServer::SetGlobalMaxVoices(int iVoices) { |
2603 |
|
dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices)); |
2604 |
|
LSCPResultSet result; |
2605 |
|
try { |
2606 |
|
if (iVoices < 1) throw Exception("Maximum voices may not be less than 1"); |
2607 |
|
GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp |
2608 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
2609 |
|
if (engines.size() > 0) { |
2610 |
|
std::set<Engine*>::iterator iter = engines.begin(); |
2611 |
|
std::set<Engine*>::iterator end = engines.end(); |
2612 |
|
for (; iter != end; ++iter) { |
2613 |
|
(*iter)->SetMaxVoices(iVoices); |
2614 |
|
} |
2615 |
|
} |
2616 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES)); |
2617 |
|
} catch (Exception e) { |
2618 |
|
result.Error(e); |
2619 |
|
} |
2620 |
|
return result.Produce(); |
2621 |
|
} |
2622 |
|
|
2623 |
|
/** |
2624 |
|
* Will be called by the parser to return the sampler global maximum |
2625 |
|
* allowed number of disk streams. |
2626 |
|
*/ |
2627 |
|
String LSCPServer::GetGlobalMaxStreams() { |
2628 |
|
dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n")); |
2629 |
|
LSCPResultSet result; |
2630 |
|
result.Add(GLOBAL_MAX_STREAMS); |
2631 |
|
return result.Produce(); |
2632 |
|
} |
2633 |
|
|
2634 |
|
/** |
2635 |
|
* Will be called by the parser to set the sampler global maximum number of |
2636 |
|
* disk streams. |
2637 |
|
*/ |
2638 |
|
String LSCPServer::SetGlobalMaxStreams(int iStreams) { |
2639 |
|
dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams)); |
2640 |
|
LSCPResultSet result; |
2641 |
|
try { |
2642 |
|
if (iStreams < 0) throw Exception("Maximum disk streams may not be negative"); |
2643 |
|
GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp |
2644 |
|
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
2645 |
|
if (engines.size() > 0) { |
2646 |
|
std::set<Engine*>::iterator iter = engines.begin(); |
2647 |
|
std::set<Engine*>::iterator end = engines.end(); |
2648 |
|
for (; iter != end; ++iter) { |
2649 |
|
(*iter)->SetMaxDiskStreams(iStreams); |
2650 |
|
} |
2651 |
|
} |
2652 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS)); |
2653 |
|
} catch (Exception e) { |
2654 |
|
result.Error(e); |
2655 |
|
} |
2656 |
return result.Produce(); |
return result.Produce(); |
2657 |
} |
} |
2658 |
|
|
2804 |
} |
} |
2805 |
result.Add("KEY_BINDINGS", ss.str()); |
result.Add("KEY_BINDINGS", ss.str()); |
2806 |
|
|
2807 |
|
b = false; |
2808 |
std::stringstream ss2; |
std::stringstream ss2; |
2809 |
for (int i = 0; i < 128; i++) { |
for (int i = 0; i < 128; i++) { |
2810 |
if (info.KeySwitchBindings[i]) { |
if (info.KeySwitchBindings[i]) { |
2840 |
throw Exception("Directory is specified"); |
throw Exception("Directory is specified"); |
2841 |
} |
} |
2842 |
#else |
#else |
2843 |
struct stat statBuf; |
File f(Filename); |
2844 |
int res = stat(Filename.c_str(), &statBuf); |
if(!f.Exist()) throw Exception(f.GetErrorMsg()); |
2845 |
if (res) { |
if (f.IsDirectory()) throw Exception("Directory is specified"); |
|
std::stringstream ss; |
|
|
ss << "Fail to stat `" << Filename << "`: " << strerror(errno); |
|
|
throw Exception(ss.str()); |
|
|
} |
|
|
|
|
|
if (S_ISDIR(statBuf.st_mode)) { |
|
|
throw Exception("Directory is specified"); |
|
|
} |
|
2846 |
#endif |
#endif |
2847 |
} |
} |
2848 |
|
|
3037 |
return result.Produce(); |
return result.Produce(); |
3038 |
} |
} |
3039 |
|
|
3040 |
String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) { |
String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) { |
3041 |
dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground)); |
dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); |
3042 |
LSCPResultSet result; |
LSCPResultSet result; |
3043 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
3044 |
try { |
try { |
3045 |
int id; |
int id; |
3046 |
InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); |
InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); |
3047 |
if (ScanMode.compare("RECURSIVE") == 0) { |
if (ScanMode.compare("RECURSIVE") == 0) { |
3048 |
id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground); |
id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir); |
3049 |
} else if (ScanMode.compare("NON_RECURSIVE") == 0) { |
} else if (ScanMode.compare("NON_RECURSIVE") == 0) { |
3050 |
id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground); |
id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir); |
3051 |
} else if (ScanMode.compare("FLAT") == 0) { |
} else if (ScanMode.compare("FLAT") == 0) { |
3052 |
id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground); |
id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir); |
3053 |
} else { |
} else { |
3054 |
throw Exception("Unknown scan mode: " + ScanMode); |
throw Exception("Unknown scan mode: " + ScanMode); |
3055 |
} |
} |