--- linuxsampler/trunk/src/network/lscpserver.cpp 2007/02/19 19:38:04 1047 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2007/05/29 23:59:36 1212 @@ -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" @@ -75,12 +74,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 +95,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, 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)); +} + +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 @@ -128,7 +218,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); @@ -148,6 +254,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); + } + } } } @@ -490,6 +605,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 +795,7 @@ String AudioRouting; int Mute = 0; bool Solo = false; - String MidiInstrumentMap; + String MidiInstrumentMap = "NONE"; if (pEngineChannel) { EngineName = pEngineChannel->EngineName(); @@ -1824,6 +1949,7 @@ LSCPResultSet result; try { result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID)); + result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID); } catch (Exception e) { result.Error(e); } @@ -1872,12 +1998,8 @@ 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)"); @@ -1892,11 +2014,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++) { @@ -1917,11 +2035,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) { @@ -1935,11 +2049,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); @@ -1953,25 +2063,27 @@ 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; for (int chan = 0; chan < pEngineChannel->Channels(); chan++) { @@ -1990,26 +2102,28 @@ return result.Produce(); } -String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) { - dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n")); +String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) { + dmsg(2,("LSCPServer: SetFxSendName()\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->SetName(Name); + 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::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); } @@ -2020,22 +2134,10 @@ dmsg(2,("LSCPServer: SetFxSendMidiController()\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"); + FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID); pFxSend->SetMidiController(MidiController); + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID)); } catch (Exception e) { result.Error(e); } @@ -2046,22 +2148,31 @@ 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("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"); - - pFxSend->SetLevel((float)dLevel); + if (!pEngineChannel) throw Exception("No engine type assigned 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); } @@ -2107,6 +2218,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(); } @@ -2141,6 +2258,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); } @@ -2173,37 +2291,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::QueryDatabase(String query) { +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 - char* zErrMsg = NULL; - sqlite3 *db; - String selectStr = "SELECT " + query; + try { + String list; + StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive); - int rc = sqlite3_open("linuxsampler.db", &db); - if (rc == SQLITE_OK) - { - rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg); + for (int i = 0; i < dirs->size(); i++) { + if (list != "") list += ","; + list += "'" + dirs->at(i) + "'"; + } + + result.Add(list); + } 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::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); } - 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::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 += "'" + 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::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::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 @@ -2222,3 +2759,13 @@ } 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; +}