/[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 1897 by persson, Sun May 10 09:31:51 2009 UTC revision 2516 by schoenebeck, Thu Feb 6 21:11:23 2014 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 - 2009 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2014 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 43  Line 43 
43  #include "../engines/EngineChannelFactory.h"  #include "../engines/EngineChannelFactory.h"
44  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
45  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
46    #include "../effects/EffectFactory.h"
47    
48  namespace LinuxSampler {  namespace LinuxSampler {
49    
50    String lscpParserProcessShellInteraction(String& line, yyparse_param_t* param);
51    
52  /**  /**
53   * Returns a copy of the given string where all special characters are   * Returns a copy of the given string where all special characters are
54   * replaced by LSCP escape sequences ("\xHH"). This function shall be used   * replaced by LSCP escape sequences ("\xHH"). This function shall be used
# Line 96  static String _escapeLscpResponse(String Line 99  static String _escapeLscpResponse(String
99   */   */
100  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
101  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
102  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();  std::vector<yyparse_param_t> LSCPServer::Sessions;
103  std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();  std::vector<yyparse_param_t>::iterator itCurrentSession;
104  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies;
105  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands;
106  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions;
107  Mutex LSCPServer::NotifyMutex = Mutex();  Mutex LSCPServer::NotifyMutex;
108  Mutex LSCPServer::NotifyBufferMutex = Mutex();  Mutex LSCPServer::NotifyBufferMutex;
109  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex;
110  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex;
111    
112  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
113      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
# Line 137  LSCPServer::LSCPServer(Sampler* pSampler Line 140  LSCPServer::LSCPServer(Sampler* pSampler
140      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
141      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
142      LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");      LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
143        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_instance_count, "EFFECT_INSTANCE_COUNT");
144        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_instance_info, "EFFECT_INSTANCE_INFO");
145        LSCPEvent::RegisterEvent(LSCPEvent::event_send_fx_chain_count, "SEND_EFFECT_CHAIN_COUNT");
146        LSCPEvent::RegisterEvent(LSCPEvent::event_send_fx_chain_info, "SEND_EFFECT_CHAIN_INFO");
147      hSocket = -1;      hSocket = -1;
148  }  }
149    
# Line 442  int LSCPServer::Main() { Line 449  int LSCPServer::Main() {
449          #endif          #endif
450          // 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
451          {          {
452                LockGuard lock(EngineChannelFactory::EngineChannelsMutex);
453              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
454              std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();              std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
455              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
# Line 510  int LSCPServer::Main() { Line 518  int LSCPServer::Main() {
518              }              }
519          }          }
520    
521          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
522          NotifyBufferMutex.Lock();          {
523          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {              LockGuard lock(NotifyBufferMutex);
524                for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
525  #ifdef MSG_NOSIGNAL  #ifdef MSG_NOSIGNAL
526                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
527  #else  #else
528                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
529  #endif  #endif
530          }              }
531          bufferedNotifies.clear();              bufferedNotifies.clear();
532          NotifyBufferMutex.Unlock();          }
533    
534          fd_set selectSet = fdSet;          fd_set selectSet = fdSet;
535          timeout.tv_sec  = 0;          timeout.tv_sec  = 0;
# Line 585  int LSCPServer::Main() { Line 594  int LSCPServer::Main() {
594          //Something was selected and it was not the hSocket, so it must be some command(s) coming.          //Something was selected and it was not the hSocket, so it must be some command(s) coming.
595          for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {          for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
596                  if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?                  if (FD_ISSET((*iter).hSession, &selectSet)) {   //Was it this socket?
597                            currentSocket = (*iter).hSession;  //a hack
598                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?                          if (GetLSCPCommand(iter)) {     //Have we read the entire command?
599                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));                                  dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
600                                  int dummy; // just a temporary hack to fulfill the restart() function prototype                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
601                                  restart(NULL, dummy); // restart the 'scanner'                                  restart(NULL, dummy); // restart the 'scanner'
                                 currentSocket = (*iter).hSession;  //a hack  
602                                  itCurrentSession = iter; // another hack                                  itCurrentSession = iter; // another hack
603                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
604                                  if ((*iter).bVerbose) { // if echo mode enabled                                  if ((*iter).bVerbose) { // if echo mode enabled
# Line 603  int LSCPServer::Main() { Line 612  int LSCPServer::Main() {
612                                          CloseConnection(iter);                                          CloseConnection(iter);
613                                  }                                  }
614                          }                          }
615                            currentSocket = -1;     //continuation of a hack
616                          //socket may have been closed, iter may be invalid, get out of the loop for now.                          //socket may have been closed, iter may be invalid, get out of the loop for now.
617                          //we'll be back if there is data.                          //we'll be back if there is data.
618                          break;                          break;
# Line 617  void LSCPServer::CloseConnection( std::v Line 627  void LSCPServer::CloseConnection( std::v
627          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
628          Sessions.erase(iter);          Sessions.erase(iter);
629          FD_CLR(socket,  &fdSet);          FD_CLR(socket,  &fdSet);
630          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)          {
631          for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {              LockGuard lock(SubscriptionMutex);
632                  iter->second.remove(socket);              // Must unsubscribe this socket from all events (if any)
633          }              for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
634          SubscriptionMutex.Unlock();                  iter->second.remove(socket);
635          NotifyMutex.Lock();              }
636            }
637            LockGuard lock(NotifyMutex);
638          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
639          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
640          #if defined(WIN32)          #if defined(WIN32)
# Line 630  void LSCPServer::CloseConnection( std::v Line 642  void LSCPServer::CloseConnection( std::v
642          #else          #else
643          close(socket);          close(socket);
644          #endif          #endif
         NotifyMutex.Unlock();  
