/[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 133 by capela, Fri Jun 18 14:29:02 2004 UTC revision 210 by schoenebeck, Sat Jul 24 12:33:49 2004 UTC
# Line 22  Line 22 
22    
23  #include "lscpserver.h"  #include "lscpserver.h"
24  #include "lscpresultset.h"  #include "lscpresultset.h"
25    #include "lscpevent.h"
26    
27  #include "../engines/gig/Engine.h"  #include "../engines/gig/Engine.h"
28  #include "../audiodriver/AudioOutputDeviceFactory.h"  #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) {  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) {
55      this->pSampler = pSampler;      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;
# Line 55  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        FD_ZERO(&fdSet);
90        FD_SET(hSocket, &fdSet);
91        int maxSessions = hSocket;
92    
93      while (true) {      while (true) {
94          hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);          fd_set selectSet = fdSet;
95          if (hSession < 0) {          int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);
96              std::cerr << "LSCPServer: Client connection failed." << std::endl;          if (retval == 0)
97              close(hSocket);                  continue; //Nothing try again
98              //return -1;          if (retval == -1) {
99              exit(EXIT_FAILURE);                  std::cerr << "LSCPServer: Socket select error." << std::endl;
100          }                  close(hSocket);
101                    exit(EXIT_FAILURE);
102          dmsg(1,("LSCPServer: Client connection established.\n"));          }
103          //send(hSession, "Welcome!\r\n", 10, 0);  
104            //Accept new connections now (if any)
105          // Parser invocation          if (FD_ISSET(hSocket, &selectSet)) {
106          yyparse_param_t yyparse_param;                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
107          yyparse_param.pServer = this;                  if (socket < 0) {
108          yylex_init(&yyparse_param.pScanner);                          std::cerr << "LSCPServer: Client connection failed." << std::endl;
109          while (yyparse(&yyparse_param) == LSCP_SYNTAX_ERROR); // recall parser in case of syntax error                          exit(EXIT_FAILURE);
110          yylex_destroy(yyparse_param.pScanner);                  }
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          close(hSession);          //Now let's deliver late notifies (if any)
155          dmsg(1,("LSCPServer: Client connection terminated.\n"));          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 87  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) {  String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
# Line 95  String LSCPServer::CreateAudioOutputDevi Line 346  String LSCPServer::CreateAudioOutputDevi
346      LSCPResultSet result;      LSCPResultSet result;
347      try {      try {
348          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
         std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();  
349          // search for the created device to get its index          // search for the created device to get its index
350          int index = -1;          int index = GetAudioOutputDeviceIndex(pDevice);
         std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();  
         for (; iter != devices.end(); iter++) {  
             if (iter->second == pDevice) {  
                 index = iter->first;  
                 break;  
             }  
         }  
351          if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");          if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");
352          result = index; // success          result = index; // success
353      }      }
# Line 114  String LSCPServer::CreateAudioOutputDevi Line 357  String LSCPServer::CreateAudioOutputDevi
357      return result.Produce();      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) {  String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
377      dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));      dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
378      LSCPResultSet result;      LSCPResultSet result;
# Line 129  String LSCPServer::DestroyAudioOutputDev Line 388  String LSCPServer::DestroyAudioOutputDev
388      return result.Produce();      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 uiInstrument, uint uiSamplerChannel) {  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(), uiInstrument, uiSamplerChannel));      dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));
411      LSCPResultSet result;      LSCPResultSet result;
412      try {      try {
# Line 140  String LSCPServer::LoadInstrument(String Line 414  String LSCPServer::LoadInstrument(String
414          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
415          Engine* pEngine = pSamplerChannel->GetEngine();          Engine* pEngine = pSamplerChannel->GetEngine();
416          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");          if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
417          LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument);          if (pSamplerChannel->GetAudioOutputDevice() == NULL)
418          pLoadInstrument->StartThread();              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) {      catch (LinuxSamplerException e) {
426           result.Error(e);           result.Error(e);
# Line 180  String LSCPServer::GetChannels() { Line 459  String LSCPServer::GetChannels() {
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    /**
479   * Will be called by the parser to add a sampler channel.   * Will be called by the parser to add a sampler channel.
480   */   */
481  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
# Line 260  String LSCPServer::GetChannelInfo(uint u Line 556  String LSCPServer::GetChannelInfo(uint u
556          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
557          result.Add("VOLUME", Volume);          result.Add("VOLUME", Volume);
558    
559          //Some hardcoded stuff for now to make GUI look good          //Some not-so-hardcoded stuff to make GUI look good
560          result.Add("AUDIO_OUTPUT_DEVICE", "0");          result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
561          result.Add("AUDIO_OUTPUT_CHANNELS", "2");          result.Add("AUDIO_OUTPUT_CHANNELS", "2");
562          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");          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);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
569          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
570          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
   
         //Some more hardcoded stuff for now to make GUI look good  
         result.Add("MIDI_INPUT_DEVICE", "0");  
         result.Add("MIDI_INPUT_PORT", "0");  
         result.Add("MIDI_INPUT_CHANNEL", "1");  
571      }      }
572      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
573           result.Error(e);           result.Error(e);
# Line 366  String LSCPServer::GetAvailableAudioOutp Line 661  String LSCPServer::GetAvailableAudioOutp
661      return result.Produce();      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) {  String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
702      dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));      dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
703      LSCPResultSet result;      LSCPResultSet result;
# Line 390  String LSCPServer::GetAudioOutputDriverI Line 722  String LSCPServer::GetAudioOutputDriverI
722      return result.Produce();      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) {  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()));      dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str()));
749      LSCPResultSet result;      LSCPResultSet result;
# Line 417  String LSCPServer::GetAudioOutputDeviceC Line 771  String LSCPServer::GetAudioOutputDeviceC
771      LSCPResultSet result;      LSCPResultSet result;
772      try {      try {
773          uint count = pSampler->AudioOutputDevices();          uint count = pSampler->AudioOutputDevices();
774          result = count; // success          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) {      catch (LinuxSamplerException e) {
790          result.Error(e);          result.Error(e);
# Line 444  String LSCPServer::GetAudioOutputDevices Line 811  String LSCPServer::GetAudioOutputDevices
811      return result.Produce();      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) {  String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
834      dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));      dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
835      LSCPResultSet result;      LSCPResultSet result;
# Line 451  String LSCPServer::GetAudioOutputDeviceI Line 837  String LSCPServer::GetAudioOutputDeviceI
837          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
838          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
839          AudioOutputDevice* pDevice = devices[DeviceIndex];          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();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
862          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
863          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 462  String LSCPServer::GetAudioOutputDeviceI Line 869  String LSCPServer::GetAudioOutputDeviceI
869      }      }
870      return result.Produce();      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) {  String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
894      dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));      dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
# Line 489  String LSCPServer::GetAudioOutputChannel Line 916  String LSCPServer::GetAudioOutputChannel
916      return result.Produce();      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) {  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()));      dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
954      LSCPResultSet result;      LSCPResultSet result;
# Line 566  String LSCPServer::SetAudioOutputDeviceP Line 1026  String LSCPServer::SetAudioOutputDeviceP
1026      return result.Produce();      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.
# Line 575  String LSCPServer::SetAudioOutputChannel Line 1071  String LSCPServer::SetAudioOutputChannel
1071      return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?      return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?
1072  }  }
1073    
1074  String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1075      dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1076      LSCPResultSet result;      LSCPResultSet result;
1077      try {      try {
1078          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1079          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1080          // FIXME: workaround until MIDI driver configuration is implemented (using a Factory class for the MIDI input drivers then, like its already done for audio output drivers)          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1081          if (MidiInputDriver != "ALSA") throw LinuxSamplerException("Unknown MIDI input driver '" + MidiInputDriver + "'.");          AudioOutputDevice* pDevice = devices[AudioDeviceId];
1082          MidiInputDevice::type_t MidiInputType = MidiInputDevice::type_alsa;          if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1083          pSamplerChannel->SetMidiInputDevice(MidiInputType);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1084      }      }
1085      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1086           result.Error(e);           result.Error(e);
# Line 592  String LSCPServer::SetMIDIInputType(Stri Line 1088  String LSCPServer::SetMIDIInputType(Stri
1088      return result.Produce();      return result.Produce();
1089  }  }
1090    
1091  /**  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1092   * Will be called by the parser to change the MIDI input port on which the      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
  * engine of a particular sampler channel should listen to.  
  */  
 String LSCPServer::SetMIDIInputPort(String MIDIInputPort, uint uiSamplerChannel) {  
     dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIInputPort=%s, Samplerchannel=%d)\n", MIDIInputPort.c_str(), uiSamplerChannel));  
