/[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 210 by schoenebeck, Sat Jul 24 12:33:49 2004 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   *                                                                         *   *                                                                         *
7   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
8   *   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  *
# Line 21  Line 21 
21   ***************************************************************************/   ***************************************************************************/
22    
23  #include "lscpserver.h"  #include "lscpserver.h"
24    #include "lscpresultset.h"
25    #include "lscpevent.h"
26    
27  LSCPServer::LSCPServer(AudioThread* pEngine) : Thread(false, 0, -4) {  #include "../engines/gig/Engine.h"
28      this->pEngine = pEngine;  #include "../drivers/audio/AudioOutputDeviceFactory.h"
29    #include "../drivers/midi/MidiInputDeviceFactory.h"
30    
31    /**
32     * Below are a few static members of the LSCPServer class.
33     * The big assumption here is that LSCPServer is going to remain a singleton.
34     * These members are used to support client connections.
35     * Class handles multiple connections at the same time using select() and non-blocking recv()
36     * Commands are processed by a single LSCPServer thread.
37     * Notifications are delivered either by the thread that originated them
38     * or (if the resultset is currently in progress) by the LSCPServer thread
39     * after the resultset was sent out.
40     * This makes sure that resultsets can not be interrupted by notifications.
41     * This also makes sure that the thread sending notification is not blocked
42     * by the LSCPServer thread.
43     */
44    fd_set LSCPServer::fdSet;
45    int LSCPServer::currentSocket = -1;
46    std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
47    std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
48    std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
49    std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
50    Mutex LSCPServer::NotifyMutex = Mutex();
51    Mutex LSCPServer::NotifyBufferMutex = Mutex();
52    Mutex LSCPServer::SubscriptionMutex = Mutex();
53    
54    LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) {
55        this->pSampler = pSampler;
56        LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS");
57        LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
58        LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
59        LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
60        LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO");
61        LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
62  }  }
63    
64  int LSCPServer::Main() {  int LSCPServer::Main() {
65      hSocket = socket(AF_INET, SOCK_STREAM, 0);      int hSocket = socket(AF_INET, SOCK_STREAM, 0);
66      if (hSocket < 0) {      if (hSocket < 0) {
67          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
68          return -1;          //return -1;
69            exit(EXIT_FAILURE);
70      }      }
71    
72      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
# Line 40  int LSCPServer::Main() { Line 76  int LSCPServer::Main() {
76      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
77          std::cerr << "LSCPServer: Could not bind server socket." << std::endl;          std::cerr << "LSCPServer: Could not bind server socket." << std::endl;
78          close(hSocket);          close(hSocket);
79          return -1;          //return -1;
80            exit(EXIT_FAILURE);
81      }      }
82    
83      listen(hSocket, 1);      listen(hSocket, 1);
# Line 49  int LSCPServer::Main() { Line 86  int LSCPServer::Main() {
86      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
87      sockaddr_in client;      sockaddr_in client;
88      int length = sizeof(client);      int length = sizeof(client);
89      while (true) {      FD_ZERO(&fdSet);
90          hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);      FD_SET(hSocket, &fdSet);
91          if (hSession < 0) {      int maxSessions = hSocket;
             std::cerr << "LSCPServer: Client connection failed." << std::endl;  
             close(hSocket);  
             return -1;  
         }  
   
         dmsg(1,("LSCPServer: Client connection established.\n"));  
         //send(hSession, "Welcome!\r\n", 10, 0);  
   
         // 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);  
