--- linuxsampler/trunk/src/network/lscpserver.cpp 2004/03/05 13:46:15 35 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2004/06/12 07:46:02 121 @@ -2,7 +2,7 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -21,16 +21,20 @@ ***************************************************************************/ #include "lscpserver.h" +#include "lscpresultset.h" -LSCPServer::LSCPServer(AudioThread* pEngine) : Thread(false, 0, -4) { - this->pEngine = pEngine; +#include "../engines/gig/Engine.h" + +LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) { + this->pSampler = pSampler; } int LSCPServer::Main() { hSocket = socket(AF_INET, SOCK_STREAM, 0); if (hSocket < 0) { std::cerr << "LSCPServer: Could not create server socket." << std::endl; - return -1; + //return -1; + exit(EXIT_FAILURE); } SocketAddress.sin_family = AF_INET; @@ -40,7 +44,8 @@ if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { std::cerr << "LSCPServer: Could not bind server socket." << std::endl; close(hSocket); - return -1; + //return -1; + exit(EXIT_FAILURE); } listen(hSocket, 1); @@ -54,7 +59,8 @@ if (hSession < 0) { std::cerr << "LSCPServer: Client connection failed." << std::endl; close(hSocket); - return -1; + //return -1; + exit(EXIT_FAILURE); } dmsg(1,("LSCPServer: Client connection established.\n")); @@ -86,18 +92,40 @@ /** * Will be called by the parser to load an instrument. */ -String LSCPServer::LoadInstrument(String Filename, uint Instrument, uint SamplerChannel) { - dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), Instrument, SamplerChannel)); - result_t res = pEngine->LoadInstrument(Filename.c_str(), Instrument); - return ConvertResult(res); +String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + pEngine->LoadInstrument(Filename.c_str(), uiInstrument); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to load and deploy an engine. */ -String LSCPServer::LoadEngine(String EngineName, uint SamplerChannel) { - dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), SamplerChannel)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel)); + LSCPResultSet result; + try { + Engine::type_t type; + if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig; + else throw LinuxSamplerException("Unknown engine type"); + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + pSamplerChannel->LoadEngine(type); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** @@ -105,7 +133,9 @@ */ String LSCPServer::GetChannels() { dmsg(2,("LSCPServer: GetChannels()\n")); - return "1\r\n"; + LSCPResultSet result; + result.Add(pSampler->SamplerChannels()); + return result.Produce(); } /** @@ -113,15 +143,19 @@ */ String LSCPServer::AddChannel() { dmsg(2,("LSCPServer: AddChannel()\n")); - return "ERR:0:Not implemented yet.\r\n"; + SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel(); + LSCPResultSet result(pSamplerChannel->Index()); + return result.Produce(); } /** * Will be called by the parser to remove a sampler channel. */ -String LSCPServer::RemoveChannel(uint SamplerChannel) { - dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", SamplerChannel)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::RemoveChannel(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel)); + LSCPResultSet result; + pSampler->RemoveSamplerChannel(uiSamplerChannel); + return result.Produce(); } /** @@ -129,7 +163,8 @@ */ String LSCPServer::GetAvailableEngines() { dmsg(2,("LSCPServer: GetAvailableEngines()\n")); - return "ERR:0:Not implemented yet.\r\n"; + LSCPResultSet result("GigEngine"); + return result.Produce(); } /** @@ -137,99 +172,258 @@ */ String LSCPServer::GetEngineInfo(String EngineName) { dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str())); - return "ERR:0:Not implemented yet.\r\n"; + LSCPResultSet result; + try { + if ((EngineName == "GigEngine") || (EngineName == "gig")) { + Engine* pEngine = new LinuxSampler::gig::Engine; + result.Add(pEngine->Description()); + delete pEngine; + } + else throw LinuxSamplerException("Unknown engine type"); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to get informations about a particular * sampler channel. */ -String LSCPServer::GetChannelInfo(uint SamplerChannel) { - dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", SamplerChannel)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::GetChannelInfo(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + Engine* pEngine = pSamplerChannel->GetEngine(); + + //Defaults values + String EngineName = "NONE"; + float Volume = 0; + String InstrumentFileName = "NONE"; + int InstrumentIndex = 0; + + if (pEngine) { + EngineName = pEngine->EngineName(); + Volume = pEngine->Volume(); + int iIdx = pEngine->InstrumentIndex(); + if (iIdx != -1) { + InstrumentFileName = pEngine->InstrumentFileName(); + InstrumentIndex = iIdx; + } + } + + result.Add("ENGINE_NAME", EngineName); + result.Add("VOLUME", Volume); + + //Some hardcoded stuff for now to make GUI look good + result.Add("AUDIO_OUTPUT_DEVICE", "0"); + result.Add("AUDIO_OUTPUT_CHANNELS", "2"); + result.Add("AUDIO_OUTPUT_ROUTING", "0,1"); + + result.Add("INSTRUMENT_FILE", InstrumentFileName); + result.Add("INSTRUMENT_NR", InstrumentIndex); + + //Some more hardcoded stuff for now to make GUI look good + result.Add("MIDI_INPUT_DEVICE", "0"); + result.Add("MIDI_INPUT_PORT", "0"); + result.Add("MIDI_INPUT_CHANNEL", "1"); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to get the amount of active voices on a * particular sampler channel. */ -String LSCPServer::GetVoiceCount(uint SamplerChannel) { - dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", SamplerChannel)); - return ToString(pEngine->ActiveVoiceCount) + "\r\n"; +String LSCPServer::GetVoiceCount(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + result.Add(pEngine->VoiceCount()); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to get the amount of active disk streams on a * particular sampler channel. */ -String LSCPServer::GetStreamCount(uint SamplerChannel) { - dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", SamplerChannel)); - return ToString(pEngine->pDiskThread->ActiveStreamCount) + "\r\n"; +String LSCPServer::GetStreamCount(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + result.Add(pEngine->DiskStreamCount()); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to get the buffer fill states of all disk * streams on a particular sampler channel. */ -String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint SamplerChannel) { - dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, SamplerChannel)); - return (ResponseType == fill_response_bytes) ? pEngine->pDiskThread->GetBufferFillBytes() + "\r\n" - : pEngine->pDiskThread->GetBufferFillPercentage() + "\r\n"; +String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + if (!pEngine->DiskStreamSupported()) return "NA\r\n"; //FIXME: Update resultset class to support "NA" + switch (ResponseType) { + case fill_response_bytes: + result.Add(pEngine->DiskStreamBufferFillBytes()); + break; + case fill_response_percentage: + result.Add(pEngine->DiskStreamBufferFillPercentage()); + break; + default: + throw LinuxSamplerException("Unknown fill response type"); + } + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to change the audio output type on a * particular sampler channel. */ -String LSCPServer::SetAudioOutputType(audio_output_type_t AudioOutputType, uint SamplerChannel) { - dmsg(2,("LSCPServer: SetAudioOutputType(AudioOutputType=%d, SamplerChannel=%d)\n", AudioOutputType, SamplerChannel)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::SetAudioOutputType(AudioOutputDevice::type_t AudioOutputType, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetAudioOutputType(AudioOutputType=%d, SamplerChannel=%d)\n", AudioOutputType, uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + pSamplerChannel->SetAudioOutputDevice(AudioOutputType); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to change the audio output channel for * playback on a particular sampler channel. */ -String LSCPServer::SetAudioOutputChannel(uint AudioOutputChannel, uint SamplerChannel) { - dmsg(2,("LSCPServer: SetAudioOutputChannel(AudioOutputChannel=%d, SamplerChannel=%d)\n", AudioOutputChannel, SamplerChannel)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::SetAudioOutputChannel(uint AudioOutputChannel, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetAudioOutputChannel(AudioOutputChannel=%d, SamplerChannel=%d)\n", AudioOutputChannel, uiSamplerChannel)); + return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class? +} + +String LSCPServer::SetMIDIInputType(MidiInputDevice::type_t MidiInputType, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetMIDIInputType(MidiInputType=%d, SamplerChannel=%d)\n", MidiInputType, uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + pSamplerChannel->SetMidiInputDevice(MidiInputType); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to change the MIDI input port on which the * engine of a particular sampler channel should listen to. */ -String LSCPServer::SetMIDIInputPort(String MIDIInputPort, uint Samplerchannel) { - dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIInputPort=%s, Samplerchannel=%d)\n", MIDIInputPort.c_str(), Samplerchannel)); - return "ERR:0:Not implemented yet.\r\n"; +String LSCPServer::SetMIDIInputPort(String MIDIInputPort, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIInputPort=%s, Samplerchannel=%d)\n", MIDIInputPort.c_str(), uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel->GetMidiInputDevice()) throw LinuxSamplerException("No MIDI input device connected yet"); + pSamplerChannel->GetMidiInputDevice()->SetInputPort(MIDIInputPort.c_str()); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to change the MIDI input channel on which the * engine of a particular sampler channel should listen to. */ -String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint SamplerChannel) { - dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n", MIDIChannel, SamplerChannel)); - return "ERR:0:Not implemented yet.\r\n"; +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("Index out of bounds"); + if (!pSamplerChannel->GetMidiInputDevice()) throw LinuxSamplerException("No MIDI input device connected yet"); + MidiInputDevice::type_t oldtype = pSamplerChannel->GetMidiInputDevice()->Type(); + pSamplerChannel->SetMidiInputDevice(oldtype, (MidiInputDevice::midi_chan_t) MIDIChannel); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to change the global volume factor on a * particular sampler channel. */ -String LSCPServer::SetVolume(double Volume, uint SamplerChannel) { - dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, SamplerChannel)); - pEngine->Volume = Volume; - return "OK\r\n"; +String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + pEngine->Volume(Volume); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /** * Will be called by the parser to reset a particular sampler channel. */ -String LSCPServer::ResetChannel(uint SamplerChannel) { - dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", SamplerChannel)); - pEngine->Reset(); - return "OK\r\n"; +String LSCPServer::ResetChannel(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + pEngine->Reset(); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } /**