/[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 184 by senkov, Wed Jul 7 02:00:37 2004 UTC revision 705 by schoenebeck, Wed Jul 20 21:43:23 2005 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        hSocket = -1;
76    }
77    
78    LSCPServer::~LSCPServer() {
79        if (hSocket >= 0) close(hSocket);
80    }
81    
82    /**
83     * Blocks the calling thread until the LSCP Server is initialized and
84     * accepting socket connections, if the server is already initialized then
85     * this method will return immediately.
86     * @param TimeoutSeconds     - optional: max. wait time in seconds
87     *                             (default: 0s)
88     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
89     *                             (default: 0ns)
90     * @returns  0 on success, a value less than 0 if timeout exceeded
91     */
92    int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
93        return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
94  }  }
95    
96  int LSCPServer::Main() {  int LSCPServer::Main() {
97      int hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
98      if (hSocket < 0) {      if (hSocket < 0) {
99          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
100          //return -1;          //return -1;
101          exit(EXIT_FAILURE);          exit(EXIT_FAILURE);
102      }      }
103    
     SocketAddress.sin_family      = AF_INET;  
     SocketAddress.sin_port        = htons(LSCP_PORT);  
     SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);  
   
104      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
105          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...";
106          close(hSocket);          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
107          //return -1;              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
108          exit(EXIT_FAILURE);                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
109                        std::cerr << "gave up!" << std::endl;
110                        close(hSocket);
111                        //return -1;
112                        exit(EXIT_FAILURE);
113                    }
114                    else sleep(1); // sleep 1s
115                }
116                else break; // success
117            }
118      }      }
119    
120      listen(hSocket, 1);      listen(hSocket, 1);
121      dmsg(1,("LSCPServer: Server running.\n")); // server running      Initialized.Set(true);
122    
123      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
124      sockaddr_in client;      sockaddr_in client;
125      int length = sizeof(client);      int length = sizeof(client);
     struct timeval tv;  
     tv.tv_sec = 30;  
     tv.tv_usec = 0;  
126      FD_ZERO(&fdSet);      FD_ZERO(&fdSet);
127      FD_SET(hSocket, &fdSet);      FD_SET(hSocket, &fdSet);
128      int maxSessions = hSocket;      int maxSessions = hSocket;
   
     // Parser initialization  
     yyparse_param_t yyparse_param;  
     yyparse_param.pServer = this;  
129    
130      while (true) {      while (true) {
131          fd_set selectSet = fdSet;          fd_set selectSet = fdSet;
132          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &tv);          int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);
133          if (retval == 0)          if (retval == 0)
134                  continue; //Nothing in 30 seconds, try again                  continue; //Nothing try again
135          if (retval == -1) {          if (retval == -1) {
136                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
137                  close(hSocket);                  close(hSocket);
138                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
139          }          }
140            
141          //Accept new connections now (if any)          //Accept new connections now (if any)
142          if (FD_ISSET(hSocket, &selectSet)) {          if (FD_ISSET(hSocket, &selectSet)) {
143                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
# Line 121  int LSCPServer::Main() { Line 151  int LSCPServer::Main() {
151                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
152                  }                  }
153    
154                  hSessions.push_back(socket);                  // Parser initialization
155                    yyparse_param_t yyparse_param;
156                    yyparse_param.pServer  = this;
157                    yyparse_param.hSession = socket;
158    
159                    Sessions.push_back(yyparse_param);
160                  FD_SET(socket, &fdSet);                  FD_SET(socket, &fdSet);
161                  if (socket > maxSessions)                  if (socket > maxSessions)
162                          maxSessions = socket;                          maxSessions = socket;
# Line 131  int LSCPServer::Main() { Line 166  int LSCPServer::Main() {
166          }          }
167    
168          //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.
169          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++) {
170                  if (FD_ISSET(*iter, &selectSet)) {      //Was it this socket?                  if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?
171                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?
172                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
173                                  yylex_init(&yyparse_param.pScanner);                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
174                                  currentSocket = *iter;  //a hack                                  restart(NULL, dummy); // restart the 'scanner'
175                                  int result = yyparse(&yyparse_param);                                  currentSocket = (*iter).hSession;  //a hack
176                                    dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
177                                    if ((*iter).bVerbose) { // if echo mode enabled
178                                        AnswerClient(bufferedCommands[currentSocket]);
179                                    }
180                                    int result = yyparse(&(*iter));
181                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
182                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
183                                  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 186  int LSCPServer::Main() {
186                          }                          }
187                          //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.
188                          //we'll be back if there is data.                          //we'll be back if there is data.
189                          break;                          break;
190                  }                  }
191          }          }
192    
193            // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
194            {
195                std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
196                std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
197                std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
198                for (; itEngineChannel != itEnd; ++itEngineChannel) {
199                    if ((*itEngineChannel)->StatusChanged()) {
200                        SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));
201                    }
202                }
203            }
204    
205          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
206          NotifyBufferMutex.Lock();          NotifyBufferMutex.Lock();
207          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
# Line 158  int LSCPServer::Main() { Line 210  int LSCPServer::Main() {
210          }          }
211          NotifyBufferMutex.Unlock();          NotifyBufferMutex.Unlock();
212      }      }
     //It will never get here anyway  
     //yylex_destroy(yyparse_param.pScanner);  