645  }  }
646    
647  void LSCPServer::CloseAllConnections() {  void LSCPServer::CloseAllConnections() {
# Line 641  void LSCPServer::CloseAllConnections() { Line 652  void LSCPServer::CloseAllConnections() {
652      }      }
653  }  }
654    
 void LSCPServer::LockRTNotify() {  
     RTNotifyMutex.Lock();  
 }  
   
 void LSCPServer::UnlockRTNotify() {  
     RTNotifyMutex.Unlock();  
 }  
   
655  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
656          int subs = 0;          int subs = 0;
657          SubscriptionMutex.Lock();          LockGuard lock(SubscriptionMutex);
658          for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();          for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
659                          iter != events.end(); iter++)                          iter != events.end(); iter++)
660          {          {
661                  subs += eventSubscriptions.count(*iter);                  subs += eventSubscriptions.count(*iter);
662          }          }
         SubscriptionMutex.Unlock();  
663          return subs;          return subs;
664  }  }
665    
666  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
667          SubscriptionMutex.Lock();          LockGuard lock(SubscriptionMutex);
668          if (eventSubscriptions.count(event.GetType()) == 0) {          if (eventSubscriptions.count(event.GetType()) == 0) {
669                  SubscriptionMutex.Unlock();     //Nobody is subscribed to this event                  // Nobody is subscribed to this event
670                  return;                  return;
671          }          }
672          std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();          std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
# Line 690  void LSCPServer::SendLSCPNotify( LSCPEve Line 692  void LSCPServer::SendLSCPNotify( LSCPEve
692                          }                          }
693                  }                  }
694          }          }
         SubscriptionMutex.Unlock();  
695  }  }
696    
697  extern int GetLSCPCommand( void *buf, int max_size ) {  extern int GetLSCPCommand( void *buf, int max_size ) {
# Line 741  bool LSCPServer::GetLSCPCommand( std::ve Line 742  bool LSCPServer::GetLSCPCommand( std::ve
742                                  bufferedCommands[socket] += "\r\n";                                  bufferedCommands[socket] += "\r\n";
743                                  return true; //Complete command was read                                  return true; //Complete command was read
744                          }                          }
745                          bufferedCommands[socket] += c;                          // backspace character - should only happen with shell
746                            if (c == '\b') {
747                                    if (!bufferedCommands[socket].empty()) {
748                                            bufferedCommands[socket] = bufferedCommands[socket].substr(
749                                                    0, bufferedCommands[socket].length() - 1
750                                            );
751                                    }
752                            } else bufferedCommands[socket] += c;
753                            // only if the other side is the LSCP shell application:
754                            // check the current (incomplete) command line for syntax errors,
755                            // possible completions and report everything back to the shell
756                            if ((*iter).bShellInteract || (*iter).bShellAutoCorrect) {
757                                    String s = lscpParserProcessShellInteraction(bufferedCommands[socket], &(*iter));
758                                    if (!s.empty() && (*iter).bShellInteract) AnswerClient(s + "\n");
759                            }
760                  }                  }
761                  #if defined(WIN32)                  #if defined(WIN32)
762                  if (result == SOCKET_ERROR) {                  if (result == SOCKET_ERROR) {
# Line 803  bool LSCPServer::GetLSCPCommand( std::ve Line 818  bool LSCPServer::GetLSCPCommand( std::ve
818   * @param ReturnMessage - message that will be send to the client   * @param ReturnMessage - message that will be send to the client
819   */   */
820  void LSCPServer::AnswerClient(String ReturnMessage) {  void LSCPServer::AnswerClient(String ReturnMessage) {
821      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage='%s')", ReturnMessage.c_str()));
822      if (currentSocket != -1) {      if (currentSocket != -1) {
823              NotifyMutex.Lock();              LockGuard lock(NotifyMutex);
824    
825            // just if other side is LSCP shell: in case respose is a multi-line
826            // one, then inform client about it before sending the actual mult-line
827            // response
828            if (GetCurrentYaccSession()->bShellInteract) {
829                // check if this is a multi-line response
830                int n = 0;
831                for (int i = 0; i < ReturnMessage.size(); ++i)
832                    if (ReturnMessage[i] == '\n') ++n;
833                if (n >= 2) {
834                    dmsg(2,("LSCP Shell <- expect mult-line response\n"));
835                    String s = LSCP_SHK_EXPECT_MULTI_LINE "\r\n";
836    #ifdef MSG_NOSIGNAL
837                    send(currentSocket, s.c_str(), s.size(), MSG_NOSIGNAL);
838    #else
839                    send(currentSocket, s.c_str(), s.size(), 0);
840    #endif                
841                }
842            }
843    
844  #ifdef MSG_NOSIGNAL  #ifdef MSG_NOSIGNAL
845              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
846  #else  #else
847              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
848  #endif  #endif
             NotifyMutex.Unlock();  
849      }      }
850  }  }
851    
# Line 961  String LSCPServer::SetEngineType(String Line 995  String LSCPServer::SetEngineType(String
995      try {      try {
996          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
997          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
998          LockRTNotify();          LockGuard lock(RTNotifyMutex);
999          pSamplerChannel->SetEngineType(EngineName);          pSamplerChannel->SetEngineType(EngineName);
1000          if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);          if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
         UnlockRTNotify();  
1001      }      }
1002      catch (Exception e) {      catch (Exception e) {
1003           result.Error(e);           result.Error(e);
# Line 1004  String LSCPServer::ListChannels() { Line 1037  String LSCPServer::ListChannels() {
1037   */   */
1038  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
1039      dmsg(2,("LSCPServer: AddChannel()\n"));      dmsg(2,("LSCPServer: AddChannel()\n"));
1040      LockRTNotify();      SamplerChannel* pSamplerChannel;
1041      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();      {
1042      UnlockRTNotify();          LockGuard lock(RTNotifyMutex);
1043            pSamplerChannel = pSampler->AddSamplerChannel();
1044        }
1045      LSCPResultSet result(pSamplerChannel->Index());      LSCPResultSet result(pSamplerChannel->Index());
1046      return result.Produce();      return result.Produce();
1047  }  }
# Line 1017  String LSCPServer::AddChannel() { Line 1052  String LSCPServer::AddChannel() {
1052  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
1053      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
1054      LSCPResultSet result;      LSCPResultSet result;
1055      LockRTNotify();      {
1056      pSampler->RemoveSamplerChannel(uiSamplerChannel);          LockGuard lock(RTNotifyMutex);
1057      UnlockRTNotify();          pSampler->RemoveSamplerChannel(uiSamplerChannel);
1058        }
1059      return result.Produce();      return result.Produce();
1060  }  }
1061    
# Line 1062  String LSCPServer::ListAvailableEngines( Line 1098  String LSCPServer::ListAvailableEngines(
1098  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
1099      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
1100      LSCPResultSet result;      LSCPResultSet result;
1101      LockRTNotify();      {
1102      try {          LockGuard lock(RTNotifyMutex);
1103          Engine* pEngine = EngineFactory::Create(EngineName);          try {
1104          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));              Engine* pEngine = EngineFactory::Create(EngineName);
1105          result.Add("VERSION",     pEngine->Version());              result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1106          EngineFactory::Destroy(pEngine);              result.Add("VERSION",     pEngine->Version());
1107      }              EngineFactory::Destroy(pEngine);
1108      catch (Exception e) {          }
1109           result.Error(e);          catch (Exception e) {
1110                result.Error(e);
1111            }
1112      }      }
     UnlockRTNotify();  
