/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/network/lscpserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1686 by schoenebeck, Thu Feb 14 14:58:50 2008 UTC revision 1763 by iliev, Wed Sep 3 17:18:51 2008 UTC
# Line 21  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24    #include <algorithm>
25    
26  #include "lscpserver.h"  #include "lscpserver.h"
27  #include "lscpresultset.h"  #include "lscpresultset.h"
28  #include "lscpevent.h"  #include "lscpevent.h"
# Line 131  LSCPServer::LSCPServer(Sampler* pSampler Line 133  LSCPServer::LSCPServer(Sampler* pSampler
133      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
134      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
135      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
136        LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
137      hSocket = -1;      hSocket = -1;
138  }  }
139    
# Line 207  void LSCPServer::EventHandler::MidiDevic Line 210  void LSCPServer::EventHandler::MidiDevic
210      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
211  }  }
212    
213    void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
214        pDevice->RemoveMidiPortCountListener(this);
215        for (int i = 0; i < pDevice->PortCount(); ++i)
216            MidiPortToBeRemoved(pDevice->GetPort(i));
217    }
218    
219    void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
220        pDevice->AddMidiPortCountListener(this);
221        for (int i = 0; i < pDevice->PortCount(); ++i)
222            MidiPortAdded(pDevice->GetPort(i));
223    }
224    
225    void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
226        // yet unused
227    }
228    
229    void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
230        for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
231            if ((*iter).pPort == pPort) {
232                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
233                pPort->Disconnect(pMidiListener);
234                deviceMidiListeners.erase(iter);
235                delete pMidiListener;
236                return;
237            }
238        }
239    }
240    
241    void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
242        // find out the device ID
243        std::map<uint, MidiInputDevice*> devices =
244            pParent->pSampler->GetMidiInputDevices();
245        for (
246            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
247            iter != devices.end(); ++iter
248        ) {
249            if (iter->second == pPort->GetDevice()) { // found
250                VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
251                pPort->Connect(pMidiListener);
252                device_midi_listener_entry entry = {
253                    pPort, pMidiListener, iter->first
254                };
255                deviceMidiListeners.push_back(entry);
256                return;
257            }
258        }
259    }
260    
261  void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {  void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
262      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
263  }  }
# Line 373  int LSCPServer::Main() { Line 424  int LSCPServer::Main() {
424              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
425              for (; itEngineChannel != itEnd; ++itEngineChannel) {              for (; itEngineChannel != itEnd; ++itEngineChannel) {
426                  if ((*itEngineChannel)->StatusChanged()) {                  if ((*itEngineChannel)->StatusChanged()) {
427                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index()));
428                  }                  }
429    
430                  for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {                  for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
431                      FxSend* fxs = (*itEngineChannel)->GetFxSend(i);                      FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
432                      if(fxs != NULL && fxs->IsInfoChanged()) {                      if(fxs != NULL && fxs->IsInfoChanged()) {
433                          int chn = (*itEngineChannel)->iSamplerChannelIndex;                          int chn = (*itEngineChannel)->GetSamplerChannel()->Index();
434                          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));                          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
435                          fxs->SetInfoChanged(false);                          fxs->SetInfoChanged(false);
436                      }                      }
# Line 411  int LSCPServer::Main() { Line 462  int LSCPServer::Main() {
462              }              }
463          }          }
464    
465            // check if MIDI data arrived on some MIDI device
466            for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
467                const EventHandler::device_midi_listener_entry entry =
468                    eventHandler.deviceMidiListeners[i];
469                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
470                if (pMidiListener->NotesChanged()) {
471                    for (int iNote = 0; iNote < 128; iNote++) {
472                        if (pMidiListener->NoteChanged(iNote)) {
473                            const bool bActive = pMidiListener->NoteIsActive(iNote);
474                            LSCPServer::SendLSCPNotify(
475                                LSCPEvent(
476                                    LSCPEvent::event_device_midi,
477                                    entry.uiDeviceID,
478                                    entry.pPort->GetPortNumber(),
479                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
480                                    iNote,
481                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
482                                            : pMidiListener->NoteOffVelocity(iNote)
483                                )
484                            );
485                        }
486                    }
487                }
488            }
489    
490          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
491          NotifyBufferMutex.Lock();          NotifyBufferMutex.Lock();
492          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
# Line 1944  String LSCPServer::GetMidiInstrumentMapp Line 2020  String LSCPServer::GetMidiInstrumentMapp
2020      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2021      LSCPResultSet result;      LSCPResultSet result;
2022      try {      try {
2023          result.Add(MidiInstrumentMapper::Entries(MidiMapID).size());          result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID));
2024      } catch (Exception e) {      } catch (Exception e) {
2025          result.Error(e);          result.Error(e);
2026      }      }
# Line 1955  String LSCPServer::GetMidiInstrumentMapp Line 2031  String LSCPServer::GetMidiInstrumentMapp
2031  String LSCPServer::GetAllMidiInstrumentMappings() {  String LSCPServer::GetAllMidiInstrumentMappings() {
2032      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2033      LSCPResultSet result;      LSCPResultSet result;
2034      std::vector<int> maps = MidiInstrumentMapper::Maps();      try {
2035      int totalMappings = 0;          result.Add(MidiInstrumentMapper::GetInstrumentCount());
2036      for (int i = 0; i < maps.size(); i++) {      } catch (Exception e) {
2037          try {          result.Error(e);
             totalMappings += MidiInstrumentMapper::Entries(maps[i]).size();  
         } catch (Exception e) { /*NOOP*/ }  
2038      }      }
     result.Add(totalMappings);  
2039      return result.Produce();      return result.Produce();
2040  }  }
2041    
# Line 1970  String LSCPServer::GetMidiInstrumentMapp Line 2043  String LSCPServer::GetMidiInstrumentMapp
2043      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2044      LSCPResultSet result;      LSCPResultSet result;
2045      try {      try {
2046          midi_prog_index_t idx;          MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg);
2047          idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;          // convert the filename into the correct encoding as defined for LSCP
2048          idx.midi_bank_lsb = MidiBank & 0x7f;          // (especially in terms of special characters -> escape sequences)
         idx.midi_prog     = MidiProg;  
   
         std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);  
         std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::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)  
