--- linuxsampler/trunk/src/network/lscpserver.cpp 2008/09/03 17:18:51 1763 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2008/09/29 18:21:21 1781 @@ -22,6 +22,7 @@ ***************************************************************************/ #include +#include #include "lscpserver.h" #include "lscpresultset.h" @@ -42,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 @@ -1133,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()); } @@ -1154,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()); } @@ -1175,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 { @@ -1856,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) { @@ -1875,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); @@ -1896,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(); @@ -2253,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(); @@ -2436,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(); @@ -2454,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. */ @@ -2461,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) { @@ -2671,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())); @@ -2903,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); } @@ -3259,3 +3289,5 @@ } return result.Produce(); } + +}