213  }  }
214    
215  void LSCPServer::CloseConnection( std::vector<int>::iterator iter ) {  void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
216          int socket = *iter;          int socket = (*iter).hSession;
217          dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));          dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
218          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
219          hSessions.erase(iter);          Sessions.erase(iter);
220          FD_CLR(socket,  &fdSet);          FD_CLR(socket,  &fdSet);
221          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
222          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 230  void LSCPServer::CloseConnection( std::v
230          NotifyMutex.Unlock();          NotifyMutex.Unlock();
231  }  }
232    
233    int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
234            int subs = 0;
235            SubscriptionMutex.Lock();
236            for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
237                            iter != events.end(); iter++)
238            {
239                    subs += eventSubscriptions.count(*iter);
240            }
241            SubscriptionMutex.Unlock();
242            return subs;
243    }
244    
245  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
246          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
247          if (eventSubscriptions.count(event.GetType()) == 0) {          if (eventSubscriptions.count(event.GetType()) == 0) {
# Line 230  extern int GetLSCPCommand( void *buf, in Line 292  extern int GetLSCPCommand( void *buf, in
292   * If command is read, it will return true. Otherwise false is returned.   * If command is read, it will return true. Otherwise false is returned.
293   * 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.
294   */   */
295  bool LSCPServer::GetLSCPCommand( std::vector<int>::iterator iter ) {  bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
296          int socket = *iter;          int socket = (*iter).hSession;
297          char c;          char c;
298          int i = 0;          int i = 0;
299          while (true) {          while (true) {
# Line 241  bool LSCPServer::GetLSCPCommand( std::ve Line 303  bool LSCPServer::GetLSCPCommand( std::ve
303                          break;                          break;
304                  }                  }
305                  if (result == 1) {                  if (result == 1) {
306                          if (c == '\r')                          if (c == '\r')
307                                  continue; //Ignore CR                                  continue; //Ignore CR
308                          if (c == '\n') {                          if (c == '\n') {
309                                  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 330  bool LSCPServer::GetLSCPCommand( std::ve
330                                          break;                                          break;
331                                  case EAGAIN:                                  case EAGAIN:
332                                          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"));
333                                          break;                                          break;
334                                  case EINTR:                                  case EINTR:
335                                          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"));
336                                          break;                                          break;
337                                  case EFAULT:                                  case EFAULT:
338                                          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"));
339                                          break;                                          break;
340                                  case EINVAL:                                  case EINVAL:
341                                          dmsg(2,("LSCPScanner: Invalid argument passed.\n"));                                          dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
342                                          break;                                          break;
343                                  case ENOMEM:                                  case ENOMEM:
344                                          dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));                                          dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
345                                          break;                                          break;
346                                  default:                                  default:
347                                          dmsg(2,("LSCPScanner: Unknown recv() error.\n"));                                          dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
348                                          break;                                          break;
349                          }                          }
350                          CloseConnection(iter);                          CloseConnection(iter);
351                          break;                          break;
352                  }                  }
# Line 376  String LSCPServer::DestroyAudioOutputDev Line 438  String LSCPServer::DestroyAudioOutputDev
438      LSCPResultSet result;      LSCPResultSet result;
439      try {      try {
440          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
441          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) + ".");
442          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
443          pSampler->DestroyAudioOutputDevice(pDevice);          pSampler->DestroyAudioOutputDevice(pDevice);
444      }      }
# Line 391  String LSCPServer::DestroyMidiInputDevic Line 453  String LSCPServer::DestroyMidiInputDevic
453      LSCPResultSet result;      LSCPResultSet result;
454      try {      try {
455          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
456            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
457          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
         if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");  
458          pSampler->DestroyMidiInputDevice(pDevice);          pSampler->DestroyMidiInputDevice(pDevice);
459      }      }
460      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 409  String LSCPServer::LoadInstrument(String Line 471  String LSCPServer::LoadInstrument(String
471      LSCPResultSet result;      LSCPResultSet result;
472      try {      try {
473          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
474          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
475          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
476          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet");
477          if (pSamplerChannel->GetAudioOutputDevice() == NULL)          if (!pSamplerChannel->GetAudioOutputDevice())
478              throw LinuxSamplerException("No audio output device on channel");              throw LinuxSamplerException("No audio output device connected to sampler channel");
479          if (bBackground) {          if (bBackground) {
480              LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument);              InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel);
481              pLoadInstrument->StartThread();          }
482            else {
483                // tell the engine channel which instrument to load
484                pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
485                // actually start to load the instrument (blocks until completed)
486                pEngineChannel->LoadInstrument();
487          }          }
         else pEngine->LoadInstrument(Filename.c_str(), uiInstrument);  
488      }      }
489      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
490           result.Error(e);           result.Error(e);
# Line 427  String LSCPServer::LoadInstrument(String Line 493  String LSCPServer::LoadInstrument(String
493  }  }
494    
495  /**  /**
496   * 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
497     * sampler channel.
498   */   */
499  String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) {  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
500      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));
501      LSCPResultSet result;      LSCPResultSet result;
502      try {      try {
         Engine::type_t type;  
         if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig;  
         else throw LinuxSamplerException("Unknown engine type");  
503          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
504          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
505          pSamplerChannel->LoadEngine(type);          LockRTNotify();
506            pSamplerChannel->SetEngineType(EngineName);
507            if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
508            UnlockRTNotify();
509      }      }
510      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
511           result.Error(e);           result.Error(e);
# Line 457  String LSCPServer::GetChannels() { Line 524  String LSCPServer::GetChannels() {
524  }  }
525    
526  /**  /**
527     * Will be called by the parser to get the list of sampler channels.
528     */
529    String LSCPServer::ListChannels() {
530        dmsg(2,("LSCPServer: ListChannels()\n"));
531        String list;
532        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
533        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
534        for (; iter != channels.end(); iter++) {
535            if (list != "") list += ",";
536            list += ToString(iter->first);
537        }
538        LSCPResultSet result;
539        result.Add(list);
540        return result.Produce();
541    }
542    
543    /**
544   * Will be called by the parser to add a sampler channel.   * Will be called by the parser to add a sampler channel.
545   */   */
546  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
# Line 472  String LSCPServer::AddChannel() { Line 556  String LSCPServer::AddChannel() {
556  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
557      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
558      LSCPResultSet result;      LSCPResultSet result;
559        LockRTNotify();
560      pSampler->RemoveSamplerChannel(uiSamplerChannel);      pSampler->RemoveSamplerChannel(uiSamplerChannel);
561        UnlockRTNotify();
562      return result.Produce();      return result.Produce();
563  }  }
564    
565  /**  /**
566   * 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.
567   */   */
568  String LSCPServer::GetAvailableEngines() {  String LSCPServer::GetAvailableEngines() {
569      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
570      LSCPResultSet result("GigEngine");      LSCPResultSet result("1");
571        return result.Produce();
572    }
573    
574    /**
575     * Will be called by the parser to get a list of all available engines.
576     */
577    String LSCPServer::ListAvailableEngines() {
578        dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
579        LSCPResultSet result("\'GIG\'");
580      return result.Produce();      return result.Produce();
581  }  }
582    
583  /**  /**
584   * Will be called by the parser to get descriptions for a particular engine.   * Will be called by the parser to get descriptions for a particular
585     * sampler engine.
586   */   */
587  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
588      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
589      LSCPResultSet result;      LSCPResultSet result;
590      try {      try {
591          if ((EngineName == "GigEngine") || (EngineName == "gig")) {          Engine* pEngine = EngineFactory::Create(EngineName);
592              Engine* pEngine = new LinuxSampler::gig::Engine;          result.Add("DESCRIPTION", pEngine->Description());
593              result.Add(pEngine->Description());          result.Add("VERSION",     pEngine->Version());
594              result.Add(pEngine->Version());          EngineFactory::Destroy(pEngine);
             delete pEngine;  
         }  
         else throw LinuxSamplerException("Unknown engine type");  
595      }      }
596      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
597           result.Error(e);           result.Error(e);
# Line 515  String LSCPServer::GetChannelInfo(uint u Line 608  String LSCPServer::GetChannelInfo(uint u
608      LSCPResultSet result;      LSCPResultSet result;
609      try {      try {
610          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
611          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
612          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
613    
614          //Defaults values          //Defaults values
615          String EngineName = "NONE";          String EngineName = "NONE";
616          float Volume = 0;          float Volume = 0.0f;
617          String InstrumentFileName = "NONE";          String InstrumentFileName = "NONE";
618            String InstrumentName = "NONE";
619          int InstrumentIndex = -1;          int InstrumentIndex = -1;
620          int InstrumentStatus = -1;          int InstrumentStatus = -1;
621            int AudioOutputChannels = 0;
622          if (pEngine) {          String AudioRouting;
623              EngineName =  pEngine->EngineName();          int Mute = 0;
624              Volume = pEngine->Volume();          bool Solo = false;
625              InstrumentStatus = pEngine->InstrumentStatus();  
626              InstrumentIndex = pEngine->InstrumentIndex();          if (pEngineChannel) {
627              if (InstrumentIndex != -1)              EngineName          = pEngineChannel->EngineName();
628                  InstrumentFileName = pEngine->InstrumentFileName();              AudioOutputChannels = pEngineChannel->Channels();
629                Volume              = pEngineChannel->Volume();
630                InstrumentStatus    = pEngineChannel->InstrumentStatus();
631                InstrumentIndex     = pEngineChannel->InstrumentIndex();
632                if (InstrumentIndex != -1) {
633                    InstrumentFileName = pEngineChannel->InstrumentFileName();
634                    InstrumentName     = pEngineChannel->InstrumentName();
635                }
636                for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
637                    if (AudioRouting != "") AudioRouting += ",";
638                    AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
639                }
640                Mute = pEngineChannel->GetMute();
641                Solo = pEngineChannel->GetSolo();
642          }          }
643    
644          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
# Line 539  String LSCPServer::GetChannelInfo(uint u Line 646  String LSCPServer::GetChannelInfo(uint u
646    
647          //Some not-so-hardcoded stuff to make GUI look good          //Some not-so-hardcoded stuff to make GUI look good
648          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
649          result.Add("AUDIO_OUTPUT_CHANNELS", "2");          result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
650          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
651    
652          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
653          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
654          result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
655            else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
656    
657          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
658          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
659            result.Add("INSTRUMENT_NAME", InstrumentName);
660          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
661            result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
662            result.Add("SOLO", Solo);
663      }      }
664      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
665           result.Error(e);           result.Error(e);
# Line 565  String LSCPServer::GetVoiceCount(uint ui Line 676  String LSCPServer::GetVoiceCount(uint ui
676      LSCPResultSet result;      LSCPResultSet result;
677      try {      try {
678          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
679          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
680          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
681          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel");
682          result.Add(pEngine->VoiceCount());          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
683            result.Add(pEngineChannel->GetEngine()->VoiceCount());
684      }      }
685      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
686           result.Error(e);           result.Error(e);
# Line 585  String LSCPServer::GetStreamCount(uint u Line 697  String LSCPServer::GetStreamCount(uint u
697      LSCPResultSet result;      LSCPResultSet result;
698      try {      try {
699          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
700          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
701          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
702          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
703          result.Add(pEngine->DiskStreamCount());          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
704            result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
705      }      }
706      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
707           result.Error(e);           result.Error(e);
# Line 605  String LSCPServer::GetBufferFill(fill_re Line 718  String LSCPServer::GetBufferFill(fill_re
718      LSCPResultSet result;      LSCPResultSet result;
719      try {      try {
720          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
721          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
722          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
723          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
724          if (!pEngine->DiskStreamSupported())          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
725              result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
726          else {          else {
727              switch (ResponseType) {              switch (ResponseType) {
728                  case fill_response_bytes:                  case fill_response_bytes:
729                      result.Add(pEngine->DiskStreamBufferFillBytes());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
730                      break;                      break;
731                  case fill_response_percentage:                  case fill_response_percentage:
732                      result.Add(pEngine->DiskStreamBufferFillPercentage());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
733                      break;                      break;
734                  default:                  default:
735                      throw LinuxSamplerException("Unknown fill response type");                      throw LinuxSamplerException("Unknown fill response type");
736              }              }
# Line 633  String LSCPServer::GetAvailableAudioOutp Line 746  String LSCPServer::GetAvailableAudioOutp
746      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
747      LSCPResultSet result;      LSCPResultSet result;
748      try {      try {
749            int n = AudioOutputDeviceFactory::AvailableDrivers().size();
750            result.Add(n);
751        }
752        catch (LinuxSamplerException e) {
753            result.Error(e);
754        }
755        return result.Produce();
756    }
757    
758    String LSCPServer::ListAvailableAudioOutputDrivers() {
759        dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
760        LSCPResultSet result;
761        try {
762          String s = AudioOutputDeviceFactory::AvailableDriversAsString();          String s = AudioOutputDeviceFactory::AvailableDriversAsString();
763          result.Add(s);          result.Add(s);
764      }      }
# Line 646  String LSCPServer::GetAvailableMidiInput Line 772  String LSCPServer::GetAvailableMidiInput
772      dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));      dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
773      LSCPResultSet result;      LSCPResultSet result;
774      try {      try {
775            int n = MidiInputDeviceFactory::AvailableDrivers().size();
776            result.Add(n);
777        }
778        catch (LinuxSamplerException e) {
779            result.Error(e);
780        }
781        return result.Produce();
782    }
783    
784    String LSCPServer::ListAvailableMidiInputDrivers() {
785        dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
786        LSCPResultSet result;
787        try {
788          String s = MidiInputDeviceFactory::AvailableDriversAsString();          String s = MidiInputDeviceFactory::AvailableDriversAsString();
789          result.Add(s);          result.Add(s);
790      }      }
# Line 704  String LSCPServer::GetAudioOutputDriverI Line 843  String LSCPServer::GetAudioOutputDriverI
843  }  }
844    
845  String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {  String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
846      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()));
847      LSCPResultSet result;      LSCPResultSet result;
848      try {      try {
849          DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);          DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
# Line 713  String LSCPServer::GetMidiInputDriverPar Line 852  String LSCPServer::GetMidiInputDriverPar
852          result.Add("MANDATORY",    pParameter->Mandatory());          result.Add("MANDATORY",    pParameter->Mandatory());
853          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
854          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
855          if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());          optional<String> oDepends       = pParameter->Depends();
856          if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());          optional<String> oDefault       = pParameter->Default(DependencyList);
857          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
858          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
859          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
860            if (oDepends)       result.Add("DEPENDS",       *oDepends);
861            if (oDefault)       result.Add("DEFAULT",       *oDefault);
862            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
863            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
864            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
865      }      }
866      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
867          result.Error(e);          result.Error(e);
# Line 726  String LSCPServer::GetMidiInputDriverPar Line 870  String LSCPServer::GetMidiInputDriverPar
870  }  }
871    
872  String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {  String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
873      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()));
874      LSCPResultSet result;      LSCPResultSet result;
875      try {      try {
876          DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);          DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
# Line 735  String LSCPServer::GetAudioOutputDriverP Line 879  String LSCPServer::GetAudioOutputDriverP
879          result.Add("MANDATORY",    pParameter->Mandatory());          result.Add("MANDATORY",    pParameter->Mandatory());
880          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
881          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
882          if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());          optional<String> oDepends       = pParameter->Depends();
883          if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());          optional<String> oDefault       = pParameter->Default(DependencyList);
884          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
885          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
886          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
887            if (oDepends)       result.Add("DEPENDS",       *oDepends);
888            if (oDefault)       result.Add("DEFAULT",       *oDefault);
889            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
890            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
891            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
892      }      }
893      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
894          result.Error(e);          result.Error(e);
# Line 816  String LSCPServer::GetAudioOutputDeviceI Line 965  String LSCPServer::GetAudioOutputDeviceI
965      LSCPResultSet result;      LSCPResultSet result;
966      try {      try {
967          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
968          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) + ".");
969          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
970          result.Add("driver", pDevice->Driver());          result.Add("DRIVER", pDevice->Driver());
971          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
972          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
973          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 836  String LSCPServer::GetMidiInputDeviceInf Line 985  String LSCPServer::GetMidiInputDeviceInf
985      LSCPResultSet result;      LSCPResultSet result;
986      try {      try {
987          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
988            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
989          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
990          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          result.Add("DRIVER", pDevice->Driver());
         result.Add("driver", pDevice->Driver());  
991          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
992          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
993          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 854  String LSCPServer::GetMidiInputPortInfo( Line 1003  String LSCPServer::GetMidiInputPortInfo(
1003      dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));      dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
1004      LSCPResultSet result;      LSCPResultSet result;
1005      try {      try {
1006            // get MIDI input device
1007          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1008            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1009          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1010          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");  
1011          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          // get MIDI port
1012          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1013          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1014          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();  
1015            // return the values of all MIDI port parameters
1016            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1017            std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1018          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
1019              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1020          }          }
# Line 877  String LSCPServer::GetAudioOutputChannel Line 1031  String LSCPServer::GetAudioOutputChannel
1031      try {      try {
1032          // get audio output device          // get audio output device
1033          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1034          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) + ".");
1035          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1036    
1037          // get audio channel          // get audio channel
1038          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1039          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) + ".");
1040    
1041          // return the values of all audio channel parameters          // return the values of all audio channel parameters
1042          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
# Line 897  String LSCPServer::GetAudioOutputChannel Line 1051  String LSCPServer::GetAudioOutputChannel
1051      return result.Produce();      return result.Produce();
1052  }  }
1053    
1054    String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
1055        dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
1056        LSCPResultSet result;
1057        try {
1058            // get MIDI input device
1059            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1060            if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");
1061            MidiInputDevice* pDevice = devices[DeviceId];
1062    
1063            // get midi port
1064            MidiInputPort* pPort = pDevice->GetPort(PortId);
1065            if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");
1066    
1067            // get desired port parameter
1068            std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1069            if (!parameters.count(ParameterName)) throw LinuxSamplerException("Midi port does not provide a parameter '" + ParameterName + "'.");
1070            DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1071    
1072            // return all fields of this audio channel parameter
1073            result.Add("TYPE",         pParameter->Type());
1074            result.Add("DESCRIPTION",  pParameter->Description());
1075            result.Add("FIX",          pParameter->Fix());
1076            result.Add("MULTIPLICITY", pParameter->Multiplicity());
1077            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1078            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1079            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1080        }
1081        catch (LinuxSamplerException e) {
1082            result.Error(e);
1083        }
1084        return result.Produce();
1085    }
1086    
1087  String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {  String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
1088      dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));      dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
1089      LSCPResultSet result;      LSCPResultSet result;
1090      try {      try {
1091          // get audio output device          // get audio output device
1092          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1093          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) + ".");
1094          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1095    
1096          // get audio channel          // get audio channel
1097          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1098          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) + ".");
1099    
1100          // get desired audio channel parameter          // get desired audio channel parameter
1101          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1102          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 + "'.");
1103          DeviceRuntimeParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1104    
1105          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
# Line 920  String LSCPServer::GetAudioOutputChannel Line 1107  String LSCPServer::GetAudioOutputChannel
1107          result.Add("DESCRIPTION",  pParameter->Description());          result.Add("DESCRIPTION",  pParameter->Description());
1108          result.Add("FIX",          pParameter->Fix());          result.Add("FIX",          pParameter->Fix());
1109          result.Add("MULTIPLICITY", pParameter->Multiplicity());          result.Add("MULTIPLICITY", pParameter->Multiplicity());
1110          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());          if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1111          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1112          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1113      }      }
1114      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1115          result.Error(e);          result.Error(e);
# Line 936  String LSCPServer::SetAudioOutputChannel Line 1123  String LSCPServer::SetAudioOutputChannel
1123      try {      try {
1124          // get audio output device          // get audio output device
1125          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1126          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) + ".");
1127          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1128    
1129          // get audio channel          // get audio channel
1130          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1131          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) + ".");
1132    
1133          // get desired audio channel parameter          // get desired audio channel parameter
1134          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1135          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 + "'.");
1136          DeviceRuntimeParameter* pParameter = parameters[ParamKey];          DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1137    
1138          // set new channel parameter value          // set new channel parameter value
# Line 962  String LSCPServer::SetAudioOutputDeviceP Line 1149  String LSCPServer::SetAudioOutputDeviceP
1149      LSCPResultSet result;      LSCPResultSet result;
1150      try {      try {
1151          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1152          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) + ".");
1153          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1154          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1155          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 + "'");
1156          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1157      }      }
1158      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 979  String LSCPServer::SetMidiInputDevicePar Line 1166  String LSCPServer::SetMidiInputDevicePar
1166      LSCPResultSet result;      LSCPResultSet result;
1167      try {      try {
1168          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1169          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) + ".");
1170          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1171          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1172          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 + "'");
1173          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1174      }      }
1175      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 995  String LSCPServer::SetMidiInputPortParam Line 1182  String LSCPServer::SetMidiInputPortParam
1182      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()));
1183      LSCPResultSet result;      LSCPResultSet result;
1184      try {      try {
1185            // get MIDI input device
1186          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1187            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1188          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1189          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");  
1190          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          // get MIDI port
1191          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1192          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1193          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");  
1194            // set port parameter value
1195            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1196            if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1197          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1198      }      }
1199      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1016  String LSCPServer::SetMidiInputPortParam Line 1208  String LSCPServer::SetMidiInputPortParam
1208   */   */
1209  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1210      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));
1211      return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?      LSCPResultSet result;
1212        try {
1213            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1214            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1215            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1216            if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1217            if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1218            pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1219        }
1220        catch (LinuxSamplerException e) {
1221             result.Error(e);
1222        }
1223        return result.Produce();
1224  }  }
1225    
1226  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
# Line 1024  String LSCPServer::SetAudioOutputDevice( Line 1228  String LSCPServer::SetAudioOutputDevice(
1228      LSCPResultSet result;      LSCPResultSet result;
1229      try {      try {
1230          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1231          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1232          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1233            if (!devices.count(AudioDeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1234          AudioOutputDevice* pDevice = devices[AudioDeviceId];          AudioOutputDevice* pDevice = devices[AudioDeviceId];
         if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));  
1235          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1236      }      }
1237      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1041  String LSCPServer::SetAudioOutputType(St Line 1245  String LSCPServer::SetAudioOutputType(St
1245      LSCPResultSet result;      LSCPResultSet result;
1246      try {      try {
1247          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1248          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1249          // Driver type name aliasing...          // Driver type name aliasing...
1250          if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1251          if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";                  if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1252          // Check if there's one audio output device already created          // Check if there's one audio output device already created
1253          // for the intended audio driver type (AudioOutputDriver)...          // for the intended audio driver type (AudioOutputDriver)...
1254          AudioOutputDevice *pDevice = NULL;          AudioOutputDevice *pDevice = NULL;
# Line 1078  String LSCPServer::SetMIDIInputPort(uint Line 1282  String LSCPServer::SetMIDIInputPort(uint
1282      LSCPResultSet result;      LSCPResultSet result;
1283      try {      try {
1284          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1285          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1286          pSamplerChannel->SetMidiInputPort(MIDIPort);          pSamplerChannel->SetMidiInputPort(MIDIPort);
1287      }      }
1288      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1092  String LSCPServer::SetMIDIInputChannel(u Line 1296  String LSCPServer::SetMIDIInputChannel(u
1296      LSCPResultSet result;      LSCPResultSet result;
1297      try {      try {
1298          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1299          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1300          pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1301      }      }
1302      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1303           result.Error(e);           result.Error(e);
# Line 1106  String LSCPServer::SetMIDIInputDevice(ui Line 1310  String LSCPServer::SetMIDIInputDevice(ui
1310      LSCPResultSet result;      LSCPResultSet result;
1311      try {      try {
1312          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1313          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1314          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1315            if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1316          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
         if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));  
1317          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1318      }      }
1319      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 1123  String LSCPServer::SetMIDIInputType(Stri Line 1327  String LSCPServer::SetMIDIInputType(Stri
1327      LSCPResultSet result;      LSCPResultSet result;
1328      try {      try {
1329          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1330          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1331          // Driver type name aliasing...          // Driver type name aliasing...
1332          if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";          if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1333          // Check if there's one MIDI input device already created          // Check if there's one MIDI input device already created
1334          // for the intended MIDI driver type (MidiInputDriver)...          // for the intended MIDI driver type (MidiInputDriver)...
1335          MidiInputDevice *pDevice = NULL;          MidiInputDevice *pDevice = NULL;
# Line 1143  String LSCPServer::SetMIDIInputType(Stri Line 1347  String LSCPServer::SetMIDIInputType(Stri
1347              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1348              // Make it with at least one initial port.              // Make it with at least one initial port.
1349              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1350              parameters["ports"]->SetValue("1");              parameters["PORTS"]->SetValue("1");
1351          }          }
1352          // Must have a device...          // Must have a device...
1353          if (pDevice == NULL)          if (pDevice == NULL)
# Line 1166  String LSCPServer::SetMIDIInput(uint MID Line 1370  String LSCPServer::SetMIDIInput(uint MID
1370      LSCPResultSet result;      LSCPResultSet result;
1371      try {      try {
1372          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1373          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1374          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1375            if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1376          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1377          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);  
1378      }      }
1379      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1380           result.Error(e);           result.Error(e);
# Line 1182  String LSCPServer::SetMIDIInput(uint MID Line 1386  String LSCPServer::SetMIDIInput(uint MID
1386   * 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
1387   * particular sampler channel.   * particular sampler channel.
1388   */   */
1389  String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) {  String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1390      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1391      LSCPResultSet result;      LSCPResultSet result;
1392      try {      try {
1393          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1394          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1395          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1396          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1397          pEngine->Volume(Volume);          pEngineChannel->Volume(dVolume);
1398      }      }
1399      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1400           result.Error(e);           result.Error(e);
# Line 1199  String LSCPServer::SetVolume(double Volu Line 1403  String LSCPServer::SetVolume(double Volu
1403  }  }
1404    
1405  /**  /**
1406     * Will be called by the parser to mute/unmute particular sampler channel.
1407     */
1408    String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1409        dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1410        LSCPResultSet result;
1411        try {
1412            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1413            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1414    
1415            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1416            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1417    
1418            if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1419            else pEngineChannel->SetMute(1);
1420        } catch (LinuxSamplerException e) {
1421            result.Error(e);
1422        }
1423        return result.Produce();
1424    }
1425    
1426    /**
1427     * Will be called by the parser to solo particular sampler channel.
1428     */
1429    String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1430        dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1431        LSCPResultSet result;
1432        try {
1433            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1434            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1435    
1436            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1437            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1438    
1439            bool oldSolo = pEngineChannel->GetSolo();
1440            bool hadSoloChannel = HasSoloChannel();
1441            
1442            pEngineChannel->SetSolo(bSolo);
1443            
1444            if(!oldSolo && bSolo) {
1445                if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1446                if(!hadSoloChannel) MuteNonSoloChannels();
1447            }
1448            
1449            if(oldSolo && !bSolo) {
1450                if(!HasSoloChannel()) UnmuteChannels();
1451                else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1452            }
1453        } catch (LinuxSamplerException e) {
1454            result.Error(e);
1455        }
1456        return result.Produce();
1457    }
1458    
1459    /**
1460     * Determines whether there is at least one solo channel in the channel list.
1461     *
1462     * @returns true if there is at least one solo channel in the channel list,
1463     * false otherwise.
1464     */
1465    bool LSCPServer::HasSoloChannel() {
1466        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1467        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1468        for (; iter != channels.end(); iter++) {
1469            EngineChannel* c = iter->second->GetEngineChannel();
1470            if(c && c->GetSolo()) return true;
1471        }
1472    
1473        return false;
1474    }
1475    
1476    /**
1477     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1478     * with -1 which indicates that they are muted because of the presence
1479     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1480     * when there are no solo channels left.
1481     */
1482    void LSCPServer::MuteNonSoloChannels() {
1483        dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1484        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1485        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1486        for (; iter != channels.end(); iter++) {
1487            EngineChannel* c = iter->second->GetEngineChannel();
1488            if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1489        }
1490    }
1491    
1492    /**
1493     * Unmutes all channels that are muted because of the presence
1494     * of a solo channel(s).
1495     */
1496    void  LSCPServer::UnmuteChannels() {
1497        dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1498        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1499        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1500        for (; iter != channels.end(); iter++) {
1501            EngineChannel* c = iter->second->GetEngineChannel();
1502            if(c && c->GetMute() == -1) c->SetMute(0);
1503        }
1504    }
1505    
1506    /**
1507   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
1508   */   */
1509  String LSCPServer::ResetChannel(uint uiSamplerChannel) {  String LSCPServer::ResetChannel(uint uiSamplerChannel) {
# Line 1206  String LSCPServer::ResetChannel(uint uiS Line 1511  String LSCPServer::ResetChannel(uint uiS
1511      LSCPResultSet result;      LSCPResultSet result;
1512      try {      try {
1513          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1514          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1515          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1516          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1517          pEngine->Reset();          pEngineChannel->Reset();
1518      }      }
1519      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1520           result.Error(e);           result.Error(e);
# Line 1218  String LSCPServer::ResetChannel(uint uiS Line 1523  String LSCPServer::ResetChannel(uint uiS
1523  }  }
1524    
1525  /**  /**
1526     * Will be called by the parser to reset the whole sampler.
1527     */
1528    String LSCPServer::ResetSampler() {
1529        dmsg(2,("LSCPServer: ResetSampler()\n"));
1530        pSampler->Reset();
1531        LSCPResultSet result;
1532        return result.Produce();
1533    }
1534    
1535    /**
1536     * Will be called by the parser to return general informations about this
1537     * sampler.
1538     */
1539    String LSCPServer::GetServerInfo() {
1540        dmsg(2,("LSCPServer: GetServerInfo()\n"));
1541        LSCPResultSet result;
1542        result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");
1543        result.Add("VERSION", VERSION);
1544        result.Add("PROTOCOL_VERSION", "1.0");
1545        return result.Produce();
1546    }
1547    
1548    /**
1549   * 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
1550   * server for receiving event messages.   * server for receiving event messages.
1551   */   */
# Line 1243  String LSCPServer::UnsubscribeNotificati Line 1571  String LSCPServer::UnsubscribeNotificati
1571      return result.Produce();      return result.Produce();
1572  }  }
1573    
1574    static int select_callback(void * lscpResultSet, int argc,
1575  // Instrument loader constructor.                          char **argv, char **azColName)
 LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)  
     : Thread(false, 0, -4)  
