/[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 1765 - (hide annotations) (download)
Sat Sep 6 16:44:42 2008 UTC (15 years, 7 months ago) by persson
File size: 129061 byte(s)
* refactoring: extracted lscp notification from main() to a separate
  function
* added helper function MidiInputPort::DispatchRaw for midi device
  implementations with raw byte data
* fixed a win32 build error (atomic.h is working on windows too)
* code cleanup: moved lscp classes into LinuxSampler namespace

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

  ViewVC Help
Powered by ViewVC