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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 198 by senkov, Tue Jul 13 15:36:16 2004 UTC revision 1471 by schoenebeck, Mon Nov 5 13:56:26 2007 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6     *   Copyright (C) 2005 - 2007 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
10   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 2 of the License, or     *
11   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
12   *                                                                         *   *                                                                         *
13   *   This program is distributed in the hope that it will be useful,       *   *   This library is distributed in the hope that it will be useful,       *
14   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16   *   GNU General Public License for more details.                          *   *   GNU General Public License for more details.                          *
17   *                                                                         *   *                                                                         *
18   *   You should have received a copy of the GNU General Public License     *   *   You should have received a copy of the GNU General Public License     *
19   *   along with this program; if not, write to the Free Software           *   *   along with this library; if not, write to the Free Software           *
20   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
# Line 24  Line 25 
25  #include "lscpresultset.h"  #include "lscpresultset.h"
26  #include "lscpevent.h"  #include "lscpevent.h"
27    
28  #include "../engines/gig/Engine.h"  #include <fcntl.h>
29  #include "../audiodriver/AudioOutputDeviceFactory.h"  
30  #include "../mididriver/MidiInputDeviceFactory.h"  #if ! HAVE_SQLITE3
31    #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
32    #endif
33    
34    #include "../engines/EngineFactory.h"
35    #include "../engines/EngineChannelFactory.h"
36    #include "../drivers/audio/AudioOutputDeviceFactory.h"
37    #include "../drivers/midi/MidiInputDeviceFactory.h"
38    
39    
40    /**
41     * Returns a copy of the given string where all special characters are
42     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
43     * to escape LSCP response fields in case the respective response field is
44     * actually defined as using escape sequences in the LSCP specs.
45     *
46     * @e Caution: DO NOT use this function for escaping path based responses,
47     * use the Path class (src/common/Path.h) for this instead!
48     */
49    static String _escapeLscpResponse(String txt) {
50        for (int i = 0; i < txt.length(); i++) {
51            const char c = txt.c_str()[i];
52            if (
53                !(c >= '0' && c <= '9') &&
54                !(c >= 'a' && c <= 'z') &&
55                !(c >= 'A' && c <= 'Z') &&
56                !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
57                !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
58                !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
59                !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
60                !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
61                !(c == '@') && !(c == '[') && !(c == ']') &&
62                !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
63                !(c == '|') && !(c == '}') && !(c == '~')
64            ) {
65                // convert the "special" character into a "\xHH" LSCP escape sequence
66                char buf[5];
67                snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
68                txt.replace(i, 1, buf);
69                i += 3;
70            }
71        }
72        return txt;
73    }
74    
75  /**  /**
76   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
# Line 43  Line 87 
87   */   */
88  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
89  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
90  std::vector<int> LSCPServer::hSessions = std::vector<int>();  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
91    std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
92  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
93  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
94  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
95  Mutex LSCPServer::NotifyMutex = Mutex();  Mutex LSCPServer::NotifyMutex = Mutex();
96  Mutex LSCPServer::NotifyBufferMutex = Mutex();  Mutex LSCPServer::NotifyBufferMutex = Mutex();
97  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
98    Mutex LSCPServer::RTNotifyMutex = Mutex();
99    
100  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) {
101        SocketAddress.sin_family      = AF_INET;
102        SocketAddress.sin_addr.s_addr = addr;
103        SocketAddress.sin_port        = port;
104      this->pSampler = pSampler;      this->pSampler = pSampler;
105      LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS");      LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_count, "AUDIO_OUTPUT_DEVICE_COUNT");
106        LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_info, "AUDIO_OUTPUT_DEVICE_INFO");
107        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_count, "MIDI_INPUT_DEVICE_COUNT");
108        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_info, "MIDI_INPUT_DEVICE_INFO");
109        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
110      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
111      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
112      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
113      LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
114        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
115        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
116        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
117        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
118        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
119        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
120        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
121        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
122        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
123        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
124        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
125      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
126        LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
127        LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
128        hSocket = -1;
129    }
130    
131    LSCPServer::~LSCPServer() {
132        if (hSocket >= 0) close(hSocket);
133    }
134    
135    void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
136        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
137    }
138    
139    void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
140        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
141    }
142    
143    void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
144        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
145    }
146    
147    void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
148        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
149    }
150    
151    void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
152        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
153    }
154    
155    void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
156        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
157    }
158    
159    void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
160        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
161    }
162    
163    void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
164        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
165    }
166    
167    void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
168        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
169    }
170    
171    void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
172        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
173    }
174    
175    void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
176        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
177    }
178    
179    void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
180        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
181    }
182    
183    #if HAVE_SQLITE3
184    void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
185        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
186    }
187    
188    void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
189        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
190    }
191    
192    void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
193        Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
194        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
195        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
196    }
197    
198    void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
199        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
200    }
201    
202    void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
203        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
204    }
205    
206    void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
207        Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
208        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
209        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
210    }
211    
212    void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
213        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
214    }
215    #endif // HAVE_SQLITE3
216    
217    
218    /**
219     * Blocks the calling thread until the LSCP Server is initialized and
220     * accepting socket connections, if the server is already initialized then
221     * this method will return immediately.
222     * @param TimeoutSeconds     - optional: max. wait time in seconds
223     *                             (default: 0s)
224     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
225     *                             (default: 0ns)
226     * @returns  0 on success, a value less than 0 if timeout exceeded
227     */
228    int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
229        return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
230  }  }
231    
232  int LSCPServer::Main() {  int LSCPServer::Main() {
233      int hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
234      if (hSocket < 0) {      if (hSocket < 0) {
235          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
236          //return -1;          //return -1;
237          exit(EXIT_FAILURE);          exit(EXIT_FAILURE);
238      }      }
239    
     SocketAddress.sin_family      = AF_INET;  
     SocketAddress.sin_port        = htons(LSCP_PORT);  
     SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);  
   
240      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
241          std::cerr << "LSCPServer: Could not bind server socket." << std::endl;          std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
242          close(hSocket);          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
243          //return -1;              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
244          exit(EXIT_FAILURE);                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
245                        std::cerr << "gave up!" << std::endl;
246                        close(hSocket);
247                        //return -1;
248                        exit(EXIT_FAILURE);
249                    }
250                    else sleep(1); // sleep 1s
251                }
252                else break; // success
253            }
254      }      }
255    
256      listen(hSocket, 1);      listen(hSocket, 1);
257      dmsg(1,("LSCPServer: Server running.\n")); // server running      Initialized.Set(true);
258    
259        // Registering event listeners
260        pSampler->AddChannelCountListener(&eventHandler);
261        pSampler->AddAudioDeviceCountListener(&eventHandler);
262        pSampler->AddMidiDeviceCountListener(&eventHandler);
263        pSampler->AddVoiceCountListener(&eventHandler);
264        pSampler->AddStreamCountListener(&eventHandler);
265        pSampler->AddBufferFillListener(&eventHandler);
266        pSampler->AddTotalVoiceCountListener(&eventHandler);
267        pSampler->AddFxSendCountListener(&eventHandler);
268        MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
269        MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
270        MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
271        MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
272    #if HAVE_SQLITE3
273        InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
274    #endif
275      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
276      sockaddr_in client;      sockaddr_in client;
277      int length = sizeof(client);      int length = sizeof(client);
278      FD_ZERO(&fdSet);      FD_ZERO(&fdSet);
279      FD_SET(hSocket, &fdSet);      FD_SET(hSocket, &fdSet);
280      int maxSessions = hSocket;      int maxSessions = hSocket;
281    
282      // Parser initialization      timeval timeout;
     yyparse_param_t yyparse_param;  
     yyparse_param.pServer = this;  
283    
284      while (true) {      while (true) {
285          fd_set selectSet = fdSet;          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
286          int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);          {
287                std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
288                std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
289                std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
290                for (; itEngineChannel != itEnd; ++itEngineChannel) {
291                    if ((*itEngineChannel)->StatusChanged()) {
292                        SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));
293                    }
294    
295                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
296                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
297                        if(fxs != NULL && fxs->IsInfoChanged()) {
298                            int chn = (*itEngineChannel)->iSamplerChannelIndex;
299                            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
300                            fxs->SetInfoChanged(false);
301                        }
302                    }
303                }
304            }
305    
306            //Now let's deliver late notifies (if any)
307            NotifyBufferMutex.Lock();
308            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
309    #ifdef MSG_NOSIGNAL
310                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
311    #else
312                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
313    #endif
314            }
315            bufferedNotifies.clear();
316            NotifyBufferMutex.Unlock();
317    
318            fd_set selectSet = fdSet;
319            timeout.tv_sec  = 0;
320            timeout.tv_usec = 100000;
321    
322            int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
323    
324          if (retval == 0)          if (retval == 0)
325                  continue; //Nothing try again                  continue; //Nothing try again
326          if (retval == -1) {          if (retval == -1) {
# Line 104  int LSCPServer::Main() { Line 328  int LSCPServer::Main() {
328                  close(hSocket);                  close(hSocket);
329                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
330          }          }
331            
332          //Accept new connections now (if any)          //Accept new connections now (if any)
333          if (FD_ISSET(hSocket, &selectSet)) {          if (FD_ISSET(hSocket, &selectSet)) {
334                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
# Line 118  int LSCPServer::Main() { Line 342  int LSCPServer::Main() {
342                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
343                  }                  }
344    
345                  hSessions.push_back(socket);                  // Parser initialization
346                    yyparse_param_t yyparse_param;
347                    yyparse_param.pServer  = this;
348                    yyparse_param.hSession = socket;
349    
350                    Sessions.push_back(yyparse_param);
351                  FD_SET(socket, &fdSet);                  FD_SET(socket, &fdSet);
352                  if (socket > maxSessions)                  if (socket > maxSessions)
353                          maxSessions = socket;                          maxSessions = socket;
# Line 128  int LSCPServer::Main() { Line 357  int LSCPServer::Main() {
357          }          }
358    
359          //Something was selected and it was not the hSocket, so it must be some command(s) coming.          //Something was selected and it was not the hSocket, so it must be some command(s) coming.
360          for (std::vector<int>::iterator iter = hSessions.begin(); iter !=  hSessions.end(); iter++) {          for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
361                  if (FD_ISSET(*iter, &selectSet)) {      //Was it this socket?                  if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?
362                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?
363                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
364                                  yylex_init(&yyparse_param.pScanner);                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
365                                  currentSocket = *iter;  //a hack                                  restart(NULL, dummy); // restart the 'scanner'
366                                  int result = yyparse(&yyparse_param);                                  currentSocket = (*iter).hSession;  //a hack
367                                    itCurrentSession = iter; // another hack
368                                    dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
369                                    if ((*iter).bVerbose) { // if echo mode enabled
370                                        AnswerClient(bufferedCommands[currentSocket]);
371                                    }
372                                    int result = yyparse(&(*iter));
373                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
374                                    itCurrentSession = Sessions.end(); // hack as well
375                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
376                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?
377                                          CloseConnection(iter);                                          CloseConnection(iter);
# Line 143  int LSCPServer::Main() { Line 379  int LSCPServer::Main() {
379                          }                          }
380                          //socket may have been closed, iter may be invalid, get out of the loop for now.                          //socket may have been closed, iter may be invalid, get out of the loop for now.
381                          //we'll be back if there is data.                          //we'll be back if there is data.
382                          break;                          break;
383                  }                  }
384          }          }
   
         //Now let's deliver late notifies (if any)  
         NotifyBufferMutex.Lock();  
         for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {  
                 send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);  
                 bufferedNotifies.erase(iterNotify);  
         }  
         NotifyBufferMutex.Unlock();  
