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 "../engines/EngineChannelFactory.h" |
37 |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
38 |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
39 |
|
|
61 |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
62 |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
63 |
|
|
64 |
LSCPServer::LSCPServer(Sampler* pSampler) : Thread(true, false, 0, -4) { |
LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) { |
65 |
|
SocketAddress.sin_family = AF_INET; |
66 |
|
SocketAddress.sin_addr.s_addr = addr; |
67 |
|
SocketAddress.sin_port = port; |
68 |
this->pSampler = pSampler; |
this->pSampler = pSampler; |
69 |
LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT"); |
70 |
LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT"); |
71 |
LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT"); |
72 |
LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL"); |
73 |
LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO"); |
74 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
75 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
76 |
|
hSocket = -1; |
77 |
|
} |
78 |
|
|
79 |
|
LSCPServer::~LSCPServer() { |
80 |
|
if (hSocket >= 0) close(hSocket); |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
95 |
} |
} |
96 |
|
|
97 |
int LSCPServer::Main() { |
int LSCPServer::Main() { |
98 |
int hSocket = socket(AF_INET, SOCK_STREAM, 0); |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
99 |
if (hSocket < 0) { |
if (hSocket < 0) { |
100 |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
101 |
//return -1; |
//return -1; |
102 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
103 |
} |
} |
104 |
|
|
|
SocketAddress.sin_family = AF_INET; |
|
|
SocketAddress.sin_port = htons(LSCP_PORT); |
|
|
SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); |
|
|
|
|
105 |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
106 |
std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds..."; |
std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds..."; |
107 |
for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds |
for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds |
128 |
FD_SET(hSocket, &fdSet); |
FD_SET(hSocket, &fdSet); |
129 |
int maxSessions = hSocket; |
int maxSessions = hSocket; |
130 |
|
|
131 |
|
timeval timeout; |
132 |
|
|
133 |
while (true) { |
while (true) { |
134 |
fd_set selectSet = fdSet; |
// check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers |
135 |
int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL); |
{ |
136 |
|
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
137 |
|
std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin(); |
138 |
|
std::set<EngineChannel*>::iterator itEnd = engineChannels.end(); |
139 |
|
for (; itEngineChannel != itEnd; ++itEngineChannel) { |
140 |
|
if ((*itEngineChannel)->StatusChanged()) { |
141 |
|
SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex)); |
142 |
|
} |
143 |
|
} |
144 |
|
} |
145 |
|
|
146 |
|
//Now let's deliver late notifies (if any) |
147 |
|
NotifyBufferMutex.Lock(); |
148 |
|
for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) { |
149 |
|
#ifdef MSG_NOSIGNAL |
150 |
|
send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL); |
151 |
|
#else |
152 |
|
send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0); |
153 |
|
#endif |
154 |
|
} |
155 |
|
bufferedNotifies.clear(); |
156 |
|
NotifyBufferMutex.Unlock(); |
157 |
|
|
158 |
|
fd_set selectSet = fdSet; |
159 |
|
timeout.tv_sec = 0; |
160 |
|
timeout.tv_usec = 100000; |
161 |
|
|
162 |
|
int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout); |
163 |
|
|
164 |
if (retval == 0) |
if (retval == 0) |
165 |
continue; //Nothing try again |
continue; //Nothing try again |
166 |
if (retval == -1) { |
if (retval == -1) { |
204 |
int dummy; // just a temporary hack to fulfill the restart() function prototype |
int dummy; // just a temporary hack to fulfill the restart() function prototype |
205 |
restart(NULL, dummy); // restart the 'scanner' |
restart(NULL, dummy); // restart the 'scanner' |
206 |
currentSocket = (*iter).hSession; //a hack |
currentSocket = (*iter).hSession; //a hack |
207 |
|
dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str())); |
208 |
if ((*iter).bVerbose) { // if echo mode enabled |
if ((*iter).bVerbose) { // if echo mode enabled |
209 |
AnswerClient(bufferedCommands[currentSocket]); |
AnswerClient(bufferedCommands[currentSocket]); |
210 |
} |
} |
220 |
break; |
break; |
221 |
} |
} |
222 |
} |
} |
|
|
|
|
//Now let's deliver late notifies (if any) |
|
|
NotifyBufferMutex.Lock(); |
|
|
for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) { |
|
|
send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0); |
|
|
bufferedNotifies.erase(iterNotify); |
|
|
} |
|
|
NotifyBufferMutex.Unlock(); |
|
223 |
} |
} |
224 |
} |
} |
225 |
|
|
266 |
while (true) { |
while (true) { |
267 |
if (NotifyMutex.Trylock()) { |
if (NotifyMutex.Trylock()) { |
268 |
for(;iter != end; iter++) |
for(;iter != end; iter++) |
269 |
|
#ifdef MSG_NOSIGNAL |
270 |
|
send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL); |
271 |
|
#else |
272 |
send(*iter, notify.c_str(), notify.size(), 0); |
send(*iter, notify.c_str(), notify.size(), 0); |
273 |
|
#endif |
274 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
275 |
break; |
break; |
276 |
} else { |
} else { |
379 |
dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str())); |
dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str())); |
380 |
if (currentSocket != -1) { |
if (currentSocket != -1) { |
381 |
NotifyMutex.Lock(); |
NotifyMutex.Lock(); |
382 |
|
#ifdef MSG_NOSIGNAL |
383 |
|
send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL); |
384 |
|
#else |
385 |
send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0); |
send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0); |
386 |
|
#endif |
387 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
388 |
} |
} |
389 |
} |
} |
491 |
try { |
try { |
492 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
493 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
494 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
495 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet"); |
496 |
if (!pSamplerChannel->GetAudioOutputDevice()) |
if (!pSamplerChannel->GetAudioOutputDevice()) |
497 |
throw LinuxSamplerException("No audio output device connected to sampler channel"); |
throw LinuxSamplerException("No audio output device connected to sampler channel"); |
498 |
if (bBackground) { |
if (bBackground) { |
499 |
InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngine); |
InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel); |
500 |
} |
} |
501 |
else { |
else { |
502 |
// tell the engine which instrument to load |
// tell the engine channel which instrument to load |
503 |
pEngine->PrepareLoadInstrument(Filename.c_str(), uiInstrument); |
pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument); |
504 |
// actually start to load the instrument (blocks until completed) |
// actually start to load the instrument (blocks until completed) |
505 |
pEngine->LoadInstrument(); |
pEngineChannel->LoadInstrument(); |
506 |
} |
} |
507 |
} |
} |
508 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
512 |
} |
} |
513 |
|
|
514 |
/** |
/** |
515 |
* 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 |
516 |
|
* sampler channel. |
517 |
*/ |
*/ |
518 |
String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) { |
String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) { |
519 |
dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel)); |
dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel)); |
520 |
LSCPResultSet result; |
LSCPResultSet result; |
521 |
try { |
try { |
|
Engine::type_t type; |
|
|
if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig; |
|
|
else throw LinuxSamplerException("Unknown engine type"); |
|
522 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
523 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
524 |
LockRTNotify(); |
LockRTNotify(); |
525 |
pSamplerChannel->LoadEngine(type); |
pSamplerChannel->SetEngineType(EngineName); |
526 |
|
if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1); |
527 |
UnlockRTNotify(); |
UnlockRTNotify(); |
528 |
} |
} |
529 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
582 |
} |
} |
583 |
|
|
584 |
/** |
/** |
585 |
* Will be called by the parser to get all available engines. |
* Will be called by the parser to get the amount of all available engines. |
586 |
*/ |
*/ |
587 |
String LSCPServer::GetAvailableEngines() { |
String LSCPServer::GetAvailableEngines() { |
588 |
dmsg(2,("LSCPServer: GetAvailableEngines()\n")); |
dmsg(2,("LSCPServer: GetAvailableEngines()\n")); |
589 |
LSCPResultSet result("GigEngine"); |
LSCPResultSet result("1"); |
590 |
return result.Produce(); |
return result.Produce(); |
591 |
} |
} |
592 |
|
|
593 |
/** |
/** |
594 |
* Will be called by the parser to get descriptions for a particular engine. |
* Will be called by the parser to get a list of all available engines. |
595 |
|
*/ |
596 |
|
String LSCPServer::ListAvailableEngines() { |
597 |
|
dmsg(2,("LSCPServer: ListAvailableEngines()\n")); |
598 |
|
LSCPResultSet result("\'GIG\'"); |
599 |
|
return result.Produce(); |
600 |
|
} |
601 |
|
|
602 |
|
/** |
603 |
|
* Will be called by the parser to get descriptions for a particular |
604 |
|
* sampler engine. |
605 |
*/ |
*/ |
606 |
String LSCPServer::GetEngineInfo(String EngineName) { |
String LSCPServer::GetEngineInfo(String EngineName) { |
607 |
dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str())); |
dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str())); |
608 |
LSCPResultSet result; |
LSCPResultSet result; |
609 |
try { |
try { |
610 |
if ((EngineName == "GigEngine") || (EngineName == "gig")) { |
Engine* pEngine = EngineFactory::Create(EngineName); |
611 |
Engine* pEngine = new LinuxSampler::gig::Engine; |
result.Add("DESCRIPTION", pEngine->Description()); |
612 |
result.Add("DESCRIPTION", pEngine->Description()); |
result.Add("VERSION", pEngine->Version()); |
613 |
result.Add("VERSION", pEngine->Version()); |
EngineFactory::Destroy(pEngine); |
|
delete pEngine; |
|
|
} |
|
|
else throw LinuxSamplerException("Unknown engine type"); |
|
614 |
} |
} |
615 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
616 |
result.Error(e); |
result.Error(e); |
628 |
try { |
try { |
629 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
630 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
631 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
632 |
|
|
633 |
//Defaults values |
//Defaults values |
634 |
String EngineName = "NONE"; |
String EngineName = "NONE"; |
639 |
int InstrumentStatus = -1; |
int InstrumentStatus = -1; |
640 |
int AudioOutputChannels = 0; |
int AudioOutputChannels = 0; |
641 |
String AudioRouting; |
String AudioRouting; |
642 |
|
int Mute = 0; |
643 |
|
bool Solo = false; |
644 |
|
|
645 |
if (pEngine) { |
if (pEngineChannel) { |
646 |
EngineName = pEngine->EngineName(); |
EngineName = pEngineChannel->EngineName(); |
647 |
AudioOutputChannels = pEngine->Channels(); |
AudioOutputChannels = pEngineChannel->Channels(); |
648 |
Volume = pEngine->Volume(); |
Volume = pEngineChannel->Volume(); |
649 |
InstrumentStatus = pEngine->InstrumentStatus(); |
InstrumentStatus = pEngineChannel->InstrumentStatus(); |
650 |
InstrumentIndex = pEngine->InstrumentIndex(); |
InstrumentIndex = pEngineChannel->InstrumentIndex(); |
651 |
if (InstrumentIndex != -1) |
if (InstrumentIndex != -1) { |
652 |
{ |
InstrumentFileName = pEngineChannel->InstrumentFileName(); |
653 |
InstrumentFileName = pEngine->InstrumentFileName(); |
InstrumentName = pEngineChannel->InstrumentName(); |
654 |
InstrumentName = pEngine->InstrumentName(); |
} |
655 |
} |
for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { |
|
for (int chan = 0; chan < pEngine->Channels(); chan++) { |
|
656 |
if (AudioRouting != "") AudioRouting += ","; |
if (AudioRouting != "") AudioRouting += ","; |
657 |
AudioRouting += ToString(pEngine->OutputChannel(chan)); |
AudioRouting += ToString(pEngineChannel->OutputChannel(chan)); |
658 |
} |
} |
659 |
|
Mute = pEngineChannel->GetMute(); |
660 |
|
Solo = pEngineChannel->GetSolo(); |
661 |
} |
} |
662 |
|
|
663 |
result.Add("ENGINE_NAME", EngineName); |
result.Add("ENGINE_NAME", EngineName); |
670 |
|
|
671 |
result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice())); |
result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice())); |
672 |
result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort()); |
result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort()); |
673 |
if (pSamplerChannel->GetMidiInputChannel() == MidiInputPort::midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
674 |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
675 |
|
|
676 |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
677 |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
678 |
result.Add("INSTRUMENT_NAME", InstrumentName); |
result.Add("INSTRUMENT_NAME", InstrumentName); |
679 |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
680 |
|
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
681 |
|
result.Add("SOLO", Solo); |
682 |
} |
} |
683 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
684 |
result.Error(e); |
result.Error(e); |
696 |
try { |
try { |
697 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
698 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
699 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
700 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel"); |
701 |
result.Add(pEngine->VoiceCount()); |
if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel"); |
702 |
|
result.Add(pEngineChannel->GetEngine()->VoiceCount()); |
703 |
} |
} |
704 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
705 |
result.Error(e); |
result.Error(e); |
717 |
try { |
try { |
718 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
719 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
720 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
721 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
722 |
result.Add(pEngine->DiskStreamCount()); |
if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel"); |
723 |
|
result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); |
724 |
} |
} |
725 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
726 |
result.Error(e); |
result.Error(e); |
738 |
try { |
try { |
739 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
740 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
741 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
742 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
743 |
if (!pEngine->DiskStreamSupported()) |
if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel"); |
744 |
result.Add("NA"); |
if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); |
745 |
else { |
else { |
746 |
switch (ResponseType) { |
switch (ResponseType) { |
747 |
case fill_response_bytes: |
case fill_response_bytes: |
748 |
result.Add(pEngine->DiskStreamBufferFillBytes()); |
result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes()); |
749 |
break; |
break; |
750 |
case fill_response_percentage: |
case fill_response_percentage: |
751 |
result.Add(pEngine->DiskStreamBufferFillPercentage()); |
result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage()); |
752 |
break; |
break; |
753 |
default: |
default: |
754 |
throw LinuxSamplerException("Unknown fill response type"); |
throw LinuxSamplerException("Unknown fill response type"); |
755 |
} |
} |
765 |
dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n")); |
dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n")); |
766 |
LSCPResultSet result; |
LSCPResultSet result; |
767 |
try { |
try { |
768 |
|
int n = AudioOutputDeviceFactory::AvailableDrivers().size(); |
769 |
|
result.Add(n); |
770 |
|
} |
771 |
|
catch (LinuxSamplerException e) { |
772 |
|
result.Error(e); |
773 |
|
} |
774 |
|
return result.Produce(); |
775 |
|
} |
776 |
|
|
777 |
|
String LSCPServer::ListAvailableAudioOutputDrivers() { |
778 |
|
dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n")); |
779 |
|
LSCPResultSet result; |
780 |
|
try { |
781 |
String s = AudioOutputDeviceFactory::AvailableDriversAsString(); |
String s = AudioOutputDeviceFactory::AvailableDriversAsString(); |
782 |
result.Add(s); |
result.Add(s); |
783 |
} |
} |
791 |
dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n")); |
dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n")); |
792 |
LSCPResultSet result; |
LSCPResultSet result; |
793 |
try { |
try { |
794 |
|
int n = MidiInputDeviceFactory::AvailableDrivers().size(); |
795 |
|
result.Add(n); |
796 |
|
} |
797 |
|
catch (LinuxSamplerException e) { |
798 |
|
result.Error(e); |
799 |
|
} |
800 |
|
return result.Produce(); |
801 |
|
} |
802 |
|
|
803 |
|
String LSCPServer::ListAvailableMidiInputDrivers() { |
804 |
|
dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n")); |
805 |
|
LSCPResultSet result; |
806 |
|
try { |
807 |
String s = MidiInputDeviceFactory::AvailableDriversAsString(); |
String s = MidiInputDeviceFactory::AvailableDriversAsString(); |
808 |
result.Add(s); |
result.Add(s); |
809 |
} |
} |
1231 |
try { |
try { |
1232 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1233 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1234 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1235 |
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)); |
1236 |
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)); |
1237 |
pEngine->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel); |
pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel); |
1238 |
} |
} |
1239 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1240 |
result.Error(e); |
result.Error(e); |
1316 |
try { |
try { |
1317 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1318 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1319 |
pSamplerChannel->SetMidiInputChannel((MidiInputPort::midi_chan_t) MIDIChannel); |
pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel); |
1320 |
} |
} |
1321 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1322 |
result.Error(e); |
result.Error(e); |
1393 |
std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices(); |
std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices(); |
1394 |
if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); |
if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); |
1395 |
MidiInputDevice* pDevice = devices[MIDIDeviceId]; |
MidiInputDevice* pDevice = devices[MIDIDeviceId]; |
1396 |
pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputPort::midi_chan_t) MIDIChannel); |
pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel); |
1397 |
} |
} |
1398 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1399 |
result.Error(e); |
result.Error(e); |
1411 |
try { |
try { |
1412 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1413 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1414 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1415 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
1416 |
pEngine->Volume(dVolume); |
pEngineChannel->Volume(dVolume); |
1417 |
} |
} |
1418 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1419 |
result.Error(e); |
result.Error(e); |
1422 |
} |
} |
1423 |
|
|
1424 |
/** |
/** |
1425 |
|
* Will be called by the parser to mute/unmute particular sampler channel. |
1426 |
|
*/ |
1427 |
|
String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) { |
1428 |
|
dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel)); |
1429 |
|
LSCPResultSet result; |
1430 |
|
try { |
1431 |
|
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1432 |
|
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1433 |
|
|
1434 |
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1435 |
|
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
1436 |
|
|
1437 |
|
if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); |
1438 |
|
else pEngineChannel->SetMute(1); |
1439 |
|
} catch (LinuxSamplerException e) { |
1440 |
|
result.Error(e); |
1441 |
|
} |
1442 |
|
return result.Produce(); |
1443 |
|
} |
1444 |
|
|
1445 |
|
/** |
1446 |
|
* Will be called by the parser to solo particular sampler channel. |
1447 |
|
*/ |
1448 |
|
String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) { |
1449 |
|
dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel)); |
1450 |
|
LSCPResultSet result; |
1451 |
|
try { |
1452 |
|
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1453 |
|
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1454 |
|
|
1455 |
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1456 |
|
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
1457 |
|
|
1458 |
|
bool oldSolo = pEngineChannel->GetSolo(); |
1459 |
|
bool hadSoloChannel = HasSoloChannel(); |
1460 |
|
|
1461 |
|
pEngineChannel->SetSolo(bSolo); |
1462 |
|
|
1463 |
|
if(!oldSolo && bSolo) { |
1464 |
|
if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0); |
1465 |
|
if(!hadSoloChannel) MuteNonSoloChannels(); |
1466 |
|
} |
1467 |
|
|
1468 |
|
if(oldSolo && !bSolo) { |
1469 |
|
if(!HasSoloChannel()) UnmuteChannels(); |
1470 |
|
else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1); |
1471 |
|
} |
1472 |
|
} catch (LinuxSamplerException e) { |
1473 |
|
result.Error(e); |
1474 |
|
} |
1475 |
|
return result.Produce(); |
1476 |
|
} |
1477 |
|
|
1478 |
|
/** |
1479 |
|
* Determines whether there is at least one solo channel in the channel list. |
1480 |
|
* |
1481 |
|
* @returns true if there is at least one solo channel in the channel list, |
1482 |
|
* false otherwise. |
1483 |
|
*/ |
1484 |
|
bool LSCPServer::HasSoloChannel() { |
1485 |
|
std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels(); |
1486 |
|
std::map<uint,SamplerChannel*>::iterator iter = channels.begin(); |
1487 |
|
for (; iter != channels.end(); iter++) { |
1488 |
|
EngineChannel* c = iter->second->GetEngineChannel(); |
1489 |
|
if(c && c->GetSolo()) return true; |
1490 |
|
} |
1491 |
|
|
1492 |
|
return false; |
1493 |
|
} |
1494 |
|
|
1495 |
|
/** |
1496 |
|
* Mutes all unmuted non-solo channels. Notice that the channels are muted |
1497 |
|
* with -1 which indicates that they are muted because of the presence |
1498 |
|
* of a solo channel(s). Channels muted with -1 will be automatically unmuted |
1499 |
|
* when there are no solo channels left. |
1500 |
|
*/ |
1501 |
|
void LSCPServer::MuteNonSoloChannels() { |
1502 |
|
dmsg(2,("LSCPServer: MuteNonSoloChannels()\n")); |
1503 |
|
std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels(); |
1504 |
|
std::map<uint,SamplerChannel*>::iterator iter = channels.begin(); |
1505 |
|
for (; iter != channels.end(); iter++) { |
1506 |
|
EngineChannel* c = iter->second->GetEngineChannel(); |
1507 |
|
if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1); |
1508 |
|
} |
1509 |
|
} |
1510 |
|
|
1511 |
|
/** |
1512 |
|
* Unmutes all channels that are muted because of the presence |
1513 |
|
* of a solo channel(s). |
1514 |
|
*/ |
1515 |
|
void LSCPServer::UnmuteChannels() { |
1516 |
|
dmsg(2,("LSCPServer: UnmuteChannels()\n")); |
1517 |
|
std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels(); |
1518 |
|
std::map<uint,SamplerChannel*>::iterator iter = channels.begin(); |
1519 |
|
for (; iter != channels.end(); iter++) { |
1520 |
|
EngineChannel* c = iter->second->GetEngineChannel(); |
1521 |
|
if(c && c->GetMute() == -1) c->SetMute(0); |
1522 |
|
} |
1523 |
|
} |
1524 |
|
|
1525 |
|
/** |
1526 |
* Will be called by the parser to reset a particular sampler channel. |
* Will be called by the parser to reset a particular sampler channel. |
1527 |
*/ |
*/ |
1528 |
String LSCPServer::ResetChannel(uint uiSamplerChannel) { |
String LSCPServer::ResetChannel(uint uiSamplerChannel) { |
1531 |
try { |
try { |
1532 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
1533 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
1534 |
Engine* pEngine = pSamplerChannel->GetEngine(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
1535 |
if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); |
if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); |
1536 |
pEngine->Reset(); |
pEngineChannel->Reset(); |
1537 |
} |
} |
1538 |
catch (LinuxSamplerException e) { |
catch (LinuxSamplerException e) { |
1539 |
result.Error(e); |
result.Error(e); |
1552 |
} |
} |
1553 |
|
|
1554 |
/** |
/** |
1555 |
|
* Will be called by the parser to return general informations about this |
1556 |
|
* sampler. |
1557 |
|
*/ |
1558 |
|
String LSCPServer::GetServerInfo() { |
1559 |
|
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
1560 |
|
LSCPResultSet result; |
1561 |
|
result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); |
1562 |
|
result.Add("VERSION", VERSION); |
1563 |
|
result.Add("PROTOCOL_VERSION", "1.0"); |
1564 |
|
return result.Produce(); |
1565 |
|
} |
1566 |
|
|
1567 |
|
/** |
1568 |
|
* Will be called by the parser to return the current number of all active voices. |
1569 |
|
*/ |
1570 |
|
String LSCPServer::GetTotalVoiceCount() { |
1571 |
|
dmsg(2,("LSCPServer: GetTotalVoiceCount()\n")); |
1572 |
|
LSCPResultSet result; |
1573 |
|
result.Add(pSampler->GetVoiceCount()); |
1574 |
|
return result.Produce(); |
1575 |
|
} |
1576 |
|
|
1577 |
|
/** |
1578 |
|
* Will be called by the parser to return the maximum number of voices. |
1579 |
|
*/ |
1580 |
|
String LSCPServer::GetTotalVoiceCountMax() { |
1581 |
|
dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n")); |
1582 |
|
LSCPResultSet result; |
1583 |
|
result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES); |
1584 |
|
return result.Produce(); |
1585 |
|
} |
1586 |
|
|
1587 |
|
/** |
1588 |
* Will be called by the parser to subscribe a client (frontend) on the |
* Will be called by the parser to subscribe a client (frontend) on the |
1589 |
* server for receiving event messages. |
* server for receiving event messages. |
1590 |
*/ |
*/ |
1610 |
return result.Produce(); |
return result.Produce(); |
1611 |
} |
} |
1612 |
|
|
1613 |
|
static int select_callback(void * lscpResultSet, int argc, |
1614 |
|
char **argv, char **azColName) |
1615 |
|
{ |
1616 |
|
LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet; |
1617 |
|
resultSet->Add(argc, argv); |
1618 |
|
return 0; |
1619 |
|
} |
1620 |
|
|
1621 |
|
String LSCPServer::QueryDatabase(String query) { |
1622 |
|
LSCPResultSet result; |
1623 |
|
#if HAVE_SQLITE3 |
1624 |
|
char* zErrMsg = NULL; |
1625 |
|
sqlite3 *db; |
1626 |
|
String selectStr = "SELECT " + query; |
1627 |
|
|
1628 |
|
int rc = sqlite3_open("linuxsampler.db", &db); |
1629 |
|
if (rc == SQLITE_OK) |
1630 |
|
{ |
1631 |
|
rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg); |
1632 |
|
} |
1633 |
|
if ( rc != SQLITE_OK ) |
1634 |
|
{ |
1635 |
|
result.Error(String(zErrMsg), rc); |
1636 |
|
} |
1637 |
|
sqlite3_close(db); |
1638 |
|
#else |
1639 |
|
result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0); |
1640 |
|
#endif |
1641 |
|
return result.Produce(); |
1642 |
|
} |
1643 |
|
|
1644 |
/** |
/** |
1645 |
* 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 |
1646 |
* mode is enabled, all commands from the client will (immediately) be |
* mode is enabled, all commands from the client will (immediately) be |