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 - 2007 Christian Schoenebeck * |
* Copyright (C) 2005 - 2008 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 * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
#include <algorithm> |
25 |
|
|
26 |
#include "lscpserver.h" |
#include "lscpserver.h" |
27 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
28 |
#include "lscpevent.h" |
#include "lscpevent.h" |
29 |
|
|
30 |
|
#if defined(WIN32) |
31 |
|
#include <windows.h> |
32 |
|
#else |
33 |
#include <fcntl.h> |
#include <fcntl.h> |
34 |
|
#endif |
35 |
|
|
36 |
#if ! HAVE_SQLITE3 |
#if ! HAVE_SQLITE3 |
37 |
#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." |
42 |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
43 |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
44 |
|
|
45 |
|
|
46 |
|
/** |
47 |
|
* Returns a copy of the given string where all special characters are |
48 |
|
* replaced by LSCP escape sequences ("\xHH"). This function shall be used |
49 |
|
* to escape LSCP response fields in case the respective response field is |
50 |
|
* actually defined as using escape sequences in the LSCP specs. |
51 |
|
* |
52 |
|
* @e Caution: DO NOT use this function for escaping path based responses, |
53 |
|
* use the Path class (src/common/Path.h) for this instead! |
54 |
|
*/ |
55 |
|
static String _escapeLscpResponse(String txt) { |
56 |
|
for (int i = 0; i < txt.length(); i++) { |
57 |
|
const char c = txt.c_str()[i]; |
58 |
|
if ( |
59 |
|
!(c >= '0' && c <= '9') && |
60 |
|
!(c >= 'a' && c <= 'z') && |
61 |
|
!(c >= 'A' && c <= 'Z') && |
62 |
|
!(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') && |
63 |
|
!(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') && |
64 |
|
!(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') && |
65 |
|
!(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') && |
66 |
|
!(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') && |
67 |
|
!(c == '@') && !(c == '[') && !(c == ']') && |
68 |
|
!(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') && |
69 |
|
!(c == '|') && !(c == '}') && !(c == '~') |
70 |
|
) { |
71 |
|
// convert the "special" character into a "\xHH" LSCP escape sequence |
72 |
|
char buf[5]; |
73 |
|
snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c)); |
74 |
|
txt.replace(i, 1, buf); |
75 |
|
i += 3; |
76 |
|
} |
77 |
|
} |
78 |
|
return txt; |
79 |
|
} |
80 |
|
|
81 |
/** |
/** |
82 |
* Below are a few static members of the LSCPServer class. |
* Below are a few static members of the LSCPServer class. |
83 |
* 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. |
103 |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
104 |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
105 |
|
|
106 |
LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) { |
LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) { |
107 |
SocketAddress.sin_family = AF_INET; |
SocketAddress.sin_family = AF_INET; |
108 |
SocketAddress.sin_addr.s_addr = addr; |
SocketAddress.sin_addr.s_addr = addr; |
109 |
SocketAddress.sin_port = port; |
SocketAddress.sin_port = port; |
129 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
130 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
131 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
132 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); |
133 |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
134 |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
135 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI"); |
136 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI"); |
137 |
hSocket = -1; |
hSocket = -1; |
138 |
} |
} |
139 |
|
|
140 |
LSCPServer::~LSCPServer() { |
LSCPServer::~LSCPServer() { |
141 |
|
#if defined(WIN32) |
142 |
|
if (hSocket >= 0) closesocket(hSocket); |
143 |
|
#else |
144 |
if (hSocket >= 0) close(hSocket); |
if (hSocket >= 0) close(hSocket); |
145 |
|
#endif |
146 |
|
} |
147 |
|
|
148 |
|
LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) { |
149 |
|
this->pParent = pParent; |
150 |
|
} |
151 |
|
|
152 |
|
LSCPServer::EventHandler::~EventHandler() { |
153 |
|
std::vector<midi_listener_entry> l = channelMidiListeners; |
154 |
|
channelMidiListeners.clear(); |
155 |
|
for (int i = 0; i < l.size(); i++) |
156 |
|
delete l[i].pMidiListener; |
157 |
} |
} |
158 |
|
|
159 |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
160 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
161 |
} |
} |
162 |
|
|
163 |
|
void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) { |
164 |
|
pChannel->AddEngineChangeListener(this); |
165 |
|
} |
166 |
|
|
167 |
|
void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) { |
168 |
|
if (!pChannel->GetEngineChannel()) return; |
169 |
|
EngineToBeChanged(pChannel->Index()); |
170 |
|
} |
171 |
|
|
172 |
|
void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) { |
173 |
|
SamplerChannel* pSamplerChannel = |
174 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
175 |
|
if (!pSamplerChannel) return; |
176 |
|
EngineChannel* pEngineChannel = |
177 |
|
pSamplerChannel->GetEngineChannel(); |
178 |
|
if (!pEngineChannel) return; |
179 |
|
for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) { |
180 |
|
if ((*iter).pEngineChannel == pEngineChannel) { |
181 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
182 |
|
pEngineChannel->Disconnect(pMidiListener); |
183 |
|
channelMidiListeners.erase(iter); |
184 |
|
delete pMidiListener; |
185 |
|
return; |
186 |
|
} |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
void LSCPServer::EventHandler::EngineChanged(int ChannelId) { |
191 |
|
SamplerChannel* pSamplerChannel = |
192 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
193 |
|
if (!pSamplerChannel) return; |
194 |
|
EngineChannel* pEngineChannel = |
195 |
|
pSamplerChannel->GetEngineChannel(); |
196 |
|
if (!pEngineChannel) return; |
197 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
198 |
|
pEngineChannel->Connect(pMidiListener); |
199 |
|
midi_listener_entry entry = { |
200 |
|
pSamplerChannel, pEngineChannel, pMidiListener |
201 |
|
}; |
202 |
|
channelMidiListeners.push_back(entry); |
203 |
|
} |
204 |
|
|
205 |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
206 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
207 |
} |
} |
210 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
211 |
} |
} |
212 |
|
|
213 |
|
void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
214 |
|
pDevice->RemoveMidiPortCountListener(this); |
215 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
216 |
|
MidiPortToBeRemoved(pDevice->GetPort(i)); |
217 |
|
} |
218 |
|
|
219 |
|
void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) { |
220 |
|
pDevice->AddMidiPortCountListener(this); |
221 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
222 |
|
MidiPortAdded(pDevice->GetPort(i)); |
223 |
|
} |
224 |
|
|
225 |
|
void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) { |
226 |
|
// yet unused |
227 |
|
} |
228 |
|
|
229 |
|
void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) { |
230 |
|
for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) { |
231 |
|
if ((*iter).pPort == pPort) { |
232 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
233 |
|
pPort->Disconnect(pMidiListener); |
234 |
|
deviceMidiListeners.erase(iter); |
235 |
|
delete pMidiListener; |
236 |
|
return; |
237 |
|
} |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) { |
242 |
|
// find out the device ID |
243 |
|
std::map<uint, MidiInputDevice*> devices = |
244 |
|
pParent->pSampler->GetMidiInputDevices(); |
245 |
|
for ( |
246 |
|
std::map<uint, MidiInputDevice*>::iterator iter = devices.begin(); |
247 |
|
iter != devices.end(); ++iter |
248 |
|
) { |
249 |
|
if (iter->second == pPort->GetDevice()) { // found |
250 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
251 |
|
pPort->Connect(pMidiListener); |
252 |
|
device_midi_listener_entry entry = { |
253 |
|
pPort, pMidiListener, iter->first |
254 |
|
}; |
255 |
|
deviceMidiListeners.push_back(entry); |
256 |
|
return; |
257 |
|
} |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
262 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
263 |
} |
} |
294 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
295 |
} |
} |
296 |
|
|
297 |
|
void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) { |
298 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount)); |
299 |
|
} |
300 |
|
|
301 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
302 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
303 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, Dir)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
304 |
} |
} |
305 |
|
|
306 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) { |
307 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, Dir)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir))); |
308 |
} |
} |
309 |
|
|
310 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
311 |
Dir = "'" + Dir + "'"; |
Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; |
312 |
NewName = "'" + NewName + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
313 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
314 |
} |
} |
315 |
|
|
316 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) { |
317 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, Dir)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir))); |
318 |
} |
} |
319 |
|
|
320 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) { |
321 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, Instr)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr))); |
322 |
} |
} |
323 |
|
|
324 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
325 |
Instr = "'" + Instr + "'"; |
Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; |
326 |
NewName = "'" + NewName + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
327 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
328 |
} |
} |
329 |
|
|
348 |
} |
} |
349 |
|
|
350 |
int LSCPServer::Main() { |
int LSCPServer::Main() { |
351 |
|
#if defined(WIN32) |
352 |
|
WSADATA wsaData; |
353 |
|
int iResult; |
354 |
|
iResult = WSAStartup(MAKEWORD(2,2), &wsaData); |
355 |
|
if (iResult != 0) { |
356 |
|
std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n"; |
357 |
|
exit(EXIT_FAILURE); |
358 |
|
} |
359 |
|
#endif |
360 |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
361 |
if (hSocket < 0) { |
if (hSocket < 0) { |
362 |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
370 |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
371 |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
372 |
std::cerr << "gave up!" << std::endl; |
std::cerr << "gave up!" << std::endl; |
373 |
|
#if defined(WIN32) |
374 |
|
closesocket(hSocket); |
375 |
|
#else |
376 |
close(hSocket); |
close(hSocket); |
377 |
|
#endif |
378 |
//return -1; |
//return -1; |
379 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
380 |
} |
} |
394 |
pSampler->AddVoiceCountListener(&eventHandler); |
pSampler->AddVoiceCountListener(&eventHandler); |
395 |
pSampler->AddStreamCountListener(&eventHandler); |
pSampler->AddStreamCountListener(&eventHandler); |
396 |
pSampler->AddBufferFillListener(&eventHandler); |
pSampler->AddBufferFillListener(&eventHandler); |
397 |
|
pSampler->AddTotalStreamCountListener(&eventHandler); |
398 |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
399 |
pSampler->AddFxSendCountListener(&eventHandler); |
pSampler->AddFxSendCountListener(&eventHandler); |
400 |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
414 |
timeval timeout; |
timeval timeout; |
415 |
|
|
416 |
while (true) { |
while (true) { |
417 |
|
#if CONFIG_PTHREAD_TESTCANCEL |
418 |
|
TestCancel(); |
419 |
|
#endif |
420 |
// check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers |
// check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers |
421 |
{ |
{ |
422 |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
424 |
std::set<EngineChannel*>::iterator itEnd = engineChannels.end(); |
std::set<EngineChannel*>::iterator itEnd = engineChannels.end(); |
425 |
for (; itEngineChannel != itEnd; ++itEngineChannel) { |
for (; itEngineChannel != itEnd; ++itEngineChannel) { |
426 |
if ((*itEngineChannel)->StatusChanged()) { |
if ((*itEngineChannel)->StatusChanged()) { |
427 |
SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex)); |
SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index())); |
428 |
} |
} |
429 |
|
|
430 |
for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { |
for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { |
431 |
FxSend* fxs = (*itEngineChannel)->GetFxSend(i); |
FxSend* fxs = (*itEngineChannel)->GetFxSend(i); |
432 |
if(fxs != NULL && fxs->IsInfoChanged()) { |
if(fxs != NULL && fxs->IsInfoChanged()) { |
433 |
int chn = (*itEngineChannel)->iSamplerChannelIndex; |
int chn = (*itEngineChannel)->GetSamplerChannel()->Index(); |
434 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id())); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id())); |
435 |
fxs->SetInfoChanged(false); |
fxs->SetInfoChanged(false); |
436 |
} |
} |
438 |
} |
} |
439 |
} |
} |
440 |
|
|
441 |
|
// check if MIDI data arrived on some engine channel |
442 |
|
for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) { |
443 |
|
const EventHandler::midi_listener_entry entry = |
444 |
|
eventHandler.channelMidiListeners[i]; |
445 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
446 |
|
if (pMidiListener->NotesChanged()) { |
447 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
448 |
|
if (pMidiListener->NoteChanged(iNote)) { |
449 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
450 |
|
LSCPServer::SendLSCPNotify( |
451 |
|
LSCPEvent( |
452 |
|
LSCPEvent::event_channel_midi, |
453 |
|
entry.pSamplerChannel->Index(), |
454 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
455 |
|
iNote, |
456 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
457 |
|
: pMidiListener->NoteOffVelocity(iNote) |
458 |
|
) |
459 |
|
); |
460 |
|
} |
461 |
|
} |
462 |
|
} |
463 |
|
} |
464 |
|
|
465 |
|
// check if MIDI data arrived on some MIDI device |
466 |
|
for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) { |
467 |
|
const EventHandler::device_midi_listener_entry entry = |
468 |
|
eventHandler.deviceMidiListeners[i]; |
469 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
470 |
|
if (pMidiListener->NotesChanged()) { |
471 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
472 |
|
if (pMidiListener->NoteChanged(iNote)) { |
473 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
474 |
|
LSCPServer::SendLSCPNotify( |
475 |
|
LSCPEvent( |
476 |
|
LSCPEvent::event_device_midi, |
477 |
|
entry.uiDeviceID, |
478 |
|
entry.pPort->GetPortNumber(), |
479 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
480 |
|
iNote, |
481 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
482 |
|
: pMidiListener->NoteOffVelocity(iNote) |
483 |
|
) |
484 |
|
); |
485 |
|
} |
486 |
|
} |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
//Now let's deliver late notifies (if any) |
//Now let's deliver late notifies (if any) |
491 |
NotifyBufferMutex.Lock(); |
NotifyBufferMutex.Lock(); |
492 |
for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) { |
for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) { |
509 |
continue; //Nothing try again |
continue; //Nothing try again |
510 |
if (retval == -1) { |
if (retval == -1) { |
511 |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
512 |
|
#if defined(WIN32) |
513 |
|
closesocket(hSocket); |
514 |
|
#else |
515 |
close(hSocket); |
close(hSocket); |
516 |
|
#endif |
517 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
518 |
} |
} |
519 |
|
|
525 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
526 |
} |
} |
527 |
|
|
528 |
|
#if defined(WIN32) |
529 |
|
u_long nonblock_io = 1; |
530 |
|
if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) { |
531 |
|
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
532 |
|
exit(EXIT_FAILURE); |
533 |
|
} |
534 |
|
#else |
535 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
536 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
537 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
538 |
} |
} |
539 |
|
#endif |
540 |
|
|
541 |
// Parser initialization |
// Parser initialization |
542 |
yyparse_param_t yyparse_param; |
yyparse_param_t yyparse_param; |
595 |
NotifyMutex.Lock(); |
NotifyMutex.Lock(); |
596 |
bufferedCommands.erase(socket); |
bufferedCommands.erase(socket); |
597 |
bufferedNotifies.erase(socket); |
bufferedNotifies.erase(socket); |
598 |
|
#if defined(WIN32) |
599 |
|
closesocket(socket); |
600 |
|
#else |
601 |
close(socket); |
close(socket); |
602 |
|
#endif |
603 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
604 |
} |
} |
605 |
|
|
606 |
|
void LSCPServer::LockRTNotify() { |
607 |
|
RTNotifyMutex.Lock(); |
608 |
|
} |
609 |
|
|
610 |
|
void LSCPServer::UnlockRTNotify() { |
611 |
|
RTNotifyMutex.Unlock(); |
612 |
|
} |
613 |
|
|
614 |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
615 |
int subs = 0; |
int subs = 0; |
616 |
SubscriptionMutex.Lock(); |
SubscriptionMutex.Lock(); |
686 |
char c; |
char c; |
687 |
int i = 0; |
int i = 0; |
688 |
while (true) { |
while (true) { |
689 |
|
#if defined(WIN32) |
690 |
|
int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now |
691 |
|
#else |
692 |
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 |
693 |
|
#endif |
694 |
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 |
695 |
CloseConnection(iter); |
CloseConnection(iter); |
696 |
break; |
break; |
705 |
} |
} |
706 |
bufferedCommands[socket] += c; |
bufferedCommands[socket] += c; |
707 |
} |
} |
708 |
|
#if defined(WIN32) |
709 |
|
if (result == SOCKET_ERROR) { |
710 |
|
int wsa_lasterror = WSAGetLastError(); |
711 |
|
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
712 |
|
return false; |
713 |
|
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
714 |
|
CloseConnection(iter); |
715 |
|
break; |
716 |
|
} |
717 |
|
#else |
718 |
if (result == -1) { |
if (result == -1) { |
719 |
if (errno == EAGAIN) //Would block, try again later. |
if (errno == EAGAIN) //Would block, try again later. |
720 |
return false; |
return false; |
753 |
CloseConnection(iter); |
CloseConnection(iter); |
754 |
break; |
break; |
755 |
} |
} |
756 |
|
#endif |
757 |
} |
} |
758 |
return false; |
return false; |
759 |
} |
} |
1027 |
LockRTNotify(); |
LockRTNotify(); |
1028 |
try { |
try { |
1029 |
Engine* pEngine = EngineFactory::Create(EngineName); |
Engine* pEngine = EngineFactory::Create(EngineName); |
1030 |
result.Add("DESCRIPTION", pEngine->Description()); |
result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description())); |
1031 |
result.Add("VERSION", pEngine->Version()); |
result.Add("VERSION", pEngine->Version()); |
1032 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
1033 |
} |
} |
1100 |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
1101 |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
1102 |
|
|
1103 |
|
// convert the filename into the correct encoding as defined for LSCP |
1104 |
|
// (especially in terms of special characters -> escape sequences) |
1105 |
|
if (InstrumentFileName != "NONE" && InstrumentFileName != "") { |
1106 |
|
#if WIN32 |
1107 |
|
InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp(); |
1108 |
|
#else |
1109 |
|
// assuming POSIX |
1110 |
|
InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp(); |
1111 |
|
#endif |
1112 |
|
} |
1113 |
|
|
1114 |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
1115 |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
1116 |
result.Add("INSTRUMENT_NAME", InstrumentName); |
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName)); |
1117 |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
1118 |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
1119 |
result.Add("SOLO", Solo); |
result.Add("SOLO", Solo); |
2020 |
dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n")); |
dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n")); |
2021 |
LSCPResultSet result; |
LSCPResultSet result; |
2022 |
try { |
try { |
2023 |
result.Add(MidiInstrumentMapper::Entries(MidiMapID).size()); |
result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID)); |
2024 |
} catch (Exception e) { |
} catch (Exception e) { |
2025 |
result.Error(e); |
result.Error(e); |
2026 |
} |
} |
2031 |
String LSCPServer::GetAllMidiInstrumentMappings() { |
String LSCPServer::GetAllMidiInstrumentMappings() { |
2032 |
dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n")); |
dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n")); |
2033 |
LSCPResultSet result; |
LSCPResultSet result; |
2034 |
std::vector<int> maps = MidiInstrumentMapper::Maps(); |
try { |
2035 |
int totalMappings = 0; |
result.Add(MidiInstrumentMapper::GetInstrumentCount()); |
2036 |
for (int i = 0; i < maps.size(); i++) { |
} catch (Exception e) { |
2037 |
try { |
result.Error(e); |
|
totalMappings += MidiInstrumentMapper::Entries(maps[i]).size(); |
|
|
} catch (Exception e) { /*NOOP*/ } |
|
2038 |
} |
} |
|
result.Add(totalMappings); |
|
2039 |
return result.Produce(); |
return result.Produce(); |
2040 |
} |
} |
2041 |
|
|
2043 |
dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n")); |
dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n")); |
2044 |
LSCPResultSet result; |
LSCPResultSet result; |
2045 |
try { |
try { |
2046 |
midi_prog_index_t idx; |
MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg); |
2047 |
idx.midi_bank_msb = (MidiBank >> 7) & 0x7f; |
// convert the filename into the correct encoding as defined for LSCP |
2048 |
idx.midi_bank_lsb = MidiBank & 0x7f; |
// (especially in terms of special characters -> escape sequences) |
2049 |
idx.midi_prog = MidiProg; |
#if WIN32 |
2050 |
|
const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp(); |
2051 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID); |
#else |
2052 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx); |
// assuming POSIX |
2053 |
if (iter == mappings.end()) result.Error("there is no map entry with that index"); |
const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp(); |
2054 |
else { // found |
#endif |
2055 |
result.Add("NAME", iter->second.Name); |
|
2056 |
result.Add("ENGINE_NAME", iter->second.EngineName); |
result.Add("NAME", _escapeLscpResponse(entry.Name)); |
2057 |
result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile); |
result.Add("ENGINE_NAME", entry.EngineName); |
2058 |
result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); |
result.Add("INSTRUMENT_FILE", instrumentFileName); |
2059 |
String instrumentName; |
result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex); |
2060 |
Engine* pEngine = EngineFactory::Create(iter->second.EngineName); |
String instrumentName; |
2061 |
if (pEngine) { |
Engine* pEngine = EngineFactory::Create(entry.EngineName); |
2062 |
if (pEngine->GetInstrumentManager()) { |
if (pEngine) { |
2063 |
InstrumentManager::instrument_id_t instrID; |
if (pEngine->GetInstrumentManager()) { |
2064 |
instrID.FileName = iter->second.InstrumentFile; |
InstrumentManager::instrument_id_t instrID; |
2065 |
instrID.Index = iter->second.InstrumentIndex; |
instrID.FileName = entry.InstrumentFile; |
2066 |
instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); |
instrID.Index = entry.InstrumentIndex; |
2067 |
} |
instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); |
|
EngineFactory::Destroy(pEngine); |
|
|
} |
|
|
result.Add("INSTRUMENT_NAME", instrumentName); |
|
|
switch (iter->second.LoadMode) { |
|
|
case MidiInstrumentMapper::ON_DEMAND: |
|
|
result.Add("LOAD_MODE", "ON_DEMAND"); |
|
|
break; |
|
|
case MidiInstrumentMapper::ON_DEMAND_HOLD: |
|
|
result.Add("LOAD_MODE", "ON_DEMAND_HOLD"); |
|
|
break; |
|
|
case MidiInstrumentMapper::PERSISTENT: |
|
|
result.Add("LOAD_MODE", "PERSISTENT"); |
|
|
break; |
|
|
default: |
|
|
throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!"); |
|
2068 |
} |
} |
2069 |
result.Add("VOLUME", iter->second.Volume); |
EngineFactory::Destroy(pEngine); |
2070 |
|
} |
2071 |
|
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); |
2072 |
|
switch (entry.LoadMode) { |
2073 |
|
case MidiInstrumentMapper::ON_DEMAND: |
2074 |
|
result.Add("LOAD_MODE", "ON_DEMAND"); |
2075 |
|
break; |
2076 |
|
case MidiInstrumentMapper::ON_DEMAND_HOLD: |
2077 |
|
result.Add("LOAD_MODE", "ON_DEMAND_HOLD"); |
2078 |
|
break; |
2079 |
|
case MidiInstrumentMapper::PERSISTENT: |
2080 |
|
result.Add("LOAD_MODE", "PERSISTENT"); |
2081 |
|
break; |
2082 |
|
default: |
2083 |
|
throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!"); |
2084 |
} |
} |
2085 |
|
result.Add("VOLUME", entry.Volume); |
2086 |
} catch (Exception e) { |
} catch (Exception e) { |
2087 |
result.Error(e); |
result.Error(e); |
2088 |
} |
} |
2222 |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
2223 |
LSCPResultSet result; |
LSCPResultSet result; |
2224 |
try { |
try { |
2225 |
result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); |
result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID))); |
2226 |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
2227 |
} catch (Exception e) { |
} catch (Exception e) { |
2228 |
result.Error(e); |
result.Error(e); |
2366 |
} |
} |
2367 |
|
|
2368 |
// success |
// success |
2369 |
result.Add("NAME", pFxSend->Name()); |
result.Add("NAME", _escapeLscpResponse(pFxSend->Name())); |
2370 |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
2371 |
result.Add("LEVEL", ToString(pFxSend->Level())); |
result.Add("LEVEL", ToString(pFxSend->Level())); |
2372 |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
2489 |
*/ |
*/ |
2490 |
String LSCPServer::GetServerInfo() { |
String LSCPServer::GetServerInfo() { |
2491 |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
2492 |
|
const std::string description = |
2493 |
|
_escapeLscpResponse("LinuxSampler - modular, streaming capable sampler"); |
2494 |
LSCPResultSet result; |
LSCPResultSet result; |
2495 |
result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); |
result.Add("DESCRIPTION", description); |
2496 |
result.Add("VERSION", VERSION); |
result.Add("VERSION", VERSION); |
2497 |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
2498 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
2505 |
} |
} |
2506 |
|
|
2507 |
/** |
/** |
2508 |
|
* Will be called by the parser to return the current number of all active streams. |
2509 |
|
*/ |
2510 |
|
String LSCPServer::GetTotalStreamCount() { |
2511 |
|
dmsg(2,("LSCPServer: GetTotalStreamCount()\n")); |
2512 |
|
LSCPResultSet result; |
2513 |
|
result.Add(pSampler->GetDiskStreamCount()); |
2514 |
|
return result.Produce(); |
2515 |
|
} |
2516 |
|
|
2517 |
|
/** |
2518 |
* 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. |
2519 |
*/ |
*/ |
2520 |
String LSCPServer::GetTotalVoiceCount() { |
String LSCPServer::GetTotalVoiceCount() { |
2544 |
LSCPResultSet result; |
LSCPResultSet result; |
2545 |
try { |
try { |
2546 |
if (dVolume < 0) throw Exception("Volume may not be negative"); |
if (dVolume < 0) throw Exception("Volume may not be negative"); |
2547 |
GLOBAL_VOLUME = dVolume; // see common/global.cpp |
GLOBAL_VOLUME = dVolume; // see common/global_private.cpp |
2548 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); |
2549 |
} catch (Exception e) { |
} catch (Exception e) { |
2550 |
result.Error(e); |
result.Error(e); |
2552 |
return result.Produce(); |
return result.Produce(); |
2553 |
} |
} |
2554 |
|
|
2555 |
|
String LSCPServer::GetFileInstruments(String Filename) { |
2556 |
|
dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2557 |
|
LSCPResultSet result; |
2558 |
|
try { |
2559 |
|
VerifyFile(Filename); |
2560 |
|
} catch (Exception e) { |
2561 |
|
result.Error(e); |
2562 |
|
return result.Produce(); |
2563 |
|
} |
2564 |
|
// try to find a sampler engine that can handle the file |
2565 |
|
bool bFound = false; |
2566 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2567 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2568 |
|
Engine* pEngine = NULL; |
2569 |
|
try { |
2570 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2571 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2572 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2573 |
|
if (pManager) { |
2574 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2575 |
|
pManager->GetInstrumentFileContent(Filename); |
2576 |
|
// return the amount of instruments in the file |
2577 |
|
result.Add(IDs.size()); |
2578 |
|
// no more need to ask other engine types |
2579 |
|
bFound = true; |
2580 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2581 |
|
} catch (Exception e) { |
2582 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2583 |
|
} |
2584 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2585 |
|
} |
2586 |
|
|
2587 |
|
if (!bFound) result.Error("Unknown file format"); |
2588 |
|
return result.Produce(); |
2589 |
|
} |
2590 |
|
|
2591 |
|
String LSCPServer::ListFileInstruments(String Filename) { |
2592 |
|
dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2593 |
|
LSCPResultSet result; |
2594 |
|
try { |
2595 |
|
VerifyFile(Filename); |
2596 |
|
} catch (Exception e) { |
2597 |
|
result.Error(e); |
2598 |
|
return result.Produce(); |
2599 |
|
} |
2600 |
|
// try to find a sampler engine that can handle the file |
2601 |
|
bool bFound = false; |
2602 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2603 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2604 |
|
Engine* pEngine = NULL; |
2605 |
|
try { |
2606 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2607 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2608 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2609 |
|
if (pManager) { |
2610 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2611 |
|
pManager->GetInstrumentFileContent(Filename); |
2612 |
|
// return a list of IDs of the instruments in the file |
2613 |
|
String s; |
2614 |
|
for (int j = 0; j < IDs.size(); j++) { |
2615 |
|
if (s.size()) s += ","; |
2616 |
|
s += ToString(IDs[j].Index); |
2617 |
|
} |
2618 |
|
result.Add(s); |
2619 |
|
// no more need to ask other engine types |
2620 |
|
bFound = true; |
2621 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2622 |
|
} catch (Exception e) { |
2623 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2624 |
|
} |
2625 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2626 |
|
} |
2627 |
|
|
2628 |
|
if (!bFound) result.Error("Unknown file format"); |
2629 |
|
return result.Produce(); |
2630 |
|
} |
2631 |
|
|
2632 |
|
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
2633 |
|
dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID)); |
2634 |
|
LSCPResultSet result; |
2635 |
|
try { |
2636 |
|
VerifyFile(Filename); |
2637 |
|
} catch (Exception e) { |
2638 |
|
result.Error(e); |
2639 |
|
return result.Produce(); |
2640 |
|
} |
2641 |
|
InstrumentManager::instrument_id_t id; |
2642 |
|
id.FileName = Filename; |
2643 |
|
id.Index = InstrumentID; |
2644 |
|
// try to find a sampler engine that can handle the file |
2645 |
|
bool bFound = false; |
2646 |
|
bool bFatalErr = false; |
2647 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2648 |
|
for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) { |
2649 |
|
Engine* pEngine = NULL; |
2650 |
|
try { |
2651 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2652 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2653 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2654 |
|
if (pManager) { |
2655 |
|
// check if the instrument index is valid |
2656 |
|
// FIXME: this won't work if an engine only supports parts of the instrument file |
2657 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2658 |
|
pManager->GetInstrumentFileContent(Filename); |
2659 |
|
if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) { |
2660 |
|
std::stringstream ss; |
2661 |
|
ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'"; |
2662 |
|
bFatalErr = true; |
2663 |
|
throw Exception(ss.str()); |
2664 |
|
} |
2665 |
|
// get the info of the requested instrument |
2666 |
|
InstrumentManager::instrument_info_t info = |
2667 |
|
pManager->GetInstrumentInfo(id); |
2668 |
|
// return detailed informations about the file |
2669 |
|
result.Add("NAME", info.InstrumentName); |
2670 |
|
result.Add("FORMAT_FAMILY", engineTypes[i]); |
2671 |
|
result.Add("FORMAT_VERSION", info.FormatVersion); |
2672 |
|
result.Add("PRODUCT", info.Product); |
2673 |
|
result.Add("ARTISTS", info.Artists); |
2674 |
|
// no more need to ask other engine types |
2675 |
|
bFound = true; |
2676 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2677 |
|
} catch (Exception e) { |
2678 |
|
// usually NOOP, as exception is thrown if engine doesn't support file |
2679 |
|
if (bFatalErr) result.Error(e); |
2680 |
|
} |
2681 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2682 |
|
} |
2683 |
|
|
2684 |
|
if (!bFound && !bFatalErr) result.Error("Unknown file format"); |
2685 |
|
return result.Produce(); |
2686 |
|
} |
2687 |
|
|
2688 |
|
void LSCPServer::VerifyFile(String Filename) { |
2689 |
|
#if WIN32 |
2690 |
|
WIN32_FIND_DATA win32FileAttributeData; |
2691 |
|
BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData ); |
2692 |
|
if (!res) { |
2693 |
|
std::stringstream ss; |
2694 |
|
ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError(); |
2695 |
|
throw Exception(ss.str()); |
2696 |
|
} |
2697 |
|
if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { |
2698 |
|
throw Exception("Directory is specified"); |
2699 |
|
} |
2700 |
|
#else |
2701 |
|
struct stat statBuf; |
2702 |
|
int res = stat(Filename.c_str(), &statBuf); |
2703 |
|
if (res) { |
2704 |
|
std::stringstream ss; |
2705 |
|
ss << "Fail to stat `" << Filename << "`: " << strerror(errno); |
2706 |
|
throw Exception(ss.str()); |
2707 |
|
} |
2708 |
|
|
2709 |
|
if (S_ISDIR(statBuf.st_mode)) { |
2710 |
|
throw Exception("Directory is specified"); |
2711 |
|
} |
2712 |
|
#endif |
2713 |
|
} |
2714 |
|
|
2715 |
/** |
/** |
2716 |
* 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 |
2717 |
* server for receiving event messages. |
* server for receiving event messages. |
2793 |
|
|
2794 |
for (int i = 0; i < dirs->size(); i++) { |
for (int i = 0; i < dirs->size(); i++) { |
2795 |
if (list != "") list += ","; |
if (list != "") list += ","; |
2796 |
list += "'" + dirs->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'"; |
2797 |
} |
} |
2798 |
|
|
2799 |
result.Add(list); |
result.Add(list); |
2813 |
try { |
try { |
2814 |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
2815 |
|
|
2816 |
result.Add("DESCRIPTION", info.Description); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
2817 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
2818 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
2819 |
} catch (Exception e) { |
} catch (Exception e) { |
2970 |
|
|
2971 |
for (int i = 0; i < instrs->size(); i++) { |
for (int i = 0; i < instrs->size(); i++) { |
2972 |
if (list != "") list += ","; |
if (list != "") list += ","; |
2973 |
list += "'" + instrs->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'"; |
2974 |
} |
} |
2975 |
|
|
2976 |
result.Add(list); |
result.Add(list); |
2997 |
result.Add("SIZE", (int)info.Size); |
result.Add("SIZE", (int)info.Size); |
2998 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
2999 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
3000 |
result.Add("DESCRIPTION", FilterEndlines(info.Description)); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
3001 |
result.Add("IS_DRUM", info.IsDrum); |
result.Add("IS_DRUM", info.IsDrum); |
3002 |
result.Add("PRODUCT", FilterEndlines(info.Product)); |
result.Add("PRODUCT", _escapeLscpResponse(info.Product)); |
3003 |
result.Add("ARTISTS", FilterEndlines(info.Artists)); |
result.Add("ARTISTS", _escapeLscpResponse(info.Artists)); |
3004 |
result.Add("KEYWORDS", FilterEndlines(info.Keywords)); |
result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords)); |
3005 |
} catch (Exception e) { |
} catch (Exception e) { |
3006 |
result.Error(e); |
result.Error(e); |
3007 |
} |
} |
3091 |
return result.Produce(); |
return result.Produce(); |
3092 |
} |
} |
3093 |
|
|
3094 |
|
String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) { |
3095 |
|
dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str())); |
3096 |
|
LSCPResultSet result; |
3097 |
|
#if HAVE_SQLITE3 |
3098 |
|
try { |
3099 |
|
InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath); |
3100 |
|
} catch (Exception e) { |
3101 |
|
result.Error(e); |
3102 |
|
} |
3103 |
|
#else |
3104 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3105 |
|
#endif |
3106 |
|
return result.Produce(); |
3107 |
|
} |
3108 |
|
|
3109 |
|
String LSCPServer::FindLostDbInstrumentFiles() { |
3110 |
|
dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n")); |
3111 |
|
LSCPResultSet result; |
3112 |
|
#if HAVE_SQLITE3 |
3113 |
|
try { |
3114 |
|
String list; |
3115 |
|
StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles(); |
3116 |
|
|
3117 |
|
for (int i = 0; i < pLostFiles->size(); i++) { |
3118 |
|
if (list != "") list += ","; |
3119 |
|
list += "'" + pLostFiles->at(i) + "'"; |
3120 |
|
} |
3121 |
|
|
3122 |
|
result.Add(list); |
3123 |
|
} catch (Exception e) { |
3124 |
|
result.Error(e); |
3125 |
|
} |
3126 |
|
#else |
3127 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3128 |
|
#endif |
3129 |
|
return result.Produce(); |
3130 |
|
} |
3131 |
|
|
3132 |
String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) { |
String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) { |
3133 |
dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str())); |
dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str())); |
3134 |
LSCPResultSet result; |
LSCPResultSet result; |
3156 |
|
|
3157 |
for (int i = 0; i < pDirectories->size(); i++) { |
for (int i = 0; i < pDirectories->size(); i++) { |
3158 |
if (list != "") list += ","; |
if (list != "") list += ","; |
3159 |
list += "'" + pDirectories->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'"; |
3160 |
} |
} |
3161 |
|
|
3162 |
result.Add(list); |
result.Add(list); |
3212 |
|
|
3213 |
for (int i = 0; i < pInstruments->size(); i++) { |
for (int i = 0; i < pInstruments->size(); i++) { |
3214 |
if (list != "") list += ","; |
if (list != "") list += ","; |
3215 |
list += "'" + pInstruments->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'"; |
3216 |
} |
} |
3217 |
|
|
3218 |
result.Add(list); |
result.Add(list); |
3225 |
return result.Produce(); |
return result.Produce(); |
3226 |
} |
} |
3227 |
|
|
3228 |
|
String LSCPServer::FormatInstrumentsDb() { |
3229 |
|
dmsg(2,("LSCPServer: FormatInstrumentsDb()\n")); |
3230 |
|
LSCPResultSet result; |
3231 |
|
#if HAVE_SQLITE3 |
3232 |
|
try { |
3233 |
|
InstrumentsDb::GetInstrumentsDb()->Format(); |
3234 |
|
} catch (Exception e) { |
3235 |
|
result.Error(e); |
3236 |
|
} |
3237 |
|
#else |
3238 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3239 |
|
#endif |
3240 |
|
return result.Produce(); |
3241 |
|
} |
3242 |
|
|
3243 |
|
|
3244 |
/** |
/** |
3245 |
* 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 |
3259 |
} |
} |
3260 |
return result.Produce(); |
return result.Produce(); |
3261 |
} |
} |
|
|
|
|
String LSCPServer::FilterEndlines(String s) { |
|
|
String s2 = s; |
|
|
for (int i = 0; i < s2.length(); i++) { |
|
|
if (s2.at(i) == '\r') s2.at(i) = ' '; |
|
|
else if (s2.at(i) == '\n') s2.at(i) = ' '; |
|
|
} |
|
|
|
|
|
return s2; |
|
|
} |
|