/[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 185 by senkov, Wed Jul 7 02:49:51 2004 UTC revision 836 by persson, Wed Feb 8 20:28:46 2006 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 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 23  Line 24 
24  #include "lscpserver.h"  #include "lscpserver.h"
25  #include "lscpresultset.h"  #include "lscpresultset.h"
26  #include "lscpevent.h"  #include "lscpevent.h"
27    //#include "../common/global.h"
28    
29  #include "../engines/gig/Engine.h"  #include <fcntl.h>
30  #include "../audiodriver/AudioOutputDeviceFactory.h"  
31  #include "../mididriver/MidiInputDeviceFactory.h"  #if HAVE_SQLITE3
32    # include "sqlite3.h"
33    #endif
34    
35    #include "../engines/EngineFactory.h"
36    #include "../engines/EngineChannelFactory.h"
37    #include "../drivers/audio/AudioOutputDeviceFactory.h"
38    #include "../drivers/midi/MidiInputDeviceFactory.h"
39    
40  /**  /**
41   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
# Line 43  Line 52 
52   */   */
53  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
54  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
55  std::vector<int> LSCPServer::hSessions = std::vector<int>();  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
56  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
57  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
58  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> >();
59  Mutex LSCPServer::NotifyMutex = Mutex();  Mutex LSCPServer::NotifyMutex = Mutex();
60  Mutex LSCPServer::NotifyBufferMutex = Mutex();  Mutex LSCPServer::NotifyBufferMutex = Mutex();
61  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
62    Mutex LSCPServer::RTNotifyMutex = Mutex();
63    
64  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) {
65        SocketAddress.sin_family      = AF_INET;
66        SocketAddress.sin_addr.s_addr = addr;
67        SocketAddress.sin_port        = port;
68      this->pSampler = pSampler;      this->pSampler = pSampler;
69      LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
70      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
71      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
72      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
73      LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
74      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
75        LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
76        hSocket = -1;
77    }
78    
79    LSCPServer::~LSCPServer() {
80        if (hSocket >= 0) close(hSocket);
81    }
82    
83    /**
84     * Blocks the calling thread until the LSCP Server is initialized and
85     * accepting socket connections, if the server is already initialized then
86     * this method will return immediately.
87     * @param TimeoutSeconds     - optional: max. wait time in seconds
88     *                             (default: 0s)
89     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
90     *                             (default: 0ns)
91     * @returns  0 on success, a value less than 0 if timeout exceeded
92     */
93    int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
94        return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
95  }  }
96    
97  int LSCPServer::Main() {  int LSCPServer::Main() {
98      int hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
99      if (hSocket < 0) {      if (hSocket < 0) {
100          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
101          //return -1;          //return -1;
102          exit(EXIT_FAILURE);          exit(EXIT_FAILURE);
103      }      }
104    
     SocketAddress.sin_family      = AF_INET;  
     SocketAddress.sin_port        = htons(LSCP_PORT);  
     SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);  
   
105      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
106          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...";
107          close(hSocket);          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
108          //return -1;              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
109          exit(EXIT_FAILURE);                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
110                        std::cerr << "gave up!" << std::endl;
111                        close(hSocket);
112                        //return -1;
113                        exit(EXIT_FAILURE);
114                    }
115                    else sleep(1); // sleep 1s
116                }
117                else break; // success
118            }
119      }      }
120    
121      listen(hSocket, 1);      listen(hSocket, 1);
122      dmsg(1,("LSCPServer: Server running.\n")); // server running      Initialized.Set(true);
123    
124      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
125      sockaddr_in client;      sockaddr_in client;
126      int length = sizeof(client);      int length = sizeof(client);
     struct timeval tv;  
     tv.tv_sec = 30;  
     tv.tv_usec = 0;  
127      FD_ZERO(&fdSet);      FD_ZERO(&fdSet);
128      FD_SET(hSocket, &fdSet);      FD_SET(hSocket, &fdSet);
129      int maxSessions = hSocket;      int maxSessions = hSocket;
130    
131      // Parser initialization      timeval timeout;
     yyparse_param_t yyparse_param;  
     yyparse_param.pServer = this;  
