25 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
26 |
#include "lscpevent.h" |
#include "lscpevent.h" |
27 |
|
|
28 |
|
#if defined(WIN32) |
29 |
|
#include <windows.h> |
30 |
|
#else |
31 |
#include <fcntl.h> |
#include <fcntl.h> |
32 |
|
#endif |
33 |
|
|
34 |
#if ! HAVE_SQLITE3 |
#if ! HAVE_SQLITE3 |
35 |
#define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built." |
#define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built." |
40 |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
41 |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
42 |
|
|
43 |
|
|
44 |
|
/** |
45 |
|
* Returns a copy of the given string where all special characters are |
46 |
|
* replaced by LSCP escape sequences ("\xHH"). This function shall be used |
47 |
|
* to escape LSCP response fields in case the respective response field is |
48 |
|
* actually defined as using escape sequences in the LSCP specs. |
49 |
|
* |
50 |
|
* @e Caution: DO NOT use this function for escaping path based responses, |
51 |
|
* use the Path class (src/common/Path.h) for this instead! |
52 |
|
*/ |
53 |
|
static String _escapeLscpResponse(String txt) { |
54 |
|
for (int i = 0; i < txt.length(); i++) { |
55 |
|
const char c = txt.c_str()[i]; |
56 |
|
if ( |
57 |
|
!(c >= '0' && c <= '9') && |
58 |
|
!(c >= 'a' && c <= 'z') && |
59 |
|
!(c >= 'A' && c <= 'Z') && |
60 |
|
!(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') && |
61 |
|
!(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') && |
62 |
|
!(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') && |
63 |
|
!(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') && |
64 |
|
!(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') && |
65 |
|
!(c == '@') && !(c == '[') && !(c == ']') && |
66 |
|
!(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') && |
67 |
|
!(c == '|') && !(c == '}') && !(c == '~') |
68 |
|
) { |
69 |
|
// convert the "special" character into a "\xHH" LSCP escape sequence |
70 |
|
char buf[5]; |
71 |
|
snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c)); |
72 |
|
txt.replace(i, 1, buf); |
73 |
|
i += 3; |
74 |
|
} |
75 |
|
} |
76 |
|
return txt; |
77 |
|
} |
78 |
|
|
79 |
/** |
/** |
80 |
* Below are a few static members of the LSCPServer class. |
* Below are a few static members of the LSCPServer class. |
81 |
* The big assumption here is that LSCPServer is going to remain a singleton. |
* The big assumption here is that LSCPServer is going to remain a singleton. |
127 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
128 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
129 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
130 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); |
131 |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
132 |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
133 |
hSocket = -1; |
hSocket = -1; |
134 |
} |
} |
135 |
|
|
136 |
LSCPServer::~LSCPServer() { |
LSCPServer::~LSCPServer() { |
137 |
|
#if defined(WIN32) |
138 |
|
if (hSocket >= 0) closesocket(hSocket); |
139 |
|
#else |
140 |
if (hSocket >= 0) close(hSocket); |
if (hSocket >= 0) close(hSocket); |
141 |
|
#endif |
142 |
} |
} |
143 |
|
|
144 |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
189 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
190 |
} |
} |
191 |
|
|
192 |
|
void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) { |
193 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount)); |
194 |
|
} |
195 |
|
|
196 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
197 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
198 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
204 |
|
|
205 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
206 |
Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; |
Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; |
207 |
NewName = "'" + InstrumentsDb::toEscapedName(NewName) + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
208 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
209 |
} |
} |
210 |
|
|
218 |
|
|
219 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
220 |
Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; |
Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; |
221 |
NewName = "'" + InstrumentsDb::toEscapedName(NewName) + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
222 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
223 |
} |
} |
224 |
|
|
243 |
} |
} |
244 |
|
|
245 |
int LSCPServer::Main() { |
int LSCPServer::Main() { |
246 |
|
#if defined(WIN32) |
247 |
|
WSADATA wsaData; |
248 |
|
int iResult; |
249 |
|
iResult = WSAStartup(MAKEWORD(2,2), &wsaData); |
250 |
|
if (iResult != 0) { |
251 |
|
std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n"; |
252 |
|
exit(EXIT_FAILURE); |
253 |
|
} |
254 |
|
#endif |
255 |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
256 |
if (hSocket < 0) { |
if (hSocket < 0) { |
257 |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
265 |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
266 |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
267 |
std::cerr << "gave up!" << std::endl; |
std::cerr << "gave up!" << std::endl; |
268 |
|
#if defined(WIN32) |
269 |
|
closesocket(hSocket); |
270 |
|
#else |
271 |
close(hSocket); |
close(hSocket); |
272 |
|
#endif |
273 |
//return -1; |
//return -1; |
274 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
275 |
} |
} |
289 |
pSampler->AddVoiceCountListener(&eventHandler); |
pSampler->AddVoiceCountListener(&eventHandler); |
290 |
pSampler->AddStreamCountListener(&eventHandler); |
pSampler->AddStreamCountListener(&eventHandler); |
291 |
pSampler->AddBufferFillListener(&eventHandler); |
pSampler->AddBufferFillListener(&eventHandler); |
292 |
|
pSampler->AddTotalStreamCountListener(&eventHandler); |
293 |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
294 |
pSampler->AddFxSendCountListener(&eventHandler); |
pSampler->AddFxSendCountListener(&eventHandler); |
295 |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
352 |
continue; //Nothing try again |
continue; //Nothing try again |
353 |
if (retval == -1) { |
if (retval == -1) { |
354 |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
355 |
|
#if defined(WIN32) |
356 |
|
closesocket(hSocket); |
357 |
|
#else |
358 |
close(hSocket); |
close(hSocket); |
359 |
|
#endif |
360 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
361 |
} |
} |
362 |
|
|
368 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
369 |
} |
} |
370 |
|
|
371 |
|
#if defined(WIN32) |
372 |
|
u_long nonblock_io = 1; |
373 |
|
if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) { |
374 |
|
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
375 |
|
exit(EXIT_FAILURE); |
376 |
|
} |
377 |
|
#else |
378 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
379 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
380 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
381 |
} |
} |
382 |
|
#endif |
383 |
|
|
384 |
// Parser initialization |
// Parser initialization |
385 |
yyparse_param_t yyparse_param; |
yyparse_param_t yyparse_param; |
438 |
NotifyMutex.Lock(); |
NotifyMutex.Lock(); |
439 |
bufferedCommands.erase(socket); |
bufferedCommands.erase(socket); |
440 |
bufferedNotifies.erase(socket); |
bufferedNotifies.erase(socket); |
441 |
|
#if defined(WIN32) |
442 |
|
closesocket(socket); |
443 |
|
#else |
444 |
close(socket); |
close(socket); |
445 |
|
#endif |
446 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
447 |
} |
} |
448 |
|
|
449 |
|
void LSCPServer::LockRTNotify() { |
450 |
|
RTNotifyMutex.Lock(); |
451 |
|
} |
452 |
|
|
453 |
|
void LSCPServer::UnlockRTNotify() { |
454 |
|
RTNotifyMutex.Unlock(); |
455 |
|
} |
456 |
|
|
457 |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
458 |
int subs = 0; |
int subs = 0; |
459 |
SubscriptionMutex.Lock(); |
SubscriptionMutex.Lock(); |
529 |
char c; |
char c; |
530 |
int i = 0; |
int i = 0; |
531 |
while (true) { |
while (true) { |
532 |
|
#if defined(WIN32) |
533 |
|
int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now |
534 |
|
#else |
535 |
int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now |
int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now |
536 |
|
#endif |
537 |
if (result == 0) { //socket was selected, so 0 here means client has closed the connection |
if (result == 0) { //socket was selected, so 0 here means client has closed the connection |
538 |
CloseConnection(iter); |
CloseConnection(iter); |
539 |
break; |
break; |
548 |
} |
} |
549 |
bufferedCommands[socket] += c; |
bufferedCommands[socket] += c; |
550 |
} |
} |
551 |
|
#if defined(WIN32) |
552 |
|
if (result == SOCKET_ERROR) { |
553 |
|
int wsa_lasterror = WSAGetLastError(); |
554 |
|
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
555 |
|
return false; |
556 |
|
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
557 |
|
CloseConnection(iter); |
558 |
|
break; |
559 |
|
} |
560 |
|
#else |
561 |
if (result == -1) { |
if (result == -1) { |
562 |
if (errno == EAGAIN) //Would block, try again later. |
if (errno == EAGAIN) //Would block, try again later. |
563 |
return false; |
return false; |
596 |
CloseConnection(iter); |
CloseConnection(iter); |
597 |
break; |
break; |
598 |
} |
} |
599 |
|
#endif |
600 |
} |
} |
601 |
return false; |
return false; |
602 |
} |
} |
870 |
LockRTNotify(); |
LockRTNotify(); |
871 |
try { |
try { |
872 |
Engine* pEngine = EngineFactory::Create(EngineName); |
Engine* pEngine = EngineFactory::Create(EngineName); |
873 |
result.Add("DESCRIPTION", pEngine->Description()); |
result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description())); |
874 |
result.Add("VERSION", pEngine->Version()); |
result.Add("VERSION", pEngine->Version()); |
875 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
876 |
} |
} |
943 |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
944 |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
945 |
|
|
946 |
|
// convert the filename into the correct encoding as defined for LSCP |
947 |
|
// (especially in terms of special characters -> escape sequences) |
948 |
|
if (InstrumentFileName != "NONE" && InstrumentFileName != "") { |
949 |
|
#if WIN32 |
950 |
|
InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp(); |
951 |
|
#else |
952 |
|
// assuming POSIX |
953 |
|
InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp(); |
954 |
|
#endif |
955 |
|
} |
956 |
|
|
957 |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
958 |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
959 |
result.Add("INSTRUMENT_NAME", InstrumentName); |
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName)); |
960 |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
961 |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
962 |
result.Add("SOLO", Solo); |
result.Add("SOLO", Solo); |
1898 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx); |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx); |
1899 |
if (iter == mappings.end()) result.Error("there is no map entry with that index"); |
if (iter == mappings.end()) result.Error("there is no map entry with that index"); |
1900 |
else { // found |
else { // found |
1901 |
result.Add("NAME", iter->second.Name); |
|
1902 |
|
// convert the filename into the correct encoding as defined for LSCP |
1903 |
|
// (especially in terms of special characters -> escape sequences) |
1904 |
|
#if WIN32 |
1905 |
|
const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp(); |
1906 |
|
#else |
1907 |
|
// assuming POSIX |
1908 |
|
const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp(); |
1909 |
|
#endif |
1910 |
|
|
1911 |
|
result.Add("NAME", _escapeLscpResponse(iter->second.Name)); |
1912 |
result.Add("ENGINE_NAME", iter->second.EngineName); |
result.Add("ENGINE_NAME", iter->second.EngineName); |
1913 |
result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile); |
result.Add("INSTRUMENT_FILE", instrumentFileName); |
1914 |
result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); |
result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); |
1915 |
String instrumentName; |
String instrumentName; |
1916 |
Engine* pEngine = EngineFactory::Create(iter->second.EngineName); |
Engine* pEngine = EngineFactory::Create(iter->second.EngineName); |
1923 |
} |
} |
1924 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
1925 |
} |
} |
1926 |
result.Add("INSTRUMENT_NAME", instrumentName); |
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); |
1927 |
switch (iter->second.LoadMode) { |
switch (iter->second.LoadMode) { |
1928 |
case MidiInstrumentMapper::ON_DEMAND: |
case MidiInstrumentMapper::ON_DEMAND: |
1929 |
result.Add("LOAD_MODE", "ON_DEMAND"); |
result.Add("LOAD_MODE", "ON_DEMAND"); |
2078 |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
2079 |
LSCPResultSet result; |
LSCPResultSet result; |
2080 |
try { |
try { |
2081 |
result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); |
result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID))); |
2082 |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
2083 |
} catch (Exception e) { |
} catch (Exception e) { |
2084 |
result.Error(e); |
result.Error(e); |
2222 |
} |
} |
2223 |
|
|
2224 |
// success |
// success |
2225 |
result.Add("NAME", pFxSend->Name()); |
result.Add("NAME", _escapeLscpResponse(pFxSend->Name())); |
2226 |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
2227 |
result.Add("LEVEL", ToString(pFxSend->Level())); |
result.Add("LEVEL", ToString(pFxSend->Level())); |
2228 |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
2345 |
*/ |
*/ |
2346 |
String LSCPServer::GetServerInfo() { |
String LSCPServer::GetServerInfo() { |
2347 |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
2348 |
|
const std::string description = |
2349 |
|
_escapeLscpResponse("LinuxSampler - modular, streaming capable sampler"); |
2350 |
LSCPResultSet result; |
LSCPResultSet result; |
2351 |
result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); |
result.Add("DESCRIPTION", description); |
2352 |
result.Add("VERSION", VERSION); |
result.Add("VERSION", VERSION); |
2353 |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
2354 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
2361 |
} |
} |
2362 |
|
|
2363 |
/** |
/** |
2364 |
|
* Will be called by the parser to return the current number of all active streams. |
2365 |
|
*/ |
2366 |
|
String LSCPServer::GetTotalStreamCount() { |
2367 |
|
dmsg(2,("LSCPServer: GetTotalStreamCount()\n")); |
2368 |
|
LSCPResultSet result; |
2369 |
|
result.Add(pSampler->GetDiskStreamCount()); |
2370 |
|
return result.Produce(); |
2371 |
|
} |
2372 |
|
|
2373 |
|
/** |
2374 |
* Will be called by the parser to return the current number of all active voices. |
* Will be called by the parser to return the current number of all active voices. |
2375 |
*/ |
*/ |
2376 |
String LSCPServer::GetTotalVoiceCount() { |
String LSCPServer::GetTotalVoiceCount() { |
2408 |
return result.Produce(); |
return result.Produce(); |
2409 |
} |
} |
2410 |
|
|
2411 |
|
String LSCPServer::GetFileInstruments(String Filename) { |
2412 |
|
dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2413 |
|
LSCPResultSet result; |
2414 |
|
try { |
2415 |
|
VerifyFile(Filename); |
2416 |
|
} catch (Exception e) { |
2417 |
|
result.Error(e); |
2418 |
|
return result.Produce(); |
2419 |
|
} |
2420 |
|
// try to find a sampler engine that can handle the file |
2421 |
|
bool bFound = false; |
2422 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2423 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2424 |
|
Engine* pEngine = NULL; |
2425 |
|
try { |
2426 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2427 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2428 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2429 |
|
if (pManager) { |
2430 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2431 |
|
pManager->GetInstrumentFileContent(Filename); |
2432 |
|
// return the amount of instruments in the file |
2433 |
|
result.Add(IDs.size()); |
2434 |
|
// no more need to ask other engine types |
2435 |
|
bFound = true; |
2436 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2437 |
|
} catch (Exception e) { |
2438 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2439 |
|
} |
2440 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2441 |
|
} |
2442 |
|
|
2443 |
|
if (!bFound) result.Error("Unknown file format"); |
2444 |
|
return result.Produce(); |
2445 |
|
} |
2446 |
|
|
2447 |
|
String LSCPServer::ListFileInstruments(String Filename) { |
2448 |
|
dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2449 |
|
LSCPResultSet result; |
2450 |
|
try { |
2451 |
|
VerifyFile(Filename); |
2452 |
|
} catch (Exception e) { |
2453 |
|
result.Error(e); |
2454 |
|
return result.Produce(); |
2455 |
|
} |
2456 |
|
// try to find a sampler engine that can handle the file |
2457 |
|
bool bFound = false; |
2458 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2459 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2460 |
|
Engine* pEngine = NULL; |
2461 |
|
try { |
2462 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2463 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2464 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2465 |
|
if (pManager) { |
2466 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2467 |
|
pManager->GetInstrumentFileContent(Filename); |
2468 |
|
// return a list of IDs of the instruments in the file |
2469 |
|
String s; |
2470 |
|
for (int j = 0; j < IDs.size(); j++) { |
2471 |
|
if (s.size()) s += ","; |
2472 |
|
s += ToString(IDs[j].Index); |
2473 |
|
} |
2474 |
|
result.Add(s); |
2475 |
|
// no more need to ask other engine types |
2476 |
|
bFound = true; |
2477 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2478 |
|
} catch (Exception e) { |
2479 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2480 |
|
} |
2481 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2482 |
|
} |
2483 |
|
|
2484 |
|
if (!bFound) result.Error("Unknown file format"); |
2485 |
|
return result.Produce(); |
2486 |
|
} |
2487 |
|
|
2488 |
|
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
2489 |
|
dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID)); |
2490 |
|
LSCPResultSet result; |
2491 |
|
try { |
2492 |
|
VerifyFile(Filename); |
2493 |
|
} catch (Exception e) { |
2494 |
|
result.Error(e); |
2495 |
|
return result.Produce(); |
2496 |
|
} |
2497 |
|
InstrumentManager::instrument_id_t id; |
2498 |
|
id.FileName = Filename; |
2499 |
|
id.Index = InstrumentID; |
2500 |
|
// try to find a sampler engine that can handle the file |
2501 |
|
bool bFound = false; |
2502 |
|
bool bFatalErr = false; |
2503 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2504 |
|
for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) { |
2505 |
|
Engine* pEngine = NULL; |
2506 |
|
try { |
2507 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2508 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2509 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2510 |
|
if (pManager) { |
2511 |
|
// check if the instrument index is valid |
2512 |
|
// FIXME: this won't work if an engine only supports parts of the instrument file |
2513 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2514 |
|
pManager->GetInstrumentFileContent(Filename); |
2515 |
|
if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) { |
2516 |
|
std::stringstream ss; |
2517 |
|
ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'"; |
2518 |
|
bFatalErr = true; |
2519 |
|
throw Exception(ss.str()); |
2520 |
|
} |
2521 |
|
// get the info of the requested instrument |
2522 |
|
InstrumentManager::instrument_info_t info = |
2523 |
|
pManager->GetInstrumentInfo(id); |
2524 |
|
// return detailed informations about the file |
2525 |
|
result.Add("NAME", info.InstrumentName); |
2526 |
|
result.Add("FORMAT_FAMILY", engineTypes[i]); |
2527 |
|
result.Add("FORMAT_VERSION", info.FormatVersion); |
2528 |
|
result.Add("PRODUCT", info.Product); |
2529 |
|
result.Add("ARTISTS", info.Artists); |
2530 |
|
// no more need to ask other engine types |
2531 |
|
bFound = true; |
2532 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2533 |
|
} catch (Exception e) { |
2534 |
|
// usually NOOP, as exception is thrown if engine doesn't support file |
2535 |
|
if (bFatalErr) result.Error(e); |
2536 |
|
} |
2537 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2538 |
|
} |
2539 |
|
|
2540 |
|
if (!bFound && !bFatalErr) result.Error("Unknown file format"); |
2541 |
|
return result.Produce(); |
2542 |
|
} |
2543 |
|
|
2544 |
|
void LSCPServer::VerifyFile(String Filename) { |
2545 |
|
#if WIN32 |
2546 |
|
WIN32_FIND_DATA win32FileAttributeData; |
2547 |
|
BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData ); |
2548 |
|
if (!res) { |
2549 |
|
std::stringstream ss; |
2550 |
|
ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError(); |
2551 |
|
throw Exception(ss.str()); |
2552 |
|
} |
2553 |
|
if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { |
2554 |
|
throw Exception("Directory is specified"); |
2555 |
|
} |
2556 |
|
#else |
2557 |
|
struct stat statBuf; |
2558 |
|
int res = stat(Filename.c_str(), &statBuf); |
2559 |
|
if (res) { |
2560 |
|
std::stringstream ss; |
2561 |
|
ss << "Fail to stat `" << Filename << "`: " << strerror(errno); |
2562 |
|
throw Exception(ss.str()); |
2563 |
|
} |
2564 |
|
|
2565 |
|
if (S_ISDIR(statBuf.st_mode)) { |
2566 |
|
throw Exception("Directory is specified"); |
2567 |
|
} |
2568 |
|
#endif |
2569 |
|
} |
2570 |
|
|
2571 |
/** |
/** |
2572 |
* 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 |
2573 |
* server for receiving event messages. |
* server for receiving event messages. |
2649 |
|
|
2650 |
for (int i = 0; i < dirs->size(); i++) { |
for (int i = 0; i < dirs->size(); i++) { |
2651 |
if (list != "") list += ","; |
if (list != "") list += ","; |
2652 |
|
list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'"; |
|
if (Recursive) list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'"; |
|
|
else list += "'" + InstrumentsDb::toEscapedName(dirs->at(i)) + "'"; |
|
2653 |
} |
} |
2654 |
|
|
2655 |
result.Add(list); |
result.Add(list); |
2669 |
try { |
try { |
2670 |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
2671 |
|
|
2672 |
result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description)); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
2673 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
2674 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
2675 |
} catch (Exception e) { |
} catch (Exception e) { |
2826 |
|
|
2827 |
for (int i = 0; i < instrs->size(); i++) { |
for (int i = 0; i < instrs->size(); i++) { |
2828 |
if (list != "") list += ","; |
if (list != "") list += ","; |
2829 |
|
list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'"; |
|
if (Recursive) list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'"; |
|
|
else list += "'" + InstrumentsDb::toEscapedName(instrs->at(i)) + "'"; |
|
2830 |
} |
} |
2831 |
|
|
2832 |
result.Add(list); |
result.Add(list); |
2853 |
result.Add("SIZE", (int)info.Size); |
result.Add("SIZE", (int)info.Size); |
2854 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
2855 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
2856 |
result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description)); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
2857 |
result.Add("IS_DRUM", info.IsDrum); |
result.Add("IS_DRUM", info.IsDrum); |
2858 |
result.Add("PRODUCT", InstrumentsDb::toEscapedText(info.Product)); |
result.Add("PRODUCT", _escapeLscpResponse(info.Product)); |
2859 |
result.Add("ARTISTS", InstrumentsDb::toEscapedText(info.Artists)); |
result.Add("ARTISTS", _escapeLscpResponse(info.Artists)); |
2860 |
result.Add("KEYWORDS", InstrumentsDb::toEscapedText(info.Keywords)); |
result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords)); |
2861 |
} catch (Exception e) { |
} catch (Exception e) { |
2862 |
result.Error(e); |
result.Error(e); |
2863 |
} |
} |
3037 |
} catch (Exception e) { |
} catch (Exception e) { |
3038 |
result.Error(e); |
result.Error(e); |
3039 |
} |
} |
3040 |
|
#else |
3041 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3042 |
|
#endif |
3043 |
|
return result.Produce(); |
3044 |
|
} |
3045 |
|
|
3046 |
|
String LSCPServer::FormatInstrumentsDb() { |
3047 |
|
dmsg(2,("LSCPServer: FormatInstrumentsDb()\n")); |
3048 |
|
LSCPResultSet result; |
3049 |
|
#if HAVE_SQLITE3 |
3050 |
|
try { |
3051 |
|
InstrumentsDb::GetInstrumentsDb()->Format(); |
3052 |
|
} catch (Exception e) { |
3053 |
|
result.Error(e); |
3054 |
|
} |
3055 |
#else |
#else |
3056 |
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3057 |
#endif |
#endif |