/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/network/lscpserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1882 - (hide annotations) (download)
Wed Apr 1 15:21:13 2009 UTC (15 years ago) by iliev
File size: 132997 byte(s)
* LSCP server: hard close of all sockets on exit
* Load instruments in background when restoring plugin state

1 schoenebeck 35 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 persson 1850 * Copyright (C) 2005 - 2009 Christian Schoenebeck *
7 schoenebeck 35 * *
8 schoenebeck 385 * This library is free software; you can redistribute it and/or modify *
9 schoenebeck 35 * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13 schoenebeck 385 * This library is distributed in the hope that it will be useful, *
14 schoenebeck 35 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19 schoenebeck 385 * along with this library; if not, write to the Free Software *
20 schoenebeck 35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24 persson 1713 #include <algorithm>
25 iliev 1771 #include <string>
26 persson 1713
27 iliev 1835 #include "../common/File.h"
28 schoenebeck 35 #include "lscpserver.h"
29 senkov 113 #include "lscpresultset.h"
30 senkov 170 #include "lscpevent.h"
31 schoenebeck 35
32 senoner 1481 #if defined(WIN32)
33 senoner 1537 #include <windows.h>
34 senoner 1481 #else
35 schoenebeck 411 #include <fcntl.h>
36 senoner 1481 #endif
37 schoenebeck 411
38 iliev 1161 #if ! HAVE_SQLITE3
39     #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
40 senkov 397 #endif
41    
42 schoenebeck 411 #include "../engines/EngineFactory.h"
43 schoenebeck 660 #include "../engines/EngineChannelFactory.h"
44 schoenebeck 203 #include "../drivers/audio/AudioOutputDeviceFactory.h"
45     #include "../drivers/midi/MidiInputDeviceFactory.h"
46 schoenebeck 53
47 persson 1765 namespace LinuxSampler {
48 schoenebeck 1399
49 senkov 170 /**
50 schoenebeck 1399 * Returns a copy of the given string where all special characters are
51     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
52     * to escape LSCP response fields in case the respective response field is
53     * actually defined as using escape sequences in the LSCP specs.
54     *
55     * @e Caution: DO NOT use this function for escaping path based responses,
56     * use the Path class (src/common/Path.h) for this instead!
57     */
58     static String _escapeLscpResponse(String txt) {
59     for (int i = 0; i < txt.length(); i++) {
60     const char c = txt.c_str()[i];
61     if (
62     !(c >= '0' && c <= '9') &&
63     !(c >= 'a' && c <= 'z') &&
64     !(c >= 'A' && c <= 'Z') &&
65     !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
66     !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
67     !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
68     !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
69     !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
70 iliev 1403 !(c == '@') && !(c == '[') && !(c == ']') &&
71 schoenebeck 1399 !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
72     !(c == '|') && !(c == '}') && !(c == '~')
73     ) {
74     // convert the "special" character into a "\xHH" LSCP escape sequence
75     char buf[5];
76     snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
77     txt.replace(i, 1, buf);
78     i += 3;
79     }
80     }
81     return txt;
82     }
83    
84     /**
85 senkov 170 * Below are a few static members of the LSCPServer class.
86     * The big assumption here is that LSCPServer is going to remain a singleton.
87     * These members are used to support client connections.
88     * Class handles multiple connections at the same time using select() and non-blocking recv()
89     * Commands are processed by a single LSCPServer thread.
90     * Notifications are delivered either by the thread that originated them
91     * or (if the resultset is currently in progress) by the LSCPServer thread
92     * after the resultset was sent out.
93     * This makes sure that resultsets can not be interrupted by notifications.
94     * This also makes sure that the thread sending notification is not blocked
95     * by the LSCPServer thread.
96     */
97     fd_set LSCPServer::fdSet;
98     int LSCPServer::currentSocket = -1;
99 schoenebeck 210 std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
100 schoenebeck 1252 std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
101 senkov 170 std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
102     std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
103     std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
104     Mutex LSCPServer::NotifyMutex = Mutex();
105     Mutex LSCPServer::NotifyBufferMutex = Mutex();
106     Mutex LSCPServer::SubscriptionMutex = Mutex();
107 senkov 360 Mutex LSCPServer::RTNotifyMutex = Mutex();
108 senkov 170
109 schoenebeck 1686 LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
110 senkov 667 SocketAddress.sin_family = AF_INET;
111     SocketAddress.sin_addr.s_addr = addr;
112     SocketAddress.sin_port = port;
113 schoenebeck 53 this->pSampler = pSampler;
114 iliev 981 LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_count, "AUDIO_OUTPUT_DEVICE_COUNT");
115     LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_info, "AUDIO_OUTPUT_DEVICE_INFO");
116     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_count, "MIDI_INPUT_DEVICE_COUNT");
117     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_info, "MIDI_INPUT_DEVICE_INFO");
118 schoenebeck 556 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
119 senkov 170 LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
120     LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
121     LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
122 schoenebeck 556 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
123 iliev 1108 LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
124     LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
125 iliev 981 LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
126     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
127     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
128     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
129 iliev 1161 LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
130     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
131     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
132     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
133 iliev 1200 LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
134 senkov 170 LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
135 iliev 1541 LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
136 iliev 778 LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
137 iliev 1108 LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
138 schoenebeck 1686 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
139 schoenebeck 1695 LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
140 schoenebeck 475 hSocket = -1;
141 schoenebeck 35 }
142    
143 schoenebeck 475 LSCPServer::~LSCPServer() {
144 iliev 1848 CloseAllConnections();
145 persson 1850 InstrumentManager::StopBackgroundThread();
146 senoner 1481 #if defined(WIN32)
147     if (hSocket >= 0) closesocket(hSocket);
148     #else
149 schoenebeck 475 if (hSocket >= 0) close(hSocket);
150 senoner 1481 #endif
151 schoenebeck 475 }
152    
153 schoenebeck 1686 LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
154     this->pParent = pParent;
155     }
156    
157     LSCPServer::EventHandler::~EventHandler() {
158     std::vector<midi_listener_entry> l = channelMidiListeners;
159     channelMidiListeners.clear();
160     for (int i = 0; i < l.size(); i++)
161     delete l[i].pMidiListener;
162     }
163    
164 iliev 1133 void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
165 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
166     }
167    
168 schoenebeck 1686 void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
169     pChannel->AddEngineChangeListener(this);
170     }
171    
172     void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
173     if (!pChannel->GetEngineChannel()) return;
174     EngineToBeChanged(pChannel->Index());
175     }
176    
177     void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
178     SamplerChannel* pSamplerChannel =
179     pParent->pSampler->GetSamplerChannel(ChannelId);
180     if (!pSamplerChannel) return;
181     EngineChannel* pEngineChannel =
182     pSamplerChannel->GetEngineChannel();
183     if (!pEngineChannel) return;
184     for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
185     if ((*iter).pEngineChannel == pEngineChannel) {
186     VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
187     pEngineChannel->Disconnect(pMidiListener);
188     channelMidiListeners.erase(iter);
189     delete pMidiListener;
190     return;
191     }
192     }
193     }
194    
195     void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
196     SamplerChannel* pSamplerChannel =
197     pParent->pSampler->GetSamplerChannel(ChannelId);
198     if (!pSamplerChannel) return;
199     EngineChannel* pEngineChannel =
200     pSamplerChannel->GetEngineChannel();
201     if (!pEngineChannel) return;
202     VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
203     pEngineChannel->Connect(pMidiListener);
204     midi_listener_entry entry = {
205     pSamplerChannel, pEngineChannel, pMidiListener
206     };
207     channelMidiListeners.push_back(entry);
208     }
209    
210 iliev 1133 void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
211 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
212     }
213    
214 iliev 1133 void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
215 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
216     }
217    
218 schoenebeck 1695 void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
219     pDevice->RemoveMidiPortCountListener(this);
220     for (int i = 0; i < pDevice->PortCount(); ++i)
221     MidiPortToBeRemoved(pDevice->GetPort(i));
222     }
223    
224     void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
225     pDevice->AddMidiPortCountListener(this);
226     for (int i = 0; i < pDevice->PortCount(); ++i)
227     MidiPortAdded(pDevice->GetPort(i));
228     }
229    
230     void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
231     // yet unused
232     }
233    
234     void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
235     for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
236     if ((*iter).pPort == pPort) {
237     VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
238     pPort->Disconnect(pMidiListener);
239     deviceMidiListeners.erase(iter);
240     delete pMidiListener;
241     return;
242     }
243     }
244     }
245    
246     void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
247     // find out the device ID
248     std::map<uint, MidiInputDevice*> devices =
249     pParent->pSampler->GetMidiInputDevices();
250     for (
251     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
252     iter != devices.end(); ++iter
253     ) {
254     if (iter->second == pPort->GetDevice()) { // found
255     VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
256     pPort->Connect(pMidiListener);
257     device_midi_listener_entry entry = {
258     pPort, pMidiListener, iter->first
259     };
260     deviceMidiListeners.push_back(entry);
261     return;
262     }
263     }
264     }
265    
266 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
267 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
268     }
269    
270 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
271 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
272     }
273    
274 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
275 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
276     }
277    
278 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
279 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
280     }
281    
282 iliev 1133 void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
283 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
284     }
285    
286 iliev 1133 void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
287 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
288     }
289    
290 iliev 1133 void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
291 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
292     }
293    
294 iliev 1133 void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
295 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
296     }
297    
298 iliev 1133 void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
299 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
300     }
301    
302 iliev 1541 void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
303     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
304     }
305    
306 iliev 1161 #if HAVE_SQLITE3
307     void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
308 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
309 iliev 1161 }
310 iliev 1130
311 iliev 1161 void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
312 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
313 iliev 1161 }
314    
315     void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
316 iliev 1345 Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
317 iliev 1350 NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
318 iliev 1161 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
319     }
320    
321     void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
322 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
323 iliev 1161 }
324    
325     void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
326 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
327 iliev 1161 }
328 iliev 1200
329 iliev 1161 void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
330 iliev 1345 Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
331 iliev 1350 NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
332 iliev 1161 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
333     }
334 iliev 1200
335     void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
336     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
337     }
338 iliev 1161 #endif // HAVE_SQLITE3
339    
340 iliev 1835 void LSCPServer::RemoveListeners() {
341     pSampler->RemoveChannelCountListener(&eventHandler);
342     pSampler->RemoveAudioDeviceCountListener(&eventHandler);
343     pSampler->RemoveMidiDeviceCountListener(&eventHandler);
344     pSampler->RemoveVoiceCountListener(&eventHandler);
345     pSampler->RemoveStreamCountListener(&eventHandler);
346     pSampler->RemoveBufferFillListener(&eventHandler);
347     pSampler->RemoveTotalStreamCountListener(&eventHandler);
348     pSampler->RemoveTotalVoiceCountListener(&eventHandler);
349     pSampler->RemoveFxSendCountListener(&eventHandler);
350     MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler);
351     MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler);
352     MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler);
353     MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler);
354     #if HAVE_SQLITE3
355     InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler);
356     #endif
357     }
358 iliev 1161
359 schoenebeck 211 /**
360     * Blocks the calling thread until the LSCP Server is initialized and
361     * accepting socket connections, if the server is already initialized then
362     * this method will return immediately.
363     * @param TimeoutSeconds - optional: max. wait time in seconds
364     * (default: 0s)
365     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
366     * (default: 0ns)
367     * @returns 0 on success, a value less than 0 if timeout exceeded
368     */
369     int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
370     return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
371     }
372    
373 schoenebeck 35 int LSCPServer::Main() {
374 senoner 1481 #if defined(WIN32)
375     WSADATA wsaData;
376     int iResult;
377     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
378     if (iResult != 0) {
379     std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
380     exit(EXIT_FAILURE);
381     }
382     #endif
383 schoenebeck 475 hSocket = socket(AF_INET, SOCK_STREAM, 0);
384 schoenebeck 35 if (hSocket < 0) {
385     std::cerr << "LSCPServer: Could not create server socket." << std::endl;
386 schoenebeck 53 //return -1;
387     exit(EXIT_FAILURE);
388 schoenebeck 35 }
389    
390     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
391 schoenebeck 227 std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
392     for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
393     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
394     if (trial > LSCP_SERVER_BIND_TIMEOUT) {
395     std::cerr << "gave up!" << std::endl;
396 senoner 1481 #if defined(WIN32)
397     closesocket(hSocket);
398     #else
399 schoenebeck 227 close(hSocket);
400 senoner 1481 #endif
401 schoenebeck 227 //return -1;
402     exit(EXIT_FAILURE);
403     }
404     else sleep(1); // sleep 1s
405     }
406     else break; // success
407     }
408 schoenebeck 35 }
409    
410     listen(hSocket, 1);
411 schoenebeck 211 Initialized.Set(true);
412 schoenebeck 1252
413 iliev 1130 // Registering event listeners
414 iliev 1133 pSampler->AddChannelCountListener(&eventHandler);
415     pSampler->AddAudioDeviceCountListener(&eventHandler);
416     pSampler->AddMidiDeviceCountListener(&eventHandler);
417     pSampler->AddVoiceCountListener(&eventHandler);
418     pSampler->AddStreamCountListener(&eventHandler);
419     pSampler->AddBufferFillListener(&eventHandler);
420 iliev 1541 pSampler->AddTotalStreamCountListener(&eventHandler);
421 iliev 1133 pSampler->AddTotalVoiceCountListener(&eventHandler);
422     pSampler->AddFxSendCountListener(&eventHandler);
423     MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
424     MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
425     MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
426     MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
427 iliev 1161 #if HAVE_SQLITE3
428     InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
429     #endif
430 schoenebeck 35 // now wait for client connections and handle their requests
431     sockaddr_in client;
432     int length = sizeof(client);
433 senkov 170 FD_ZERO(&fdSet);
434     FD_SET(hSocket, &fdSet);
435     int maxSessions = hSocket;
436 schoenebeck 203
437 iliev 793 timeval timeout;
438    
439 schoenebeck 35 while (true) {
440 nagata 1649 #if CONFIG_PTHREAD_TESTCANCEL
441     TestCancel();
442     #endif
443 iliev 793 // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
444     {
445     std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
446     std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
447     std::set<EngineChannel*>::iterator itEnd = engineChannels.end();
448     for (; itEngineChannel != itEnd; ++itEngineChannel) {
449     if ((*itEngineChannel)->StatusChanged()) {
450 iliev 1761 SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index()));
451 iliev 793 }
452 iliev 1108
453     for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
454     FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
455     if(fxs != NULL && fxs->IsInfoChanged()) {
456 iliev 1761 int chn = (*itEngineChannel)->GetSamplerChannel()->Index();
457 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
458     fxs->SetInfoChanged(false);
459     }
460     }
461 iliev 793 }
462     }
463    
464 schoenebeck 1686 // check if MIDI data arrived on some engine channel
465     for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
466     const EventHandler::midi_listener_entry entry =
467     eventHandler.channelMidiListeners[i];
468     VirtualMidiDevice* pMidiListener = entry.pMidiListener;
469     if (pMidiListener->NotesChanged()) {
470     for (int iNote = 0; iNote < 128; iNote++) {
471     if (pMidiListener->NoteChanged(iNote)) {
472     const bool bActive = pMidiListener->NoteIsActive(iNote);
473     LSCPServer::SendLSCPNotify(
474     LSCPEvent(
475     LSCPEvent::event_channel_midi,
476     entry.pSamplerChannel->Index(),
477     std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
478     iNote,
479     bActive ? pMidiListener->NoteOnVelocity(iNote)
480     : pMidiListener->NoteOffVelocity(iNote)
481     )
482     );
483     }
484     }
485     }
486     }
487    
488 schoenebeck 1695 // check if MIDI data arrived on some MIDI device
489     for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
490     const EventHandler::device_midi_listener_entry entry =
491     eventHandler.deviceMidiListeners[i];
492     VirtualMidiDevice* pMidiListener = entry.pMidiListener;
493     if (pMidiListener->NotesChanged()) {
494     for (int iNote = 0; iNote < 128; iNote++) {
495     if (pMidiListener->NoteChanged(iNote)) {
496     const bool bActive = pMidiListener->NoteIsActive(iNote);
497     LSCPServer::SendLSCPNotify(
498     LSCPEvent(
499     LSCPEvent::event_device_midi,
500     entry.uiDeviceID,
501     entry.pPort->GetPortNumber(),
502     std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
503     iNote,
504     bActive ? pMidiListener->NoteOnVelocity(iNote)
505     : pMidiListener->NoteOffVelocity(iNote)
506     )
507     );
508     }
509     }
510     }
511     }
512    
513 iliev 793 //Now let's deliver late notifies (if any)
514     NotifyBufferMutex.Lock();
515     for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
516 wylder 814 #ifdef MSG_NOSIGNAL
517 iliev 793 send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
518 wylder 814 #else
519     send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
520     #endif
521 iliev 793 }
522 persson 836 bufferedNotifies.clear();
523 iliev 793 NotifyBufferMutex.Unlock();
524    
525     fd_set selectSet = fdSet;
526     timeout.tv_sec = 0;
527     timeout.tv_usec = 100000;
528    
529     int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
530    
531 persson 1808 if (retval == 0 || (retval == -1 && errno == EINTR))
532 senkov 198 continue; //Nothing try again
533 senkov 170 if (retval == -1) {
534     std::cerr << "LSCPServer: Socket select error." << std::endl;
535 senoner 1481 #if defined(WIN32)
536     closesocket(hSocket);
537     #else
538 senkov 170 close(hSocket);
539 senoner 1481 #endif
540 senkov 170 exit(EXIT_FAILURE);
541     }
542 schoenebeck 203
543 senkov 170 //Accept new connections now (if any)
544     if (FD_ISSET(hSocket, &selectSet)) {
545     int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
546     if (socket < 0) {
547     std::cerr << "LSCPServer: Client connection failed." << std::endl;
548     exit(EXIT_FAILURE);
549     }
550 schoenebeck 35
551 senoner 1481 #if defined(WIN32)
552     u_long nonblock_io = 1;
553     if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
554     std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
555     exit(EXIT_FAILURE);
556     }
557 schoenebeck 1536 #else
558 iliev 1882 struct linger linger;
559     linger.l_onoff = 1;
560     linger.l_linger = 0;
561     if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) {
562     std::cerr << "LSCPServer: Failed to set SO_LINGER\n";
563     }
564    
565 senkov 170 if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
566     std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
567     exit(EXIT_FAILURE);
568     }
569 senoner 1481 #endif
570 schoenebeck 35
571 schoenebeck 210 // Parser initialization
572     yyparse_param_t yyparse_param;
573     yyparse_param.pServer = this;
574     yyparse_param.hSession = socket;
575    
576     Sessions.push_back(yyparse_param);
577 senkov 170 FD_SET(socket, &fdSet);
578     if (socket > maxSessions)
579     maxSessions = socket;
580     dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
581     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
582     continue; //Maybe this was the only selected socket, better select again
583     }
584 schoenebeck 35
585 senkov 170 //Something was selected and it was not the hSocket, so it must be some command(s) coming.
586 schoenebeck 210 for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
587     if (FD_ISSET((*iter).hSession, &selectSet)) { //Was it this socket?
588 senkov 170 if (GetLSCPCommand(iter)) { //Have we read the entire command?
589     dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
590 schoenebeck 219 int dummy; // just a temporary hack to fulfill the restart() function prototype
591     restart(NULL, dummy); // restart the 'scanner'
592 schoenebeck 210 currentSocket = (*iter).hSession; //a hack
593 schoenebeck 1252 itCurrentSession = iter; // another hack
594 schoenebeck 475 dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
595 schoenebeck 210 if ((*iter).bVerbose) { // if echo mode enabled
596     AnswerClient(bufferedCommands[currentSocket]);
597     }
598     int result = yyparse(&(*iter));
599 senkov 170 currentSocket = -1; //continuation of a hack
600 schoenebeck 1252 itCurrentSession = Sessions.end(); // hack as well
601 senkov 170 dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
602     if (result == LSCP_QUIT) { //Was it a quit command by any chance?
603     CloseConnection(iter);
604     }
605     }
606     //socket may have been closed, iter may be invalid, get out of the loop for now.
607     //we'll be back if there is data.
608 schoenebeck 203 break;
609 senkov 170 }
610     }
611 schoenebeck 35 }
612     }
613    
614 schoenebeck 210 void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
615     int socket = (*iter).hSession;
616 senkov 170 dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
617     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
618 schoenebeck 210 Sessions.erase(iter);
619 senkov 170 FD_CLR(socket, &fdSet);
620     SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
621     for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
622     iter->second.remove(socket);
623     }
624     SubscriptionMutex.Unlock();
625     NotifyMutex.Lock();
626     bufferedCommands.erase(socket);
627     bufferedNotifies.erase(socket);
628 senoner 1481 #if defined(WIN32)
629     closesocket(socket);
630     #else
631 senkov 170 close(socket);
632 senoner 1481 #endif
633 senkov 170 NotifyMutex.Unlock();
634     }
635    
636 iliev 1848 void LSCPServer::CloseAllConnections() {
637     std::vector<yyparse_param_t>::iterator iter = Sessions.begin();
638     while(iter != Sessions.end()) {
639     CloseConnection(iter);
640     iter = Sessions.begin();
641     }
642     }
643    
644 schoenebeck 1551 void LSCPServer::LockRTNotify() {
645     RTNotifyMutex.Lock();
646     }
647    
648     void LSCPServer::UnlockRTNotify() {
649     RTNotifyMutex.Unlock();
650     }
651    
652 senkov 360 int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
653     int subs = 0;
654     SubscriptionMutex.Lock();
655     for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
656     iter != events.end(); iter++)
657     {
658     subs += eventSubscriptions.count(*iter);
659     }
660     SubscriptionMutex.Unlock();
661     return subs;
662     }
663    
664 senkov 170 void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
665     SubscriptionMutex.Lock();
666     if (eventSubscriptions.count(event.GetType()) == 0) {
667     SubscriptionMutex.Unlock(); //Nobody is subscribed to this event
668     return;
669     }
670     std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
671     std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
672     String notify = event.Produce();
673    
674     while (true) {
675     if (NotifyMutex.Trylock()) {
676     for(;iter != end; iter++)
677 wylder 814 #ifdef MSG_NOSIGNAL
678 iliev 707 send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
679 wylder 814 #else
680     send(*iter, notify.c_str(), notify.size(), 0);
681     #endif
682 senkov 170 NotifyMutex.Unlock();
683     break;
684     } else {
685     if (NotifyBufferMutex.Trylock()) {
686     for(;iter != end; iter++)
687     bufferedNotifies[*iter] += notify;
688     NotifyBufferMutex.Unlock();
689     break;
690     }
691     }
692     }
693     SubscriptionMutex.Unlock();
694     }
695    
696     extern int GetLSCPCommand( void *buf, int max_size ) {
697     String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
698     if (command.size() == 0) { //Parser wants input but we have nothing.
699     strcpy((char*) buf, "\n"); //So give it an empty command
700     return 1; //to keep it happy.
701     }
702    
703     if (max_size < command.size()) {
704     std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
705     return 0; //This will never happen
706     }
707    
708     strcpy((char*) buf, command.c_str());
709     LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
710     return command.size();
711     }
712    
713 schoenebeck 1252 extern yyparse_param_t* GetCurrentYaccSession() {
714     return &(*itCurrentSession);
715     }
716    
717 schoenebeck 35 /**
718 senkov 170 * Will be called to try to read the command from the socket
719     * If command is read, it will return true. Otherwise false is returned.
720     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
721     */
722 schoenebeck 210 bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
723     int socket = (*iter).hSession;
724 senkov 170 char c;
725     int i = 0;
726     while (true) {
727 senoner 1481 #if defined(WIN32)
728     int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
729     #else
730 senkov 170 int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
731 senoner 1481 #endif
732 senkov 170 if (result == 0) { //socket was selected, so 0 here means client has closed the connection
733     CloseConnection(iter);
734     break;
735     }
736     if (result == 1) {
737 schoenebeck 203 if (c == '\r')
738 senkov 170 continue; //Ignore CR
739     if (c == '\n') {
740 senkov 184 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
741 iliev 907 bufferedCommands[socket] += "\r\n";
742 senkov 170 return true; //Complete command was read
743     }
744     bufferedCommands[socket] += c;
745     }
746 senoner 1481 #if defined(WIN32)
747     if (result == SOCKET_ERROR) {
748     int wsa_lasterror = WSAGetLastError();
749     if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
750     return false;
751 schoenebeck 1536 dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
752 senoner 1481 CloseConnection(iter);
753     break;
754     }
755     #else
756 senkov 170 if (result == -1) {
757     if (errno == EAGAIN) //Would block, try again later.
758     return false;
759     switch(errno) {
760     case EBADF:
761     dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
762     break;
763     case ECONNREFUSED:
764     dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
765     break;
766     case ENOTCONN:
767     dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
768     break;
769     case ENOTSOCK:
770     dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
771     break;
772     case EAGAIN:
773     dmsg(2,("LSCPScanner: The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received.\n"));
774 schoenebeck 203 break;
775     case EINTR:
776 senkov 170 dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
777 schoenebeck 203 break;
778     case EFAULT:
779     dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
780     break;
781     case EINVAL:
782     dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
783     break;
784     case ENOMEM:
785     dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
786     break;
787     default:
788     dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
789     break;
790     }
791 senkov 170 CloseConnection(iter);
792     break;
793     }
794 senoner 1481 #endif
795 senkov 170 }
796     return false;
797     }
798    
799     /**
800 schoenebeck 35 * Will be called by the parser whenever it wants to send an answer to the
801     * client / frontend.
802     *
803     * @param ReturnMessage - message that will be send to the client
804     */
805     void LSCPServer::AnswerClient(String ReturnMessage) {
806     dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
807 senkov 170 if (currentSocket != -1) {
808     NotifyMutex.Lock();
809 wylder 814 #ifdef MSG_NOSIGNAL
810 iliev 707 send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
811 wylder 814 #else
812     send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
813     #endif
814 senkov 170 NotifyMutex.Unlock();
815     }
816 schoenebeck 35 }
817    
818 capela 143 /**
819     * Find a created audio output device index.
820     */
821     int LSCPServer::GetAudioOutputDeviceIndex ( AudioOutputDevice *pDevice )
822     {
823     // Search for the created device to get its index
824     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
825     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
826     for (; iter != devices.end(); iter++) {
827     if (iter->second == pDevice)
828     return iter->first;
829     }
830     // Not found.
831     return -1;
832     }
833    
834 senkov 155 /**
835     * Find a created midi input device index.
836     */
837     int LSCPServer::GetMidiInputDeviceIndex ( MidiInputDevice *pDevice )
838     {
839     // Search for the created device to get its index
840     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
841     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
842     for (; iter != devices.end(); iter++) {
843     if (iter->second == pDevice)
844     return iter->first;
845     }
846     // Not found.
847     return -1;
848     }
849    
850 schoenebeck 123 String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
851     dmsg(2,("LSCPServer: CreateAudioOutputDevice(Driver=%s)\n", Driver.c_str()));
852     LSCPResultSet result;
853     try {
854     AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
855     // search for the created device to get its index
856 capela 143 int index = GetAudioOutputDeviceIndex(pDevice);
857 schoenebeck 880 if (index == -1) throw Exception("Internal error: could not find created audio output device.");
858 schoenebeck 123 result = index; // success
859     }
860 schoenebeck 880 catch (Exception e) {
861 schoenebeck 123 result.Error(e);
862     }
863     return result.Produce();
864     }
865    
866 senkov 155 String LSCPServer::CreateMidiInputDevice(String Driver, std::map<String,String> Parameters) {
867     dmsg(2,("LSCPServer: CreateMidiInputDevice(Driver=%s)\n", Driver.c_str()));
868     LSCPResultSet result;
869     try {
870     MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
871     // search for the created device to get its index
872     int index = GetMidiInputDeviceIndex(pDevice);
873 schoenebeck 880 if (index == -1) throw Exception("Internal error: could not find created midi input device.");
874 senkov 155 result = index; // success
875     }
876 schoenebeck 880 catch (Exception e) {
877 senkov 155 result.Error(e);
878     }
879     return result.Produce();
880     }
881    
882 schoenebeck 123 String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
883     dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
884     LSCPResultSet result;
885     try {
886     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
887 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
888 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
889     pSampler->DestroyAudioOutputDevice(pDevice);
890     }
891 schoenebeck 880 catch (Exception e) {
892 schoenebeck 123 result.Error(e);
893     }
894     return result.Produce();
895     }
896    
897 senkov 155 String LSCPServer::DestroyMidiInputDevice(uint DeviceIndex) {
898     dmsg(2,("LSCPServer: DestroyMidiInputDevice(DeviceIndex=%d)\n", DeviceIndex));
899     LSCPResultSet result;
900     try {
901     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
902 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
903 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
904     pSampler->DestroyMidiInputDevice(pDevice);
905     }
906 schoenebeck 880 catch (Exception e) {
907 senkov 155 result.Error(e);
908     }
909     return result.Produce();
910     }
911    
912 iliev 1135 EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
913     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
914     if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
915    
916     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
917     if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
918    
919 schoenebeck 1252 return pEngineChannel;
920 iliev 1135 }
921    
922 schoenebeck 35 /**
923     * Will be called by the parser to load an instrument.
924     */
925 capela 137 String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground) {
926 schoenebeck 53 dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));
927 senkov 120 LSCPResultSet result;
928 schoenebeck 53 try {
929     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
930 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
931 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
932 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel yet");
933 schoenebeck 223 if (!pSamplerChannel->GetAudioOutputDevice())
934 schoenebeck 880 throw Exception("No audio output device connected to sampler channel");
935 capela 137 if (bBackground) {
936 schoenebeck 947 InstrumentManager::instrument_id_t id;
937     id.FileName = Filename;
938     id.Index = uiInstrument;
939     InstrumentManager::LoadInstrumentInBackground(id, pEngineChannel);
940 capela 137 }
941 schoenebeck 392 else {
942 schoenebeck 411 // tell the engine channel which instrument to load
943     pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
944 schoenebeck 392 // actually start to load the instrument (blocks until completed)
945 schoenebeck 411 pEngineChannel->LoadInstrument();
946 schoenebeck 392 }
947 schoenebeck 53 }
948 schoenebeck 880 catch (Exception e) {
949 senkov 120 result.Error(e);
950 schoenebeck 53 }
951 senkov 120 return result.Produce();
952 schoenebeck 35 }
953    
954     /**
955 schoenebeck 411 * Will be called by the parser to assign a sampler engine type to a
956     * sampler channel.
957 schoenebeck 35 */
958 schoenebeck 411 String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
959 schoenebeck 705 dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
960 senkov 120 LSCPResultSet result;
961 schoenebeck 475 try {
962 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
963 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
964 senkov 360 LockRTNotify();
965 schoenebeck 411 pSamplerChannel->SetEngineType(EngineName);
966 schoenebeck 705 if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
967 senkov 360 UnlockRTNotify();
968 schoenebeck 53 }
969 schoenebeck 880 catch (Exception e) {
970 senkov 120 result.Error(e);
971 schoenebeck 53 }
972 senkov 120 return result.Produce();
973 schoenebeck 35 }
974    
975     /**
976     * Will be called by the parser to get the amount of sampler channels.
977     */
978     String LSCPServer::GetChannels() {
979     dmsg(2,("LSCPServer: GetChannels()\n"));
980 senkov 120 LSCPResultSet result;
981     result.Add(pSampler->SamplerChannels());
982     return result.Produce();
983 schoenebeck 35 }
984    
985     /**
986 schoenebeck 209 * Will be called by the parser to get the list of sampler channels.
987     */
988     String LSCPServer::ListChannels() {
989     dmsg(2,("LSCPServer: ListChannels()\n"));
990     String list;
991     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
992     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
993     for (; iter != channels.end(); iter++) {
994     if (list != "") list += ",";
995     list += ToString(iter->first);
996     }
997     LSCPResultSet result;
998     result.Add(list);
999     return result.Produce();
1000     }
1001    
1002     /**
1003 schoenebeck 35 * Will be called by the parser to add a sampler channel.
1004     */
1005     String LSCPServer::AddChannel() {
1006     dmsg(2,("LSCPServer: AddChannel()\n"));
1007 persson 841 LockRTNotify();
1008 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
1009 persson 841 UnlockRTNotify();
1010 senkov 120 LSCPResultSet result(pSamplerChannel->Index());
1011     return result.Produce();
1012 schoenebeck 35 }
1013    
1014     /**
1015     * Will be called by the parser to remove a sampler channel.
1016     */
1017 schoenebeck 53 String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
1018     dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
1019 senkov 120 LSCPResultSet result;
1020 senkov 360 LockRTNotify();
1021 schoenebeck 53 pSampler->RemoveSamplerChannel(uiSamplerChannel);
1022 senkov 360 UnlockRTNotify();
1023 senkov 120 return result.Produce();
1024 schoenebeck 35 }
1025    
1026     /**
1027 capela 527 * Will be called by the parser to get the amount of all available engines.
1028 schoenebeck 35 */
1029     String LSCPServer::GetAvailableEngines() {
1030     dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
1031 schoenebeck 905 LSCPResultSet result;
1032     try {
1033     int n = EngineFactory::AvailableEngineTypes().size();
1034     result.Add(n);
1035     }
1036     catch (Exception e) {
1037     result.Error(e);
1038     }
1039 senkov 120 return result.Produce();
1040 schoenebeck 35 }
1041    
1042     /**
1043 capela 527 * Will be called by the parser to get a list of all available engines.
1044     */
1045     String LSCPServer::ListAvailableEngines() {
1046     dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
1047 schoenebeck 905 LSCPResultSet result;
1048     try {
1049     String s = EngineFactory::AvailableEngineTypesAsString();
1050     result.Add(s);
1051     }
1052     catch (Exception e) {
1053     result.Error(e);
1054     }
1055 capela 527 return result.Produce();
1056     }
1057    
1058     /**
1059 schoenebeck 411 * Will be called by the parser to get descriptions for a particular
1060     * sampler engine.
1061 schoenebeck 35 */
1062     String LSCPServer::GetEngineInfo(String EngineName) {
1063     dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
1064 senkov 120 LSCPResultSet result;
1065 persson 841 LockRTNotify();
1066 schoenebeck 53 try {
1067 schoenebeck 411 Engine* pEngine = EngineFactory::Create(EngineName);
1068 schoenebeck 1399 result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1069 schoenebeck 411 result.Add("VERSION", pEngine->Version());
1070 schoenebeck 660 EngineFactory::Destroy(pEngine);
1071 schoenebeck 53 }
1072 schoenebeck 880 catch (Exception e) {
1073 senkov 120 result.Error(e);
1074 schoenebeck 53 }
1075 persson 841 UnlockRTNotify();
1076 senkov 120 return result.Produce();
1077 schoenebeck 35 }
1078    
1079     /**
1080     * Will be called by the parser to get informations about a particular
1081     * sampler channel.
1082     */
1083 schoenebeck 53 String LSCPServer::GetChannelInfo(uint uiSamplerChannel) {
1084     dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", uiSamplerChannel));
1085 senkov 120 LSCPResultSet result;
1086 senkov 113 try {
1087     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1088 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1089 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1090 schoenebeck 123
1091 senkov 117 //Defaults values
1092     String EngineName = "NONE";
1093 schoenebeck 225 float Volume = 0.0f;
1094 senkov 117 String InstrumentFileName = "NONE";
1095 senkov 376 String InstrumentName = "NONE";
1096 capela 133 int InstrumentIndex = -1;
1097     int InstrumentStatus = -1;
1098 schoenebeck 225 int AudioOutputChannels = 0;
1099     String AudioRouting;
1100 schoenebeck 705 int Mute = 0;
1101     bool Solo = false;
1102 iliev 1130 String MidiInstrumentMap = "NONE";
1103 schoenebeck 123
1104 schoenebeck 475 if (pEngineChannel) {
1105     EngineName = pEngineChannel->EngineName();
1106 schoenebeck 411 AudioOutputChannels = pEngineChannel->Channels();
1107     Volume = pEngineChannel->Volume();
1108     InstrumentStatus = pEngineChannel->InstrumentStatus();
1109     InstrumentIndex = pEngineChannel->InstrumentIndex();
1110     if (InstrumentIndex != -1) {
1111     InstrumentFileName = pEngineChannel->InstrumentFileName();
1112     InstrumentName = pEngineChannel->InstrumentName();
1113     }
1114     for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
1115 schoenebeck 225 if (AudioRouting != "") AudioRouting += ",";
1116 schoenebeck 411 AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
1117 schoenebeck 225 }
1118 schoenebeck 705 Mute = pEngineChannel->GetMute();
1119     Solo = pEngineChannel->GetSolo();
1120 schoenebeck 973 if (pEngineChannel->UsesNoMidiInstrumentMap())
1121     MidiInstrumentMap = "NONE";
1122     else if (pEngineChannel->UsesDefaultMidiInstrumentMap())
1123     MidiInstrumentMap = "DEFAULT";
1124     else
1125     MidiInstrumentMap = ToString(pEngineChannel->GetMidiInstrumentMap());
1126 senkov 113 }
1127 senkov 117
1128     result.Add("ENGINE_NAME", EngineName);
1129     result.Add("VOLUME", Volume);
1130    
1131 capela 143 //Some not-so-hardcoded stuff to make GUI look good
1132     result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
1133 schoenebeck 225 result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
1134     result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
1135 senkov 113
1136 capela 159 result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
1137     result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
1138 schoenebeck 675 if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
1139 schoenebeck 274 else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
1140 capela 159
1141 schoenebeck 1471 // convert the filename into the correct encoding as defined for LSCP
1142     // (especially in terms of special characters -> escape sequences)
1143     if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
1144     #if WIN32
1145     InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
1146     #else
1147     // assuming POSIX
1148     InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
1149     #endif
1150     }
1151    
1152     result.Add("INSTRUMENT_FILE", InstrumentFileName);
1153 senkov 117 result.Add("INSTRUMENT_NR", InstrumentIndex);
1154 schoenebeck 1399 result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
1155 capela 133 result.Add("INSTRUMENT_STATUS", InstrumentStatus);
1156 schoenebeck 705 result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
1157     result.Add("SOLO", Solo);
1158 schoenebeck 973 result.Add("MIDI_INSTRUMENT_MAP", MidiInstrumentMap);
1159 senkov 113 }
1160 schoenebeck 880 catch (Exception e) {
1161 senkov 120 result.Error(e);
1162 senkov 113 }
1163 senkov 120 return result.Produce();
1164 schoenebeck 35 }
1165    
1166     /**
1167     * Will be called by the parser to get the amount of active voices on a
1168     * particular sampler channel.
1169     */
1170 schoenebeck 53 String LSCPServer::GetVoiceCount(uint uiSamplerChannel) {
1171     dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
1172 senkov 120 LSCPResultSet result;
1173 schoenebeck 53 try {
1174 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1175 schoenebeck 880 if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1176 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->VoiceCount());
1177 schoenebeck 53 }
1178 schoenebeck 880 catch (Exception e) {
1179 senkov 120 result.Error(e);
1180 schoenebeck 53 }
1181 senkov 120 return result.Produce();
1182 schoenebeck 35 }
1183    
1184     /**
1185     * Will be called by the parser to get the amount of active disk streams on a
1186     * particular sampler channel.
1187     */
1188 schoenebeck 53 String LSCPServer::GetStreamCount(uint uiSamplerChannel) {
1189     dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
1190 senkov 120 LSCPResultSet result;
1191 schoenebeck 53 try {
1192 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1193 schoenebeck 880 if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1194 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
1195 schoenebeck 53 }
1196 schoenebeck 880 catch (Exception e) {
1197 senkov 120 result.Error(e);
1198 schoenebeck 53 }
1199 senkov 120 return result.Produce();
1200 schoenebeck 35 }
1201    
1202     /**
1203     * Will be called by the parser to get the buffer fill states of all disk
1204     * streams on a particular sampler channel.
1205     */
1206 schoenebeck 53 String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel) {
1207     dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
1208 senkov 120 LSCPResultSet result;
1209 schoenebeck 53 try {
1210 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1211 schoenebeck 880 if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1212 schoenebeck 411 if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
1213 senkov 129 else {
1214     switch (ResponseType) {
1215     case fill_response_bytes:
1216 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
1217     break;
1218 senkov 129 case fill_response_percentage:
1219 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
1220     break;
1221 senkov 129 default:
1222 schoenebeck 880 throw Exception("Unknown fill response type");
1223 senkov 129 }
1224     }
1225 schoenebeck 53 }
1226 schoenebeck 880 catch (Exception e) {
1227 senkov 120 result.Error(e);
1228 schoenebeck 53 }
1229 senkov 120 return result.Produce();
1230 schoenebeck 35 }
1231    
1232 schoenebeck 123 String LSCPServer::GetAvailableAudioOutputDrivers() {
1233     dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
1234 senkov 120 LSCPResultSet result;
1235 schoenebeck 53 try {
1236 capela 527 int n = AudioOutputDeviceFactory::AvailableDrivers().size();
1237     result.Add(n);
1238     }
1239 schoenebeck 880 catch (Exception e) {
1240 capela 527 result.Error(e);
1241     }
1242     return result.Produce();
1243     }
1244    
1245     String LSCPServer::ListAvailableAudioOutputDrivers() {
1246     dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
1247     LSCPResultSet result;
1248     try {
1249 schoenebeck 123 String s = AudioOutputDeviceFactory::AvailableDriversAsString();
1250     result.Add(s);
1251 schoenebeck 53 }
1252 schoenebeck 880 catch (Exception e) {
1253 schoenebeck 123 result.Error(e);
1254 schoenebeck 53 }
1255 senkov 120 return result.Produce();
1256 schoenebeck 35 }
1257    
1258 senkov 155 String LSCPServer::GetAvailableMidiInputDrivers() {
1259     dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
1260     LSCPResultSet result;
1261     try {
1262 capela 527 int n = MidiInputDeviceFactory::AvailableDrivers().size();
1263     result.Add(n);
1264     }
1265 schoenebeck 880 catch (Exception e) {
1266 capela 527 result.Error(e);
1267     }
1268     return result.Produce();
1269     }
1270    
1271     String LSCPServer::ListAvailableMidiInputDrivers() {
1272     dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
1273     LSCPResultSet result;
1274     try {
1275 senkov 155 String s = MidiInputDeviceFactory::AvailableDriversAsString();
1276     result.Add(s);
1277     }
1278 schoenebeck 880 catch (Exception e) {
1279 senkov 155 result.Error(e);
1280     }
1281     return result.Produce();
1282     }
1283    
1284     String LSCPServer::GetMidiInputDriverInfo(String Driver) {
1285     dmsg(2,("LSCPServer: GetMidiInputDriverInfo(Driver=%s)\n",Driver.c_str()));
1286     LSCPResultSet result;
1287     try {
1288     result.Add("DESCRIPTION", MidiInputDeviceFactory::GetDriverDescription(Driver));
1289     result.Add("VERSION", MidiInputDeviceFactory::GetDriverVersion(Driver));
1290    
1291     std::map<String,DeviceCreationParameter*> parameters = MidiInputDeviceFactory::GetAvailableDriverParameters(Driver);
1292     if (parameters.size()) { // if there are parameters defined for this driver
1293     String s;
1294     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1295     for (;iter != parameters.end(); iter++) {
1296     if (s != "") s += ",";
1297     s += iter->first;
1298 iliev 1835 delete iter->second;
1299 senkov 155 }
1300     result.Add("PARAMETERS", s);
1301     }
1302     }
1303 schoenebeck 880 catch (Exception e) {
1304 senkov 155 result.Error(e);
1305     }
1306     return result.Produce();
1307     }
1308    
1309 schoenebeck 123 String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
1310     dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
1311     LSCPResultSet result;
1312     try {
1313     result.Add("DESCRIPTION", AudioOutputDeviceFactory::GetDriverDescription(Driver));
1314     result.Add("VERSION", AudioOutputDeviceFactory::GetDriverVersion(Driver));
1315    
1316     std::map<String,DeviceCreationParameter*> parameters = AudioOutputDeviceFactory::GetAvailableDriverParameters(Driver);
1317     if (parameters.size()) { // if there are parameters defined for this driver
1318     String s;
1319     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1320     for (;iter != parameters.end(); iter++) {
1321     if (s != "") s += ",";
1322     s += iter->first;
1323 iliev 1835 delete iter->second;
1324 schoenebeck 123 }
1325     result.Add("PARAMETERS", s);
1326     }
1327     }
1328 schoenebeck 880 catch (Exception e) {
1329 schoenebeck 123 result.Error(e);
1330     }
1331     return result.Produce();
1332     }
1333    
1334 senkov 155 String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1335 schoenebeck 226 dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1336 senkov 155 LSCPResultSet result;
1337     try {
1338     DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
1339     result.Add("TYPE", pParameter->Type());
1340     result.Add("DESCRIPTION", pParameter->Description());
1341 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
1342     result.Add("FIX", pParameter->Fix());
1343     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1344 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
1345     optional<String> oDefault = pParameter->Default(DependencyList);
1346     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
1347     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
1348     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1349     if (oDepends) result.Add("DEPENDS", *oDepends);
1350     if (oDefault) result.Add("DEFAULT", *oDefault);
1351     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
1352     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
1353     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1354 iliev 1835 delete pParameter;
1355 senkov 155 }
1356 schoenebeck 880 catch (Exception e) {
1357 senkov 155 result.Error(e);
1358     }
1359     return result.Produce();
1360     }
1361    
1362 schoenebeck 123 String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1363 schoenebeck 226 dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1364 schoenebeck 123 LSCPResultSet result;
1365     try {
1366     DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
1367     result.Add("TYPE", pParameter->Type());
1368     result.Add("DESCRIPTION", pParameter->Description());
1369 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
1370     result.Add("FIX", pParameter->Fix());
1371     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1372 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
1373     optional<String> oDefault = pParameter->Default(DependencyList);
1374     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
1375     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
1376     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1377     if (oDepends) result.Add("DEPENDS", *oDepends);
1378     if (oDefault) result.Add("DEFAULT", *oDefault);
1379     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
1380     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
1381     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1382 iliev 1835 delete pParameter;
1383 schoenebeck 123 }
1384 schoenebeck 880 catch (Exception e) {
1385 schoenebeck 123 result.Error(e);
1386     }
1387     return result.Produce();
1388     }
1389    
1390     String LSCPServer::GetAudioOutputDeviceCount() {
1391     dmsg(2,("LSCPServer: GetAudioOutputDeviceCount()\n"));
1392     LSCPResultSet result;
1393     try {
1394     uint count = pSampler->AudioOutputDevices();
1395 senkov 138 result.Add(count); // success
1396 schoenebeck 123 }
1397 schoenebeck 880 catch (Exception e) {
1398 schoenebeck 123 result.Error(e);
1399     }
1400     return result.Produce();
1401     }
1402    
1403 senkov 155 String LSCPServer::GetMidiInputDeviceCount() {
1404     dmsg(2,("LSCPServer: GetMidiInputDeviceCount()\n"));
1405     LSCPResultSet result;
1406     try {
1407     uint count = pSampler->MidiInputDevices();
1408     result.Add(count); // success
1409     }
1410 schoenebeck 880 catch (Exception e) {
1411 senkov 155 result.Error(e);
1412     }
1413     return result.Produce();
1414     }
1415    
1416 schoenebeck 123 String LSCPServer::GetAudioOutputDevices() {
1417     dmsg(2,("LSCPServer: GetAudioOutputDevices()\n"));
1418     LSCPResultSet result;
1419     try {
1420     String s;
1421     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1422     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1423     for (; iter != devices.end(); iter++) {
1424     if (s != "") s += ",";
1425     s += ToString(iter->first);
1426     }
1427     result.Add(s);
1428     }
1429 schoenebeck 880 catch (Exception e) {
1430 schoenebeck 123 result.Error(e);
1431     }
1432     return result.Produce();
1433     }
1434    
1435 senkov 155 String LSCPServer::GetMidiInputDevices() {
1436     dmsg(2,("LSCPServer: GetMidiInputDevices()\n"));
1437     LSCPResultSet result;
1438     try {
1439     String s;
1440     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1441     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1442     for (; iter != devices.end(); iter++) {
1443     if (s != "") s += ",";
1444     s += ToString(iter->first);
1445     }
1446     result.Add(s);
1447     }
1448 schoenebeck 880 catch (Exception e) {
1449 senkov 155 result.Error(e);
1450     }
1451     return result.Produce();
1452     }
1453    
1454 schoenebeck 123 String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
1455     dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
1456     LSCPResultSet result;
1457     try {
1458     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1459 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1460 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
1461 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
1462 schoenebeck 123 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1463     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1464     for (; iter != parameters.end(); iter++) {
1465     result.Add(iter->first, iter->second->Value());
1466     }
1467     }
1468 schoenebeck 880 catch (Exception e) {
1469 schoenebeck 123 result.Error(e);
1470     }
1471     return result.Produce();
1472     }
1473    
1474 senkov 155 String LSCPServer::GetMidiInputDeviceInfo(uint DeviceIndex) {
1475     dmsg(2,("LSCPServer: GetMidiInputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
1476     LSCPResultSet result;
1477     try {
1478     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1479 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1480 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1481 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
1482 senkov 155 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1483     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1484     for (; iter != parameters.end(); iter++) {
1485     result.Add(iter->first, iter->second->Value());
1486     }
1487     }
1488 schoenebeck 880 catch (Exception e) {
1489 senkov 155 result.Error(e);
1490     }
1491     return result.Produce();
1492     }
1493     String LSCPServer::GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex) {
1494     dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
1495     LSCPResultSet result;
1496     try {
1497 schoenebeck 223 // get MIDI input device
1498 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1499 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1500 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1501 schoenebeck 223
1502     // get MIDI port
1503 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1504 schoenebeck 880 if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1505 schoenebeck 223
1506     // return the values of all MIDI port parameters
1507 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1508     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1509 senkov 155 for (; iter != parameters.end(); iter++) {
1510     result.Add(iter->first, iter->second->Value());
1511     }
1512     }
1513 schoenebeck 880 catch (Exception e) {
1514 senkov 155 result.Error(e);
1515     }
1516     return result.Produce();
1517     }
1518    
1519 schoenebeck 123 String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
1520     dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
1521     LSCPResultSet result;
1522     try {
1523     // get audio output device
1524     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1525 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1526 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1527    
1528     // get audio channel
1529     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1530 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1531 schoenebeck 123
1532     // return the values of all audio channel parameters
1533     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1534     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1535     for (; iter != parameters.end(); iter++) {
1536     result.Add(iter->first, iter->second->Value());
1537     }
1538     }
1539 schoenebeck 880 catch (Exception e) {
1540 schoenebeck 123 result.Error(e);
1541     }
1542     return result.Produce();
1543     }
1544    
1545 senkov 185 String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
1546     dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
1547     LSCPResultSet result;
1548     try {
1549 schoenebeck 223 // get MIDI input device
1550     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1551 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no midi input device with index " + ToString(DeviceId) + ".");
1552 schoenebeck 223 MidiInputDevice* pDevice = devices[DeviceId];
1553 senkov 185
1554 schoenebeck 221 // get midi port
1555     MidiInputPort* pPort = pDevice->GetPort(PortId);
1556 schoenebeck 880 if (!pPort) throw Exception("Midi input device does not have port " + ToString(PortId) + ".");
1557 senkov 185
1558 schoenebeck 223 // get desired port parameter
1559     std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1560 schoenebeck 880 if (!parameters.count(ParameterName)) throw Exception("Midi port does not provide a parameter '" + ParameterName + "'.");
1561 schoenebeck 223 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1562 schoenebeck 203
1563 senkov 185 // return all fields of this audio channel parameter
1564     result.Add("TYPE", pParameter->Type());
1565     result.Add("DESCRIPTION", pParameter->Description());
1566     result.Add("FIX", pParameter->Fix());
1567     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1568 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1569     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1570     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1571 senkov 185 }
1572 schoenebeck 880 catch (Exception e) {
1573 senkov 185 result.Error(e);
1574     }
1575     return result.Produce();
1576     }
1577    
1578 schoenebeck 123 String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
1579     dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
1580     LSCPResultSet result;
1581     try {
1582     // get audio output device
1583     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1584 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1585 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1586    
1587     // get audio channel
1588     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1589 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1590 schoenebeck 123
1591     // get desired audio channel parameter
1592     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1593 schoenebeck 880 if (!parameters.count(ParameterName)) throw Exception("Audio channel does not provide a parameter '" + ParameterName + "'.");
1594 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1595    
1596     // return all fields of this audio channel parameter
1597     result.Add("TYPE", pParameter->Type());
1598     result.Add("DESCRIPTION", pParameter->Description());
1599     result.Add("FIX", pParameter->Fix());
1600     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1601 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1602     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1603     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1604 schoenebeck 123 }
1605 schoenebeck 880 catch (Exception e) {
1606 schoenebeck 123 result.Error(e);
1607     }
1608     return result.Produce();
1609     }
1610    
1611     String LSCPServer::SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal) {
1612     dmsg(2,("LSCPServer: SetAudioOutputChannelParameter(DeviceId=%d,ChannelId=%d,ParamKey=%s,ParamVal=%s)\n",DeviceId,ChannelId,ParamKey.c_str(),ParamVal.c_str()));
1613     LSCPResultSet result;
1614     try {
1615     // get audio output device
1616     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1617 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1618 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1619    
1620     // get audio channel
1621     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1622 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1623 schoenebeck 123
1624     // get desired audio channel parameter
1625     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1626 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("Audio channel does not provide a parameter '" + ParamKey + "'.");
1627 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1628    
1629     // set new channel parameter value
1630     pParameter->SetValue(ParamVal);
1631 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceId));
1632 schoenebeck 123 }
1633 schoenebeck 880 catch (Exception e) {
1634 schoenebeck 123 result.Error(e);
1635     }
1636     return result.Produce();
1637     }
1638    
1639     String LSCPServer::SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1640     dmsg(2,("LSCPServer: SetAudioOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1641     LSCPResultSet result;
1642     try {
1643     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1644 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1645 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
1646     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1647 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1648 schoenebeck 123 parameters[ParamKey]->SetValue(ParamVal);
1649 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceIndex));
1650 schoenebeck 123 }
1651 schoenebeck 880 catch (Exception e) {
1652 schoenebeck 123 result.Error(e);
1653     }
1654     return result.Produce();
1655     }
1656    
1657 senkov 155 String LSCPServer::SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1658     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1659     LSCPResultSet result;
1660     try {
1661     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1662 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1663 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1664     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1665 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1666 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1667 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1668 senkov 155 }
1669 schoenebeck 880 catch (Exception e) {
1670 senkov 155 result.Error(e);
1671     }
1672     return result.Produce();
1673     }
1674    
1675     String LSCPServer::SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal) {
1676     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1677     LSCPResultSet result;
1678     try {
1679 schoenebeck 223 // get MIDI input device
1680 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1681 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1682 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1683 schoenebeck 223
1684     // get MIDI port
1685 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1686 schoenebeck 880 if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1687 schoenebeck 223
1688     // set port parameter value
1689 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1690 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1691 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1692 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1693 senkov 155 }
1694 schoenebeck 880 catch (Exception e) {
1695 senkov 155 result.Error(e);
1696     }
1697     return result.Produce();
1698     }
1699    
1700 schoenebeck 35 /**
1701     * Will be called by the parser to change the audio output channel for
1702     * playback on a particular sampler channel.
1703     */
1704 schoenebeck 123 String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1705     dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));
1706 schoenebeck 225 LSCPResultSet result;
1707     try {
1708     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1709 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1710 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1711 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1712     if (!pSamplerChannel->GetAudioOutputDevice()) throw Exception("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1713 schoenebeck 411 pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1714 schoenebeck 225 }
1715 schoenebeck 880 catch (Exception e) {
1716 schoenebeck 225 result.Error(e);
1717     }
1718     return result.Produce();
1719 schoenebeck 35 }
1720    
1721 capela 159 String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1722     dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1723     LSCPResultSet result;
1724 persson 841 LockRTNotify();
1725 capela 159 try {
1726     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1727 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1728 capela 159 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1729 schoenebeck 880 if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));
1730 capela 159 AudioOutputDevice* pDevice = devices[AudioDeviceId];
1731     pSamplerChannel->SetAudioOutputDevice(pDevice);
1732     }
1733 schoenebeck 880 catch (Exception e) {
1734 capela 159 result.Error(e);
1735     }
1736 persson 841 UnlockRTNotify();
1737 capela 159 return result.Produce();
1738     }
1739    
1740 capela 143 String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1741     dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1742     LSCPResultSet result;
1743 persson 841 LockRTNotify();
1744 capela 143 try {
1745     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1746 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1747 capela 143 // Driver type name aliasing...
1748 schoenebeck 226 if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1749     if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1750 capela 143 // Check if there's one audio output device already created
1751     // for the intended audio driver type (AudioOutputDriver)...
1752     AudioOutputDevice *pDevice = NULL;
1753     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1754     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1755     for (; iter != devices.end(); iter++) {
1756     if ((iter->second)->Driver() == AudioOutputDriver) {
1757     pDevice = iter->second;
1758     break;
1759     }
1760     }
1761     // If it doesn't exist, create a new one with default parameters...
1762     if (pDevice == NULL) {
1763     std::map<String,String> params;
1764     pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1765     }
1766     // Must have a device...
1767     if (pDevice == NULL)
1768 schoenebeck 880 throw Exception("Internal error: could not create audio output device.");
1769 capela 143 // Set it as the current channel device...
1770     pSamplerChannel->SetAudioOutputDevice(pDevice);
1771     }
1772 schoenebeck 880 catch (Exception e) {
1773 capela 143 result.Error(e);
1774     }
1775 persson 841 UnlockRTNotify();
1776 capela 143 return result.Produce();
1777     }
1778    
1779 capela 159 String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1780     dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel));
1781 senkov 120 LSCPResultSet result;
1782 schoenebeck 53 try {
1783     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1784 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1785 capela 159 pSamplerChannel->SetMidiInputPort(MIDIPort);
1786 schoenebeck 53 }
1787 schoenebeck 880 catch (Exception e) {
1788 senkov 120 result.Error(e);
1789 schoenebeck 53 }
1790 senkov 120 return result.Produce();
1791 schoenebeck 53 }
1792    
1793 capela 159 String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1794     dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
1795 senkov 120 LSCPResultSet result;
1796 senkov 68 try {
1797     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1798 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1799 schoenebeck 675 pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1800 senkov 68 }
1801 schoenebeck 880 catch (Exception e) {
1802 senkov 120 result.Error(e);
1803 senkov 68 }
1804 senkov 120 return result.Produce();
1805 schoenebeck 35 }
1806    
1807 capela 159 String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1808     dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1809 schoenebeck 123 LSCPResultSet result;
1810     try {
1811 capela 159 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1812 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1813 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1814 schoenebeck 880 if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1815 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1816     pSamplerChannel->SetMidiInputDevice(pDevice);
1817 schoenebeck 123 }
1818 schoenebeck 880 catch (Exception e) {
1819 schoenebeck 123 result.Error(e);
1820     }
1821     return result.Produce();
1822     }
1823    
1824 capela 159 String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1825     dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1826     LSCPResultSet result;
1827     try {
1828     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1829 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1830 capela 159 // Driver type name aliasing...
1831 schoenebeck 226 if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1832 capela 159 // Check if there's one MIDI input device already created
1833     // for the intended MIDI driver type (MidiInputDriver)...
1834     MidiInputDevice *pDevice = NULL;
1835     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1836     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1837     for (; iter != devices.end(); iter++) {
1838     if ((iter->second)->Driver() == MidiInputDriver) {
1839     pDevice = iter->second;
1840     break;
1841     }
1842     }
1843     // If it doesn't exist, create a new one with default parameters...
1844     if (pDevice == NULL) {
1845     std::map<String,String> params;
1846     pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1847     // Make it with at least one initial port.
1848     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1849 schoenebeck 221 parameters["PORTS"]->SetValue("1");
1850 capela 159 }
1851     // Must have a device...
1852     if (pDevice == NULL)
1853 schoenebeck 880 throw Exception("Internal error: could not create MIDI input device.");
1854 capela 159 // Set it as the current channel device...
1855     pSamplerChannel->SetMidiInputDevice(pDevice);
1856     }
1857 schoenebeck 880 catch (Exception e) {
1858 capela 159 result.Error(e);
1859     }
1860     return result.Produce();
1861     }
1862    
1863 schoenebeck 35 /**
1864 capela 159 * Will be called by the parser to change the MIDI input device, port and channel on which
1865     * engine of a particular sampler channel should listen to.
1866     */
1867     String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1868     dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1869     LSCPResultSet result;
1870     try {
1871     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1872 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1873 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1874 schoenebeck 880 if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1875 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1876 schoenebeck 675 pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
1877 capela 159 }
1878 schoenebeck 880 catch (Exception e) {
1879 capela 159 result.Error(e);
1880     }
1881     return result.Produce();
1882     }
1883    
1884     /**
1885 schoenebeck 35 * Will be called by the parser to change the global volume factor on a
1886     * particular sampler channel.
1887     */
1888 schoenebeck 225 String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1889     dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1890 senkov 120 LSCPResultSet result;
1891 schoenebeck 53 try {
1892 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1893 schoenebeck 411 pEngineChannel->Volume(dVolume);
1894 schoenebeck 53 }
1895 schoenebeck 880 catch (Exception e) {
1896 senkov 120 result.Error(e);
1897 schoenebeck 53 }
1898 senkov 120 return result.Produce();
1899 schoenebeck 35 }
1900    
1901     /**
1902 schoenebeck 705 * Will be called by the parser to mute/unmute particular sampler channel.
1903     */
1904     String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1905     dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1906     LSCPResultSet result;
1907     try {
1908 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1909 schoenebeck 705
1910     if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1911     else pEngineChannel->SetMute(1);
1912 schoenebeck 880 } catch (Exception e) {
1913 schoenebeck 705 result.Error(e);
1914     }
1915     return result.Produce();
1916     }
1917    
1918     /**
1919     * Will be called by the parser to solo particular sampler channel.
1920     */
1921     String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1922     dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1923     LSCPResultSet result;
1924     try {
1925 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1926 schoenebeck 705
1927     bool oldSolo = pEngineChannel->GetSolo();
1928     bool hadSoloChannel = HasSoloChannel();
1929 schoenebeck 1009
1930 schoenebeck 705 pEngineChannel->SetSolo(bSolo);
1931 schoenebeck 1009
1932 schoenebeck 705 if(!oldSolo && bSolo) {
1933     if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1934     if(!hadSoloChannel) MuteNonSoloChannels();
1935     }
1936 schoenebeck 1009
1937 schoenebeck 705 if(oldSolo && !bSolo) {
1938     if(!HasSoloChannel()) UnmuteChannels();
1939     else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1940     }
1941 schoenebeck 880 } catch (Exception e) {
1942 schoenebeck 705 result.Error(e);
1943     }
1944     return result.Produce();
1945     }
1946    
1947     /**
1948     * Determines whether there is at least one solo channel in the channel list.
1949     *
1950     * @returns true if there is at least one solo channel in the channel list,
1951     * false otherwise.
1952     */
1953     bool LSCPServer::HasSoloChannel() {
1954     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1955     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1956     for (; iter != channels.end(); iter++) {
1957     EngineChannel* c = iter->second->GetEngineChannel();
1958     if(c && c->GetSolo()) return true;
1959     }
1960    
1961     return false;
1962     }
1963    
1964     /**
1965     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1966     * with -1 which indicates that they are muted because of the presence
1967     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1968     * when there are no solo channels left.
1969     */
1970     void LSCPServer::MuteNonSoloChannels() {
1971     dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1972     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1973     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1974     for (; iter != channels.end(); iter++) {
1975     EngineChannel* c = iter->second->GetEngineChannel();
1976     if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1977     }
1978     }
1979    
1980     /**
1981     * Unmutes all channels that are muted because of the presence
1982     * of a solo channel(s).
1983     */
1984     void LSCPServer::UnmuteChannels() {
1985     dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1986     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1987     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1988     for (; iter != channels.end(); iter++) {
1989     EngineChannel* c = iter->second->GetEngineChannel();
1990     if(c && c->GetMute() == -1) c->SetMute(0);
1991     }
1992     }
1993    
1994 schoenebeck 1047 String LSCPServer::AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name, bool bModal) {
1995 schoenebeck 947 dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1996    
1997     midi_prog_index_t idx;
1998 schoenebeck 973 idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1999     idx.midi_bank_lsb = MidiBank & 0x7f;
2000 schoenebeck 947 idx.midi_prog = MidiProg;
2001    
2002     MidiInstrumentMapper::entry_t entry;
2003     entry.EngineName = EngineType;
2004     entry.InstrumentFile = InstrumentFile;
2005     entry.InstrumentIndex = InstrumentIndex;
2006     entry.LoadMode = LoadMode;
2007     entry.Volume = Volume;
2008     entry.Name = Name;
2009    
2010     LSCPResultSet result;
2011     try {
2012 schoenebeck 1047 // PERSISTENT mapping commands might block for a long time, so in
2013     // that case we add/replace the mapping in another thread in case
2014     // the NON_MODAL argument was supplied, non persistent mappings
2015     // should return immediately, so we don't need to do that for them
2016     bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
2017 schoenebeck 973 MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
2018 schoenebeck 947 } catch (Exception e) {
2019     result.Error(e);
2020     }
2021     return result.Produce();
2022     }
2023    
2024 schoenebeck 973 String LSCPServer::RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2025 schoenebeck 947 dmsg(2,("LSCPServer: RemoveMIDIInstrumentMapping()\n"));
2026    
2027     midi_prog_index_t idx;
2028 schoenebeck 973 idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
2029     idx.midi_bank_lsb = MidiBank & 0x7f;
2030 schoenebeck 947 idx.midi_prog = MidiProg;
2031    
2032     LSCPResultSet result;
2033     try {
2034 schoenebeck 973 MidiInstrumentMapper::RemoveEntry(MidiMapID, idx);
2035 schoenebeck 947 } catch (Exception e) {
2036     result.Error(e);
2037     }
2038     return result.Produce();
2039     }
2040    
2041 schoenebeck 973 String LSCPServer::GetMidiInstrumentMappings(uint MidiMapID) {
2042     dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2043 schoenebeck 947 LSCPResultSet result;
2044 schoenebeck 973 try {
2045 iliev 1763 result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID));
2046 schoenebeck 973 } catch (Exception e) {
2047     result.Error(e);
2048     }
2049 schoenebeck 947 return result.Produce();
2050     }
2051    
2052 schoenebeck 973
2053     String LSCPServer::GetAllMidiInstrumentMappings() {
2054     dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2055     LSCPResultSet result;
2056 iliev 1763 try {
2057     result.Add(MidiInstrumentMapper::GetInstrumentCount());
2058     } catch (Exception e) {
2059     result.Error(e);
2060 schoenebeck 973 }
2061     return result.Produce();
2062     }
2063    
2064     String LSCPServer::GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2065 schoenebeck 947 dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2066     LSCPResultSet result;
2067     try {
2068 iliev 1763 MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg);
2069     // convert the filename into the correct encoding as defined for LSCP
2070     // (especially in terms of special characters -> escape sequences)
2071 schoenebeck 1471 #if WIN32
2072 iliev 1763 const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp();
2073 schoenebeck 1471 #else
2074 iliev 1763 // assuming POSIX
2075     const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp();
2076 schoenebeck 1471 #endif
2077    
2078 iliev 1763 result.Add("NAME", _escapeLscpResponse(entry.Name));
2079     result.Add("ENGINE_NAME", entry.EngineName);
2080     result.Add("INSTRUMENT_FILE", instrumentFileName);
2081     result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex);
2082     String instrumentName;
2083     Engine* pEngine = EngineFactory::Create(entry.EngineName);
2084     if (pEngine) {
2085     if (pEngine->GetInstrumentManager()) {
2086     InstrumentManager::instrument_id_t instrID;
2087     instrID.FileName = entry.InstrumentFile;
2088     instrID.Index = entry.InstrumentIndex;
2089     instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
2090 schoenebeck 947 }
2091 iliev 1763 EngineFactory::Destroy(pEngine);
2092 schoenebeck 947 }
2093 iliev 1763 result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2094     switch (entry.LoadMode) {
2095     case MidiInstrumentMapper::ON_DEMAND:
2096     result.Add("LOAD_MODE", "ON_DEMAND");
2097     break;
2098     case MidiInstrumentMapper::ON_DEMAND_HOLD:
2099     result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2100     break;
2101     case MidiInstrumentMapper::PERSISTENT:
2102     result.Add("LOAD_MODE", "PERSISTENT");
2103     break;
2104     default:
2105     throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2106     }
2107     result.Add("VOLUME", entry.Volume);
2108 schoenebeck 947 } catch (Exception e) {
2109     result.Error(e);
2110     }
2111     return result.Produce();
2112     }
2113    
2114 schoenebeck 973 String LSCPServer::ListMidiInstrumentMappings(uint MidiMapID) {
2115     dmsg(2,("LSCPServer: ListMidiInstrumentMappings()\n"));
2116 schoenebeck 947 LSCPResultSet result;
2117     try {
2118     String s;
2119 schoenebeck 973 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
2120 schoenebeck 947 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2121     for (; iter != mappings.end(); iter++) {
2122     if (s.size()) s += ",";
2123 schoenebeck 973 s += "{" + ToString(MidiMapID) + ","
2124 schoenebeck 1007 + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2125 schoenebeck 973 + ToString(int(iter->first.midi_prog)) + "}";
2126 schoenebeck 947 }
2127     result.Add(s);
2128     } catch (Exception e) {
2129     result.Error(e);
2130     }
2131     return result.Produce();
2132     }
2133    
2134 schoenebeck 973 String LSCPServer::ListAllMidiInstrumentMappings() {
2135     dmsg(2,("LSCPServer: ListAllMidiInstrumentMappings()\n"));
2136     LSCPResultSet result;
2137     try {
2138     std::vector<int> maps = MidiInstrumentMapper::Maps();
2139     String s;
2140     for (int i = 0; i < maps.size(); i++) {
2141     std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(maps[i]);
2142     std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2143     for (; iter != mappings.end(); iter++) {
2144     if (s.size()) s += ",";
2145     s += "{" + ToString(maps[i]) + ","
2146 schoenebeck 1009 + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2147 schoenebeck 973 + ToString(int(iter->first.midi_prog)) + "}";
2148     }
2149     }
2150     result.Add(s);
2151     } catch (Exception e) {
2152     result.Error(e);
2153     }
2154     return result.Produce();
2155     }
2156    
2157     String LSCPServer::ClearMidiInstrumentMappings(uint MidiMapID) {
2158 schoenebeck 947 dmsg(2,("LSCPServer: ClearMidiInstrumentMappings()\n"));
2159     LSCPResultSet result;
2160     try {
2161 schoenebeck 973 MidiInstrumentMapper::RemoveAllEntries(MidiMapID);
2162 schoenebeck 947 } catch (Exception e) {
2163     result.Error(e);
2164     }
2165     return result.Produce();
2166     }
2167    
2168 schoenebeck 973 String LSCPServer::ClearAllMidiInstrumentMappings() {
2169     dmsg(2,("LSCPServer: ClearAllMidiInstrumentMappings()\n"));
2170     LSCPResultSet result;
2171     try {
2172     std::vector<int> maps = MidiInstrumentMapper::Maps();
2173     for (int i = 0; i < maps.size(); i++)
2174     MidiInstrumentMapper::RemoveAllEntries(maps[i]);
2175     } catch (Exception e) {
2176     result.Error(e);
2177     }
2178     return result.Produce();
2179     }
2180    
2181     String LSCPServer::AddMidiInstrumentMap(String MapName) {
2182     dmsg(2,("LSCPServer: AddMidiInstrumentMap()\n"));
2183     LSCPResultSet result;
2184     try {
2185     int MapID = MidiInstrumentMapper::AddMap(MapName);
2186     result = LSCPResultSet(MapID);
2187     } catch (Exception e) {
2188     result.Error(e);
2189     }
2190     return result.Produce();
2191     }
2192    
2193     String LSCPServer::RemoveMidiInstrumentMap(uint MidiMapID) {
2194     dmsg(2,("LSCPServer: RemoveMidiInstrumentMap()\n"));
2195     LSCPResultSet result;
2196     try {
2197     MidiInstrumentMapper::RemoveMap(MidiMapID);
2198     } catch (Exception e) {
2199     result.Error(e);
2200     }
2201     return result.Produce();
2202     }
2203    
2204     String LSCPServer::RemoveAllMidiInstrumentMaps() {
2205     dmsg(2,("LSCPServer: RemoveAllMidiInstrumentMaps()\n"));
2206     LSCPResultSet result;
2207     try {
2208     MidiInstrumentMapper::RemoveAllMaps();
2209     } catch (Exception e) {
2210     result.Error(e);
2211     }
2212     return result.Produce();
2213     }
2214    
2215     String LSCPServer::GetMidiInstrumentMaps() {
2216     dmsg(2,("LSCPServer: GetMidiInstrumentMaps()\n"));
2217     LSCPResultSet result;
2218     try {
2219     result.Add(MidiInstrumentMapper::Maps().size());
2220     } catch (Exception e) {
2221     result.Error(e);
2222     }
2223     return result.Produce();
2224     }
2225    
2226     String LSCPServer::ListMidiInstrumentMaps() {
2227     dmsg(2,("LSCPServer: ListMidiInstrumentMaps()\n"));
2228     LSCPResultSet result;
2229     try {
2230     std::vector<int> maps = MidiInstrumentMapper::Maps();
2231     String sList;
2232     for (int i = 0; i < maps.size(); i++) {
2233     if (sList != "") sList += ",";
2234     sList += ToString(maps[i]);
2235     }
2236     result.Add(sList);
2237     } catch (Exception e) {
2238     result.Error(e);
2239     }
2240     return result.Produce();
2241     }
2242    
2243     String LSCPServer::GetMidiInstrumentMap(uint MidiMapID) {
2244     dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2245     LSCPResultSet result;
2246     try {
2247 schoenebeck 1399 result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2248 iliev 1135 result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2249 schoenebeck 973 } catch (Exception e) {
2250     result.Error(e);
2251     }
2252     return result.Produce();
2253     }
2254    
2255     String LSCPServer::SetMidiInstrumentMapName(uint MidiMapID, String NewName) {
2256     dmsg(2,("LSCPServer: SetMidiInstrumentMapName()\n"));
2257     LSCPResultSet result;
2258     try {
2259     MidiInstrumentMapper::RenameMap(MidiMapID, NewName);
2260     } catch (Exception e) {
2261     result.Error(e);
2262     }
2263     return result.Produce();
2264     }
2265    
2266 schoenebeck 705 /**
2267 schoenebeck 973 * Set the MIDI instrument map the given sampler channel shall use for
2268     * handling MIDI program change messages. There are the following two
2269     * special (negative) values:
2270     *
2271     * - (-1) : set to NONE (ignore program changes)
2272     * - (-2) : set to DEFAULT map
2273     */
2274     String LSCPServer::SetChannelMap(uint uiSamplerChannel, int MidiMapID) {
2275     dmsg(2,("LSCPServer: SetChannelMap()\n"));
2276     LSCPResultSet result;
2277     try {
2278 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2279 schoenebeck 973
2280     if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2281     else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
2282     else pEngineChannel->SetMidiInstrumentMap(MidiMapID);
2283     } catch (Exception e) {
2284     result.Error(e);
2285     }
2286     return result.Produce();
2287     }
2288    
2289 schoenebeck 1001 String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) {
2290     dmsg(2,("LSCPServer: CreateFxSend()\n"));
2291     LSCPResultSet result;
2292     try {
2293 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2294 schoenebeck 1252
2295 schoenebeck 1001 FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2296     if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2297    
2298     result = LSCPResultSet(pFxSend->Id()); // success
2299     } catch (Exception e) {
2300     result.Error(e);
2301     }
2302     return result.Produce();
2303     }
2304    
2305     String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) {
2306     dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2307     LSCPResultSet result;
2308     try {
2309 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2310 schoenebeck 1001
2311     FxSend* pFxSend = NULL;
2312     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2313     if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2314     pFxSend = pEngineChannel->GetFxSend(i);
2315     break;
2316     }
2317     }
2318     if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2319     pEngineChannel->RemoveFxSend(pFxSend);
2320     } catch (Exception e) {
2321     result.Error(e);
2322     }
2323     return result.Produce();
2324     }
2325    
2326     String LSCPServer::GetFxSends(uint uiSamplerChannel) {
2327     dmsg(2,("LSCPServer: GetFxSends()\n"));
2328     LSCPResultSet result;
2329     try {
2330 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2331 schoenebeck 1001
2332     result.Add(pEngineChannel->GetFxSendCount());
2333     } catch (Exception e) {
2334     result.Error(e);
2335     }
2336     return result.Produce();
2337     }
2338    
2339     String LSCPServer::ListFxSends(uint uiSamplerChannel) {
2340     dmsg(2,("LSCPServer: ListFxSends()\n"));
2341     LSCPResultSet result;
2342     String list;
2343     try {
2344 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2345 schoenebeck 1001
2346     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2347     FxSend* pFxSend = pEngineChannel->GetFxSend(i);
2348     if (list != "") list += ",";
2349     list += ToString(pFxSend->Id());
2350     }
2351     result.Add(list);
2352     } catch (Exception e) {
2353     result.Error(e);
2354     }
2355     return result.Produce();
2356     }
2357    
2358 iliev 1135 FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2359     EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2360    
2361     FxSend* pFxSend = NULL;
2362     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2363     if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2364     pFxSend = pEngineChannel->GetFxSend(i);
2365     break;
2366     }
2367     }
2368     if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2369     return pFxSend;
2370     }
2371    
2372 schoenebeck 1001 String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2373     dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2374     LSCPResultSet result;
2375     try {
2376 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2377     FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2378 schoenebeck 1252
2379 schoenebeck 1001 // gather audio routing informations
2380     String AudioRouting;
2381     for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
2382     if (AudioRouting != "") AudioRouting += ",";
2383     AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2384     }
2385    
2386     // success
2387 schoenebeck 1399 result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2388 schoenebeck 1026 result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2389     result.Add("LEVEL", ToString(pFxSend->Level()));
2390 schoenebeck 1001 result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2391     } catch (Exception e) {
2392     result.Error(e);
2393     }
2394     return result.Produce();
2395     }
2396    
2397 iliev 1135 String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2398     dmsg(2,("LSCPServer: SetFxSendName()\n"));
2399     LSCPResultSet result;
2400     try {
2401     FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2402    
2403     pFxSend->SetName(Name);
2404     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2405     } catch (Exception e) {
2406     result.Error(e);
2407     }
2408     return result.Produce();
2409     }
2410    
2411 schoenebeck 1001 String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2412     dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2413     LSCPResultSet result;
2414     try {
2415 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2416 schoenebeck 1001
2417     pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2418 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2419 schoenebeck 1001 } catch (Exception e) {
2420     result.Error(e);
2421     }
2422     return result.Produce();
2423     }
2424    
2425 schoenebeck 1026 String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2426     dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2427     LSCPResultSet result;
2428     try {
2429 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2430 schoenebeck 1026
2431     pFxSend->SetMidiController(MidiController);
2432 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2433 schoenebeck 1026 } catch (Exception e) {
2434     result.Error(e);
2435     }
2436     return result.Produce();
2437     }
2438    
2439     String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2440     dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2441     LSCPResultSet result;
2442     try {
2443 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2444 schoenebeck 1026
2445     pFxSend->SetLevel((float)dLevel);
2446 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2447 schoenebeck 1026 } catch (Exception e) {
2448     result.Error(e);
2449     }
2450     return result.Produce();
2451     }
2452    
2453 schoenebeck 1212 String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2454     dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2455     LSCPResultSet result;
2456     try {
2457 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2458 iliev 1337 if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2459 schoenebeck 1212 Engine* pEngine = pEngineChannel->GetEngine();
2460     InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2461     if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2462     InstrumentManager::instrument_id_t instrumentID;
2463     instrumentID.FileName = pEngineChannel->InstrumentFileName();
2464     instrumentID.Index = pEngineChannel->InstrumentIndex();
2465     pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2466     } catch (Exception e) {
2467     result.Error(e);
2468     }
2469     return result.Produce();
2470     }
2471    
2472 iliev 1771 String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) {
2473     dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2));
2474     LSCPResultSet result;
2475     try {
2476     EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2477    
2478     if (Arg1 > 127 || Arg2 > 127) {
2479     throw Exception("Invalid MIDI message");
2480     }
2481    
2482     VirtualMidiDevice* pMidiDevice = NULL;
2483     std::vector<EventHandler::midi_listener_entry>::iterator iter = eventHandler.channelMidiListeners.begin();
2484     for (; iter != eventHandler.channelMidiListeners.end(); ++iter) {
2485     if ((*iter).pEngineChannel == pEngineChannel) {
2486     pMidiDevice = (*iter).pMidiListener;
2487     break;
2488     }
2489     }
2490    
2491     if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device");
2492    
2493     if (MidiMsg == "NOTE_ON") {
2494 iliev 1774 pMidiDevice->SendNoteOnToDevice(Arg1, Arg2);
2495 iliev 1771 bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2);
2496     if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2497     } else if (MidiMsg == "NOTE_OFF") {
2498 iliev 1774 pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);
2499 iliev 1771 bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);
2500     if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2501     } else {
2502     throw Exception("Unknown MIDI message type: " + MidiMsg);
2503     }
2504     } catch (Exception e) {
2505     result.Error(e);
2506     }
2507     return result.Produce();
2508     }
2509    
2510 schoenebeck 973 /**
2511 schoenebeck 35 * Will be called by the parser to reset a particular sampler channel.
2512     */
2513 schoenebeck 53 String LSCPServer::ResetChannel(uint uiSamplerChannel) {
2514     dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
2515 senkov 120 LSCPResultSet result;
2516 schoenebeck 53 try {
2517 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2518 schoenebeck 670 pEngineChannel->Reset();
2519 schoenebeck 53 }
2520 schoenebeck 880 catch (Exception e) {
2521 senkov 120 result.Error(e);
2522 schoenebeck 53 }
2523 senkov 120 return result.Produce();
2524 schoenebeck 35 }
2525    
2526     /**
2527 schoenebeck 212 * Will be called by the parser to reset the whole sampler.
2528     */
2529     String LSCPServer::ResetSampler() {
2530     dmsg(2,("LSCPServer: ResetSampler()\n"));
2531     pSampler->Reset();
2532     LSCPResultSet result;
2533     return result.Produce();
2534     }
2535    
2536     /**
2537 schoenebeck 563 * Will be called by the parser to return general informations about this
2538     * sampler.
2539     */
2540     String LSCPServer::GetServerInfo() {
2541     dmsg(2,("LSCPServer: GetServerInfo()\n"));
2542 schoenebeck 1399 const std::string description =
2543     _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2544 schoenebeck 563 LSCPResultSet result;
2545 schoenebeck 1399 result.Add("DESCRIPTION", description);
2546 schoenebeck 570 result.Add("VERSION", VERSION);
2547 schoenebeck 947 result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2548 iliev 1161 #if HAVE_SQLITE3
2549     result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2550     #else
2551     result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2552     #endif
2553 schoenebeck 1252
2554 schoenebeck 563 return result.Produce();
2555     }
2556    
2557     /**
2558 iliev 1541 * Will be called by the parser to return the current number of all active streams.
2559     */
2560     String LSCPServer::GetTotalStreamCount() {
2561     dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2562     LSCPResultSet result;
2563     result.Add(pSampler->GetDiskStreamCount());
2564     return result.Produce();
2565     }
2566    
2567     /**
2568 iliev 778 * Will be called by the parser to return the current number of all active voices.
2569     */
2570     String LSCPServer::GetTotalVoiceCount() {
2571     dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
2572     LSCPResultSet result;
2573     result.Add(pSampler->GetVoiceCount());
2574     return result.Produce();
2575     }
2576    
2577     /**
2578     * Will be called by the parser to return the maximum number of voices.
2579     */
2580     String LSCPServer::GetTotalVoiceCountMax() {
2581     dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
2582     LSCPResultSet result;
2583 schoenebeck 1800 result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES);
2584 iliev 778 return result.Produce();
2585     }
2586    
2587 schoenebeck 1800 /**
2588     * Will be called by the parser to return the sampler global maximum
2589     * allowed number of voices.
2590     */
2591     String LSCPServer::GetGlobalMaxVoices() {
2592     dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));
2593     LSCPResultSet result;
2594     result.Add(GLOBAL_MAX_VOICES);
2595     return result.Produce();
2596     }
2597    
2598     /**
2599     * Will be called by the parser to set the sampler global maximum number of
2600     * voices.
2601     */
2602     String LSCPServer::SetGlobalMaxVoices(int iVoices) {
2603     dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));
2604     LSCPResultSet result;
2605     try {
2606     if (iVoices < 1) throw Exception("Maximum voices may not be less than 1");
2607     GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp
2608     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
2609     if (engines.size() > 0) {
2610     std::set<Engine*>::iterator iter = engines.begin();
2611     std::set<Engine*>::iterator end = engines.end();
2612     for (; iter != end; ++iter) {
2613     (*iter)->SetMaxVoices(iVoices);
2614     }
2615     }
2616     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES));
2617     } catch (Exception e) {
2618     result.Error(e);
2619     }
2620     return result.Produce();
2621     }
2622    
2623     /**
2624     * Will be called by the parser to return the sampler global maximum
2625     * allowed number of disk streams.
2626     */
2627     String LSCPServer::GetGlobalMaxStreams() {
2628     dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));
2629     LSCPResultSet result;
2630     result.Add(GLOBAL_MAX_STREAMS);
2631     return result.Produce();
2632     }
2633    
2634     /**
2635     * Will be called by the parser to set the sampler global maximum number of
2636     * disk streams.
2637     */
2638     String LSCPServer::SetGlobalMaxStreams(int iStreams) {
2639     dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));
2640     LSCPResultSet result;
2641     try {
2642     if (iStreams < 0) throw Exception("Maximum disk streams may not be negative");
2643     GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp
2644     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
2645     if (engines.size() > 0) {
2646     std::set<Engine*>::iterator iter = engines.begin();
2647     std::set<Engine*>::iterator end = engines.end();
2648     for (; iter != end; ++iter) {
2649     (*iter)->SetMaxDiskStreams(iStreams);
2650     }
2651     }
2652     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS));
2653     } catch (Exception e) {
2654     result.Error(e);
2655     }
2656     return result.Produce();
2657     }
2658    
2659 schoenebeck 1005 String LSCPServer::GetGlobalVolume() {
2660     LSCPResultSet result;
2661     result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp
2662     return result.Produce();
2663     }
2664    
2665     String LSCPServer::SetGlobalVolume(double dVolume) {
2666     LSCPResultSet result;
2667     try {
2668     if (dVolume < 0) throw Exception("Volume may not be negative");
2669 schoenebeck 1724 GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
2670 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
2671 schoenebeck 1005 } catch (Exception e) {
2672     result.Error(e);
2673     }
2674     return result.Produce();
2675     }
2676    
2677 schoenebeck 1525 String LSCPServer::GetFileInstruments(String Filename) {
2678 iliev 1535 dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2679 schoenebeck 1525 LSCPResultSet result;
2680 iliev 1535 try {
2681     VerifyFile(Filename);
2682     } catch (Exception e) {
2683     result.Error(e);
2684     return result.Produce();
2685     }
2686 schoenebeck 1525 // try to find a sampler engine that can handle the file
2687     bool bFound = false;
2688     std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2689     for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2690     Engine* pEngine = NULL;
2691     try {
2692     pEngine = EngineFactory::Create(engineTypes[i]);
2693     if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2694     InstrumentManager* pManager = pEngine->GetInstrumentManager();
2695     if (pManager) {
2696     std::vector<InstrumentManager::instrument_id_t> IDs =
2697     pManager->GetInstrumentFileContent(Filename);
2698     // return the amount of instruments in the file
2699     result.Add(IDs.size());
2700     // no more need to ask other engine types
2701     bFound = true;
2702     } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2703     } catch (Exception e) {
2704     // NOOP, as exception is thrown if engine doesn't support file
2705     }
2706     if (pEngine) EngineFactory::Destroy(pEngine);
2707     }
2708 iliev 1535
2709     if (!bFound) result.Error("Unknown file format");
2710 schoenebeck 1525 return result.Produce();
2711     }
2712    
2713     String LSCPServer::ListFileInstruments(String Filename) {
2714 iliev 1535 dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2715 schoenebeck 1525 LSCPResultSet result;
2716 iliev 1535 try {
2717     VerifyFile(Filename);
2718     } catch (Exception e) {
2719     result.Error(e);
2720     return result.Produce();
2721     }
2722 schoenebeck 1525 // try to find a sampler engine that can handle the file
2723     bool bFound = false;
2724     std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2725     for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2726     Engine* pEngine = NULL;
2727     try {
2728     pEngine = EngineFactory::Create(engineTypes[i]);
2729     if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2730     InstrumentManager* pManager = pEngine->GetInstrumentManager();
2731     if (pManager) {
2732     std::vector<InstrumentManager::instrument_id_t> IDs =
2733     pManager->GetInstrumentFileContent(Filename);
2734     // return a list of IDs of the instruments in the file
2735     String s;
2736     for (int j = 0; j < IDs.size(); j++) {
2737     if (s.size()) s += ",";
2738     s += ToString(IDs[j].Index);
2739     }
2740     result.Add(s);
2741     // no more need to ask other engine types
2742     bFound = true;
2743     } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2744     } catch (Exception e) {
2745     // NOOP, as exception is thrown if engine doesn't support file
2746     }
2747     if (pEngine) EngineFactory::Destroy(pEngine);
2748     }
2749 iliev 1535
2750     if (!bFound) result.Error("Unknown file format");
2751 schoenebeck 1525 return result.Produce();
2752     }
2753    
2754     String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2755 iliev 1535 dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2756 schoenebeck 1525 LSCPResultSet result;
2757 iliev 1535 try {
2758     VerifyFile(Filename);
2759     } catch (Exception e) {
2760     result.Error(e);
2761     return result.Produce();
2762     }
2763 schoenebeck 1525 InstrumentManager::instrument_id_t id;
2764     id.FileName = Filename;
2765     id.Index = InstrumentID;
2766     // try to find a sampler engine that can handle the file
2767     bool bFound = false;
2768 schoenebeck 1536 bool bFatalErr = false;
2769 schoenebeck 1525 std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2770 schoenebeck 1536 for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
2771 schoenebeck 1525 Engine* pEngine = NULL;
2772     try {
2773     pEngine = EngineFactory::Create(engineTypes[i]);
2774     if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2775     InstrumentManager* pManager = pEngine->GetInstrumentManager();
2776     if (pManager) {
2777 schoenebeck 1536 // check if the instrument index is valid
2778     // FIXME: this won't work if an engine only supports parts of the instrument file
2779     std::vector<InstrumentManager::instrument_id_t> IDs =
2780     pManager->GetInstrumentFileContent(Filename);
2781     if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2782     std::stringstream ss;
2783     ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2784     bFatalErr = true;
2785     throw Exception(ss.str());
2786     }
2787     // get the info of the requested instrument
2788 schoenebeck 1525 InstrumentManager::instrument_info_t info =
2789     pManager->GetInstrumentInfo(id);
2790     // return detailed informations about the file
2791     result.Add("NAME", info.InstrumentName);
2792 iliev 1535 result.Add("FORMAT_FAMILY", engineTypes[i]);
2793 schoenebeck 1525 result.Add("FORMAT_VERSION", info.FormatVersion);
2794     result.Add("PRODUCT", info.Product);
2795     result.Add("ARTISTS", info.Artists);
2796 iliev 1771
2797     std::stringstream ss;
2798     bool b = false;
2799     for (int i = 0; i < 128; i++) {
2800     if (info.KeyBindings[i]) {
2801     if (b) ss << ',';
2802     ss << i; b = true;
2803     }
2804     }
2805     result.Add("KEY_BINDINGS", ss.str());
2806    
2807 iliev 1774 b = false;
2808 iliev 1771 std::stringstream ss2;
2809     for (int i = 0; i < 128; i++) {
2810     if (info.KeySwitchBindings[i]) {
2811     if (b) ss2 << ',';
2812     ss2 << i; b = true;
2813     }
2814     }
2815     result.Add("KEYSWITCH_BINDINGS", ss2.str());
2816 schoenebeck 1525 // no more need to ask other engine types
2817     bFound = true;
2818     } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2819     } catch (Exception e) {
2820 schoenebeck 1536 // usually NOOP, as exception is thrown if engine doesn't support file
2821     if (bFatalErr) result.Error(e);
2822 schoenebeck 1525 }
2823     if (pEngine) EngineFactory::Destroy(pEngine);
2824     }
2825 iliev 1535
2826 schoenebeck 1536 if (!bFound && !bFatalErr) result.Error("Unknown file format");
2827 schoenebeck 1525 return result.Produce();
2828     }
2829    
2830 iliev 1535 void LSCPServer::VerifyFile(String Filename) {
2831 senoner 1537 #if WIN32
2832     WIN32_FIND_DATA win32FileAttributeData;
2833     BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2834     if (!res) {
2835     std::stringstream ss;
2836     ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2837     throw Exception(ss.str());
2838     }
2839     if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2840     throw Exception("Directory is specified");
2841     }
2842 schoenebeck 1551 #else
2843 iliev 1835 File f(Filename);
2844     if(!f.Exist()) throw Exception(f.GetErrorMsg());
2845     if (f.IsDirectory()) throw Exception("Directory is specified");
2846 senoner 1537 #endif
2847 iliev 1535 }
2848    
2849 iliev 778 /**
2850 schoenebeck 35 * Will be called by the parser to subscribe a client (frontend) on the
2851     * server for receiving event messages.
2852     */
2853 senkov 170 String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
2854     dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
2855     LSCPResultSet result;
2856     SubscriptionMutex.Lock();
2857     eventSubscriptions[type].push_back(currentSocket);
2858     SubscriptionMutex.Unlock();
2859     return result.Produce();
2860 schoenebeck 35 }
2861    
2862     /**
2863     * Will be called by the parser to unsubscribe a client on the server
2864     * for not receiving further event messages.
2865     */
2866 senkov 170 String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
2867     dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
2868     LSCPResultSet result;
2869     SubscriptionMutex.Lock();
2870     eventSubscriptions[type].remove(currentSocket);
2871     SubscriptionMutex.Unlock();
2872     return result.Produce();
2873 schoenebeck 35 }
2874 capela 133
2875 iliev 1161 String LSCPServer::AddDbInstrumentDirectory(String Dir) {
2876     dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
2877     LSCPResultSet result;
2878     #if HAVE_SQLITE3
2879     try {
2880     InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
2881     } catch (Exception e) {
2882     result.Error(e);
2883     }
2884     #else
2885     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2886     #endif
2887     return result.Produce();
2888 senkov 397 }
2889    
2890 iliev 1161 String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
2891     dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
2892 senkov 397 LSCPResultSet result;
2893 schoenebeck 401 #if HAVE_SQLITE3
2894 iliev 1161 try {
2895     InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
2896     } catch (Exception e) {
2897     result.Error(e);
2898     }
2899     #else
2900     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2901     #endif
2902     return result.Produce();
2903     }
2904 senkov 397
2905 iliev 1187 String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
2906     dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2907 iliev 1161 LSCPResultSet result;
2908     #if HAVE_SQLITE3
2909     try {
2910 iliev 1187 result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
2911 iliev 1161 } catch (Exception e) {
2912     result.Error(e);
2913 senkov 397 }
2914 iliev 1161 #else
2915     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2916     #endif
2917     return result.Produce();
2918     }
2919    
2920 iliev 1187 String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
2921     dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2922 iliev 1161 LSCPResultSet result;
2923     #if HAVE_SQLITE3
2924     try {
2925     String list;
2926 iliev 1187 StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
2927 iliev 1161
2928     for (int i = 0; i < dirs->size(); i++) {
2929     if (list != "") list += ",";
2930 iliev 1350 list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
2931 iliev 1161 }
2932    
2933     result.Add(list);
2934     } catch (Exception e) {
2935     result.Error(e);
2936 senkov 397 }
2937     #else
2938 iliev 1161 result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2939 senkov 397 #endif
2940     return result.Produce();
2941     }
2942    
2943 iliev 1161 String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
2944     dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
2945     LSCPResultSet result;
2946     #if HAVE_SQLITE3
2947     try {
2948     DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2949    
2950 iliev 1403 result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2951 iliev 1161 result.Add("CREATED", info.Created);
2952     result.Add("MODIFIED", info.Modified);
2953     } catch (Exception e) {
2954     result.Error(e);
2955     }
2956     #else
2957     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2958     #endif
2959     return result.Produce();
2960     }
2961    
2962     String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
2963     dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
2964     LSCPResultSet result;
2965     #if HAVE_SQLITE3
2966     try {
2967     InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
2968     } catch (Exception e) {
2969     result.Error(e);
2970     }
2971     #else
2972     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2973     #endif
2974     return result.Produce();
2975     }
2976    
2977     String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
2978     dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2979     LSCPResultSet result;
2980     #if HAVE_SQLITE3
2981     try {
2982     InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
2983     } catch (Exception e) {
2984     result.Error(e);
2985     }
2986     #else
2987     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2988     #endif
2989     return result.Produce();
2990     }
2991    
2992 iliev 1187 String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
2993     dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2994     LSCPResultSet result;
2995     #if HAVE_SQLITE3
2996     try {
2997     InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
2998     } catch (Exception e) {
2999     result.Error(e);
3000     }
3001     #else
3002     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3003     #endif
3004     return result.Produce();
3005     }
3006    
3007 iliev 1161 String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
3008     dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
3009     LSCPResultSet result;
3010     #if HAVE_SQLITE3
3011     try {
3012     InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
3013     } catch (Exception e) {
3014     result.Error(e);
3015     }
3016     #else
3017     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3018     #endif
3019     return result.Produce();
3020     }
3021    
3022 iliev 1200 String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
3023     dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
3024 iliev 1161 LSCPResultSet result;
3025     #if HAVE_SQLITE3
3026     try {
3027 iliev 1200 int id;
3028     InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3029     id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
3030     if (bBackground) result = id;
3031 iliev 1161 } catch (Exception e) {
3032     result.Error(e);
3033     }
3034     #else
3035     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3036     #endif
3037     return result.Produce();
3038     }
3039    
3040 iliev 1781 String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) {
3041     dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir));
3042 iliev 1161 LSCPResultSet result;
3043     #if HAVE_SQLITE3
3044     try {
3045 iliev 1200 int id;
3046     InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3047     if (ScanMode.compare("RECURSIVE") == 0) {
3048 iliev 1781 id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir);
3049 iliev 1200 } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
3050 iliev 1781 id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir);
3051 iliev 1200 } else if (ScanMode.compare("FLAT") == 0) {
3052 iliev 1781 id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir);
3053 iliev 1200 } else {
3054     throw Exception("Unknown scan mode: " + ScanMode);
3055     }
3056 schoenebeck 1252
3057 iliev 1200 if (bBackground) result = id;
3058 iliev 1161 } catch (Exception e) {
3059     result.Error(e);
3060     }
3061     #else
3062     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3063     #endif
3064     return result.Produce();
3065     }
3066    
3067     String LSCPServer::RemoveDbInstrument(String Instr) {
3068     dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
3069     LSCPResultSet result;
3070     #if HAVE_SQLITE3
3071     try {
3072     InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
3073     } catch (Exception e) {
3074     result.Error(e);
3075     }
3076     #else
3077     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3078     #endif
3079     return result.Produce();
3080     }
3081    
3082 iliev 1187 String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
3083     dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3084 iliev 1161 LSCPResultSet result;
3085     #if HAVE_SQLITE3
3086     try {
3087 iliev 1187 result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
3088 iliev 1161 } catch (Exception e) {
3089     result.Error(e);
3090     }
3091     #else
3092     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3093     #endif
3094     return result.Produce();
3095     }
3096    
3097 iliev 1187 String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
3098     dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3099 iliev 1161 LSCPResultSet result;
3100     #if HAVE_SQLITE3
3101     try {
3102     String list;
3103 iliev 1187 StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
3104 iliev 1161
3105     for (int i = 0; i < instrs->size(); i++) {
3106     if (list != "") list += ",";
3107 iliev 1350 list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
3108 iliev 1161 }
3109    
3110     result.Add(list);
3111     } catch (Exception e) {
3112     result.Error(e);
3113     }
3114     #else
3115     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3116     #endif
3117     return result.Produce();
3118     }
3119    
3120     String LSCPServer::GetDbInstrumentInfo(String Instr) {
3121     dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
3122     LSCPResultSet result;
3123     #if HAVE_SQLITE3
3124     try {
3125     DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
3126    
3127     result.Add("INSTRUMENT_FILE", info.InstrFile);
3128     result.Add("INSTRUMENT_NR", info.InstrNr);
3129     result.Add("FORMAT_FAMILY", info.FormatFamily);
3130     result.Add("FORMAT_VERSION", info.FormatVersion);
3131     result.Add("SIZE", (int)info.Size);
3132     result.Add("CREATED", info.Created);
3133     result.Add("MODIFIED", info.Modified);
3134 iliev 1403 result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3135 iliev 1161 result.Add("IS_DRUM", info.IsDrum);
3136 iliev 1403 result.Add("PRODUCT", _escapeLscpResponse(info.Product));
3137     result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
3138     result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
3139 iliev 1161 } catch (Exception e) {
3140     result.Error(e);
3141     }
3142     #else
3143     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3144     #endif
3145     return result.Produce();
3146     }
3147    
3148 iliev 1200 String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
3149     dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
3150     LSCPResultSet result;
3151     #if HAVE_SQLITE3
3152     try {
3153     ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
3154    
3155     result.Add("FILES_TOTAL", job.FilesTotal);
3156     result.Add("FILES_SCANNED", job.FilesScanned);
3157     result.Add("SCANNING", job.Scanning);
3158     result.Add("STATUS", job.Status);
3159     } catch (Exception e) {
3160     result.Error(e);
3161     }
3162     #else
3163     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3164     #endif
3165     return result.Produce();
3166     }
3167    
3168 iliev 1161 String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
3169     dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
3170     LSCPResultSet result;
3171     #if HAVE_SQLITE3
3172     try {
3173     InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
3174     } catch (Exception e) {
3175     result.Error(e);
3176     }
3177     #else
3178     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3179     #endif
3180     return result.Produce();
3181     }
3182    
3183     String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
3184     dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3185     LSCPResultSet result;
3186     #if HAVE_SQLITE3
3187     try {
3188     InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
3189     } catch (Exception e) {
3190     result.Error(e);
3191     }
3192     #else
3193     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3194     #endif
3195     return result.Produce();
3196     }
3197    
3198 iliev 1187 String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
3199     dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3200     LSCPResultSet result;
3201     #if HAVE_SQLITE3
3202     try {
3203     InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
3204     } catch (Exception e) {
3205     result.Error(e);
3206     }
3207     #else
3208     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3209     #endif
3210     return result.Produce();
3211     }
3212    
3213 iliev 1161 String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
3214     dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
3215     LSCPResultSet result;
3216     #if HAVE_SQLITE3
3217     try {
3218     InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
3219     } catch (Exception e) {
3220     result.Error(e);
3221     }
3222     #else
3223     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3224     #endif
3225     return result.Produce();
3226     }
3227    
3228 iliev 1727 String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) {
3229     dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str()));
3230     LSCPResultSet result;
3231     #if HAVE_SQLITE3
3232     try {
3233     InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath);
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     String LSCPServer::FindLostDbInstrumentFiles() {
3244     dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n"));
3245     LSCPResultSet result;
3246     #if HAVE_SQLITE3
3247     try {
3248     String list;
3249     StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles();
3250    
3251     for (int i = 0; i < pLostFiles->size(); i++) {
3252     if (list != "") list += ",";
3253     list += "'" + pLostFiles->at(i) + "'";
3254     }
3255    
3256     result.Add(list);
3257     } catch (Exception e) {
3258     result.Error(e);
3259     }
3260     #else
3261     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3262     #endif
3263     return result.Produce();
3264     }
3265    
3266 iliev 1187 String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
3267     dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
3268     LSCPResultSet result;
3269     #if HAVE_SQLITE3
3270     try {
3271     SearchQuery Query;
3272     std::map<String,String>::iterator iter;
3273     for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3274     if (iter->first.compare("NAME") == 0) {
3275     Query.Name = iter->second;
3276     } else if (iter->first.compare("CREATED") == 0) {
3277     Query.SetCreated(iter->second);
3278     } else if (iter->first.compare("MODIFIED") == 0) {
3279     Query.SetModified(iter->second);
3280     } else if (iter->first.compare("DESCRIPTION") == 0) {
3281     Query.Description = iter->second;
3282     } else {
3283     throw Exception("Unknown search criteria: " + iter->first);
3284     }
3285     }
3286 iliev 1161
3287 iliev 1187 String list;
3288     StringListPtr pDirectories =
3289     InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
3290    
3291     for (int i = 0; i < pDirectories->size(); i++) {
3292     if (list != "") list += ",";
3293 iliev 1345 list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
3294 iliev 1187 }
3295    
3296     result.Add(list);
3297     } catch (Exception e) {
3298     result.Error(e);
3299     }
3300     #else
3301     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3302     #endif
3303     return result.Produce();
3304     }
3305    
3306     String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
3307     dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
3308     LSCPResultSet result;
3309     #if HAVE_SQLITE3
3310     try {
3311     SearchQuery Query;
3312     std::map<String,String>::iterator iter;
3313     for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3314     if (iter->first.compare("NAME") == 0) {
3315     Query.Name = iter->second;
3316     } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
3317     Query.SetFormatFamilies(iter->second);
3318     } else if (iter->first.compare("SIZE") == 0) {
3319     Query.SetSize(iter->second);
3320     } else if (iter->first.compare("CREATED") == 0) {
3321     Query.SetCreated(iter->second);
3322     } else if (iter->first.compare("MODIFIED") == 0) {
3323     Query.SetModified(iter->second);
3324     } else if (iter->first.compare("DESCRIPTION") == 0) {
3325     Query.Description = iter->second;
3326     } else if (iter->first.compare("IS_DRUM") == 0) {
3327     if (!strcasecmp(iter->second.c_str(), "true")) {
3328     Query.InstrType = SearchQuery::DRUM;
3329     } else {
3330     Query.InstrType = SearchQuery::CHROMATIC;
3331     }
3332     } else if (iter->first.compare("PRODUCT") == 0) {
3333     Query.Product = iter->second;
3334     } else if (iter->first.compare("ARTISTS") == 0) {
3335     Query.Artists = iter->second;
3336     } else if (iter->first.compare("KEYWORDS") == 0) {
3337     Query.Keywords = iter->second;
3338     } else {
3339     throw Exception("Unknown search criteria: " + iter->first);
3340     }
3341     }
3342    
3343     String list;
3344     StringListPtr pInstruments =
3345     InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
3346    
3347     for (int i = 0; i < pInstruments->size(); i++) {
3348     if (list != "") list += ",";
3349 iliev 1345 list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
3350 iliev 1187 }
3351    
3352     result.Add(list);
3353     } catch (Exception e) {
3354     result.Error(e);
3355     }
3356     #else
3357     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3358     #endif
3359     return result.Produce();
3360     }
3361    
3362 iliev 1353 String LSCPServer::FormatInstrumentsDb() {
3363     dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
3364     LSCPResultSet result;
3365     #if HAVE_SQLITE3
3366     try {
3367     InstrumentsDb::GetInstrumentsDb()->Format();
3368     } catch (Exception e) {
3369     result.Error(e);
3370     }
3371     #else
3372     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3373     #endif
3374     return result.Produce();
3375     }
3376 iliev 1187
3377 iliev 1353
3378 schoenebeck 210 /**
3379     * Will be called by the parser to enable or disable echo mode; if echo
3380     * mode is enabled, all commands from the client will (immediately) be
3381     * echoed back to the client.
3382     */
3383     String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
3384     dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
3385     LSCPResultSet result;
3386     try {
3387     if (boolean_value == 0) pSession->bVerbose = false;
3388     else if (boolean_value == 1) pSession->bVerbose = true;
3389 schoenebeck 880 else throw Exception("Not a boolean value, must either be 0 or 1");
3390 schoenebeck 210 }
3391 schoenebeck 880 catch (Exception e) {
3392 schoenebeck 210 result.Error(e);
3393     }
3394     return result.Produce();
3395     }
3396 persson 1765
3397     }

  ViewVC Help
Powered by ViewVC