1093      LSCPResultSet result;      LSCPResultSet result;
1094      try {      try {
1095          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1096          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1097          if (!pSamplerChannel->GetMidiInputDevice()) throw LinuxSamplerException("No MIDI input device connected yet");          // Driver type name aliasing...
1098          pSamplerChannel->GetMidiInputDevice()->SetInputPort(MIDIInputPort.c_str());          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) {      catch (LinuxSamplerException e) {
1137           result.Error(e);           result.Error(e);
# Line 611  String LSCPServer::SetMIDIInputPort(Stri Line 1139  String LSCPServer::SetMIDIInputPort(Stri
1139      return result.Produce();      return result.Produce();
1140  }  }
1141    
 /**  
  * Will be called by the parser to change the MIDI input channel on which the  
  * engine of a particular sampler channel should listen to.  
  */  
1142  String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {  String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1143      dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n", MIDIChannel, uiSamplerChannel));      dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
1144      LSCPResultSet result;      LSCPResultSet result;
1145      try {      try {
1146          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1147          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1148          if (!pSamplerChannel->GetMidiInputDevice()) throw LinuxSamplerException("No MIDI input device connected yet");          pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);
         MidiInputDevice::type_t oldtype = pSamplerChannel->GetMidiInputDevice()->Type();  
         pSamplerChannel->SetMidiInputDevice(oldtype, (MidiInputDevice::midi_chan_t) MIDIChannel);  
1149      }      }
1150      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1151           result.Error(e);           result.Error(e);
# Line 631  String LSCPServer::SetMIDIInputChannel(u Line 1153  String LSCPServer::SetMIDIInputChannel(u
1153      return result.Produce();      return result.Produce();
1154  }  }
1155    
1156  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint SamplerChannel) {  String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1157        dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1158      LSCPResultSet result;      LSCPResultSet result;
1159      try {      try {
1160          throw LinuxSamplerException("Command not yet implemented");          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 device, port and channel on which
1214     * engine of a particular sampler channel should listen to.
1215     */
1216    String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1217        dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1218        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) {      catch (LinuxSamplerException e) {
1228           result.Error(e);           result.Error(e);
# Line 685  String LSCPServer::ResetChannel(uint uiS Line 1273  String LSCPServer::ResetChannel(uint uiS
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.  // Instrument loader constructor.
1318  LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)  LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)

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

  ViewVC Help
Powered by ViewVC