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

Legend:
Removed from v.35  
changed lines
  Added in v.793

  ViewVC Help
Powered by ViewVC