1113      return result.Produce();      return result.Produce();
1114  }  }
1115    
# Line 1721  String LSCPServer::SetAudioOutputChannel Line 1758  String LSCPServer::SetAudioOutputChannel
1758  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1759      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1760      LSCPResultSet result;      LSCPResultSet result;
1761      LockRTNotify();      {
1762            LockGuard lock(RTNotifyMutex);
1763            try {
1764                SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1765                if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1766                std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1767                if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));
1768                AudioOutputDevice* pDevice = devices[AudioDeviceId];
1769                pSamplerChannel->SetAudioOutputDevice(pDevice);
1770            }
1771            catch (Exception e) {
1772                result.Error(e);
1773            }
1774        }
1775        return result.Produce();
1776    }
1777    
1778    String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1779        dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1780        LSCPResultSet result;
1781        {
1782            LockGuard lock(RTNotifyMutex);
1783            try {
1784                SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1785                if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1786                // Driver type name aliasing...
1787                if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1788                if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1789                // Check if there's one audio output device already created
1790                // for the intended audio driver type (AudioOutputDriver)...
1791                AudioOutputDevice *pDevice = NULL;
1792                std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1793                std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1794                for (; iter != devices.end(); iter++) {
1795                    if ((iter->second)->Driver() == AudioOutputDriver) {
1796                        pDevice = iter->second;
1797                        break;
1798                    }
1799                }
1800                // If it doesn't exist, create a new one with default parameters...
1801                if (pDevice == NULL) {
1802                    std::map<String,String> params;
1803                    pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1804                }
1805                // Must have a device...
1806                if (pDevice == NULL)
1807                    throw Exception("Internal error: could not create audio output device.");
1808                // Set it as the current channel device...
1809                pSamplerChannel->SetAudioOutputDevice(pDevice);
1810            }
1811            catch (Exception e) {
1812                result.Error(e);
1813            }
1814        }
1815        return result.Produce();
1816    }
1817    
1818    String LSCPServer::AddChannelMidiInput(uint uiSamplerChannel, uint MIDIDeviceId, uint MIDIPort) {
1819        dmsg(2,("LSCPServer: AddChannelMidiInput(uiSamplerChannel=%d, MIDIDeviceId=%d, MIDIPort=%d)\n",uiSamplerChannel,MIDIDeviceId,MIDIPort));
1820        LSCPResultSet result;
1821      try {      try {
1822          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1823          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1824          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();  
1825          if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1826          AudioOutputDevice* pDevice = devices[AudioDeviceId];          if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1827          pSamplerChannel->SetAudioOutputDevice(pDevice);          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1828    
1829            MidiInputPort* pPort = pDevice->GetPort(MIDIPort);
1830            if (!pPort) throw Exception("There is no MIDI input port with index " + ToString(MIDIPort) + " on MIDI input device with index " + ToString(MIDIDeviceId));
1831    
1832            pSamplerChannel->Connect(pPort);
1833        } catch (Exception e) {
1834            result.Error(e);
1835      }      }
1836      catch (Exception e) {      return result.Produce();
1837           result.Error(e);  }
1838    
1839    String LSCPServer::RemoveChannelMidiInput(uint uiSamplerChannel) {
1840        dmsg(2,("LSCPServer: RemoveChannelMidiInput(uiSamplerChannel=%d)\n",uiSamplerChannel));
1841        LSCPResultSet result;
1842        try {
1843            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1844            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1845            pSamplerChannel->DisconnectAllMidiInputPorts();
1846        } catch (Exception e) {
1847            result.Error(e);
1848      }      }
     UnlockRTNotify();  
1849      return result.Produce();      return result.Produce();
1850  }  }
1851    
1852  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::RemoveChannelMidiInput(uint uiSamplerChannel, uint MIDIDeviceId) {
1853      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));      dmsg(2,("LSCPServer: RemoveChannelMidiInput(uiSamplerChannel=%d, MIDIDeviceId=%d)\n",uiSamplerChannel,MIDIDeviceId));
1854      LSCPResultSet result;      LSCPResultSet result;
     LockRTNotify();  
