--- linuxsampler/trunk/src/network/lscpserver.cpp 2004/07/25 23:27:41 211 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2006/03/04 16:23:53 841 @@ -3,19 +3,20 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005 Christian Schoenebeck * * * - * This program is free software; you can redistribute it and/or modify * + * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * - * This program is distributed in the hope that it will be useful, * + * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software * + * along with this library; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * * MA 02111-1307 USA * ***************************************************************************/ @@ -23,8 +24,16 @@ #include "lscpserver.h" #include "lscpresultset.h" #include "lscpevent.h" +//#include "../common/global.h" -#include "../engines/gig/Engine.h" +#include + +#if HAVE_SQLITE3 +# include "sqlite3.h" +#endif + +#include "../engines/EngineFactory.h" +#include "../engines/EngineChannelFactory.h" #include "../drivers/audio/AudioOutputDeviceFactory.h" #include "../drivers/midi/MidiInputDeviceFactory.h" @@ -50,15 +59,25 @@ Mutex LSCPServer::NotifyMutex = Mutex(); Mutex LSCPServer::NotifyBufferMutex = Mutex(); Mutex LSCPServer::SubscriptionMutex = Mutex(); +Mutex LSCPServer::RTNotifyMutex = Mutex(); -LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) { +LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) { + SocketAddress.sin_family = AF_INET; + SocketAddress.sin_addr.s_addr = addr; + SocketAddress.sin_port = port; this->pSampler = pSampler; - LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS"); + LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT"); 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_channel_info, "CHANNEL_INFO"); LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); + LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); + hSocket = -1; +} + +LSCPServer::~LSCPServer() { + if (hSocket >= 0) close(hSocket); } /** @@ -76,22 +95,27 @@ } int LSCPServer::Main() { - int hSocket = socket(AF_INET, SOCK_STREAM, 0); + hSocket = socket(AF_INET, SOCK_STREAM, 0); if (hSocket < 0) { std::cerr << "LSCPServer: Could not create server socket." << std::endl; //return -1; exit(EXIT_FAILURE); } - SocketAddress.sin_family = AF_INET; - SocketAddress.sin_port = htons(LSCP_PORT); - SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { - std::cerr << "LSCPServer: Could not bind server socket." << std::endl; - close(hSocket); - //return -1; - exit(EXIT_FAILURE); + std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds..."; + for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds + if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { + if (trial > LSCP_SERVER_BIND_TIMEOUT) { + std::cerr << "gave up!" << std::endl; + close(hSocket); + //return -1; + exit(EXIT_FAILURE); + } + else sleep(1); // sleep 1s + } + else break; // success + } } listen(hSocket, 1); @@ -104,9 +128,39 @@ FD_SET(hSocket, &fdSet); int maxSessions = hSocket; + timeval timeout; + while (true) { - fd_set selectSet = fdSet; - int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL); + // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers + { + std::set engineChannels = EngineChannelFactory::EngineChannelInstances(); + std::set::iterator itEngineChannel = engineChannels.begin(); + std::set::iterator itEnd = engineChannels.end(); + for (; itEngineChannel != itEnd; ++itEngineChannel) { + if ((*itEngineChannel)->StatusChanged()) { + SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex)); + } + } + } + + //Now let's deliver late notifies (if any) + NotifyBufferMutex.Lock(); + for (std::map::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) { +#ifdef MSG_NOSIGNAL + send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL); +#else + send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0); +#endif + } + bufferedNotifies.clear(); + NotifyBufferMutex.Unlock(); + + fd_set selectSet = fdSet; + timeout.tv_sec = 0; + timeout.tv_usec = 100000; + + int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout); + if (retval == 0) continue; //Nothing try again if (retval == -1) { @@ -147,8 +201,10 @@ if (FD_ISSET((*iter).hSession, &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(&((*iter).pScanner)); //FIXME: should me moved out of this loop and initialized only when a new session is created + int dummy; // just a temporary hack to fulfill the restart() function prototype + restart(NULL, dummy); // restart the 'scanner' currentSocket = (*iter).hSession; //a hack + dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str())); if ((*iter).bVerbose) { // if echo mode enabled AnswerClient(bufferedCommands[currentSocket]); } @@ -164,17 +220,7 @@ 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 ) { @@ -193,7 +239,18 @@ bufferedNotifies.erase(socket); close(socket); NotifyMutex.Unlock(); - //yylex_destroy((*iter).pScanner); +} + +int LSCPServer::EventSubscribers( std::list events ) { + int subs = 0; + SubscriptionMutex.Lock(); + for( std::list::iterator iter = events.begin(); + iter != events.end(); iter++) + { + subs += eventSubscriptions.count(*iter); + } + SubscriptionMutex.Unlock(); + return subs; } void LSCPServer::SendLSCPNotify( LSCPEvent event ) { @@ -209,7 +266,11 @@ while (true) { if (NotifyMutex.Trylock()) { for(;iter != end; iter++) +#ifdef MSG_NOSIGNAL + send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL); +#else send(*iter, notify.c_str(), notify.size(), 0); +#endif NotifyMutex.Unlock(); break; } else { @@ -318,7 +379,11 @@ dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str())); if (currentSocket != -1) { NotifyMutex.Lock(); +#ifdef MSG_NOSIGNAL + send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL); +#else send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0); +#endif NotifyMutex.Unlock(); } } @@ -392,7 +457,7 @@ LSCPResultSet result; try { std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); AudioOutputDevice* pDevice = devices[DeviceIndex]; pSampler->DestroyAudioOutputDevice(pDevice); } @@ -407,8 +472,8 @@ LSCPResultSet result; try { std::map devices = pSampler->GetMidiInputDevices(); + if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; - if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); pSampler->DestroyMidiInputDevice(pDevice); } catch (LinuxSamplerException e) { @@ -425,16 +490,20 @@ 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 (pSamplerChannel->GetAudioOutputDevice() == NULL) - throw LinuxSamplerException("No audio output device on channel"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet"); + if (!pSamplerChannel->GetAudioOutputDevice()) + throw LinuxSamplerException("No audio output device connected to sampler channel"); if (bBackground) { - LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument); - pLoadInstrument->StartThread(); + InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel); + } + else { + // tell the engine channel which instrument to load + pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument); + // actually start to load the instrument (blocks until completed) + pEngineChannel->LoadInstrument(); } - else pEngine->LoadInstrument(Filename.c_str(), uiInstrument); } catch (LinuxSamplerException e) { result.Error(e); @@ -443,18 +512,19 @@ } /** - * Will be called by the parser to load and deploy an engine. + * Will be called by the parser to assign a sampler engine type to a + * sampler channel. */ -String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) { - dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel)); +String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%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); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + LockRTNotify(); + pSamplerChannel->SetEngineType(EngineName); + if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1); + UnlockRTNotify(); } catch (LinuxSamplerException e) { result.Error(e); @@ -494,7 +564,9 @@ */ String LSCPServer::AddChannel() { dmsg(2,("LSCPServer: AddChannel()\n")); + LockRTNotify(); SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel(); + UnlockRTNotify(); LSCPResultSet result(pSamplerChannel->Index()); return result.Produce(); } @@ -505,37 +577,48 @@ String LSCPServer::RemoveChannel(uint uiSamplerChannel) { dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; + LockRTNotify(); pSampler->RemoveSamplerChannel(uiSamplerChannel); + UnlockRTNotify(); return result.Produce(); } /** - * Will be called by the parser to get all available engines. + * Will be called by the parser to get the amount of all available engines. */ String LSCPServer::GetAvailableEngines() { dmsg(2,("LSCPServer: GetAvailableEngines()\n")); - LSCPResultSet result("GigEngine"); + LSCPResultSet result("1"); + return result.Produce(); +} + +/** + * Will be called by the parser to get a list of all available engines. + */ +String LSCPServer::ListAvailableEngines() { + dmsg(2,("LSCPServer: ListAvailableEngines()\n")); + LSCPResultSet result("\'GIG\'"); return result.Produce(); } /** - * Will be called by the parser to get descriptions for a particular engine. + * Will be called by the parser to get descriptions for a particular + * sampler engine. */ String LSCPServer::GetEngineInfo(String EngineName) { dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str())); LSCPResultSet result; + LockRTNotify(); try { - if ((EngineName == "GigEngine") || (EngineName == "gig")) { - Engine* pEngine = new LinuxSampler::gig::Engine; - result.Add(pEngine->Description()); - result.Add(pEngine->Version()); - delete pEngine; - } - else throw LinuxSamplerException("Unknown engine type"); + Engine* pEngine = EngineFactory::Create(EngineName); + result.Add("DESCRIPTION", pEngine->Description()); + result.Add("VERSION", pEngine->Version()); + EngineFactory::Destroy(pEngine); } catch (LinuxSamplerException e) { result.Error(e); } + UnlockRTNotify(); return result.Produce(); } @@ -548,23 +631,37 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); - Engine* pEngine = pSamplerChannel->GetEngine(); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); //Defaults values String EngineName = "NONE"; - float Volume = 0; + float Volume = 0.0f; String InstrumentFileName = "NONE"; + String InstrumentName = "NONE"; int InstrumentIndex = -1; int InstrumentStatus = -1; - - if (pEngine) { - EngineName = pEngine->EngineName(); - Volume = pEngine->Volume(); - InstrumentStatus = pEngine->InstrumentStatus(); - InstrumentIndex = pEngine->InstrumentIndex(); - if (InstrumentIndex != -1) - InstrumentFileName = pEngine->InstrumentFileName(); + int AudioOutputChannels = 0; + String AudioRouting; + int Mute = 0; + bool Solo = false; + + if (pEngineChannel) { + EngineName = pEngineChannel->EngineName(); + AudioOutputChannels = pEngineChannel->Channels(); + Volume = pEngineChannel->Volume(); + InstrumentStatus = pEngineChannel->InstrumentStatus(); + InstrumentIndex = pEngineChannel->InstrumentIndex(); + if (InstrumentIndex != -1) { + InstrumentFileName = pEngineChannel->InstrumentFileName(); + InstrumentName = pEngineChannel->InstrumentName(); + } + for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { + if (AudioRouting != "") AudioRouting += ","; + AudioRouting += ToString(pEngineChannel->OutputChannel(chan)); + } + Mute = pEngineChannel->GetMute(); + Solo = pEngineChannel->GetSolo(); } result.Add("ENGINE_NAME", EngineName); @@ -572,16 +669,20 @@ //Some not-so-hardcoded stuff to make GUI look good result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice())); - result.Add("AUDIO_OUTPUT_CHANNELS", "2"); - result.Add("AUDIO_OUTPUT_ROUTING", "0,1"); + result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels); + result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice())); result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort()); - result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); + if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); + else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); result.Add("INSTRUMENT_FILE", InstrumentFileName); result.Add("INSTRUMENT_NR", InstrumentIndex); + result.Add("INSTRUMENT_NAME", InstrumentName); result.Add("INSTRUMENT_STATUS", InstrumentStatus); + result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); + result.Add("SOLO", Solo); } catch (LinuxSamplerException e) { result.Error(e); @@ -598,10 +699,11 @@ 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()); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel"); + if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel"); + result.Add(pEngineChannel->GetEngine()->VoiceCount()); } catch (LinuxSamplerException e) { result.Error(e); @@ -618,10 +720,11 @@ 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()); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel"); + result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); } catch (LinuxSamplerException e) { result.Error(e); @@ -638,19 +741,19 @@ 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()) - result.Add("NA"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel"); + if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); else { switch (ResponseType) { case fill_response_bytes: - result.Add(pEngine->DiskStreamBufferFillBytes()); - break; + result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes()); + break; case fill_response_percentage: - result.Add(pEngine->DiskStreamBufferFillPercentage()); - break; + result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage()); + break; default: throw LinuxSamplerException("Unknown fill response type"); } @@ -666,6 +769,19 @@ dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n")); LSCPResultSet result; try { + int n = AudioOutputDeviceFactory::AvailableDrivers().size(); + result.Add(n); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListAvailableAudioOutputDrivers() { + dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n")); + LSCPResultSet result; + try { String s = AudioOutputDeviceFactory::AvailableDriversAsString(); result.Add(s); } @@ -679,6 +795,19 @@ dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n")); LSCPResultSet result; try { + int n = MidiInputDeviceFactory::AvailableDrivers().size(); + result.Add(n); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListAvailableMidiInputDrivers() { + dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n")); + LSCPResultSet result; + try { String s = MidiInputDeviceFactory::AvailableDriversAsString(); result.Add(s); } @@ -737,7 +866,7 @@ } String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map DependencyList) { - dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str())); + dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size())); LSCPResultSet result; try { DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter); @@ -746,11 +875,16 @@ result.Add("MANDATORY", pParameter->Mandatory()); result.Add("FIX", pParameter->Fix()); result.Add("MULTIPLICITY", pParameter->Multiplicity()); - if (pParameter->Depends()) result.Add("DEPENDS", pParameter->Depends()); - if (pParameter->Default()) result.Add("DEFAULT", pParameter->Default()); - 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()); + optional oDepends = pParameter->Depends(); + optional oDefault = pParameter->Default(DependencyList); + optional oRangeMin = pParameter->RangeMin(DependencyList); + optional oRangeMax = pParameter->RangeMax(DependencyList); + optional oPossibilities = pParameter->Possibilities(DependencyList); + if (oDepends) result.Add("DEPENDS", *oDepends); + if (oDefault) result.Add("DEFAULT", *oDefault); + if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); + if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); + if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); } catch (LinuxSamplerException e) { result.Error(e); @@ -759,7 +893,7 @@ } String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map DependencyList) { - dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str())); + dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size())); LSCPResultSet result; try { DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter); @@ -768,11 +902,16 @@ result.Add("MANDATORY", pParameter->Mandatory()); result.Add("FIX", pParameter->Fix()); result.Add("MULTIPLICITY", pParameter->Multiplicity()); - if (pParameter->Depends()) result.Add("DEPENDS", pParameter->Depends()); - if (pParameter->Default()) result.Add("DEFAULT", pParameter->Default()); - 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()); + optional oDepends = pParameter->Depends(); + optional oDefault = pParameter->Default(DependencyList); + optional oRangeMin = pParameter->RangeMin(DependencyList); + optional oRangeMax = pParameter->RangeMax(DependencyList); + optional oPossibilities = pParameter->Possibilities(DependencyList); + if (oDepends) result.Add("DEPENDS", *oDepends); + if (oDefault) result.Add("DEFAULT", *oDefault); + if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); + if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); + if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); } catch (LinuxSamplerException e) { result.Error(e); @@ -849,9 +988,9 @@ LSCPResultSet result; try { std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); AudioOutputDevice* pDevice = devices[DeviceIndex]; - result.Add("driver", pDevice->Driver()); + result.Add("DRIVER", pDevice->Driver()); std::map parameters = pDevice->DeviceParameters(); std::map::iterator iter = parameters.begin(); for (; iter != parameters.end(); iter++) { @@ -869,9 +1008,9 @@ LSCPResultSet result; try { std::map devices = pSampler->GetMidiInputDevices(); + if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; - if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); - result.Add("driver", pDevice->Driver()); + result.Add("DRIVER", pDevice->Driver()); std::map parameters = pDevice->DeviceParameters(); std::map::iterator iter = parameters.begin(); for (; iter != parameters.end(); iter++) { @@ -887,13 +1026,18 @@ dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex)); LSCPResultSet result; try { + // get MIDI input device std::map devices = pSampler->GetMidiInputDevices(); + if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[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) + "."); - std::map parameters = pMidiInputPort->DeviceParameters(); - std::map::iterator iter = parameters.begin(); + + // get MIDI port + MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex); + if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + "."); + + // return the values of all MIDI port parameters + std::map parameters = pMidiInputPort->PortParameters(); + std::map::iterator iter = parameters.begin(); for (; iter != parameters.end(); iter++) { result.Add(iter->first, iter->second->Value()); } @@ -910,12 +1054,12 @@ try { // get audio output device std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); + if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); AudioOutputDevice* pDevice = devices[DeviceId]; // get audio channel AudioChannel* pChannel = pDevice->Channel(ChannelId); - if (!pChannel) throw LinuxSamplerException("Audio ouotput device does not have channel " + ToString(ChannelId) + "."); + if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // return the values of all audio channel parameters std::map parameters = pChannel->ChannelParameters(); @@ -934,28 +1078,28 @@ 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]; + // get MIDI input device + std::map devices = pSampler->GetMidiInputDevices(); + if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + "."); + MidiInputDevice* pDevice = devices[DeviceId]; + + // get midi port + 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->PortParameters(); + if (!parameters.count(ParameterName)) throw LinuxSamplerException("Midi port does not provide a parameter '" + ParameterName + "'."); + DeviceRuntimeParameter* 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()); + 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); @@ -969,16 +1113,16 @@ try { // get audio output device std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); + if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); AudioOutputDevice* pDevice = devices[DeviceId]; // get audio channel AudioChannel* pChannel = pDevice->Channel(ChannelId); - if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + "."); + if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // get desired audio channel parameter std::map parameters = pChannel->ChannelParameters(); - if (!parameters[ParameterName]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'."); + if (!parameters.count(ParameterName)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'."); DeviceRuntimeParameter* pParameter = parameters[ParameterName]; // return all fields of this audio channel parameter @@ -986,9 +1130,9 @@ 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()); + 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); @@ -1002,16 +1146,16 @@ try { // get audio output device std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); + if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); AudioOutputDevice* pDevice = devices[DeviceId]; // get audio channel AudioChannel* pChannel = pDevice->Channel(ChannelId); - if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + "."); + if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // get desired audio channel parameter std::map parameters = pChannel->ChannelParameters(); - if (!parameters[ParamKey]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'."); + if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'."); DeviceRuntimeParameter* pParameter = parameters[ParamKey]; // set new channel parameter value @@ -1028,10 +1172,10 @@ LSCPResultSet result; try { std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); AudioOutputDevice* pDevice = devices[DeviceIndex]; std::map parameters = pDevice->DeviceParameters(); - if (!parameters[ParamKey]) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'"); + if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'"); parameters[ParamKey]->SetValue(ParamVal); } catch (LinuxSamplerException e) { @@ -1045,10 +1189,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.count(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.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'"); parameters[ParamKey]->SetValue(ParamVal); } catch (LinuxSamplerException e) { @@ -1061,13 +1205,18 @@ dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str())); LSCPResultSet result; try { + // get MIDI input device std::map devices = pSampler->GetMidiInputDevices(); + if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[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) + "."); - std::map parameters = pMidiInputPort->DeviceParameters(); - if (!parameters[ParamKey]) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'"); + + // get MIDI port + MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex); + if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + "."); + + // set port parameter value + std::map parameters = pMidiInputPort->PortParameters(); + if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'"); parameters[ParamKey]->SetValue(ParamVal); } catch (LinuxSamplerException e) { @@ -1082,35 +1231,50 @@ */ String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) { dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel)); - return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class? + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel)); + if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel)); + pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); } String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) { dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel)); LSCPResultSet result; + LockRTNotify(); try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); std::map devices = pSampler->GetAudioOutputDevices(); + if (!devices.count(AudioDeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId)); 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); } + UnlockRTNotify(); 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; + LockRTNotify(); try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... - if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa"; - if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack"; + if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA"; + 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; @@ -1136,6 +1300,7 @@ catch (LinuxSamplerException e) { result.Error(e); } + UnlockRTNotify(); return result.Produce(); } @@ -1144,7 +1309,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); pSamplerChannel->SetMidiInputPort(MIDIPort); } catch (LinuxSamplerException e) { @@ -1158,8 +1323,8 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); - pSamplerChannel->SetMidiInputChannel((MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel); } catch (LinuxSamplerException e) { result.Error(e); @@ -1172,10 +1337,10 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); std::map devices = pSampler->GetMidiInputDevices(); + if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); MidiInputDevice* pDevice = devices[MIDIDeviceId]; - if (!pDevice) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); pSamplerChannel->SetMidiInputDevice(pDevice); } catch (LinuxSamplerException e) { @@ -1189,9 +1354,9 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... - if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa"; + 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; @@ -1209,7 +1374,7 @@ pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params); // Make it with at least one initial port. std::map parameters = pDevice->DeviceParameters(); - parameters["ports"]->SetValue("1"); + parameters["PORTS"]->SetValue("1"); } // Must have a device... if (pDevice == NULL) @@ -1232,11 +1397,11 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); std::map devices = pSampler->GetMidiInputDevices(); + if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); 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); + pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel); } catch (LinuxSamplerException e) { result.Error(e); @@ -1248,15 +1413,15 @@ * Will be called by the parser to change the global volume factor on a * particular sampler channel. */ -String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) { - dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel)); +String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, 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); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + pEngineChannel->Volume(dVolume); } catch (LinuxSamplerException e) { result.Error(e); @@ -1265,6 +1430,107 @@ } /** + * Will be called by the parser to mute/unmute particular sampler channel. + */ +String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + + if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); + else pEngineChannel->SetMute(1); + } catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + +/** + * Will be called by the parser to solo particular sampler channel. + */ +String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) { + dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel)); + LSCPResultSet result; + try { + SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + + bool oldSolo = pEngineChannel->GetSolo(); + bool hadSoloChannel = HasSoloChannel(); + + pEngineChannel->SetSolo(bSolo); + + if(!oldSolo && bSolo) { + if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0); + if(!hadSoloChannel) MuteNonSoloChannels(); + } + + if(oldSolo && !bSolo) { + if(!HasSoloChannel()) UnmuteChannels(); + else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1); + } + } catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} + +/** + * Determines whether there is at least one solo channel in the channel list. + * + * @returns true if there is at least one solo channel in the channel list, + * false otherwise. + */ +bool LSCPServer::HasSoloChannel() { + std::map channels = pSampler->GetSamplerChannels(); + std::map::iterator iter = channels.begin(); + for (; iter != channels.end(); iter++) { + EngineChannel* c = iter->second->GetEngineChannel(); + if(c && c->GetSolo()) return true; + } + + return false; +} + +/** + * Mutes all unmuted non-solo channels. Notice that the channels are muted + * with -1 which indicates that they are muted because of the presence + * of a solo channel(s). Channels muted with -1 will be automatically unmuted + * when there are no solo channels left. + */ +void LSCPServer::MuteNonSoloChannels() { + dmsg(2,("LSCPServer: MuteNonSoloChannels()\n")); + std::map channels = pSampler->GetSamplerChannels(); + std::map::iterator iter = channels.begin(); + for (; iter != channels.end(); iter++) { + EngineChannel* c = iter->second->GetEngineChannel(); + if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1); + } +} + +/** + * Unmutes all channels that are muted because of the presence + * of a solo channel(s). + */ +void LSCPServer::UnmuteChannels() { + dmsg(2,("LSCPServer: UnmuteChannels()\n")); + std::map channels = pSampler->GetSamplerChannels(); + std::map::iterator iter = channels.begin(); + for (; iter != channels.end(); iter++) { + EngineChannel* c = iter->second->GetEngineChannel(); + if(c && c->GetMute() == -1) c->SetMute(0); + } +} + +/** * Will be called by the parser to reset a particular sampler channel. */ String LSCPServer::ResetChannel(uint uiSamplerChannel) { @@ -1272,10 +1538,10 @@ 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(); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + pEngineChannel->Reset(); } catch (LinuxSamplerException e) { result.Error(e); @@ -1284,6 +1550,49 @@ } /** + * Will be called by the parser to reset the whole sampler. + */ +String LSCPServer::ResetSampler() { + dmsg(2,("LSCPServer: ResetSampler()\n")); + pSampler->Reset(); + LSCPResultSet result; + return result.Produce(); +} + +/** + * Will be called by the parser to return general informations about this + * sampler. + */ +String LSCPServer::GetServerInfo() { + dmsg(2,("LSCPServer: GetServerInfo()\n")); + LSCPResultSet result; + result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); + result.Add("VERSION", VERSION); + result.Add("PROTOCOL_VERSION", "1.0"); + return result.Produce(); +} + +/** + * Will be called by the parser to return the current number of all active voices. + */ +String LSCPServer::GetTotalVoiceCount() { + dmsg(2,("LSCPServer: GetTotalVoiceCount()\n")); + LSCPResultSet result; + result.Add(pSampler->GetVoiceCount()); + return result.Produce(); +} + +/** + * Will be called by the parser to return the maximum number of voices. + */ +String LSCPServer::GetTotalVoiceCountMax() { + dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n")); + LSCPResultSet result; + result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES); + return result.Produce(); +} + +/** * Will be called by the parser to subscribe a client (frontend) on the * server for receiving event messages. */ @@ -1309,6 +1618,37 @@ return result.Produce(); } +static int select_callback(void * lscpResultSet, int argc, + char **argv, char **azColName) +{ + LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet; + resultSet->Add(argc, argv); + return 0; +} + +String LSCPServer::QueryDatabase(String query) { + LSCPResultSet result; +#if HAVE_SQLITE3 + char* zErrMsg = NULL; + sqlite3 *db; + String selectStr = "SELECT " + query; + + int rc = sqlite3_open("linuxsampler.db", &db); + if (rc == SQLITE_OK) + { + rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg); + } + if ( rc != SQLITE_OK ) + { + result.Error(String(zErrMsg), rc); + } + sqlite3_close(db); +#else + result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0); +#endif + return result.Produce(); +} + /** * Will be called by the parser to enable or disable echo mode; if echo * mode is enabled, all commands from the client will (immediately) be @@ -1327,30 +1667,3 @@ } return result.Produce(); } - -// Instrument loader constructor. -LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument) - : Thread(false, 0, -4) -{ - this->pEngine = pEngine; - this->Filename = Filename; - this->uiInstrument = uiInstrument; -} - -// Instrument loader process. -int LSCPLoadInstrument::Main() -{ - try { - pEngine->LoadInstrument(Filename.c_str(), uiInstrument); - } - - catch (LinuxSamplerException e) { - e.PrintMessage(); - } - - // Always re-enable the engine. - pEngine->Enable(); - - // FIXME: Shoot ourselves on the foot? - delete this; -}