385      }      }
     //It will never get here anyway  
     //yylex_destroy(yyparse_param.pScanner);  
386  }  }
387    
388  void LSCPServer::CloseConnection( std::vector<int>::iterator iter ) {  void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
389          int socket = *iter;          int socket = (*iter).hSession;
390          dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));          dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
391          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
392          hSessions.erase(iter);          Sessions.erase(iter);
393          FD_CLR(socket,  &fdSet);          FD_CLR(socket,  &fdSet);
394          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
395          for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {          for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
# Line 177  void LSCPServer::CloseConnection( std::v Line 403  void LSCPServer::CloseConnection( std::v
403          NotifyMutex.Unlock();          NotifyMutex.Unlock();
404  }  }
405    
406    int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
407            int subs = 0;
408            SubscriptionMutex.Lock();
409            for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
410                            iter != events.end(); iter++)
411            {
412                    subs += eventSubscriptions.count(*iter);
413            }
414            SubscriptionMutex.Unlock();
415            return subs;
416    }
417    
418  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
419          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
420          if (eventSubscriptions.count(event.GetType()) == 0) {          if (eventSubscriptions.count(event.GetType()) == 0) {
# Line 190  void LSCPServer::SendLSCPNotify( LSCPEve Line 428  void LSCPServer::SendLSCPNotify( LSCPEve
428          while (true) {          while (true) {
429                  if (NotifyMutex.Trylock()) {                  if (NotifyMutex.Trylock()) {
430                          for(;iter != end; iter++)                          for(;iter != end; iter++)
431    #ifdef MSG_NOSIGNAL
432                                    send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
433    #else
434                                  send(*iter, notify.c_str(), notify.size(), 0);                                  send(*iter, notify.c_str(), notify.size(), 0);
435    #endif
436                          NotifyMutex.Unlock();                          NotifyMutex.Unlock();
437                          break;                          break;
438                  } else {                  } else {
# Line 222  extern int GetLSCPCommand( void *buf, in Line 464  extern int GetLSCPCommand( void *buf, in
464          return command.size();          return command.size();
465  }  }
466    
467    extern yyparse_param_t* GetCurrentYaccSession() {
468        return &(*itCurrentSession);
469    }
470    
471  /**  /**
472   * Will be called to try to read the command from the socket   * Will be called to try to read the command from the socket
473   * If command is read, it will return true. Otherwise false is returned.   * If command is read, it will return true. Otherwise false is returned.
474   * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.   * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
475   */   */
476  bool LSCPServer::GetLSCPCommand( std::vector<int>::iterator iter ) {  bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
477          int socket = *iter;          int socket = (*iter).hSession;
478          char c;          char c;
479          int i = 0;          int i = 0;
480          while (true) {          while (true) {
# Line 238  bool LSCPServer::GetLSCPCommand( std::ve Line 484  bool LSCPServer::GetLSCPCommand( std::ve
484                          break;                          break;
485                  }                  }
486                  if (result == 1) {                  if (result == 1) {
487                          if (c == '\r')                          if (c == '\r')
488                                  continue; //Ignore CR                                  continue; //Ignore CR
489                          if (c == '\n') {                          if (c == '\n') {
490                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
491                                  bufferedCommands[socket] += "\n";                                  bufferedCommands[socket] += "\r\n";
492                                  return true; //Complete command was read                                  return true; //Complete command was read
493                          }                          }
494                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
# Line 265  bool LSCPServer::GetLSCPCommand( std::ve Line 511  bool LSCPServer::GetLSCPCommand( std::ve
511                                          break;                                          break;
512                                  case EAGAIN:                                  case EAGAIN:
513                                          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"));                                          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"));
514                                          break;                                          break;
515                                  case EINTR:                                  case EINTR:
516                                          dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));                                          dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
517                                          break;                                          break;
518                                  case EFAULT:                                  case EFAULT:
519                                          dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));                                          dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
520                                          break;                                          break;
521                                  case EINVAL:                                  case EINVAL:
522                                          dmsg(2,("LSCPScanner: Invalid argument passed.\n"));                                          dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
523                                          break;                                          break;
524                                  case ENOMEM:                                  case ENOMEM:
525                                          dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));                                          dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
526                                          break;                                          break;
527                                  default:                                  default:
528                                          dmsg(2,("LSCPScanner: Unknown recv() error.\n"));                                          dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
529                                          break;                                          break;
530                          }                          }
531                          CloseConnection(iter);                          CloseConnection(iter);
532                          break;                          break;
533                  }                  }
# Line 299  void LSCPServer::AnswerClient(String Ret Line 545  void LSCPServer::AnswerClient(String Ret
545      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
546      if (currentSocket != -1) {      if (currentSocket != -1) {
547              NotifyMutex.Lock();              NotifyMutex.Lock();
548    #ifdef MSG_NOSIGNAL
549                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
550    #else
551              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
552    #endif
553              NotifyMutex.Unlock();              NotifyMutex.Unlock();
554      }      }
555  }  }
# Line 343  String LSCPServer::CreateAudioOutputDevi Line 593  String LSCPServer::CreateAudioOutputDevi
593          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
594          // search for the created device to get its index          // search for the created device to get its index
595          int index = GetAudioOutputDeviceIndex(pDevice);          int index = GetAudioOutputDeviceIndex(pDevice);
596          if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");          if (index == -1) throw Exception("Internal error: could not find created audio output device.");
597          result = index; // success          result = index; // success
598      }      }
599      catch (LinuxSamplerException e) {      catch (Exception e) {
600          result.Error(e);          result.Error(e);
601      }      }
602      return result.Produce();      return result.Produce();
# Line 359  String LSCPServer::CreateMidiInputDevice Line 609  String LSCPServer::CreateMidiInputDevice
609          MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);          MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
610          // search for the created device to get its index          // search for the created device to get its index
611          int index = GetMidiInputDeviceIndex(pDevice);          int index = GetMidiInputDeviceIndex(pDevice);
612          if (index == -1) throw LinuxSamplerException("Internal error: could not find created midi input device.");          if (index == -1) throw Exception("Internal error: could not find created midi input device.");
613          result = index; // success          result = index; // success
614      }      }
615      catch (LinuxSamplerException e) {      catch (Exception e) {
616          result.Error(e);          result.Error(e);
617      }      }
618      return result.Produce();      return result.Produce();
# Line 373  String LSCPServer::DestroyAudioOutputDev Line 623  String LSCPServer::DestroyAudioOutputDev
623      LSCPResultSet result;      LSCPResultSet result;
624      try {      try {
625          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
626          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
627          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
628          pSampler->DestroyAudioOutputDevice(pDevice);          pSampler->DestroyAudioOutputDevice(pDevice);
629      }      }
630      catch (LinuxSamplerException e) {      catch (Exception e) {
631          result.Error(e);          result.Error(e);
632      }      }
633      return result.Produce();      return result.Produce();
# Line 388  String LSCPServer::DestroyMidiInputDevic Line 638  String LSCPServer::DestroyMidiInputDevic
638      LSCPResultSet result;      LSCPResultSet result;
639      try {      try {
640          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
641            if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
642          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
         if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");  
643          pSampler->DestroyMidiInputDevice(pDevice);          pSampler->DestroyMidiInputDevice(pDevice);
644      }      }
645      catch (LinuxSamplerException e) {      catch (Exception e) {
646          result.Error(e);          result.Error(e);
647      }      }
648      return result.Produce();      return result.Produce();
649  }  }
650    
651    EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
652        SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
653        if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
654    
655        EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
656        if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
657    
658        return pEngineChannel;
659    }
660    
661  /**  /**
662   * Will be called by the parser to load an instrument.   * Will be called by the parser to load an instrument.
663   */   */
# Line 406  String LSCPServer::LoadInstrument(String Line 666  String LSCPServer::LoadInstrument(String
666      LSCPResultSet result;      LSCPResultSet result;
667      try {      try {
668          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
669          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
670          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
671          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel yet");
672          if (pSamplerChannel->GetAudioOutputDevice() == NULL)          if (!pSamplerChannel->GetAudioOutputDevice())
673              throw LinuxSamplerException("No audio output device on channel");              throw Exception("No audio output device connected to sampler channel");
674          if (bBackground) {          if (bBackground) {
675              LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument);              InstrumentManager::instrument_id_t id;
676              pLoadInstrument->StartThread();              id.FileName = Filename;
677                id.Index    = uiInstrument;
678                InstrumentManager::LoadInstrumentInBackground(id, pEngineChannel);
679            }
680            else {
681                // tell the engine channel which instrument to load
682                pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
683                // actually start to load the instrument (blocks until completed)
684                pEngineChannel->LoadInstrument();
685          }          }
         else pEngine->LoadInstrument(Filename.c_str(), uiInstrument);  
686      }      }
687      catch (LinuxSamplerException e) {      catch (Exception e) {
688           result.Error(e);           result.Error(e);
689      }      }
690      return result.Produce();      return result.Produce();
691  }  }
692    
693  /**  /**
694   * Will be called by the parser to load and deploy an engine.   * Will be called by the parser to assign a sampler engine type to a
695     * sampler channel.
696   */   */
697  String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) {  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
698      dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));      dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
699      LSCPResultSet result;      LSCPResultSet result;
700      try {      try {
         Engine::type_t type;  
         if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig;  
         else throw LinuxSamplerException("Unknown engine type");  
701          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
702          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
703          pSamplerChannel->LoadEngine(type);          LockRTNotify();
704            pSamplerChannel->SetEngineType(EngineName);
705            if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
706            UnlockRTNotify();
707      }      }
708      catch (LinuxSamplerException e) {      catch (Exception e) {
709           result.Error(e);           result.Error(e);
710      }      }
711      return result.Produce();      return result.Produce();
# Line 454  String LSCPServer::GetChannels() { Line 722  String LSCPServer::GetChannels() {
722  }  }
723    
724  /**  /**
725     * Will be called by the parser to get the list of sampler channels.
726     */
727    String LSCPServer::ListChannels() {
728        dmsg(2,("LSCPServer: ListChannels()\n"));
729        String list;
730        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
731        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
732        for (; iter != channels.end(); iter++) {
733            if (list != "") list += ",";
734            list += ToString(iter->first);
735        }
736        LSCPResultSet result;
737        result.Add(list);
738        return result.Produce();
739    }
740    
741    /**
742   * Will be called by the parser to add a sampler channel.   * Will be called by the parser to add a sampler channel.
743   */   */
744  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
745      dmsg(2,("LSCPServer: AddChannel()\n"));      dmsg(2,("LSCPServer: AddChannel()\n"));
746        LockRTNotify();
747      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
748        UnlockRTNotify();
749      LSCPResultSet result(pSamplerChannel->Index());      LSCPResultSet result(pSamplerChannel->Index());
750      return result.Produce();      return result.Produce();
751  }  }
# Line 469  String LSCPServer::AddChannel() { Line 756  String LSCPServer::AddChannel() {
756  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
757      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
758      LSCPResultSet result;      LSCPResultSet result;
759        LockRTNotify();
760      pSampler->RemoveSamplerChannel(uiSamplerChannel);      pSampler->RemoveSamplerChannel(uiSamplerChannel);
761        UnlockRTNotify();
762      return result.Produce();      return result.Produce();
763  }  }
764    
765  /**  /**
766   * Will be called by the parser to get all available engines.   * Will be called by the parser to get the amount of all available engines.
767   */   */
768  String LSCPServer::GetAvailableEngines() {  String LSCPServer::GetAvailableEngines() {
769      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
770      LSCPResultSet result("GigEngine");      LSCPResultSet result;
771        try {
772            int n = EngineFactory::AvailableEngineTypes().size();
773            result.Add(n);
774        }
775        catch (Exception e) {
776            result.Error(e);
777        }
778      return result.Produce();      return result.Produce();
779  }  }
780    
781  /**  /**
782   * Will be called by the parser to get descriptions for a particular engine.   * Will be called by the parser to get a list of all available engines.
783     */
784    String LSCPServer::ListAvailableEngines() {
785        dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
786        LSCPResultSet result;
787        try {
788            String s = EngineFactory::AvailableEngineTypesAsString();
789            result.Add(s);
790        }
791        catch (Exception e) {
792            result.Error(e);
793        }
794        return result.Produce();
795    }
796    
797    /**
798     * Will be called by the parser to get descriptions for a particular
799     * sampler engine.
800   */   */
801  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
802      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
803      LSCPResultSet result;      LSCPResultSet result;
804        LockRTNotify();
805      try {      try {
806          if ((EngineName == "GigEngine") || (EngineName == "gig")) {          Engine* pEngine = EngineFactory::Create(EngineName);
807              Engine* pEngine = new LinuxSampler::gig::Engine;          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
808              result.Add(pEngine->Description());          result.Add("VERSION",     pEngine->Version());
809              result.Add(pEngine->Version());          EngineFactory::Destroy(pEngine);
             delete pEngine;  
         }  
         else throw LinuxSamplerException("Unknown engine type");  
810      }      }
811      catch (LinuxSamplerException e) {      catch (Exception e) {
812           result.Error(e);           result.Error(e);
813      }      }
814        UnlockRTNotify();
815      return result.Produce();      return result.Produce();
816  }  }
817    
# Line 512  String LSCPServer::GetChannelInfo(uint u Line 824  String LSCPServer::GetChannelInfo(uint u
824      LSCPResultSet result;      LSCPResultSet result;
825      try {      try {
826          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
827          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
828          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
829    
830          //Defaults values          //Defaults values
831          String EngineName = "NONE";          String EngineName = "NONE";
832          float Volume = 0;          float Volume = 0.0f;
833          String InstrumentFileName = "NONE";          String InstrumentFileName = "NONE";
834            String InstrumentName = "NONE";
835          int InstrumentIndex = -1;          int InstrumentIndex = -1;
836          int InstrumentStatus = -1;          int InstrumentStatus = -1;
837            int AudioOutputChannels = 0;
838          if (pEngine) {          String AudioRouting;
839              EngineName =  pEngine->EngineName();          int Mute = 0;
840              Volume = pEngine->Volume();          bool Solo = false;
841              InstrumentStatus = pEngine->InstrumentStatus();          String MidiInstrumentMap = "NONE";
842              InstrumentIndex = pEngine->InstrumentIndex();  
843              if (InstrumentIndex != -1)          if (pEngineChannel) {
844                  InstrumentFileName = pEngine->InstrumentFileName();              EngineName          = pEngineChannel->EngineName();
845                AudioOutputChannels = pEngineChannel->Channels();
846                Volume              = pEngineChannel->Volume();
847                InstrumentStatus    = pEngineChannel->InstrumentStatus();
848                InstrumentIndex     = pEngineChannel->InstrumentIndex();
849                if (InstrumentIndex != -1) {
850                    InstrumentFileName = pEngineChannel->InstrumentFileName();
851                    InstrumentName     = pEngineChannel->InstrumentName();
852                }
853                for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
854                    if (AudioRouting != "") AudioRouting += ",";
855                    AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
856                }
857                Mute = pEngineChannel->GetMute();
858                Solo = pEngineChannel->GetSolo();
859                if (pEngineChannel->UsesNoMidiInstrumentMap())
860                    MidiInstrumentMap = "NONE";
861                else if (pEngineChannel->UsesDefaultMidiInstrumentMap())
862                    MidiInstrumentMap = "DEFAULT";
863                else
864                    MidiInstrumentMap = ToString(pEngineChannel->GetMidiInstrumentMap());
865          }          }
866    
867          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
# Line 536  String LSCPServer::GetChannelInfo(uint u Line 869  String LSCPServer::GetChannelInfo(uint u
869    
870          //Some not-so-hardcoded stuff to make GUI look good          //Some not-so-hardcoded stuff to make GUI look good
871          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
872          result.Add("AUDIO_OUTPUT_CHANNELS", "2");          result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
873          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
874    
875          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
876          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
877          result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
878            else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
879    
880            // convert the filename into the correct encoding as defined for LSCP
881            // (especially in terms of special characters -> escape sequences)
882            if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
883    #if WIN32
884                InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
885    #else
886                // assuming POSIX
887                InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
888    #endif
889            }
890    
891          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
892          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
893            result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
894          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
895            result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
896            result.Add("SOLO", Solo);
897            result.Add("MIDI_INSTRUMENT_MAP", MidiInstrumentMap);
898      }      }
899      catch (LinuxSamplerException e) {      catch (Exception e) {
900           result.Error(e);           result.Error(e);
901      }      }
902      return result.Produce();      return result.Produce();
# Line 562  String LSCPServer::GetVoiceCount(uint ui Line 911  String LSCPServer::GetVoiceCount(uint ui
911      LSCPResultSet result;      LSCPResultSet result;
912      try {      try {
913          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
914          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
915          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
916          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw Exception("No engine loaded on sampler channel");
917          result.Add(pEngine->VoiceCount());          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
918            result.Add(pEngineChannel->GetEngine()->VoiceCount());
919      }      }
920      catch (LinuxSamplerException e) {      catch (Exception e) {
921           result.Error(e);           result.Error(e);
922      }      }
923      return result.Produce();      return result.Produce();
# Line 582  String LSCPServer::GetStreamCount(uint u Line 932  String LSCPServer::GetStreamCount(uint u
932      LSCPResultSet result;      LSCPResultSet result;
933      try {      try {
934          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
935          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
936          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
937          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
938          result.Add(pEngine->DiskStreamCount());          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
939            result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
940      }      }
941      catch (LinuxSamplerException e) {      catch (Exception e) {
942           result.Error(e);           result.Error(e);
943      }      }
944      return result.Produce();      return result.Produce();
# Line 602  String LSCPServer::GetBufferFill(fill_re Line 953  String LSCPServer::GetBufferFill(fill_re
953      LSCPResultSet result;      LSCPResultSet result;
954      try {      try {
955          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
956          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
957          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
958          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
959          if (!pEngine->DiskStreamSupported())          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
960              result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
961          else {          else {
962              switch (ResponseType) {              switch (ResponseType) {
963                  case fill_response_bytes:                  case fill_response_bytes:
964                      result.Add(pEngine->DiskStreamBufferFillBytes());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
965                      break;                      break;
966                  case fill_response_percentage:                  case fill_response_percentage:
967                      result.Add(pEngine->DiskStreamBufferFillPercentage());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
968                      break;                      break;
969                  default:                  default:
970                      throw LinuxSamplerException("Unknown fill response type");                      throw Exception("Unknown fill response type");
971              }              }
972          }          }
973      }      }
974      catch (LinuxSamplerException e) {      catch (Exception e) {
975           result.Error(e);           result.Error(e);
976      }      }
977      return result.Produce();      return result.Produce();
# Line 630  String LSCPServer::GetAvailableAudioOutp Line 981  String LSCPServer::GetAvailableAudioOutp
981      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
982      LSCPResultSet result;      LSCPResultSet result;
983      try {      try {
984            int n = AudioOutputDeviceFactory::AvailableDrivers().size();
985            result.Add(n);
986        }
987        catch (Exception e) {
988            result.Error(e);
989        }
990        return result.Produce();
991    }
992    
993    String LSCPServer::ListAvailableAudioOutputDrivers() {
994        dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
995        LSCPResultSet result;
996        try {
997          String s = AudioOutputDeviceFactory::AvailableDriversAsString();          String s = AudioOutputDeviceFactory::AvailableDriversAsString();
998          result.Add(s);          result.Add(s);
999      }      }
1000      catch (LinuxSamplerException e) {      catch (Exception e) {
1001          result.Error(e);          result.Error(e);
1002      }      }
1003      return result.Produce();      return result.Produce();
# Line 643  String LSCPServer::GetAvailableMidiInput Line 1007  String LSCPServer::GetAvailableMidiInput
1007      dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));      dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
1008      LSCPResultSet result;      LSCPResultSet result;
1009      try {      try {
1010            int n = MidiInputDeviceFactory::AvailableDrivers().size();
1011            result.Add(n);
1012        }
1013        catch (Exception e) {
1014            result.Error(e);
1015        }
1016        return result.Produce();
1017    }
1018    
1019    String LSCPServer::ListAvailableMidiInputDrivers() {
1020        dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
1021        LSCPResultSet result;
1022        try {
1023          String s = MidiInputDeviceFactory::AvailableDriversAsString();          String s = MidiInputDeviceFactory::AvailableDriversAsString();
1024          result.Add(s);          result.Add(s);
1025      }      }
1026      catch (LinuxSamplerException e) {      catch (Exception e) {
1027          result.Error(e);          result.Error(e);
1028      }      }
1029      return result.Produce();      return result.Produce();
# Line 670  String LSCPServer::GetMidiInputDriverInf Line 1047  String LSCPServer::GetMidiInputDriverInf
1047              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1048          }          }
1049      }      }
1050      catch (LinuxSamplerException e) {      catch (Exception e) {
1051          result.Error(e);          result.Error(e);
1052      }      }
1053      return result.Produce();      return result.Produce();
# Line 694  String LSCPServer::GetAudioOutputDriverI Line 1071  String LSCPServer::GetAudioOutputDriverI
1071              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1072          }          }
1073      }      }
1074      catch (LinuxSamplerException e) {      catch (Exception e) {
1075          result.Error(e);          result.Error(e);
1076      }      }
1077      return result.Produce();      return result.Produce();
1078  }  }
1079    
1080  String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {  String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1081      dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str()));      dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1082      LSCPResultSet result;      LSCPResultSet result;
1083      try {      try {
1084          DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);          DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
# Line 710  String LSCPServer::GetMidiInputDriverPar Line 1087  String LSCPServer::GetMidiInputDriverPar
1087          result.Add("MANDATORY",    pParameter->Mandatory());          result.Add("MANDATORY",    pParameter->Mandatory());
1088          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
1089          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
1090          if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());          optional<String> oDepends       = pParameter->Depends();
1091          if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());          optional<String> oDefault       = pParameter->Default(DependencyList);
1092          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
1093          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
1094          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1095            if (oDepends)       result.Add("DEPENDS",       *oDepends);
1096            if (oDefault)       result.Add("DEFAULT",       *oDefault);
1097            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1098            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1099            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1100      }      }
1101      catch (LinuxSamplerException e) {      catch (Exception e) {
1102          result.Error(e);          result.Error(e);
1103      }      }
1104      return result.Produce();      return result.Produce();
1105  }  }
1106    
1107  String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {  String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1108      dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str()));      dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1109      LSCPResultSet result;      LSCPResultSet result;
1110      try {      try {
1111          DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);          DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
# Line 732  String LSCPServer::GetAudioOutputDriverP Line 1114  String LSCPServer::GetAudioOutputDriverP
1114          result.Add("MANDATORY",    pParameter->Mandatory());          result.Add("MANDATORY",    pParameter->Mandatory());
1115          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
1116          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
1117          if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());          optional<String> oDepends       = pParameter->Depends();
1118          if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());          optional<String> oDefault       = pParameter->Default(DependencyList);
1119          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
1120          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
1121          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1122            if (oDepends)       result.Add("DEPENDS",       *oDepends);
1123            if (oDefault)       result.Add("DEFAULT",       *oDefault);
1124            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1125            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1126            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1127      }      }
1128      catch (LinuxSamplerException e) {      catch (Exception e) {
1129          result.Error(e);          result.Error(e);
1130      }      }
1131      return result.Produce();      return result.Produce();
# Line 751  String LSCPServer::GetAudioOutputDeviceC Line 1138  String LSCPServer::GetAudioOutputDeviceC
1138          uint count = pSampler->AudioOutputDevices();          uint count = pSampler->AudioOutputDevices();
1139          result.Add(count); // success          result.Add(count); // success
1140      }      }
1141      catch (LinuxSamplerException e) {      catch (Exception e) {
1142          result.Error(e);          result.Error(e);
1143      }      }
1144      return result.Produce();      return result.Produce();
# Line 764  String LSCPServer::GetMidiInputDeviceCou Line 1151  String LSCPServer::GetMidiInputDeviceCou
1151          uint count = pSampler->MidiInputDevices();          uint count = pSampler->MidiInputDevices();
1152          result.Add(count); // success          result.Add(count); // success
1153      }      }
1154      catch (LinuxSamplerException e) {      catch (Exception e) {
1155          result.Error(e);          result.Error(e);
1156      }      }
1157      return result.Produce();      return result.Produce();
# Line 783  String LSCPServer::GetAudioOutputDevices Line 1170  String LSCPServer::GetAudioOutputDevices
1170          }          }
1171          result.Add(s);          result.Add(s);
1172      }      }
1173      catch (LinuxSamplerException e) {      catch (Exception e) {
1174          result.Error(e);          result.Error(e);
1175      }      }
1176      return result.Produce();      return result.Produce();
# Line 802  String LSCPServer::GetMidiInputDevices() Line 1189  String LSCPServer::GetMidiInputDevices()
1189          }          }
1190          result.Add(s);          result.Add(s);
1191      }      }
1192      catch (LinuxSamplerException e) {      catch (Exception e) {
1193          result.Error(e);          result.Error(e);
1194      }      }
1195      return result.Produce();      return result.Produce();
# Line 813  String LSCPServer::GetAudioOutputDeviceI Line 1200  String LSCPServer::GetAudioOutputDeviceI
1200      LSCPResultSet result;      LSCPResultSet result;
1201      try {      try {
1202          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1203          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1204          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1205          result.Add("driver", pDevice->Driver());          result.Add("DRIVER", pDevice->Driver());
1206          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1207          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1208          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
1209              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1210          }          }
1211      }      }
1212      catch (LinuxSamplerException e) {      catch (Exception e) {
1213          result.Error(e);          result.Error(e);
1214      }      }
1215      return result.Produce();      return result.Produce();
# Line 833  String LSCPServer::GetMidiInputDeviceInf Line 1220  String LSCPServer::GetMidiInputDeviceInf
1220      LSCPResultSet result;      LSCPResultSet result;
1221      try {      try {
1222          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1223            if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1224          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1225          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          result.Add("DRIVER", pDevice->Driver());
         result.Add("driver", pDevice->Driver());  
1226          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1227          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1228          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
1229              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1230          }          }
1231      }      }
1232      catch (LinuxSamplerException e) {      catch (Exception e) {
1233          result.Error(e);          result.Error(e);
1234      }      }
1235      return result.Produce();      return result.Produce();
# Line 851  String LSCPServer::GetMidiInputPortInfo( Line 1238  String LSCPServer::GetMidiInputPortInfo(
1238      dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));      dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
1239      LSCPResultSet result;      LSCPResultSet result;
1240      try {      try {
1241            // get MIDI input device
1242          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1243            if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1244          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1245          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");  
1246          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          // get MIDI port
1247          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1248          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1249          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();  
1250            // return the values of all MIDI port parameters
1251            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1252            std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1253          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
1254              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1255          }          }
1256      }      }
1257      catch (LinuxSamplerException e) {      catch (Exception e) {
1258          result.Error(e);          result.Error(e);
1259      }      }
1260      return result.Produce();      return result.Produce();
# Line 874  String LSCPServer::GetAudioOutputChannel Line 1266  String LSCPServer::GetAudioOutputChannel
1266      try {      try {
1267          // get audio output device          // get audio output device
1268          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1269          if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1270          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1271    
1272          // get audio channel          // get audio channel
1273          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1274          if (!pChannel) throw LinuxSamplerException("Audio ouotput device does not have channel " + ToString(ChannelId) + ".");          if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1275    
1276          // return the values of all audio channel parameters          // return the values of all audio channel parameters
1277          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
# Line 888  String LSCPServer::GetAudioOutputChannel Line 1280  String LSCPServer::GetAudioOutputChannel
1280              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1281          }          }
1282      }      }
1283      catch (LinuxSamplerException e) {      catch (Exception e) {
1284          result.Error(e);          result.Error(e);
1285      }      }
1286      return result.Produce();      return result.Produce();
# Line 898  String LSCPServer::GetMidiInputPortParam Line 1290  String LSCPServer::GetMidiInputPortParam
1290      dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));      dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
1291      LSCPResultSet result;      LSCPResultSet result;
1292      try {      try {
1293          // get audio output device          // get MIDI input device
1294          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1295          if (!devices[DeviceId]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no midi input device with index " + ToString(DeviceId) + ".");
1296          MidiInputDevice* pDevice = devices[DeviceId];          MidiInputDevice* pDevice = devices[DeviceId];
1297    
1298          // get midi port          // get midi port
1299          MidiInputDevice::MidiInputPort* pPort = pDevice->GetPort(PortId);          MidiInputPort* pPort = pDevice->GetPort(PortId);
1300          if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");          if (!pPort) throw Exception("Midi input device does not have port " + ToString(PortId) + ".");
1301    
1302          // get desired port parameter          // get desired port parameter
1303          std::map<String,DeviceCreationParameter*> parameters = pPort->DeviceParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1304          if (!parameters[ParameterName]) throw LinuxSamplerException("Midi port does not provice a parameters '" + ParameterName + "'.");          if (!parameters.count(ParameterName)) throw Exception("Midi port does not provide a parameter '" + ParameterName + "'.");
1305          DeviceCreationParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1306            
1307          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
1308          result.Add("TYPE",         pParameter->Type());          result.Add("TYPE",         pParameter->Type());
1309          result.Add("DESCRIPTION",  pParameter->Description());          result.Add("DESCRIPTION",  pParameter->Description());
1310          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
1311          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
1312          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1313          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1314          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1315      }      }
1316      catch (LinuxSamplerException e) {      catch (Exception e) {
1317          result.Error(e);          result.Error(e);
1318      }      }
1319      return result.Produce();      return result.Produce();
# Line 933  String LSCPServer::GetAudioOutputChannel Line 1325  String LSCPServer::GetAudioOutputChannel
1325      try {      try {
1326          // get audio output device          // get audio output device
1327          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1328          if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1329          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1330    
1331          // get audio channel          // get audio channel
1332          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1333          if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");          if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1334    
1335          // get desired audio channel parameter          // get desired audio channel parameter
1336          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1337          if (!parameters[ParameterName]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");          if (!parameters.count(ParameterName)) throw Exception("Audio channel does not provide a parameter '" + ParameterName + "'.");
1338          DeviceRuntimeParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1339    
1340          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
# Line 950  String LSCPServer::GetAudioOutputChannel Line 1342  String LSCPServer::GetAudioOutputChannel
1342          result.Add("DESCRIPTION",  pParameter->Description());          result.Add("DESCRIPTION",  pParameter->Description());
1343          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
1344          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
1345          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1346          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1347          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1348      }      }
1349      catch (LinuxSamplerException e) {      catch (Exception e) {
1350          result.Error(e);          result.Error(e);
1351      }      }
1352      return result.Produce();      return result.Produce();
# Line 966  String LSCPServer::SetAudioOutputChannel Line 1358  String LSCPServer::SetAudioOutputChannel
1358      try {      try {
1359          // get audio output device          // get audio output device
1360          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1361          if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1362          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1363    
1364          // get audio channel          // get audio channel
1365          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1366          if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");          if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1367    
1368          // get desired audio channel parameter          // get desired audio channel parameter
1369          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1370          if (!parameters[ParamKey]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");          if (!parameters.count(ParamKey)) throw Exception("Audio channel does not provide a parameter '" + ParamKey + "'.");
1371          DeviceRuntimeParameter* pParameter = parameters[ParamKey];          DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1372    
1373          // set new channel parameter value          // set new channel parameter value
1374          pParameter->SetValue(ParamVal);          pParameter->SetValue(ParamVal);
1375            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceId));
1376      }      }
1377      catch (LinuxSamplerException e) {      catch (Exception e) {
1378          result.Error(e);          result.Error(e);
1379      }      }
1380      return result.Produce();      return result.Produce();
# Line 992  String LSCPServer::SetAudioOutputDeviceP Line 1385  String LSCPServer::SetAudioOutputDeviceP
1385      LSCPResultSet result;      LSCPResultSet result;
1386      try {      try {
1387          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1388          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1389          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1390          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1391          if (!parameters[ParamKey]) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");          if (!parameters.count(ParamKey)) throw Exception("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1392          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1393            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceIndex));
1394      }      }
1395      catch (LinuxSamplerException e) {      catch (Exception e) {
1396          result.Error(e);          result.Error(e);
1397      }      }
1398      return result.Produce();      return result.Produce();
# Line 1009  String LSCPServer::SetMidiInputDevicePar Line 1403  String LSCPServer::SetMidiInputDevicePar
1403      LSCPResultSet result;      LSCPResultSet result;
1404      try {      try {
1405          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1406          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1407          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1408          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1409          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");          if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1410          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1411            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1412      }      }
1413      catch (LinuxSamplerException e) {      catch (Exception e) {
1414          result.Error(e);          result.Error(e);
1415      }      }
1416      return result.Produce();      return result.Produce();
# Line 1025  String LSCPServer::SetMidiInputPortParam Line 1420  String LSCPServer::SetMidiInputPortParam
1420      dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));      dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1421      LSCPResultSet result;      LSCPResultSet result;
1422      try {      try {
1423            // get MIDI input device
1424          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1425            if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1426          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1427          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");  
1428          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          // get MIDI port
1429          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1430          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1431          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");  
1432            // set port parameter value
1433            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1434            if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1435          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1436            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1437      }      }
1438      catch (LinuxSamplerException e) {      catch (Exception e) {
1439          result.Error(e);          result.Error(e);
1440      }      }
1441      return result.Produce();      return result.Produce();
# Line 1046  String LSCPServer::SetMidiInputPortParam Line 1447  String LSCPServer::SetMidiInputPortParam
1447   */   */
1448  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1449      dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));
1450      return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?      LSCPResultSet result;
1451        try {
1452            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1453            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1454            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1455            if (!pEngineChannel) throw Exception("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1456            if (!pSamplerChannel->GetAudioOutputDevice()) throw Exception("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1457            pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1458        }
1459        catch (Exception e) {
1460             result.Error(e);
1461        }
1462        return result.Produce();
1463  }  }
1464    
1465  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1466      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1467      LSCPResultSet result;      LSCPResultSet result;
1468        LockRTNotify();
1469      try {      try {
1470          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1471          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1472          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1473            if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));
1474          AudioOutputDevice* pDevice = devices[AudioDeviceId];          AudioOutputDevice* pDevice = devices[AudioDeviceId];
         if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));  