1855      try {      try {
1856          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1857          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1858          // Driver type name aliasing...  
1859          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1860          if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";          if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1861          // Check if there's one audio output device already created          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1862          // for the intended audio driver type (AudioOutputDriver)...          
1863          AudioOutputDevice *pDevice = NULL;          std::vector<MidiInputPort*> vPorts = pSamplerChannel->GetMidiInputPorts();
1864          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          for (int i = 0; i < vPorts.size(); ++i)
1865          std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();              if (vPorts[i]->GetDevice() == pDevice)
1866          for (; iter != devices.end(); iter++) {                  pSamplerChannel->Disconnect(vPorts[i]);
1867              if ((iter->second)->Driver() == AudioOutputDriver) {  
1868                  pDevice = iter->second;      } catch (Exception e) {
1869                  break;          result.Error(e);
             }  
         }  
         // If it doesn't exist, create a new one with default parameters...  
         if (pDevice == NULL) {  
             std::map<String,String> params;  
             pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);  
         }  
         // Must have a device...  
         if (pDevice == NULL)  
             throw Exception("Internal error: could not create audio output device.");  
         // Set it as the current channel device...  
         pSamplerChannel->SetAudioOutputDevice(pDevice);  
1870      }      }
1871      catch (Exception e) {      return result.Produce();
1872           result.Error(e);  }
1873    
1874    String LSCPServer::RemoveChannelMidiInput(uint uiSamplerChannel, uint MIDIDeviceId, uint MIDIPort) {
1875        dmsg(2,("LSCPServer: RemoveChannelMidiInput(uiSamplerChannel=%d, MIDIDeviceId=%d, MIDIPort=%d)\n",uiSamplerChannel,MIDIDeviceId,MIDIPort));
1876        LSCPResultSet result;
1877        try {
1878            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1879            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1880    
1881            std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1882            if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1883            MidiInputDevice* pDevice = devices[MIDIDeviceId];
1884    
1885            MidiInputPort* pPort = pDevice->GetPort(MIDIPort);
1886            if (!pPort) throw Exception("There is no MIDI input port with index " + ToString(MIDIPort) + " on MIDI input device with index " + ToString(MIDIDeviceId));
1887    
1888            pSamplerChannel->Disconnect(pPort);
1889        } catch (Exception e) {
1890            result.Error(e);
1891        }
1892        return result.Produce();
1893    }
1894    
1895    String LSCPServer::ListChannelMidiInputs(uint uiSamplerChannel) {
1896        dmsg(2,("LSCPServer: ListChannelMidiInputs(uiSamplerChannel=%d)\n",uiSamplerChannel));
1897        LSCPResultSet result;
1898        try {
1899            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1900            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1901            std::vector<MidiInputPort*> vPorts = pSamplerChannel->GetMidiInputPorts();
1902    
1903            String s;
1904            for (int i = 0; i < vPorts.size(); ++i) {
1905                const int iDeviceID = vPorts[i]->GetDevice()->MidiInputDeviceID();
1906                const int iPortNr   = vPorts[i]->GetPortNumber();
1907                if (s.size()) s += ",";
1908                s += "{" + ToString(iDeviceID) + ","
1909                         + ToString(iPortNr) + "}";
1910            }
1911            result.Add(s);
1912        } catch (Exception e) {
1913            result.Error(e);
1914      }      }
     UnlockRTNotify();  
