--- linuxsampler/trunk/src/network/lscpserver.cpp 2005/07/21 08:38:15 707 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2007/05/16 14:22:26 1187 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 Christian Schoenebeck * + * Copyright (C) 2005 - 2007 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 * @@ -24,12 +24,11 @@ #include "lscpserver.h" #include "lscpresultset.h" #include "lscpevent.h" -//#include "../common/global.h" #include -#if HAVE_SQLITE3 -# include "sqlite3.h" +#if ! HAVE_SQLITE3 +#define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built." #endif #include "../engines/EngineFactory.h" @@ -66,12 +65,28 @@ SocketAddress.sin_addr.s_addr = addr; SocketAddress.sin_port = port; this->pSampler = pSampler; + LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_count, "AUDIO_OUTPUT_DEVICE_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_info, "AUDIO_OUTPUT_DEVICE_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_count, "MIDI_INPUT_DEVICE_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_info, "MIDI_INPUT_DEVICE_INFO"); 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_channel_info, "CHANNEL_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO"); + LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); + LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); + LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); hSocket = -1; } @@ -79,6 +94,84 @@ if (hSocket >= 0) close(hSocket); } +void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); +} + +void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); +} + +void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); +} + +void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); +} + +void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program)); +} + +void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount)); +} + +void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId)); +} + +void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount)); +} + +void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount)); +} + +void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount)); +} + +void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData)); +} + +void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); +} + +#if HAVE_SQLITE3 +void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, Dir)); +} + +void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, Dir)); +} + +void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { + Dir = "'" + Dir + "'"; + NewName = "'" + NewName + "'"; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName)); +} + +void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, Dir)); +} + +void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, Instr)); +} +void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { + Instr = "'" + Instr + "'"; + NewName = "'" + NewName + "'"; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); +} +#endif // HAVE_SQLITE3 + + /** * Blocks the calling thread until the LSCP Server is initialized and * accepting socket connections, if the server is already initialized then @@ -119,7 +212,23 @@ listen(hSocket, 1); Initialized.Set(true); - + + // Registering event listeners + pSampler->AddChannelCountListener(&eventHandler); + pSampler->AddAudioDeviceCountListener(&eventHandler); + pSampler->AddMidiDeviceCountListener(&eventHandler); + pSampler->AddVoiceCountListener(&eventHandler); + pSampler->AddStreamCountListener(&eventHandler); + pSampler->AddBufferFillListener(&eventHandler); + pSampler->AddTotalVoiceCountListener(&eventHandler); + pSampler->AddFxSendCountListener(&eventHandler); + MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); + MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler); + MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler); + MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler); +#if HAVE_SQLITE3 + InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler); +#endif // now wait for client connections and handle their requests sockaddr_in client; int length = sizeof(client); @@ -127,9 +236,48 @@ 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)); + } + + for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { + FxSend* fxs = (*itEngineChannel)->GetFxSend(i); + if(fxs != NULL && fxs->IsInfoChanged()) { + int chn = (*itEngineChannel)->iSamplerChannelIndex; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id())); + fxs->SetInfoChanged(false); + } + } + } + } + + //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) { @@ -189,26 +337,6 @@ break; } } - - // 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++) { - send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL); - bufferedNotifies.erase(iterNotify); - } - NotifyBufferMutex.Unlock(); } } @@ -255,7 +383,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 { @@ -307,7 +439,7 @@ continue; //Ignore CR if (c == '\n') { LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket)); - bufferedCommands[socket] += "\n"; + bufferedCommands[socket] += "\r\n"; return true; //Complete command was read } bufferedCommands[socket] += c; @@ -364,7 +496,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(); } } @@ -408,10 +544,10 @@ AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters); // search for the created device to get its index int index = GetAudioOutputDeviceIndex(pDevice); - if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device."); + if (index == -1) throw Exception("Internal error: could not find created audio output device."); result = index; // success } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -424,10 +560,10 @@ MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters); // search for the created device to get its index int index = GetMidiInputDeviceIndex(pDevice); - if (index == -1) throw LinuxSamplerException("Internal error: could not find created midi input device."); + if (index == -1) throw Exception("Internal error: could not find created midi input device."); result = index; // success } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -438,11 +574,11 @@ LSCPResultSet result; try { std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + "."); AudioOutputDevice* pDevice = devices[DeviceIndex]; pSampler->DestroyAudioOutputDevice(pDevice); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -453,16 +589,26 @@ LSCPResultSet result; try { std::map devices = pSampler->GetMidiInputDevices(); - if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; pSampler->DestroyMidiInputDevice(pDevice); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); } +EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) { + 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"); + + return pEngineChannel; +} + /** * Will be called by the parser to load an instrument. */ @@ -471,13 +617,16 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet"); + if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel yet"); if (!pSamplerChannel->GetAudioOutputDevice()) - throw LinuxSamplerException("No audio output device connected to sampler channel"); + throw Exception("No audio output device connected to sampler channel"); if (bBackground) { - InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel); + InstrumentManager::instrument_id_t id; + id.FileName = Filename; + id.Index = uiInstrument; + InstrumentManager::LoadInstrumentInBackground(id, pEngineChannel); } else { // tell the engine channel which instrument to load @@ -486,7 +635,7 @@ pEngineChannel->LoadInstrument(); } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -501,13 +650,13 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); LockRTNotify(); pSamplerChannel->SetEngineType(EngineName); if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1); UnlockRTNotify(); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -545,7 +694,9 @@ */ String LSCPServer::AddChannel() { dmsg(2,("LSCPServer: AddChannel()\n")); + LockRTNotify(); SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel(); + UnlockRTNotify(); LSCPResultSet result(pSamplerChannel->Index()); return result.Produce(); } @@ -567,7 +718,14 @@ */ String LSCPServer::GetAvailableEngines() { dmsg(2,("LSCPServer: GetAvailableEngines()\n")); - LSCPResultSet result("1"); + LSCPResultSet result; + try { + int n = EngineFactory::AvailableEngineTypes().size(); + result.Add(n); + } + catch (Exception e) { + result.Error(e); + } return result.Produce(); } @@ -576,7 +734,14 @@ */ String LSCPServer::ListAvailableEngines() { dmsg(2,("LSCPServer: ListAvailableEngines()\n")); - LSCPResultSet result("\'GIG\'"); + LSCPResultSet result; + try { + String s = EngineFactory::AvailableEngineTypesAsString(); + result.Add(s); + } + catch (Exception e) { + result.Error(e); + } return result.Produce(); } @@ -587,15 +752,17 @@ String LSCPServer::GetEngineInfo(String EngineName) { dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str())); LSCPResultSet result; + LockRTNotify(); try { Engine* pEngine = EngineFactory::Create(EngineName); result.Add("DESCRIPTION", pEngine->Description()); result.Add("VERSION", pEngine->Version()); EngineFactory::Destroy(pEngine); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } + UnlockRTNotify(); return result.Produce(); } @@ -608,7 +775,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); //Defaults values @@ -622,6 +789,7 @@ String AudioRouting; int Mute = 0; bool Solo = false; + String MidiInstrumentMap = "NONE"; if (pEngineChannel) { EngineName = pEngineChannel->EngineName(); @@ -639,6 +807,12 @@ } Mute = pEngineChannel->GetMute(); Solo = pEngineChannel->GetSolo(); + if (pEngineChannel->UsesNoMidiInstrumentMap()) + MidiInstrumentMap = "NONE"; + else if (pEngineChannel->UsesDefaultMidiInstrumentMap()) + MidiInstrumentMap = "DEFAULT"; + else + MidiInstrumentMap = ToString(pEngineChannel->GetMidiInstrumentMap()); } result.Add("ENGINE_NAME", EngineName); @@ -660,8 +834,9 @@ result.Add("INSTRUMENT_STATUS", InstrumentStatus); result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false")); result.Add("SOLO", Solo); + result.Add("MIDI_INSTRUMENT_MAP", MidiInstrumentMap); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -676,13 +851,13 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("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"); + if (!pEngineChannel) throw Exception("No engine loaded on sampler channel"); + if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->VoiceCount()); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -697,13 +872,13 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("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) throw Exception("No engine type assigned to sampler channel"); + if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -718,10 +893,10 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("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) throw Exception("No engine type assigned to sampler channel"); + if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); else { switch (ResponseType) { @@ -732,11 +907,11 @@ result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage()); break; default: - throw LinuxSamplerException("Unknown fill response type"); + throw Exception("Unknown fill response type"); } } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -749,7 +924,7 @@ int n = AudioOutputDeviceFactory::AvailableDrivers().size(); result.Add(n); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -762,7 +937,7 @@ String s = AudioOutputDeviceFactory::AvailableDriversAsString(); result.Add(s); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -775,7 +950,7 @@ int n = MidiInputDeviceFactory::AvailableDrivers().size(); result.Add(n); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -788,7 +963,7 @@ String s = MidiInputDeviceFactory::AvailableDriversAsString(); result.Add(s); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -812,7 +987,7 @@ result.Add("PARAMETERS", s); } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -836,7 +1011,7 @@ result.Add("PARAMETERS", s); } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -863,7 +1038,7 @@ if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -890,7 +1065,7 @@ if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -903,7 +1078,7 @@ uint count = pSampler->AudioOutputDevices(); result.Add(count); // success } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -916,7 +1091,7 @@ uint count = pSampler->MidiInputDevices(); result.Add(count); // success } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -935,7 +1110,7 @@ } result.Add(s); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -954,7 +1129,7 @@ } result.Add(s); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -965,7 +1140,7 @@ LSCPResultSet result; try { std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + "."); AudioOutputDevice* pDevice = devices[DeviceIndex]; result.Add("DRIVER", pDevice->Driver()); std::map parameters = pDevice->DeviceParameters(); @@ -974,7 +1149,7 @@ result.Add(iter->first, iter->second->Value()); } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -985,7 +1160,7 @@ LSCPResultSet result; try { std::map devices = pSampler->GetMidiInputDevices(); - if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; result.Add("DRIVER", pDevice->Driver()); std::map parameters = pDevice->DeviceParameters(); @@ -994,7 +1169,7 @@ result.Add(iter->first, iter->second->Value()); } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1005,12 +1180,12 @@ 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) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; // get MIDI port MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex); - if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + "."); + if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + "."); // return the values of all MIDI port parameters std::map parameters = pMidiInputPort->PortParameters(); @@ -1019,7 +1194,7 @@ result.Add(iter->first, iter->second->Value()); } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1031,12 +1206,12 @@ try { // get audio output device std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); + if (!devices.count(DeviceId)) throw Exception("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 audio channel " + ToString(ChannelId) + "."); + if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // return the values of all audio channel parameters std::map parameters = pChannel->ChannelParameters(); @@ -1045,7 +1220,7 @@ result.Add(iter->first, iter->second->Value()); } } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1057,16 +1232,16 @@ try { // 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) + "."); + if (!devices.count(DeviceId)) throw Exception("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) + "."); + if (!pPort) throw Exception("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 + "'."); + if (!parameters.count(ParameterName)) throw Exception("Midi port does not provide a parameter '" + ParameterName + "'."); DeviceRuntimeParameter* pParameter = parameters[ParameterName]; // return all fields of this audio channel parameter @@ -1078,7 +1253,7 @@ if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax()); if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities()); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1090,16 +1265,16 @@ try { // get audio output device std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); + if (!devices.count(DeviceId)) throw Exception("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 audio channel " + ToString(ChannelId) + "."); + if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // get desired audio channel parameter std::map parameters = pChannel->ChannelParameters(); - if (!parameters.count(ParameterName)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'."); + if (!parameters.count(ParameterName)) throw Exception("Audio channel does not provide a parameter '" + ParameterName + "'."); DeviceRuntimeParameter* pParameter = parameters[ParameterName]; // return all fields of this audio channel parameter @@ -1111,7 +1286,7 @@ if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax()); if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities()); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1123,22 +1298,23 @@ try { // get audio output device std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + "."); + if (!devices.count(DeviceId)) throw Exception("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 audio channel " + ToString(ChannelId) + "."); + if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // get desired audio channel parameter std::map parameters = pChannel->ChannelParameters(); - if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'."); + if (!parameters.count(ParamKey)) throw Exception("Audio channel does not provide a parameter '" + ParamKey + "'."); DeviceRuntimeParameter* pParameter = parameters[ParamKey]; // set new channel parameter value pParameter->SetValue(ParamVal); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceId)); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1149,13 +1325,14 @@ LSCPResultSet result; try { std::map devices = pSampler->GetAudioOutputDevices(); - if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + "."); AudioOutputDevice* pDevice = devices[DeviceIndex]; std::map parameters = pDevice->DeviceParameters(); - if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'"); + if (!parameters.count(ParamKey)) throw Exception("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'"); parameters[ParamKey]->SetValue(ParamVal); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceIndex)); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1166,13 +1343,14 @@ LSCPResultSet result; try { std::map devices = pSampler->GetMidiInputDevices(); - if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; std::map parameters = pDevice->DeviceParameters(); - if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'"); + if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'"); parameters[ParamKey]->SetValue(ParamVal); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex)); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1184,19 +1362,20 @@ 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) + "."); + if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + "."); MidiInputDevice* pDevice = devices[DeviceIndex]; // get MIDI port MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex); - if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + "."); + if (!pMidiInputPort) throw Exception("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 + "'"); + if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'"); parameters[ParamKey]->SetValue(ParamVal); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex)); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1211,13 +1390,13 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("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)); + if (!pEngineChannel) throw Exception("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel)); + if (!pSamplerChannel->GetAudioOutputDevice()) throw Exception("No audio output device connected to sampler channel " + ToString(uiSamplerChannel)); pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1226,26 +1405,29 @@ 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 sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("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)); + if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId)); AudioOutputDevice* pDevice = devices[AudioDeviceId]; pSamplerChannel->SetAudioOutputDevice(pDevice); } - catch (LinuxSamplerException e) { + catch (Exception 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 sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA"; if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK"; @@ -1267,13 +1449,14 @@ } // Must have a device... if (pDevice == NULL) - throw LinuxSamplerException("Internal error: could not create audio output device."); + throw Exception("Internal error: could not create audio output device."); // Set it as the current channel device... pSamplerChannel->SetAudioOutputDevice(pDevice); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } + UnlockRTNotify(); return result.Produce(); } @@ -1282,10 +1465,10 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); pSamplerChannel->SetMidiInputPort(MIDIPort); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1296,10 +1479,10 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1310,13 +1493,13 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("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)); + if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId)); MidiInputDevice* pDevice = devices[MIDIDeviceId]; pSamplerChannel->SetMidiInputDevice(pDevice); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1327,7 +1510,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA"; // Check if there's one MIDI input device already created @@ -1351,11 +1534,11 @@ } // Must have a device... if (pDevice == NULL) - throw LinuxSamplerException("Internal error: could not create MIDI input device."); + throw Exception("Internal error: could not create MIDI input device."); // Set it as the current channel device... pSamplerChannel->SetMidiInputDevice(pDevice); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1370,13 +1553,13 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("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)); + if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId)); MidiInputDevice* pDevice = devices[MIDIDeviceId]; pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1391,12 +1574,12 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); pEngineChannel->Volume(dVolume); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1410,14 +1593,14 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); else pEngineChannel->SetMute(1); - } catch (LinuxSamplerException e) { + } catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1431,26 +1614,26 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + if (!pEngineChannel) throw Exception("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) { + } catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1503,6 +1686,472 @@ } } +String LSCPServer::AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name, bool bModal) { + dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n")); + + midi_prog_index_t idx; + idx.midi_bank_msb = (MidiBank >> 7) & 0x7f; + idx.midi_bank_lsb = MidiBank & 0x7f; + idx.midi_prog = MidiProg; + + MidiInstrumentMapper::entry_t entry; + entry.EngineName = EngineType; + entry.InstrumentFile = InstrumentFile; + entry.InstrumentIndex = InstrumentIndex; + entry.LoadMode = LoadMode; + entry.Volume = Volume; + entry.Name = Name; + + LSCPResultSet result; + try { + // PERSISTENT mapping commands might block for a long time, so in + // that case we add/replace the mapping in another thread in case + // the NON_MODAL argument was supplied, non persistent mappings + // should return immediately, so we don't need to do that for them + bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal); + MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) { + dmsg(2,("LSCPServer: RemoveMIDIInstrumentMapping()\n")); + + midi_prog_index_t idx; + idx.midi_bank_msb = (MidiBank >> 7) & 0x7f; + idx.midi_bank_lsb = MidiBank & 0x7f; + idx.midi_prog = MidiProg; + + LSCPResultSet result; + try { + MidiInstrumentMapper::RemoveEntry(MidiMapID, idx); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetMidiInstrumentMappings(uint MidiMapID) { + dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n")); + LSCPResultSet result; + try { + result.Add(MidiInstrumentMapper::Entries(MidiMapID).size()); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + + +String LSCPServer::GetAllMidiInstrumentMappings() { + dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n")); + LSCPResultSet result; + std::vector maps = MidiInstrumentMapper::Maps(); + int totalMappings = 0; + for (int i = 0; i < maps.size(); i++) { + try { + totalMappings += MidiInstrumentMapper::Entries(maps[i]).size(); + } catch (Exception e) { /*NOOP*/ } + } + result.Add(totalMappings); + return result.Produce(); +} + +String LSCPServer::GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) { + dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n")); + LSCPResultSet result; + try { + midi_prog_index_t idx; + idx.midi_bank_msb = (MidiBank >> 7) & 0x7f; + idx.midi_bank_lsb = MidiBank & 0x7f; + idx.midi_prog = MidiProg; + + std::map mappings = MidiInstrumentMapper::Entries(MidiMapID); + std::map::iterator iter = mappings.find(idx); + if (iter == mappings.end()) result.Error("there is no map entry with that index"); + else { // found + result.Add("NAME", iter->second.Name); + result.Add("ENGINE_NAME", iter->second.EngineName); + result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile); + result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); + String instrumentName; + Engine* pEngine = EngineFactory::Create(iter->second.EngineName); + if (pEngine) { + if (pEngine->GetInstrumentManager()) { + InstrumentManager::instrument_id_t instrID; + instrID.FileName = iter->second.InstrumentFile; + instrID.Index = iter->second.InstrumentIndex; + instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); + } + EngineFactory::Destroy(pEngine); + } + result.Add("INSTRUMENT_NAME", instrumentName); + switch (iter->second.LoadMode) { + case MidiInstrumentMapper::ON_DEMAND: + result.Add("LOAD_MODE", "ON_DEMAND"); + break; + case MidiInstrumentMapper::ON_DEMAND_HOLD: + result.Add("LOAD_MODE", "ON_DEMAND_HOLD"); + break; + case MidiInstrumentMapper::PERSISTENT: + result.Add("LOAD_MODE", "PERSISTENT"); + break; + default: + throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!"); + } + result.Add("VOLUME", iter->second.Volume); + } + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListMidiInstrumentMappings(uint MidiMapID) { + dmsg(2,("LSCPServer: ListMidiInstrumentMappings()\n")); + LSCPResultSet result; + try { + String s; + std::map mappings = MidiInstrumentMapper::Entries(MidiMapID); + std::map::iterator iter = mappings.begin(); + for (; iter != mappings.end(); iter++) { + if (s.size()) s += ","; + s += "{" + ToString(MidiMapID) + "," + + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + "," + + ToString(int(iter->first.midi_prog)) + "}"; + } + result.Add(s); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListAllMidiInstrumentMappings() { + dmsg(2,("LSCPServer: ListAllMidiInstrumentMappings()\n")); + LSCPResultSet result; + try { + std::vector maps = MidiInstrumentMapper::Maps(); + String s; + for (int i = 0; i < maps.size(); i++) { + std::map mappings = MidiInstrumentMapper::Entries(maps[i]); + std::map::iterator iter = mappings.begin(); + for (; iter != mappings.end(); iter++) { + if (s.size()) s += ","; + s += "{" + ToString(maps[i]) + "," + + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + "," + + ToString(int(iter->first.midi_prog)) + "}"; + } + } + result.Add(s); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ClearMidiInstrumentMappings(uint MidiMapID) { + dmsg(2,("LSCPServer: ClearMidiInstrumentMappings()\n")); + LSCPResultSet result; + try { + MidiInstrumentMapper::RemoveAllEntries(MidiMapID); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ClearAllMidiInstrumentMappings() { + dmsg(2,("LSCPServer: ClearAllMidiInstrumentMappings()\n")); + LSCPResultSet result; + try { + std::vector maps = MidiInstrumentMapper::Maps(); + for (int i = 0; i < maps.size(); i++) + MidiInstrumentMapper::RemoveAllEntries(maps[i]); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::AddMidiInstrumentMap(String MapName) { + dmsg(2,("LSCPServer: AddMidiInstrumentMap()\n")); + LSCPResultSet result; + try { + int MapID = MidiInstrumentMapper::AddMap(MapName); + result = LSCPResultSet(MapID); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::RemoveMidiInstrumentMap(uint MidiMapID) { + dmsg(2,("LSCPServer: RemoveMidiInstrumentMap()\n")); + LSCPResultSet result; + try { + MidiInstrumentMapper::RemoveMap(MidiMapID); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::RemoveAllMidiInstrumentMaps() { + dmsg(2,("LSCPServer: RemoveAllMidiInstrumentMaps()\n")); + LSCPResultSet result; + try { + MidiInstrumentMapper::RemoveAllMaps(); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetMidiInstrumentMaps() { + dmsg(2,("LSCPServer: GetMidiInstrumentMaps()\n")); + LSCPResultSet result; + try { + result.Add(MidiInstrumentMapper::Maps().size()); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListMidiInstrumentMaps() { + dmsg(2,("LSCPServer: ListMidiInstrumentMaps()\n")); + LSCPResultSet result; + try { + std::vector maps = MidiInstrumentMapper::Maps(); + String sList; + for (int i = 0; i < maps.size(); i++) { + if (sList != "") sList += ","; + sList += ToString(maps[i]); + } + result.Add(sList); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetMidiInstrumentMap(uint MidiMapID) { + dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n")); + LSCPResultSet result; + try { + result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); + result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::SetMidiInstrumentMapName(uint MidiMapID, String NewName) { + dmsg(2,("LSCPServer: SetMidiInstrumentMapName()\n")); + LSCPResultSet result; + try { + MidiInstrumentMapper::RenameMap(MidiMapID, NewName); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +/** + * Set the MIDI instrument map the given sampler channel shall use for + * handling MIDI program change messages. There are the following two + * special (negative) values: + * + * - (-1) : set to NONE (ignore program changes) + * - (-2) : set to DEFAULT map + */ +String LSCPServer::SetChannelMap(uint uiSamplerChannel, int MidiMapID) { + 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"); + + if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone(); + else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault(); + else pEngineChannel->SetMidiInstrumentMap(MidiMapID); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) { + dmsg(2,("LSCPServer: CreateFxSend()\n")); + LSCPResultSet result; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name); + if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)"); + + result = LSCPResultSet(pFxSend->Id()); // success + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) { + dmsg(2,("LSCPServer: DestroyFxSend()\n")); + LSCPResultSet result; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + FxSend* pFxSend = NULL; + for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) { + if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) { + pFxSend = pEngineChannel->GetFxSend(i); + break; + } + } + if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel"); + pEngineChannel->RemoveFxSend(pFxSend); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::GetFxSends(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: GetFxSends()\n")); + LSCPResultSet result; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + result.Add(pEngineChannel->GetFxSendCount()); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::ListFxSends(uint uiSamplerChannel) { + dmsg(2,("LSCPServer: ListFxSends()\n")); + LSCPResultSet result; + String list; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) { + FxSend* pFxSend = pEngineChannel->GetFxSend(i); + if (list != "") list += ","; + list += ToString(pFxSend->Id()); + } + result.Add(list); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + FxSend* pFxSend = NULL; + for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) { + if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) { + pFxSend = pEngineChannel->GetFxSend(i); + break; + } + } + if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel"); + return pFxSend; +} + +String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) { + dmsg(2,("LSCPServer: GetFxSendInfo()\n")); + LSCPResultSet result; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); + + // gather audio routing informations + String AudioRouting; + for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { + if (AudioRouting != "") AudioRouting += ","; + AudioRouting += ToString(pFxSend->DestinationChannel(chan)); + } + + // success + result.Add("NAME", pFxSend->Name()); + result.Add("MIDI_CONTROLLER", pFxSend->MidiController()); + result.Add("LEVEL", ToString(pFxSend->Level())); + result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) { + dmsg(2,("LSCPServer: SetFxSendName()\n")); + LSCPResultSet result; + try { + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); + + pFxSend->SetName(Name); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) { + dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n")); + LSCPResultSet result; + try { + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); + + pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) { + dmsg(2,("LSCPServer: SetFxSendMidiController()\n")); + LSCPResultSet result; + try { + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); + + pFxSend->SetMidiController(MidiController); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) { + dmsg(2,("LSCPServer: SetFxSendLevel()\n")); + LSCPResultSet result; + try { + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); + + pFxSend->SetLevel((float)dLevel); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + /** * Will be called by the parser to reset a particular sampler channel. */ @@ -1511,12 +2160,12 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel"); + if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); pEngineChannel->Reset(); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); @@ -1541,7 +2190,51 @@ LSCPResultSet result; result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); result.Add("VERSION", VERSION); - result.Add("PROTOCOL_VERSION", "1.0"); + result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR)); +#if HAVE_SQLITE3 + result.Add("INSTRUMENTS_DB_SUPPORT", "yes"); +#else + result.Add("INSTRUMENTS_DB_SUPPORT", "no"); +#endif + + 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(); +} + +String LSCPServer::GetGlobalVolume() { + LSCPResultSet result; + result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp + return result.Produce(); +} + +String LSCPServer::SetGlobalVolume(double dVolume) { + LSCPResultSet result; + try { + if (dVolume < 0) throw Exception("Volume may not be negative"); + GLOBAL_VOLUME = dVolume; // see common/global.cpp + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); + } catch (Exception e) { + result.Error(e); + } return result.Produce(); } @@ -1571,37 +2264,436 @@ 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::AddDbInstrumentDirectory(String Dir) { + dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) { + dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) { + dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive)); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) { + dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + String list; + StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive); + + for (int i = 0; i < dirs->size(); i++) { + if (list != "") list += ","; + list += "'" + dirs->at(i) + "'"; + } + + result.Add(list); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) { + dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir); + + result.Add("DESCRIPTION", info.Description); + result.Add("CREATED", info.Created); + result.Add("MODIFIED", info.Modified); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) { + dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) { + dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) { + dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); } -String LSCPServer::QueryDatabase(String query) { +String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) { + dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str())); LSCPResultSet result; #if HAVE_SQLITE3 - char* zErrMsg = NULL; - sqlite3 *db; - String selectStr = "SELECT " + query; + try { + InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} - int rc = sqlite3_open("linuxsampler.db", &db); - if (rc == SQLITE_OK) - { - rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg); +String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index) { + dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->AddInstruments(DbDir, FilePath, Index); + } catch (Exception e) { + result.Error(e); } - if ( rc != SQLITE_OK ) - { - result.Error(String(zErrMsg), rc); +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::AddDbInstrumentsFlat(String DbDir, String FsDir) { + dmsg(2,("LSCPServer: AddDbInstrumentsFlat(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->AddInstrumentsRecursive(DbDir, FsDir, true); + } catch (Exception e) { + result.Error(e); } - sqlite3_close(db); #else - result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0); + result.Error(String(DOESNT_HAVE_SQLITE3), 0); #endif return result.Produce(); } +String LSCPServer::AddDbInstrumentsNonrecursive(String DbDir, String FsDir) { + dmsg(2,("LSCPServer: AddDbInstrumentsNonrecursive(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->AddInstrumentsNonrecursive(DbDir, FsDir); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::RemoveDbInstrument(String Instr) { + dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) { + dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive)); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstruments(String Dir, bool Recursive) { + dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + String list; + StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive); + + for (int i = 0; i < instrs->size(); i++) { + if (list != "") list += ","; + list += "'" + instrs->at(i) + "'"; + } + + result.Add(list); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstrumentInfo(String Instr) { + dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr); + + result.Add("INSTRUMENT_FILE", info.InstrFile); + result.Add("INSTRUMENT_NR", info.InstrNr); + result.Add("FORMAT_FAMILY", info.FormatFamily); + result.Add("FORMAT_VERSION", info.FormatVersion); + result.Add("SIZE", (int)info.Size); + result.Add("CREATED", info.Created); + result.Add("MODIFIED", info.Modified); + result.Add("DESCRIPTION", FilterEndlines(info.Description)); + result.Add("IS_DRUM", info.IsDrum); + result.Add("PRODUCT", FilterEndlines(info.Product)); + result.Add("ARTISTS", FilterEndlines(info.Artists)); + result.Add("KEYWORDS", FilterEndlines(info.Keywords)); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::SetDbInstrumentName(String Instr, String Name) { + dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::MoveDbInstrument(String Instr, String Dst) { + dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::CopyDbInstrument(String Instr, String Dst) { + dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) { + dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map Parameters, bool Recursive) { + dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + SearchQuery Query; + std::map::iterator iter; + for (iter = Parameters.begin(); iter != Parameters.end(); iter++) { + if (iter->first.compare("NAME") == 0) { + Query.Name = iter->second; + } else if (iter->first.compare("CREATED") == 0) { + Query.SetCreated(iter->second); + } else if (iter->first.compare("MODIFIED") == 0) { + Query.SetModified(iter->second); + } else if (iter->first.compare("DESCRIPTION") == 0) { + Query.Description = iter->second; + } else { + throw Exception("Unknown search criteria: " + iter->first); + } + } + + String list; + StringListPtr pDirectories = + InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive); + + for (int i = 0; i < pDirectories->size(); i++) { + if (list != "") list += ","; + list += "'" + pDirectories->at(i) + "'"; + } + + result.Add(list); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::FindDbInstruments(String Dir, std::map Parameters, bool Recursive) { + dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + SearchQuery Query; + std::map::iterator iter; + for (iter = Parameters.begin(); iter != Parameters.end(); iter++) { + if (iter->first.compare("NAME") == 0) { + Query.Name = iter->second; + } else if (iter->first.compare("FORMAT_FAMILIES") == 0) { + Query.SetFormatFamilies(iter->second); + } else if (iter->first.compare("SIZE") == 0) { + Query.SetSize(iter->second); + } else if (iter->first.compare("CREATED") == 0) { + Query.SetCreated(iter->second); + } else if (iter->first.compare("MODIFIED") == 0) { + Query.SetModified(iter->second); + } else if (iter->first.compare("DESCRIPTION") == 0) { + Query.Description = iter->second; + } else if (iter->first.compare("IS_DRUM") == 0) { + if (!strcasecmp(iter->second.c_str(), "true")) { + Query.InstrType = SearchQuery::DRUM; + } else { + Query.InstrType = SearchQuery::CHROMATIC; + } + } else if (iter->first.compare("PRODUCT") == 0) { + Query.Product = iter->second; + } else if (iter->first.compare("ARTISTS") == 0) { + Query.Artists = iter->second; + } else if (iter->first.compare("KEYWORDS") == 0) { + Query.Keywords = iter->second; + } else { + throw Exception("Unknown search criteria: " + iter->first); + } + } + + String list; + StringListPtr pInstruments = + InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive); + + for (int i = 0; i < pInstruments->size(); i++) { + if (list != "") list += ","; + list += "'" + pInstruments->at(i) + "'"; + } + + result.Add(list); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 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 @@ -1613,10 +2705,20 @@ try { if (boolean_value == 0) pSession->bVerbose = false; else if (boolean_value == 1) pSession->bVerbose = true; - else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1"); + else throw Exception("Not a boolean value, must either be 0 or 1"); } - catch (LinuxSamplerException e) { + catch (Exception e) { result.Error(e); } return result.Produce(); } + +String LSCPServer::FilterEndlines(String s) { + String s2 = s; + for (int i = 0; i < s2.length(); i++) { + if (s2.at(i) == '\r') s2.at(i) = ' '; + else if (s2.at(i) == '\n') s2.at(i) = ' '; + } + + return s2; +}