--- linuxsampler/trunk/src/network/lscpserver.cpp 2008/02/14 14:58:50 1686 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2008/09/29 18:21:21 1781 @@ -21,6 +21,9 @@ * MA 02111-1307 USA * ***************************************************************************/ +#include +#include + #include "lscpserver.h" #include "lscpresultset.h" #include "lscpevent.h" @@ -40,6 +43,7 @@ #include "../drivers/audio/AudioOutputDeviceFactory.h" #include "../drivers/midi/MidiInputDeviceFactory.h" +namespace LinuxSampler { /** * Returns a copy of the given string where all special characters are @@ -131,6 +135,7 @@ LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI"); + LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI"); hSocket = -1; } @@ -207,6 +212,54 @@ LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); } +void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { + pDevice->RemoveMidiPortCountListener(this); + for (int i = 0; i < pDevice->PortCount(); ++i) + MidiPortToBeRemoved(pDevice->GetPort(i)); +} + +void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) { + pDevice->AddMidiPortCountListener(this); + for (int i = 0; i < pDevice->PortCount(); ++i) + MidiPortAdded(pDevice->GetPort(i)); +} + +void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) { + // yet unused +} + +void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) { + for (std::vector::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) { + if ((*iter).pPort == pPort) { + VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; + pPort->Disconnect(pMidiListener); + deviceMidiListeners.erase(iter); + delete pMidiListener; + return; + } + } +} + +void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) { + // find out the device ID + std::map devices = + pParent->pSampler->GetMidiInputDevices(); + for ( + std::map::iterator iter = devices.begin(); + iter != devices.end(); ++iter + ) { + if (iter->second == pPort->GetDevice()) { // found + VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; + pPort->Connect(pMidiListener); + device_midi_listener_entry entry = { + pPort, pMidiListener, iter->first + }; + deviceMidiListeners.push_back(entry); + return; + } + } +} + void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); } @@ -373,13 +426,13 @@ std::set::iterator itEnd = engineChannels.end(); for (; itEngineChannel != itEnd; ++itEngineChannel) { if ((*itEngineChannel)->StatusChanged()) { - SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex)); + SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index())); } for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { FxSend* fxs = (*itEngineChannel)->GetFxSend(i); if(fxs != NULL && fxs->IsInfoChanged()) { - int chn = (*itEngineChannel)->iSamplerChannelIndex; + int chn = (*itEngineChannel)->GetSamplerChannel()->Index(); LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id())); fxs->SetInfoChanged(false); } @@ -411,6 +464,31 @@ } } + // check if MIDI data arrived on some MIDI device + for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) { + const EventHandler::device_midi_listener_entry entry = + eventHandler.deviceMidiListeners[i]; + VirtualMidiDevice* pMidiListener = entry.pMidiListener; + if (pMidiListener->NotesChanged()) { + for (int iNote = 0; iNote < 128; iNote++) { + if (pMidiListener->NoteChanged(iNote)) { + const bool bActive = pMidiListener->NoteIsActive(iNote); + LSCPServer::SendLSCPNotify( + LSCPEvent( + LSCPEvent::event_device_midi, + entry.uiDeviceID, + entry.pPort->GetPortNumber(), + std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), + iNote, + bActive ? pMidiListener->NoteOnVelocity(iNote) + : pMidiListener->NoteOffVelocity(iNote) + ) + ); + } + } + } + } + //Now let's deliver late notifies (if any) NotifyBufferMutex.Lock(); for (std::map::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) { @@ -1057,10 +1135,7 @@ dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine loaded on sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->VoiceCount()); } @@ -1078,10 +1153,7 @@ dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); } @@ -1099,10 +1171,7 @@ dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); else { @@ -1780,10 +1849,7 @@ dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); pEngineChannel->Volume(dVolume); } catch (Exception e) { @@ -1799,11 +1865,7 @@ dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); else pEngineChannel->SetMute(1); @@ -1820,11 +1882,7 @@ dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); bool oldSolo = pEngineChannel->GetSolo(); bool hadSoloChannel = HasSoloChannel(); @@ -1944,7 +2002,7 @@ dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n")); LSCPResultSet result; try { - result.Add(MidiInstrumentMapper::Entries(MidiMapID).size()); + result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID)); } catch (Exception e) { result.Error(e); } @@ -1955,14 +2013,11 @@ 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*/ } + try { + result.Add(MidiInstrumentMapper::GetInstrumentCount()); + } catch (Exception e) { + result.Error(e); } - result.Add(totalMappings); return result.Produce(); } @@ -1970,56 +2025,46 @@ 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 - - // convert the filename into the correct encoding as defined for LSCP - // (especially in terms of special characters -> escape sequences) + MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg); + // convert the filename into the correct encoding as defined for LSCP + // (especially in terms of special characters -> escape sequences) #if WIN32 - const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp(); + const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp(); #else - // assuming POSIX - const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp(); + // assuming POSIX + const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp(); #endif - result.Add("NAME", _escapeLscpResponse(iter->second.Name)); - result.Add("ENGINE_NAME", iter->second.EngineName); - result.Add("INSTRUMENT_FILE", instrumentFileName); - 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("NAME", _escapeLscpResponse(entry.Name)); + result.Add("ENGINE_NAME", entry.EngineName); + result.Add("INSTRUMENT_FILE", instrumentFileName); + result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex); + String instrumentName; + Engine* pEngine = EngineFactory::Create(entry.EngineName); + if (pEngine) { + if (pEngine->GetInstrumentManager()) { + InstrumentManager::instrument_id_t instrID; + instrID.FileName = entry.InstrumentFile; + instrID.Index = entry.InstrumentIndex; + instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); } - result.Add("INSTRUMENT_NAME", _escapeLscpResponse(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); + EngineFactory::Destroy(pEngine); + } + result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); + switch (entry.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", entry.Volume); } catch (Exception e) { result.Error(e); } @@ -2190,11 +2235,7 @@ dmsg(2,("LSCPServer: SetChannelMap()\n")); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone(); else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault(); @@ -2373,10 +2414,7 @@ dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel"); Engine* pEngine = pEngineChannel->GetEngine(); InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager(); @@ -2391,6 +2429,44 @@ return result.Produce(); } +String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) { + dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2)); + LSCPResultSet result; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + if (Arg1 > 127 || Arg2 > 127) { + throw Exception("Invalid MIDI message"); + } + + VirtualMidiDevice* pMidiDevice = NULL; + std::vector::iterator iter = eventHandler.channelMidiListeners.begin(); + for (; iter != eventHandler.channelMidiListeners.end(); ++iter) { + if ((*iter).pEngineChannel == pEngineChannel) { + pMidiDevice = (*iter).pMidiListener; + break; + } + } + + if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device"); + + if (MidiMsg == "NOTE_ON") { + pMidiDevice->SendNoteOnToDevice(Arg1, Arg2); + bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2); + if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); + } else if (MidiMsg == "NOTE_OFF") { + pMidiDevice->SendNoteOffToDevice(Arg1, Arg2); + bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2); + if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); + } else { + throw Exception("Unknown MIDI message type: " + MidiMsg); + } + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + /** * Will be called by the parser to reset a particular sampler channel. */ @@ -2398,10 +2474,7 @@ dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); pEngineChannel->Reset(); } catch (Exception e) { @@ -2481,7 +2554,7 @@ LSCPResultSet result; try { if (dVolume < 0) throw Exception("Volume may not be negative"); - GLOBAL_VOLUME = dVolume; // see common/global.cpp + GLOBAL_VOLUME = dVolume; // see common/global_private.cpp LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); } catch (Exception e) { result.Error(e); @@ -2608,6 +2681,26 @@ result.Add("FORMAT_VERSION", info.FormatVersion); result.Add("PRODUCT", info.Product); result.Add("ARTISTS", info.Artists); + + std::stringstream ss; + bool b = false; + for (int i = 0; i < 128; i++) { + if (info.KeyBindings[i]) { + if (b) ss << ','; + ss << i; b = true; + } + } + result.Add("KEY_BINDINGS", ss.str()); + + b = false; + std::stringstream ss2; + for (int i = 0; i < 128; i++) { + if (info.KeySwitchBindings[i]) { + if (b) ss2 << ','; + ss2 << i; b = true; + } + } + result.Add("KEYSWITCH_BINDINGS", ss2.str()); // no more need to ask other engine types bFound = true; } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); @@ -2840,19 +2933,19 @@ return result.Produce(); } -String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) { - dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground)); +String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) { + dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); LSCPResultSet result; #if HAVE_SQLITE3 try { int id; InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); if (ScanMode.compare("RECURSIVE") == 0) { - id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground); + id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir); } else if (ScanMode.compare("NON_RECURSIVE") == 0) { - id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground); + id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir); } else if (ScanMode.compare("FLAT") == 0) { - id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground); + id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir); } else { throw Exception("Unknown scan mode: " + ScanMode); } @@ -3028,6 +3121,44 @@ return result.Produce(); } +String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) { + dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::FindLostDbInstrumentFiles() { + dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n")); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + String list; + StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles(); + + for (int i = 0; i < pLostFiles->size(); i++) { + if (list != "") list += ","; + list += "'" + pLostFiles->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::FindDbInstrumentDirectories(String Dir, std::map Parameters, bool Recursive) { dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str())); LSCPResultSet result; @@ -3158,3 +3289,5 @@ } return result.Produce(); } + +}