1915      return result.Produce();      return result.Produce();
1916  }  }
1917    
# Line 2382  String LSCPServer::GetFxSendInfo(uint ui Line 2521  String LSCPServer::GetFxSendInfo(uint ui
2521              AudioRouting += ToString(pFxSend->DestinationChannel(chan));              AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2522          }          }
2523    
2524            const String sEffectRouting =
2525                (pFxSend->DestinationEffectChain() >= 0 && pFxSend->DestinationEffectChainPosition() >= 0)
2526                    ? ToString(pFxSend->DestinationEffectChain()) + "," + ToString(pFxSend->DestinationEffectChainPosition())
2527                    : "NONE";
2528    
2529          // success          // success
2530          result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));          result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2531          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2532          result.Add("LEVEL", ToString(pFxSend->Level()));          result.Add("LEVEL", ToString(pFxSend->Level()));
2533          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2534            result.Add("EFFECT", sEffectRouting);
2535      } catch (Exception e) {      } catch (Exception e) {
2536          result.Error(e);          result.Error(e);
2537      }      }
# Line 2449  String LSCPServer::SetFxSendLevel(uint u Line 2594  String LSCPServer::SetFxSendLevel(uint u
2594      return result.Produce();      return result.Produce();
2595  }  }
2596    
2597    String LSCPServer::SetFxSendEffect(uint uiSamplerChannel, uint FxSendID, int iSendEffectChain, int iEffectChainPosition) {
2598        dmsg(2,("LSCPServer: SetFxSendEffect(%d,%d)\n", iSendEffectChain, iEffectChainPosition));
2599        LSCPResultSet result;
2600        try {
2601            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2602    
2603            pFxSend->SetDestinationEffect(iSendEffectChain, iEffectChainPosition);
2604            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2605        } catch (Exception e) {
2606            result.Error(e);
2607        }
2608        return result.Produce();
2609    }
2610    
2611    String LSCPServer::GetAvailableEffects() {
2612        dmsg(2,("LSCPServer: GetAvailableEffects()\n"));
2613        LSCPResultSet result;
2614        try {
2615            int n = EffectFactory::AvailableEffectsCount();
2616            result.Add(n);
2617        }
2618        catch (Exception e) {
2619            result.Error(e);
2620        }
2621        return result.Produce();
2622    }
2623    
2624    String LSCPServer::ListAvailableEffects() {
2625        dmsg(2,("LSCPServer: ListAvailableEffects()\n"));
2626        LSCPResultSet result;
2627        String list;
2628        try {
2629            //FIXME: for now we simply enumerate from 0 .. EffectFactory::AvailableEffectsCount() here, in future we should use unique IDs for effects during the whole sampler session. This issue comes into game when the user forces a reload of available effect plugins
2630            int n = EffectFactory::AvailableEffectsCount();
2631            for (int i = 0; i < n; i++) {
2632                if (i) list += ",";
2633                list += ToString(i);
2634            }
2635        }
2636        catch (Exception e) {
2637            result.Error(e);
2638        }
2639        result.Add(list);
2640        return result.Produce();
2641    }
2642    
2643    String LSCPServer::GetEffectInfo(int iEffectIndex) {
2644        dmsg(2,("LSCPServer: GetEffectInfo(%d)\n", iEffectIndex));
2645        LSCPResultSet result;
2646        try {
2647            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex);
2648            if (!pEffectInfo)
2649                throw Exception("There is no effect with index " + ToString(iEffectIndex));
2650    
2651            // convert the filename into the correct encoding as defined for LSCP
2652            // (especially in terms of special characters -> escape sequences)
2653    #if WIN32
2654            const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2655    #else
2656            // assuming POSIX
2657            const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2658    #endif
2659    
2660            result.Add("SYSTEM", pEffectInfo->EffectSystem());
2661            result.Add("MODULE", dllFileName);
2662            result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2663            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2664        }
2665        catch (Exception e) {
2666            result.Error(e);
2667        }
2668        return result.Produce();    
2669    }
2670    
2671    String LSCPServer::GetEffectInstanceInfo(int iEffectInstance) {
2672        dmsg(2,("LSCPServer: GetEffectInstanceInfo(%d)\n", iEffectInstance));
2673        LSCPResultSet result;
2674        try {
2675            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2676            if (!pEffect)
2677                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2678    
2679            EffectInfo* pEffectInfo = pEffect->GetEffectInfo();
2680    
2681            // convert the filename into the correct encoding as defined for LSCP
2682            // (especially in terms of special characters -> escape sequences)
2683    #if WIN32
2684            const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2685    #else
2686            // assuming POSIX
2687            const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2688    #endif
2689    
2690            result.Add("SYSTEM", pEffectInfo->EffectSystem());
2691            result.Add("MODULE", dllFileName);
2692            result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2693            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2694            result.Add("INPUT_CONTROLS", ToString(pEffect->InputControlCount()));
2695        }
2696        catch (Exception e) {
2697            result.Error(e);
2698        }
2699        return result.Produce();
2700    }
2701    
2702    String LSCPServer::GetEffectInstanceInputControlInfo(int iEffectInstance, int iInputControlIndex) {
2703        dmsg(2,("LSCPServer: GetEffectInstanceInputControlInfo(%d,%d)\n", iEffectInstance, iInputControlIndex));
2704        LSCPResultSet result;
2705        try {
2706            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2707            if (!pEffect)
2708                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2709    
2710            EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2711            if (!pEffectControl)
2712                throw Exception(
2713                    "Effect instance " + ToString(iEffectInstance) +
2714                    " does not have an input control with index " +
2715                    ToString(iInputControlIndex)
2716                );
2717    
2718            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectControl->Description()));
2719            result.Add("VALUE", pEffectControl->Value());
2720            if (pEffectControl->MinValue())
2721                 result.Add("RANGE_MIN", *pEffectControl->MinValue());
2722            if (pEffectControl->MaxValue())
2723                 result.Add("RANGE_MAX", *pEffectControl->MaxValue());
2724            if (!pEffectControl->Possibilities().empty())
2725                 result.Add("POSSIBILITIES", pEffectControl->Possibilities());
2726            if (pEffectControl->DefaultValue())
2727                 result.Add("DEFAULT", *pEffectControl->DefaultValue());
2728        } catch (Exception e) {
2729            result.Error(e);
2730        }
2731        return result.Produce();
2732    }
2733    
2734    String LSCPServer::SetEffectInstanceInputControlValue(int iEffectInstance, int iInputControlIndex, double dValue) {
2735        dmsg(2,("LSCPServer: SetEffectInstanceInputControlValue(%d,%d,%f)\n", iEffectInstance, iInputControlIndex, dValue));
2736        LSCPResultSet result;
2737        try {
2738            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2739            if (!pEffect)
2740                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2741    
2742            EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2743            if (!pEffectControl)
2744                throw Exception(
2745                    "Effect instance " + ToString(iEffectInstance) +
2746                    " does not have an input control with index " +
2747                    ToString(iInputControlIndex)
2748                );
2749    
2750            pEffectControl->SetValue(dValue);
2751            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_info, iEffectInstance));
2752        } catch (Exception e) {
2753            result.Error(e);
2754        }
2755        return result.Produce();
2756    }
2757    
2758    String LSCPServer::CreateEffectInstance(int iEffectIndex) {
2759        dmsg(2,("LSCPServer: CreateEffectInstance(%d)\n", iEffectIndex));
2760        LSCPResultSet result;
2761        try {
2762            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex);
2763            if (!pEffectInfo)
2764                throw Exception("There is no effect with index " + ToString(iEffectIndex));
2765            Effect* pEffect = EffectFactory::Create(pEffectInfo);
2766            result = pEffect->ID(); // success
2767            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount()));
2768        } catch (Exception e) {
2769            result.Error(e);
2770        }
2771        return result.Produce();
2772    }
2773    
2774    String LSCPServer::CreateEffectInstance(String effectSystem, String module, String effectName) {
2775        dmsg(2,("LSCPServer: CreateEffectInstance('%s','%s','%s')\n", effectSystem.c_str(), module.c_str(), effectName.c_str()));
2776        LSCPResultSet result;
2777        try {
2778            // to allow loading the same LSCP session file on different systems
2779            // successfully, probably with different effect plugin DLL paths or even
2780            // running completely different operating systems, we do the following
2781            // for finding the right effect:
2782            //
2783            // first try to search for an exact match of the effect plugin DLL
2784            // (a.k.a 'module'), to avoid picking the wrong DLL with the same
2785            // effect name ...
2786            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_MATCH_EXACTLY);
2787            // ... if no effect with exactly matchin DLL filename was found, then
2788            // try to lower the restrictions of matching the effect plugin DLL
2789            // filename and try again and again ...
2790            if (!pEffectInfo) {
2791                dmsg(2,("no exact module match, trying MODULE_IGNORE_PATH\n"));
2792                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH);
2793            }
2794            if (!pEffectInfo) {
2795                dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE\n"));
2796                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE);
2797            }
2798            if (!pEffectInfo) {
2799                dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE | MODULE_IGNORE_EXTENSION\n"));
2800                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE | EffectFactory::MODULE_IGNORE_EXTENSION);
2801            }
2802            // ... if there was still no effect found, then completely ignore the
2803            // DLL plugin filename argument and just search for the matching effect
2804            // system type and effect name
2805            if (!pEffectInfo) {
2806                dmsg(2,("no module match, trying MODULE_IGNORE_ALL\n"));
2807                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_ALL);
2808            }
2809            if (!pEffectInfo)
2810                throw Exception("There is no such effect '" + effectSystem + "' '" + module + "' '" + effectName + "'");
2811    
2812            Effect* pEffect = EffectFactory::Create(pEffectInfo);
2813            result = LSCPResultSet(pEffect->ID());
2814            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount()));
2815        } catch (Exception e) {
2816            result.Error(e);
2817        }
2818        return result.Produce();
2819    }
2820    
2821    String LSCPServer::DestroyEffectInstance(int iEffectInstance) {
2822        dmsg(2,("LSCPServer: DestroyEffectInstance(%d)\n", iEffectInstance));
2823        LSCPResultSet result;
2824        try {
2825            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2826            if (!pEffect)
2827                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2828            EffectFactory::Destroy(pEffect);
2829            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount()));
2830        } catch (Exception e) {
2831            result.Error(e);
2832        }
2833        return result.Produce();
2834    }
2835    
2836    String LSCPServer::GetEffectInstances() {
2837        dmsg(2,("LSCPServer: GetEffectInstances()\n"));
2838        LSCPResultSet result;
2839        try {
2840            int n = EffectFactory::EffectInstancesCount();
2841            result.Add(n);
2842        } catch (Exception e) {
2843            result.Error(e);
2844        }
2845        return result.Produce();
2846    }
2847    
2848    String LSCPServer::ListEffectInstances() {
2849        dmsg(2,("LSCPServer: ListEffectInstances()\n"));
2850        LSCPResultSet result;
2851        String list;
2852        try {
2853            int n = EffectFactory::EffectInstancesCount();
2854            for (int i = 0; i < n; i++) {
2855                Effect* pEffect = EffectFactory::GetEffectInstance(i);
2856                if (i) list += ",";
2857                list += ToString(pEffect->ID());
2858            }
2859        } catch (Exception e) {
2860            result.Error(e);
2861        }
2862        result.Add(list);
2863        return result.Produce();
2864    }
2865    
2866    String LSCPServer::GetSendEffectChains(int iAudioOutputDevice) {
2867        dmsg(2,("LSCPServer: GetSendEffectChains(%d)\n", iAudioOutputDevice));
2868        LSCPResultSet result;
2869        try {
2870            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2871            if (!devices.count(iAudioOutputDevice))
2872                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2873            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2874            int n = pDevice->SendEffectChainCount();
2875            result.Add(n);
2876        } catch (Exception e) {
2877            result.Error(e);
2878        }
2879        return result.Produce();
2880    }
2881    
2882    String LSCPServer::ListSendEffectChains(int iAudioOutputDevice) {
2883        dmsg(2,("LSCPServer: ListSendEffectChains(%d)\n", iAudioOutputDevice));
2884        LSCPResultSet result;
2885        String list;
2886        try {
2887            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2888            if (!devices.count(iAudioOutputDevice))
2889                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2890            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2891            int n = pDevice->SendEffectChainCount();
2892            for (int i = 0; i < n; i++) {
2893                EffectChain* pEffectChain = pDevice->SendEffectChain(i);
2894                if (i) list += ",";
2895                list += ToString(pEffectChain->ID());
2896            }
2897        } catch (Exception e) {
2898            result.Error(e);
2899        }
2900        result.Add(list);
2901        return result.Produce();
2902    }
2903    
2904    String LSCPServer::AddSendEffectChain(int iAudioOutputDevice) {
2905        dmsg(2,("LSCPServer: AddSendEffectChain(%d)\n", iAudioOutputDevice));
2906        LSCPResultSet result;
2907        try {
2908            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2909            if (!devices.count(iAudioOutputDevice))
2910                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2911            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2912            EffectChain* pEffectChain = pDevice->AddSendEffectChain();
2913            result = pEffectChain->ID();
2914            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_count, iAudioOutputDevice, pDevice->SendEffectChainCount()));
2915        } catch (Exception e) {
2916            result.Error(e);
2917        }
2918        return result.Produce();
2919    }
2920    
2921    String LSCPServer::RemoveSendEffectChain(int iAudioOutputDevice, int iSendEffectChain) {
2922        dmsg(2,("LSCPServer: RemoveSendEffectChain(%d,%d)\n", iAudioOutputDevice, iSendEffectChain));
2923        LSCPResultSet result;
2924        try {
2925            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2926            if (!devices.count(iAudioOutputDevice))
2927                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2928    
2929            std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
2930            std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
2931            std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
2932            for (; itEngineChannel != itEnd; ++itEngineChannel) {
2933                AudioOutputDevice* pDev = (*itEngineChannel)->GetAudioOutputDevice();
2934                if (pDev != NULL && pDev->deviceId() == iAudioOutputDevice) {
2935                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
2936                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
2937                        if(fxs != NULL && fxs->DestinationEffectChain() == iSendEffectChain) {
2938                            throw Exception("The effect chain is still in use by channel " + ToString((*itEngineChannel)->GetSamplerChannel()->Index()));
2939                        }
2940                    }
2941                }
2942            }
2943    
2944            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2945            for (int i = 0; i < pDevice->SendEffectChainCount(); i++) {
2946                EffectChain* pEffectChain = pDevice->SendEffectChain(i);
2947                if (pEffectChain->ID() == iSendEffectChain) {
2948                    pDevice->RemoveSendEffectChain(i);
2949                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_count, iAudioOutputDevice, pDevice->SendEffectChainCount()));
2950                    return result.Produce();
2951                }
2952            }
2953            throw Exception(
2954                "There is no send effect chain with ID " +
2955                ToString(iSendEffectChain) + " for audio output device " +
2956                ToString(iAudioOutputDevice) + "."
2957            );
2958        } catch (Exception e) {
2959            result.Error(e);
2960        }
2961        return result.Produce();
2962    }
2963    
2964    static EffectChain* _getSendEffectChain(Sampler* pSampler, int iAudioOutputDevice, int iSendEffectChain) throw (Exception) {
2965        std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2966        if (!devices.count(iAudioOutputDevice))
2967            throw Exception(
2968                "There is no audio output device with index " +
2969                ToString(iAudioOutputDevice) + "."
2970            );
2971        AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2972        EffectChain* pEffectChain = pDevice->SendEffectChainByID(iSendEffectChain);
2973        if(pEffectChain != NULL) return pEffectChain;
2974        throw Exception(
2975            "There is no send effect chain with ID " +
2976            ToString(iSendEffectChain) + " for audio output device " +
2977            ToString(iAudioOutputDevice) + "."
2978        );
2979    }
2980    
2981    String LSCPServer::GetSendEffectChainInfo(int iAudioOutputDevice, int iSendEffectChain) {
2982        dmsg(2,("LSCPServer: GetSendEffectChainInfo(%d,%d)\n", iAudioOutputDevice, iSendEffectChain));
2983        LSCPResultSet result;
2984        try {
2985            EffectChain* pEffectChain =
2986                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2987            String sEffectSequence;
2988            for (int i = 0; i < pEffectChain->EffectCount(); i++) {
2989                if (i) sEffectSequence += ",";
2990                sEffectSequence += ToString(pEffectChain->GetEffect(i)->ID());
2991            }
2992            result.Add("EFFECT_COUNT", pEffectChain->EffectCount());
2993            result.Add("EFFECT_SEQUENCE", sEffectSequence);
2994        } catch (Exception e) {
2995            result.Error(e);
2996        }
2997        return result.Produce();
2998    }
2999    
3000    String LSCPServer::AppendSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectInstance) {
3001        dmsg(2,("LSCPServer: AppendSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectInstance));
3002        LSCPResultSet result;
3003        try {
3004            EffectChain* pEffectChain =
3005                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
3006            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
3007            if (!pEffect)
3008                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
3009            pEffectChain->AppendEffect(pEffect);
3010            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount()));
3011        } catch (Exception e) {
3012            result.Error(e);
3013        }
3014        return result.Produce();
3015    }
3016    
3017    String LSCPServer::InsertSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition, int iEffectInstance) {
3018        dmsg(2,("LSCPServer: InsertSendEffectChainEffect(%d,%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition, iEffectInstance));
3019        LSCPResultSet result;
3020        try {
3021            EffectChain* pEffectChain =
3022                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
3023            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
3024            if (!pEffect)
3025                throw Exception("There is no effect instance with index " + ToString(iEffectInstance));
3026            pEffectChain->InsertEffect(pEffect, iEffectChainPosition);
3027            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount()));
3028        } catch (Exception e) {
3029            result.Error(e);
3030        }
3031        return result.Produce();
3032    }
3033    
3034    String LSCPServer::RemoveSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition) {
3035        dmsg(2,("LSCPServer: RemoveSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition));
3036        LSCPResultSet result;
3037        try {
3038            EffectChain* pEffectChain =
3039                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
3040    
3041            std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
3042            std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
3043            std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
3044            for (; itEngineChannel != itEnd; ++itEngineChannel) {
3045                AudioOutputDevice* pDev = (*itEngineChannel)->GetAudioOutputDevice();
3046                if (pDev != NULL && pDev->deviceId() == iAudioOutputDevice) {
3047                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
3048                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
3049                        if(fxs != NULL && fxs->DestinationEffectChain() == iSendEffectChain && fxs->DestinationEffectChainPosition() == iEffectChainPosition) {
3050                            throw Exception("The effect instance is still in use by channel " + ToString((*itEngineChannel)->GetSamplerChannel()->Index()));
3051                        }
3052                    }
3053                }
3054            }
3055    
3056            pEffectChain->RemoveEffect(iEffectChainPosition);
3057            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount()));
3058        } catch (Exception e) {
3059            result.Error(e);
3060        }
3061        return result.Produce();
3062    }
3063    
3064  String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {  String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
3065      dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
3066      LSCPResultSet result;      LSCPResultSet result;
# Line 2497  String LSCPServer::SendChannelMidiData(S Line 3109  String LSCPServer::SendChannelMidiData(S
3109              pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);              pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);
3110              bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);              bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);
3111              if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));              if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
3112            } else if (MidiMsg == "CC") {
3113                pMidiDevice->SendCCToDevice(Arg1, Arg2);
3114                bool b = pMidiDevice->SendCCToSampler(Arg1, Arg2);
3115                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
3116          } else {          } else {
3117              throw Exception("Unknown MIDI message type: " + MidiMsg);              throw Exception("Unknown MIDI message type: " + MidiMsg);
3118          }          }
# Line 2579  String LSCPServer::GetTotalVoiceCount() Line 3195  String LSCPServer::GetTotalVoiceCount()
3195  String LSCPServer::GetTotalVoiceCountMax() {  String LSCPServer::GetTotalVoiceCountMax() {
3196      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
3197      LSCPResultSet result;      LSCPResultSet result;
3198      result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES);      result.Add(EngineFactory::EngineInstances().size() * pSampler->GetGlobalMaxVoices());
3199      return result.Produce();      return result.Produce();
3200  }  }
3201    
# Line 2590  String LSCPServer::GetTotalVoiceCountMax Line 3206  String LSCPServer::GetTotalVoiceCountMax
3206  String LSCPServer::GetGlobalMaxVoices() {  String LSCPServer::GetGlobalMaxVoices() {
3207      dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));      dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));
3208      LSCPResultSet result;      LSCPResultSet result;
3209      result.Add(GLOBAL_MAX_VOICES);      result.Add(pSampler->GetGlobalMaxVoices());
3210      return result.Produce();      return result.Produce();
3211  }  }
3212    
# Line 2602  String LSCPServer::SetGlobalMaxVoices(in Line 3218  String LSCPServer::SetGlobalMaxVoices(in
3218      dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));      dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));
3219      LSCPResultSet result;      LSCPResultSet result;
3220      try {      try {
3221          if (iVoices < 1) throw Exception("Maximum voices may not be less than 1");          pSampler->SetGlobalMaxVoices(iVoices);
3222          GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp          LSCPServer::SendLSCPNotify(
3223          const std::set<Engine*>& engines = EngineFactory::EngineInstances();              LSCPEvent(LSCPEvent::event_global_info, "VOICES", pSampler->GetGlobalMaxVoices())
3224          if (engines.size() > 0) {          );
             std::set<Engine*>::iterator iter = engines.begin();  
             std::set<Engine*>::iterator end  = engines.end();  
             for (; iter != end; ++iter) {  
                 (*iter)->SetMaxVoices(iVoices);  
             }  
         }  
         LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES));  
