/[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 121 by senkov, Sat Jun 12 07:46:02 2004 UTC revision 778 by iliev, Fri Sep 23 06:58:26 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   ***************************************************************************/   ***************************************************************************/
23    
24  #include "lscpserver.h"  #include "lscpserver.h"
25  #include "lscpresultset.h"  #include "lscpresultset.h"
26    #include "lscpevent.h"
27    //#include "../common/global.h"
28    
29  #include "../engines/gig/Engine.h"  #include <fcntl.h>
30    
31  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) {  #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.
42     * The big assumption here is that LSCPServer is going to remain a singleton.
43     * These members are used to support client connections.
44     * Class handles multiple connections at the same time using select() and non-blocking recv()
45     * Commands are processed by a single LSCPServer thread.
46     * Notifications are delivered either by the thread that originated them
47     * or (if the resultset is currently in progress) by the LSCPServer thread
48     * after the resultset was sent out.
49     * This makes sure that resultsets can not be interrupted by notifications.
50     * This also makes sure that the thread sending notification is not blocked
51     * by the LSCPServer thread.
52     */
53    fd_set LSCPServer::fdSet;
54    int LSCPServer::currentSocket = -1;
55    std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
56    std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
57    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> >();
59    Mutex LSCPServer::NotifyMutex = Mutex();
60    Mutex LSCPServer::NotifyBufferMutex = Mutex();
61    Mutex LSCPServer::SubscriptionMutex = Mutex();
62    Mutex LSCPServer::RTNotifyMutex = Mutex();
63    
64    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_channel_count, "CHANNEL_COUNT");
70        LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
71        LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
72        LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
73        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
74        LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
75        LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
76        hSocket = -1;
77    }
78    
79    LSCPServer::~LSCPServer() {
80        if (hSocket >= 0) close(hSocket);
81    }
82    
83    /**
84     * Blocks the calling thread until the LSCP Server is initialized and
85     * accepting socket connections, if the server is already initialized then
86     * this method will return immediately.
87     * @param TimeoutSeconds     - optional: max. wait time in seconds
88     *                             (default: 0s)
89     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
90     *                             (default: 0ns)
91     * @returns  0 on success, a value less than 0 if timeout exceeded
92     */
93    int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
94        return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
95  }  }
96    
97  int LSCPServer::Main() {  int LSCPServer::Main() {
# Line 37  int LSCPServer::Main() { Line 102  int LSCPServer::Main() {
102          exit(EXIT_FAILURE);          exit(EXIT_FAILURE);
103      }      }
104    
     SocketAddress.sin_family      = AF_INET;  
     SocketAddress.sin_port        = htons(LSCP_PORT);  
     SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);  
   