1475          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1476      }      }
1477      catch (LinuxSamplerException e) {      catch (Exception e) {
1478           result.Error(e);           result.Error(e);
1479      }      }
1480        UnlockRTNotify();
1481      return result.Produce();      return result.Produce();
1482  }  }
1483    
1484  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1485      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1486      LSCPResultSet result;      LSCPResultSet result;
1487        LockRTNotify();
1488      try {      try {
1489          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1490          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1491          // Driver type name aliasing...          // Driver type name aliasing...
1492          if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1493          if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";                  if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1494          // Check if there's one audio output device already created          // Check if there's one audio output device already created
1495          // for the intended audio driver type (AudioOutputDriver)...          // for the intended audio driver type (AudioOutputDriver)...
1496          AudioOutputDevice *pDevice = NULL;          AudioOutputDevice *pDevice = NULL;
# Line 1093  String LSCPServer::SetAudioOutputType(St Line 1509  String LSCPServer::SetAudioOutputType(St
1509          }          }
1510          // Must have a device...          // Must have a device...
1511          if (pDevice == NULL)          if (pDevice == NULL)
1512              throw LinuxSamplerException("Internal error: could not create audio output device.");              throw Exception("Internal error: could not create audio output device.");
1513          // Set it as the current channel device...          // Set it as the current channel device...
1514          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1515      }      }
1516      catch (LinuxSamplerException e) {      catch (Exception e) {
1517           result.Error(e);           result.Error(e);
1518      }      }
1519        UnlockRTNotify();
1520      return result.Produce();      return result.Produce();
1521  }  }
1522    
# Line 1108  String LSCPServer::SetMIDIInputPort(uint Line 1525  String LSCPServer::SetMIDIInputPort(uint
1525      LSCPResultSet result;      LSCPResultSet result;
1526      try {      try {
1527          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1528          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1529          pSamplerChannel->SetMidiInputPort(MIDIPort);          pSamplerChannel->SetMidiInputPort(MIDIPort);
1530      }      }
1531      catch (LinuxSamplerException e) {      catch (Exception e) {
1532           result.Error(e);           result.Error(e);
1533      }      }
1534      return result.Produce();      return result.Produce();
# Line 1122  String LSCPServer::SetMIDIInputChannel(u Line 1539  String LSCPServer::SetMIDIInputChannel(u
1539      LSCPResultSet result;      LSCPResultSet result;
1540      try {      try {
1541          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1542          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1543          pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1544      }      }
1545      catch (LinuxSamplerException e) {      catch (Exception e) {
1546           result.Error(e);           result.Error(e);
1547      }      }
1548      return result.Produce();      return result.Produce();
# Line 1136  String LSCPServer::SetMIDIInputDevice(ui Line 1553  String LSCPServer::SetMIDIInputDevice(ui
1553      LSCPResultSet result;      LSCPResultSet result;
1554      try {      try {
1555          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1556          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1557          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1558            if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1559          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
         if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));  