2049  #if WIN32  #if WIN32
2050              const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp();          const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp();
2051  #else  #else
2052              // assuming POSIX          // assuming POSIX
2053              const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp();          const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp();
2054  #endif  #endif
2055    
2056              result.Add("NAME", _escapeLscpResponse(iter->second.Name));          result.Add("NAME", _escapeLscpResponse(entry.Name));
2057              result.Add("ENGINE_NAME", iter->second.EngineName);          result.Add("ENGINE_NAME", entry.EngineName);
2058              result.Add("INSTRUMENT_FILE", instrumentFileName);          result.Add("INSTRUMENT_FILE", instrumentFileName);
2059              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);          result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex);
2060              String instrumentName;          String instrumentName;
2061              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);          Engine* pEngine = EngineFactory::Create(entry.EngineName);
2062              if (pEngine) {          if (pEngine) {
2063                  if (pEngine->GetInstrumentManager()) {              if (pEngine->GetInstrumentManager()) {
2064                      InstrumentManager::instrument_id_t instrID;                  InstrumentManager::instrument_id_t instrID;
2065                      instrID.FileName = iter->second.InstrumentFile;                  instrID.FileName = entry.InstrumentFile;
2066                      instrID.Index    = iter->second.InstrumentIndex;                  instrID.Index    = entry.InstrumentIndex;
2067                      instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);                  instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
                 }  
                 EngineFactory::Destroy(pEngine);  
2068              }              }
2069              result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));              EngineFactory::Destroy(pEngine);
2070              switch (iter->second.LoadMode) {          }
2071                  case MidiInstrumentMapper::ON_DEMAND:          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2072                      result.Add("LOAD_MODE", "ON_DEMAND");          switch (entry.LoadMode) {
2073                      break;              case MidiInstrumentMapper::ON_DEMAND:
2074                  case MidiInstrumentMapper::ON_DEMAND_HOLD:                  result.Add("LOAD_MODE", "ON_DEMAND");
2075                      result.Add("LOAD_MODE", "ON_DEMAND_HOLD");                  break;
2076                      break;              case MidiInstrumentMapper::ON_DEMAND_HOLD:
2077                  case MidiInstrumentMapper::PERSISTENT:                  result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2078                      result.Add("LOAD_MODE", "PERSISTENT");                  break;
2079                      break;              case MidiInstrumentMapper::PERSISTENT:
2080                  default:                  result.Add("LOAD_MODE", "PERSISTENT");
2081                      throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");                  break;
2082              }              default:
2083              result.Add("VOLUME", iter->second.Volume);                  throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2084          }          }
2085            result.Add("VOLUME", entry.Volume);
2086      } catch (Exception e) {      } catch (Exception e) {
2087          result.Error(e);          result.Error(e);
2088      }      }
# Line 2481  String LSCPServer::SetGlobalVolume(doubl Line 2544  String LSCPServer::SetGlobalVolume(doubl
2544      LSCPResultSet result;      LSCPResultSet result;
2545      try {      try {
2546          if (dVolume < 0) throw Exception("Volume may not be negative");          if (dVolume < 0) throw Exception("Volume may not be negative");
2547          GLOBAL_VOLUME = dVolume; // see common/global.cpp          GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
2548          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
2549      } catch (Exception e) {      } catch (Exception e) {
2550          result.Error(e);          result.Error(e);
# Line 3022  String LSCPServer::SetDbInstrumentDescri Line 3085  String LSCPServer::SetDbInstrumentDescri
3085      } catch (Exception e) {      } catch (Exception e) {
3086           result.Error(e);           result.Error(e);
3087      }      }
3088    #else
3089        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3090    #endif
3091        return result.Produce();
3092    }
3093    
3094    String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) {
3095        dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str()));
3096        LSCPResultSet result;
3097    #if HAVE_SQLITE3
3098        try {
3099            InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath);
3100        } catch (Exception e) {
3101             result.Error(e);
3102        }
3103    #else
3104        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3105    #endif
3106        return result.Produce();
3107    }
3108    
3109    String LSCPServer::FindLostDbInstrumentFiles() {
3110        dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n"));
3111        LSCPResultSet result;
3112    #if HAVE_SQLITE3
3113        try {
3114            String list;
3115            StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles();
3116    
3117            for (int i = 0; i < pLostFiles->size(); i++) {
3118                if (list != "") list += ",";
3119                list += "'" + pLostFiles->at(i) + "'";
3120            }
3121    
3122            result.Add(list);
3123        } catch (Exception e) {
3124             result.Error(e);
3125        }
3126  #else  #else
3127      result.Error(String(DOESNT_HAVE_SQLITE3), 0);      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3128  #endif  #endif

Legend:
Removed from v.1686  
changed lines
  Added in v.1763

  ViewVC Help
Powered by ViewVC