/[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 155 by senkov, Mon Jun 28 04:30:11 2004 UTC revision 184 by senkov, Wed Jul 7 02:00:37 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 "../audiodriver/AudioOutputDeviceFactory.h"
29  #include "../mididriver/MidiInputDeviceFactory.h"  #include "../mididriver/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<int> LSCPServer::hSessions = std::vector<int>();
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 56  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        struct timeval tv;
90        tv.tv_sec = 30;
91        tv.tv_usec = 0;
92        FD_ZERO(&fdSet);
93        FD_SET(hSocket, &fdSet);
94        int maxSessions = hSocket;
95    
96        // Parser initialization
97        yyparse_param_t yyparse_param;
98        yyparse_param.pServer = this;
99    
100      while (true) {      while (true) {
101          hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);          fd_set selectSet = fdSet;
102          if (hSession < 0) {          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &tv);
103              std::cerr << "LSCPServer: Client connection failed." << std::endl;          if (retval == 0)
104              close(hSocket);                  continue; //Nothing in 30 seconds, try again
105              //return -1;          if (retval == -1) {
106              exit(EXIT_FAILURE);                  std::cerr << "LSCPServer: Socket select error." << std::endl;
107          }                  close(hSocket);
108                    exit(EXIT_FAILURE);
109          dmsg(1,("LSCPServer: Client connection established.\n"));          }
110          //send(hSession, "Welcome!\r\n", 10, 0);          
111            //Accept new connections now (if any)
112          // Parser invocation          if (FD_ISSET(hSocket, &selectSet)) {
113          yyparse_param_t yyparse_param;                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
114          yyparse_param.pServer = this;                  if (socket < 0) {
115          yylex_init(&yyparse_param.pScanner);                          std::cerr << "LSCPServer: Client connection failed." << std::endl;
116          while (yyparse(&yyparse_param) == LSCP_SYNTAX_ERROR); // recall parser in case of syntax error                          exit(EXIT_FAILURE);
117          yylex_destroy(yyparse_param.pScanner);                  }
118    
119                    if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
120                            std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
121                            exit(EXIT_FAILURE);
122                    }
123    
124          close(hSession);                  hSessions.push_back(socket);
125          dmsg(1,("LSCPServer: Client connection terminated.\n"));                  FD_SET(socket, &fdSet);
126                    if (socket > maxSessions)
127                            maxSessions = socket;
128                    dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
129                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
130                    continue; //Maybe this was the only selected socket, better select again
131            }
132    
133            //Something was selected and it was not the hSocket, so it must be some command(s) coming.
134            for (std::vector<int>::iterator iter = hSessions.begin(); iter !=  hSessions.end(); iter++) {
135                    if (FD_ISSET(*iter, &selectSet)) {      //Was it this socket?
136                            if (GetLSCPCommand(iter)) {     //Have we read the entire command?
137                                    dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
138                                    yylex_init(&yyparse_param.pScanner);
139                                    currentSocket = *iter;  //a hack
140                                    int result = yyparse(&yyparse_param);
141                                    currentSocket = -1;     //continuation of a hack
142                                    dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
143                                    if (result == LSCP_QUIT) { //Was it a quit command by any chance?
144                                            CloseConnection(iter);
145                                    }
146                            }
147                            //socket may have been closed, iter may be invalid, get out of the loop for now.
148                            //we'll be back if there is data.
149                            break;
150                    }
151            }
152    
153            //Now let's deliver late notifies (if any)
154            NotifyBufferMutex.Lock();
155            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
156                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
157                    bufferedNotifies.erase(iterNotify);
158            }
159            NotifyBufferMutex.Unlock();
160      }      }
161        //It will never get here anyway
162        //yylex_destroy(yyparse_param.pScanner);
163    }
164    
165    void LSCPServer::CloseConnection( std::vector<int>::iterator iter ) {
166            int socket = *iter;
167            dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
168            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
169            hSessions.erase(iter);
170            FD_CLR(socket,  &fdSet);
171            SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
172            for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
173                    iter->second.remove(socket);
174            }
175            SubscriptionMutex.Unlock();
176            NotifyMutex.Lock();
177            bufferedCommands.erase(socket);
178            bufferedNotifies.erase(socket);
179            close(socket);
180            NotifyMutex.Unlock();
181    }
182    
183    void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
184            SubscriptionMutex.Lock();
185            if (eventSubscriptions.count(event.GetType()) == 0) {
186                    SubscriptionMutex.Unlock();     //Nobody is subscribed to this event
187                    return;
188            }
189            std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
190            std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
191            String notify = event.Produce();
192    
193            while (true) {
194                    if (NotifyMutex.Trylock()) {
195                            for(;iter != end; iter++)
196                                    send(*iter, notify.c_str(), notify.size(), 0);
197                            NotifyMutex.Unlock();
198                            break;
199                    } else {
200                            if (NotifyBufferMutex.Trylock()) {
201                                    for(;iter != end; iter++)
202                                            bufferedNotifies[*iter] += notify;
203                                    NotifyBufferMutex.Unlock();
204                                    break;
205                            }
206                    }
207            }
208            SubscriptionMutex.Unlock();
209    }
210    
211    extern int GetLSCPCommand( void *buf, int max_size ) {
212            String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
213            if (command.size() == 0) {              //Parser wants input but we have nothing.
214                    strcpy((char*) buf, "\n");      //So give it an empty command
215                    return 1;                       //to keep it happy.
216            }
217    
218            if (max_size < command.size()) {
219                    std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
220                    return 0;       //This will never happen
221            }
222    
223            strcpy((char*) buf, command.c_str());
224            LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
225            return command.size();
226    }
227    
228    /**
229     * Will be called to try to read the command from the socket
230     * If command is read, it will return true. Otherwise false is returned.
231     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
232     */
233    bool LSCPServer::GetLSCPCommand( std::vector<int>::iterator iter ) {
234            int socket = *iter;
235            char c;
236            int i = 0;
237            while (true) {
238                    int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
239                    if (result == 0) { //socket was selected, so 0 here means client has closed the connection
240                            CloseConnection(iter);
241                            break;
242                    }
243                    if (result == 1) {
244                            if (c == '\r')
245                                    continue; //Ignore CR
246                            if (c == '\n') {
247                                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
248                                    bufferedCommands[socket] += "\n";
249                                    return true; //Complete command was read
250                            }
251                            bufferedCommands[socket] += c;
252                    }
253                    if (result == -1) {
254                            if (errno == EAGAIN) //Would block, try again later.
255                                    return false;
256                            switch(errno) {
257                                    case EBADF:
258                                            dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
259                                            break;
260                                    case ECONNREFUSED:
261                                            dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
262                                            break;
263                                    case ENOTCONN:
264                                            dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
265                                            break;
266                                    case ENOTSOCK:
267                                            dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
268                                            break;
269                                    case EAGAIN:
270                                            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"));
271                                            break;
272                                    case EINTR:
273                                            dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
274                                            break;
275                                    case EFAULT:
276                                            dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
277                                            break;
278                                    case EINVAL:
279                                            dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
280                                            break;
281                                    case ENOMEM:
282                                            dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
283                                            break;
284                                    default:
285                                            dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
286                                            break;
287                            }
288                            CloseConnection(iter);
289                            break;
290                    }
291            }
292            return false;
293  }  }
294    
295  /**  /**
# Line 88  int LSCPServer::Main() { Line 300  int LSCPServer::Main() {
300   */   */
301  void LSCPServer::AnswerClient(String ReturnMessage) {  void LSCPServer::AnswerClient(String ReturnMessage) {
302      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
303      send(hSession, ReturnMessage.c_str(), ReturnMessage.size(), 0);      if (currentSocket != -1) {
304                NotifyMutex.Lock();
305                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
306                NotifyMutex.Unlock();
307        }
308  }  }
309    
310  /**  /**
# Line 326  String LSCPServer::GetChannelInfo(uint u Line 542  String LSCPServer::GetChannelInfo(uint u
542          result.Add("AUDIO_OUTPUT_CHANNELS", "2");          result.Add("AUDIO_OUTPUT_CHANNELS", "2");
543          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");
544    
545            result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
546            result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
547            result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
548    
549          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
550          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
551          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
   
         MidiInputDevice *pDevice = pSamplerChannel->GetMidiInputDevice();  
         if (pDevice) {  
                 result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pDevice));  
                 MidiInputDevice::MidiInputPort *pPort = pSamplerChannel->GetMidiInputPort();  
                 if (pPort) {  
                         result.Add("MIDI_INPUT_PORT", (int)pPort->GetPortNumber());  
                         result.Add("MIDI_INPUT_CHANNEL", (int)pSamplerChannel->GetMidiInputChannel());  
                 }  
   
         }  
552      }      }
553      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
554           result.Error(e);           result.Error(e);
# Line 628  String LSCPServer::GetMidiInputDeviceInf Line 837  String LSCPServer::GetMidiInputDeviceInf
837      try {      try {
838          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
839          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
840          if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
841          result.Add("driver", pDevice->Driver());          result.Add("driver", pDevice->Driver());
842          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
843          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
# Line 647  String LSCPServer::GetMidiInputPortInfo( Line 856  String LSCPServer::GetMidiInputPortInfo(
856      try {      try {
857          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
858          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
859          if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
860          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
861          if (!pMidiInputPort) throw LinuxSamplerException("There is no midi input port with index " + ToString(PortIndex) + ".");          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
862          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
863          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
864          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 770  String LSCPServer::SetMidiInputDevicePar Line 979  String LSCPServer::SetMidiInputDevicePar
979      LSCPResultSet result;      LSCPResultSet result;
980      try {      try {
981          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
982          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");          if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
983          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
984          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
985          if (!parameters[ParamKey]) throw LinuxSamplerException("Midi input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
986          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
987      }      }
988      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 788  String LSCPServer::SetMidiInputPortParam Line 997  String LSCPServer::SetMidiInputPortParam
997      try {      try {
998          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
999          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1000          if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1001          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1002          if (!pMidiInputPort) throw LinuxSamplerException("There is no midi input port with index " + ToString(PortIndex) + ".");          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1003          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
1004          if (!parameters[ParamKey]) throw LinuxSamplerException("Midi input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1005          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1006      }      }
1007      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 810  String LSCPServer::SetAudioOutputChannel Line 1019  String LSCPServer::SetAudioOutputChannel
1019      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?
1020  }  }
1021    
1022    String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1023        dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1024        LSCPResultSet result;
1025        try {
1026            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1027            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1028            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1029            AudioOutputDevice* pDevice = devices[AudioDeviceId];
1030            if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1031            pSamplerChannel->SetAudioOutputDevice(pDevice);
1032        }
1033        catch (LinuxSamplerException e) {
1034             result.Error(e);
1035        }
1036        return result.Produce();
1037    }
1038    
1039  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1040      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1041      LSCPResultSet result;      LSCPResultSet result;
# Line 847  String LSCPServer::SetAudioOutputType(St Line 1073  String LSCPServer::SetAudioOutputType(St
1073      return result.Produce();      return result.Produce();
1074  }  }
1075    
1076  String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {  String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1077      dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));      dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel));
1078      LSCPResultSet result;      LSCPResultSet result;
1079      try {      try {
 #if 1  
         throw LinuxSamplerException("Command deprecated");  
 #else  
1080          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1081          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1082          // 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)          pSamplerChannel->SetMidiInputPort(MIDIPort);
         if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";  
         if (MidiInputDriver != "Alsa") throw LinuxSamplerException("Unknown MIDI input driver '" + MidiInputDriver + "'.");  
         MidiInputDevice::type_t MidiInputType = MidiInputDevice::type_alsa;  
         pSamplerChannel->SetMidiInputDevice(MidiInputType);  
 #endif  
1083      }      }
1084      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1085           result.Error(e);           result.Error(e);
# Line 869  String LSCPServer::SetMIDIInputType(Stri Line 1087  String LSCPServer::SetMIDIInputType(Stri
1087      return result.Produce();      return result.Produce();
1088  }  }
1089    
1090  /**  String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1091   * Will be called by the parser to change the MIDI input device, port and channel on which      dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
  * engine of a particular sampler channel should listen to.  
  */  
 String LSCPServer::SetMIDIInput(uint MIDIDevice, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {  
     dmsg(2,("LSCPServer: SetMIDIInput(MIDIDevice=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDevice, MIDIPort, MIDIChannel, uiSamplerChannel));  
1092      LSCPResultSet result;      LSCPResultSet result;
1093      try {      try {
1094          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1095          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1096          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();          pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);
         MidiInputDevice* pDevice = devices[MIDIDevice];  
         if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(MIDIDevice));  
         pSamplerChannel->SetMidiInputPort(pDevice, MIDIPort, (MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);  
1097      }      }
1098      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1099           result.Error(e);           result.Error(e);
# Line 890  String LSCPServer::SetMIDIInput(uint MID Line 1101  String LSCPServer::SetMIDIInput(uint MID
1101      return result.Produce();      return result.Produce();
1102  }  }
1103    
1104  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1105        dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1106      LSCPResultSet result;      LSCPResultSet result;
1107      try {      try {
1108          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1109          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1110          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1111          AudioOutputDevice* pDevice = devices[AudioDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1112          if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));          if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1113          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1114        }
1115        catch (LinuxSamplerException e) {
1116             result.Error(e);
1117        }
1118        return result.Produce();
1119    }
1120    
1121    String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1122        dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1123        LSCPResultSet result;
1124        try {
1125            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1126            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1127            // Driver type name aliasing...
1128            if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";
1129            // Check if there's one MIDI input device already created
1130            // for the intended MIDI driver type (MidiInputDriver)...
1131            MidiInputDevice *pDevice = NULL;
1132            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1133            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1134            for (; iter != devices.end(); iter++) {
1135                if ((iter->second)->Driver() == MidiInputDriver) {
1136                    pDevice = iter->second;
1137                    break;
1138                }
1139            }
1140            // If it doesn't exist, create a new one with default parameters...
1141            if (pDevice == NULL) {
1142                std::map<String,String> params;
1143                pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1144                // Make it with at least one initial port.
1145                std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1146                parameters["ports"]->SetValue("1");
1147            }
1148            // Must have a device...
1149            if (pDevice == NULL)
1150                throw LinuxSamplerException("Internal error: could not create MIDI input device.");
1151            // Set it as the current channel device...
1152            pSamplerChannel->SetMidiInputDevice(pDevice);
1153        }
1154        catch (LinuxSamplerException e) {
1155             result.Error(e);
1156        }
1157        return result.Produce();
1158    }
1159    
1160    /**
1161     * Will be called by the parser to change the MIDI input device, port and channel on which
1162     * engine of a particular sampler channel should listen to.
1163     */
1164    String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1165        dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1166        LSCPResultSet result;
1167        try {
1168            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1169            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1170            std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1171            MidiInputDevice* pDevice = devices[MIDIDeviceId];
1172            if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1173            pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);
1174      }      }
1175      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1176           result.Error(e);           result.Error(e);
# Line 949  String LSCPServer::ResetChannel(uint uiS Line 1221  String LSCPServer::ResetChannel(uint uiS
1221   * 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
1222   * server for receiving event messages.   * server for receiving event messages.
1223   */   */
1224  String LSCPServer::SubscribeNotification(event_t Event) {  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
1225      dmsg(2,("LSCPServer: SubscribeNotification(Event=%d)\n", Event));      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1226      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1227        SubscriptionMutex.Lock();
1228        eventSubscriptions[type].push_back(currentSocket);
1229        SubscriptionMutex.Unlock();
1230        return result.Produce();
1231  }  }
1232    
1233  /**  /**
1234   * 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
1235   * for not receiving further event messages.   * for not receiving further event messages.
1236   */   */
1237  String LSCPServer::UnsubscribeNotification(event_t Event) {  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
1238      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%d)\n", Event));      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1239      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1240        SubscriptionMutex.Lock();
1241        eventSubscriptions[type].remove(currentSocket);
1242        SubscriptionMutex.Unlock();
1243        return result.Produce();
1244  }  }
1245    
1246    

Legend:
Removed from v.155  
changed lines
  Added in v.184

  ViewVC Help
Powered by ViewVC