1560          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1561      }      }
1562      catch (LinuxSamplerException e) {      catch (Exception e) {
1563           result.Error(e);           result.Error(e);
1564      }      }
1565      return result.Produce();      return result.Produce();
# Line 1153  String LSCPServer::SetMIDIInputType(Stri Line 1570  String LSCPServer::SetMIDIInputType(Stri
1570      LSCPResultSet result;      LSCPResultSet result;
1571      try {      try {
1572          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1573          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1574          // Driver type name aliasing...          // Driver type name aliasing...
1575          if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";          if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1576          // Check if there's one MIDI input device already created          // Check if there's one MIDI input device already created
1577          // for the intended MIDI driver type (MidiInputDriver)...          // for the intended MIDI driver type (MidiInputDriver)...
1578          MidiInputDevice *pDevice = NULL;          MidiInputDevice *pDevice = NULL;
# Line 1173  String LSCPServer::SetMIDIInputType(Stri Line 1590  String LSCPServer::SetMIDIInputType(Stri
1590              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1591              // Make it with at least one initial port.              // Make it with at least one initial port.
1592              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1593              parameters["ports"]->SetValue("1");              parameters["PORTS"]->SetValue("1");
1594          }          }
1595          // Must have a device...          // Must have a device...
1596          if (pDevice == NULL)          if (pDevice == NULL)
1597              throw LinuxSamplerException("Internal error: could not create MIDI input device.");              throw Exception("Internal error: could not create MIDI input device.");
1598          // Set it as the current channel device...          // Set it as the current channel device...
1599          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1600      }      }
1601      catch (LinuxSamplerException e) {      catch (Exception e) {
1602           result.Error(e);           result.Error(e);
1603      }      }
1604      return result.Produce();      return result.Produce();
# Line 1196  String LSCPServer::SetMIDIInput(uint MID Line 1613  String LSCPServer::SetMIDIInput(uint MID
1613      LSCPResultSet result;      LSCPResultSet result;
1614      try {      try {
1615          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1616          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1617          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1618            if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1619          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1620          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));          pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
         pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);  
