--- linuxsampler/trunk/src/network/lscpserver.cpp 2004/06/28 04:30:11 155 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2004/07/18 00:29:39 209 @@ -22,17 +22,47 @@ #include "lscpserver.h" #include "lscpresultset.h" +#include "lscpevent.h" #include "../engines/gig/Engine.h" -#include "../audiodriver/AudioOutputDeviceFactory.h" -#include "../mididriver/MidiInputDeviceFactory.h" +#include "../drivers/audio/AudioOutputDeviceFactory.h" +#include "../drivers/midi/MidiInputDeviceFactory.h" + +/** + * Below are a few static members of the LSCPServer class. + * The big assumption here is that LSCPServer is going to remain a singleton. + * These members are used to support client connections. + * Class handles multiple connections at the same time using select() and non-blocking recv() + * Commands are processed by a single LSCPServer thread. + * Notifications are delivered either by the thread that originated them + * or (if the resultset is currently in progress) by the LSCPServer thread + * after the resultset was sent out. + * This makes sure that resultsets can not be interrupted by notifications. + * This also makes sure that the thread sending notification is not blocked + * by the LSCPServer thread. + */ +fd_set LSCPServer::fdSet; +int LSCPServer::currentSocket = -1; +std::vector LSCPServer::hSessions = std::vector(); +std::map LSCPServer::bufferedNotifies = std::map(); +std::map LSCPServer::bufferedCommands = std::map(); +std::map< LSCPEvent::event_t, std::list > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list >(); +Mutex LSCPServer::NotifyMutex = Mutex(); +Mutex LSCPServer::NotifyBufferMutex = Mutex(); +Mutex LSCPServer::SubscriptionMutex = Mutex(); LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) { this->pSampler = pSampler; + LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS"); + LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL"); + LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); } int LSCPServer::Main() { - hSocket = socket(AF_INET, SOCK_STREAM, 0); + int hSocket = socket(AF_INET, SOCK_STREAM, 0); if (hSocket < 0) { std::cerr << "LSCPServer: Could not create server socket." << std::endl; //return -1; @@ -56,28 +86,207 @@ // now wait for client connections and handle their requests sockaddr_in client; int length = sizeof(client); + FD_ZERO(&fdSet); + FD_SET(hSocket, &fdSet); + int maxSessions = hSocket; + + // Parser initialization + yyparse_param_t yyparse_param; + yyparse_param.pServer = this; + while (true) { - hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length); - if (hSession < 0) { - std::cerr << "LSCPServer: Client connection failed." << std::endl; - close(hSocket); - //return -1; - exit(EXIT_FAILURE); - } - - dmsg(1,("LSCPServer: Client connection established.\n")); - //send(hSession, "Welcome!\r\n", 10, 0); - - // Parser invocation - yyparse_param_t yyparse_param; - yyparse_param.pServer = this; - yylex_init(&yyparse_param.pScanner); - while (yyparse(&yyparse_param) == LSCP_SYNTAX_ERROR); // recall parser in case of syntax error - yylex_destroy(yyparse_param.pScanner); + fd_set selectSet = fdSet; + int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL); + if (retval == 0) + continue; //Nothing try again + if (retval == -1) { + std::cerr << "LSCPServer: Socket select error." << std::endl; + close(hSocket); + exit(EXIT_FAILURE); + } - close(hSession); - dmsg(1,("LSCPServer: Client connection terminated.\n")); + //Accept new connections now (if any) + if (FD_ISSET(hSocket, &selectSet)) { + int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length); + if (socket < 0) { + std::cerr << "LSCPServer: Client connection failed." << std::endl; + exit(EXIT_FAILURE); + } + + if (fcntl(socket, F_SETFL, O_NONBLOCK)) { + std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; + exit(EXIT_FAILURE); + } + + hSessions.push_back(socket); + FD_SET(socket, &fdSet); + if (socket > maxSessions) + maxSessions = socket; + dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket)); + continue; //Maybe this was the only selected socket, better select again + } + + //Something was selected and it was not the hSocket, so it must be some command(s) coming. + for (std::vector::iterator iter = hSessions.begin(); iter != hSessions.end(); iter++) { + if (FD_ISSET(*iter, &selectSet)) { //Was it this socket? + if (GetLSCPCommand(iter)) { //Have we read the entire command? + dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket)); + yylex_init(&yyparse_param.pScanner); + currentSocket = *iter; //a hack + int result = yyparse(&yyparse_param); + currentSocket = -1; //continuation of a hack + dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); + if (result == LSCP_QUIT) { //Was it a quit command by any chance? + CloseConnection(iter); + } + } + //socket may have been closed, iter may be invalid, get out of the loop for now. + //we'll be back if there is data. + break; + } + } + + //Now let's deliver late notifies (if any) + NotifyBufferMutex.Lock(); + for (std::map::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) { + send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0); + bufferedNotifies.erase(iterNotify); + } + NotifyBufferMutex.Unlock(); } + //It will never get here anyway + //yylex_destroy(yyparse_param.pScanner); +} + +void LSCPServer::CloseConnection( std::vector::iterator iter ) { + int socket = *iter; + dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket)); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket)); + hSessions.erase(iter); + FD_CLR(socket, &fdSet); + SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any) + for (std::map< LSCPEvent::event_t, std::list >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) { + iter->second.remove(socket); + } + SubscriptionMutex.Unlock(); + NotifyMutex.Lock(); + bufferedCommands.erase(socket); + bufferedNotifies.erase(socket); + close(socket); + NotifyMutex.Unlock(); +} + +void LSCPServer::SendLSCPNotify( LSCPEvent event ) { + SubscriptionMutex.Lock(); + if (eventSubscriptions.count(event.GetType()) == 0) { + SubscriptionMutex.Unlock(); //Nobody is subscribed to this event + return; + } + std::list::iterator iter = eventSubscriptions[event.GetType()].begin(); + std::list::iterator end = eventSubscriptions[event.GetType()].end(); + String notify = event.Produce(); + + while (true) { + if (NotifyMutex.Trylock()) { + for(;iter != end; iter++) + send(*iter, notify.c_str(), notify.size(), 0); + NotifyMutex.Unlock(); + break; + } else { + if (NotifyBufferMutex.Trylock()) { + for(;iter != end; iter++) + bufferedNotifies[*iter] += notify; + NotifyBufferMutex.Unlock(); + break; + } + } + } + SubscriptionMutex.Unlock(); +} + +extern int GetLSCPCommand( void *buf, int max_size ) { + String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket]; + if (command.size() == 0) { //Parser wants input but we have nothing. + strcpy((char*) buf, "\n"); //So give it an empty command + return 1; //to keep it happy. + } + + if (max_size < command.size()) { + std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl; + return 0; //This will never happen + } + + strcpy((char*) buf, command.c_str()); + LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket); + return command.size(); +} + +/** + * Will be called to try to read the command from the socket + * If command is read, it will return true. Otherwise false is returned. + * In any case the received portion (complete or incomplete) is saved into bufferedCommand map. + */ +bool LSCPServer::GetLSCPCommand( std::vector::iterator iter ) { + int socket = *iter; + char c; + int i = 0; + while (true) { + int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now + if (result == 0) { //socket was selected, so 0 here means client has closed the connection + CloseConnection(iter); + break; + } + if (result == 1) { + if (c == '\r') + continue; //Ignore CR + if (c == '\n') { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket)); + bufferedCommands[socket] += "\n"; + return true; //Complete command was read + } + bufferedCommands[socket] += c; + } + if (result == -1) { + if (errno == EAGAIN) //Would block, try again later. + return false; + switch(errno) { + case EBADF: + dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n")); + break; + case ECONNREFUSED: + dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n")); + break; + case ENOTCONN: + dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n")); + break; + case ENOTSOCK: + dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n")); + break; + case EAGAIN: + 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")); + break; + case EINTR: + dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n")); + break; + case EFAULT: + dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n")); + break; + case EINVAL: + dmsg(2,("LSCPScanner: Invalid argument passed.\n")); + break; + case ENOMEM: + dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n")); + break; + default: + dmsg(2,("LSCPScanner: Unknown recv() error.\n")); + break; + } + CloseConnection(iter); + break; + } + } + return false; } /** @@ -88,7 +297,11 @@ */ void LSCPServer::AnswerClient(String ReturnMessage) { dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str())); - send(hSession, ReturnMessage.c_str(), ReturnMessage.size(), 0); + if (currentSocket != -1) { + NotifyMutex.Lock(); + send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0); + NotifyMutex.Unlock(); + } } /** @@ -241,6 +454,23 @@ } /** + * Will be called by the parser to get the list of sampler channels. + */ +String LSCPServer::ListChannels() { + dmsg(2,("LSCPServer: ListChannels()\n")); + String list; + std::map channels = pSampler->GetSamplerChannels(); + std::map::iterator iter = channels.begin(); + for (; iter != channels.end(); iter++) { + if (list != "") list += ","; + list += ToString(iter->first); + } + LSCPResultSet result; + result.Add(list); + return result.Produce(); +} + +/** * Will be called by the parser to add a sampler channel. */ String LSCPServer::AddChannel() { @@ -326,20 +556,13 @@ result.Add("AUDIO_OUTPUT_CHANNELS", "2"); result.Add("AUDIO_OUTPUT_ROUTING", "0,1"); + result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice())); + result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort()); + result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); + result.Add("INSTRUMENT_FILE", InstrumentFileName); result.Add("INSTRUMENT_NR", InstrumentIndex); 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()); - } - - } } catch (LinuxSamplerException e) { result.Error(e); @@ -628,7 +851,7 @@ try { std::map devices = pSampler->GetMidiInputDevices(); MidiInputDevice* pDevice = devices[DeviceIndex]; - 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) + "."); result.Add("driver", pDevice->Driver()); std::map parameters = pDevice->DeviceParameters(); std::map::iterator iter = parameters.begin(); @@ -647,9 +870,9 @@ try { std::map devices = pSampler->GetMidiInputDevices(); MidiInputDevice* pDevice = devices[DeviceIndex]; - 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) + "."); MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex); - 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) + "."); std::map parameters = pMidiInputPort->DeviceParameters(); std::map::iterator iter = parameters.begin(); for (; iter != parameters.end(); iter++) { @@ -688,6 +911,39 @@ return result.Produce(); } +String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) { + dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str())); + LSCPResultSet result; + try { + // get audio output device + std::map devices = pSampler->GetMidiInputDevices(); + if (!devices[DeviceId]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + "."); + MidiInputDevice* pDevice = devices[DeviceId]; + + // get midi port + MidiInputDevice::MidiInputPort* pPort = pDevice->GetPort(PortId); + if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + "."); + + // get desired port parameter + std::map parameters = pPort->DeviceParameters(); + if (!parameters[ParameterName]) throw LinuxSamplerException("Midi port does not provice a parameters '" + ParameterName + "'."); + DeviceCreationParameter* pParameter = parameters[ParameterName]; + + // return all fields of this audio channel parameter + result.Add("TYPE", pParameter->Type()); + result.Add("DESCRIPTION", pParameter->Description()); + result.Add("FIX", pParameter->Fix()); + result.Add("MULTIPLICITY", pParameter->Multiplicity()); + if (pParameter->RangeMin()) result.Add("RANGE_MIN", pParameter->RangeMin()); + if (pParameter->RangeMax()) result.Add("RANGE_MAX", pParameter->RangeMax()); + if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities()); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) { dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str())); LSCPResultSet result; @@ -770,10 +1026,10 @@ LSCPResultSet result; try { std::map devices = pSampler->GetMidiInputDevices(); - 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) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; std::map parameters = pDevice->DeviceParameters(); - 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 + "'"); parameters[ParamKey]->SetValue(ParamVal); } catch (LinuxSamplerException e) { @@ -788,11 +1044,11 @@ try { std::map devices = pSampler->GetMidiInputDevices(); MidiInputDevice* pDevice = devices[DeviceIndex]; - 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) + "."); MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex); - 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) + "."); std::map parameters = pMidiInputPort->DeviceParameters(); - 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 + "'"); parameters[ParamKey]->SetValue(ParamVal); } catch (LinuxSamplerException e) { @@ -810,6 +1066,23 @@ return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class? } +String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + std::map devices = pSampler->GetAudioOutputDevices(); + AudioOutputDevice* pDevice = devices[AudioDeviceId]; + if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId)); + pSamplerChannel->SetAudioOutputDevice(pDevice); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) { dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel)); LSCPResultSet result; @@ -818,7 +1091,7 @@ if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa"; - if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack"; + if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack"; // Check if there's one audio output device already created // for the intended audio driver type (AudioOutputDriver)... AudioOutputDevice *pDevice = NULL; @@ -847,21 +1120,13 @@ return result.Produce(); } -String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) { - dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel)); +String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel)); LSCPResultSet result; try { -#if 1 - throw LinuxSamplerException("Command deprecated"); -#else SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); - // 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) - 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 + if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + pSamplerChannel->SetMidiInputPort(MIDIPort); } catch (LinuxSamplerException e) { result.Error(e); @@ -869,20 +1134,13 @@ return result.Produce(); } -/** - * Will be called by the parser to change the MIDI input device, port and channel on which - * 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)); +String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel)); LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); - std::map devices = pSampler->GetMidiInputDevices(); - 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); + pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel); } catch (LinuxSamplerException e) { result.Error(e); @@ -890,15 +1148,76 @@ return result.Produce(); } -String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) { +String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); - std::map devices = pSampler->GetAudioOutputDevices(); - AudioOutputDevice* pDevice = devices[AudioDeviceId]; - if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId)); - pSamplerChannel->SetAudioOutputDevice(pDevice); + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + std::map devices = pSampler->GetMidiInputDevices(); + MidiInputDevice* pDevice = devices[MIDIDeviceId]; + if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); + pSamplerChannel->SetMidiInputDevice(pDevice); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + // Driver type name aliasing... + if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa"; + // Check if there's one MIDI input device already created + // for the intended MIDI driver type (MidiInputDriver)... + MidiInputDevice *pDevice = NULL; + std::map devices = pSampler->GetMidiInputDevices(); + std::map::iterator iter = devices.begin(); + for (; iter != devices.end(); iter++) { + if ((iter->second)->Driver() == MidiInputDriver) { + pDevice = iter->second; + break; + } + } + // If it doesn't exist, create a new one with default parameters... + if (pDevice == NULL) { + std::map params; + pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params); + // Make it with at least one initial port. + std::map parameters = pDevice->DeviceParameters(); + parameters["ports"]->SetValue("1"); + } + // Must have a device... + if (pDevice == NULL) + throw LinuxSamplerException("Internal error: could not create MIDI input device."); + // Set it as the current channel device... + pSamplerChannel->SetMidiInputDevice(pDevice); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + +/** + * Will be called by the parser to change the MIDI input device, port and channel on which + * engine of a particular sampler channel should listen to. + */ +String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + std::map devices = pSampler->GetMidiInputDevices(); + MidiInputDevice* pDevice = devices[MIDIDeviceId]; + if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); + pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel); } catch (LinuxSamplerException e) { result.Error(e); @@ -949,18 +1268,26 @@ * Will be called by the parser to subscribe a client (frontend) on the * server for receiving event messages. */ -String LSCPServer::SubscribeNotification(event_t Event) { - dmsg(2,("LSCPServer: SubscribeNotification(Event=%d)\n", Event)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) { + dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str())); + LSCPResultSet result; + SubscriptionMutex.Lock(); + eventSubscriptions[type].push_back(currentSocket); + SubscriptionMutex.Unlock(); + return result.Produce(); } /** * Will be called by the parser to unsubscribe a client on the server * for not receiving further event messages. */ -String LSCPServer::UnsubscribeNotification(event_t Event) { - dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%d)\n", Event)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) { + dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str())); + LSCPResultSet result; + SubscriptionMutex.Lock(); + eventSubscriptions[type].remove(currentSocket); + SubscriptionMutex.Unlock(); + return result.Produce(); }