/[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 203 by schoenebeck, Tue Jul 13 22:44:13 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 "../mididriver/MidiInputDeviceFactory.h"  #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<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        FD_ZERO(&fdSet);
90        FD_SET(hSocket, &fdSet);
91        int maxSessions = hSocket;
92    
93        // Parser initialization
94        yyparse_param_t yyparse_param;
95        yyparse_param.pServer = this;
96    
97      while (true) {      while (true) {
98          hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);          fd_set selectSet = fdSet;
99          if (hSession < 0) {          int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);
100              std::cerr << "LSCPServer: Client connection failed." << std::endl;          if (retval == 0)
101              close(hSocket);                  continue; //Nothing try again
102              //return -1;          if (retval == -1) {
103              exit(EXIT_FAILURE);                  std::cerr << "LSCPServer: Socket select error." << std::endl;
104          }                  close(hSocket);
105                    exit(EXIT_FAILURE);
106          dmsg(1,("LSCPServer: Client connection established.\n"));          }
107          //send(hSession, "Welcome!\r\n", 10, 0);  
108            //Accept new connections now (if any)
109          // Parser invocation          if (FD_ISSET(hSocket, &selectSet)) {
110          yyparse_param_t yyparse_param;                  int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
111          yyparse_param.pServer = this;                  if (socket < 0) {
112          yylex_init(&yyparse_param.pScanner);                          std::cerr << "LSCPServer: Client connection failed." << std::endl;
113          while (yyparse(&yyparse_param) == LSCP_SYNTAX_ERROR); // recall parser in case of syntax error                          exit(EXIT_FAILURE);
114          yylex_destroy(yyparse_param.pScanner);                  }
115    
116                    if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
117                            std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
118                            exit(EXIT_FAILURE);
119                    }
120    
121          close(hSession);                  hSessions.push_back(socket);
122          dmsg(1,("LSCPServer: Client connection terminated.\n"));                  FD_SET(socket, &fdSet);
123                    if (socket > maxSessions)
124                            maxSessions = socket;
125                    dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
126                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
127                    continue; //Maybe this was the only selected socket, better select again
128            }
129    
130            //Something was selected and it was not the hSocket, so it must be some command(s) coming.
131            for (std::vector<int>::iterator iter = hSessions.begin(); iter !=  hSessions.end(); iter++) {
132                    if (FD_ISSET(*iter, &selectSet)) {      //Was it this socket?
133                            if (GetLSCPCommand(iter)) {     //Have we read the entire command?
134                                    dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
135                                    yylex_init(&yyparse_param.pScanner);
136                                    currentSocket = *iter;  //a hack
137                                    int result = yyparse(&yyparse_param);
138                                    currentSocket = -1;     //continuation of a hack
139                                    dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
140                                    if (result == LSCP_QUIT) { //Was it a quit command by any chance?
141                                            CloseConnection(iter);
142                                    }
143                            }
144                            //socket may have been closed, iter may be invalid, get out of the loop for now.
145                            //we'll be back if there is data.
146                            break;
147                    }
148            }
149    
150            //Now let's deliver late notifies (if any)
151            NotifyBufferMutex.Lock();
152            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
153                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
154                    bufferedNotifies.erase(iterNotify);
155            }
156            NotifyBufferMutex.Unlock();
157      }      }
158        //It will never get here anyway
159        //yylex_destroy(yyparse_param.pScanner);
160    }
161    
162    void LSCPServer::CloseConnection( std::vector<int>::iterator iter ) {
163            int socket = *iter;
164            dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
165            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
166            hSessions.erase(iter);
167            FD_CLR(socket,  &fdSet);
168            SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
169            for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
170                    iter->second.remove(socket);
171            }
172            SubscriptionMutex.Unlock();
173            NotifyMutex.Lock();
174            bufferedCommands.erase(socket);
175            bufferedNotifies.erase(socket);
176            close(socket);
177            NotifyMutex.Unlock();
178    }
179    
180    void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
181            SubscriptionMutex.Lock();
182            if (eventSubscriptions.count(event.GetType()) == 0) {
183                    SubscriptionMutex.Unlock();     //Nobody is subscribed to this event
184                    return;
185            }
186            std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
187            std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
188            String notify = event.Produce();
189    
190            while (true) {
191                    if (NotifyMutex.Trylock()) {
192                            for(;iter != end; iter++)
193                                    send(*iter, notify.c_str(), notify.size(), 0);
194                            NotifyMutex.Unlock();
195                            break;
196                    } else {
197                            if (NotifyBufferMutex.Trylock()) {
198                                    for(;iter != end; iter++)
199                                            bufferedNotifies[*iter] += notify;
200                                    NotifyBufferMutex.Unlock();
201                                    break;
202                            }
203                    }
204            }
205            SubscriptionMutex.Unlock();
206    }
207    
208    extern int GetLSCPCommand( void *buf, int max_size ) {
209            String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
210            if (command.size() == 0) {              //Parser wants input but we have nothing.
211                    strcpy((char*) buf, "\n");      //So give it an empty command
212                    return 1;                       //to keep it happy.
213            }
214    
215            if (max_size < command.size()) {
216                    std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
217                    return 0;       //This will never happen
218            }
219    
220            strcpy((char*) buf, command.c_str());
221            LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
222            return command.size();
223    }
224    
225    /**
226     * Will be called to try to read the command from the socket
227     * If command is read, it will return true. Otherwise false is returned.
228     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
229     */
230    bool LSCPServer::GetLSCPCommand( std::vector<int>::iterator iter ) {
231            int socket = *iter;
232            char c;
233            int i = 0;
234            while (true) {
235                    int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
236                    if (result == 0) { //socket was selected, so 0 here means client has closed the connection
237                            CloseConnection(iter);
238                            break;
239                    }
240                    if (result == 1) {
241                            if (c == '\r')
242                                    continue; //Ignore CR
243                            if (c == '\n') {
244                                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
245                                    bufferedCommands[socket] += "\n";
246                                    return true; //Complete command was read
247                            }
248                            bufferedCommands[socket] += c;
249                    }
250                    if (result == -1) {
251                            if (errno == EAGAIN) //Would block, try again later.
252                                    return false;
253                            switch(errno) {
254                                    case EBADF:
255                                            dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
256                                            break;
257                                    case ECONNREFUSED:
258                                            dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
259                                            break;
260                                    case ENOTCONN:
261                                            dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
262                                            break;
263                                    case ENOTSOCK:
264                                            dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
265                                            break;
266                                    case EAGAIN:
267                                            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"));
268                                            break;
269                                    case EINTR:
270                                            dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
271                                            break;
272                                    case EFAULT:
273                                            dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
274                                            break;
275                                    case EINVAL:
276                                            dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
277                                            break;
278                                    case ENOMEM:
279                                            dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
280                                            break;
281                                    default:
282                                            dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
283                                            break;
284                            }
285                            CloseConnection(iter);
286                            break;
287                    }
288            }
289            return false;
290  }  }
291    
292  /**  /**
# Line 88  int LSCPServer::Main() { Line 297  int LSCPServer::Main() {
297   */   */
298  void LSCPServer::AnswerClient(String ReturnMessage) {  void LSCPServer::AnswerClient(String ReturnMessage) {
299      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
300      send(hSession, ReturnMessage.c_str(), ReturnMessage.size(), 0);      if (currentSocket != -1) {
301                NotifyMutex.Lock();
302                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
303                NotifyMutex.Unlock();
304        }
305  }  }
306    
307  /**  /**
# Line 326  String LSCPServer::GetChannelInfo(uint u Line 539  String LSCPServer::GetChannelInfo(uint u
539          result.Add("AUDIO_OUTPUT_CHANNELS", "2");          result.Add("AUDIO_OUTPUT_CHANNELS", "2");
540          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");          result.Add("AUDIO_OUTPUT_ROUTING", "0,1");
541    
542            result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
543            result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
544            result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
545    
546          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
547          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
548          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());  
                 }  
   
         }  
549      }      }
550      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
551           result.Error(e);           result.Error(e);
# Line 628  String LSCPServer::GetMidiInputDeviceInf Line 834  String LSCPServer::GetMidiInputDeviceInf
834      try {      try {
835          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
836          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
837          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) + ".");
838          result.Add("driver", pDevice->Driver());          result.Add("driver", pDevice->Driver());
839          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
840          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
# Line 647  String LSCPServer::GetMidiInputPortInfo( Line 853  String LSCPServer::GetMidiInputPortInfo(
853      try {      try {
854          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
855          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
856          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) + ".");
857          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
858          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) + ".");
859          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
860          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
861          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 688  String LSCPServer::GetAudioOutputChannel Line 894  String LSCPServer::GetAudioOutputChannel
894      return result.Produce();      return result.Produce();
895  }  }
896    
897    String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
898        dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
899        LSCPResultSet result;
900        try {
901            // get audio output device
902            std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
903            if (!devices[DeviceId]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");
904            MidiInputDevice* pDevice = devices[DeviceId];
905    
906            // get midi port
907            MidiInputDevice::MidiInputPort* pPort = pDevice->GetPort(PortId);
908            if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");
909    
910            // get desired port parameter
911            std::map<String,DeviceCreationParameter*> parameters = pPort->DeviceParameters();
912            if (!parameters[ParameterName]) throw LinuxSamplerException("Midi port does not provice a parameters '" + ParameterName + "'.");
913            DeviceCreationParameter* pParameter = parameters[ParameterName];
914    
915            // return all fields of this audio channel parameter
916            result.Add("TYPE",         pParameter->Type());
917            result.Add("DESCRIPTION",  pParameter->Description());
918            result.Add("FIX",          pParameter->Fix());
919            result.Add("MULTIPLICITY", pParameter->Multiplicity());
920            if (pParameter->RangeMin())      result.Add("RANGE_MIN",     pParameter->RangeMin());
921            if (pParameter->RangeMax())      result.Add("RANGE_MAX",     pParameter->RangeMax());
922            if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
923        }
924        catch (LinuxSamplerException e) {
925            result.Error(e);
926        }
927        return result.Produce();
928    }
929    
930  String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {  String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
931      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()));
932      LSCPResultSet result;      LSCPResultSet result;
# Line 770  String LSCPServer::SetMidiInputDevicePar Line 1009  String LSCPServer::SetMidiInputDevicePar
1009      LSCPResultSet result;      LSCPResultSet result;
1010      try {      try {
1011          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1012          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) + ".");
1013          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1014          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1015          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 + "'");
1016          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1017      }      }
1018      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 788  String LSCPServer::SetMidiInputPortParam Line 1027  String LSCPServer::SetMidiInputPortParam
1027      try {      try {
1028          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1029          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1030          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) + ".");
1031          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1032          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) + ".");
1033          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
1034          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 + "'");
1035          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1036      }      }
1037      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 810  String LSCPServer::SetAudioOutputChannel Line 1049  String LSCPServer::SetAudioOutputChannel
1049      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?
1050  }  }
1051    
1052    String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1053        dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1054        LSCPResultSet result;
1055        try {
1056            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1057            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1058            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1059            AudioOutputDevice* pDevice = devices[AudioDeviceId];
1060            if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1061            pSamplerChannel->SetAudioOutputDevice(pDevice);
1062        }
1063        catch (LinuxSamplerException e) {
1064             result.Error(e);
1065        }
1066        return result.Produce();
1067    }
1068    
1069  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1070      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));
1071      LSCPResultSet result;      LSCPResultSet result;
# Line 818  String LSCPServer::SetAudioOutputType(St Line 1074  String LSCPServer::SetAudioOutputType(St
1074          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1075          // Driver type name aliasing...          // Driver type name aliasing...
1076          if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";          if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";
1077          if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";                  if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";
1078          // Check if there's one audio output device already created          // Check if there's one audio output device already created
1079          // for the intended audio driver type (AudioOutputDriver)...          // for the intended audio driver type (AudioOutputDriver)...
1080          AudioOutputDevice *pDevice = NULL;          AudioOutputDevice *pDevice = NULL;
# Line 847  String LSCPServer::SetAudioOutputType(St Line 1103  String LSCPServer::SetAudioOutputType(St
1103      return result.Produce();      return result.Produce();
1104  }  }
1105    
1106  String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {  String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1107      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));
1108      LSCPResultSet result;      LSCPResultSet result;
1109      try {      try {
 #if 1  
         throw LinuxSamplerException("Command deprecated");  
 #else  
1110          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1111          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1112          // 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  
1113      }      }
1114      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1115           result.Error(e);           result.Error(e);
# Line 869  String LSCPServer::SetMIDIInputType(Stri Line 1117  String LSCPServer::SetMIDIInputType(Stri
1117      return result.Produce();      return result.Produce();
1118  }  }
1119    
1120  /**  String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1121   * 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));  
1122      LSCPResultSet result;      LSCPResultSet result;
1123      try {      try {
1124          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1125          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1126          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);  
1127      }      }
1128      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1129           result.Error(e);           result.Error(e);
# Line 890  String LSCPServer::SetMIDIInput(uint MID Line 1131  String LSCPServer::SetMIDIInput(uint MID
1131      return result.Produce();      return result.Produce();
1132  }  }
1133    
1134  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1135        dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1136      LSCPResultSet result;      LSCPResultSet result;
1137      try {      try {
1138          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1139          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1140          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1141          AudioOutputDevice* pDevice = devices[AudioDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1142          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));
1143          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1144        }
1145        catch (LinuxSamplerException e) {
1146             result.Error(e);
1147        }
1148        return result.Produce();
1149    }
1150    
1151    String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1152        dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1153        LSCPResultSet result;
1154        try {
1155            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1156            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1157            // Driver type name aliasing...
1158            if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";
1159            // Check if there's one MIDI input device already created
1160            // for the intended MIDI driver type (MidiInputDriver)...
1161            MidiInputDevice *pDevice = NULL;
1162            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1163            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1164            for (; iter != devices.end(); iter++) {
1165                if ((iter->second)->Driver() == MidiInputDriver) {
1166                    pDevice = iter->second;
1167                    break;
1168                }
1169            }
1170            // If it doesn't exist, create a new one with default parameters...
1171            if (pDevice == NULL) {
1172                std::map<String,String> params;
1173                pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1174                // Make it with at least one initial port.
1175                std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1176                parameters["ports"]->SetValue("1");
1177            }
1178            // Must have a device...
1179            if (pDevice == NULL)
1180                throw LinuxSamplerException("Internal error: could not create MIDI input device.");
1181            // Set it as the current channel device...
1182            pSamplerChannel->SetMidiInputDevice(pDevice);
1183        }
1184        catch (LinuxSamplerException e) {
1185             result.Error(e);
1186        }
1187        return result.Produce();
1188    }
1189    
1190    /**
1191     * Will be called by the parser to change the MIDI input device, port and channel on which
1192     * engine of a particular sampler channel should listen to.
1193     */
1194    String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1195        dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1196        LSCPResultSet result;
1197        try {
1198            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1199            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1200            std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1201            MidiInputDevice* pDevice = devices[MIDIDeviceId];
1202            if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1203            pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);
1204      }      }
1205      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1206           result.Error(e);           result.Error(e);
# Line 949  String LSCPServer::ResetChannel(uint uiS Line 1251  String LSCPServer::ResetChannel(uint uiS
1251   * 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
1252   * server for receiving event messages.   * server for receiving event messages.
1253   */   */
1254  String LSCPServer::SubscribeNotification(event_t Event) {  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
1255      dmsg(2,("LSCPServer: SubscribeNotification(Event=%d)\n", Event));      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1256      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1257        SubscriptionMutex.Lock();
1258        eventSubscriptions[type].push_back(currentSocket);
1259        SubscriptionMutex.Unlock();
1260        return result.Produce();
1261  }  }
1262    
1263  /**  /**
1264   * 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
1265   * for not receiving further event messages.   * for not receiving further event messages.
1266   */   */
1267  String LSCPServer::UnsubscribeNotification(event_t Event) {  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
1268      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%d)\n", Event));      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1269      return "ERR:0:Not implemented yet.\r\n";      LSCPResultSet result;
1270        SubscriptionMutex.Lock();
1271        eventSubscriptions[type].remove(currentSocket);
1272        SubscriptionMutex.Unlock();
1273        return result.Produce();
1274  }  }
1275    
1276    

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

  ViewVC Help
Powered by ViewVC