105      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
106          std::cerr << "LSCPServer: Could not bind server socket." << std::endl;          std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
107          close(hSocket);          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
108          //return -1;              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
109          exit(EXIT_FAILURE);                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
110                        std::cerr << "gave up!" << std::endl;
111                        close(hSocket);
112                        //return -1;
113                        exit(EXIT_FAILURE);
114                    }
115                    else sleep(1); // sleep 1s
116                }
117                else break; // success
118            }
119      }      }
120    
121      listen(hSocket, 1);      listen(hSocket, 1);
122      dmsg(1,("LSCPServer: Server running.\n")); // server running      Initialized.Set(true);
123    
124      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
125      sockaddr_in client;      sockaddr_in client;
126      int length = sizeof(client);      int length = sizeof(client);
127        FD_ZERO(&fdSet);
128        FD_SET(hSocket, &fdSet);
129        int maxSessions = hSocket;
130    
131      while (true) {      while (true) {
132          hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);          fd_set selectSet = fdSet;
133          if (hSession < 0) {          int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);
134              std::cerr << "LSCPServer: Client connection failed." << std::endl;          if (retval == 0)
135              close(hSocket);                  continue; //Nothing try again
136              //return -1;          if (retval == -1) {
137              exit(EXIT_FAILURE);                  std::cerr << "LSCPServer: Socket select error." << std::endl;
138          }                  close(hSocket);
139                    exit(EXIT_FAILURE);
140          dmsg(1,("LSCPServer: Client connection established.\n"));          }
141          //send(hSession, "Welcome!\r\n", 10, 0);  
142            //Accept new connections now (if any)
143          // Parser invocation          if (FD_ISSET(hSocket, &selectSet)) {
144          yyparse_param_t yyparse_param;                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
145          yyparse_param.pServer = this;                  if (socket < 0) {
146          yylex_init(&yyparse_param.pScanner);                          std::cerr << "LSCPServer: Client connection failed." << std::endl;
147          while (yyparse(&yyparse_param) == LSCP_SYNTAX_ERROR); // recall parser in case of syntax error                          exit(EXIT_FAILURE);
148          yylex_destroy(yyparse_param.pScanner);                  }
149    
150                    if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
151                            std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
152                            exit(EXIT_FAILURE);
153                    }
154    
155                    // Parser initialization
156                    yyparse_param_t yyparse_param;
157                    yyparse_param.pServer  = this;
158                    yyparse_param.hSession = socket;
159    
160                    Sessions.push_back(yyparse_param);
161                    FD_SET(socket, &fdSet);
162                    if (socket > maxSessions)
163                            maxSessions = socket;
164                    dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
165                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
166                    continue; //Maybe this was the only selected socket, better select again
167            }
168    
169          close(hSession);          //Something was selected and it was not the hSocket, so it must be some command(s) coming.
170          dmsg(1,("LSCPServer: Client connection terminated.\n"));          for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
171                    if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?
172                            if (GetLSCPCommand(iter)) {     //Have we read the entire command?
173                                    dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
174                                    int dummy; // just a temporary hack to fulfill the restart() function prototype
175                                    restart(NULL, dummy); // restart the 'scanner'
176                                    currentSocket = (*iter).hSession;  //a hack
177                                    dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
178                                    if ((*iter).bVerbose) { // if echo mode enabled
179                                        AnswerClient(bufferedCommands[currentSocket]);
180                                    }
181                                    int result = yyparse(&(*iter));
182                                    currentSocket = -1;     //continuation of a hack
183                                    dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
184                                    if (result == LSCP_QUIT) { //Was it a quit command by any chance?
185                                            CloseConnection(iter);
186                                    }
187                            }
188                            //socket may have been closed, iter may be invalid, get out of the loop for now.
189                            //we'll be back if there is data.
190                            break;
191                    }
192            }
193    
194            // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
195            {
196                std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
197                std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
198                std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
199                for (; itEngineChannel != itEnd; ++itEngineChannel) {
200                    if ((*itEngineChannel)->StatusChanged()) {
201                        SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));
202                    }
203                }
204            }
205    
206            //Now let's deliver late notifies (if any)
207            NotifyBufferMutex.Lock();
208            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
209                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
210                    bufferedNotifies.erase(iterNotify);
211            }
212            NotifyBufferMutex.Unlock();
213      }      }
214  }  }
215    
216    void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
217            int socket = (*iter).hSession;
218            dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
219            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
220            Sessions.erase(iter);
221            FD_CLR(socket,  &fdSet);
222            SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
223            for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
224                    iter->second.remove(socket);
225            }
226            SubscriptionMutex.Unlock();
227            NotifyMutex.Lock();
228            bufferedCommands.erase(socket);
229            bufferedNotifies.erase(socket);
230            close(socket);
231            NotifyMutex.Unlock();
232    }
233    
234    int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
235            int subs = 0;
236            SubscriptionMutex.Lock();
237            for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
238                            iter != events.end(); iter++)
239            {
240                    subs += eventSubscriptions.count(*iter);
241            }
242            SubscriptionMutex.Unlock();
243            return subs;
244    }
245    
246    void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
247            SubscriptionMutex.Lock();
248            if (eventSubscriptions.count(event.GetType()) == 0) {
249                    SubscriptionMutex.Unlock();     //Nobody is subscribed to this event
250                    return;
251            }
252            std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
253            std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
254            String notify = event.Produce();
255    
256            while (true) {
257                    if (NotifyMutex.Trylock()) {
258                            for(;iter != end; iter++)
259                                    send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
260                            NotifyMutex.Unlock();
261                            break;
262                    } else {
263                            if (NotifyBufferMutex.Trylock()) {
264                                    for(;iter != end; iter++)
265                                            bufferedNotifies[*iter] += notify;
266                                    NotifyBufferMutex.Unlock();
267                                    break;
268                            }
269                    }
270            }
271            SubscriptionMutex.Unlock();
272    }
273    
274    extern int GetLSCPCommand( void *buf, int max_size ) {
275            String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
276            if (command.size() == 0) {              //Parser wants input but we have nothing.
277                    strcpy((char*) buf, "\n");      //So give it an empty command
278                    return 1;                       //to keep it happy.
279            }
280    
281            if (max_size < command.size()) {
282                    std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
283                    return 0;       //This will never happen
284            }
285    
286            strcpy((char*) buf, command.c_str());
287            LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
288            return command.size();
289    }
290    
291    /**
292     * Will be called to try to read the command from the socket
293     * If command is read, it will return true. Otherwise false is returned.
294     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
295     */
296    bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
297            int socket = (*iter).hSession;
298            char c;
299            int i = 0;
300            while (true) {
301                    int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
302                    if (result == 0) { //socket was selected, so 0 here means client has closed the connection
303                            CloseConnection(iter);
304                            break;
305                    }
306                    if (result == 1) {
307                            if (c == '\r')
308                                    continue; //Ignore CR
309                            if (c == '\n') {
310                                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
311                                    bufferedCommands[socket] += "\n";
312                                    return true; //Complete command was read
313                            }
314                            bufferedCommands[socket] += c;
315                    }
316                    if (result == -1) {
317                            if (errno == EAGAIN) //Would block, try again later.
318                                    return false;
319                            switch(errno) {
320                                    case EBADF:
321                                            dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
322                                            break;
323                                    case ECONNREFUSED:
324                                            dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
325                                            break;
326                                    case ENOTCONN:
327                                            dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
328                                            break;
329                                    case ENOTSOCK:
330                                            dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
331                                            break;
332                                    case EAGAIN:
333                                            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"));
334                                            break;
335                                    case EINTR:
336                                            dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
337                                            break;
338                                    case EFAULT:
339                                            dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
340                                            break;
341                                    case EINVAL:
342                                            dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
343                                            break;
344                                    case ENOMEM:
345                                            dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
346                                            break;
347                                    default:
348                                            dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
349                                            break;
350                            }
351                            CloseConnection(iter);
352                            break;
353                    }
354            }
355            return false;
356    }
357    
358  /**  /**
359   * Will be called by the parser whenever it wants to send an answer to the   * Will be called by the parser whenever it wants to send an answer to the
360   * client / frontend.   * client / frontend.
# Line 86  int LSCPServer::Main() { Line 363  int LSCPServer::Main() {
363   */   */
364  void LSCPServer::AnswerClient(String ReturnMessage) {  void LSCPServer::AnswerClient(String ReturnMessage) {
365      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
366      send(hSession, ReturnMessage.c_str(), ReturnMessage.size(), 0);      if (currentSocket != -1) {
367                NotifyMutex.Lock();
368                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
369                NotifyMutex.Unlock();
370        }
371    }
372    
373    /**
374     * Find a created audio output device index.
375     */
376    int LSCPServer::GetAudioOutputDeviceIndex ( AudioOutputDevice *pDevice )
377    {
378        // Search for the created device to get its index
379        std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
380        std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
381        for (; iter != devices.end(); iter++) {
382            if (iter->second == pDevice)
383                return iter->first;
384        }
385        // Not found.
386        return -1;
387    }
388    
389    /**
390     * Find a created midi input device index.
391     */
392    int LSCPServer::GetMidiInputDeviceIndex ( MidiInputDevice *pDevice )
393    {
394        // Search for the created device to get its index
395        std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
396        std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
397        for (; iter != devices.end(); iter++) {
398            if (iter->second == pDevice)
399                return iter->first;
400        }
401        // Not found.
402        return -1;
403    }
404    
405    String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
406        dmsg(2,("LSCPServer: CreateAudioOutputDevice(Driver=%s)\n", Driver.c_str()));
407        LSCPResultSet result;
408        try {
409            AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
410            // search for the created device to get its index
411            int index = GetAudioOutputDeviceIndex(pDevice);
412            if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");
413            result = index; // success
414        }
415        catch (LinuxSamplerException e) {
416            result.Error(e);
417        }
418        return result.Produce();
419    }
420    
421    String LSCPServer::CreateMidiInputDevice(String Driver, std::map<String,String> Parameters) {
422        dmsg(2,("LSCPServer: CreateMidiInputDevice(Driver=%s)\n", Driver.c_str()));
423        LSCPResultSet result;
424        try {
425            MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
426            // search for the created device to get its index
427            int index = GetMidiInputDeviceIndex(pDevice);
428            if (index == -1) throw LinuxSamplerException("Internal error: could not find created midi input device.");
429            result = index; // success
430        }
431        catch (LinuxSamplerException e) {
432            result.Error(e);
433        }
434        return result.Produce();
435    }
436    
437    String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
438        dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
439        LSCPResultSet result;
440        try {
441            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
442            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
443            AudioOutputDevice* pDevice = devices[DeviceIndex];
444            pSampler->DestroyAudioOutputDevice(pDevice);
445        }
446        catch (LinuxSamplerException e) {
447            result.Error(e);
448        }
449        return result.Produce();
450    }
451    
452    String LSCPServer::DestroyMidiInputDevice(uint DeviceIndex) {
453        dmsg(2,("LSCPServer: DestroyMidiInputDevice(DeviceIndex=%d)\n", DeviceIndex));
454        LSCPResultSet result;
455        try {
456            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
457            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
458            MidiInputDevice* pDevice = devices[DeviceIndex];
459            pSampler->DestroyMidiInputDevice(pDevice);
460        }
461        catch (LinuxSamplerException e) {
462            result.Error(e);
463        }
464        return result.Produce();
465  }  }
466    
467  /**  /**
468   * Will be called by the parser to load an instrument.   * Will be called by the parser to load an instrument.
469   */   */
470  String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel) {  String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground) {
471      dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));      dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));
472      LSCPResultSet result;      LSCPResultSet result;
473      try {      try {
474          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
475          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
476          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
477          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet");
478          pEngine->LoadInstrument(Filename.c_str(), uiInstrument);          if (!pSamplerChannel->GetAudioOutputDevice())
479                throw LinuxSamplerException("No audio output device connected to sampler channel");
480            if (bBackground) {
481                InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel);
482            }
483            else {
484                // tell the engine channel which instrument to load
485                pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
486                // actually start to load the instrument (blocks until completed)
487                pEngineChannel->LoadInstrument();
488            }
489      }      }
490      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
491           result.Error(e);           result.Error(e);
# Line 109  String LSCPServer::LoadInstrument(String Line 494  String LSCPServer::LoadInstrument(String
494  }  }
495    
496  /**  /**
497   * 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
498     * sampler channel.
499   */   */
500  String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) {  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
501      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));
502      LSCPResultSet result;      LSCPResultSet result;
503      try {      try {
         Engine::type_t type;  
         if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig;  
         else throw LinuxSamplerException("Unknown engine type");  
504          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
505          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
506          pSamplerChannel->LoadEngine(type);          LockRTNotify();
507            pSamplerChannel->SetEngineType(EngineName);
508            if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
509            UnlockRTNotify();
510      }      }
511      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
512           result.Error(e);           result.Error(e);
# Line 139  String LSCPServer::GetChannels() { Line 525  String LSCPServer::GetChannels() {
525  }  }
526    
527  /**  /**
528     * Will be called by the parser to get the list of sampler channels.
529     */
530    String LSCPServer::ListChannels() {
531        dmsg(2,("LSCPServer: ListChannels()\n"));
532        String list;
533        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
534        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
535        for (; iter != channels.end(); iter++) {
536            if (list != "") list += ",";
537            list += ToString(iter->first);
538        }
539        LSCPResultSet result;
540        result.Add(list);
541        return result.Produce();
542    }
543    
544    /**
545   * Will be called by the parser to add a sampler channel.   * Will be called by the parser to add a sampler channel.
546   */   */
547  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
# Line 154  String LSCPServer::AddChannel() { Line 557  String LSCPServer::AddChannel() {
557  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
558      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
559      LSCPResultSet result;      LSCPResultSet result;
560        LockRTNotify();
561      pSampler->RemoveSamplerChannel(uiSamplerChannel);      pSampler->RemoveSamplerChannel(uiSamplerChannel);
562        UnlockRTNotify();
563      return result.Produce();      return result.Produce();
564  }  }
565    
566  /**  /**
567   * 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.
568   */   */
569  String LSCPServer::GetAvailableEngines() {  String LSCPServer::GetAvailableEngines() {
570      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
571      LSCPResultSet result("GigEngine");      LSCPResultSet result("1");
572      return result.Produce();      return result.Produce();
573  }  }
574    
575  /**  /**
576   * Will be called by the parser to get descriptions for a particular engine.   * Will be called by the parser to get a list of all available engines.
577     */
578    String LSCPServer::ListAvailableEngines() {
579        dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
580        LSCPResultSet result("\'GIG\'");
581        return result.Produce();
582    }
583    
584    /**
585     * Will be called by the parser to get descriptions for a particular
586     * sampler engine.
587   */   */
588  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
589      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
590      LSCPResultSet result;      LSCPResultSet result;
591      try {      try {
592          if ((EngineName == "GigEngine") || (EngineName == "gig")) {          Engine* pEngine = EngineFactory::Create(EngineName);
593              Engine* pEngine = new LinuxSampler::gig::Engine;          result.Add("DESCRIPTION", pEngine->Description());
594              result.Add(pEngine->Description());          result.Add("VERSION",     pEngine->Version());
595              delete pEngine;          EngineFactory::Destroy(pEngine);
         }  
         else throw LinuxSamplerException("Unknown engine type");  
596      }      }
597      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
598           result.Error(e);           result.Error(e);
# Line 196  String LSCPServer::GetChannelInfo(uint u Line 609  String LSCPServer::GetChannelInfo(uint u
609      LSCPResultSet result;      LSCPResultSet result;
610      try {      try {
611          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
612          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
613          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
614            
615          //Defaults values          //Defaults values
616          String EngineName = "NONE";          String EngineName = "NONE";
617          float Volume = 0;          float Volume = 0.0f;
618          String InstrumentFileName = "NONE";          String InstrumentFileName = "NONE";
619          int InstrumentIndex = 0;          String InstrumentName = "NONE";
620                    int InstrumentIndex = -1;
621          if (pEngine) {          int InstrumentStatus = -1;
622              EngineName =  pEngine->EngineName();          int AudioOutputChannels = 0;
623              Volume = pEngine->Volume();          String AudioRouting;
624              int iIdx = pEngine->InstrumentIndex();          int Mute = 0;
625              if (iIdx != -1) {          bool Solo = false;
626                  InstrumentFileName = pEngine->InstrumentFileName();  
627                  InstrumentIndex = iIdx;          if (pEngineChannel) {
628                EngineName          = pEngineChannel->EngineName();
629                AudioOutputChannels = pEngineChannel->Channels();
630                Volume              = pEngineChannel->Volume();
631                InstrumentStatus    = pEngineChannel->InstrumentStatus();
632                InstrumentIndex     = pEngineChannel->InstrumentIndex();
633                if (InstrumentIndex != -1) {
634                    InstrumentFileName = pEngineChannel->InstrumentFileName();
635                    InstrumentName     = pEngineChannel->InstrumentName();
636              }              }
637                for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
638                    if (AudioRouting != "") AudioRouting += ",";
639                    AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
640                }
641                Mute = pEngineChannel->GetMute();
642                Solo = pEngineChannel->GetSolo();
643          }          }
644    
645          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
646          result.Add("VOLUME", Volume);          result.Add("VOLUME", Volume);
647    
648          //Some hardcoded stuff for now to make GUI look good          //Some not-so-hardcoded stuff to make GUI look good
649          result.Add("AUDIO_OUTPUT_DEVICE", "0");          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
650          result.Add("AUDIO_OUTPUT_CHANNELS", "2");          result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
651          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
652    
653            result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
654            result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
655            if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
656            else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
657    
658          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
659          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
660                    result.Add("INSTRUMENT_NAME", InstrumentName);
661          //Some more hardcoded stuff for now to make GUI look good          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
662          result.Add("MIDI_INPUT_DEVICE", "0");          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
663          result.Add("MIDI_INPUT_PORT", "0");          result.Add("SOLO", Solo);
         result.Add("MIDI_INPUT_CHANNEL", "1");  
664      }      }
665      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
666           result.Error(e);           result.Error(e);
# Line 246  String LSCPServer::GetVoiceCount(uint ui Line 677  String LSCPServer::GetVoiceCount(uint ui
677      LSCPResultSet result;      LSCPResultSet result;
678      try {      try {
679          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
680          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
681          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
682          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel");
683          result.Add(pEngine->VoiceCount());          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
684            result.Add(pEngineChannel->GetEngine()->VoiceCount());
685      }      }
686      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
687           result.Error(e);           result.Error(e);
# Line 266  String LSCPServer::GetStreamCount(uint u Line 698  String LSCPServer::GetStreamCount(uint u
698      LSCPResultSet result;      LSCPResultSet result;
699      try {      try {
700          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
701          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
702          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
703          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
704          result.Add(pEngine->DiskStreamCount());          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
705            result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
706      }      }
707      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
708           result.Error(e);           result.Error(e);
# Line 286  String LSCPServer::GetBufferFill(fill_re Line 719  String LSCPServer::GetBufferFill(fill_re
719      LSCPResultSet result;      LSCPResultSet result;
720      try {      try {
721          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
722          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
723          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
724          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
725          if (!pEngine->DiskStreamSupported()) return "NA\r\n"; //FIXME: Update resultset class to support "NA"          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");
726          switch (ResponseType) {          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
727              case fill_response_bytes:          else {
728                  result.Add(pEngine->DiskStreamBufferFillBytes());              switch (ResponseType) {
729                  break;                  case fill_response_bytes:
730              case fill_response_percentage:                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
731                  result.Add(pEngine->DiskStreamBufferFillPercentage());                      break;
732                  break;                  case fill_response_percentage:
733              default:                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
734                  throw LinuxSamplerException("Unknown fill response type");                      break;
735          }                  default:
736                        throw LinuxSamplerException("Unknown fill response type");
737                }
738            }
739      }      }
740      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
741           result.Error(e);           result.Error(e);
# Line 307  String LSCPServer::GetBufferFill(fill_re Line 743  String LSCPServer::GetBufferFill(fill_re
743      return result.Produce();      return result.Produce();
744  }  }
745    
746  /**  String LSCPServer::GetAvailableAudioOutputDrivers() {
747   * Will be called by the parser to change the audio output type on a      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
  * particular sampler channel.  
  */  
 String LSCPServer::SetAudioOutputType(AudioOutputDevice::type_t AudioOutputType, uint uiSamplerChannel) {  
     dmsg(2,("LSCPServer: SetAudioOutputType(AudioOutputType=%d, SamplerChannel=%d)\n", AudioOutputType, uiSamplerChannel));  
748      LSCPResultSet result;      LSCPResultSet result;
749      try {      try {
750          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          int n = AudioOutputDeviceFactory::AvailableDrivers().size();
751          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          result.Add(n);
         pSamplerChannel->SetAudioOutputDevice(AudioOutputType);  
752      }      }
753      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
754           result.Error(e);          result.Error(e);
755        }
756        return result.Produce();
757    }
758    
759    String LSCPServer::ListAvailableAudioOutputDrivers() {
760        dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
761        LSCPResultSet result;
762        try {
763            String s = AudioOutputDeviceFactory::AvailableDriversAsString();
764            result.Add(s);
765        }
766        catch (LinuxSamplerException e) {
767            result.Error(e);
768        }
769        return result.Produce();
770    }
771    
772    String LSCPServer::GetAvailableMidiInputDrivers() {
773        dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
774        LSCPResultSet result;
775        try {
776            int n = MidiInputDeviceFactory::AvailableDrivers().size();
777            result.Add(n);
778        }
779        catch (LinuxSamplerException e) {
780            result.Error(e);
781        }
782        return result.Produce();
783    }
784    
785    String LSCPServer::ListAvailableMidiInputDrivers() {
786        dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
787        LSCPResultSet result;
788        try {
789            String s = MidiInputDeviceFactory::AvailableDriversAsString();
790            result.Add(s);
791        }
792        catch (LinuxSamplerException e) {
793            result.Error(e);
794        }
795        return result.Produce();
796    }
797    
798    String LSCPServer::GetMidiInputDriverInfo(String Driver) {
799        dmsg(2,("LSCPServer: GetMidiInputDriverInfo(Driver=%s)\n",Driver.c_str()));
800        LSCPResultSet result;
801        try {
802            result.Add("DESCRIPTION", MidiInputDeviceFactory::GetDriverDescription(Driver));
803            result.Add("VERSION",     MidiInputDeviceFactory::GetDriverVersion(Driver));
804    
805            std::map<String,DeviceCreationParameter*> parameters = MidiInputDeviceFactory::GetAvailableDriverParameters(Driver);
806            if (parameters.size()) { // if there are parameters defined for this driver
807                String s;
808                std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
809                for (;iter != parameters.end(); iter++) {
810                    if (s != "") s += ",";
811                    s += iter->first;
812                }
813                result.Add("PARAMETERS", s);
814            }
815        }
816        catch (LinuxSamplerException e) {
817            result.Error(e);
818        }
819        return result.Produce();
820    }
821    
822    String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
823        dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
824        LSCPResultSet result;
825        try {
826            result.Add("DESCRIPTION", AudioOutputDeviceFactory::GetDriverDescription(Driver));
827            result.Add("VERSION",     AudioOutputDeviceFactory::GetDriverVersion(Driver));
828    
829            std::map<String,DeviceCreationParameter*> parameters = AudioOutputDeviceFactory::GetAvailableDriverParameters(Driver);
830            if (parameters.size()) { // if there are parameters defined for this driver
831                String s;
832                std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
833                for (;iter != parameters.end(); iter++) {
834                    if (s != "") s += ",";
835                    s += iter->first;
836                }
837                result.Add("PARAMETERS", s);
838            }
839        }
840        catch (LinuxSamplerException e) {
841            result.Error(e);
842        }
843        return result.Produce();
844    }
845    
846    String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
847        dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
848        LSCPResultSet result;
849        try {
850            DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
851            result.Add("TYPE",         pParameter->Type());
852            result.Add("DESCRIPTION",  pParameter->Description());
853            result.Add("MANDATORY",    pParameter->Mandatory());
854            result.Add("FIX",          pParameter->Fix());
855            result.Add("MULTIPLICITY", pParameter->Multiplicity());
856            optional<String> oDepends       = pParameter->Depends();
857            optional<String> oDefault       = pParameter->Default(DependencyList);
858            optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
859            optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
860            optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
861            if (oDepends)       result.Add("DEPENDS",       *oDepends);
862            if (oDefault)       result.Add("DEFAULT",       *oDefault);
863            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
864            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
865            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
866        }
867        catch (LinuxSamplerException e) {
868            result.Error(e);
869        }
870        return result.Produce();
871    }
872    
873    String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
874        dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
875        LSCPResultSet result;
876        try {
877            DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
878            result.Add("TYPE",         pParameter->Type());
879            result.Add("DESCRIPTION",  pParameter->Description());
880            result.Add("MANDATORY",    pParameter->Mandatory());
881            result.Add("FIX",          pParameter->Fix());
882            result.Add("MULTIPLICITY", pParameter->Multiplicity());
883            optional<String> oDepends       = pParameter->Depends();
884            optional<String> oDefault       = pParameter->Default(DependencyList);
885            optional<String> oRangeMin      = pParameter->RangeMin(DependencyList);
886            optional<String> oRangeMax      = pParameter->RangeMax(DependencyList);
887            optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
888            if (oDepends)       result.Add("DEPENDS",       *oDepends);
889            if (oDefault)       result.Add("DEFAULT",       *oDefault);
890            if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
891            if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
892            if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
893        }
894        catch (LinuxSamplerException e) {
895            result.Error(e);
896        }
897        return result.Produce();
898    }
899    
900    String LSCPServer::GetAudioOutputDeviceCount() {
901        dmsg(2,("LSCPServer: GetAudioOutputDeviceCount()\n"));
902        LSCPResultSet result;
903        try {
904            uint count = pSampler->AudioOutputDevices();
905            result.Add(count); // success
906        }
907        catch (LinuxSamplerException e) {
908            result.Error(e);
909        }
910        return result.Produce();
911    }
912    
913    String LSCPServer::GetMidiInputDeviceCount() {
914        dmsg(2,("LSCPServer: GetMidiInputDeviceCount()\n"));
915        LSCPResultSet result;
916        try {
917            uint count = pSampler->MidiInputDevices();
918            result.Add(count); // success
919        }
920        catch (LinuxSamplerException e) {
921            result.Error(e);
922        }
923        return result.Produce();
924    }
925    
926    String LSCPServer::GetAudioOutputDevices() {
927        dmsg(2,("LSCPServer: GetAudioOutputDevices()\n"));
928        LSCPResultSet result;
929        try {
930            String s;
931            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
932            std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
933            for (; iter != devices.end(); iter++) {
934                if (s != "") s += ",";
935                s += ToString(iter->first);
936            }
937            result.Add(s);
938        }
939        catch (LinuxSamplerException e) {
940            result.Error(e);
941        }
942        return result.Produce();
943    }
944    
945    String LSCPServer::GetMidiInputDevices() {
946        dmsg(2,("LSCPServer: GetMidiInputDevices()\n"));
947        LSCPResultSet result;
948        try {
949            String s;
950            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
951            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
952            for (; iter != devices.end(); iter++) {
953                if (s != "") s += ",";
954                s += ToString(iter->first);
955            }
956            result.Add(s);
957        }
958        catch (LinuxSamplerException e) {
959            result.Error(e);
960        }
961        return result.Produce();
962    }
963    
964    String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
965        dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
966        LSCPResultSet result;
967        try {
968            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
969            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
970            AudioOutputDevice* pDevice = devices[DeviceIndex];
971            result.Add("DRIVER", pDevice->Driver());
972            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
973            std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
974            for (; iter != parameters.end(); iter++) {
975                result.Add(iter->first, iter->second->Value());
976            }
977        }
978        catch (LinuxSamplerException e) {
979            result.Error(e);
980        }
981        return result.Produce();
982    }
983    
984    String LSCPServer::GetMidiInputDeviceInfo(uint DeviceIndex) {
985        dmsg(2,("LSCPServer: GetMidiInputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
986        LSCPResultSet result;
987        try {
988            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
989            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
990            MidiInputDevice* pDevice = devices[DeviceIndex];
991            result.Add("DRIVER", pDevice->Driver());
992            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
993            std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
994            for (; iter != parameters.end(); iter++) {
995                result.Add(iter->first, iter->second->Value());
996            }
997        }
998        catch (LinuxSamplerException e) {
999            result.Error(e);
1000        }
1001        return result.Produce();
1002    }
1003    String LSCPServer::GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex) {
1004        dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
1005        LSCPResultSet result;
1006        try {
1007            // get MIDI input device
1008            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1009            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1010            MidiInputDevice* pDevice = devices[DeviceIndex];
1011    
1012            // get MIDI port
1013            MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1014            if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1015    
1016            // return the values of all MIDI port parameters
1017            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1018            std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1019            for (; iter != parameters.end(); iter++) {
1020                result.Add(iter->first, iter->second->Value());
1021            }
1022        }
1023        catch (LinuxSamplerException e) {
1024            result.Error(e);
1025        }
1026        return result.Produce();
1027    }
1028    
1029    String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
1030        dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
1031        LSCPResultSet result;
1032        try {
1033            // get audio output device
1034            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1035            if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1036            AudioOutputDevice* pDevice = devices[DeviceId];
1037    
1038            // get audio channel
1039            AudioChannel* pChannel = pDevice->Channel(ChannelId);
1040            if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1041    
1042            // return the values of all audio channel parameters
1043            std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1044            std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1045            for (; iter != parameters.end(); iter++) {
1046                result.Add(iter->first, iter->second->Value());
1047            }
1048        }
1049        catch (LinuxSamplerException e) {
1050            result.Error(e);
1051        }
1052        return result.Produce();
1053    }
1054    
1055    String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
1056        dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
1057        LSCPResultSet result;
1058        try {
1059            // get MIDI input device
1060            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1061            if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");
1062            MidiInputDevice* pDevice = devices[DeviceId];
1063    
1064            // get midi port
1065            MidiInputPort* pPort = pDevice->GetPort(PortId);
1066            if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");
1067    
1068            // get desired port parameter
1069            std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1070            if (!parameters.count(ParameterName)) throw LinuxSamplerException("Midi port does not provide a parameter '" + ParameterName + "'.");
1071            DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1072    
1073            // return all fields of this audio channel parameter
1074            result.Add("TYPE",         pParameter->Type());
1075            result.Add("DESCRIPTION",  pParameter->Description());
1076            result.Add("FIX",          pParameter->Fix());
1077            result.Add("MULTIPLICITY", pParameter->Multiplicity());
1078            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1079            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1080            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1081        }
1082        catch (LinuxSamplerException e) {
1083            result.Error(e);
1084        }
1085        return result.Produce();
1086    }
1087    
1088    String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
1089        dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
1090        LSCPResultSet result;
1091        try {
1092            // get audio output device
1093            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1094            if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1095            AudioOutputDevice* pDevice = devices[DeviceId];
1096    
1097            // get audio channel
1098            AudioChannel* pChannel = pDevice->Channel(ChannelId);
1099            if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1100    
1101            // get desired audio channel parameter
1102            std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1103            if (!parameters.count(ParameterName)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");
1104            DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1105    
1106            // return all fields of this audio channel parameter
1107            result.Add("TYPE",         pParameter->Type());
1108            result.Add("DESCRIPTION",  pParameter->Description());
1109            result.Add("FIX",          pParameter->Fix());
1110            result.Add("MULTIPLICITY", pParameter->Multiplicity());
1111            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     *pParameter->RangeMin());
1112            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1113            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1114        }
1115        catch (LinuxSamplerException e) {
1116            result.Error(e);
1117        }
1118        return result.Produce();
1119    }
1120    
1121    String LSCPServer::SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal) {
1122        dmsg(2,("LSCPServer: SetAudioOutputChannelParameter(DeviceId=%d,ChannelId=%d,ParamKey=%s,ParamVal=%s)\n",DeviceId,ChannelId,ParamKey.c_str(),ParamVal.c_str()));
1123        LSCPResultSet result;
1124        try {
1125            // get audio output device
1126            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1127            if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1128            AudioOutputDevice* pDevice = devices[DeviceId];
1129    
1130            // get audio channel
1131            AudioChannel* pChannel = pDevice->Channel(ChannelId);
1132            if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1133    
1134            // get desired audio channel parameter
1135            std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1136            if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");
1137            DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1138    
1139            // set new channel parameter value
1140            pParameter->SetValue(ParamVal);
1141        }
1142        catch (LinuxSamplerException e) {
1143            result.Error(e);
1144        }
1145        return result.Produce();
1146    }
1147    
1148    String LSCPServer::SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1149        dmsg(2,("LSCPServer: SetAudioOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1150        LSCPResultSet result;
1151        try {
1152            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1153            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1154            AudioOutputDevice* pDevice = devices[DeviceIndex];
1155            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1156            if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1157            parameters[ParamKey]->SetValue(ParamVal);
1158        }
1159        catch (LinuxSamplerException e) {
1160            result.Error(e);
1161        }
1162        return result.Produce();
1163    }
1164    
1165    String LSCPServer::SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1166        dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1167        LSCPResultSet result;
1168        try {
1169            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1170            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1171            MidiInputDevice* pDevice = devices[DeviceIndex];
1172            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1173            if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1174            parameters[ParamKey]->SetValue(ParamVal);
1175        }
1176        catch (LinuxSamplerException e) {
1177            result.Error(e);
1178        }
1179        return result.Produce();
1180    }
1181    
1182    String LSCPServer::SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal) {
1183        dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1184        LSCPResultSet result;
1185        try {
1186            // get MIDI input device
1187            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1188            if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1189            MidiInputDevice* pDevice = devices[DeviceIndex];
1190    
1191            // get MIDI port
1192            MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1193            if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1194    
1195            // set port parameter value
1196            std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1197            if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1198            parameters[ParamKey]->SetValue(ParamVal);
1199        }
1200        catch (LinuxSamplerException e) {
1201            result.Error(e);
1202      }      }
1203      return result.Produce();      return result.Produce();
1204  }  }
# Line 329  String LSCPServer::SetAudioOutputType(Au Line 1207  String LSCPServer::SetAudioOutputType(Au
1207   * Will be called by the parser to change the audio output channel for   * Will be called by the parser to change the audio output channel for
1208   * playback on a particular sampler channel.   * playback on a particular sampler channel.
1209   */   */
1210  String LSCPServer::SetAudioOutputChannel(uint AudioOutputChannel, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1211      dmsg(2,("LSCPServer: SetAudioOutputChannel(AudioOutputChannel=%d, SamplerChannel=%d)\n", AudioOutputChannel, uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));
1212      return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?      LSCPResultSet result;
1213        try {
1214            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1215            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1216            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1217            if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1218            if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1219            pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1220        }
1221        catch (LinuxSamplerException e) {
1222             result.Error(e);
1223        }
1224        return result.Produce();
1225  }  }
1226    
1227  String LSCPServer::SetMIDIInputType(MidiInputDevice::type_t MidiInputType, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1228      dmsg(2,("LSCPServer: SetMIDIInputType(MidiInputType=%d, SamplerChannel=%d)\n", MidiInputType, uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1229      LSCPResultSet result;      LSCPResultSet result;
1230      try {      try {
1231          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1232          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1233          pSamplerChannel->SetMidiInputDevice(MidiInputType);          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1234            if (!devices.count(AudioDeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1235            AudioOutputDevice* pDevice = devices[AudioDeviceId];
1236            pSamplerChannel->SetAudioOutputDevice(pDevice);
1237      }      }
1238      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1239           result.Error(e);           result.Error(e);
# Line 348  String LSCPServer::SetMIDIInputType(Midi Line 1241  String LSCPServer::SetMIDIInputType(Midi
1241      return result.Produce();      return result.Produce();
1242  }  }
1243    
1244  /**  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1245   * Will be called by the parser to change the MIDI input port on which the      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1246   * engine of a particular sampler channel should listen to.      LSCPResultSet result;
1247   */      try {
1248  String LSCPServer::SetMIDIInputPort(String MIDIInputPort, uint uiSamplerChannel) {          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1249      dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIInputPort=%s, Samplerchannel=%d)\n", MIDIInputPort.c_str(), uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1250            // Driver type name aliasing...
1251            if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1252            if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1253            // Check if there's one audio output device already created
1254            // for the intended audio driver type (AudioOutputDriver)...
1255            AudioOutputDevice *pDevice = NULL;
1256            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1257            std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1258            for (; iter != devices.end(); iter++) {
1259                if ((iter->second)->Driver() == AudioOutputDriver) {
1260                    pDevice = iter->second;
1261                    break;
1262                }
1263            }
1264            // If it doesn't exist, create a new one with default parameters...
1265            if (pDevice == NULL) {
1266                std::map<String,String> params;
1267                pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1268            }
1269            // Must have a device...
1270            if (pDevice == NULL)
1271                throw LinuxSamplerException("Internal error: could not create audio output device.");
1272            // Set it as the current channel device...
1273            pSamplerChannel->SetAudioOutputDevice(pDevice);
1274        }
1275        catch (LinuxSamplerException e) {
1276             result.Error(e);
1277        }
1278        return result.Produce();
1279    }
1280    
1281    String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1282        dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel));
1283        LSCPResultSet result;
1284        try {
1285            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1286            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1287            pSamplerChannel->SetMidiInputPort(MIDIPort);
1288        }
1289        catch (LinuxSamplerException e) {
1290             result.Error(e);
1291        }
1292        return result.Produce();
1293    }
1294    
1295    String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1296        dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
1297      LSCPResultSet result;      LSCPResultSet result;
1298      try {      try {
1299          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1300          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1301          if (!pSamplerChannel->GetMidiInputDevice()) throw LinuxSamplerException("No MIDI input device connected yet");          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1302          pSamplerChannel->GetMidiInputDevice()->SetInputPort(MIDIInputPort.c_str());      }
1303        catch (LinuxSamplerException e) {
1304             result.Error(e);
1305        }
1306        return result.Produce();
1307    }
1308    
1309    String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1310        dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1311        LSCPResultSet result;
1312        try {
1313            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1314            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1315            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1316            if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1317            MidiInputDevice* pDevice = devices[MIDIDeviceId];
1318            pSamplerChannel->SetMidiInputDevice(pDevice);
1319        }
1320        catch (LinuxSamplerException e) {
1321             result.Error(e);
1322        }
1323        return result.Produce();
1324    }
1325    
1326    String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1327        dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1328        LSCPResultSet result;
1329        try {
1330            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1331            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1332            // Driver type name aliasing...
1333            if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1334            // Check if there's one MIDI input device already created
1335            // for the intended MIDI driver type (MidiInputDriver)...
1336            MidiInputDevice *pDevice = NULL;
1337            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1338            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1339            for (; iter != devices.end(); iter++) {
1340                if ((iter->second)->Driver() == MidiInputDriver) {
1341                    pDevice = iter->second;
1342                    break;
1343                }
1344            }
1345            // If it doesn't exist, create a new one with default parameters...
1346            if (pDevice == NULL) {
1347                std::map<String,String> params;
1348                pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1349                // Make it with at least one initial port.
1350                std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1351                parameters["PORTS"]->SetValue("1");
1352            }
1353            // Must have a device...
1354            if (pDevice == NULL)
1355                throw LinuxSamplerException("Internal error: could not create MIDI input device.");
1356            // Set it as the current channel device...
1357            pSamplerChannel->SetMidiInputDevice(pDevice);
1358      }      }
1359      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1360           result.Error(e);           result.Error(e);
# Line 368  String LSCPServer::SetMIDIInputPort(Stri Line 1363  String LSCPServer::SetMIDIInputPort(Stri
1363  }  }
1364    
1365  /**  /**
1366   * Will be called by the parser to change the MIDI input channel on which the   * Will be called by the parser to change the MIDI input device, port and channel on which
1367   * engine of a particular sampler channel should listen to.   * engine of a particular sampler channel should listen to.
1368   */   */
1369  String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {  String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1370      dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n", MIDIChannel, uiSamplerChannel));      dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1371      LSCPResultSet result;      LSCPResultSet result;
1372      try {      try {
1373          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1374          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1375          if (!pSamplerChannel->GetMidiInputDevice()) throw LinuxSamplerException("No MIDI input device connected yet");          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1376          MidiInputDevice::type_t oldtype = pSamplerChannel->GetMidiInputDevice()->Type();          if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1377          pSamplerChannel->SetMidiInputDevice(oldtype, (MidiInputDevice::midi_chan_t) MIDIChannel);          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1378            pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
1379      }      }
1380      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1381           result.Error(e);           result.Error(e);
# Line 391  String LSCPServer::SetMIDIInputChannel(u Line 1387  String LSCPServer::SetMIDIInputChannel(u
1387   * 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
1388   * particular sampler channel.   * particular sampler channel.
1389   */   */
1390  String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) {  String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1391      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1392      LSCPResultSet result;      LSCPResultSet result;
1393      try {      try {
1394          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1395          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1396          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1397          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1398          pEngine->Volume(Volume);          pEngineChannel->Volume(dVolume);
1399      }      }
1400      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1401           result.Error(e);           result.Error(e);
# Line 408  String LSCPServer::SetVolume(double Volu Line 1404  String LSCPServer::SetVolume(double Volu
1404  }  }
1405    
1406  /**  /**
1407     * Will be called by the parser to mute/unmute particular sampler channel.
1408     */
1409    String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1410        dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1411        LSCPResultSet result;
1412        try {
1413            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1414            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1415    
1416            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1417            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1418    
1419            if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1420            else pEngineChannel->SetMute(1);
1421        } catch (LinuxSamplerException e) {
1422            result.Error(e);
1423        }
1424        return result.Produce();
1425    }
1426    
1427    /**
1428     * Will be called by the parser to solo particular sampler channel.
1429     */
1430    String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1431        dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1432        LSCPResultSet result;
1433        try {
1434            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1435            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1436    
1437            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1438            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1439    
1440            bool oldSolo = pEngineChannel->GetSolo();
1441            bool hadSoloChannel = HasSoloChannel();
1442            
1443            pEngineChannel->SetSolo(bSolo);
1444            
1445            if(!oldSolo && bSolo) {
1446                if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1447                if(!hadSoloChannel) MuteNonSoloChannels();
1448            }
1449            
1450            if(oldSolo && !bSolo) {
1451                if(!HasSoloChannel()) UnmuteChannels();
1452                else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1453            }
1454        } catch (LinuxSamplerException e) {
1455            result.Error(e);
1456        }
1457        return result.Produce();
1458    }
1459    
1460    /**
1461     * Determines whether there is at least one solo channel in the channel list.
1462     *
1463     * @returns true if there is at least one solo channel in the channel list,
1464     * false otherwise.
1465     */
1466    bool LSCPServer::HasSoloChannel() {
1467        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1468        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1469        for (; iter != channels.end(); iter++) {
1470            EngineChannel* c = iter->second->GetEngineChannel();
1471            if(c && c->GetSolo()) return true;
1472        }
1473    
1474        return false;
1475    }
1476    
1477    /**
1478     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1479     * with -1 which indicates that they are muted because of the presence
1480     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1481     * when there are no solo channels left.
1482     */
1483    void LSCPServer::MuteNonSoloChannels() {
1484        dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1485        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1486        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1487        for (; iter != channels.end(); iter++) {
1488            EngineChannel* c = iter->second->GetEngineChannel();
1489            if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1490        }
1491    }
1492    
1493    /**
1494     * Unmutes all channels that are muted because of the presence
1495     * of a solo channel(s).
1496     */
1497    void  LSCPServer::UnmuteChannels() {
1498        dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1499        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1500        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1501        for (; iter != channels.end(); iter++) {
1502            EngineChannel* c = iter->second->GetEngineChannel();
1503            if(c && c->GetMute() == -1) c->SetMute(0);
1504        }
1505    }
1506    
1507    /**
1508   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
1509   */   */
1510  String LSCPServer::ResetChannel(uint uiSamplerChannel) {  String LSCPServer::ResetChannel(uint uiSamplerChannel) {
# Line 415  String LSCPServer::ResetChannel(uint uiS Line 1512  String LSCPServer::ResetChannel(uint uiS
1512      LSCPResultSet result;      LSCPResultSet result;
1513      try {      try {
1514          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1515          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1516          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1517          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1518          pEngine->Reset();          pEngineChannel->Reset();
1519      }      }
1520      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1521           result.Error(e);           result.Error(e);
# Line 427  String LSCPServer::ResetChannel(uint uiS Line 1524  String LSCPServer::ResetChannel(uint uiS
1524  }  }
1525    
1526  /**  /**
1527     * Will be called by the parser to reset the whole sampler.
1528     */
1529    String LSCPServer::ResetSampler() {
1530        dmsg(2,("LSCPServer: ResetSampler()\n"));
1531        pSampler->Reset();
1532        LSCPResultSet result;
1533        return result.Produce();
1534    }
1535    
1536    /**
1537     * Will be called by the parser to return general informations about this
1538     * sampler.
1539     */
1540    String LSCPServer::GetServerInfo() {
1541        dmsg(2,("LSCPServer: GetServerInfo()\n"));
1542        LSCPResultSet result;
1543        result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");
1544        result.Add("VERSION", VERSION);
1545        result.Add("PROTOCOL_VERSION", "1.0");
1546        return result.Produce();
1547    }
1548    
1549    /**
1550     * Will be called by the parser to return the current number of all active voices.
1551     */
1552    String LSCPServer::GetTotalVoiceCount() {
1553        dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
1554        LSCPResultSet result;
1555        result.Add(pSampler->GetVoiceCount());
1556        return result.Produce();
1557    }
1558    
1559    /**
1560     * Will be called by the parser to return the maximum number of voices.
1561     */
1562    String LSCPServer::GetTotalVoiceCountMax() {
1563        dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
1564        LSCPResultSet result;
1565        result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);
1566        return result.Produce();
1567    }
1568    
1569    /**
1570   * 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
1571   * server for receiving event messages.   * server for receiving event messages.
1572   */   */
1573  String LSCPServer::SubscribeNotification(uint UDPPort) {  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
1574      dmsg(2,("LSCPServer: SubscribeNotification(UDPPort=%d)\n", UDPPort));      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1575      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1576        SubscriptionMutex.Lock();
1577        eventSubscriptions[type].push_back(currentSocket);
1578        SubscriptionMutex.Unlock();
1579        return result.Produce();
1580  }  }
1581    
1582  /**  /**
1583   * Will be called by the parser to unsubscribe a client on the server   * Will be called by the parser to unsubscribe a client on the server
1584   * for not receiving further event messages.   * for not receiving further event messages.
1585   */   */
1586  String LSCPServer::UnsubscribeNotification(String SessionID) {  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
1587      dmsg(2,("LSCPServer: UnsubscribeNotification(SessionID=%s)\n", SessionID.c_str()));      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1588      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1589        SubscriptionMutex.Lock();
1590        eventSubscriptions[type].remove(currentSocket);
1591        SubscriptionMutex.Unlock();
1592        return result.Produce();
1593    }
1594    
1595    static int select_callback(void * lscpResultSet, int argc,
1596                            char **argv, char **azColName)
1597    {
1598        LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;
1599        resultSet->Add(argc, argv);
1600        return 0;
1601    }
1602    
1603    String LSCPServer::QueryDatabase(String query) {
1604        LSCPResultSet result;
1605    #if HAVE_SQLITE3
1606        char* zErrMsg = NULL;
1607        sqlite3 *db;
1608        String selectStr = "SELECT " + query;
1609    
1610        int rc = sqlite3_open("linuxsampler.db", &db);
1611        if (rc == SQLITE_OK)
1612        {
1613                rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);
1614        }
1615        if ( rc != SQLITE_OK )
1616        {
1617                result.Error(String(zErrMsg), rc);
1618        }
1619        sqlite3_close(db);
1620    #else
1621        result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);
1622    #endif
1623        return result.Produce();
1624    }
1625    
1626    /**
1627     * Will be called by the parser to enable or disable echo mode; if echo
1628     * mode is enabled, all commands from the client will (immediately) be
1629     * echoed back to the client.
1630     */
1631    String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
1632        dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
1633        LSCPResultSet result;
1634        try {
1635            if      (boolean_value == 0) pSession->bVerbose = false;
1636            else if (boolean_value == 1) pSession->bVerbose = true;
1637            else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1");
1638        }
1639        catch (LinuxSamplerException e) {
1640             result.Error(e);
1641        }
1642        return result.Produce();
1643  }  }

Legend:
Removed from v.121  
changed lines
  Added in v.778

  ViewVC Help
Powered by ViewVC