24 |
#include "lscpserver.h" |
#include "lscpserver.h" |
25 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
26 |
#include "lscpevent.h" |
#include "lscpevent.h" |
27 |
|
//#include "../common/global.h" |
28 |
|
|
29 |
#include "../engines/gig/Engine.h" |
#include <fcntl.h> |
30 |
|
|
31 |
|
#if HAVE_SQLITE3 |
32 |
|
# include "sqlite3.h" |
33 |
|
#endif |
34 |
|
|
35 |
|
#include "../engines/EngineFactory.h" |
36 |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
37 |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
38 |
|
|
68 |
LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL"); |
69 |
LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO"); |
70 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
71 |
|
hSocket = -1; |
72 |
|
} |
73 |
|
|
74 |
|
LSCPServer::~LSCPServer() { |
75 |
|
if (hSocket >= 0) close(hSocket); |
76 |
} |
} |
77 |
|
|
78 |
/** |
/** |
90 |
} |
} |
91 |
|
|
92 |
int LSCPServer::Main() { |
int LSCPServer::Main() { |
93 |
int hSocket = socket(AF_INET, SOCK_STREAM, 0); |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
94 |
if (hSocket < 0) { |
if (hSocket < 0) { |
95 |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
96 |
//return -1; |
//return -1; |
173 |
int dummy; // just a temporary hack to fulfill the restart() function prototype |
int dummy; // just a temporary hack to fulfill the restart() function prototype |
174 |
restart(NULL, dummy); // restart the 'scanner' |
restart(NULL, dummy); // restart the 'scanner' |
175 |
currentSocket = (*iter).hSession; //a hack |
currentSocket = (*iter).hSession; //a hack |
176 |
|
dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str())); |
177 |
if ((*iter).bVerbose) { // if echo mode enabled |
if ((*iter).bVerbose) { // if echo mode enabled |
178 |
AnswerClient(bufferedCommands[currentSocket]); |
AnswerClient(bufferedCommands[currentSocket]); |
179 |
} |
} |
460 |
try { |
try { |
461 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
462 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
463 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
464 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet"); |
465 |
if (!pSamplerChannel->GetAudioOutputDevice()) |
if (!pSamplerChannel->GetAudioOutputDevice()) |
466 |
throw LinuxSamplerException("No audio output device connected to sampler channel"); |
throw LinuxSamplerException("No audio output device connected to sampler channel"); |
467 |
if (bBackground) { |
if (bBackground) { |
468 |
InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngine); |
InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel); |
469 |
} |
} |
470 |
else { |
else { |
471 |
// tell the engine which instrument to load |
// tell the engine channel which instrument to load |
472 |
pEngine->PrepareLoadInstrument(Filename.c_str(), uiInstrument); |
pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument); |
473 |
// actually start to load the instrument (blocks until completed) |
// actually start to load the instrument (blocks until completed) |
474 |
pEngine->LoadInstrument(); |
pEngineChannel->LoadInstrument(); |
475 |
} |
} |
476 |
} |
} |
477 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
481 |
} |
} |
482 |
|
|
483 |
/** |
/** |
484 |
* Will be called by the parser to load and deploy an engine. |
* Will be called by the parser to assign a sampler engine type to a |
485 |
|
* sampler channel. |
486 |
*/ |
*/ |
487 |
String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) { |
String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) { |
488 |
dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel)); |
dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel)); |
489 |
LSCPResultSet result; |
LSCPResultSet result; |
490 |
try { |
try { |
|
Engine::type_t type; |
|
|
if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig; |
|
|
else throw LinuxSamplerException("Unknown engine type"); |
|
491 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
492 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
493 |
LockRTNotify(); |
LockRTNotify(); |
494 |
pSamplerChannel->LoadEngine(type); |
pSamplerChannel->SetEngineType(EngineName); |
495 |
UnlockRTNotify(); |
UnlockRTNotify(); |
496 |
} |
} |
497 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
559 |
} |
} |
560 |
|
|
561 |
/** |
/** |
562 |
* Will be called by the parser to get descriptions for a particular engine. |
* Will be called by the parser to get descriptions for a particular |
563 |
|
* sampler engine. |
564 |
*/ |
*/ |
565 |
String LSCPServer::GetEngineInfo(String EngineName) { |
String LSCPServer::GetEngineInfo(String EngineName) { |
566 |
dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str())); |
dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str())); |
567 |
LSCPResultSet result; |
LSCPResultSet result; |
568 |
try { |
try { |
569 |
if ((EngineName == "GigEngine") || (EngineName == "gig")) { |
Engine* pEngine = EngineFactory::Create(EngineName); |
570 |
Engine* pEngine = new LinuxSampler::gig::Engine; |
result.Add("DESCRIPTION", pEngine->Description()); |
571 |
result.Add("DESCRIPTION", pEngine->Description()); |
result.Add("VERSION", pEngine->Version()); |
572 |
result.Add("VERSION", pEngine->Version()); |
delete pEngine; |
|
delete pEngine; |
|
|
} |
|
|
else throw LinuxSamplerException("Unknown engine type"); |
|
573 |
} |
} |
574 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
575 |
result.Error(e); |
result.Error(e); |
587 |
try { |
try { |
588 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
589 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
590 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
591 |
|
|
592 |
//Defaults values |
//Defaults values |
593 |
String EngineName = "NONE"; |
String EngineName = "NONE"; |
599 |
int AudioOutputChannels = 0; |
int AudioOutputChannels = 0; |
600 |
String AudioRouting; |
String AudioRouting; |
601 |
|
|
602 |
if (pEngine) { |
if (pEngineChannel) { |
603 |
EngineName = pEngine->EngineName(); |
EngineName = pEngineChannel->EngineName(); |
604 |
AudioOutputChannels = pEngine->Channels(); |
AudioOutputChannels = pEngineChannel->Channels(); |
605 |
Volume = pEngine->Volume(); |
Volume = pEngineChannel->Volume(); |
606 |
InstrumentStatus = pEngine->InstrumentStatus(); |
InstrumentStatus = pEngineChannel->InstrumentStatus(); |
607 |
InstrumentIndex = pEngine->InstrumentIndex(); |
InstrumentIndex = pEngineChannel->InstrumentIndex(); |
608 |
if (InstrumentIndex != -1) |
if (InstrumentIndex != -1) { |
609 |
{ |
InstrumentFileName = pEngineChannel->InstrumentFileName(); |
610 |
InstrumentFileName = pEngine->InstrumentFileName(); |
InstrumentName = pEngineChannel->InstrumentName(); |
611 |
InstrumentName = pEngine->InstrumentName(); |
} |
612 |
} |
for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { |
|
for (int chan = 0; chan < pEngine->Channels(); chan++) { |
|
613 |
if (AudioRouting != "") AudioRouting += ","; |
if (AudioRouting != "") AudioRouting += ","; |
614 |
AudioRouting += ToString(pEngine->OutputChannel(chan)); |
AudioRouting += ToString(pEngineChannel->OutputChannel(chan)); |
615 |
} |
} |
616 |
} |
} |
617 |
|
|
649 |
try { |
try { |
650 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
651 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
652 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
653 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel"); |
654 |
result.Add(pEngine->VoiceCount()); |
result.Add(pEngineChannel->GetEngine()->VoiceCount()); |
655 |
} |
} |
656 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
657 |
result.Error(e); |
result.Error(e); |
669 |
try { |
try { |
670 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
671 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
672 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
673 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
674 |
result.Add(pEngine->DiskStreamCount()); |
result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); |
675 |
} |
} |
676 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
677 |
result.Error(e); |
result.Error(e); |
689 |
try { |
try { |
690 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
691 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
692 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
693 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
694 |
if (!pEngine->DiskStreamSupported()) |
if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); |
|
result.Add("NA"); |
|
695 |
else { |
else { |
696 |
switch (ResponseType) { |
switch (ResponseType) { |
697 |
case fill_response_bytes: |
case fill_response_bytes: |
698 |
result.Add(pEngine->DiskStreamBufferFillBytes()); |
result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes()); |
699 |
break; |
break; |
700 |
case fill_response_percentage: |
case fill_response_percentage: |
701 |
result.Add(pEngine->DiskStreamBufferFillPercentage()); |
result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage()); |
702 |
break; |
break; |
703 |
default: |
default: |
704 |
throw LinuxSamplerException("Unknown fill response type"); |
throw LinuxSamplerException("Unknown fill response type"); |
705 |
} |
} |
1155 |
try { |
try { |
1156 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1157 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1158 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1159 |
if (!pEngine) throw LinuxSamplerException("No engine deployed on sampler channel " + ToString(uiSamplerChannel)); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel)); |
1160 |
if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel)); |
1161 |
pEngine->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel); |
pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel); |
1162 |
} |
} |
1163 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1164 |
result.Error(e); |
result.Error(e); |
1335 |
try { |
try { |
1336 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1337 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1338 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1339 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
1340 |
pEngine->Volume(dVolume); |
pEngineChannel->Volume(dVolume); |
1341 |
} |
} |
1342 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1343 |
result.Error(e); |
result.Error(e); |
1354 |
try { |
try { |
1355 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1356 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1357 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1358 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
1359 |
pEngine->Reset(); |
pEngineChannel->GetEngine()->Reset(); |
1360 |
} |
} |
1361 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1362 |
result.Error(e); |
result.Error(e); |
1400 |
return result.Produce(); |
return result.Produce(); |
1401 |
} |
} |
1402 |
|
|
1403 |
|
static int select_callback(void * lscpResultSet, int argc, |
1404 |
|
char **argv, char **azColName) |
1405 |
|
{ |
1406 |
|
LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet; |
1407 |
|
resultSet->Add(argc, argv); |
1408 |
|
return 0; |
1409 |
|
} |
1410 |
|
|
1411 |
|
String LSCPServer::QueryDatabase(String query) { |
1412 |
|
LSCPResultSet result; |
1413 |
|
#if HAVE_SQLITE3 |
1414 |
|
char* zErrMsg = NULL; |
1415 |
|
sqlite3 *db; |
1416 |
|
String selectStr = "SELECT " + query; |
1417 |
|
|
1418 |
|
int rc = sqlite3_open("linuxsampler.db", &db); |
1419 |
|
if (rc == SQLITE_OK) |
1420 |
|
{ |
1421 |
|
rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg); |
1422 |
|
} |
1423 |
|
if ( rc != SQLITE_OK ) |
1424 |
|
{ |
1425 |
|
result.Error(String(zErrMsg), rc); |
1426 |
|
} |
1427 |
|
sqlite3_close(db); |
1428 |
|
#else |
1429 |
|
result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0); |
1430 |
|
#endif |
1431 |
|
return result.Produce(); |
1432 |
|
} |
1433 |
|
|
1434 |
/** |
/** |
1435 |
* Will be called by the parser to enable or disable echo mode; if echo |
* Will be called by the parser to enable or disable echo mode; if echo |
1436 |
* mode is enabled, all commands from the client will (immediately) be |
* mode is enabled, all commands from the client will (immediately) be |