--- linuxsampler/trunk/src/network/lscpserver.cpp 2008/09/03 17:18:51 1763 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2011/07/03 18:06:51 2198 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2008 Christian Schoenebeck * + * Copyright (C) 2005 - 2010 Christian Schoenebeck * * * * 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 * @@ -22,7 +22,9 @@ ***************************************************************************/ #include +#include +#include "../common/File.h" #include "lscpserver.h" #include "lscpresultset.h" #include "lscpevent.h" @@ -41,7 +43,9 @@ #include "../engines/EngineChannelFactory.h" #include "../drivers/audio/AudioOutputDeviceFactory.h" #include "../drivers/midi/MidiInputDeviceFactory.h" +#include "../effects/EffectFactory.h" +namespace LinuxSampler { /** * Returns a copy of the given string where all special characters are @@ -134,10 +138,16 @@ LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI"); LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI"); + LSCPEvent::RegisterEvent(LSCPEvent::event_fx_instance_count, "EFFECT_INSTANCE_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_fx_instance_info, "EFFECT_INSTANCE_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_send_fx_chain_count, "SEND_EFFECT_CHAIN_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_send_fx_chain_info, "SEND_EFFECT_CHAIN_INFO"); hSocket = -1; } LSCPServer::~LSCPServer() { + CloseAllConnections(); + InstrumentManager::StopBackgroundThread(); #if defined(WIN32) if (hSocket >= 0) closesocket(hSocket); #else @@ -332,6 +342,24 @@ } #endif // HAVE_SQLITE3 +void LSCPServer::RemoveListeners() { + pSampler->RemoveChannelCountListener(&eventHandler); + pSampler->RemoveAudioDeviceCountListener(&eventHandler); + pSampler->RemoveMidiDeviceCountListener(&eventHandler); + pSampler->RemoveVoiceCountListener(&eventHandler); + pSampler->RemoveStreamCountListener(&eventHandler); + pSampler->RemoveBufferFillListener(&eventHandler); + pSampler->RemoveTotalStreamCountListener(&eventHandler); + pSampler->RemoveTotalVoiceCountListener(&eventHandler); + pSampler->RemoveFxSendCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler); +#if HAVE_SQLITE3 + InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler); +#endif +} /** * Blocks the calling thread until the LSCP Server is initialized and @@ -505,7 +533,7 @@ int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout); - if (retval == 0) + if (retval == 0 || (retval == -1 && errno == EINTR)) continue; //Nothing try again if (retval == -1) { std::cerr << "LSCPServer: Socket select error." << std::endl; @@ -532,6 +560,13 @@ exit(EXIT_FAILURE); } #else + struct linger linger; + linger.l_onoff = 1; + linger.l_linger = 0; + if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) { + std::cerr << "LSCPServer: Failed to set SO_LINGER\n"; + } + if (fcntl(socket, F_SETFL, O_NONBLOCK)) { std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; exit(EXIT_FAILURE); @@ -603,6 +638,14 @@ NotifyMutex.Unlock(); } +void LSCPServer::CloseAllConnections() { + std::vector::iterator iter = Sessions.begin(); + while(iter != Sessions.end()) { + CloseConnection(iter); + iter = Sessions.begin(); + } +} + void LSCPServer::LockRTNotify() { RTNotifyMutex.Lock(); } @@ -1133,10 +1176,7 @@ dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine loaded on sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->VoiceCount()); } @@ -1154,10 +1194,7 @@ dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); } @@ -1175,10 +1212,7 @@ dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); else { @@ -1266,6 +1300,7 @@ for (;iter != parameters.end(); iter++) { if (s != "") s += ","; s += iter->first; + delete iter->second; } result.Add("PARAMETERS", s); } @@ -1290,6 +1325,7 @@ for (;iter != parameters.end(); iter++) { if (s != "") s += ","; s += iter->first; + delete iter->second; } result.Add("PARAMETERS", s); } @@ -1320,6 +1356,7 @@ if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); + delete pParameter; } catch (Exception e) { result.Error(e); @@ -1347,6 +1384,7 @@ if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); + delete pParameter; } catch (Exception e) { result.Error(e); @@ -1813,7 +1851,6 @@ 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) @@ -1856,10 +1893,7 @@ dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); pEngineChannel->Volume(dVolume); } catch (Exception e) { @@ -1875,11 +1909,7 @@ dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); else pEngineChannel->SetMute(1); @@ -1896,11 +1926,7 @@ dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); bool oldSolo = pEngineChannel->GetSolo(); bool hadSoloChannel = HasSoloChannel(); @@ -2253,11 +2279,7 @@ dmsg(2,("LSCPServer: SetChannelMap()\n")); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone(); else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault(); @@ -2365,11 +2387,17 @@ AudioRouting += ToString(pFxSend->DestinationChannel(chan)); } + const String sEffectRouting = + (pFxSend->DestinationEffectChain() >= 0 && pFxSend->DestinationEffectChainPosition() >= 0) + ? ToString(pFxSend->DestinationEffectChain()) + "," + ToString(pFxSend->DestinationEffectChainPosition()) + : "NONE"; + // success result.Add("NAME", _escapeLscpResponse(pFxSend->Name())); result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); result.Add("LEVEL", ToString(pFxSend->Level())); result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); + result.Add("EFFECT", sEffectRouting); } catch (Exception e) { result.Error(e); } @@ -2432,14 +2460,478 @@ return result.Produce(); } +String LSCPServer::SetFxSendEffect(uint uiSamplerChannel, uint FxSendID, int iSendEffectChain, int iEffectChainPosition) { + dmsg(2,("LSCPServer: SetFxSendEffect(%d,%d)\n", iSendEffectChain, iEffectChainPosition)); + LSCPResultSet result; + try { + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); + + pFxSend->SetDestinationEffect(iSendEffectChain, iEffectChainPosition); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetAvailableEffects() { + dmsg(2,("LSCPServer: GetAvailableEffects()\n")); + LSCPResultSet result; + try { + int n = EffectFactory::AvailableEffectsCount(); + result.Add(n); + } + catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListAvailableEffects() { + dmsg(2,("LSCPServer: ListAvailableEffects()\n")); + LSCPResultSet result; + String list; + try { + //FIXME: for now we simply enumerate from 0 .. EffectFactory::AvailableEffectsCount() here, in future we should use unique IDs for effects during the whole sampler session. This issue comes into game when the user forces a reload of available effect plugins + int n = EffectFactory::AvailableEffectsCount(); + for (int i = 0; i < n; i++) { + if (i) list += ","; + list += ToString(i); + } + } + catch (Exception e) { + result.Error(e); + } + result.Add(list); + return result.Produce(); +} + +String LSCPServer::GetEffectInfo(int iEffectIndex) { + dmsg(2,("LSCPServer: GetEffectInfo(%d)\n", iEffectIndex)); + LSCPResultSet result; + try { + EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex); + if (!pEffectInfo) + throw Exception("There is no effect with index " + ToString(iEffectIndex)); + + // convert the filename into the correct encoding as defined for LSCP + // (especially in terms of special characters -> escape sequences) +#if WIN32 + const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp(); +#else + // assuming POSIX + const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp(); +#endif + + result.Add("SYSTEM", pEffectInfo->EffectSystem()); + result.Add("MODULE", dllFileName); + result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name())); + result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description())); + } + catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetEffectInstanceInfo(int iEffectInstance) { + dmsg(2,("LSCPServer: GetEffectInstanceInfo(%d)\n", iEffectInstance)); + LSCPResultSet result; + try { + Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance); + if (!pEffect) + throw Exception("There is no effect instance with ID " + ToString(iEffectInstance)); + + EffectInfo* pEffectInfo = pEffect->GetEffectInfo(); + + // convert the filename into the correct encoding as defined for LSCP + // (especially in terms of special characters -> escape sequences) +#if WIN32 + const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp(); +#else + // assuming POSIX + const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp(); +#endif + + result.Add("SYSTEM", pEffectInfo->EffectSystem()); + result.Add("MODULE", dllFileName); + result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name())); + result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description())); + result.Add("INPUT_CONTROLS", ToString(pEffect->InputControlCount())); + } + catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetEffectInstanceInputControlInfo(int iEffectInstance, int iInputControlIndex) { + dmsg(2,("LSCPServer: GetEffectInstanceInputControlInfo(%d,%d)\n", iEffectInstance, iInputControlIndex)); + LSCPResultSet result; + try { + Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance); + if (!pEffect) + throw Exception("There is no effect instance with ID " + ToString(iEffectInstance)); + + EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex); + if (!pEffectControl) + throw Exception( + "Effect instance " + ToString(iEffectInstance) + + " does not have an input control with index " + + ToString(iInputControlIndex) + ); + + result.Add("DESCRIPTION", _escapeLscpResponse(pEffectControl->Description())); + result.Add("VALUE", pEffectControl->Value()); + if (pEffectControl->MinValue()) + result.Add("RANGE_MIN", *pEffectControl->MinValue()); + if (pEffectControl->MaxValue()) + result.Add("RANGE_MAX", *pEffectControl->MaxValue()); + if (!pEffectControl->Possibilities().empty()) + result.Add("POSSIBILITIES", pEffectControl->Possibilities()); + if (pEffectControl->DefaultValue()) + result.Add("DEFAULT", *pEffectControl->DefaultValue()); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::SetEffectInstanceInputControlValue(int iEffectInstance, int iInputControlIndex, double dValue) { + dmsg(2,("LSCPServer: SetEffectInstanceInputControlValue(%d,%d,%f)\n", iEffectInstance, iInputControlIndex, dValue)); + LSCPResultSet result; + try { + Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance); + if (!pEffect) + throw Exception("There is no effect instance with ID " + ToString(iEffectInstance)); + + EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex); + if (!pEffectControl) + throw Exception( + "Effect instance " + ToString(iEffectInstance) + + " does not have an input control with index " + + ToString(iInputControlIndex) + ); + + pEffectControl->SetValue(dValue); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_info, iEffectInstance)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::CreateEffectInstance(int iEffectIndex) { + dmsg(2,("LSCPServer: CreateEffectInstance(%d)\n", iEffectIndex)); + LSCPResultSet result; + try { + EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex); + if (!pEffectInfo) + throw Exception("There is no effect with index " + ToString(iEffectIndex)); + Effect* pEffect = EffectFactory::Create(pEffectInfo); + result = pEffect->ID(); // success + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount())); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::CreateEffectInstance(String effectSystem, String module, String effectName) { + dmsg(2,("LSCPServer: CreateEffectInstance('%s','%s','%s')\n", effectSystem.c_str(), module.c_str(), effectName.c_str())); + LSCPResultSet result; + try { + // to allow loading the same LSCP session file on different systems + // successfully, probably with different effect plugin DLL paths or even + // running completely different operating systems, we do the following + // for finding the right effect: + // + // first try to search for an exact match of the effect plugin DLL + // (a.k.a 'module'), to avoid picking the wrong DLL with the same + // effect name ... + EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_MATCH_EXACTLY); + // ... if no effect with exactly matchin DLL filename was found, then + // try to lower the restrictions of matching the effect plugin DLL + // filename and try again and again ... + if (!pEffectInfo) { + dmsg(2,("no exact module match, trying MODULE_IGNORE_PATH\n")); + pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH); + } + if (!pEffectInfo) { + dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE\n")); + pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE); + } + if (!pEffectInfo) { + dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE | MODULE_IGNORE_EXTENSION\n")); + pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE | EffectFactory::MODULE_IGNORE_EXTENSION); + } + // ... if there was still no effect found, then completely ignore the + // DLL plugin filename argument and just search for the matching effect + // system type and effect name + if (!pEffectInfo) { + dmsg(2,("no module match, trying MODULE_IGNORE_ALL\n")); + pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_ALL); + } + if (!pEffectInfo) + throw Exception("There is no such effect '" + effectSystem + "' '" + module + "' '" + effectName + "'"); + + Effect* pEffect = EffectFactory::Create(pEffectInfo); + result = LSCPResultSet(pEffect->ID()); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount())); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::DestroyEffectInstance(int iEffectInstance) { + dmsg(2,("LSCPServer: DestroyEffectInstance(%d)\n", iEffectInstance)); + LSCPResultSet result; + try { + Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance); + if (!pEffect) + throw Exception("There is no effect instance with ID " + ToString(iEffectInstance)); + EffectFactory::Destroy(pEffect); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount())); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetEffectInstances() { + dmsg(2,("LSCPServer: GetEffectInstances()\n")); + LSCPResultSet result; + try { + int n = EffectFactory::EffectInstancesCount(); + result.Add(n); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListEffectInstances() { + dmsg(2,("LSCPServer: ListEffectInstances()\n")); + LSCPResultSet result; + String list; + try { + int n = EffectFactory::EffectInstancesCount(); + for (int i = 0; i < n; i++) { + Effect* pEffect = EffectFactory::GetEffectInstance(i); + if (i) list += ","; + list += ToString(pEffect->ID()); + } + } catch (Exception e) { + result.Error(e); + } + result.Add(list); + return result.Produce(); +} + +String LSCPServer::GetSendEffectChains(int iAudioOutputDevice) { + dmsg(2,("LSCPServer: GetSendEffectChains(%d)\n", iAudioOutputDevice)); + LSCPResultSet result; + try { + std::map devices = pSampler->GetAudioOutputDevices(); + if (!devices.count(iAudioOutputDevice)) + throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + "."); + AudioOutputDevice* pDevice = devices[iAudioOutputDevice]; + int n = pDevice->SendEffectChainCount(); + result.Add(n); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListSendEffectChains(int iAudioOutputDevice) { + dmsg(2,("LSCPServer: ListSendEffectChains(%d)\n", iAudioOutputDevice)); + LSCPResultSet result; + String list; + try { + std::map devices = pSampler->GetAudioOutputDevices(); + if (!devices.count(iAudioOutputDevice)) + throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + "."); + AudioOutputDevice* pDevice = devices[iAudioOutputDevice]; + int n = pDevice->SendEffectChainCount(); + for (int i = 0; i < n; i++) { + EffectChain* pEffectChain = pDevice->SendEffectChain(i); + if (i) list += ","; + list += ToString(pEffectChain->ID()); + } + } catch (Exception e) { + result.Error(e); + } + result.Add(list); + return result.Produce(); +} + +String LSCPServer::AddSendEffectChain(int iAudioOutputDevice) { + dmsg(2,("LSCPServer: AddSendEffectChain(%d)\n", iAudioOutputDevice)); + LSCPResultSet result; + try { + std::map devices = pSampler->GetAudioOutputDevices(); + if (!devices.count(iAudioOutputDevice)) + throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + "."); + AudioOutputDevice* pDevice = devices[iAudioOutputDevice]; + EffectChain* pEffectChain = pDevice->AddSendEffectChain(); + result = pEffectChain->ID(); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_count, iAudioOutputDevice, pDevice->SendEffectChainCount())); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::RemoveSendEffectChain(int iAudioOutputDevice, int iSendEffectChain) { + dmsg(2,("LSCPServer: RemoveSendEffectChain(%d,%d)\n", iAudioOutputDevice, iSendEffectChain)); + LSCPResultSet result; + try { + std::map devices = pSampler->GetAudioOutputDevices(); + if (!devices.count(iAudioOutputDevice)) + throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + "."); + + std::set engineChannels = EngineChannelFactory::EngineChannelInstances(); + std::set::iterator itEngineChannel = engineChannels.begin(); + std::set::iterator itEnd = engineChannels.end(); + for (; itEngineChannel != itEnd; ++itEngineChannel) { + AudioOutputDevice* pDev = (*itEngineChannel)->GetAudioOutputDevice(); + if (pDev != NULL && pDev->deviceId() == iAudioOutputDevice) { + for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { + FxSend* fxs = (*itEngineChannel)->GetFxSend(i); + if(fxs != NULL && fxs->DestinationEffectChain() == iSendEffectChain) { + throw Exception("The effect chain is still in use by channel " + ToString((*itEngineChannel)->GetSamplerChannel()->Index())); + } + } + } + } + + AudioOutputDevice* pDevice = devices[iAudioOutputDevice]; + for (int i = 0; i < pDevice->SendEffectChainCount(); i++) { + EffectChain* pEffectChain = pDevice->SendEffectChain(i); + if (pEffectChain->ID() == iSendEffectChain) { + pDevice->RemoveSendEffectChain(i); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_count, iAudioOutputDevice, pDevice->SendEffectChainCount())); + return result.Produce(); + } + } + throw Exception( + "There is no send effect chain with ID " + + ToString(iSendEffectChain) + " for audio output device " + + ToString(iAudioOutputDevice) + "." + ); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +static EffectChain* _getSendEffectChain(Sampler* pSampler, int iAudioOutputDevice, int iSendEffectChain) throw (Exception) { + std::map devices = pSampler->GetAudioOutputDevices(); + if (!devices.count(iAudioOutputDevice)) + throw Exception( + "There is no audio output device with index " + + ToString(iAudioOutputDevice) + "." + ); + AudioOutputDevice* pDevice = devices[iAudioOutputDevice]; + EffectChain* pEffectChain = pDevice->SendEffectChainByID(iSendEffectChain); + if(pEffectChain != NULL) return pEffectChain; + throw Exception( + "There is no send effect chain with ID " + + ToString(iSendEffectChain) + " for audio output device " + + ToString(iAudioOutputDevice) + "." + ); +} + +String LSCPServer::GetSendEffectChainInfo(int iAudioOutputDevice, int iSendEffectChain) { + dmsg(2,("LSCPServer: GetSendEffectChainInfo(%d,%d)\n", iAudioOutputDevice, iSendEffectChain)); + LSCPResultSet result; + try { + EffectChain* pEffectChain = + _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain); + String sEffectSequence; + for (int i = 0; i < pEffectChain->EffectCount(); i++) { + if (i) sEffectSequence += ","; + sEffectSequence += ToString(pEffectChain->GetEffect(i)->ID()); + } + result.Add("EFFECT_COUNT", pEffectChain->EffectCount()); + result.Add("EFFECT_SEQUENCE", sEffectSequence); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::AppendSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectInstance) { + dmsg(2,("LSCPServer: AppendSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectInstance)); + LSCPResultSet result; + try { + EffectChain* pEffectChain = + _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain); + Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance); + if (!pEffect) + throw Exception("There is no effect instance with ID " + ToString(iEffectInstance)); + pEffectChain->AppendEffect(pEffect); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount())); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::InsertSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition, int iEffectInstance) { + dmsg(2,("LSCPServer: InsertSendEffectChainEffect(%d,%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition, iEffectInstance)); + LSCPResultSet result; + try { + EffectChain* pEffectChain = + _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain); + Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance); + if (!pEffect) + throw Exception("There is no effect instance with index " + ToString(iEffectInstance)); + pEffectChain->InsertEffect(pEffect, iEffectChainPosition); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount())); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::RemoveSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition) { + dmsg(2,("LSCPServer: RemoveSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition)); + LSCPResultSet result; + try { + EffectChain* pEffectChain = + _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain); + + std::set engineChannels = EngineChannelFactory::EngineChannelInstances(); + std::set::iterator itEngineChannel = engineChannels.begin(); + std::set::iterator itEnd = engineChannels.end(); + for (; itEngineChannel != itEnd; ++itEngineChannel) { + AudioOutputDevice* pDev = (*itEngineChannel)->GetAudioOutputDevice(); + if (pDev != NULL && pDev->deviceId() == iAudioOutputDevice) { + for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { + FxSend* fxs = (*itEngineChannel)->GetFxSend(i); + if(fxs != NULL && fxs->DestinationEffectChain() == iSendEffectChain && fxs->DestinationEffectChainPosition() == iEffectChainPosition) { + throw Exception("The effect instance is still in use by channel " + ToString((*itEngineChannel)->GetSamplerChannel()->Index())); + } + } + } + } + + pEffectChain->RemoveEffect(iEffectChainPosition); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount())); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) { dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel"); Engine* pEngine = pEngineChannel->GetEngine(); InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager(); @@ -2454,6 +2946,48 @@ return result.Produce(); } +String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) { + dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2)); + LSCPResultSet result; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + if (Arg1 > 127 || Arg2 > 127) { + throw Exception("Invalid MIDI message"); + } + + VirtualMidiDevice* pMidiDevice = NULL; + std::vector::iterator iter = eventHandler.channelMidiListeners.begin(); + for (; iter != eventHandler.channelMidiListeners.end(); ++iter) { + if ((*iter).pEngineChannel == pEngineChannel) { + pMidiDevice = (*iter).pMidiListener; + break; + } + } + + if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device"); + + if (MidiMsg == "NOTE_ON") { + pMidiDevice->SendNoteOnToDevice(Arg1, Arg2); + bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2); + if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); + } else if (MidiMsg == "NOTE_OFF") { + pMidiDevice->SendNoteOffToDevice(Arg1, Arg2); + bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2); + if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); + } else if (MidiMsg == "CC") { + pMidiDevice->SendCCToDevice(Arg1, Arg2); + bool b = pMidiDevice->SendCCToSampler(Arg1, Arg2); + if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); + } else { + throw Exception("Unknown MIDI message type: " + MidiMsg); + } + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + /** * Will be called by the parser to reset a particular sampler channel. */ @@ -2461,10 +2995,7 @@ dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); pEngineChannel->Reset(); } catch (Exception e) { @@ -2530,7 +3061,79 @@ String LSCPServer::GetTotalVoiceCountMax() { dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n")); LSCPResultSet result; - result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES); + result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES); + return result.Produce(); +} + +/** + * Will be called by the parser to return the sampler global maximum + * allowed number of voices. + */ +String LSCPServer::GetGlobalMaxVoices() { + dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n")); + LSCPResultSet result; + result.Add(GLOBAL_MAX_VOICES); + return result.Produce(); +} + +/** + * Will be called by the parser to set the sampler global maximum number of + * voices. + */ +String LSCPServer::SetGlobalMaxVoices(int iVoices) { + dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices)); + LSCPResultSet result; + try { + if (iVoices < 1) throw Exception("Maximum voices may not be less than 1"); + GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.size() > 0) { + std::set::iterator iter = engines.begin(); + std::set::iterator end = engines.end(); + for (; iter != end; ++iter) { + (*iter)->SetMaxVoices(iVoices); + } + } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +/** + * Will be called by the parser to return the sampler global maximum + * allowed number of disk streams. + */ +String LSCPServer::GetGlobalMaxStreams() { + dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n")); + LSCPResultSet result; + result.Add(GLOBAL_MAX_STREAMS); + return result.Produce(); +} + +/** + * Will be called by the parser to set the sampler global maximum number of + * disk streams. + */ +String LSCPServer::SetGlobalMaxStreams(int iStreams) { + dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams)); + LSCPResultSet result; + try { + if (iStreams < 0) throw Exception("Maximum disk streams may not be negative"); + GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.size() > 0) { + std::set::iterator iter = engines.begin(); + std::set::iterator end = engines.end(); + for (; iter != end; ++iter) { + (*iter)->SetMaxDiskStreams(iStreams); + } + } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS)); + } catch (Exception e) { + result.Error(e); + } return result.Produce(); } @@ -2671,6 +3274,26 @@ result.Add("FORMAT_VERSION", info.FormatVersion); result.Add("PRODUCT", info.Product); result.Add("ARTISTS", info.Artists); + + std::stringstream ss; + bool b = false; + for (int i = 0; i < 128; i++) { + if (info.KeyBindings[i]) { + if (b) ss << ','; + ss << i; b = true; + } + } + result.Add("KEY_BINDINGS", ss.str()); + + b = false; + std::stringstream ss2; + for (int i = 0; i < 128; i++) { + if (info.KeySwitchBindings[i]) { + if (b) ss2 << ','; + ss2 << i; b = true; + } + } + result.Add("KEYSWITCH_BINDINGS", ss2.str()); // no more need to ask other engine types bFound = true; } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); @@ -2698,17 +3321,9 @@ throw Exception("Directory is specified"); } #else - struct stat statBuf; - int res = stat(Filename.c_str(), &statBuf); - if (res) { - std::stringstream ss; - ss << "Fail to stat `" << Filename << "`: " << strerror(errno); - throw Exception(ss.str()); - } - - if (S_ISDIR(statBuf.st_mode)) { - throw Exception("Directory is specified"); - } + File f(Filename); + if(!f.Exist()) throw Exception(f.GetErrorMsg()); + if (f.IsDirectory()) throw Exception("Directory is specified"); #endif } @@ -2903,19 +3518,19 @@ return result.Produce(); } -String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) { - dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground)); +String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) { + dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); LSCPResultSet result; #if HAVE_SQLITE3 try { int id; InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); if (ScanMode.compare("RECURSIVE") == 0) { - id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground); + id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir); } else if (ScanMode.compare("NON_RECURSIVE") == 0) { - id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground); + id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir); } else if (ScanMode.compare("FLAT") == 0) { - id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground); + id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir); } else { throw Exception("Unknown scan mode: " + ScanMode); } @@ -3259,3 +3874,5 @@ } return result.Produce(); } + +}