132    
133      while (true) {      while (true) {
134          fd_set selectSet = fdSet;          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
135          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &tv);          {
136                std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
137                std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
138                std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
139                for (; itEngineChannel != itEnd; ++itEngineChannel) {
140                    if ((*itEngineChannel)->StatusChanged()) {
141                        SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));
142                    }
143                }
144            }
145    
146            //Now let's deliver late notifies (if any)
147            NotifyBufferMutex.Lock();
148            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
149    #ifdef MSG_NOSIGNAL
150                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
151    #else
152                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
153    #endif
154            }
155            bufferedNotifies.clear();
156            NotifyBufferMutex.Unlock();
157    
158            fd_set selectSet = fdSet;
159            timeout.tv_sec  = 0;
160            timeout.tv_usec = 100000;
161    
162            int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
163    
164          if (retval == 0)          if (retval == 0)
165                  continue; //Nothing in 30 seconds, try again                  continue; //Nothing try again
166          if (retval == -1) {          if (retval == -1) {
167                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
168                  close(hSocket);                  close(hSocket);
169                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
170          }          }
171            
172          //Accept new connections now (if any)          //Accept new connections now (if any)
173          if (FD_ISSET(hSocket, &selectSet)) {          if (FD_ISSET(hSocket, &selectSet)) {
174                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
# Line 121  int LSCPServer::Main() { Line 182  int LSCPServer::Main() {
182                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
183                  }                  }
184    
185                  hSessions.push_back(socket);                  // Parser initialization
186                    yyparse_param_t yyparse_param;
187                    yyparse_param.pServer  = this;
188                    yyparse_param.hSession = socket;
189    
190                    Sessions.push_back(yyparse_param);
191                  FD_SET(socket, &fdSet);                  FD_SET(socket, &fdSet);
192                  if (socket > maxSessions)                  if (socket > maxSessions)
193                          maxSessions = socket;                          maxSessions = socket;
# Line 131  int LSCPServer::Main() { Line 197  int LSCPServer::Main() {
197          }          }
198    
199          //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.
200          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++) {
201                  if (FD_ISSET(*iter, &selectSet)) {      //Was it this socket?                  if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?
202                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?
203                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
204                                  yylex_init(&yyparse_param.pScanner);                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
205                                  currentSocket = *iter;  //a hack                                  restart(NULL, dummy); // restart the 'scanner'
206                                  int result = yyparse(&yyparse_param);                                  currentSocket = (*iter).hSession;  //a hack
207                                    dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
208                                    if ((*iter).bVerbose) { // if echo mode enabled
209                                        AnswerClient(bufferedCommands[currentSocket]);
210                                    }
211                                    int result = yyparse(&(*iter));
212                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
213                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
214                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?
# Line 146  int LSCPServer::Main() { Line 217  int LSCPServer::Main() {
217                          }                          }
218                          //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.
219                          //we'll be back if there is data.                          //we'll be back if there is data.
220                          break;                          break;
221                  }                  }
222          }          }
   
         //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();  
223      }      }
     //It will never get here anyway  
     //yylex_destroy(yyparse_param.pScanner);  
