/[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 385 by schoenebeck, Thu Feb 17 02:53:45 2005 UTC revision 475 by schoenebeck, Thu Mar 17 23:56:56 2005 UTC
# Line 24  Line 24 
24  #include "lscpserver.h"  #include "lscpserver.h"
25  #include "lscpresultset.h"  #include "lscpresultset.h"
26  #include "lscpevent.h"  #include "lscpevent.h"
27    //#include "../common/global.h"
28    
29  #include "../engines/gig/Engine.h"  #include <fcntl.h>
30    
31    #if HAVE_SQLITE3
32    # include "sqlite3.h"
33    #endif
34    
35    #include "../engines/EngineFactory.h"
36  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
37  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
38    
# Line 53  Mutex LSCPServer::NotifyBufferMutex = Mu Line 60  Mutex LSCPServer::NotifyBufferMutex = Mu
60  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
61  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
62    
63  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(true, false, 0, -4) {
64      this->pSampler = pSampler;      this->pSampler = pSampler;
65      LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS");      LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS");
66      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
# Line 61  LSCPServer::LSCPServer(Sampler* pSampler Line 68  LSCPServer::LSCPServer(Sampler* pSampler
68      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
69      LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO");
70      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
71        hSocket = -1;
72    }
73    
74    LSCPServer::~LSCPServer() {
75        if (hSocket >= 0) close(hSocket);
76  }  }
77    
78  /**  /**
# Line 78  int LSCPServer::WaitUntilInitialized(lon Line 90  int LSCPServer::WaitUntilInitialized(lon
90  }  }
91    
92  int LSCPServer::Main() {  int LSCPServer::Main() {
93      int hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
94      if (hSocket < 0) {      if (hSocket < 0) {
95          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
96          //return -1;          //return -1;
# Line 161  int LSCPServer::Main() { Line 173  int LSCPServer::Main() {
173                                  int dummy; // just a temporary hack to fulfill the restart() function prototype                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
174                                  restart(NULL, dummy); // restart the 'scanner'                                  restart(NULL, dummy); // restart the 'scanner'
175                                  currentSocket = (*iter).hSession;  //a hack                                  currentSocket = (*iter).hSession;  //a hack
176                                    dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
177                                  if ((*iter).bVerbose) { // if echo mode enabled                                  if ((*iter).bVerbose) { // if echo mode enabled
178                                      AnswerClient(bufferedCommands[currentSocket]);                                      AnswerClient(bufferedCommands[currentSocket]);
179                                  }                                  }
# Line 447  String LSCPServer::LoadInstrument(String Line 460  String LSCPServer::LoadInstrument(String
460      try {      try {
461          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
462          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
463          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
464          if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet");
465          if (!pSamplerChannel->GetAudioOutputDevice())          if (!pSamplerChannel->GetAudioOutputDevice())
466              throw LinuxSamplerException("No audio output device connected to sampler channel");              throw LinuxSamplerException("No audio output device connected to sampler channel");
467          if (bBackground) {          if (bBackground) {
468              InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngine);              InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel);
469            }
470            else {
471                // tell the engine channel which instrument to load
472                pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
473                // actually start to load the instrument (blocks until completed)
474                pEngineChannel->LoadInstrument();
475          }          }
         else pEngine->LoadInstrument(Filename.c_str(), uiInstrument);  
476      }      }
477      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
478           result.Error(e);           result.Error(e);
# Line 463  String LSCPServer::LoadInstrument(String Line 481  String LSCPServer::LoadInstrument(String
481  }  }
482    
483  /**  /**
484   * Will be called by the parser to load and deploy an engine.   * Will be called by the parser to assign a sampler engine type to a
485     * sampler channel.
486   */   */
487  String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) {  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
488      dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));      dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
489      LSCPResultSet result;      LSCPResultSet result;
490      try {      try {
         Engine::type_t type;  
         if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig;  
         else throw LinuxSamplerException("Unknown engine type");  
491          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
492          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
493          LockRTNotify();          LockRTNotify();
494          pSamplerChannel->LoadEngine(type);          pSamplerChannel->SetEngineType(EngineName);
495          UnlockRTNotify();          UnlockRTNotify();
496      }      }
497      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 543  String LSCPServer::GetAvailableEngines() Line 559  String LSCPServer::GetAvailableEngines()
559  }  }
560    
561  /**  /**
562   * Will be called by the parser to get descriptions for a particular engine.   * Will be called by the parser to get descriptions for a particular
563     * sampler engine.
564   */   */
565  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
566      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
567      LSCPResultSet result;      LSCPResultSet result;
568      try {      try {
569          if ((EngineName == "GigEngine") || (EngineName == "gig")) {          Engine* pEngine = EngineFactory::Create(EngineName);
570              Engine* pEngine = new LinuxSampler::gig::Engine;          result.Add("DESCRIPTION", pEngine->Description());
571              result.Add("DESCRIPTION", pEngine->Description());          result.Add("VERSION",     pEngine->Version());
572              result.Add("VERSION",     pEngine->Version());          delete pEngine;
             delete pEngine;  
         }  
         else throw LinuxSamplerException("Unknown engine type");  
573      }      }
574      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
575           result.Error(e);           result.Error(e);
# Line 573  String LSCPServer::GetChannelInfo(uint u Line 587  String LSCPServer::GetChannelInfo(uint u
587      try {      try {
588          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
589          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
590          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
591    
592          //Defaults values          //Defaults values
593          String EngineName = "NONE";          String EngineName = "NONE";
# Line 585  String LSCPServer::GetChannelInfo(uint u Line 599  String LSCPServer::GetChannelInfo(uint u
599          int AudioOutputChannels = 0;          int AudioOutputChannels = 0;
600          String AudioRouting;          String AudioRouting;
601    
602          if (pEngine) {          if (pEngineChannel) {
603              EngineName =  pEngine->EngineName();              EngineName          = pEngineChannel->EngineName();
604              AudioOutputChannels = pEngine->Channels();              AudioOutputChannels = pEngineChannel->Channels();
605              Volume = pEngine->Volume();              Volume              = pEngineChannel->Volume();
606              InstrumentStatus = pEngine->InstrumentStatus();              InstrumentStatus    = pEngineChannel->InstrumentStatus();
607              InstrumentIndex = pEngine->InstrumentIndex();              InstrumentIndex     = pEngineChannel->InstrumentIndex();
608              if (InstrumentIndex != -1)              if (InstrumentIndex != -1) {
609              {                  InstrumentFileName = pEngineChannel->InstrumentFileName();
610                  InstrumentFileName = pEngine->InstrumentFileName();                  InstrumentName     = pEngineChannel->InstrumentName();
611                  InstrumentName = pEngine->InstrumentName();              }
612              }              for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
             for (int chan = 0; chan < pEngine->Channels(); chan++) {  
613                  if (AudioRouting != "") AudioRouting += ",";                  if (AudioRouting != "") AudioRouting += ",";
614                  AudioRouting += ToString(pEngine->OutputChannel(chan));                  AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
615              }              }
616          }          }
617    
# Line 636  String LSCPServer::GetVoiceCount(uint ui Line 649  String LSCPServer::GetVoiceCount(uint ui
649      try {      try {
650          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
651          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
652          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
653          if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel");
654          result.Add(pEngine->VoiceCount());          result.Add(pEngineChannel->GetEngine()->VoiceCount());
655      }      }
656      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
657           result.Error(e);           result.Error(e);
# Line 656  String LSCPServer::GetStreamCount(uint u Line 669  String LSCPServer::GetStreamCount(uint u
669      try {      try {
670          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
671          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
672          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
673          if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
674          result.Add(pEngine->DiskStreamCount());          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
675      }      }
676      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
677           result.Error(e);           result.Error(e);
# Line 676  String LSCPServer::GetBufferFill(fill_re Line 689  String LSCPServer::GetBufferFill(fill_re
689      try {      try {
690          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
691          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
692          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
693          if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
694          if (!pEngine->DiskStreamSupported())          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
             result.Add("NA");  
695          else {          else {
696              switch (ResponseType) {              switch (ResponseType) {
697                  case fill_response_bytes:                  case fill_response_bytes:
698                      result.Add(pEngine->DiskStreamBufferFillBytes());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
699                      break;                      break;
700                  case fill_response_percentage:                  case fill_response_percentage:
701                      result.Add(pEngine->DiskStreamBufferFillPercentage());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
702                      break;                      break;
703                  default:                  default:
704                      throw LinuxSamplerException("Unknown fill response type");                      throw LinuxSamplerException("Unknown fill response type");
705              }              }
# Line 1143  String LSCPServer::SetAudioOutputChannel Line 1155  String LSCPServer::SetAudioOutputChannel
1155      try {      try {
1156          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1157          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1158          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1159          if (!pEngine) throw LinuxSamplerException("No engine deployed on sampler channel " + ToString(uiSamplerChannel));          if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1160          if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));          if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1161          pEngine->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);          pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1162      }      }
1163      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1164           result.Error(e);           result.Error(e);
# Line 1323  String LSCPServer::SetVolume(double dVol Line 1335  String LSCPServer::SetVolume(double dVol
1335      try {      try {
1336          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1337          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1338          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1339          if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1340          pEngine->Volume(dVolume);          pEngineChannel->Volume(dVolume);
1341      }      }
1342      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1343           result.Error(e);           result.Error(e);
# Line 1342  String LSCPServer::ResetChannel(uint uiS Line 1354  String LSCPServer::ResetChannel(uint uiS
1354      try {      try {
1355          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1356          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1357          Engine* pEngine = pSamplerChannel->GetEngine();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1358          if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1359          pEngine->Reset();          pEngineChannel->GetEngine()->Reset();
1360      }      }
1361      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1362           result.Error(e);           result.Error(e);
# Line 1388  String LSCPServer::UnsubscribeNotificati Line 1400  String LSCPServer::UnsubscribeNotificati
1400      return result.Produce();      return result.Produce();
1401  }  }
1402    
1403    static int select_callback(void * lscpResultSet, int argc,
1404                            char **argv, char **azColName)
1405    {
1406        LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;
1407        resultSet->Add(argc, argv);
1408        return 0;
1409    }
1410    
1411    String LSCPServer::QueryDatabase(String query) {
1412        LSCPResultSet result;
1413    #if HAVE_SQLITE3
1414        char* zErrMsg = NULL;
1415        sqlite3 *db;
1416        String selectStr = "SELECT " + query;
1417    
1418        int rc = sqlite3_open("linuxsampler.db", &db);
1419        if (rc == SQLITE_OK)
1420        {
1421                rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);
1422        }
1423        if ( rc != SQLITE_OK )
1424        {
1425                result.Error(String(zErrMsg), rc);
1426        }
1427        sqlite3_close(db);
1428    #else
1429        result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);
1430    #endif
1431        return result.Produce();
1432    }
1433    
1434  /**  /**
1435   * Will be called by the parser to enable or disable echo mode; if echo   * Will be called by the parser to enable or disable echo mode; if echo
1436   * mode is enabled, all commands from the client will (immediately) be   * mode is enabled, all commands from the client will (immediately) be

Legend:
Removed from v.385  
changed lines
  Added in v.475

  ViewVC Help
Powered by ViewVC