3225      } catch (Exception e) {      } catch (Exception e) {
3226          result.Error(e);          result.Error(e);
3227      }      }
# Line 2626  String LSCPServer::SetGlobalMaxVoices(in Line 3235  String LSCPServer::SetGlobalMaxVoices(in
3235  String LSCPServer::GetGlobalMaxStreams() {  String LSCPServer::GetGlobalMaxStreams() {
3236      dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));      dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));
3237      LSCPResultSet result;      LSCPResultSet result;
3238      result.Add(GLOBAL_MAX_STREAMS);      result.Add(pSampler->GetGlobalMaxStreams());
3239      return result.Produce();      return result.Produce();
3240  }  }
3241    
# Line 2638  String LSCPServer::SetGlobalMaxStreams(i Line 3247  String LSCPServer::SetGlobalMaxStreams(i
3247      dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));      dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));
3248      LSCPResultSet result;      LSCPResultSet result;
3249      try {      try {
3250          if (iStreams < 0) throw Exception("Maximum disk streams may not be negative");          pSampler->SetGlobalMaxStreams(iStreams);
3251          GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp          LSCPServer::SendLSCPNotify(
3252          const std::set<Engine*>& engines = EngineFactory::EngineInstances();              LSCPEvent(LSCPEvent::event_global_info, "STREAMS", pSampler->GetGlobalMaxStreams())
3253          if (engines.size() > 0) {          );
             std::set<Engine*>::iterator iter = engines.begin();  
             std::set<Engine*>::iterator end  = engines.end();  
             for (; iter != end; ++iter) {  
                 (*iter)->SetMaxDiskStreams(iStreams);  
             }  
         }  
         LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS));  