1576  {  {
1577      this->pEngine = pEngine;      LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;
1578      this->Filename = Filename;      resultSet->Add(argc, argv);
1579      this->uiInstrument = uiInstrument;      return 0;
1580  }  }
1581    
1582  // Instrument loader process.  String LSCPServer::QueryDatabase(String query) {
1583  int LSCPLoadInstrument::Main()      LSCPResultSet result;
1584  {  #if HAVE_SQLITE3
1585      try {      char* zErrMsg = NULL;
1586          pEngine->LoadInstrument(Filename.c_str(), uiInstrument);      sqlite3 *db;
1587        String selectStr = "SELECT " + query;
1588    
1589        int rc = sqlite3_open("linuxsampler.db", &db);
1590        if (rc == SQLITE_OK)
1591        {
1592                rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);
1593      }      }
1594        if ( rc != SQLITE_OK )
1595        {
1596                result.Error(String(zErrMsg), rc);
1597        }
1598        sqlite3_close(db);
1599    #else
1600        result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);
1601    #endif
1602        return result.Produce();
1603    }
1604    
1605    /**
1606     * Will be called by the parser to enable or disable echo mode; if echo
1607     * mode is enabled, all commands from the client will (immediately) be
1608     * echoed back to the client.
1609     */
1610    String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
1611        dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
1612        LSCPResultSet result;
1613        try {
1614            if      (boolean_value == 0) pSession->bVerbose = false;
1615            else if (boolean_value == 1) pSession->bVerbose = true;
1616            else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1");
1617        }
1618      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1619          e.PrintMessage();           result.Error(e);
1620      }      }
1621        return result.Produce();
     // Always re-enable the engine.  
     pEngine->Enable();  
   
     // FIXME: Shoot ourselves on the foot?  
     delete this;  
1622  }  }

Legend:
Removed from v.184  
changed lines
  Added in v.705

  ViewVC Help
Powered by ViewVC