--- linuxsampler/trunk/src/network/lscpserver.cpp 2008/01/25 15:06:02 1649 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2009/05/10 09:31:51 1897 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2007 Christian Schoenebeck * + * Copyright (C) 2005 - 2009 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 * @@ -21,6 +21,10 @@ * MA 02111-1307 USA * ***************************************************************************/ +#include +#include + +#include "../common/File.h" #include "lscpserver.h" #include "lscpresultset.h" #include "lscpevent.h" @@ -40,6 +44,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 @@ -101,7 +106,7 @@ Mutex LSCPServer::SubscriptionMutex = Mutex(); Mutex LSCPServer::RTNotifyMutex = Mutex(); -LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) { +LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) { SocketAddress.sin_family = AF_INET; SocketAddress.sin_addr.s_addr = addr; SocketAddress.sin_port = port; @@ -130,10 +135,14 @@ LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); 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; } LSCPServer::~LSCPServer() { + CloseAllConnections(); + InstrumentManager::StopBackgroundThread(); #if defined(WIN32) if (hSocket >= 0) closesocket(hSocket); #else @@ -141,10 +150,63 @@ #endif } +LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) { + this->pParent = pParent; +} + +LSCPServer::EventHandler::~EventHandler() { + std::vector l = channelMidiListeners; + channelMidiListeners.clear(); + for (int i = 0; i < l.size(); i++) + delete l[i].pMidiListener; +} + void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); } +void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) { + pChannel->AddEngineChangeListener(this); +} + +void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) { + if (!pChannel->GetEngineChannel()) return; + EngineToBeChanged(pChannel->Index()); +} + +void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) { + SamplerChannel* pSamplerChannel = + pParent->pSampler->GetSamplerChannel(ChannelId); + if (!pSamplerChannel) return; + EngineChannel* pEngineChannel = + pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) return; + for (std::vector::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) { + if ((*iter).pEngineChannel == pEngineChannel) { + VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; + pEngineChannel->Disconnect(pMidiListener); + channelMidiListeners.erase(iter); + delete pMidiListener; + return; + } + } +} + +void LSCPServer::EventHandler::EngineChanged(int ChannelId) { + SamplerChannel* pSamplerChannel = + pParent->pSampler->GetSamplerChannel(ChannelId); + if (!pSamplerChannel) return; + EngineChannel* pEngineChannel = + pSamplerChannel->GetEngineChannel(); + if (!pEngineChannel) return; + VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; + pEngineChannel->Connect(pMidiListener); + midi_listener_entry entry = { + pSamplerChannel, pEngineChannel, pMidiListener + }; + channelMidiListeners.push_back(entry); +} + void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); } @@ -153,6 +215,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)); } @@ -227,6 +337,24 @@ } #endif // HAVE_SQLITE3 +void LSCPServer::RemoveListeners() { + pSampler->RemoveChannelCountListener(&eventHandler); + pSampler->RemoveAudioDeviceCountListener(&eventHandler); + pSampler->RemoveMidiDeviceCountListener(&eventHandler); + pSampler->RemoveVoiceCountListener(&eventHandler); + pSampler->RemoveStreamCountListener(&eventHandler); + pSampler->RemoveBufferFillListener(&eventHandler); + pSampler->RemoveTotalStreamCountListener(&eventHandler); + pSampler->RemoveTotalVoiceCountListener(&eventHandler); + pSampler->RemoveFxSendCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler); +#if HAVE_SQLITE3 + InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler); +#endif +} /** * Blocks the calling thread until the LSCP Server is initialized and @@ -319,13 +447,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); } @@ -333,6 +461,55 @@ } } + // check if MIDI data arrived on some engine channel + for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) { + const EventHandler::midi_listener_entry entry = + eventHandler.channelMidiListeners[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_channel_midi, + entry.pSamplerChannel->Index(), + std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), + iNote, + bActive ? pMidiListener->NoteOnVelocity(iNote) + : pMidiListener->NoteOffVelocity(iNote) + ) + ); + } + } + } + } + + // 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++) { @@ -351,7 +528,7 @@ int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout); - if (retval == 0) + if (retval == 0 || (retval == -1 && errno == EINTR)) continue; //Nothing try again if (retval == -1) { std::cerr << "LSCPServer: Socket select error." << std::endl; @@ -378,6 +555,13 @@ exit(EXIT_FAILURE); } #else + struct linger linger; + linger.l_onoff = 1; + linger.l_linger = 0; + if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) { + std::cerr << "LSCPServer: Failed to set SO_LINGER\n"; + } + if (fcntl(socket, F_SETFL, O_NONBLOCK)) { std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; exit(EXIT_FAILURE); @@ -449,6 +633,14 @@ NotifyMutex.Unlock(); } +void LSCPServer::CloseAllConnections() { + std::vector::iterator iter = Sessions.begin(); + while(iter != Sessions.end()) { + CloseConnection(iter); + iter = Sessions.begin(); + } +} + void LSCPServer::LockRTNotify() { RTNotifyMutex.Lock(); } @@ -979,10 +1171,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()); } @@ -1000,10 +1189,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()); } @@ -1021,10 +1207,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 { @@ -1112,6 +1295,7 @@ for (;iter != parameters.end(); iter++) { if (s != "") s += ","; s += iter->first; + delete iter->second; } result.Add("PARAMETERS", s); } @@ -1136,6 +1320,7 @@ for (;iter != parameters.end(); iter++) { if (s != "") s += ","; s += iter->first; + delete iter->second; } result.Add("PARAMETERS", s); } @@ -1166,6 +1351,7 @@ if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); + delete pParameter; } catch (Exception e) { result.Error(e); @@ -1193,6 +1379,7 @@ if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); + delete pParameter; } catch (Exception e) { result.Error(e); @@ -1659,7 +1846,6 @@ pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params); // Make it with at least one initial port. std::map parameters = pDevice->DeviceParameters(); - parameters["PORTS"]->SetValue("1"); } // Must have a device... if (pDevice == NULL) @@ -1702,10 +1888,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) { @@ -1721,11 +1904,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); @@ -1742,11 +1921,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(); @@ -1866,7 +2041,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); } @@ -1877,14 +2052,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(); } @@ -1892,56 +2064,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); } @@ -2112,11 +2274,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(); @@ -2295,10 +2453,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(); @@ -2313,6 +2468,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. */ @@ -2320,10 +2513,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) { @@ -2389,7 +2579,79 @@ String LSCPServer::GetTotalVoiceCountMax() { dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n")); LSCPResultSet result; - result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES); + result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES); + return result.Produce(); +} + +/** + * Will be called by the parser to return the sampler global maximum + * allowed number of voices. + */ +String LSCPServer::GetGlobalMaxVoices() { + dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n")); + LSCPResultSet result; + result.Add(GLOBAL_MAX_VOICES); + return result.Produce(); +} + +/** + * Will be called by the parser to set the sampler global maximum number of + * voices. + */ +String LSCPServer::SetGlobalMaxVoices(int iVoices) { + dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices)); + LSCPResultSet result; + try { + if (iVoices < 1) throw Exception("Maximum voices may not be less than 1"); + GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.size() > 0) { + std::set::iterator iter = engines.begin(); + std::set::iterator end = engines.end(); + for (; iter != end; ++iter) { + (*iter)->SetMaxVoices(iVoices); + } + } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +/** + * Will be called by the parser to return the sampler global maximum + * allowed number of disk streams. + */ +String LSCPServer::GetGlobalMaxStreams() { + dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n")); + LSCPResultSet result; + result.Add(GLOBAL_MAX_STREAMS); + return result.Produce(); +} + +/** + * Will be called by the parser to set the sampler global maximum number of + * disk streams. + */ +String LSCPServer::SetGlobalMaxStreams(int iStreams) { + dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams)); + LSCPResultSet result; + try { + if (iStreams < 0) throw Exception("Maximum disk streams may not be negative"); + GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.size() > 0) { + std::set::iterator iter = engines.begin(); + std::set::iterator end = engines.end(); + for (; iter != end; ++iter) { + (*iter)->SetMaxDiskStreams(iStreams); + } + } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS)); + } catch (Exception e) { + result.Error(e); + } return result.Produce(); } @@ -2403,7 +2665,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); @@ -2530,6 +2792,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())); @@ -2557,17 +2839,9 @@ throw Exception("Directory is specified"); } #else - struct stat statBuf; - int res = stat(Filename.c_str(), &statBuf); - if (res) { - std::stringstream ss; - ss << "Fail to stat `" << Filename << "`: " << strerror(errno); - throw Exception(ss.str()); - } - - if (S_ISDIR(statBuf.st_mode)) { - throw Exception("Directory is specified"); - } + File f(Filename); + if(!f.Exist()) throw Exception(f.GetErrorMsg()); + if (f.IsDirectory()) throw Exception("Directory is specified"); #endif } @@ -2762,19 +3036,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); } @@ -2950,6 +3224,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; @@ -3080,3 +3392,5 @@ } return result.Produce(); } + +}