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

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

  ViewVC Help
Powered by ViewVC