224  }  }
225    
226  void LSCPServer::CloseConnection( std::vector<int>::iterator iter ) {  void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
227          int socket = *iter;          int socket = (*iter).hSession;
228          dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));          dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
229          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
230          hSessions.erase(iter);          Sessions.erase(iter);
231          FD_CLR(socket,  &fdSet);          FD_CLR(socket,  &fdSet);
232          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
233          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 180  void LSCPServer::CloseConnection( std::v Line 241  void LSCPServer::CloseConnection( std::v
241          NotifyMutex.Unlock();          NotifyMutex.Unlock();
242  }  }
243    
244    int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
245            int subs = 0;
246            SubscriptionMutex.Lock();
247            for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
248                            iter != events.end(); iter++)
249            {
250                    subs += eventSubscriptions.count(*iter);
251            }
252            SubscriptionMutex.Unlock();
253            return subs;
254    }
255    
256  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
257          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
258          if (eventSubscriptions.count(event.GetType()) == 0) {          if (eventSubscriptions.count(event.GetType()) == 0) {
# Line 193  void LSCPServer::SendLSCPNotify( LSCPEve Line 266  void LSCPServer::SendLSCPNotify( LSCPEve
266          while (true) {          while (true) {
267                  if (NotifyMutex.Trylock()) {                  if (NotifyMutex.Trylock()) {
268                          for(;iter != end; iter++)                          for(;iter != end; iter++)
269    #ifdef MSG_NOSIGNAL
270                                    send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
271    #else
272                                  send(*iter, notify.c_str(), notify.size(), 0);                                  send(*iter, notify.c_str(), notify.size(), 0);
273    #endif
274                          NotifyMutex.Unlock();                          NotifyMutex.Unlock();
275                          break;                          break;
276                  } else {                  } else {
# Line 230  extern int GetLSCPCommand( void *buf, in Line 307  extern int GetLSCPCommand( void *buf, in
307   * If command is read, it will return true. Otherwise false is returned.   * If command is read, it will return true. Otherwise false is returned.
308   * 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.
309   */   */
310  bool LSCPServer::GetLSCPCommand( std::vector<int>::iterator iter ) {  bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
311          int socket = *iter;          int socket = (*iter).hSession;
312          char c;          char c;
313          int i = 0;          int i = 0;
314          while (true) {          while (true) {
# Line 241  bool LSCPServer::GetLSCPCommand( std::ve Line 318  bool LSCPServer::GetLSCPCommand( std::ve
318                          break;                          break;
319                  }                  }
320                  if (result == 1) {                  if (result == 1) {
321                          if (c == '\r')                          if (c == '\r')
322                                  continue; //Ignore CR                                  continue; //Ignore CR
323                          if (c == '\n') {                          if (c == '\n') {
324                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
# Line 268  bool LSCPServer::GetLSCPCommand( std::ve Line 345  bool LSCPServer::GetLSCPCommand( std::ve
345                                          break;                                          break;
346                                  case EAGAIN:                                  case EAGAIN:
347                                          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"));
348                                          break;                                          break;
349                                  case EINTR:                                  case EINTR:
350                                          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"));
351                                          break;                                          break;
352                                  case EFAULT:                                  case EFAULT:
353                                          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"));
354                                          break;                                          break;
355                                  case EINVAL:                                  case EINVAL:
356                                          dmsg(2,("LSCPScanner: Invalid argument passed.\n"));                                          dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
357                                          break;                                          break;
358                                  case ENOMEM:                                  case ENOMEM:
359                                          dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));                                          dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
360                                          break;                                          break;
361                                  default:                                  default:
362                                          dmsg(2,("LSCPScanner: Unknown recv() error.\n"));                                          dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
363                                          break;                                          break;
364                          }                          }
365                          CloseConnection(iter);                          CloseConnection(iter);
366                          break;                          break;
367                  }                  }
# Line 302  void LSCPServer::AnswerClient(String Ret Line 379  void LSCPServer::AnswerClient(String Ret
379      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
380      if (currentSocket != -1) {      if (currentSocket != -1) {
381              NotifyMutex.Lock();              NotifyMutex.Lock();
382    #ifdef MSG_NOSIGNAL
383                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
384    #else
385              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
386    #endif
387              NotifyMutex.Unlock();              NotifyMutex.Unlock();
388      }      }
389  }  }
# Line 376  String LSCPServer::DestroyAudioOutputDev Line 457  String LSCPServer::DestroyAudioOutputDev
457      LSCPResultSet result;      LSCPResultSet result;
458      try {      try {
459          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
460          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
461          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
462          pSampler->DestroyAudioOutputDevice(pDevice);          pSampler->DestroyAudioOutputDevice(pDevice);
463      }      }
# Line 391  String LSCPServer::DestroyMidiInputDevic Line 472  String LSCPServer::DestroyMidiInputDevic
472      LSCPResultSet result;      LSCPResultSet result;
473      try {      try {
474          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
475            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
476          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
         if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");  
477          pSampler->DestroyMidiInputDevice(pDevice);          pSampler->DestroyMidiInputDevice(pDevice);
478      }      }
479      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 409  String LSCPServer::LoadInstrument(String Line 490  String LSCPServer::LoadInstrument(String
490      LSCPResultSet result;      LSCPResultSet result;
491      try {      try {
492          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
493          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
494          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
495          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet");
496          if (pSamplerChannel->GetAudioOutputDevice() == NULL)          if (!pSamplerChannel->GetAudioOutputDevice())
497              throw LinuxSamplerException("No audio output device on channel");              throw LinuxSamplerException("No audio output device connected to sampler channel");
498          if (bBackground) {          if (bBackground) {
499              LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument);              InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel);
500              pLoadInstrument->StartThread();          }
501            else {
502                // tell the engine channel which instrument to load
503                pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
504                // actually start to load the instrument (blocks until completed)
505                pEngineChannel->LoadInstrument();
506          }          }
         else pEngine->LoadInstrument(Filename.c_str(), uiInstrument);  
507      }      }
508      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
509           result.Error(e);           result.Error(e);
# Line 427  String LSCPServer::LoadInstrument(String Line 512  String LSCPServer::LoadInstrument(String
512  }  }
513    
514  /**  /**
515   * 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
516     * sampler channel.
517   */   */
518  String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) {  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
519      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));
520      LSCPResultSet result;      LSCPResultSet result;
521      try {      try {
         Engine::type_t type;  
         if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig;  
         else throw LinuxSamplerException("Unknown engine type");  
522          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
523          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
524          pSamplerChannel->LoadEngine(type);          LockRTNotify();
525            pSamplerChannel->SetEngineType(EngineName);
526            if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
527            UnlockRTNotify();
528      }      }
529      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
530           result.Error(e);           result.Error(e);
# Line 457  String LSCPServer::GetChannels() { Line 543  String LSCPServer::GetChannels() {
543  }  }
544    
545  /**  /**
546     * Will be called by the parser to get the list of sampler channels.
547     */
548    String LSCPServer::ListChannels() {
549        dmsg(2,("LSCPServer: ListChannels()\n"));
550        String list;
551        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
552        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
553        for (; iter != channels.end(); iter++) {
554            if (list != "") list += ",";
555            list += ToString(iter->first);
556        }
557        LSCPResultSet result;
558        result.Add(list);
559        return result.Produce();
560    }
561    
562    /**
563   * Will be called by the parser to add a sampler channel.   * Will be called by the parser to add a sampler channel.
564   */   */
565  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
# Line 472  String LSCPServer::AddChannel() { Line 575  String LSCPServer::AddChannel() {
575  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
576      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
577      LSCPResultSet result;      LSCPResultSet result;
578        LockRTNotify();
579      pSampler->RemoveSamplerChannel(uiSamplerChannel);      pSampler->RemoveSamplerChannel(uiSamplerChannel);
580        UnlockRTNotify();
581      return result.Produce();      return result.Produce();
582  }  }
583    
584  /**  /**
585   * 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.
586   */   */
587  String LSCPServer::GetAvailableEngines() {  String LSCPServer::GetAvailableEngines() {
588      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
589      LSCPResultSet result("GigEngine");      LSCPResultSet result("1");
590      return result.Produce();      return result.Produce();
591  }  }
592    
593  /**  /**
594   * 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.
595     */
596    String LSCPServer::ListAvailableEngines() {
597        dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
598        LSCPResultSet result("\'GIG\'");
599        return result.Produce();
600    }
601    
602    /**
603     * Will be called by the parser to get descriptions for a particular
604     * sampler engine.
605   */   */
606  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
607      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
608      LSCPResultSet result;      LSCPResultSet result;
609      try {      try {
610          if ((EngineName == "GigEngine") || (EngineName == "gig")) {          Engine* pEngine = EngineFactory::Create(EngineName);
611              Engine* pEngine = new LinuxSampler::gig::Engine;          result.Add("DESCRIPTION", pEngine->Description());
612              result.Add(pEngine->Description());          result.Add("VERSION",     pEngine->Version());
613              result.Add(pEngine->Version());          EngineFactory::Destroy(pEngine);
             delete pEngine;  
         }  
         else throw LinuxSamplerException("Unknown engine type");  
614      }      }
615      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
616           result.Error(e);           result.Error(e);
# Line 515  String LSCPServer::GetChannelInfo(uint u Line 627  String LSCPServer::GetChannelInfo(uint u
627      LSCPResultSet result;      LSCPResultSet result;
628      try {      try {
629          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
630          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
631          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
632    
633          //Defaults values          //Defaults values
634          String EngineName = "NONE";          String EngineName = "NONE";
635          float Volume = 0;          float Volume = 0.0f;
636          String InstrumentFileName = "NONE";          String InstrumentFileName = "NONE";
637            String InstrumentName = "NONE";
638          int InstrumentIndex = -1;          int InstrumentIndex = -1;
639          int InstrumentStatus = -1;          int InstrumentStatus = -1;
640            int AudioOutputChannels = 0;
641          if (pEngine) {          String AudioRouting;
642              EngineName =  pEngine->EngineName();          int Mute = 0;
643              Volume = pEngine->Volume();          bool Solo = false;
644              InstrumentStatus = pEngine->InstrumentStatus();  
645              InstrumentIndex = pEngine->InstrumentIndex();          if (pEngineChannel) {
646              if (InstrumentIndex != -1)              EngineName          = pEngineChannel->EngineName();
647                  InstrumentFileName = pEngine->InstrumentFileName();              AudioOutputChannels = pEngineChannel->Channels();
648                Volume              = pEngineChannel->Volume();
649                InstrumentStatus    = pEngineChannel->InstrumentStatus();
650                InstrumentIndex     = pEngineChannel->InstrumentIndex();
651                if (InstrumentIndex != -1) {
652                    InstrumentFileName = pEngineChannel->InstrumentFileName();
653                    InstrumentName     = pEngineChannel->InstrumentName();
654                }
655                for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
656                    if (AudioRouting != "") AudioRouting += ",";
657                    AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
658                }
659                Mute = pEngineChannel->GetMute();
660                Solo = pEngineChannel->GetSolo();
661          }          }
662    
663          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
# Line 539  String LSCPServer::GetChannelInfo(uint u Line 665  String LSCPServer::GetChannelInfo(uint u
665    
666          //Some not-so-hardcoded stuff to make GUI look good          //Some not-so-hardcoded stuff to make GUI look good
667          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
668          result.Add("AUDIO_OUTPUT_CHANNELS", "2");          result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
669          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
670    
671          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
672          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
673          result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
674            else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
675    
676          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
677          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
678            result.Add("INSTRUMENT_NAME", InstrumentName);
679          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
680            result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
681            result.Add("SOLO", Solo);
682      }      }
683      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
684           result.Error(e);           result.Error(e);
# Line 565  String LSCPServer::GetVoiceCount(uint ui Line 695  String LSCPServer::GetVoiceCount(uint ui
695      LSCPResultSet result;      LSCPResultSet result;
696      try {      try {
697          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
698          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
699          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
700          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel");
701          result.Add(pEngine->VoiceCount());          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
702            result.Add(pEngineChannel->GetEngine()->VoiceCount());
703      }      }
704      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
705           result.Error(e);           result.Error(e);
# Line 585  String LSCPServer::GetStreamCount(uint u Line 716  String LSCPServer::GetStreamCount(uint u
716      LSCPResultSet result;      LSCPResultSet result;
717      try {      try {
718          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
719          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
720          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
721          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
722          result.Add(pEngine->DiskStreamCount());          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
723            result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
724      }      }
725      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
726           result.Error(e);           result.Error(e);
# Line 605  String LSCPServer::GetBufferFill(fill_re Line 737  String LSCPServer::GetBufferFill(fill_re
737      LSCPResultSet result;      LSCPResultSet result;
738      try {      try {
739          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
740          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
741          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
742          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
743          if (!pEngine->DiskStreamSupported())          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
744              result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
745          else {          else {
746              switch (ResponseType) {              switch (ResponseType) {
747                  case fill_response_bytes:                  case fill_response_bytes:
748                      result.Add(pEngine->DiskStreamBufferFillBytes());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
749                      break;                      break;
750                  case fill_response_percentage:                  case fill_response_percentage:
751                      result.Add(pEngine->DiskStreamBufferFillPercentage());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
752                      break;                      break;
753                  default:                  default:
754                      throw LinuxSamplerException("Unknown fill response type");                      throw LinuxSamplerException("Unknown fill response type");
755              }              }
# Line 633  String LSCPServer::GetAvailableAudioOutp Line 765  String LSCPServer::GetAvailableAudioOutp
765      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
766      LSCPResultSet result;      LSCPResultSet result;
767      try {      try {
768            int n = AudioOutputDeviceFactory::AvailableDrivers().size();
769            result.Add(n);
770        }
771        catch (LinuxSamplerException e) {
772            result.Error(e);
773        }
774        return result.Produce();
775    }
776    
777    String LSCPServer::ListAvailableAudioOutputDrivers() {
778        dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
779        LSCPResultSet result;
780        try {
781          String s = AudioOutputDeviceFactory::AvailableDriversAsString();          String s = AudioOutputDeviceFactory::AvailableDriversAsString();
782          result.Add(s);          result.Add(s);
783      }      }
# Line 646  String LSCPServer::GetAvailableMidiInput Line 791  String LSCPServer::GetAvailableMidiInput
791      dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));      dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
792      LSCPResultSet result;      LSCPResultSet result;
793      try {      try {
794            int n = MidiInputDeviceFactory::AvailableDrivers().size();
795            result.Add(n);
796        }
797        catch (LinuxSamplerException e) {
798            result.Error(e);
799        }
800        return result.Produce();
801    }
802    
803    String LSCPServer::ListAvailableMidiInputDrivers() {
804        dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
805        LSCPResultSet result;
806        try {
807          String s = MidiInputDeviceFactory::AvailableDriversAsString();          String s = MidiInputDeviceFactory::AvailableDriversAsString();
808          result.Add(s);          result.Add(s);
809      }      }
# Line 704  String LSCPServer::GetAudioOutputDriverI Line 862  String LSCPServer::GetAudioOutputDriverI
862  }  }
863    
864  String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {  String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
865      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()));
866      LSCPResultSet result;      LSCPResultSet result;
867      try {      try {
868          DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);          DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
# Line 713  String LSCPServer::GetMidiInputDriverPar Line 871  String LSCPServer::GetMidiInputDriverPar
871          result.Add("MANDATORY",    pParameter->Mandatory());          result.Add("MANDATORY",    pParameter->Mandatory());
872          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
873          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
874          if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());          optional<String> oDepends       = pParameter->Depends();
875          if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());          optional<String> oDefault       = pParameter->Default(DependencyList);
876          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
877          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
878          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
879            if (oDepends)       result.Add("DEPENDS",       *oDepends);
880            if (oDefault)       result.Add("DEFAULT",       *oDefault);
881            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
882            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
883            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
884      }      }
885      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
886          result.Error(e);          result.Error(e);
# Line 726  String LSCPServer::GetMidiInputDriverPar Line 889  String LSCPServer::GetMidiInputDriverPar
889  }  }
890    
891  String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {  String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
892      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()));
893      LSCPResultSet result;      LSCPResultSet result;
894      try {      try {
895          DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);          DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
# Line 735  String LSCPServer::GetAudioOutputDriverP Line 898  String LSCPServer::GetAudioOutputDriverP
898          result.Add("MANDATORY",    pParameter->Mandatory());          result.Add("MANDATORY",    pParameter->Mandatory());
899          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
900          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
901          if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());          optional<String> oDepends       = pParameter->Depends();
902          if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());          optional<String> oDefault       = pParameter->Default(DependencyList);
903          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
904          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
905          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
906            if (oDepends)       result.Add("DEPENDS",       *oDepends);
907            if (oDefault)       result.Add("DEFAULT",       *oDefault);
908            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
909            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
910            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
911      }      }
912      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
913          result.Error(e);          result.Error(e);
# Line 816  String LSCPServer::GetAudioOutputDeviceI Line 984  String LSCPServer::GetAudioOutputDeviceI
984      LSCPResultSet result;      LSCPResultSet result;
985      try {      try {
986          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
987          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
988          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
989          result.Add("driver", pDevice->Driver());          result.Add("DRIVER", pDevice->Driver());
990          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
991          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
992          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 836  String LSCPServer::GetMidiInputDeviceInf Line 1004  String LSCPServer::GetMidiInputDeviceInf
1004      LSCPResultSet result;      LSCPResultSet result;
1005      try {      try {
1006          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1007            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1008          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1009          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          result.Add("DRIVER", pDevice->Driver());
         result.Add("driver", pDevice->Driver());  
1010          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1011          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1012          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 854  String LSCPServer::GetMidiInputPortInfo( Line 1022  String LSCPServer::GetMidiInputPortInfo(
1022      dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));      dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
1023      LSCPResultSet result;      LSCPResultSet result;
1024      try {      try {
1025            // get MIDI input device
1026          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1027            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1028          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1029          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");  
1030          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          // get MIDI port
1031          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1032          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1033          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();  
1034            // return the values of all MIDI port parameters
1035            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1036            std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1037          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
1038              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1039          }          }
# Line 877  String LSCPServer::GetAudioOutputChannel Line 1050  String LSCPServer::GetAudioOutputChannel
1050      try {      try {
1051          // get audio output device          // get audio output device
1052          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1053          if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1054          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1055    
1056          // get audio channel          // get audio channel
1057          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1058          if (!pChannel) throw LinuxSamplerException("Audio ouotput device does not have channel " + ToString(ChannelId) + ".");          if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1059    
1060          // return the values of all audio channel parameters          // return the values of all audio channel parameters
1061          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
# Line 901  String LSCPServer::GetMidiInputPortParam Line 1074  String LSCPServer::GetMidiInputPortParam
1074      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()));
1075      LSCPResultSet result;      LSCPResultSet result;
1076      try {      try {
1077          // get audio output device          // get MIDI input device
1078          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1079          if (!devices[DeviceId]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");
1080          MidiInputDevice* pDevice = devices[DeviceId];          MidiInputDevice* pDevice = devices[DeviceId];
1081    
1082          // get midi port          // get midi port
1083          MidiInputDevice::MidiInputPort* pPort = pDevice->GetPort(PortId);          MidiInputPort* pPort = pDevice->GetPort(PortId);
1084          if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");          if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");
1085    
1086          // get desired port parameter          // get desired port parameter
1087          std::map<String,DeviceCreationParameter*> parameters = pPort->DeviceParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1088          if (!parameters[ParameterName]) throw LinuxSamplerException("Midi port does not provice a parameters '" + ParameterName + "'.");          if (!parameters.count(ParameterName)) throw LinuxSamplerException("Midi port does not provide a parameter '" + ParameterName + "'.");
1089          DeviceCreationParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1090            
1091          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
1092          result.Add("TYPE",         pParameter->Type());          result.Add("TYPE",         pParameter->Type());
1093          result.Add("DESCRIPTION",  pParameter->Description());          result.Add("DESCRIPTION",  pParameter->Description());
1094          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
1095          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
1096          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1097          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1098          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1099      }      }
1100      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1101          result.Error(e);          result.Error(e);
# Line 936  String LSCPServer::GetAudioOutputChannel Line 1109  String LSCPServer::GetAudioOutputChannel
1109      try {      try {
1110          // get audio output device          // get audio output device
1111          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1112          if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1113          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1114    
1115          // get audio channel          // get audio channel
1116          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1117          if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");          if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1118    
1119          // get desired audio channel parameter          // get desired audio channel parameter
1120          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1121          if (!parameters[ParameterName]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");          if (!parameters.count(ParameterName)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");
1122          DeviceRuntimeParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1123    
1124          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
# Line 953  String LSCPServer::GetAudioOutputChannel Line 1126  String LSCPServer::GetAudioOutputChannel
1126          result.Add("DESCRIPTION",  pParameter->Description());          result.Add("DESCRIPTION",  pParameter->Description());
1127          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
1128          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
1129          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1130          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1131          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1132      }      }
1133      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1134          result.Error(e);          result.Error(e);
# Line 969  String LSCPServer::SetAudioOutputChannel Line 1142  String LSCPServer::SetAudioOutputChannel
1142      try {      try {
1143          // get audio output device          // get audio output device
1144          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1145          if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1146          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1147    
1148          // get audio channel          // get audio channel
1149          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1150          if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");          if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1151    
1152          // get desired audio channel parameter          // get desired audio channel parameter
1153          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1154          if (!parameters[ParamKey]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");          if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");
1155          DeviceRuntimeParameter* pParameter = parameters[ParamKey];          DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1156    
1157          // set new channel parameter value          // set new channel parameter value
# Line 995  String LSCPServer::SetAudioOutputDeviceP Line 1168  String LSCPServer::SetAudioOutputDeviceP
1168      LSCPResultSet result;      LSCPResultSet result;
1169      try {      try {
1170          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1171          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1172          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1173          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1174          if (!parameters[ParamKey]) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");          if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1175          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1176      }      }
1177      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1012  String LSCPServer::SetMidiInputDevicePar Line 1185  String LSCPServer::SetMidiInputDevicePar
1185      LSCPResultSet result;      LSCPResultSet result;
1186      try {      try {
1187          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1188          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1189          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1190          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1191          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");          if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1192          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1193      }      }
1194      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1028  String LSCPServer::SetMidiInputPortParam Line 1201  String LSCPServer::SetMidiInputPortParam
1201      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()));
1202      LSCPResultSet result;      LSCPResultSet result;
1203      try {      try {
1204            // get MIDI input device
1205          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1206            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1207          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1208          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");  
1209          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          // get MIDI port
1210          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1211          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1212          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");  
1213            // set port parameter value
1214            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1215            if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1216          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1217      }      }
1218      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1049  String LSCPServer::SetMidiInputPortParam Line 1227  String LSCPServer::SetMidiInputPortParam
1227   */   */
1228  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1229      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));
1230      return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?      LSCPResultSet result;
1231        try {
1232            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1233            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1234            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1235            if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1236            if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1237            pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1238        }
1239        catch (LinuxSamplerException e) {
1240             result.Error(e);
1241        }
1242        return result.Produce();
1243  }  }
1244    
1245  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
# Line 1057  String LSCPServer::SetAudioOutputDevice( Line 1247  String LSCPServer::SetAudioOutputDevice(
1247      LSCPResultSet result;      LSCPResultSet result;
1248      try {      try {
1249          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1250          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1251          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1252            if (!devices.count(AudioDeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1253          AudioOutputDevice* pDevice = devices[AudioDeviceId];          AudioOutputDevice* pDevice = devices[AudioDeviceId];
         if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));  
1254          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1255      }      }
1256      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1074  String LSCPServer::SetAudioOutputType(St Line 1264  String LSCPServer::SetAudioOutputType(St
1264      LSCPResultSet result;      LSCPResultSet result;
1265      try {      try {
1266          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1267          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1268          // Driver type name aliasing...          // Driver type name aliasing...
1269          if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1270          if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";                  if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1271          // Check if there's one audio output device already created          // Check if there's one audio output device already created
1272          // for the intended audio driver type (AudioOutputDriver)...          // for the intended audio driver type (AudioOutputDriver)...
1273          AudioOutputDevice *pDevice = NULL;          AudioOutputDevice *pDevice = NULL;
# Line 1111  String LSCPServer::SetMIDIInputPort(uint Line 1301  String LSCPServer::SetMIDIInputPort(uint
1301      LSCPResultSet result;      LSCPResultSet result;
1302      try {      try {
1303          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1304          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1305          pSamplerChannel->SetMidiInputPort(MIDIPort);          pSamplerChannel->SetMidiInputPort(MIDIPort);
1306      }      }
1307      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1125  String LSCPServer::SetMIDIInputChannel(u Line 1315  String LSCPServer::SetMIDIInputChannel(u
1315      LSCPResultSet result;      LSCPResultSet result;
1316      try {      try {
1317          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1318          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1319          pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1320      }      }
1321      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1322           result.Error(e);           result.Error(e);
# Line 1139  String LSCPServer::SetMIDIInputDevice(ui Line 1329  String LSCPServer::SetMIDIInputDevice(ui
1329      LSCPResultSet result;      LSCPResultSet result;
1330      try {      try {
1331          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1332          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1333          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1334            if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1335          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
         if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));  
1336          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1337      }      }
1338      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1156  String LSCPServer::SetMIDIInputType(Stri Line 1346  String LSCPServer::SetMIDIInputType(Stri
1346      LSCPResultSet result;      LSCPResultSet result;
1347      try {      try {
1348          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1349          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1350          // Driver type name aliasing...          // Driver type name aliasing...
1351          if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";          if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1352          // Check if there's one MIDI input device already created          // Check if there's one MIDI input device already created
1353          // for the intended MIDI driver type (MidiInputDriver)...          // for the intended MIDI driver type (MidiInputDriver)...
1354          MidiInputDevice *pDevice = NULL;          MidiInputDevice *pDevice = NULL;
# Line 1176  String LSCPServer::SetMIDIInputType(Stri Line 1366  String LSCPServer::SetMIDIInputType(Stri
1366              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1367              // Make it with at least one initial port.              // Make it with at least one initial port.
1368              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1369              parameters["ports"]->SetValue("1");              parameters["PORTS"]->SetValue("1");
1370          }          }
1371          // Must have a device...          // Must have a device...
1372          if (pDevice == NULL)          if (pDevice == NULL)
# Line 1199  String LSCPServer::SetMIDIInput(uint MID Line 1389  String LSCPServer::SetMIDIInput(uint MID
1389      LSCPResultSet result;      LSCPResultSet result;
1390      try {      try {
1391          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1392          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1393          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1394            if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1395          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1396          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);  
1397      }      }
1398      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1399           result.Error(e);           result.Error(e);
# Line 1215  String LSCPServer::SetMIDIInput(uint MID Line 1405  String LSCPServer::SetMIDIInput(uint MID
1405   * 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
1406   * particular sampler channel.   * particular sampler channel.
1407   */   */
1408  String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) {  String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1409      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1410      LSCPResultSet result;      LSCPResultSet result;
1411      try {      try {
1412          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1413          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1414          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1415          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1416          pEngine->Volume(Volume);          pEngineChannel->Volume(dVolume);
1417      }      }
1418      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1419           result.Error(e);           result.Error(e);
# Line 1232  String LSCPServer::SetVolume(double Volu Line 1422  String LSCPServer::SetVolume(double Volu
1422  }  }
1423    
1424  /**  /**
1425     * Will be called by the parser to mute/unmute particular sampler channel.
1426     */
1427    String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1428        dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1429        LSCPResultSet result;
1430        try {
1431            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1432            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1433    
1434            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1435            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1436    
1437            if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1438            else pEngineChannel->SetMute(1);
1439        } catch (LinuxSamplerException e) {
1440            result.Error(e);
1441        }
1442        return result.Produce();
1443    }
1444    
1445    /**
1446     * Will be called by the parser to solo particular sampler channel.
1447     */
1448    String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1449        dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1450        LSCPResultSet result;
1451        try {
1452            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1453            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1454    
1455            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1456            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1457    
1458            bool oldSolo = pEngineChannel->GetSolo();
1459            bool hadSoloChannel = HasSoloChannel();
1460            
1461            pEngineChannel->SetSolo(bSolo);
1462            
1463            if(!oldSolo && bSolo) {
1464                if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1465                if(!hadSoloChannel) MuteNonSoloChannels();
1466            }
1467            
1468            if(oldSolo && !bSolo) {
1469                if(!HasSoloChannel()) UnmuteChannels();
1470                else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1471            }
1472        } catch (LinuxSamplerException e) {
1473            result.Error(e);
1474        }
1475        return result.Produce();
1476    }
1477    
1478    /**
1479     * Determines whether there is at least one solo channel in the channel list.
1480     *
1481     * @returns true if there is at least one solo channel in the channel list,
1482     * false otherwise.
1483     */
1484    bool LSCPServer::HasSoloChannel() {
1485        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1486        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1487        for (; iter != channels.end(); iter++) {
1488            EngineChannel* c = iter->second->GetEngineChannel();
1489            if(c && c->GetSolo()) return true;
1490        }
1491    
1492        return false;
1493    }
1494    
1495    /**
1496     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1497     * with -1 which indicates that they are muted because of the presence
1498     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1499     * when there are no solo channels left.
1500     */
1501    void LSCPServer::MuteNonSoloChannels() {
1502        dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1503        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1504        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1505        for (; iter != channels.end(); iter++) {
1506            EngineChannel* c = iter->second->GetEngineChannel();
1507            if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1508        }
1509    }
1510    
1511    /**
1512     * Unmutes all channels that are muted because of the presence
1513     * of a solo channel(s).
1514     */
1515    void  LSCPServer::UnmuteChannels() {
1516        dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1517        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1518        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1519        for (; iter != channels.end(); iter++) {
1520            EngineChannel* c = iter->second->GetEngineChannel();
1521            if(c && c->GetMute() == -1) c->SetMute(0);
1522        }
1523    }
1524    
1525    /**
1526   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
1527   */   */
1528  String LSCPServer::ResetChannel(uint uiSamplerChannel) {  String LSCPServer::ResetChannel(uint uiSamplerChannel) {
# Line 1239  String LSCPServer::ResetChannel(uint uiS Line 1530  String LSCPServer::ResetChannel(uint uiS
1530      LSCPResultSet result;      LSCPResultSet result;
1531      try {      try {
1532          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1533          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1534          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1535          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1536          pEngine->Reset();          pEngineChannel->Reset();
1537      }      }
1538      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1539           result.Error(e);           result.Error(e);
# Line 1251  String LSCPServer::ResetChannel(uint uiS Line 1542  String LSCPServer::ResetChannel(uint uiS
1542  }  }
1543    
1544  /**  /**
1545     * Will be called by the parser to reset the whole sampler.
1546     */
1547    String LSCPServer::ResetSampler() {
1548        dmsg(2,("LSCPServer: ResetSampler()\n"));
1549        pSampler->Reset();
1550        LSCPResultSet result;
1551        return result.Produce();
1552    }
1553    
1554    /**
1555     * Will be called by the parser to return general informations about this
1556     * sampler.
1557     */
1558    String LSCPServer::GetServerInfo() {
1559        dmsg(2,("LSCPServer: GetServerInfo()\n"));
1560        LSCPResultSet result;
1561        result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");
1562        result.Add("VERSION", VERSION);
1563        result.Add("PROTOCOL_VERSION", "1.0");
1564        return result.Produce();
1565    }
1566    
1567    /**
1568     * Will be called by the parser to return the current number of all active voices.
1569     */
1570    String LSCPServer::GetTotalVoiceCount() {
1571        dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
1572        LSCPResultSet result;
1573        result.Add(pSampler->GetVoiceCount());
1574        return result.Produce();
1575    }
1576    
1577    /**
1578     * Will be called by the parser to return the maximum number of voices.
1579     */
1580    String LSCPServer::GetTotalVoiceCountMax() {
1581        dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
1582        LSCPResultSet result;
1583        result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);
1584        return result.Produce();
1585    }
1586    
1587    /**
1588   * 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
1589   * server for receiving event messages.   * server for receiving event messages.
1590   */   */
# Line 1276  String LSCPServer::UnsubscribeNotificati Line 1610  String LSCPServer::UnsubscribeNotificati
1610      return result.Produce();      return result.Produce();
1611  }  }
1612    
1613    static int select_callback(void * lscpResultSet, int argc,
1614  // Instrument loader constructor.                          char **argv, char **azColName)
 LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)  
     : Thread(false, 0, -4)  
1615  {  {
1616      this->pEngine = pEngine;      LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;
1617      this->Filename = Filename;      resultSet->Add(argc, argv);
1618      this->uiInstrument = uiInstrument;      return 0;
1619  }  }
1620    
1621  // Instrument loader process.  String LSCPServer::QueryDatabase(String query) {
1622  int LSCPLoadInstrument::Main()      LSCPResultSet result;
1623  {  #if HAVE_SQLITE3
1624      try {      char* zErrMsg = NULL;
1625          pEngine->LoadInstrument(Filename.c_str(), uiInstrument);      sqlite3 *db;
1626        String selectStr = "SELECT " + query;
1627    
1628        int rc = sqlite3_open("linuxsampler.db", &db);
1629        if (rc == SQLITE_OK)
1630        {
1631                rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);
1632        }
1633        if ( rc != SQLITE_OK )
1634        {
1635                result.Error(String(zErrMsg), rc);
1636      }      }
1637        sqlite3_close(db);
1638    #else
1639        result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);
1640    #endif
1641        return result.Produce();
1642    }
1643    
1644    /**
1645     * Will be called by the parser to enable or disable echo mode; if echo
1646     * mode is enabled, all commands from the client will (immediately) be
1647     * echoed back to the client.
1648     */
1649    String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
1650        dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
1651        LSCPResultSet result;
1652        try {
1653            if      (boolean_value == 0) pSession->bVerbose = false;
1654            else if (boolean_value == 1) pSession->bVerbose = true;
1655            else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1");
1656        }
1657      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1658          e.PrintMessage();           result.Error(e);
1659      }      }
1660        return result.Produce();
     // Always re-enable the engine.  
     pEngine->Enable();  
   
     // FIXME: Shoot ourselves on the foot?  
     delete this;  
1661  }  }

Legend:
Removed from v.185  
changed lines
  Added in v.836

  ViewVC Help
Powered by ViewVC