/[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 1537 by senoner, Mon Dec 3 18:30:47 2007 UTC revision 1686 by schoenebeck, Thu Feb 14 14:58:50 2008 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 - 2007 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 101  Mutex LSCPServer::NotifyBufferMutex = Mu Line 101  Mutex LSCPServer::NotifyBufferMutex = Mu
101  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
102  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
103    
104  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) {
105      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
106      SocketAddress.sin_addr.s_addr = addr;      SocketAddress.sin_addr.s_addr = addr;
107      SocketAddress.sin_port        = port;      SocketAddress.sin_port        = port;
# Line 127  LSCPServer::LSCPServer(Sampler* pSampler Line 127  LSCPServer::LSCPServer(Sampler* pSampler
127      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
128      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
129      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
130        LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
131      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
132      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
133        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
134      hSocket = -1;      hSocket = -1;
135  }  }
136    
# Line 140  LSCPServer::~LSCPServer() { Line 142  LSCPServer::~LSCPServer() {
142  #endif  #endif
143  }  }
144    
145    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
146        this->pParent = pParent;
147    }
148    
149    LSCPServer::EventHandler::~EventHandler() {
150        std::vector<midi_listener_entry> l = channelMidiListeners;
151        channelMidiListeners.clear();
152        for (int i = 0; i < l.size(); i++)
153            delete l[i].pMidiListener;
154    }
155    
156  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
157      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
158  }  }
159    
160    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
161        pChannel->AddEngineChangeListener(this);
162    }
163    
164    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
165        if (!pChannel->GetEngineChannel()) return;
166        EngineToBeChanged(pChannel->Index());
167    }
168    
169    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
170        SamplerChannel* pSamplerChannel =
171            pParent->pSampler->GetSamplerChannel(ChannelId);
172        if (!pSamplerChannel) return;
173        EngineChannel* pEngineChannel =
174            pSamplerChannel->GetEngineChannel();
175        if (!pEngineChannel) return;
176        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
177            if ((*iter).pEngineChannel == pEngineChannel) {
178                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
179                pEngineChannel->Disconnect(pMidiListener);
180                channelMidiListeners.erase(iter);
181                delete pMidiListener;
182                return;
183            }
184        }
185    }
186    
187    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
188        SamplerChannel* pSamplerChannel =
189            pParent->pSampler->GetSamplerChannel(ChannelId);
190        if (!pSamplerChannel) return;
191        EngineChannel* pEngineChannel =
192            pSamplerChannel->GetEngineChannel();
193        if (!pEngineChannel) return;
194        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
195        pEngineChannel->Connect(pMidiListener);
196        midi_listener_entry entry = {
197            pSamplerChannel, pEngineChannel, pMidiListener
198        };
199        channelMidiListeners.push_back(entry);
200    }
201    
202  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
203      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
204  }  }
# Line 188  void LSCPServer::EventHandler::TotalVoic Line 243  void LSCPServer::EventHandler::TotalVoic
243      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
244  }  }
245    
246    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
247        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
248    }
249    
250  #if HAVE_SQLITE3  #if HAVE_SQLITE3
251  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
252      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
# Line 284  int LSCPServer::Main() { Line 343  int LSCPServer::Main() {
343      pSampler->AddVoiceCountListener(&eventHandler);      pSampler->AddVoiceCountListener(&eventHandler);
344      pSampler->AddStreamCountListener(&eventHandler);      pSampler->AddStreamCountListener(&eventHandler);
345      pSampler->AddBufferFillListener(&eventHandler);      pSampler->AddBufferFillListener(&eventHandler);
346        pSampler->AddTotalStreamCountListener(&eventHandler);
347      pSampler->AddTotalVoiceCountListener(&eventHandler);      pSampler->AddTotalVoiceCountListener(&eventHandler);
348      pSampler->AddFxSendCountListener(&eventHandler);      pSampler->AddFxSendCountListener(&eventHandler);
349      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
# Line 303  int LSCPServer::Main() { Line 363  int LSCPServer::Main() {
363      timeval timeout;      timeval timeout;
364    
365      while (true) {      while (true) {
366            #if CONFIG_PTHREAD_TESTCANCEL
367                    TestCancel();
368            #endif
369          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
370          {          {
371              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
# Line 324  int LSCPServer::Main() { Line 387  int LSCPServer::Main() {
387              }              }
388          }          }
389    
390            // check if MIDI data arrived on some engine channel
391            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
392                const EventHandler::midi_listener_entry entry =
393                    eventHandler.channelMidiListeners[i];
394                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
395                if (pMidiListener->NotesChanged()) {
396                    for (int iNote = 0; iNote < 128; iNote++) {
397                        if (pMidiListener->NoteChanged(iNote)) {
398                            const bool bActive = pMidiListener->NoteIsActive(iNote);
399                            LSCPServer::SendLSCPNotify(
400                                LSCPEvent(
401                                    LSCPEvent::event_channel_midi,
402                                    entry.pSamplerChannel->Index(),
403                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
404                                    iNote,
405                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
406                                            : pMidiListener->NoteOffVelocity(iNote)
407                                )
408                            );
409                        }
410                    }
411                }
412            }
413    
414          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
415          NotifyBufferMutex.Lock();          NotifyBufferMutex.Lock();
416          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 440  void LSCPServer::CloseConnection( std::v Line 527  void LSCPServer::CloseConnection( std::v
527          NotifyMutex.Unlock();          NotifyMutex.Unlock();
528  }  }
529    
530    void LSCPServer::LockRTNotify() {
531        RTNotifyMutex.Lock();
532    }
533    
534    void LSCPServer::UnlockRTNotify() {
535        RTNotifyMutex.Unlock();
536    }
537    
538  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
539          int subs = 0;          int subs = 0;
540          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 2347  String LSCPServer::GetServerInfo() { Line 2442  String LSCPServer::GetServerInfo() {
2442  }  }
2443    
2444  /**  /**
2445     * Will be called by the parser to return the current number of all active streams.
2446     */
2447    String LSCPServer::GetTotalStreamCount() {
2448        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2449        LSCPResultSet result;
2450        result.Add(pSampler->GetDiskStreamCount());
2451        return result.Produce();
2452    }
2453    
2454    /**
2455   * Will be called by the parser to return the current number of all active voices.   * Will be called by the parser to return the current number of all active voices.
2456   */   */
2457  String LSCPServer::GetTotalVoiceCount() {  String LSCPServer::GetTotalVoiceCount() {
# Line 2529  void LSCPServer::VerifyFile(String Filen Line 2634  void LSCPServer::VerifyFile(String Filen
2634      if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {      if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2635          throw Exception("Directory is specified");          throw Exception("Directory is specified");
2636      }      }
2637      #else          #else
2638      struct stat statBuf;      struct stat statBuf;
2639      int res = stat(Filename.c_str(), &statBuf);      int res = stat(Filename.c_str(), &statBuf);
2640      if (res) {      if (res) {

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

  ViewVC Help
Powered by ViewVC