92    
93          close(hSession);      while (true) {
94          dmsg(1,("LSCPServer: Client connection terminated.\n"));          fd_set selectSet = fdSet;
95            int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);
96            if (retval == 0)
97                    continue; //Nothing try again
98            if (retval == -1) {
99                    std::cerr << "LSCPServer: Socket select error." << std::endl;
100                    close(hSocket);
101                    exit(EXIT_FAILURE);
102            }
103    
104            //Accept new connections now (if any)
105            if (FD_ISSET(hSocket, &selectSet)) {
106                    int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
107                    if (socket < 0) {
108                            std::cerr << "LSCPServer: Client connection failed." << std::endl;
109                            exit(EXIT_FAILURE);
110                    }
111    
112                    if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
113                            std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
114                            exit(EXIT_FAILURE);
115                    }
116    
117                    // Parser initialization
118                    yyparse_param_t yyparse_param;
119                    yyparse_param.pServer  = this;
120                    yyparse_param.hSession = socket;
121    
122                    Sessions.push_back(yyparse_param);
123                    FD_SET(socket, &fdSet);
124                    if (socket > maxSessions)
125                            maxSessions = socket;
126                    dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
127                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
128                    continue; //Maybe this was the only selected socket, better select again
129            }
130    
131            //Something was selected and it was not the hSocket, so it must be some command(s) coming.
132            for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
133                    if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?
134                            if (GetLSCPCommand(iter)) {     //Have we read the entire command?
135                                    dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
136                                    yylex_init(&((*iter).pScanner)); //FIXME: should me moved out of this loop and initialized only when a new session is created
137                                    currentSocket = (*iter).hSession;  //a hack
138                                    if ((*iter).bVerbose) { // if echo mode enabled
139                                        AnswerClient(bufferedCommands[currentSocket]);
140                                    }
141                                    int result = yyparse(&(*iter));
142                                    currentSocket = -1;     //continuation of a hack
143                                    dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
144                                    if (result == LSCP_QUIT) { //Was it a quit command by any chance?
145                                            CloseConnection(iter);
146                                    }
147                            }
148                            //socket may have been closed, iter may be invalid, get out of the loop for now.
149                            //we'll be back if there is data.
150                            break;
151                    }
152            }
153    
154            //Now let's deliver late notifies (if any)
155            NotifyBufferMutex.Lock();
156            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
157                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
158                    bufferedNotifies.erase(iterNotify);
159            }
160            NotifyBufferMutex.Unlock();
161      }      }
162        //It will never get here anyway
163        //yylex_destroy(yyparse_param.pScanner);
164    }
165    
166    void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
167            int socket = (*iter).hSession;
168            dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
169            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
170            Sessions.erase(iter);
171            FD_CLR(socket,  &fdSet);
172            SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
173            for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
174                    iter->second.remove(socket);
175            }
176            SubscriptionMutex.Unlock();
177            NotifyMutex.Lock();
178            bufferedCommands.erase(socket);
179            bufferedNotifies.erase(socket);
180            close(socket);
181            NotifyMutex.Unlock();
182            //yylex_destroy((*iter).pScanner);
183    }
184    
185    void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
186            SubscriptionMutex.Lock();
187            if (eventSubscriptions.count(event.GetType()) == 0) {
188                    SubscriptionMutex.Unlock();     //Nobody is subscribed to this event
189                    return;
190            }
191            std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
192            std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
193            String notify = event.Produce();
194    
195            while (true) {
196                    if (NotifyMutex.Trylock()) {
197                            for(;iter != end; iter++)
198                                    send(*iter, notify.c_str(), notify.size(), 0);
199                            NotifyMutex.Unlock();
200                            break;
201                    } else {
202                            if (NotifyBufferMutex.Trylock()) {
203                                    for(;iter != end; iter++)
204                                            bufferedNotifies[*iter] += notify;
205                                    NotifyBufferMutex.Unlock();
206                                    break;
207                            }
208                    }
209            }
210            SubscriptionMutex.Unlock();
211    }
212    
213    extern int GetLSCPCommand( void *buf, int max_size ) {
214            String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
215            if (command.size() == 0) {              //Parser wants input but we have nothing.
216                    strcpy((char*) buf, "\n");      //So give it an empty command
217                    return 1;                       //to keep it happy.
218            }
219    
220            if (max_size < command.size()) {
221                    std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
222                    return 0;       //This will never happen
223            }
224    
225            strcpy((char*) buf, command.c_str());
226            LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
227            return command.size();
228    }
229    
230    /**
231     * Will be called to try to read the command from the socket
232     * If command is read, it will return true. Otherwise false is returned.
233     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
234     */
235    bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
236            int socket = (*iter).hSession;
237            char c;
238            int i = 0;
239            while (true) {
240                    int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
241                    if (result == 0) { //socket was selected, so 0 here means client has closed the connection
242                            CloseConnection(iter);
243                            break;
244                    }
245                    if (result == 1) {
246                            if (c == '\r')
247                                    continue; //Ignore CR
248                            if (c == '\n') {
249                                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
250                                    bufferedCommands[socket] += "\n";
251                                    return true; //Complete command was read
252                            }
253                            bufferedCommands[socket] += c;
254                    }
255                    if (result == -1) {
256                            if (errno == EAGAIN) //Would block, try again later.
257                                    return false;
258                            switch(errno) {
259                                    case EBADF:
260                                            dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
261                                            break;
262                                    case ECONNREFUSED:
263                                            dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
264                                            break;
265                                    case ENOTCONN:
266                                            dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
267                                            break;
268                                    case ENOTSOCK:
269                                            dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
270                                            break;
271                                    case EAGAIN:
272                                            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"));
273                                            break;
274                                    case EINTR:
275                                            dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
276                                            break;
277                                    case EFAULT:
278                                            dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
279                                            break;
280                                    case EINVAL:
281                                            dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
282                                            break;
283                                    case ENOMEM:
284                                            dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
285                                            break;
286                                    default:
287                                            dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
288                                            break;
289                            }
290                            CloseConnection(iter);
291                            break;
292                    }
293            }
294            return false;
295  }  }
296    
297  /**  /**
# Line 80  int LSCPServer::Main() { Line 302  int LSCPServer::Main() {
302   */   */
303  void LSCPServer::AnswerClient(String ReturnMessage) {  void LSCPServer::AnswerClient(String ReturnMessage) {
304      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
305      send(hSession, ReturnMessage.c_str(), ReturnMessage.size(), 0);      if (currentSocket != -1) {
306                NotifyMutex.Lock();
307                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
308                NotifyMutex.Unlock();
309        }
310    }
311    
312    /**
313     * Find a created audio output device index.
314     */
315    int LSCPServer::GetAudioOutputDeviceIndex ( AudioOutputDevice *pDevice )
316    {
317        // Search for the created device to get its index
318        std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
319        std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
320        for (; iter != devices.end(); iter++) {
321            if (iter->second == pDevice)
322                return iter->first;
323        }
324        // Not found.
325        return -1;
326    }
327    
328    /**
329     * Find a created midi input device index.
330     */
331    int LSCPServer::GetMidiInputDeviceIndex ( MidiInputDevice *pDevice )
332    {
333        // Search for the created device to get its index
334        std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
335        std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
336        for (; iter != devices.end(); iter++) {
337            if (iter->second == pDevice)
338                return iter->first;
339        }
340        // Not found.
341        return -1;
342    }
343    
344    String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
345        dmsg(2,("LSCPServer: CreateAudioOutputDevice(Driver=%s)\n", Driver.c_str()));
346        LSCPResultSet result;
347        try {
348            AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
349            // search for the created device to get its index
350            int index = GetAudioOutputDeviceIndex(pDevice);
351            if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");
352            result = index; // success
353        }
354        catch (LinuxSamplerException e) {
355            result.Error(e);
356        }
357        return result.Produce();
358    }
359    
360    String LSCPServer::CreateMidiInputDevice(String Driver, std::map<String,String> Parameters) {
361        dmsg(2,("LSCPServer: CreateMidiInputDevice(Driver=%s)\n", Driver.c_str()));
362        LSCPResultSet result;
363        try {
364            MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
365            // search for the created device to get its index
366            int index = GetMidiInputDeviceIndex(pDevice);
367            if (index == -1) throw LinuxSamplerException("Internal error: could not find created midi input device.");
368            result = index; // success
369        }
370        catch (LinuxSamplerException e) {
371            result.Error(e);
372        }
373        return result.Produce();
374    }
375    
376    String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
377        dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
378        LSCPResultSet result;
379        try {
380            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
381            if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
382            AudioOutputDevice* pDevice = devices[DeviceIndex];
383            pSampler->DestroyAudioOutputDevice(pDevice);
384        }
385        catch (LinuxSamplerException e) {
386            result.Error(e);
387        }
388        return result.Produce();
389    }
390    
391    String LSCPServer::DestroyMidiInputDevice(uint DeviceIndex) {
392        dmsg(2,("LSCPServer: DestroyMidiInputDevice(DeviceIndex=%d)\n", DeviceIndex));
393        LSCPResultSet result;
394        try {
395            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
396            MidiInputDevice* pDevice = devices[DeviceIndex];
397            if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
398            pSampler->DestroyMidiInputDevice(pDevice);
399        }
400        catch (LinuxSamplerException e) {
401            result.Error(e);
402        }
403        return result.Produce();
404  }  }
405    
406  /**  /**
407   * Will be called by the parser to load an instrument.   * Will be called by the parser to load an instrument.
408   */   */
409  String LSCPServer::LoadInstrument(String Filename, uint Instrument, uint SamplerChannel) {  String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground) {
410      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));
411      result_t res = pEngine->LoadInstrument(Filename.c_str(), Instrument);      LSCPResultSet result;
412      return ConvertResult(res);      try {
413            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
414            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
415            Engine* pEngine = pSamplerChannel->GetEngine();
416            if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
417            if (pSamplerChannel->GetAudioOutputDevice() == NULL)
418                throw LinuxSamplerException("No audio output device on channel");
419            if (bBackground) {
420                LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument);
421                pLoadInstrument->StartThread();
422            }
423            else pEngine->LoadInstrument(Filename.c_str(), uiInstrument);
424        }
425        catch (LinuxSamplerException e) {
426             result.Error(e);
427        }
428        return result.Produce();
429  }  }
430    
431  /**  /**
432   * Will be called by the parser to load and deploy an engine.   * Will be called by the parser to load and deploy an engine.
433   */   */
434  String LSCPServer::LoadEngine(String EngineName, uint SamplerChannel) {  String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) {
435      dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), SamplerChannel));      dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
436      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
437        try {
438            Engine::type_t type;
439            if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig;
440            else throw LinuxSamplerException("Unknown engine type");
441            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
442            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
443            pSamplerChannel->LoadEngine(type);
444        }
445        catch (LinuxSamplerException e) {
446             result.Error(e);
447        }
448        return result.Produce();
449  }  }
450    
451  /**  /**
# Line 105  String LSCPServer::LoadEngine(String Eng Line 453  String LSCPServer::LoadEngine(String Eng
453   */   */
454  String LSCPServer::GetChannels() {  String LSCPServer::GetChannels() {
455      dmsg(2,("LSCPServer: GetChannels()\n"));      dmsg(2,("LSCPServer: GetChannels()\n"));
456      return "1\r\n";      LSCPResultSet result;
457        result.Add(pSampler->SamplerChannels());
458        return result.Produce();
459    }
460    
461    /**
462     * Will be called by the parser to get the list of sampler channels.
463     */
464    String LSCPServer::ListChannels() {
465        dmsg(2,("LSCPServer: ListChannels()\n"));
466        String list;
467        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
468        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
469        for (; iter != channels.end(); iter++) {
470            if (list != "") list += ",";
471            list += ToString(iter->first);
472        }
473        LSCPResultSet result;
474        result.Add(list);
475        return result.Produce();
476  }  }
477    
478  /**  /**
# Line 113  String LSCPServer::GetChannels() { Line 480  String LSCPServer::GetChannels() {
480   */   */
481  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
482      dmsg(2,("LSCPServer: AddChannel()\n"));      dmsg(2,("LSCPServer: AddChannel()\n"));
483      return "ERR:0:Not implemented yet.\r\n";      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
484        LSCPResultSet result(pSamplerChannel->Index());
485        return result.Produce();
486  }  }
487    
488  /**  /**
489   * Will be called by the parser to remove a sampler channel.   * Will be called by the parser to remove a sampler channel.
490   */   */
491  String LSCPServer::RemoveChannel(uint SamplerChannel) {  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
492      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", SamplerChannel));      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
493      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
494        pSampler->RemoveSamplerChannel(uiSamplerChannel);
495        return result.Produce();
496  }  }
497    
498  /**  /**
# Line 129  String LSCPServer::RemoveChannel(uint Sa Line 500  String LSCPServer::RemoveChannel(uint Sa
500   */   */
501  String LSCPServer::GetAvailableEngines() {  String LSCPServer::GetAvailableEngines() {
502      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
503      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result("GigEngine");
504        return result.Produce();
505  }  }
506    
507  /**  /**
# Line 137  String LSCPServer::GetAvailableEngines() Line 509  String LSCPServer::GetAvailableEngines()
509   */   */
510  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
511      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
512      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
513        try {
514            if ((EngineName == "GigEngine") || (EngineName == "gig")) {
515                Engine* pEngine = new LinuxSampler::gig::Engine;
516                result.Add(pEngine->Description());
517                result.Add(pEngine->Version());
518                delete pEngine;
519            }
520            else throw LinuxSamplerException("Unknown engine type");
521        }
522        catch (LinuxSamplerException e) {
523             result.Error(e);
524        }
525        return result.Produce();
526  }  }
527    
528  /**  /**
529   * Will be called by the parser to get informations about a particular   * Will be called by the parser to get informations about a particular
530   * sampler channel.   * sampler channel.
531   */   */
532  String LSCPServer::GetChannelInfo(uint SamplerChannel) {  String LSCPServer::GetChannelInfo(uint uiSamplerChannel) {
533      dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", SamplerChannel));      dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", uiSamplerChannel));
534      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
535        try {
536            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
537            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
538            Engine* pEngine = pSamplerChannel->GetEngine();
539    
540            //Defaults values
541            String EngineName = "NONE";
542            float Volume = 0;
543            String InstrumentFileName = "NONE";
544            int InstrumentIndex = -1;
545            int InstrumentStatus = -1;
546    
547            if (pEngine) {
548                EngineName =  pEngine->EngineName();
549                Volume = pEngine->Volume();
550                InstrumentStatus = pEngine->InstrumentStatus();
551                InstrumentIndex = pEngine->InstrumentIndex();
552                if (InstrumentIndex != -1)
553                    InstrumentFileName = pEngine->InstrumentFileName();
554            }
555    
556            result.Add("ENGINE_NAME", EngineName);
557            result.Add("VOLUME", Volume);
558    
559            //Some not-so-hardcoded stuff to make GUI look good
560            result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
561            result.Add("AUDIO_OUTPUT_CHANNELS", "2");
562            result.Add("AUDIO_OUTPUT_ROUTING", "0,1");
563    
564            result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
565            result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
566            result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
567    
568            result.Add("INSTRUMENT_FILE", InstrumentFileName);
569            result.Add("INSTRUMENT_NR", InstrumentIndex);
570            result.Add("INSTRUMENT_STATUS", InstrumentStatus);
571        }
572        catch (LinuxSamplerException e) {
573             result.Error(e);
574        }
575        return result.Produce();
576  }  }
577    
578  /**  /**
579   * 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
580   * particular sampler channel.   * particular sampler channel.
581   */   */
582  String LSCPServer::GetVoiceCount(uint SamplerChannel) {  String LSCPServer::GetVoiceCount(uint uiSamplerChannel) {
583      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", SamplerChannel));      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
584      return ToString(pEngine->ActiveVoiceCount) + "\r\n";      LSCPResultSet result;
585        try {
586            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
587            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
588            Engine* pEngine = pSamplerChannel->GetEngine();
589            if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
590            result.Add(pEngine->VoiceCount());
591        }
592        catch (LinuxSamplerException e) {
593             result.Error(e);
594        }
595        return result.Produce();
596  }  }
597    
598  /**  /**
599   * 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
600   * particular sampler channel.   * particular sampler channel.
601   */   */
602  String LSCPServer::GetStreamCount(uint SamplerChannel) {  String LSCPServer::GetStreamCount(uint uiSamplerChannel) {
603      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", SamplerChannel));      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
604      return ToString(pEngine->pDiskThread->ActiveStreamCount) + "\r\n";      LSCPResultSet result;
605        try {
606            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
607            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
608            Engine* pEngine = pSamplerChannel->GetEngine();
609            if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
610            result.Add(pEngine->DiskStreamCount());
611        }
612        catch (LinuxSamplerException e) {
613             result.Error(e);
614        }
615        return result.Produce();
616  }  }
617    
618  /**  /**
619   * 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
620   * streams on a particular sampler channel.   * streams on a particular sampler channel.
621   */   */
622  String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint SamplerChannel) {  String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel) {
623      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, SamplerChannel));      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
624      return (ResponseType == fill_response_bytes) ? pEngine->pDiskThread->GetBufferFillBytes() + "\r\n"      LSCPResultSet result;
625                                                   : pEngine->pDiskThread->GetBufferFillPercentage() + "\r\n";      try {
626            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
627            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
628            Engine* pEngine = pSamplerChannel->GetEngine();
629            if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
630            if (!pEngine->DiskStreamSupported())
631                result.Add("NA");
632            else {
633                switch (ResponseType) {
634                    case fill_response_bytes:
635                        result.Add(pEngine->DiskStreamBufferFillBytes());
636                        break;
637                    case fill_response_percentage:
638                        result.Add(pEngine->DiskStreamBufferFillPercentage());
639                        break;
640                    default:
641                        throw LinuxSamplerException("Unknown fill response type");
642                }
643            }
644        }
645        catch (LinuxSamplerException e) {
646             result.Error(e);
647        }
648        return result.Produce();
649  }  }
650    
651  /**  String LSCPServer::GetAvailableAudioOutputDrivers() {
652   * Will be called by the parser to change the audio output type on a      dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
653   * particular sampler channel.      LSCPResultSet result;
654   */      try {
655  String LSCPServer::SetAudioOutputType(audio_output_type_t AudioOutputType, uint SamplerChannel) {          String s = AudioOutputDeviceFactory::AvailableDriversAsString();
656      dmsg(2,("LSCPServer: SetAudioOutputType(AudioOutputType=%d, SamplerChannel=%d)\n", AudioOutputType, SamplerChannel));          result.Add(s);
657      return "ERR:0:Not implemented yet.\r\n";      }
658        catch (LinuxSamplerException e) {
659            result.Error(e);
660        }
661        return result.Produce();
662    }
663    
664    String LSCPServer::GetAvailableMidiInputDrivers() {
665        dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
666        LSCPResultSet result;
667        try {
668            String s = MidiInputDeviceFactory::AvailableDriversAsString();
669            result.Add(s);
670        }
671        catch (LinuxSamplerException e) {
672            result.Error(e);
673        }
674        return result.Produce();
675    }
676    
677    String LSCPServer::GetMidiInputDriverInfo(String Driver) {
678        dmsg(2,("LSCPServer: GetMidiInputDriverInfo(Driver=%s)\n",Driver.c_str()));
679        LSCPResultSet result;
680        try {
681            result.Add("DESCRIPTION", MidiInputDeviceFactory::GetDriverDescription(Driver));
682            result.Add("VERSION",     MidiInputDeviceFactory::GetDriverVersion(Driver));
683    
684            std::map<String,DeviceCreationParameter*> parameters = MidiInputDeviceFactory::GetAvailableDriverParameters(Driver);
685            if (parameters.size()) { // if there are parameters defined for this driver
686                String s;
687                std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
688                for (;iter != parameters.end(); iter++) {
689                    if (s != "") s += ",";
690                    s += iter->first;
691                }
692                result.Add("PARAMETERS", s);
693            }
694        }
695        catch (LinuxSamplerException e) {
696            result.Error(e);
697        }
698        return result.Produce();
699    }
700    
701    String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
702        dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
703        LSCPResultSet result;
704        try {
705            result.Add("DESCRIPTION", AudioOutputDeviceFactory::GetDriverDescription(Driver));
706            result.Add("VERSION",     AudioOutputDeviceFactory::GetDriverVersion(Driver));
707    
708            std::map<String,DeviceCreationParameter*> parameters = AudioOutputDeviceFactory::GetAvailableDriverParameters(Driver);
709            if (parameters.size()) { // if there are parameters defined for this driver
710                String s;
711                std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
712                for (;iter != parameters.end(); iter++) {
713                    if (s != "") s += ",";
714                    s += iter->first;
715                }
716                result.Add("PARAMETERS", s);
717            }
718        }
719        catch (LinuxSamplerException e) {
720            result.Error(e);
721        }
722        return result.Produce();
723    }
724    
725    String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
726        dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str()));
727        LSCPResultSet result;
728        try {
729            DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
730            result.Add("TYPE",         pParameter->Type());
731            result.Add("DESCRIPTION",  pParameter->Description());
732            result.Add("MANDATORY",    pParameter->Mandatory());
733            result.Add("FIX",          pParameter->Fix());
734            result.Add("MULTIPLICITY", pParameter->Multiplicity());
735            if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());
736            if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());
737            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());
738            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());
739            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
740        }
741        catch (LinuxSamplerException e) {
742            result.Error(e);
743        }
744        return result.Produce();
745    }
746    
747    String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
748        dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str()));
749        LSCPResultSet result;
750        try {
751            DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
752            result.Add("TYPE",         pParameter->Type());
753            result.Add("DESCRIPTION",  pParameter->Description());
754            result.Add("MANDATORY",    pParameter->Mandatory());
755            result.Add("FIX",          pParameter->Fix());
756            result.Add("MULTIPLICITY", pParameter->Multiplicity());
757            if (pParameter->Depends())       result.Add("DEPENDS",       pParameter->Depends());
758            if (pParameter->Default())       result.Add("DEFAULT",       pParameter->Default());
759            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());
760            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());
761            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
762        }
763        catch (LinuxSamplerException e) {
764            result.Error(e);
765        }
766        return result.Produce();
767    }
768    
769    String LSCPServer::GetAudioOutputDeviceCount() {
770        dmsg(2,("LSCPServer: GetAudioOutputDeviceCount()\n"));
771        LSCPResultSet result;
772        try {
773            uint count = pSampler->AudioOutputDevices();
774            result.Add(count); // success
775        }
776        catch (LinuxSamplerException e) {
777            result.Error(e);
778        }
779        return result.Produce();
780    }
781    
782    String LSCPServer::GetMidiInputDeviceCount() {
783        dmsg(2,("LSCPServer: GetMidiInputDeviceCount()\n"));
784        LSCPResultSet result;
785        try {
786            uint count = pSampler->MidiInputDevices();
787            result.Add(count); // success
788        }
789        catch (LinuxSamplerException e) {
790            result.Error(e);
791        }
792        return result.Produce();
793    }
794    
795    String LSCPServer::GetAudioOutputDevices() {
796        dmsg(2,("LSCPServer: GetAudioOutputDevices()\n"));
797        LSCPResultSet result;
798        try {
799            String s;
800            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
801            std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
802            for (; iter != devices.end(); iter++) {
803                if (s != "") s += ",";
804                s += ToString(iter->first);
805            }
806            result.Add(s);
807        }
808        catch (LinuxSamplerException e) {
809            result.Error(e);
810        }
811        return result.Produce();
812    }
813    
814    String LSCPServer::GetMidiInputDevices() {
815        dmsg(2,("LSCPServer: GetMidiInputDevices()\n"));
816        LSCPResultSet result;
817        try {
818            String s;
819            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
820            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
821            for (; iter != devices.end(); iter++) {
822                if (s != "") s += ",";
823                s += ToString(iter->first);
824            }
825            result.Add(s);
826        }
827        catch (LinuxSamplerException e) {
828            result.Error(e);
829        }
830        return result.Produce();
831    }
832    
833    String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
834        dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
835        LSCPResultSet result;
836        try {
837            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
838            if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
839            AudioOutputDevice* pDevice = devices[DeviceIndex];
840            result.Add("driver", pDevice->Driver());
841            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
842            std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
843            for (; iter != parameters.end(); iter++) {
844                result.Add(iter->first, iter->second->Value());
845            }
846        }
847        catch (LinuxSamplerException e) {
848            result.Error(e);
849        }
850        return result.Produce();
851    }
852    
853    String LSCPServer::GetMidiInputDeviceInfo(uint DeviceIndex) {
854        dmsg(2,("LSCPServer: GetMidiInputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
855        LSCPResultSet result;
856        try {
857            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
858            MidiInputDevice* pDevice = devices[DeviceIndex];
859            if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
860            result.Add("driver", pDevice->Driver());
861            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
862            std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
863            for (; iter != parameters.end(); iter++) {
864                result.Add(iter->first, iter->second->Value());
865            }
866        }
867        catch (LinuxSamplerException e) {
868            result.Error(e);
869        }
870        return result.Produce();
871    }
872    String LSCPServer::GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex) {
873        dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
874        LSCPResultSet result;
875        try {
876            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
877            MidiInputDevice* pDevice = devices[DeviceIndex];
878            if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
879            MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
880            if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
881            std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
882            std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
883            for (; iter != parameters.end(); iter++) {
884                result.Add(iter->first, iter->second->Value());
885            }
886        }
887        catch (LinuxSamplerException e) {
888            result.Error(e);
889        }
890        return result.Produce();
891    }
892    
893    String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
894        dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
895        LSCPResultSet result;
896        try {
897            // get audio output device
898            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
899            if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
900            AudioOutputDevice* pDevice = devices[DeviceId];
901    
902            // get audio channel
903            AudioChannel* pChannel = pDevice->Channel(ChannelId);
904            if (!pChannel) throw LinuxSamplerException("Audio ouotput device does not have channel " + ToString(ChannelId) + ".");
905    
906            // return the values of all audio channel parameters
907            std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
908            std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
909            for (; iter != parameters.end(); iter++) {
910                result.Add(iter->first, iter->second->Value());
911            }
912        }
913        catch (LinuxSamplerException e) {
914            result.Error(e);
915        }
916        return result.Produce();
917    }
918    
919    String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
920        dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
921        LSCPResultSet result;
922        try {
923            // get audio output device
924            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
925            if (!devices[DeviceId]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");
926            MidiInputDevice* pDevice = devices[DeviceId];
927    
928            // get midi port
929            MidiInputDevice::MidiInputPort* pPort = pDevice->GetPort(PortId);
930            if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");
931    
932            // get desired port parameter
933            std::map<String,DeviceCreationParameter*> parameters = pPort->DeviceParameters();
934            if (!parameters[ParameterName]) throw LinuxSamplerException("Midi port does not provice a parameters '" + ParameterName + "'.");
935            DeviceCreationParameter* pParameter = parameters[ParameterName];
936    
937            // return all fields of this audio channel parameter
938            result.Add("TYPE",         pParameter->Type());
939            result.Add("DESCRIPTION",  pParameter->Description());
940            result.Add("FIX",          pParameter->Fix());
941            result.Add("MULTIPLICITY", pParameter->Multiplicity());
942            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());
943            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());
944            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
945        }
946        catch (LinuxSamplerException e) {
947            result.Error(e);
948        }
949        return result.Produce();
950    }
951    
952    String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
953        dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
954        LSCPResultSet result;
955        try {
956            // get audio output device
957            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
958            if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
959            AudioOutputDevice* pDevice = devices[DeviceId];
960    
961            // get audio channel
962            AudioChannel* pChannel = pDevice->Channel(ChannelId);
963            if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");
964    
965            // get desired audio channel parameter
966            std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
967            if (!parameters[ParameterName]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");
968            DeviceRuntimeParameter* pParameter = parameters[ParameterName];
969    
970            // return all fields of this audio channel parameter
971            result.Add("TYPE",         pParameter->Type());
972            result.Add("DESCRIPTION",  pParameter->Description());
973            result.Add("FIX",          pParameter->Fix());
974            result.Add("MULTIPLICITY", pParameter->Multiplicity());
975            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());
976            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());
977            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
978        }
979        catch (LinuxSamplerException e) {
980            result.Error(e);
981        }
982        return result.Produce();
983    }
984    
985    String LSCPServer::SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal) {
986        dmsg(2,("LSCPServer: SetAudioOutputChannelParameter(DeviceId=%d,ChannelId=%d,ParamKey=%s,ParamVal=%s)\n",DeviceId,ChannelId,ParamKey.c_str(),ParamVal.c_str()));
987        LSCPResultSet result;
988        try {
989            // get audio output device
990            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
991            if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
992            AudioOutputDevice* pDevice = devices[DeviceId];
993    
994            // get audio channel
995            AudioChannel* pChannel = pDevice->Channel(ChannelId);
996            if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");
997    
998            // get desired audio channel parameter
999            std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1000            if (!parameters[ParamKey]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");
1001            DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1002    
1003            // set new channel parameter value
1004            pParameter->SetValue(ParamVal);
1005        }
1006        catch (LinuxSamplerException e) {
1007            result.Error(e);
1008        }
1009        return result.Produce();
1010    }
1011    
1012    String LSCPServer::SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1013        dmsg(2,("LSCPServer: SetAudioOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1014        LSCPResultSet result;
1015        try {
1016            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1017            if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1018            AudioOutputDevice* pDevice = devices[DeviceIndex];
1019            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1020            if (!parameters[ParamKey]) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1021            parameters[ParamKey]->SetValue(ParamVal);
1022        }
1023        catch (LinuxSamplerException e) {
1024            result.Error(e);
1025        }
1026        return result.Produce();
1027    }
1028    
1029    String LSCPServer::SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1030        dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1031        LSCPResultSet result;
1032        try {
1033            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1034            if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1035            MidiInputDevice* pDevice = devices[DeviceIndex];
1036            std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1037            if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1038            parameters[ParamKey]->SetValue(ParamVal);
1039        }
1040        catch (LinuxSamplerException e) {
1041            result.Error(e);
1042        }
1043        return result.Produce();
1044    }
1045    
1046    String LSCPServer::SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal) {
1047        dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1048        LSCPResultSet result;
1049        try {
1050            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1051            MidiInputDevice* pDevice = devices[DeviceIndex];
1052            if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1053            MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1054            if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1055            std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
1056            if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1057            parameters[ParamKey]->SetValue(ParamVal);
1058        }
1059        catch (LinuxSamplerException e) {
1060            result.Error(e);
1061        }
1062        return result.Produce();
1063  }  }
1064    
1065  /**  /**
1066   * 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
1067   * playback on a particular sampler channel.   * playback on a particular sampler channel.
1068   */   */
1069  String LSCPServer::SetAudioOutputChannel(uint AudioOutputChannel, uint SamplerChannel) {  String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1070      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));
1071      return "ERR:0:Not implemented yet.\r\n";      return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?
1072  }  }
1073    
1074  /**  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1075   * 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));
1076   * engine of a particular sampler channel should listen to.      LSCPResultSet result;
1077   */      try {
1078  String LSCPServer::SetMIDIInputPort(String MIDIInputPort, uint Samplerchannel) {          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1079      dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIInputPort=%s, Samplerchannel=%d)\n", MIDIInputPort.c_str(), Samplerchannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1080      return "ERR:0:Not implemented yet.\r\n";          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1081            AudioOutputDevice* pDevice = devices[AudioDeviceId];
1082            if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1083            pSamplerChannel->SetAudioOutputDevice(pDevice);
1084        }
1085        catch (LinuxSamplerException e) {
1086             result.Error(e);
1087        }
1088        return result.Produce();
1089    }
1090    
1091    String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1092        dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1093        LSCPResultSet result;
1094        try {
1095            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1096            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1097            // Driver type name aliasing...
1098            if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";
1099            if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";
1100            // Check if there's one audio output device already created
1101            // for the intended audio driver type (AudioOutputDriver)...
1102            AudioOutputDevice *pDevice = NULL;
1103            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1104            std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1105            for (; iter != devices.end(); iter++) {
1106                if ((iter->second)->Driver() == AudioOutputDriver) {
1107                    pDevice = iter->second;
1108                    break;
1109                }
1110            }
1111            // If it doesn't exist, create a new one with default parameters...
1112            if (pDevice == NULL) {
1113                std::map<String,String> params;
1114                pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1115            }
1116            // Must have a device...
1117            if (pDevice == NULL)
1118                throw LinuxSamplerException("Internal error: could not create audio output device.");
1119            // Set it as the current channel device...
1120            pSamplerChannel->SetAudioOutputDevice(pDevice);
1121        }
1122        catch (LinuxSamplerException e) {
1123             result.Error(e);
1124        }
1125        return result.Produce();
1126    }
1127    
1128    String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1129        dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel));
1130        LSCPResultSet result;
1131        try {
1132            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1133            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1134            pSamplerChannel->SetMidiInputPort(MIDIPort);
1135        }
1136        catch (LinuxSamplerException e) {
1137             result.Error(e);
1138        }
1139        return result.Produce();
1140    }
1141    
1142    String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1143        dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
1144        LSCPResultSet result;
1145        try {
1146            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1147            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1148            pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);
1149        }
1150        catch (LinuxSamplerException e) {
1151             result.Error(e);
1152        }
1153        return result.Produce();
1154    }
1155    
1156    String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1157        dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1158        LSCPResultSet result;
1159        try {
1160            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1161            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1162            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1163            MidiInputDevice* pDevice = devices[MIDIDeviceId];
1164            if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1165            pSamplerChannel->SetMidiInputDevice(pDevice);
1166        }
1167        catch (LinuxSamplerException e) {
1168             result.Error(e);
1169        }
1170        return result.Produce();
1171    }
1172    
1173    String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1174        dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1175        LSCPResultSet result;
1176        try {
1177            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1178            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1179            // Driver type name aliasing...
1180            if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";
1181            // Check if there's one MIDI input device already created
1182            // for the intended MIDI driver type (MidiInputDriver)...
1183            MidiInputDevice *pDevice = NULL;
1184            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1185            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1186            for (; iter != devices.end(); iter++) {
1187                if ((iter->second)->Driver() == MidiInputDriver) {
1188                    pDevice = iter->second;
1189                    break;
1190                }
1191            }
1192            // If it doesn't exist, create a new one with default parameters...
1193            if (pDevice == NULL) {
1194                std::map<String,String> params;
1195                pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1196                // Make it with at least one initial port.
1197                std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1198                parameters["ports"]->SetValue("1");
1199            }
1200            // Must have a device...
1201            if (pDevice == NULL)
1202                throw LinuxSamplerException("Internal error: could not create MIDI input device.");
1203            // Set it as the current channel device...
1204            pSamplerChannel->SetMidiInputDevice(pDevice);
1205        }
1206        catch (LinuxSamplerException e) {
1207             result.Error(e);
1208        }
1209        return result.Produce();
1210  }  }
1211    
1212  /**  /**
1213   * Will be called by the parser to change the MIDI input channel on which the   * Will be called by the parser to change the MIDI input device, port and channel on which
1214   * engine of a particular sampler channel should listen to.   * engine of a particular sampler channel should listen to.
1215   */   */
1216  String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint SamplerChannel) {  String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1217      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));
1218      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1219        try {
1220            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1221            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1222            std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1223            MidiInputDevice* pDevice = devices[MIDIDeviceId];
1224            if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1225            pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);
1226        }
1227        catch (LinuxSamplerException e) {
1228             result.Error(e);
1229        }
1230        return result.Produce();
1231  }  }
1232    
1233  /**  /**
1234   * 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
1235   * particular sampler channel.   * particular sampler channel.
1236   */   */
1237  String LSCPServer::SetVolume(double Volume, uint SamplerChannel) {  String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) {
1238      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, SamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel));
1239      pEngine->Volume = Volume;      LSCPResultSet result;
1240      return "OK\r\n";      try {
1241            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1242            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
1243            Engine* pEngine = pSamplerChannel->GetEngine();
1244            if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
1245            pEngine->Volume(Volume);
1246        }
1247        catch (LinuxSamplerException e) {
1248             result.Error(e);
1249        }
1250        return result.Produce();
1251  }  }
1252    
1253  /**  /**
1254   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
1255   */   */
1256  String LSCPServer::ResetChannel(uint SamplerChannel) {  String LSCPServer::ResetChannel(uint uiSamplerChannel) {
1257      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", SamplerChannel));      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
1258      pEngine->Reset();      LSCPResultSet result;
1259      return "OK\r\n";      try {
1260            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1261            if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
1262            Engine* pEngine = pSamplerChannel->GetEngine();
1263            if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
1264            pEngine->Reset();
1265        }
1266        catch (LinuxSamplerException e) {
1267             result.Error(e);
1268        }
1269        return result.Produce();
1270  }  }
1271    
1272  /**  /**
1273   * 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
1274   * server for receiving event messages.   * server for receiving event messages.
1275   */   */
1276  String LSCPServer::SubscribeNotification(uint UDPPort) {  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
1277      dmsg(2,("LSCPServer: SubscribeNotification(UDPPort=%d)\n", UDPPort));      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1278      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1279        SubscriptionMutex.Lock();
1280        eventSubscriptions[type].push_back(currentSocket);
1281        SubscriptionMutex.Unlock();
1282        return result.Produce();
1283  }  }
1284    
1285  /**  /**
1286   * 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
1287   * for not receiving further event messages.   * for not receiving further event messages.
1288   */   */
1289  String LSCPServer::UnsubscribeNotification(String SessionID) {  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
1290      dmsg(2,("LSCPServer: UnsubscribeNotification(SessionID=%s)\n", SessionID.c_str()));      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1291      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1292        SubscriptionMutex.Lock();
1293        eventSubscriptions[type].remove(currentSocket);
1294        SubscriptionMutex.Unlock();
1295        return result.Produce();
1296    }
1297    
1298    /**
1299     * Will be called by the parser to enable or disable echo mode; if echo
1300     * mode is enabled, all commands from the client will (immediately) be
1301     * echoed back to the client.
1302     */
1303    String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
1304        dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
1305        LSCPResultSet result;
1306        try {
1307            if      (boolean_value == 0) pSession->bVerbose = false;
1308            else if (boolean_value == 1) pSession->bVerbose = true;
1309            else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1");
1310        }
1311        catch (LinuxSamplerException e) {
1312             result.Error(e);
1313        }
1314        return result.Produce();
1315    }
1316    
1317    // Instrument loader constructor.
1318    LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)
1319        : Thread(false, 0, -4)
1320    {
1321        this->pEngine = pEngine;
1322        this->Filename = Filename;
1323        this->uiInstrument = uiInstrument;
1324    }
1325    
1326    // Instrument loader process.
1327    int LSCPLoadInstrument::Main()
1328    {
1329        try {
1330            pEngine->LoadInstrument(Filename.c_str(), uiInstrument);
1331        }
1332    
1333        catch (LinuxSamplerException e) {
1334            e.PrintMessage();
1335        }
1336    
1337        // Always re-enable the engine.
1338        pEngine->Enable();
1339    
1340        // FIXME: Shoot ourselves on the foot?
1341        delete this;
1342  }  }

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

  ViewVC Help
Powered by ViewVC