/[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 1551 - (hide annotations) (download)
Wed Dec 5 22:05:28 2007 UTC (16 years, 4 months ago) by schoenebeck
File size: 122381 byte(s)
- seems Windows has problems with accessing static variables from
  a static libtool archive

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

  ViewVC Help
Powered by ViewVC