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 |
|
#include <string> |
26 |
|
|
27 |
#include "lscpserver.h" |
#include "lscpserver.h" |
28 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
29 |
#include "lscpevent.h" |
#include "lscpevent.h" |
30 |
|
|
31 |
|
#if defined(WIN32) |
32 |
|
#include <windows.h> |
33 |
|
#else |
34 |
#include <fcntl.h> |
#include <fcntl.h> |
35 |
|
#endif |
36 |
|
|
37 |
#if ! HAVE_SQLITE3 |
#if ! HAVE_SQLITE3 |
38 |
#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." |
43 |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
44 |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
45 |
|
|
46 |
|
namespace LinuxSampler { |
47 |
|
|
48 |
|
/** |
49 |
|
* Returns a copy of the given string where all special characters are |
50 |
|
* replaced by LSCP escape sequences ("\xHH"). This function shall be used |
51 |
|
* to escape LSCP response fields in case the respective response field is |
52 |
|
* actually defined as using escape sequences in the LSCP specs. |
53 |
|
* |
54 |
|
* @e Caution: DO NOT use this function for escaping path based responses, |
55 |
|
* use the Path class (src/common/Path.h) for this instead! |
56 |
|
*/ |
57 |
|
static String _escapeLscpResponse(String txt) { |
58 |
|
for (int i = 0; i < txt.length(); i++) { |
59 |
|
const char c = txt.c_str()[i]; |
60 |
|
if ( |
61 |
|
!(c >= '0' && c <= '9') && |
62 |
|
!(c >= 'a' && c <= 'z') && |
63 |
|
!(c >= 'A' && c <= 'Z') && |
64 |
|
!(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') && |
65 |
|
!(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') && |
66 |
|
!(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') && |
67 |
|
!(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') && |
68 |
|
!(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') && |
69 |
|
!(c == '@') && !(c == '[') && !(c == ']') && |
70 |
|
!(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') && |
71 |
|
!(c == '|') && !(c == '}') && !(c == '~') |
72 |
|
) { |
73 |
|
// convert the "special" character into a "\xHH" LSCP escape sequence |
74 |
|
char buf[5]; |
75 |
|
snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c)); |
76 |
|
txt.replace(i, 1, buf); |
77 |
|
i += 3; |
78 |
|
} |
79 |
|
} |
80 |
|
return txt; |
81 |
|
} |
82 |
|
|
83 |
/** |
/** |
84 |
* Below are a few static members of the LSCPServer class. |
* Below are a few static members of the LSCPServer class. |
85 |
* 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. |
96 |
fd_set LSCPServer::fdSet; |
fd_set LSCPServer::fdSet; |
97 |
int LSCPServer::currentSocket = -1; |
int LSCPServer::currentSocket = -1; |
98 |
std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>(); |
std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>(); |
99 |
|
std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator(); |
100 |
std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>(); |
std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>(); |
101 |
std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>(); |
std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>(); |
102 |
std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >(); |
std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >(); |
105 |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
106 |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
107 |
|
|
108 |
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) { |
109 |
SocketAddress.sin_family = AF_INET; |
SocketAddress.sin_family = AF_INET; |
110 |
SocketAddress.sin_addr.s_addr = addr; |
SocketAddress.sin_addr.s_addr = addr; |
111 |
SocketAddress.sin_port = port; |
SocketAddress.sin_port = port; |
129 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO"); |
130 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT"); |
131 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
132 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
133 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
134 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); |
135 |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
136 |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
137 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI"); |
138 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI"); |
139 |
hSocket = -1; |
hSocket = -1; |
140 |
} |
} |
141 |
|
|
142 |
LSCPServer::~LSCPServer() { |
LSCPServer::~LSCPServer() { |
143 |
|
#if defined(WIN32) |
144 |
|
if (hSocket >= 0) closesocket(hSocket); |
145 |
|
#else |
146 |
if (hSocket >= 0) close(hSocket); |
if (hSocket >= 0) close(hSocket); |
147 |
|
#endif |
148 |
|
} |
149 |
|
|
150 |
|
LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) { |
151 |
|
this->pParent = pParent; |
152 |
|
} |
153 |
|
|
154 |
|
LSCPServer::EventHandler::~EventHandler() { |
155 |
|
std::vector<midi_listener_entry> l = channelMidiListeners; |
156 |
|
channelMidiListeners.clear(); |
157 |
|
for (int i = 0; i < l.size(); i++) |
158 |
|
delete l[i].pMidiListener; |
159 |
} |
} |
160 |
|
|
161 |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
162 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
163 |
} |
} |
164 |
|
|
165 |
|
void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) { |
166 |
|
pChannel->AddEngineChangeListener(this); |
167 |
|
} |
168 |
|
|
169 |
|
void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) { |
170 |
|
if (!pChannel->GetEngineChannel()) return; |
171 |
|
EngineToBeChanged(pChannel->Index()); |
172 |
|
} |
173 |
|
|
174 |
|
void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) { |
175 |
|
SamplerChannel* pSamplerChannel = |
176 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
177 |
|
if (!pSamplerChannel) return; |
178 |
|
EngineChannel* pEngineChannel = |
179 |
|
pSamplerChannel->GetEngineChannel(); |
180 |
|
if (!pEngineChannel) return; |
181 |
|
for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) { |
182 |
|
if ((*iter).pEngineChannel == pEngineChannel) { |
183 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
184 |
|
pEngineChannel->Disconnect(pMidiListener); |
185 |
|
channelMidiListeners.erase(iter); |
186 |
|
delete pMidiListener; |
187 |
|
return; |
188 |
|
} |
189 |
|
} |
190 |
|
} |
191 |
|
|
192 |
|
void LSCPServer::EventHandler::EngineChanged(int ChannelId) { |
193 |
|
SamplerChannel* pSamplerChannel = |
194 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
195 |
|
if (!pSamplerChannel) return; |
196 |
|
EngineChannel* pEngineChannel = |
197 |
|
pSamplerChannel->GetEngineChannel(); |
198 |
|
if (!pEngineChannel) return; |
199 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
200 |
|
pEngineChannel->Connect(pMidiListener); |
201 |
|
midi_listener_entry entry = { |
202 |
|
pSamplerChannel, pEngineChannel, pMidiListener |
203 |
|
}; |
204 |
|
channelMidiListeners.push_back(entry); |
205 |
|
} |
206 |
|
|
207 |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
208 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
209 |
} |
} |
212 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
213 |
} |
} |
214 |
|
|
215 |
|
void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
216 |
|
pDevice->RemoveMidiPortCountListener(this); |
217 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
218 |
|
MidiPortToBeRemoved(pDevice->GetPort(i)); |
219 |
|
} |
220 |
|
|
221 |
|
void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) { |
222 |
|
pDevice->AddMidiPortCountListener(this); |
223 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
224 |
|
MidiPortAdded(pDevice->GetPort(i)); |
225 |
|
} |
226 |
|
|
227 |
|
void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) { |
228 |
|
// yet unused |
229 |
|
} |
230 |
|
|
231 |
|
void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) { |
232 |
|
for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) { |
233 |
|
if ((*iter).pPort == pPort) { |
234 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
235 |
|
pPort->Disconnect(pMidiListener); |
236 |
|
deviceMidiListeners.erase(iter); |
237 |
|
delete pMidiListener; |
238 |
|
return; |
239 |
|
} |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
|
void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) { |
244 |
|
// find out the device ID |
245 |
|
std::map<uint, MidiInputDevice*> devices = |
246 |
|
pParent->pSampler->GetMidiInputDevices(); |
247 |
|
for ( |
248 |
|
std::map<uint, MidiInputDevice*>::iterator iter = devices.begin(); |
249 |
|
iter != devices.end(); ++iter |
250 |
|
) { |
251 |
|
if (iter->second == pPort->GetDevice()) { // found |
252 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
253 |
|
pPort->Connect(pMidiListener); |
254 |
|
device_midi_listener_entry entry = { |
255 |
|
pPort, pMidiListener, iter->first |
256 |
|
}; |
257 |
|
deviceMidiListeners.push_back(entry); |
258 |
|
return; |
259 |
|
} |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
264 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
265 |
} |
} |
296 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
297 |
} |
} |
298 |
|
|
299 |
|
void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) { |
300 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount)); |
301 |
|
} |
302 |
|
|
303 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
304 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
305 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, Dir)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
306 |
} |
} |
307 |
|
|
308 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) { |
309 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, Dir)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir))); |
310 |
} |
} |
311 |
|
|
312 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { |
313 |
Dir = "'" + Dir + "'"; |
Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; |
314 |
NewName = "'" + NewName + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
315 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); |
316 |
} |
} |
317 |
|
|
318 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) { |
319 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, Dir)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir))); |
320 |
} |
} |
321 |
|
|
322 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) { |
323 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, Instr)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr))); |
324 |
} |
} |
325 |
|
|
326 |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { |
327 |
Instr = "'" + Instr + "'"; |
Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; |
328 |
NewName = "'" + NewName + "'"; |
NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; |
329 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); |
330 |
} |
} |
331 |
|
|
332 |
|
void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) { |
333 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId)); |
334 |
|
} |
335 |
#endif // HAVE_SQLITE3 |
#endif // HAVE_SQLITE3 |
336 |
|
|
337 |
|
|
350 |
} |
} |
351 |
|
|
352 |
int LSCPServer::Main() { |
int LSCPServer::Main() { |
353 |
|
#if defined(WIN32) |
354 |
|
WSADATA wsaData; |
355 |
|
int iResult; |
356 |
|
iResult = WSAStartup(MAKEWORD(2,2), &wsaData); |
357 |
|
if (iResult != 0) { |
358 |
|
std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n"; |
359 |
|
exit(EXIT_FAILURE); |
360 |
|
} |
361 |
|
#endif |
362 |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
hSocket = socket(AF_INET, SOCK_STREAM, 0); |
363 |
if (hSocket < 0) { |
if (hSocket < 0) { |
364 |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
std::cerr << "LSCPServer: Could not create server socket." << std::endl; |
372 |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { |
373 |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
if (trial > LSCP_SERVER_BIND_TIMEOUT) { |
374 |
std::cerr << "gave up!" << std::endl; |
std::cerr << "gave up!" << std::endl; |
375 |
|
#if defined(WIN32) |
376 |
|
closesocket(hSocket); |
377 |
|
#else |
378 |
close(hSocket); |
close(hSocket); |
379 |
|
#endif |
380 |
//return -1; |
//return -1; |
381 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
382 |
} |
} |
388 |
|
|
389 |
listen(hSocket, 1); |
listen(hSocket, 1); |
390 |
Initialized.Set(true); |
Initialized.Set(true); |
391 |
|
|
392 |
// Registering event listeners |
// Registering event listeners |
393 |
pSampler->AddChannelCountListener(&eventHandler); |
pSampler->AddChannelCountListener(&eventHandler); |
394 |
pSampler->AddAudioDeviceCountListener(&eventHandler); |
pSampler->AddAudioDeviceCountListener(&eventHandler); |
396 |
pSampler->AddVoiceCountListener(&eventHandler); |
pSampler->AddVoiceCountListener(&eventHandler); |
397 |
pSampler->AddStreamCountListener(&eventHandler); |
pSampler->AddStreamCountListener(&eventHandler); |
398 |
pSampler->AddBufferFillListener(&eventHandler); |
pSampler->AddBufferFillListener(&eventHandler); |
399 |
|
pSampler->AddTotalStreamCountListener(&eventHandler); |
400 |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
401 |
pSampler->AddFxSendCountListener(&eventHandler); |
pSampler->AddFxSendCountListener(&eventHandler); |
402 |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
416 |
timeval timeout; |
timeval timeout; |
417 |
|
|
418 |
while (true) { |
while (true) { |
419 |
|
#if CONFIG_PTHREAD_TESTCANCEL |
420 |
|
TestCancel(); |
421 |
|
#endif |
422 |
// 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 |
423 |
{ |
{ |
424 |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
426 |
std::set<EngineChannel*>::iterator itEnd = engineChannels.end(); |
std::set<EngineChannel*>::iterator itEnd = engineChannels.end(); |
427 |
for (; itEngineChannel != itEnd; ++itEngineChannel) { |
for (; itEngineChannel != itEnd; ++itEngineChannel) { |
428 |
if ((*itEngineChannel)->StatusChanged()) { |
if ((*itEngineChannel)->StatusChanged()) { |
429 |
SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex)); |
SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index())); |
430 |
} |
} |
431 |
|
|
432 |
for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { |
for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { |
433 |
FxSend* fxs = (*itEngineChannel)->GetFxSend(i); |
FxSend* fxs = (*itEngineChannel)->GetFxSend(i); |
434 |
if(fxs != NULL && fxs->IsInfoChanged()) { |
if(fxs != NULL && fxs->IsInfoChanged()) { |
435 |
int chn = (*itEngineChannel)->iSamplerChannelIndex; |
int chn = (*itEngineChannel)->GetSamplerChannel()->Index(); |
436 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id())); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id())); |
437 |
fxs->SetInfoChanged(false); |
fxs->SetInfoChanged(false); |
438 |
} |
} |
440 |
} |
} |
441 |
} |
} |
442 |
|
|
443 |
|
// check if MIDI data arrived on some engine channel |
444 |
|
for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) { |
445 |
|
const EventHandler::midi_listener_entry entry = |
446 |
|
eventHandler.channelMidiListeners[i]; |
447 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
448 |
|
if (pMidiListener->NotesChanged()) { |
449 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
450 |
|
if (pMidiListener->NoteChanged(iNote)) { |
451 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
452 |
|
LSCPServer::SendLSCPNotify( |
453 |
|
LSCPEvent( |
454 |
|
LSCPEvent::event_channel_midi, |
455 |
|
entry.pSamplerChannel->Index(), |
456 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
457 |
|
iNote, |
458 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
459 |
|
: pMidiListener->NoteOffVelocity(iNote) |
460 |
|
) |
461 |
|
); |
462 |
|
} |
463 |
|
} |
464 |
|
} |
465 |
|
} |
466 |
|
|
467 |
|
// check if MIDI data arrived on some MIDI device |
468 |
|
for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) { |
469 |
|
const EventHandler::device_midi_listener_entry entry = |
470 |
|
eventHandler.deviceMidiListeners[i]; |
471 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
472 |
|
if (pMidiListener->NotesChanged()) { |
473 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
474 |
|
if (pMidiListener->NoteChanged(iNote)) { |
475 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
476 |
|
LSCPServer::SendLSCPNotify( |
477 |
|
LSCPEvent( |
478 |
|
LSCPEvent::event_device_midi, |
479 |
|
entry.uiDeviceID, |
480 |
|
entry.pPort->GetPortNumber(), |
481 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
482 |
|
iNote, |
483 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
484 |
|
: pMidiListener->NoteOffVelocity(iNote) |
485 |
|
) |
486 |
|
); |
487 |
|
} |
488 |
|
} |
489 |
|
} |
490 |
|
} |
491 |
|
|
492 |
//Now let's deliver late notifies (if any) |
//Now let's deliver late notifies (if any) |
493 |
NotifyBufferMutex.Lock(); |
NotifyBufferMutex.Lock(); |
494 |
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++) { |
511 |
continue; //Nothing try again |
continue; //Nothing try again |
512 |
if (retval == -1) { |
if (retval == -1) { |
513 |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
514 |
|
#if defined(WIN32) |
515 |
|
closesocket(hSocket); |
516 |
|
#else |
517 |
close(hSocket); |
close(hSocket); |
518 |
|
#endif |
519 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
520 |
} |
} |
521 |
|
|
527 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
528 |
} |
} |
529 |
|
|
530 |
|
#if defined(WIN32) |
531 |
|
u_long nonblock_io = 1; |
532 |
|
if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) { |
533 |
|
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
534 |
|
exit(EXIT_FAILURE); |
535 |
|
} |
536 |
|
#else |
537 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
538 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
539 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
540 |
} |
} |
541 |
|
#endif |
542 |
|
|
543 |
// Parser initialization |
// Parser initialization |
544 |
yyparse_param_t yyparse_param; |
yyparse_param_t yyparse_param; |
562 |
int dummy; // just a temporary hack to fulfill the restart() function prototype |
int dummy; // just a temporary hack to fulfill the restart() function prototype |
563 |
restart(NULL, dummy); // restart the 'scanner' |
restart(NULL, dummy); // restart the 'scanner' |
564 |
currentSocket = (*iter).hSession; //a hack |
currentSocket = (*iter).hSession; //a hack |
565 |
|
itCurrentSession = iter; // another hack |
566 |
dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str())); |
dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str())); |
567 |
if ((*iter).bVerbose) { // if echo mode enabled |
if ((*iter).bVerbose) { // if echo mode enabled |
568 |
AnswerClient(bufferedCommands[currentSocket]); |
AnswerClient(bufferedCommands[currentSocket]); |
569 |
} |
} |
570 |
int result = yyparse(&(*iter)); |
int result = yyparse(&(*iter)); |
571 |
currentSocket = -1; //continuation of a hack |
currentSocket = -1; //continuation of a hack |
572 |
|
itCurrentSession = Sessions.end(); // hack as well |
573 |
dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); |
dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); |
574 |
if (result == LSCP_QUIT) { //Was it a quit command by any chance? |
if (result == LSCP_QUIT) { //Was it a quit command by any chance? |
575 |
CloseConnection(iter); |
CloseConnection(iter); |
597 |
NotifyMutex.Lock(); |
NotifyMutex.Lock(); |
598 |
bufferedCommands.erase(socket); |
bufferedCommands.erase(socket); |
599 |
bufferedNotifies.erase(socket); |
bufferedNotifies.erase(socket); |
600 |
|
#if defined(WIN32) |
601 |
|
closesocket(socket); |
602 |
|
#else |
603 |
close(socket); |
close(socket); |
604 |
|
#endif |
605 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
606 |
} |
} |
607 |
|
|
608 |
|
void LSCPServer::LockRTNotify() { |
609 |
|
RTNotifyMutex.Lock(); |
610 |
|
} |
611 |
|
|
612 |
|
void LSCPServer::UnlockRTNotify() { |
613 |
|
RTNotifyMutex.Unlock(); |
614 |
|
} |
615 |
|
|
616 |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
617 |
int subs = 0; |
int subs = 0; |
618 |
SubscriptionMutex.Lock(); |
SubscriptionMutex.Lock(); |
674 |
return command.size(); |
return command.size(); |
675 |
} |
} |
676 |
|
|
677 |
|
extern yyparse_param_t* GetCurrentYaccSession() { |
678 |
|
return &(*itCurrentSession); |
679 |
|
} |
680 |
|
|
681 |
/** |
/** |
682 |
* Will be called to try to read the command from the socket |
* Will be called to try to read the command from the socket |
683 |
* If command is read, it will return true. Otherwise false is returned. |
* If command is read, it will return true. Otherwise false is returned. |
688 |
char c; |
char c; |
689 |
int i = 0; |
int i = 0; |
690 |
while (true) { |
while (true) { |
691 |
|
#if defined(WIN32) |
692 |
|
int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now |
693 |
|
#else |
694 |
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 |
695 |
|
#endif |
696 |
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 |
697 |
CloseConnection(iter); |
CloseConnection(iter); |
698 |
break; |
break; |
707 |
} |
} |
708 |
bufferedCommands[socket] += c; |
bufferedCommands[socket] += c; |
709 |
} |
} |
710 |
|
#if defined(WIN32) |
711 |
|
if (result == SOCKET_ERROR) { |
712 |
|
int wsa_lasterror = WSAGetLastError(); |
713 |
|
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
714 |
|
return false; |
715 |
|
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
716 |
|
CloseConnection(iter); |
717 |
|
break; |
718 |
|
} |
719 |
|
#else |
720 |
if (result == -1) { |
if (result == -1) { |
721 |
if (errno == EAGAIN) //Would block, try again later. |
if (errno == EAGAIN) //Would block, try again later. |
722 |
return false; |
return false; |
755 |
CloseConnection(iter); |
CloseConnection(iter); |
756 |
break; |
break; |
757 |
} |
} |
758 |
|
#endif |
759 |
} |
} |
760 |
return false; |
return false; |
761 |
} |
} |
880 |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
881 |
if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); |
if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); |
882 |
|
|
883 |
return pEngineChannel; |
return pEngineChannel; |
884 |
} |
} |
885 |
|
|
886 |
/** |
/** |
1029 |
LockRTNotify(); |
LockRTNotify(); |
1030 |
try { |
try { |
1031 |
Engine* pEngine = EngineFactory::Create(EngineName); |
Engine* pEngine = EngineFactory::Create(EngineName); |
1032 |
result.Add("DESCRIPTION", pEngine->Description()); |
result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description())); |
1033 |
result.Add("VERSION", pEngine->Version()); |
result.Add("VERSION", pEngine->Version()); |
1034 |
EngineFactory::Destroy(pEngine); |
EngineFactory::Destroy(pEngine); |
1035 |
} |
} |
1102 |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); |
1103 |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); |
1104 |
|
|
1105 |
|
// convert the filename into the correct encoding as defined for LSCP |
1106 |
|
// (especially in terms of special characters -> escape sequences) |
1107 |
|
if (InstrumentFileName != "NONE" && InstrumentFileName != "") { |
1108 |
|
#if WIN32 |
1109 |
|
InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp(); |
1110 |
|
#else |
1111 |
|
// assuming POSIX |
1112 |
|
InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp(); |
1113 |
|
#endif |
1114 |
|
} |
1115 |
|
|
1116 |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
result.Add("INSTRUMENT_FILE", InstrumentFileName); |
1117 |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
result.Add("INSTRUMENT_NR", InstrumentIndex); |
1118 |
result.Add("INSTRUMENT_NAME", InstrumentName); |
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName)); |
1119 |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
result.Add("INSTRUMENT_STATUS", InstrumentStatus); |
1120 |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); |
1121 |
result.Add("SOLO", Solo); |
result.Add("SOLO", Solo); |
1135 |
dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel)); |
dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel)); |
1136 |
LSCPResultSet result; |
LSCPResultSet result; |
1137 |
try { |
try { |
1138 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("No engine loaded on sampler channel"); |
|
1139 |
if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); |
if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); |
1140 |
result.Add(pEngineChannel->GetEngine()->VoiceCount()); |
result.Add(pEngineChannel->GetEngine()->VoiceCount()); |
1141 |
} |
} |
1153 |
dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel)); |
dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel)); |
1154 |
LSCPResultSet result; |
LSCPResultSet result; |
1155 |
try { |
try { |
1156 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); |
|
1157 |
if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); |
if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); |
1158 |
result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); |
result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); |
1159 |
} |
} |
1171 |
dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel)); |
dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel)); |
1172 |
LSCPResultSet result; |
LSCPResultSet result; |
1173 |
try { |
try { |
1174 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); |
|
1175 |
if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); |
if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); |
1176 |
if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); |
if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); |
1177 |
else { |
else { |
1849 |
dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel)); |
dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel)); |
1850 |
LSCPResultSet result; |
LSCPResultSet result; |
1851 |
try { |
try { |
1852 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); |
|
1853 |
pEngineChannel->Volume(dVolume); |
pEngineChannel->Volume(dVolume); |
1854 |
} |
} |
1855 |
catch (Exception e) { |
catch (Exception e) { |
1865 |
dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel)); |
dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel)); |
1866 |
LSCPResultSet result; |
LSCPResultSet result; |
1867 |
try { |
try { |
1868 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
|
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); |
|
1869 |
|
|
1870 |
if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); |
if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); |
1871 |
else pEngineChannel->SetMute(1); |
else pEngineChannel->SetMute(1); |
1882 |
dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel)); |
dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel)); |
1883 |
LSCPResultSet result; |
LSCPResultSet result; |
1884 |
try { |
try { |
1885 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
|
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); |
|
1886 |
|
|
1887 |
bool oldSolo = pEngineChannel->GetSolo(); |
bool oldSolo = pEngineChannel->GetSolo(); |
1888 |
bool hadSoloChannel = HasSoloChannel(); |
bool hadSoloChannel = HasSoloChannel(); |
2002 |
dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n")); |
dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n")); |
2003 |
LSCPResultSet result; |
LSCPResultSet result; |
2004 |
try { |
try { |
2005 |
result.Add(MidiInstrumentMapper::Entries(MidiMapID).size()); |
result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID)); |
2006 |
} catch (Exception e) { |
} catch (Exception e) { |
2007 |
result.Error(e); |
result.Error(e); |
2008 |
} |
} |
2013 |
String LSCPServer::GetAllMidiInstrumentMappings() { |
String LSCPServer::GetAllMidiInstrumentMappings() { |
2014 |
dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n")); |
dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n")); |
2015 |
LSCPResultSet result; |
LSCPResultSet result; |
2016 |
std::vector<int> maps = MidiInstrumentMapper::Maps(); |
try { |
2017 |
int totalMappings = 0; |
result.Add(MidiInstrumentMapper::GetInstrumentCount()); |
2018 |
for (int i = 0; i < maps.size(); i++) { |
} catch (Exception e) { |
2019 |
try { |
result.Error(e); |
|
totalMappings += MidiInstrumentMapper::Entries(maps[i]).size(); |
|
|
} catch (Exception e) { /*NOOP*/ } |
|
2020 |
} |
} |
|
result.Add(totalMappings); |
|
2021 |
return result.Produce(); |
return result.Produce(); |
2022 |
} |
} |
2023 |
|
|
2025 |
dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n")); |
dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n")); |
2026 |
LSCPResultSet result; |
LSCPResultSet result; |
2027 |
try { |
try { |
2028 |
midi_prog_index_t idx; |
MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg); |
2029 |
idx.midi_bank_msb = (MidiBank >> 7) & 0x7f; |
// convert the filename into the correct encoding as defined for LSCP |
2030 |
idx.midi_bank_lsb = MidiBank & 0x7f; |
// (especially in terms of special characters -> escape sequences) |
2031 |
idx.midi_prog = MidiProg; |
#if WIN32 |
2032 |
|
const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp(); |
2033 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID); |
#else |
2034 |
std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx); |
// assuming POSIX |
2035 |
if (iter == mappings.end()) result.Error("there is no map entry with that index"); |
const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp(); |
2036 |
else { // found |
#endif |
2037 |
result.Add("NAME", iter->second.Name); |
|
2038 |
result.Add("ENGINE_NAME", iter->second.EngineName); |
result.Add("NAME", _escapeLscpResponse(entry.Name)); |
2039 |
result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile); |
result.Add("ENGINE_NAME", entry.EngineName); |
2040 |
result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); |
result.Add("INSTRUMENT_FILE", instrumentFileName); |
2041 |
String instrumentName; |
result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex); |
2042 |
Engine* pEngine = EngineFactory::Create(iter->second.EngineName); |
String instrumentName; |
2043 |
if (pEngine) { |
Engine* pEngine = EngineFactory::Create(entry.EngineName); |
2044 |
if (pEngine->GetInstrumentManager()) { |
if (pEngine) { |
2045 |
InstrumentManager::instrument_id_t instrID; |
if (pEngine->GetInstrumentManager()) { |
2046 |
instrID.FileName = iter->second.InstrumentFile; |
InstrumentManager::instrument_id_t instrID; |
2047 |
instrID.Index = iter->second.InstrumentIndex; |
instrID.FileName = entry.InstrumentFile; |
2048 |
instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); |
instrID.Index = entry.InstrumentIndex; |
2049 |
} |
instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); |
|
EngineFactory::Destroy(pEngine); |
|
2050 |
} |
} |
2051 |
result.Add("INSTRUMENT_NAME", instrumentName); |
EngineFactory::Destroy(pEngine); |
2052 |
switch (iter->second.LoadMode) { |
} |
2053 |
case MidiInstrumentMapper::ON_DEMAND: |
result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); |
2054 |
result.Add("LOAD_MODE", "ON_DEMAND"); |
switch (entry.LoadMode) { |
2055 |
break; |
case MidiInstrumentMapper::ON_DEMAND: |
2056 |
case MidiInstrumentMapper::ON_DEMAND_HOLD: |
result.Add("LOAD_MODE", "ON_DEMAND"); |
2057 |
result.Add("LOAD_MODE", "ON_DEMAND_HOLD"); |
break; |
2058 |
break; |
case MidiInstrumentMapper::ON_DEMAND_HOLD: |
2059 |
case MidiInstrumentMapper::PERSISTENT: |
result.Add("LOAD_MODE", "ON_DEMAND_HOLD"); |
2060 |
result.Add("LOAD_MODE", "PERSISTENT"); |
break; |
2061 |
break; |
case MidiInstrumentMapper::PERSISTENT: |
2062 |
default: |
result.Add("LOAD_MODE", "PERSISTENT"); |
2063 |
throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!"); |
break; |
2064 |
} |
default: |
2065 |
result.Add("VOLUME", iter->second.Volume); |
throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!"); |
2066 |
} |
} |
2067 |
|
result.Add("VOLUME", entry.Volume); |
2068 |
} catch (Exception e) { |
} catch (Exception e) { |
2069 |
result.Error(e); |
result.Error(e); |
2070 |
} |
} |
2204 |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); |
2205 |
LSCPResultSet result; |
LSCPResultSet result; |
2206 |
try { |
try { |
2207 |
result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); |
result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID))); |
2208 |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); |
2209 |
} catch (Exception e) { |
} catch (Exception e) { |
2210 |
result.Error(e); |
result.Error(e); |
2235 |
dmsg(2,("LSCPServer: SetChannelMap()\n")); |
dmsg(2,("LSCPServer: SetChannelMap()\n")); |
2236 |
LSCPResultSet result; |
LSCPResultSet result; |
2237 |
try { |
try { |
2238 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
|
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); |
|
2239 |
|
|
2240 |
if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone(); |
if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone(); |
2241 |
else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault(); |
else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault(); |
2251 |
LSCPResultSet result; |
LSCPResultSet result; |
2252 |
try { |
try { |
2253 |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
2254 |
|
|
2255 |
FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name); |
FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name); |
2256 |
if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)"); |
if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)"); |
2257 |
|
|
2335 |
try { |
try { |
2336 |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
2337 |
FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); |
FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); |
2338 |
|
|
2339 |
// gather audio routing informations |
// gather audio routing informations |
2340 |
String AudioRouting; |
String AudioRouting; |
2341 |
for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { |
for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { |
2344 |
} |
} |
2345 |
|
|
2346 |
// success |
// success |
2347 |
result.Add("NAME", pFxSend->Name()); |
result.Add("NAME", _escapeLscpResponse(pFxSend->Name())); |
2348 |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); |
2349 |
result.Add("LEVEL", ToString(pFxSend->Level())); |
result.Add("LEVEL", ToString(pFxSend->Level())); |
2350 |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); |
2410 |
return result.Produce(); |
return result.Produce(); |
2411 |
} |
} |
2412 |
|
|
2413 |
|
String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) { |
2414 |
|
dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel)); |
2415 |
|
LSCPResultSet result; |
2416 |
|
try { |
2417 |
|
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
2418 |
|
if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel"); |
2419 |
|
Engine* pEngine = pEngineChannel->GetEngine(); |
2420 |
|
InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager(); |
2421 |
|
if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager"); |
2422 |
|
InstrumentManager::instrument_id_t instrumentID; |
2423 |
|
instrumentID.FileName = pEngineChannel->InstrumentFileName(); |
2424 |
|
instrumentID.Index = pEngineChannel->InstrumentIndex(); |
2425 |
|
pInstrumentManager->LaunchInstrumentEditor(instrumentID); |
2426 |
|
} catch (Exception e) { |
2427 |
|
result.Error(e); |
2428 |
|
} |
2429 |
|
return result.Produce(); |
2430 |
|
} |
2431 |
|
|
2432 |
|
String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) { |
2433 |
|
dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2)); |
2434 |
|
LSCPResultSet result; |
2435 |
|
try { |
2436 |
|
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
2437 |
|
|
2438 |
|
if (Arg1 > 127 || Arg2 > 127) { |
2439 |
|
throw Exception("Invalid MIDI message"); |
2440 |
|
} |
2441 |
|
|
2442 |
|
VirtualMidiDevice* pMidiDevice = NULL; |
2443 |
|
std::vector<EventHandler::midi_listener_entry>::iterator iter = eventHandler.channelMidiListeners.begin(); |
2444 |
|
for (; iter != eventHandler.channelMidiListeners.end(); ++iter) { |
2445 |
|
if ((*iter).pEngineChannel == pEngineChannel) { |
2446 |
|
pMidiDevice = (*iter).pMidiListener; |
2447 |
|
break; |
2448 |
|
} |
2449 |
|
} |
2450 |
|
|
2451 |
|
if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device"); |
2452 |
|
|
2453 |
|
if (MidiMsg == "NOTE_ON") { |
2454 |
|
bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2); |
2455 |
|
if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); |
2456 |
|
} else if (MidiMsg == "NOTE_OFF") { |
2457 |
|
bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2); |
2458 |
|
if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); |
2459 |
|
} else { |
2460 |
|
throw Exception("Unknown MIDI message type: " + MidiMsg); |
2461 |
|
} |
2462 |
|
} catch (Exception e) { |
2463 |
|
result.Error(e); |
2464 |
|
} |
2465 |
|
return result.Produce(); |
2466 |
|
} |
2467 |
|
|
2468 |
/** |
/** |
2469 |
* Will be called by the parser to reset a particular sampler channel. |
* Will be called by the parser to reset a particular sampler channel. |
2470 |
*/ |
*/ |
2472 |
dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel)); |
dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel)); |
2473 |
LSCPResultSet result; |
LSCPResultSet result; |
2474 |
try { |
try { |
2475 |
SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); |
EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); |
|
if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); |
|
|
EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); |
|
|
if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); |
|
2476 |
pEngineChannel->Reset(); |
pEngineChannel->Reset(); |
2477 |
} |
} |
2478 |
catch (Exception e) { |
catch (Exception e) { |
2497 |
*/ |
*/ |
2498 |
String LSCPServer::GetServerInfo() { |
String LSCPServer::GetServerInfo() { |
2499 |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
dmsg(2,("LSCPServer: GetServerInfo()\n")); |
2500 |
|
const std::string description = |
2501 |
|
_escapeLscpResponse("LinuxSampler - modular, streaming capable sampler"); |
2502 |
LSCPResultSet result; |
LSCPResultSet result; |
2503 |
result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); |
result.Add("DESCRIPTION", description); |
2504 |
result.Add("VERSION", VERSION); |
result.Add("VERSION", VERSION); |
2505 |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); |
2506 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
2508 |
#else |
#else |
2509 |
result.Add("INSTRUMENTS_DB_SUPPORT", "no"); |
result.Add("INSTRUMENTS_DB_SUPPORT", "no"); |
2510 |
#endif |
#endif |
2511 |
|
|
2512 |
|
return result.Produce(); |
2513 |
|
} |
2514 |
|
|
2515 |
|
/** |
2516 |
|
* Will be called by the parser to return the current number of all active streams. |
2517 |
|
*/ |
2518 |
|
String LSCPServer::GetTotalStreamCount() { |
2519 |
|
dmsg(2,("LSCPServer: GetTotalStreamCount()\n")); |
2520 |
|
LSCPResultSet result; |
2521 |
|
result.Add(pSampler->GetDiskStreamCount()); |
2522 |
return result.Produce(); |
return result.Produce(); |
2523 |
} |
} |
2524 |
|
|
2552 |
LSCPResultSet result; |
LSCPResultSet result; |
2553 |
try { |
try { |
2554 |
if (dVolume < 0) throw Exception("Volume may not be negative"); |
if (dVolume < 0) throw Exception("Volume may not be negative"); |
2555 |
GLOBAL_VOLUME = dVolume; // see common/global.cpp |
GLOBAL_VOLUME = dVolume; // see common/global_private.cpp |
2556 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); |
2557 |
} catch (Exception e) { |
} catch (Exception e) { |
2558 |
result.Error(e); |
result.Error(e); |
2560 |
return result.Produce(); |
return result.Produce(); |
2561 |
} |
} |
2562 |
|
|
2563 |
|
String LSCPServer::GetFileInstruments(String Filename) { |
2564 |
|
dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2565 |
|
LSCPResultSet result; |
2566 |
|
try { |
2567 |
|
VerifyFile(Filename); |
2568 |
|
} catch (Exception e) { |
2569 |
|
result.Error(e); |
2570 |
|
return result.Produce(); |
2571 |
|
} |
2572 |
|
// try to find a sampler engine that can handle the file |
2573 |
|
bool bFound = false; |
2574 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2575 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2576 |
|
Engine* pEngine = NULL; |
2577 |
|
try { |
2578 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2579 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2580 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2581 |
|
if (pManager) { |
2582 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2583 |
|
pManager->GetInstrumentFileContent(Filename); |
2584 |
|
// return the amount of instruments in the file |
2585 |
|
result.Add(IDs.size()); |
2586 |
|
// no more need to ask other engine types |
2587 |
|
bFound = true; |
2588 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2589 |
|
} catch (Exception e) { |
2590 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2591 |
|
} |
2592 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2593 |
|
} |
2594 |
|
|
2595 |
|
if (!bFound) result.Error("Unknown file format"); |
2596 |
|
return result.Produce(); |
2597 |
|
} |
2598 |
|
|
2599 |
|
String LSCPServer::ListFileInstruments(String Filename) { |
2600 |
|
dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2601 |
|
LSCPResultSet result; |
2602 |
|
try { |
2603 |
|
VerifyFile(Filename); |
2604 |
|
} catch (Exception e) { |
2605 |
|
result.Error(e); |
2606 |
|
return result.Produce(); |
2607 |
|
} |
2608 |
|
// try to find a sampler engine that can handle the file |
2609 |
|
bool bFound = false; |
2610 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2611 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2612 |
|
Engine* pEngine = NULL; |
2613 |
|
try { |
2614 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2615 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2616 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2617 |
|
if (pManager) { |
2618 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2619 |
|
pManager->GetInstrumentFileContent(Filename); |
2620 |
|
// return a list of IDs of the instruments in the file |
2621 |
|
String s; |
2622 |
|
for (int j = 0; j < IDs.size(); j++) { |
2623 |
|
if (s.size()) s += ","; |
2624 |
|
s += ToString(IDs[j].Index); |
2625 |
|
} |
2626 |
|
result.Add(s); |
2627 |
|
// no more need to ask other engine types |
2628 |
|
bFound = true; |
2629 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2630 |
|
} catch (Exception e) { |
2631 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2632 |
|
} |
2633 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2634 |
|
} |
2635 |
|
|
2636 |
|
if (!bFound) result.Error("Unknown file format"); |
2637 |
|
return result.Produce(); |
2638 |
|
} |
2639 |
|
|
2640 |
|
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
2641 |
|
dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID)); |
2642 |
|
LSCPResultSet result; |
2643 |
|
try { |
2644 |
|
VerifyFile(Filename); |
2645 |
|
} catch (Exception e) { |
2646 |
|
result.Error(e); |
2647 |
|
return result.Produce(); |
2648 |
|
} |
2649 |
|
InstrumentManager::instrument_id_t id; |
2650 |
|
id.FileName = Filename; |
2651 |
|
id.Index = InstrumentID; |
2652 |
|
// try to find a sampler engine that can handle the file |
2653 |
|
bool bFound = false; |
2654 |
|
bool bFatalErr = false; |
2655 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2656 |
|
for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) { |
2657 |
|
Engine* pEngine = NULL; |
2658 |
|
try { |
2659 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2660 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2661 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2662 |
|
if (pManager) { |
2663 |
|
// check if the instrument index is valid |
2664 |
|
// FIXME: this won't work if an engine only supports parts of the instrument file |
2665 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2666 |
|
pManager->GetInstrumentFileContent(Filename); |
2667 |
|
if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) { |
2668 |
|
std::stringstream ss; |
2669 |
|
ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'"; |
2670 |
|
bFatalErr = true; |
2671 |
|
throw Exception(ss.str()); |
2672 |
|
} |
2673 |
|
// get the info of the requested instrument |
2674 |
|
InstrumentManager::instrument_info_t info = |
2675 |
|
pManager->GetInstrumentInfo(id); |
2676 |
|
// return detailed informations about the file |
2677 |
|
result.Add("NAME", info.InstrumentName); |
2678 |
|
result.Add("FORMAT_FAMILY", engineTypes[i]); |
2679 |
|
result.Add("FORMAT_VERSION", info.FormatVersion); |
2680 |
|
result.Add("PRODUCT", info.Product); |
2681 |
|
result.Add("ARTISTS", info.Artists); |
2682 |
|
|
2683 |
|
std::stringstream ss; |
2684 |
|
bool b = false; |
2685 |
|
for (int i = 0; i < 128; i++) { |
2686 |
|
if (info.KeyBindings[i]) { |
2687 |
|
if (b) ss << ','; |
2688 |
|
ss << i; b = true; |
2689 |
|
} |
2690 |
|
} |
2691 |
|
result.Add("KEY_BINDINGS", ss.str()); |
2692 |
|
|
2693 |
|
std::stringstream ss2; |
2694 |
|
for (int i = 0; i < 128; i++) { |
2695 |
|
if (info.KeySwitchBindings[i]) { |
2696 |
|
if (b) ss2 << ','; |
2697 |
|
ss2 << i; b = true; |
2698 |
|
} |
2699 |
|
} |
2700 |
|
result.Add("KEYSWITCH_BINDINGS", ss2.str()); |
2701 |
|
// no more need to ask other engine types |
2702 |
|
bFound = true; |
2703 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2704 |
|
} catch (Exception e) { |
2705 |
|
// usually NOOP, as exception is thrown if engine doesn't support file |
2706 |
|
if (bFatalErr) result.Error(e); |
2707 |
|
} |
2708 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2709 |
|
} |
2710 |
|
|
2711 |
|
if (!bFound && !bFatalErr) result.Error("Unknown file format"); |
2712 |
|
return result.Produce(); |
2713 |
|
} |
2714 |
|
|
2715 |
|
void LSCPServer::VerifyFile(String Filename) { |
2716 |
|
#if WIN32 |
2717 |
|
WIN32_FIND_DATA win32FileAttributeData; |
2718 |
|
BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData ); |
2719 |
|
if (!res) { |
2720 |
|
std::stringstream ss; |
2721 |
|
ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError(); |
2722 |
|
throw Exception(ss.str()); |
2723 |
|
} |
2724 |
|
if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { |
2725 |
|
throw Exception("Directory is specified"); |
2726 |
|
} |
2727 |
|
#else |
2728 |
|
struct stat statBuf; |
2729 |
|
int res = stat(Filename.c_str(), &statBuf); |
2730 |
|
if (res) { |
2731 |
|
std::stringstream ss; |
2732 |
|
ss << "Fail to stat `" << Filename << "`: " << strerror(errno); |
2733 |
|
throw Exception(ss.str()); |
2734 |
|
} |
2735 |
|
|
2736 |
|
if (S_ISDIR(statBuf.st_mode)) { |
2737 |
|
throw Exception("Directory is specified"); |
2738 |
|
} |
2739 |
|
#endif |
2740 |
|
} |
2741 |
|
|
2742 |
/** |
/** |
2743 |
* 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 |
2744 |
* server for receiving event messages. |
* server for receiving event messages. |
2820 |
|
|
2821 |
for (int i = 0; i < dirs->size(); i++) { |
for (int i = 0; i < dirs->size(); i++) { |
2822 |
if (list != "") list += ","; |
if (list != "") list += ","; |
2823 |
list += "'" + dirs->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'"; |
2824 |
} |
} |
2825 |
|
|
2826 |
result.Add(list); |
result.Add(list); |
2840 |
try { |
try { |
2841 |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); |
2842 |
|
|
2843 |
result.Add("DESCRIPTION", info.Description); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
2844 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
2845 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
2846 |
} catch (Exception e) { |
} catch (Exception e) { |
2912 |
return result.Produce(); |
return result.Produce(); |
2913 |
} |
} |
2914 |
|
|
2915 |
String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index) { |
String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) { |
2916 |
dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); |
dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); |
2917 |
LSCPResultSet result; |
LSCPResultSet result; |
2918 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
2919 |
try { |
try { |
2920 |
InstrumentsDb::GetInstrumentsDb()->AddInstruments(DbDir, FilePath, Index); |
int id; |
2921 |
|
InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); |
2922 |
|
id = db->AddInstruments(DbDir, FilePath, Index, bBackground); |
2923 |
|
if (bBackground) result = id; |
2924 |
} catch (Exception e) { |
} catch (Exception e) { |
2925 |
result.Error(e); |
result.Error(e); |
2926 |
} |
} |
2930 |
return result.Produce(); |
return result.Produce(); |
2931 |
} |
} |
2932 |
|
|
2933 |
String LSCPServer::AddDbInstrumentsFlat(String DbDir, String FsDir) { |
String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) { |
2934 |
dmsg(2,("LSCPServer: AddDbInstrumentsFlat(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str())); |
dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground)); |
2935 |
LSCPResultSet result; |
LSCPResultSet result; |
2936 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
2937 |
try { |
try { |
2938 |
InstrumentsDb::GetInstrumentsDb()->AddInstrumentsRecursive(DbDir, FsDir, true); |
int id; |
2939 |
} catch (Exception e) { |
InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); |
2940 |
result.Error(e); |
if (ScanMode.compare("RECURSIVE") == 0) { |
2941 |
} |
id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground); |
2942 |
#else |
} else if (ScanMode.compare("NON_RECURSIVE") == 0) { |
2943 |
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground); |
2944 |
#endif |
} else if (ScanMode.compare("FLAT") == 0) { |
2945 |
return result.Produce(); |
id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground); |
2946 |
} |
} else { |
2947 |
|
throw Exception("Unknown scan mode: " + ScanMode); |
2948 |
|
} |
2949 |
|
|
2950 |
String LSCPServer::AddDbInstrumentsNonrecursive(String DbDir, String FsDir) { |
if (bBackground) result = id; |
|
dmsg(2,("LSCPServer: AddDbInstrumentsNonrecursive(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str())); |
|
|
LSCPResultSet result; |
|
|
#if HAVE_SQLITE3 |
|
|
try { |
|
|
InstrumentsDb::GetInstrumentsDb()->AddInstrumentsNonrecursive(DbDir, FsDir); |
|
2951 |
} catch (Exception e) { |
} catch (Exception e) { |
2952 |
result.Error(e); |
result.Error(e); |
2953 |
} |
} |
2997 |
|
|
2998 |
for (int i = 0; i < instrs->size(); i++) { |
for (int i = 0; i < instrs->size(); i++) { |
2999 |
if (list != "") list += ","; |
if (list != "") list += ","; |
3000 |
list += "'" + instrs->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'"; |
3001 |
} |
} |
3002 |
|
|
3003 |
result.Add(list); |
result.Add(list); |
3024 |
result.Add("SIZE", (int)info.Size); |
result.Add("SIZE", (int)info.Size); |
3025 |
result.Add("CREATED", info.Created); |
result.Add("CREATED", info.Created); |
3026 |
result.Add("MODIFIED", info.Modified); |
result.Add("MODIFIED", info.Modified); |
3027 |
result.Add("DESCRIPTION", FilterEndlines(info.Description)); |
result.Add("DESCRIPTION", _escapeLscpResponse(info.Description)); |
3028 |
result.Add("IS_DRUM", info.IsDrum); |
result.Add("IS_DRUM", info.IsDrum); |
3029 |
result.Add("PRODUCT", FilterEndlines(info.Product)); |
result.Add("PRODUCT", _escapeLscpResponse(info.Product)); |
3030 |
result.Add("ARTISTS", FilterEndlines(info.Artists)); |
result.Add("ARTISTS", _escapeLscpResponse(info.Artists)); |
3031 |
result.Add("KEYWORDS", FilterEndlines(info.Keywords)); |
result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords)); |
3032 |
|
} catch (Exception e) { |
3033 |
|
result.Error(e); |
3034 |
|
} |
3035 |
|
#else |
3036 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3037 |
|
#endif |
3038 |
|
return result.Produce(); |
3039 |
|
} |
3040 |
|
|
3041 |
|
String LSCPServer::GetDbInstrumentsJobInfo(int JobId) { |
3042 |
|
dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId)); |
3043 |
|
LSCPResultSet result; |
3044 |
|
#if HAVE_SQLITE3 |
3045 |
|
try { |
3046 |
|
ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId); |
3047 |
|
|
3048 |
|
result.Add("FILES_TOTAL", job.FilesTotal); |
3049 |
|
result.Add("FILES_SCANNED", job.FilesScanned); |
3050 |
|
result.Add("SCANNING", job.Scanning); |
3051 |
|
result.Add("STATUS", job.Status); |
3052 |
} catch (Exception e) { |
} catch (Exception e) { |
3053 |
result.Error(e); |
result.Error(e); |
3054 |
} |
} |
3118 |
return result.Produce(); |
return result.Produce(); |
3119 |
} |
} |
3120 |
|
|
3121 |
|
String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) { |
3122 |
|
dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str())); |
3123 |
|
LSCPResultSet result; |
3124 |
|
#if HAVE_SQLITE3 |
3125 |
|
try { |
3126 |
|
InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath); |
3127 |
|
} catch (Exception e) { |
3128 |
|
result.Error(e); |
3129 |
|
} |
3130 |
|
#else |
3131 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3132 |
|
#endif |
3133 |
|
return result.Produce(); |
3134 |
|
} |
3135 |
|
|
3136 |
|
String LSCPServer::FindLostDbInstrumentFiles() { |
3137 |
|
dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n")); |
3138 |
|
LSCPResultSet result; |
3139 |
|
#if HAVE_SQLITE3 |
3140 |
|
try { |
3141 |
|
String list; |
3142 |
|
StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles(); |
3143 |
|
|
3144 |
|
for (int i = 0; i < pLostFiles->size(); i++) { |
3145 |
|
if (list != "") list += ","; |
3146 |
|
list += "'" + pLostFiles->at(i) + "'"; |
3147 |
|
} |
3148 |
|
|
3149 |
|
result.Add(list); |
3150 |
|
} catch (Exception e) { |
3151 |
|
result.Error(e); |
3152 |
|
} |
3153 |
|
#else |
3154 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3155 |
|
#endif |
3156 |
|
return result.Produce(); |
3157 |
|
} |
3158 |
|
|
3159 |
String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) { |
String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) { |
3160 |
dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str())); |
dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str())); |
3161 |
LSCPResultSet result; |
LSCPResultSet result; |
3183 |
|
|
3184 |
for (int i = 0; i < pDirectories->size(); i++) { |
for (int i = 0; i < pDirectories->size(); i++) { |
3185 |
if (list != "") list += ","; |
if (list != "") list += ","; |
3186 |
list += "'" + pDirectories->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'"; |
3187 |
} |
} |
3188 |
|
|
3189 |
result.Add(list); |
result.Add(list); |
3239 |
|
|
3240 |
for (int i = 0; i < pInstruments->size(); i++) { |
for (int i = 0; i < pInstruments->size(); i++) { |
3241 |
if (list != "") list += ","; |
if (list != "") list += ","; |
3242 |
list += "'" + pInstruments->at(i) + "'"; |
list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'"; |
3243 |
} |
} |
3244 |
|
|
3245 |
result.Add(list); |
result.Add(list); |
3252 |
return result.Produce(); |
return result.Produce(); |
3253 |
} |
} |
3254 |
|
|
3255 |
|
String LSCPServer::FormatInstrumentsDb() { |
3256 |
|
dmsg(2,("LSCPServer: FormatInstrumentsDb()\n")); |
3257 |
|
LSCPResultSet result; |
3258 |
|
#if HAVE_SQLITE3 |
3259 |
|
try { |
3260 |
|
InstrumentsDb::GetInstrumentsDb()->Format(); |
3261 |
|
} catch (Exception e) { |
3262 |
|
result.Error(e); |
3263 |
|
} |
3264 |
|
#else |
3265 |
|
result.Error(String(DOESNT_HAVE_SQLITE3), 0); |
3266 |
|
#endif |
3267 |
|
return result.Produce(); |
3268 |
|
} |
3269 |
|
|
3270 |
|
|
3271 |
/** |
/** |
3272 |
* 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 |
3287 |
return result.Produce(); |
return result.Produce(); |
3288 |
} |
} |
3289 |
|
|
|
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; |
|
3290 |
} |
} |