/[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 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 "../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<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 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      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          close(hSession);          //Something was selected and it was not the hSocket, so it must be some command(s) coming.
132          dmsg(1,("LSCPServer: Client connection terminated.\n"));          for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
133                    if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?
134                            if (GetLSCPCommand(iter)) {     //Have we read the entire command?
135                                    dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
136                                    yylex_init(&((*iter).pScanner)); //FIXME: should me moved out of this loop and initialized only when a new session is created
137                                    currentSocket = (*iter).hSession;  //a hack
138                                    if ((*iter).bVerbose) { // if echo mode enabled
139                                        AnswerClient(bufferedCommands[currentSocket]);
140                                    }
141                                    int result = yyparse(&(*iter));
142                                    currentSocket = -1;     //continuation of a hack
143                                    dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
144                                    if (result == LSCP_QUIT) { //Was it a quit command by any chance?
145                                            CloseConnection(iter);
146                                    }
147                            }
148                            //socket may have been closed, iter may be invalid, get out of the loop for now.
149                            //we'll be back if there is data.
150                            break;
151                    }
152            }
153    
154            //Now let's deliver late notifies (if any)
155            NotifyBufferMutex.Lock();
156            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
157                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
158                    bufferedNotifies.erase(iterNotify);
159            }
160            NotifyBufferMutex.Unlock();
161      }      }
162        //It will never get here anyway
163        //yylex_destroy(yyparse_param.pScanner);
164    }
165    
166    void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
167            int socket = (*iter).hSession;
168            dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
169            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
170            Sessions.erase(iter);
171            FD_CLR(socket,  &fdSet);
172            SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
173            for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
174                    iter->second.remove(socket);
175            }
176            SubscriptionMutex.Unlock();
177            NotifyMutex.Lock();
178            bufferedCommands.erase(socket);
179            bufferedNotifies.erase(socket);
180            close(socket);
181            NotifyMutex.Unlock();
182            //yylex_destroy((*iter).pScanner);
183    }
184    
185    void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
186            SubscriptionMutex.Lock();
187            if (eventSubscriptions.count(event.GetType()) == 0) {
188                    SubscriptionMutex.Unlock();     //Nobody is subscribed to this event
189                    return;
190            }
191            std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
192            std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
193            String notify = event.Produce();
194    
195            while (true) {
196                    if (NotifyMutex.Trylock()) {
197                            for(;iter != end; iter++)
198                                    send(*iter, notify.c_str(), notify.size(), 0);
199                            NotifyMutex.Unlock();
200                            break;
201                    } else {
202                            if (NotifyBufferMutex.Trylock()) {
203                                    for(;iter != end; iter++)
204                                            bufferedNotifies[*iter] += notify;
205                                    NotifyBufferMutex.Unlock();
206                                    break;
207                            }
208                    }
209            }
210            SubscriptionMutex.Unlock();
211    }
212    
213    extern int GetLSCPCommand( void *buf, int max_size ) {
214            String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
215            if (command.size() == 0) {              //Parser wants input but we have nothing.
216                    strcpy((char*) buf, "\n");      //So give it an empty command
217                    return 1;                       //to keep it happy.
218            }
219    
220            if (max_size < command.size()) {
221                    std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
222                    return 0;       //This will never happen
223            }
224    
225            strcpy((char*) buf, command.c_str());
226            LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
227            return command.size();
228    }
229    
230    /**
231     * Will be called to try to read the command from the socket
232     * If command is read, it will return true. Otherwise false is returned.
233     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
234     */
235    bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
236            int socket = (*iter).hSession;
237            char c;
238            int i = 0;
239            while (true) {
240                    int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
241                    if (result == 0) { //socket was selected, so 0 here means client has closed the connection
242                            CloseConnection(iter);
243                            break;
244                    }
245                    if (result == 1) {
246                            if (c == '\r')
247                                    continue; //Ignore CR
248                            if (c == '\n') {
249                                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
250                                    bufferedCommands[socket] += "\n";
251                                    return true; //Complete command was read
252                            }
253                            bufferedCommands[socket] += c;
254                    }
255                    if (result == -1) {
256                            if (errno == EAGAIN) //Would block, try again later.
257                                    return false;
258                            switch(errno) {
259                                    case EBADF:
260                                            dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
261                                            break;
262                                    case ECONNREFUSED:
263                                            dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
264                                            break;
265                                    case ENOTCONN:
266                                            dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
267                                            break;
268                                    case ENOTSOCK:
269                                            dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
270                                            break;
271                                    case EAGAIN:
272                                            dmsg(2,("LSCPScanner: The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received.\n"));
273                                            break;
274                                    case EINTR:
275                                            dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
276                                            break;
277                                    case EFAULT:
278                                            dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
279                                            break;
280                                    case EINVAL:
281                                            dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
282                                            break;
283                                    case ENOMEM:
284                                            dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
285                                            break;
286                                    default:
287                                            dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
288                                            break;
289                            }
290                            CloseConnection(iter);
291                            break;
292                    }
293            }
294            return false;
295  }  }
296    
297  /**  /**
# Line 88  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  /**  /**
# Line 241  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 326  String LSCPServer::GetChannelInfo(uint u Line 561  String LSCPServer::GetChannelInfo(uint u
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);
   
         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());  
                 }  
   
         }  
571      }      }
572      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
573           result.Error(e);           result.Error(e);
# Line 628  String LSCPServer::GetMidiInputDeviceInf Line 856  String LSCPServer::GetMidiInputDeviceInf
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          result.Add("driver", pDevice->Driver());          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();
# Line 647  String LSCPServer::GetMidiInputPortInfo( Line 875  String LSCPServer::GetMidiInputPortInfo(
875      try {      try {
876          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
877          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
878          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) + ".");
879          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
880          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) + ".");
881          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
882          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();          std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
883          for (; iter != parameters.end(); iter++) {          for (; iter != parameters.end(); iter++) {
# Line 688  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 770  String LSCPServer::SetMidiInputDevicePar Line 1031  String LSCPServer::SetMidiInputDevicePar
1031      LSCPResultSet result;      LSCPResultSet result;
1032      try {      try {
1033          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1034          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) + ".");
1035          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1036          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          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 + "'");          if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1038          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1039      }      }
1040      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 788  String LSCPServer::SetMidiInputPortParam Line 1049  String LSCPServer::SetMidiInputPortParam
1049      try {      try {
1050          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1051          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1052          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) + ".");
1053          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1054          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) + ".");
1055          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
1056          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 + "'");
1057          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1058      }      }
1059      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 810  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::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1075        dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1076        LSCPResultSet result;
1077        try {
1078            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1079            if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1080            std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1081            AudioOutputDevice* pDevice = devices[AudioDeviceId];
1082            if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1083            pSamplerChannel->SetAudioOutputDevice(pDevice);
1084        }
1085        catch (LinuxSamplerException e) {
1086             result.Error(e);
1087        }
1088        return result.Produce();
1089    }
1090    
1091  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1092      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));
1093      LSCPResultSet result;      LSCPResultSet result;
# Line 818  String LSCPServer::SetAudioOutputType(St Line 1096  String LSCPServer::SetAudioOutputType(St
1096          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1097          // Driver type name aliasing...          // Driver type name aliasing...
1098          if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";          if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";
1099          if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";                  if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";
1100          // Check if there's one audio output device already created          // Check if there's one audio output device already created
1101          // for the intended audio driver type (AudioOutputDriver)...          // for the intended audio driver type (AudioOutputDriver)...
1102          AudioOutputDevice *pDevice = NULL;          AudioOutputDevice *pDevice = NULL;
# Line 847  String LSCPServer::SetAudioOutputType(St Line 1125  String LSCPServer::SetAudioOutputType(St
1125      return result.Produce();      return result.Produce();
1126  }  }
1127    
1128  String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {  String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1129      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));
1130      LSCPResultSet result;      LSCPResultSet result;
1131      try {      try {
 #if 1  
         throw LinuxSamplerException("Command deprecated");  
 #else  
1132          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1133          if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1134          // 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  
1135      }      }
1136      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1137           result.Error(e);           result.Error(e);
# Line 869  String LSCPServer::SetMIDIInputType(Stri Line 1139  String LSCPServer::SetMIDIInputType(Stri
1139      return result.Produce();      return result.Produce();
1140  }  }
1141    
1142  /**  String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1143   * 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));  
1144      LSCPResultSet result;      LSCPResultSet result;
1145      try {      try {
1146          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1147          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1148          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);  
1149      }      }
1150      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1151           result.Error(e);           result.Error(e);
# Line 890  String LSCPServer::SetMIDIInput(uint MID Line 1153  String LSCPServer::SetMIDIInput(uint MID
1153      return result.Produce();      return result.Produce();
1154  }  }
1155    
1156  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  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          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1161          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
1162          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1163          AudioOutputDevice* pDevice = devices[AudioDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1164          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));
1165          pSamplerChannel->SetAudioOutputDevice(pDevice);          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 949  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(event_t Event) {  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
1277      dmsg(2,("LSCPServer: SubscribeNotification(Event=%d)\n", Event));      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(event_t Event) {  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
1290      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%d)\n", Event));      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.155  
changed lines
  Added in v.210

  ViewVC Help
Powered by ViewVC