--- linuxsampler/trunk/src/network/lscpserver.cpp 2006/12/29 20:06:14 1005 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2007/09/16 23:06:10 1350 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005, 2006 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" @@ -53,6 +52,7 @@ fd_set LSCPServer::fdSet; int LSCPServer::currentSocket = -1; std::vector LSCPServer::Sessions = std::vector(); +std::vector::iterator itCurrentSession = std::vector::iterator(); std::map LSCPServer::bufferedNotifies = std::map(); std::map LSCPServer::bufferedCommands = std::map(); std::map< LSCPEvent::event_t, std::list > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list >(); @@ -75,12 +75,20 @@ 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_db_instrs_job_info, "DB_INSTRUMENTS_JOB_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; } @@ -88,6 +96,89 @@ 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, InstrumentsDb::toEscapedPath(Dir))); +} + +void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir))); +} + +void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) { + Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'"; + NewName = "'" + InstrumentsDb::toEscapedPath(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, InstrumentsDb::toEscapedPath(Dir))); +} + +void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr))); +} + +void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) { + Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'"; + NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'"; + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName)); +} + +void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) { + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId)); +} +#endif // HAVE_SQLITE3 + + /** * Blocks the calling thread until the LSCP Server is initialized and * accepting socket connections, if the server is already initialized then @@ -129,6 +220,22 @@ 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); @@ -148,6 +255,15 @@ 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); + } + } } } @@ -212,12 +328,14 @@ int dummy; // just a temporary hack to fulfill the restart() function prototype restart(NULL, dummy); // restart the 'scanner' currentSocket = (*iter).hSession; //a hack + itCurrentSession = iter; // another hack dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str())); if ((*iter).bVerbose) { // if echo mode enabled AnswerClient(bufferedCommands[currentSocket]); } int result = yyparse(&(*iter)); currentSocket = -1; //continuation of a hack + itCurrentSession = Sessions.end(); // hack as well dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); if (result == LSCP_QUIT) { //Was it a quit command by any chance? CloseConnection(iter); @@ -310,6 +428,10 @@ return command.size(); } +extern yyparse_param_t* GetCurrentYaccSession() { + return &(*itCurrentSession); +} + /** * Will be called to try to read the command from the socket * If command is read, it will return true. Otherwise false is returned. @@ -490,6 +612,16 @@ 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. */ @@ -670,7 +802,7 @@ String AudioRouting; int Mute = 0; bool Solo = false; - String MidiInstrumentMap; + String MidiInstrumentMap = "NONE"; if (pEngineChannel) { EngineName = pEngineChannel->EngineName(); @@ -1502,14 +1634,14 @@ 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); @@ -1567,7 +1699,7 @@ } } -String LSCPServer::AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name) { +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; @@ -1585,9 +1717,11 @@ LSCPResultSet result; try { - // PERSISTENT mapping commands might bloock for a long time, so in - // that case we add/replace the mapping in another thread - bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT); + // 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); @@ -1698,7 +1832,7 @@ 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_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + "," + ToString(int(iter->first.midi_prog)) + "}"; } result.Add(s); @@ -1720,7 +1854,7 @@ 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_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + "," + ToString(int(iter->first.midi_prog)) + "}"; } } @@ -1822,6 +1956,7 @@ LSCPResultSet result; try { result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); + result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); } catch (Exception e) { result.Error(e); } @@ -1870,11 +2005,7 @@ dmsg(2,("LSCPServer: CreateFxSend()\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); 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)"); @@ -1890,11 +2021,7 @@ dmsg(2,("LSCPServer: DestroyFxSend()\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); FxSend* pFxSend = NULL; for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) { @@ -1915,11 +2042,7 @@ dmsg(2,("LSCPServer: GetFxSends()\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); result.Add(pEngineChannel->GetFxSendCount()); } catch (Exception e) { @@ -1933,11 +2056,7 @@ LSCPResultSet result; String list; 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); for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) { FxSend* pFxSend = pEngineChannel->GetFxSend(i); @@ -1951,24 +2070,26 @@ 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 { - 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"); - - 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"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); // gather audio routing informations String AudioRouting; @@ -1979,6 +2100,8 @@ // 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); @@ -1986,26 +2109,78 @@ 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 { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); + pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} - 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"); +String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) { + dmsg(2,("LSCPServer: SetFxSendMidiController()\n")); + LSCPResultSet result; + try { + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); - pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel); + 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(); +} + +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"); + if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel"); + Engine* pEngine = pEngineChannel->GetEngine(); + InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager(); + if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager"); + InstrumentManager::instrument_id_t instrumentID; + instrumentID.FileName = pEngineChannel->InstrumentFileName(); + instrumentID.Index = pEngineChannel->InstrumentIndex(); + pInstrumentManager->LaunchInstrumentEditor(instrumentID); } catch (Exception e) { result.Error(e); } @@ -2051,6 +2226,12 @@ result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler"); result.Add("VERSION", VERSION); 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(); } @@ -2085,6 +2266,7 @@ 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); } @@ -2117,37 +2299,456 @@ 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 += "'" + InstrumentsDb::toEscapedPath(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", InstrumentsDb::toEscapedText(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::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 + try { + InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) { + dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + int id; + InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); + id = db->AddInstruments(DbDir, FilePath, Index, bBackground); + if (bBackground) result = id; + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + 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)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + int id; + InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); + if (ScanMode.compare("RECURSIVE") == 0) { + id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground); + } else if (ScanMode.compare("NON_RECURSIVE") == 0) { + id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground); + } else if (ScanMode.compare("FLAT") == 0) { + id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground); + } else { + throw Exception("Unknown scan mode: " + ScanMode); + } + + if (bBackground) result = id; + } 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 += "'" + InstrumentsDb::toEscapedPath(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", InstrumentsDb::toEscapedText(info.Description)); + result.Add("IS_DRUM", info.IsDrum); + result.Add("PRODUCT", InstrumentsDb::toEscapedText(info.Product)); + result.Add("ARTISTS", InstrumentsDb::toEscapedText(info.Artists)); + result.Add("KEYWORDS", InstrumentsDb::toEscapedText(info.Keywords)); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::GetDbInstrumentsJobInfo(int JobId) { + dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId)); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId); + + result.Add("FILES_TOTAL", job.FilesTotal); + result.Add("FILES_SCANNED", job.FilesScanned); + result.Add("SCANNING", job.Scanning); + result.Add("STATUS", job.Status); + } 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::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 - char* zErrMsg = NULL; - sqlite3 *db; - String selectStr = "SELECT " + query; + try { + InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name); + } 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::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); } - if ( rc != SQLITE_OK ) - { - result.Error(String(zErrMsg), rc); +#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); } - 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::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 += "'" + InstrumentsDb::toEscapedPath(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 += "'" + InstrumentsDb::toEscapedPath(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