3254      } catch (Exception e) {      } catch (Exception e) {
3255          result.Error(e);          result.Error(e);
3256      }      }
# Line 2852  void LSCPServer::VerifyFile(String Filen Line 3454  void LSCPServer::VerifyFile(String Filen
3454  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
3455      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
3456      LSCPResultSet result;      LSCPResultSet result;
3457      SubscriptionMutex.Lock();      {
3458      eventSubscriptions[type].push_back(currentSocket);          LockGuard lock(SubscriptionMutex);
3459      SubscriptionMutex.Unlock();          eventSubscriptions[type].push_back(currentSocket);
3460        }
3461      return result.Produce();      return result.Produce();
3462  }  }
3463    
# Line 2865  String LSCPServer::SubscribeNotification Line 3468  String LSCPServer::SubscribeNotification
3468  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
3469      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
3470      LSCPResultSet result;      LSCPResultSet result;
3471      SubscriptionMutex.Lock();      {
3472      eventSubscriptions[type].remove(currentSocket);          LockGuard lock(SubscriptionMutex);
3473      SubscriptionMutex.Unlock();          eventSubscriptions[type].remove(currentSocket);
3474        }
3475      return result.Produce();      return result.Produce();
3476  }  }
3477    
# Line 3392  String LSCPServer::SetEcho(yyparse_param Line 3996  String LSCPServer::SetEcho(yyparse_param
3996      }      }
3997      return result.Produce();      return result.Produce();
3998  }  }
3999    
4000    String LSCPServer::SetShellInteract(yyparse_param_t* pSession, double boolean_value) {
4001        dmsg(2,("LSCPServer: SetShellInteract(val=%f)\n", boolean_value));
4002        LSCPResultSet result;
4003        try {
4004            if      (boolean_value == 0) pSession->bShellInteract = false;
4005            else if (boolean_value == 1) pSession->bShellInteract = true;
4006            else throw Exception("Not a boolean value, must either be 0 or 1");
4007        } catch (Exception e) {
4008            result.Error(e);
4009        }
4010        return result.Produce();
4011    }
4012    
4013    String LSCPServer::SetShellAutoCorrect(yyparse_param_t* pSession, double boolean_value) {
4014        dmsg(2,("LSCPServer: SetShellAutoCorrect(val=%f)\n", boolean_value));
4015        LSCPResultSet result;
4016        try {
4017            if      (boolean_value == 0) pSession->bShellAutoCorrect = false;
4018            else if (boolean_value == 1) pSession->bShellAutoCorrect = true;
4019            else throw Exception("Not a boolean value, must either be 0 or 1");
4020        } catch (Exception e) {
4021            result.Error(e);
4022        }
4023        return result.Produce();
4024    }
4025    
4026  }  }

Legend:
Removed from v.1897  
changed lines
  Added in v.2516

  ViewVC Help
Powered by ViewVC