1621      }      }
1622      catch (LinuxSamplerException e) {      catch (Exception e) {
1623           result.Error(e);           result.Error(e);
1624      }      }
1625      return result.Produce();      return result.Produce();
# Line 1212  String LSCPServer::SetMIDIInput(uint MID Line 1629  String LSCPServer::SetMIDIInput(uint MID
1629   * Will be called by the parser to change the global volume factor on a   * Will be called by the parser to change the global volume factor on a
1630   * particular sampler channel.   * particular sampler channel.
1631   */   */
1632  String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) {  String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1633      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1634      LSCPResultSet result;      LSCPResultSet result;
1635      try {      try {
1636          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1637          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1638          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1639          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1640          pEngine->Volume(Volume);          pEngineChannel->Volume(dVolume);
1641      }      }
1642      catch (LinuxSamplerException e) {      catch (Exception e) {
1643           result.Error(e);           result.Error(e);
1644      }      }
1645      return result.Produce();      return result.Produce();
1646  }  }
1647    
1648  /**  /**
1649     * Will be called by the parser to mute/unmute particular sampler channel.
1650     */
1651    String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1652        dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1653        LSCPResultSet result;
1654        try {
1655            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1656            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1657    
1658            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1659            if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1660    
1661            if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1662            else pEngineChannel->SetMute(1);
1663        } catch (Exception e) {
1664            result.Error(e);
1665        }
1666        return result.Produce();
1667    }
1668    
1669    /**
1670     * Will be called by the parser to solo particular sampler channel.
1671     */
1672    String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1673        dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1674        LSCPResultSet result;
1675        try {
1676            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1677            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1678    
1679            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1680            if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1681    
1682            bool oldSolo = pEngineChannel->GetSolo();
1683            bool hadSoloChannel = HasSoloChannel();
1684    
1685            pEngineChannel->SetSolo(bSolo);
1686    
1687            if(!oldSolo && bSolo) {
1688                if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1689                if(!hadSoloChannel) MuteNonSoloChannels();
1690            }
1691    
1692            if(oldSolo && !bSolo) {
1693                if(!HasSoloChannel()) UnmuteChannels();
1694                else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1695            }
1696        } catch (Exception e) {
1697            result.Error(e);
1698        }
1699        return result.Produce();
1700    }
1701    
1702    /**
1703     * Determines whether there is at least one solo channel in the channel list.
1704     *
1705     * @returns true if there is at least one solo channel in the channel list,
1706     * false otherwise.
1707     */
1708    bool LSCPServer::HasSoloChannel() {
1709        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1710        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1711        for (; iter != channels.end(); iter++) {
1712            EngineChannel* c = iter->second->GetEngineChannel();
1713            if(c && c->GetSolo()) return true;
1714        }
1715    
1716        return false;
1717    }
1718    
1719    /**
1720     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1721     * with -1 which indicates that they are muted because of the presence
1722     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1723     * when there are no solo channels left.
1724     */
1725    void LSCPServer::MuteNonSoloChannels() {
1726        dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1727        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1728        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1729        for (; iter != channels.end(); iter++) {
1730            EngineChannel* c = iter->second->GetEngineChannel();
1731            if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1732        }
1733    }
1734    
1735    /**
1736     * Unmutes all channels that are muted because of the presence
1737     * of a solo channel(s).
1738     */
1739    void  LSCPServer::UnmuteChannels() {
1740        dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1741        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1742        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1743        for (; iter != channels.end(); iter++) {
1744            EngineChannel* c = iter->second->GetEngineChannel();
1745            if(c && c->GetMute() == -1) c->SetMute(0);
1746        }
1747    }
1748    
1749    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) {
1750        dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1751    
1752        midi_prog_index_t idx;
1753        idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1754        idx.midi_bank_lsb = MidiBank & 0x7f;
1755        idx.midi_prog     = MidiProg;
1756    
1757        MidiInstrumentMapper::entry_t entry;
1758        entry.EngineName      = EngineType;
1759        entry.InstrumentFile  = InstrumentFile;
1760        entry.InstrumentIndex = InstrumentIndex;
1761        entry.LoadMode        = LoadMode;
1762        entry.Volume          = Volume;
1763        entry.Name            = Name;
1764    
1765        LSCPResultSet result;
1766        try {
1767            // PERSISTENT mapping commands might block for a long time, so in
1768            // that case we add/replace the mapping in another thread in case
1769            // the NON_MODAL argument was supplied, non persistent mappings
1770            // should return immediately, so we don't need to do that for them
1771            bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
1772            MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
1773        } catch (Exception e) {
1774            result.Error(e);
1775        }
1776        return result.Produce();
1777    }
1778    
1779    String LSCPServer::RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
1780        dmsg(2,("LSCPServer: RemoveMIDIInstrumentMapping()\n"));
1781    
1782        midi_prog_index_t idx;
1783        idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1784        idx.midi_bank_lsb = MidiBank & 0x7f;
1785        idx.midi_prog     = MidiProg;
1786    
1787        LSCPResultSet result;
1788        try {
1789            MidiInstrumentMapper::RemoveEntry(MidiMapID, idx);
1790        } catch (Exception e) {
1791            result.Error(e);
1792        }
1793        return result.Produce();
1794    }
1795    
1796    String LSCPServer::GetMidiInstrumentMappings(uint MidiMapID) {
1797        dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
1798        LSCPResultSet result;
1799        try {
1800            result.Add(MidiInstrumentMapper::Entries(MidiMapID).size());
1801        } catch (Exception e) {
1802            result.Error(e);
1803        }
1804        return result.Produce();
1805    }
1806    
1807    
1808    String LSCPServer::GetAllMidiInstrumentMappings() {
1809        dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
1810        LSCPResultSet result;
1811        std::vector<int> maps = MidiInstrumentMapper::Maps();
1812        int totalMappings = 0;
1813        for (int i = 0; i < maps.size(); i++) {
1814            try {
1815                totalMappings += MidiInstrumentMapper::Entries(maps[i]).size();
1816            } catch (Exception e) { /*NOOP*/ }
1817        }
1818        result.Add(totalMappings);
1819        return result.Produce();
1820    }
1821    
1822    String LSCPServer::GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
1823        dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
1824        LSCPResultSet result;
1825        try {
1826            midi_prog_index_t idx;
1827            idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1828            idx.midi_bank_lsb = MidiBank & 0x7f;
1829            idx.midi_prog     = MidiProg;
1830    
1831            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
1832            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);
1833            if (iter == mappings.end()) result.Error("there is no map entry with that index");
1834            else { // found
1835    
1836                // convert the filename into the correct encoding as defined for LSCP
1837                // (especially in terms of special characters -> escape sequences)
1838    #if WIN32
1839                const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp();
1840    #else
1841                // assuming POSIX
1842                const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp();
1843    #endif
1844    
1845                result.Add("NAME", _escapeLscpResponse(iter->second.Name));
1846                result.Add("ENGINE_NAME", iter->second.EngineName);
1847                result.Add("INSTRUMENT_FILE", instrumentFileName);
1848                result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);
1849                String instrumentName;
1850                Engine* pEngine = EngineFactory::Create(iter->second.EngineName);
1851                if (pEngine) {
1852                    if (pEngine->GetInstrumentManager()) {
1853                        InstrumentManager::instrument_id_t instrID;
1854                        instrID.FileName = iter->second.InstrumentFile;
1855                        instrID.Index    = iter->second.InstrumentIndex;
1856                        instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
1857                    }
1858                    EngineFactory::Destroy(pEngine);
1859                }
1860                result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
1861                switch (iter->second.LoadMode) {
1862                    case MidiInstrumentMapper::ON_DEMAND:
1863                        result.Add("LOAD_MODE", "ON_DEMAND");
1864                        break;
1865                    case MidiInstrumentMapper::ON_DEMAND_HOLD:
1866                        result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
1867                        break;
1868                    case MidiInstrumentMapper::PERSISTENT:
1869                        result.Add("LOAD_MODE", "PERSISTENT");
1870                        break;
1871                    default:
1872                        throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
1873                }
1874                result.Add("VOLUME", iter->second.Volume);
1875            }
1876        } catch (Exception e) {
1877            result.Error(e);
1878        }
1879        return result.Produce();
1880    }
1881    
1882    String LSCPServer::ListMidiInstrumentMappings(uint MidiMapID) {
1883        dmsg(2,("LSCPServer: ListMidiInstrumentMappings()\n"));
1884        LSCPResultSet result;
1885        try {
1886            String s;
1887            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
1888            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
1889            for (; iter != mappings.end(); iter++) {
1890                if (s.size()) s += ",";
1891                s += "{" + ToString(MidiMapID) + ","
1892                         + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
1893                         + ToString(int(iter->first.midi_prog)) + "}";
1894            }
1895            result.Add(s);
1896        } catch (Exception e) {
1897            result.Error(e);
1898        }
1899        return result.Produce();
1900    }
1901    
1902    String LSCPServer::ListAllMidiInstrumentMappings() {
1903        dmsg(2,("LSCPServer: ListAllMidiInstrumentMappings()\n"));
1904        LSCPResultSet result;
1905        try {
1906            std::vector<int> maps = MidiInstrumentMapper::Maps();
1907            String s;
1908            for (int i = 0; i < maps.size(); i++) {
1909                std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(maps[i]);
1910                std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
1911                for (; iter != mappings.end(); iter++) {
1912                    if (s.size()) s += ",";
1913                    s += "{" + ToString(maps[i]) + ","
1914                             + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
1915                             + ToString(int(iter->first.midi_prog)) + "}";
1916                }
1917            }
1918            result.Add(s);
1919        } catch (Exception e) {
1920            result.Error(e);
1921        }
1922        return result.Produce();
1923    }
1924    
1925    String LSCPServer::ClearMidiInstrumentMappings(uint MidiMapID) {
1926        dmsg(2,("LSCPServer: ClearMidiInstrumentMappings()\n"));
1927        LSCPResultSet result;
1928        try {
1929            MidiInstrumentMapper::RemoveAllEntries(MidiMapID);
1930        } catch (Exception e) {
1931            result.Error(e);
1932        }
1933        return result.Produce();
1934    }
1935    
1936    String LSCPServer::ClearAllMidiInstrumentMappings() {
1937        dmsg(2,("LSCPServer: ClearAllMidiInstrumentMappings()\n"));
1938        LSCPResultSet result;
1939        try {
1940            std::vector<int> maps = MidiInstrumentMapper::Maps();
1941            for (int i = 0; i < maps.size(); i++)
1942                MidiInstrumentMapper::RemoveAllEntries(maps[i]);
1943        } catch (Exception e) {
1944            result.Error(e);
1945        }
1946        return result.Produce();
1947    }
1948    
1949    String LSCPServer::AddMidiInstrumentMap(String MapName) {
1950        dmsg(2,("LSCPServer: AddMidiInstrumentMap()\n"));
1951        LSCPResultSet result;
1952        try {
1953            int MapID = MidiInstrumentMapper::AddMap(MapName);
1954            result = LSCPResultSet(MapID);
1955        } catch (Exception e) {
1956            result.Error(e);
1957        }
1958        return result.Produce();
1959    }
1960    
1961    String LSCPServer::RemoveMidiInstrumentMap(uint MidiMapID) {
1962        dmsg(2,("LSCPServer: RemoveMidiInstrumentMap()\n"));
1963        LSCPResultSet result;
1964        try {
1965            MidiInstrumentMapper::RemoveMap(MidiMapID);
1966        } catch (Exception e) {
1967            result.Error(e);
1968        }
1969        return result.Produce();
1970    }
1971    
1972    String LSCPServer::RemoveAllMidiInstrumentMaps() {
1973        dmsg(2,("LSCPServer: RemoveAllMidiInstrumentMaps()\n"));
1974        LSCPResultSet result;
1975        try {
1976            MidiInstrumentMapper::RemoveAllMaps();
1977        } catch (Exception e) {
1978            result.Error(e);
1979        }
1980        return result.Produce();
1981    }
1982    
1983    String LSCPServer::GetMidiInstrumentMaps() {
1984        dmsg(2,("LSCPServer: GetMidiInstrumentMaps()\n"));
1985        LSCPResultSet result;
1986        try {
1987            result.Add(MidiInstrumentMapper::Maps().size());
1988        } catch (Exception e) {
1989            result.Error(e);
1990        }
1991        return result.Produce();
1992    }
1993    
1994    String LSCPServer::ListMidiInstrumentMaps() {
1995        dmsg(2,("LSCPServer: ListMidiInstrumentMaps()\n"));
1996        LSCPResultSet result;
1997        try {
1998            std::vector<int> maps = MidiInstrumentMapper::Maps();
1999            String sList;
2000            for (int i = 0; i < maps.size(); i++) {
2001                if (sList != "") sList += ",";
2002                sList += ToString(maps[i]);
2003            }
2004            result.Add(sList);
2005        } catch (Exception e) {
2006            result.Error(e);
2007        }
2008        return result.Produce();
2009    }
2010    
2011    String LSCPServer::GetMidiInstrumentMap(uint MidiMapID) {
2012        dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2013        LSCPResultSet result;
2014        try {
2015            result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2016            result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2017        } catch (Exception e) {
2018            result.Error(e);
2019        }
2020        return result.Produce();
2021    }
2022    
2023    String LSCPServer::SetMidiInstrumentMapName(uint MidiMapID, String NewName) {
2024        dmsg(2,("LSCPServer: SetMidiInstrumentMapName()\n"));
2025        LSCPResultSet result;
2026        try {
2027            MidiInstrumentMapper::RenameMap(MidiMapID, NewName);
2028        } catch (Exception e) {
2029            result.Error(e);
2030        }
2031        return result.Produce();
2032    }
2033    
2034    /**
2035     * Set the MIDI instrument map the given sampler channel shall use for
2036     * handling MIDI program change messages. There are the following two
2037     * special (negative) values:
2038     *
2039     *    - (-1) :  set to NONE (ignore program changes)
2040     *    - (-2) :  set to DEFAULT map
2041     */
2042    String LSCPServer::SetChannelMap(uint uiSamplerChannel, int MidiMapID) {
2043        dmsg(2,("LSCPServer: SetChannelMap()\n"));
2044        LSCPResultSet result;
2045        try {
2046            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2047            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2048    
2049            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2050            if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
2051    
2052            if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2053            else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
2054            else                      pEngineChannel->SetMidiInstrumentMap(MidiMapID);
2055        } catch (Exception e) {
2056            result.Error(e);
2057        }
2058        return result.Produce();
2059    }
2060    
2061    String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) {
2062        dmsg(2,("LSCPServer: CreateFxSend()\n"));
2063        LSCPResultSet result;
2064        try {
2065            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2066    
2067            FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2068            if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2069    
2070            result = LSCPResultSet(pFxSend->Id()); // success
2071        } catch (Exception e) {
2072            result.Error(e);
2073        }
2074        return result.Produce();
2075    }
2076    
2077    String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) {
2078        dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2079        LSCPResultSet result;
2080        try {
2081            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2082    
2083            FxSend* pFxSend = NULL;
2084            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2085                if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2086                    pFxSend = pEngineChannel->GetFxSend(i);
2087                    break;
2088                }
2089            }
2090            if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2091            pEngineChannel->RemoveFxSend(pFxSend);
2092        } catch (Exception e) {
2093            result.Error(e);
2094        }
2095        return result.Produce();
2096    }
2097    
2098    String LSCPServer::GetFxSends(uint uiSamplerChannel) {
2099        dmsg(2,("LSCPServer: GetFxSends()\n"));
2100        LSCPResultSet result;
2101        try {
2102            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2103    
2104            result.Add(pEngineChannel->GetFxSendCount());
2105        } catch (Exception e) {
2106            result.Error(e);
2107        }
2108        return result.Produce();
2109    }
2110    
2111    String LSCPServer::ListFxSends(uint uiSamplerChannel) {
2112        dmsg(2,("LSCPServer: ListFxSends()\n"));
2113        LSCPResultSet result;
2114        String list;
2115        try {
2116            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2117    
2118            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2119                FxSend* pFxSend = pEngineChannel->GetFxSend(i);
2120                if (list != "") list += ",";
2121                list += ToString(pFxSend->Id());
2122            }
2123            result.Add(list);
2124        } catch (Exception e) {
2125            result.Error(e);
2126        }
2127        return result.Produce();
2128    }
2129    
2130    FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2131        EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2132    
2133        FxSend* pFxSend = NULL;
2134        for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2135            if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2136                pFxSend = pEngineChannel->GetFxSend(i);
2137                break;
2138            }
2139        }
2140        if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2141        return pFxSend;
2142    }
2143    
2144    String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2145        dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2146        LSCPResultSet result;
2147        try {
2148            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2149            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2150    
2151            // gather audio routing informations
2152            String AudioRouting;
2153            for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
2154                if (AudioRouting != "") AudioRouting += ",";
2155                AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2156            }
2157    
2158            // success
2159            result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2160            result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2161            result.Add("LEVEL", ToString(pFxSend->Level()));
2162            result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2163        } catch (Exception e) {
2164            result.Error(e);
2165        }
2166        return result.Produce();
2167    }
2168    
2169    String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2170        dmsg(2,("LSCPServer: SetFxSendName()\n"));
2171        LSCPResultSet result;
2172        try {
2173            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2174    
2175            pFxSend->SetName(Name);
2176            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2177        } catch (Exception e) {
2178            result.Error(e);
2179        }
2180        return result.Produce();
2181    }
2182    
2183    String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2184        dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2185        LSCPResultSet result;
2186        try {
2187            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2188    
2189            pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2190            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2191        } catch (Exception e) {
2192            result.Error(e);
2193        }
2194        return result.Produce();
2195    }
2196    
2197    String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2198        dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2199        LSCPResultSet result;
2200        try {
2201            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2202    
2203            pFxSend->SetMidiController(MidiController);
2204            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2205        } catch (Exception e) {
2206            result.Error(e);
2207        }
2208        return result.Produce();
2209    }
2210    
2211    String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2212        dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2213        LSCPResultSet result;
2214        try {
2215            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2216    
2217            pFxSend->SetLevel((float)dLevel);
2218            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2219        } catch (Exception e) {
2220            result.Error(e);
2221        }
2222        return result.Produce();
2223    }
2224    
2225    String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2226        dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2227        LSCPResultSet result;
2228        try {
2229            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2230            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2231            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2232            if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
2233            if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2234            Engine* pEngine = pEngineChannel->GetEngine();
2235            InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2236            if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2237            InstrumentManager::instrument_id_t instrumentID;
2238            instrumentID.FileName = pEngineChannel->InstrumentFileName();
2239            instrumentID.Index    = pEngineChannel->InstrumentIndex();
2240            pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2241        } catch (Exception e) {
2242            result.Error(e);
2243        }
2244        return result.Produce();
2245    }
2246    
2247    /**
2248   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
2249   */   */
2250  String LSCPServer::ResetChannel(uint uiSamplerChannel) {  String LSCPServer::ResetChannel(uint uiSamplerChannel) {
# Line 1236  String LSCPServer::ResetChannel(uint uiS Line 2252  String LSCPServer::ResetChannel(uint uiS
2252      LSCPResultSet result;      LSCPResultSet result;
2253      try {      try {
2254          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2255          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2256          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2257          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
2258          pEngine->Reset();          pEngineChannel->Reset();
2259      }      }
2260      catch (LinuxSamplerException e) {      catch (Exception e) {
2261           result.Error(e);           result.Error(e);
2262      }      }
2263      return result.Produce();      return result.Produce();
2264  }  }
2265    
2266  /**  /**
2267     * Will be called by the parser to reset the whole sampler.
2268     */
2269    String LSCPServer::ResetSampler() {
2270        dmsg(2,("LSCPServer: ResetSampler()\n"));
2271        pSampler->Reset();
2272        LSCPResultSet result;
2273        return result.Produce();
2274    }
2275    
2276    /**
2277     * Will be called by the parser to return general informations about this
2278     * sampler.
2279     */
2280    String LSCPServer::GetServerInfo() {
2281        dmsg(2,("LSCPServer: GetServerInfo()\n"));
2282        const std::string description =
2283            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2284        LSCPResultSet result;
2285        result.Add("DESCRIPTION", description);
2286        result.Add("VERSION", VERSION);
2287        result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2288    #if HAVE_SQLITE3
2289        result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2290    #else
2291        result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2292    #endif
2293    
2294        return result.Produce();
2295    }
2296    
2297    /**
2298     * Will be called by the parser to return the current number of all active voices.
2299     */
2300    String LSCPServer::GetTotalVoiceCount() {
2301        dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
2302        LSCPResultSet result;
2303        result.Add(pSampler->GetVoiceCount());
2304        return result.Produce();
2305    }
2306    
2307    /**
2308     * Will be called by the parser to return the maximum number of voices.
2309     */
2310    String LSCPServer::GetTotalVoiceCountMax() {
2311        dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
2312        LSCPResultSet result;
2313        result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);
2314        return result.Produce();
2315    }
2316    
2317    String LSCPServer::GetGlobalVolume() {
2318        LSCPResultSet result;
2319        result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp
2320        return result.Produce();
2321    }
2322    
2323    String LSCPServer::SetGlobalVolume(double dVolume) {
2324        LSCPResultSet result;
2325        try {
2326            if (dVolume < 0) throw Exception("Volume may not be negative");
2327            GLOBAL_VOLUME = dVolume; // see common/global.cpp
2328            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
2329        } catch (Exception e) {
2330            result.Error(e);
2331        }
2332        return result.Produce();
2333    }
2334    
2335    /**
2336   * Will be called by the parser to subscribe a client (frontend) on the   * Will be called by the parser to subscribe a client (frontend) on the
2337   * server for receiving event messages.   * server for receiving event messages.
2338   */   */
# Line 1273  String LSCPServer::UnsubscribeNotificati Line 2358  String LSCPServer::UnsubscribeNotificati
2358      return result.Produce();      return result.Produce();
2359  }  }
2360    
2361    String LSCPServer::AddDbInstrumentDirectory(String Dir) {
2362        dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
2363        LSCPResultSet result;
2364    #if HAVE_SQLITE3
2365        try {
2366            InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
2367        } catch (Exception e) {
2368             result.Error(e);
2369        }
2370    #else
2371        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2372    #endif
2373        return result.Produce();
2374    }
2375    
2376  // Instrument loader constructor.  String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
2377  LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)      dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
2378      : Thread(false, 0, -4)      LSCPResultSet result;
2379  {  #if HAVE_SQLITE3
2380      this->pEngine = pEngine;      try {
2381      this->Filename = Filename;          InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
2382      this->uiInstrument = uiInstrument;      } catch (Exception e) {
2383             result.Error(e);
2384        }
2385    #else
2386        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2387    #endif
2388        return result.Produce();
2389  }  }
2390    
2391  // Instrument loader process.  String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
2392  int LSCPLoadInstrument::Main()      dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2393  {      LSCPResultSet result;
2394    #if HAVE_SQLITE3
2395        try {
2396            result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
2397        } catch (Exception e) {
2398             result.Error(e);
2399        }
2400    #else
2401        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2402    #endif
2403        return result.Produce();
2404    }
2405    
2406    String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
2407        dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2408        LSCPResultSet result;
2409    #if HAVE_SQLITE3
2410        try {
2411            String list;
2412            StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
2413    
2414            for (int i = 0; i < dirs->size(); i++) {
2415                if (list != "") list += ",";
2416                list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
2417            }
2418    
2419            result.Add(list);
2420        } catch (Exception e) {
2421             result.Error(e);
2422        }
2423    #else
2424        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2425    #endif
2426        return result.Produce();
2427    }
2428    
2429    String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
2430        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
2431        LSCPResultSet result;
2432    #if HAVE_SQLITE3
2433      try {      try {
2434          pEngine->LoadInstrument(Filename.c_str(), uiInstrument);          DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2435    
2436            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2437            result.Add("CREATED", info.Created);
2438            result.Add("MODIFIED", info.Modified);
2439        } catch (Exception e) {
2440             result.Error(e);
2441      }      }
2442    #else
2443        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2444    #endif
2445        return result.Produce();
2446    }
2447    
2448      catch (LinuxSamplerException e) {  String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
2449          e.PrintMessage();      dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
2450        LSCPResultSet result;
2451    #if HAVE_SQLITE3
2452        try {
2453            InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
2454        } catch (Exception e) {
2455             result.Error(e);
2456      }      }
2457    #else
2458        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2459    #endif
2460        return result.Produce();
2461    }
2462    
2463      // Always re-enable the engine.  String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
2464      pEngine->Enable();      dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2465        LSCPResultSet result;
2466    #if HAVE_SQLITE3
2467        try {
2468            InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
2469        } catch (Exception e) {
2470             result.Error(e);
2471        }
2472    #else
2473        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2474    #endif
2475        return result.Produce();
2476    }
2477    
2478    String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
2479        dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2480        LSCPResultSet result;
2481    #if HAVE_SQLITE3
2482        try {
2483            InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
2484        } catch (Exception e) {
2485             result.Error(e);
2486        }
2487    #else
2488        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2489    #endif
2490        return result.Produce();
2491    }
2492    
2493    String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
2494        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
2495        LSCPResultSet result;
2496    #if HAVE_SQLITE3
2497        try {
2498            InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
2499        } catch (Exception e) {
2500             result.Error(e);
2501        }
2502    #else
2503        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2504    #endif
2505        return result.Produce();
2506    }
2507    
2508    String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
2509        dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
2510        LSCPResultSet result;
2511    #if HAVE_SQLITE3
2512        try {
2513            int id;
2514            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2515            id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
2516            if (bBackground) result = id;
2517        } catch (Exception e) {
2518             result.Error(e);
2519        }
2520    #else
2521        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2522    #endif
2523        return result.Produce();
2524    }
2525    
2526    String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) {
2527        dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground));
2528        LSCPResultSet result;
2529    #if HAVE_SQLITE3
2530        try {
2531            int id;
2532            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2533            if (ScanMode.compare("RECURSIVE") == 0) {
2534               id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground);
2535            } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
2536               id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground);
2537            } else if (ScanMode.compare("FLAT") == 0) {
2538               id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground);
2539            } else {
2540                throw Exception("Unknown scan mode: " + ScanMode);
2541            }
2542    
2543            if (bBackground) result = id;
2544        } catch (Exception e) {
2545             result.Error(e);
2546        }
2547    #else
2548        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2549    #endif
2550        return result.Produce();
2551    }
2552    
2553    String LSCPServer::RemoveDbInstrument(String Instr) {
2554        dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
2555        LSCPResultSet result;
2556    #if HAVE_SQLITE3
2557        try {
2558            InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
2559        } catch (Exception e) {
2560             result.Error(e);
2561        }
2562    #else
2563        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2564    #endif
2565        return result.Produce();
2566    }
2567    
2568    String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
2569        dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2570        LSCPResultSet result;
2571    #if HAVE_SQLITE3
2572        try {
2573            result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
2574        } catch (Exception e) {
2575             result.Error(e);
2576        }
2577    #else
2578        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2579    #endif
2580        return result.Produce();
2581    }
2582    
2583    String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
2584        dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2585        LSCPResultSet result;
2586    #if HAVE_SQLITE3
2587        try {
2588            String list;
2589            StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
2590    
2591            for (int i = 0; i < instrs->size(); i++) {
2592                if (list != "") list += ",";
2593                list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
2594            }
2595    
2596      // FIXME: Shoot ourselves on the foot?          result.Add(list);
2597      delete this;      } catch (Exception e) {
2598             result.Error(e);
2599        }
2600    #else
2601        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2602    #endif
2603        return result.Produce();
2604    }
2605    
2606    String LSCPServer::GetDbInstrumentInfo(String Instr) {
2607        dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
2608        LSCPResultSet result;
2609    #if HAVE_SQLITE3
2610        try {
2611            DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
2612    
2613            result.Add("INSTRUMENT_FILE", info.InstrFile);
2614            result.Add("INSTRUMENT_NR", info.InstrNr);
2615            result.Add("FORMAT_FAMILY", info.FormatFamily);
2616            result.Add("FORMAT_VERSION", info.FormatVersion);
2617            result.Add("SIZE", (int)info.Size);
2618            result.Add("CREATED", info.Created);
2619            result.Add("MODIFIED", info.Modified);
2620            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2621            result.Add("IS_DRUM", info.IsDrum);
2622            result.Add("PRODUCT", _escapeLscpResponse(info.Product));
2623            result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
2624            result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
2625        } catch (Exception e) {
2626             result.Error(e);
2627        }
2628    #else
2629        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2630    #endif
2631        return result.Produce();
2632    }
2633    
2634    String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
2635        dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
2636        LSCPResultSet result;
2637    #if HAVE_SQLITE3
2638        try {
2639            ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
2640    
2641            result.Add("FILES_TOTAL", job.FilesTotal);
2642            result.Add("FILES_SCANNED", job.FilesScanned);
2643            result.Add("SCANNING", job.Scanning);
2644            result.Add("STATUS", job.Status);
2645        } catch (Exception e) {
2646             result.Error(e);
2647        }
2648    #else
2649        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2650    #endif
2651        return result.Produce();
2652    }
2653    
2654    String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
2655        dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
2656        LSCPResultSet result;
2657    #if HAVE_SQLITE3
2658        try {
2659            InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
2660        } catch (Exception e) {
2661             result.Error(e);
2662        }
2663    #else
2664        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2665    #endif
2666        return result.Produce();
2667    }
2668    
2669    String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
2670        dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
2671        LSCPResultSet result;
2672    #if HAVE_SQLITE3
2673        try {
2674            InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
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::CopyDbInstrument(String Instr, String Dst) {
2685        dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
2686        LSCPResultSet result;
2687    #if HAVE_SQLITE3
2688        try {
2689            InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
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::SetDbInstrumentDescription(String Instr, String Desc) {
2700        dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
2701        LSCPResultSet result;
2702    #if HAVE_SQLITE3
2703        try {
2704            InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
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    String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
2715        dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
2716        LSCPResultSet result;
2717    #if HAVE_SQLITE3
2718        try {
2719            SearchQuery Query;
2720            std::map<String,String>::iterator iter;
2721            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
2722                if (iter->first.compare("NAME") == 0) {
2723                    Query.Name = iter->second;
2724                } else if (iter->first.compare("CREATED") == 0) {
2725                    Query.SetCreated(iter->second);
2726                } else if (iter->first.compare("MODIFIED") == 0) {
2727                    Query.SetModified(iter->second);
2728                } else if (iter->first.compare("DESCRIPTION") == 0) {
2729                    Query.Description = iter->second;
2730                } else {
2731                    throw Exception("Unknown search criteria: " + iter->first);
2732                }
2733            }
2734    
2735            String list;
2736            StringListPtr pDirectories =
2737                InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
2738    
2739            for (int i = 0; i < pDirectories->size(); i++) {
2740                if (list != "") list += ",";
2741                list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
2742            }
2743    
2744            result.Add(list);
2745        } catch (Exception e) {
2746             result.Error(e);
2747        }
2748    #else
2749        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2750    #endif
2751        return result.Produce();
2752    }
2753    
2754    String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
2755        dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
2756        LSCPResultSet result;
2757    #if HAVE_SQLITE3
2758        try {
2759            SearchQuery Query;
2760            std::map<String,String>::iterator iter;
2761            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
2762                if (iter->first.compare("NAME") == 0) {
2763                    Query.Name = iter->second;
2764                } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
2765                    Query.SetFormatFamilies(iter->second);
2766                } else if (iter->first.compare("SIZE") == 0) {
2767                    Query.SetSize(iter->second);
2768                } else if (iter->first.compare("CREATED") == 0) {
2769                    Query.SetCreated(iter->second);
2770                } else if (iter->first.compare("MODIFIED") == 0) {
2771                    Query.SetModified(iter->second);
2772                } else if (iter->first.compare("DESCRIPTION") == 0) {
2773                    Query.Description = iter->second;
2774                } else if (iter->first.compare("IS_DRUM") == 0) {
2775                    if (!strcasecmp(iter->second.c_str(), "true")) {
2776                        Query.InstrType = SearchQuery::DRUM;
2777                    } else {
2778                        Query.InstrType = SearchQuery::CHROMATIC;
2779                    }
2780                } else if (iter->first.compare("PRODUCT") == 0) {
2781                     Query.Product = iter->second;
2782                } else if (iter->first.compare("ARTISTS") == 0) {
2783                     Query.Artists = iter->second;
2784                } else if (iter->first.compare("KEYWORDS") == 0) {
2785                     Query.Keywords = iter->second;
2786                } else {
2787                    throw Exception("Unknown search criteria: " + iter->first);
2788                }
2789            }
2790    
2791            String list;
2792            StringListPtr pInstruments =
2793                InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
2794    
2795            for (int i = 0; i < pInstruments->size(); i++) {
2796                if (list != "") list += ",";
2797                list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
2798            }
2799    
2800            result.Add(list);
2801        } catch (Exception e) {
2802             result.Error(e);
2803        }
2804    #else
2805        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2806    #endif
2807        return result.Produce();
2808    }
2809    
2810    String LSCPServer::FormatInstrumentsDb() {
2811        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
2812        LSCPResultSet result;
2813    #if HAVE_SQLITE3
2814        try {
2815            InstrumentsDb::GetInstrumentsDb()->Format();
2816        } catch (Exception e) {
2817             result.Error(e);
2818        }
2819    #else
2820        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2821    #endif
2822        return result.Produce();
2823    }
2824    
2825    
2826    /**
2827     * Will be called by the parser to enable or disable echo mode; if echo
2828     * mode is enabled, all commands from the client will (immediately) be
2829     * echoed back to the client.
2830     */
2831    String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
2832        dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
2833        LSCPResultSet result;
2834        try {
2835            if      (boolean_value == 0) pSession->bVerbose = false;
2836            else if (boolean_value == 1) pSession->bVerbose = true;
2837            else throw Exception("Not a boolean value, must either be 0 or 1");
2838        }
2839        catch (Exception e) {
2840             result.Error(e);
2841        }
2842        return result.Produce();
2843  }  }

Legend:
Removed from v.198  
changed lines
  Added in v.1471

  ViewVC Help
Powered by ViewVC