/[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 660 by schoenebeck, Fri Jun 17 19:49:30 2005 UTC revision 778 by iliev, Fri Sep 23 06:58:26 2005 UTC
# Line 61  Mutex LSCPServer::NotifyBufferMutex = Mu Line 61  Mutex LSCPServer::NotifyBufferMutex = Mu
61  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
62  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
63    
64  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(true, false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) {
65        SocketAddress.sin_family      = AF_INET;
66        SocketAddress.sin_addr.s_addr = addr;
67        SocketAddress.sin_port        = port;
68      this->pSampler = pSampler;      this->pSampler = pSampler;
69      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
70      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
# Line 69  LSCPServer::LSCPServer(Sampler* pSampler Line 72  LSCPServer::LSCPServer(Sampler* pSampler
72      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
73      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
74      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
75        LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
76      hSocket = -1;      hSocket = -1;
77  }  }
78    
# Line 98  int LSCPServer::Main() { Line 102  int LSCPServer::Main() {
102          exit(EXIT_FAILURE);          exit(EXIT_FAILURE);
103      }      }
104    
     SocketAddress.sin_family      = AF_INET;  
     SocketAddress.sin_port        = htons(LSCP_PORT);  
     SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);  
   
105      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
106          std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";          std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
107          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
# Line 206  int LSCPServer::Main() { Line 206  int LSCPServer::Main() {
206          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
207          NotifyBufferMutex.Lock();          NotifyBufferMutex.Lock();
208          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
209                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
210                  bufferedNotifies.erase(iterNotify);                  bufferedNotifies.erase(iterNotify);
211          }          }
212          NotifyBufferMutex.Unlock();          NotifyBufferMutex.Unlock();
# Line 256  void LSCPServer::SendLSCPNotify( LSCPEve Line 256  void LSCPServer::SendLSCPNotify( LSCPEve
256          while (true) {          while (true) {
257                  if (NotifyMutex.Trylock()) {                  if (NotifyMutex.Trylock()) {
258                          for(;iter != end; iter++)                          for(;iter != end; iter++)
259                                  send(*iter, notify.c_str(), notify.size(), 0);                                  send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
260                          NotifyMutex.Unlock();                          NotifyMutex.Unlock();
261                          break;                          break;
262                  } else {                  } else {
# Line 365  void LSCPServer::AnswerClient(String Ret Line 365  void LSCPServer::AnswerClient(String Ret
365      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
366      if (currentSocket != -1) {      if (currentSocket != -1) {
367              NotifyMutex.Lock();              NotifyMutex.Lock();
368              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
369              NotifyMutex.Unlock();              NotifyMutex.Unlock();
370      }      }
371  }  }
# Line 498  String LSCPServer::LoadInstrument(String Line 498  String LSCPServer::LoadInstrument(String
498   * sampler channel.   * sampler channel.
499   */   */
500  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
501      dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));      dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
502      LSCPResultSet result;      LSCPResultSet result;
503      try {      try {
504          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
505          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
506          LockRTNotify();          LockRTNotify();
507          pSamplerChannel->SetEngineType(EngineName);          pSamplerChannel->SetEngineType(EngineName);
508            if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
509          UnlockRTNotify();          UnlockRTNotify();
510      }      }
511      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
# Line 620  String LSCPServer::GetChannelInfo(uint u Line 621  String LSCPServer::GetChannelInfo(uint u
621          int InstrumentStatus = -1;          int InstrumentStatus = -1;
622          int AudioOutputChannels = 0;          int AudioOutputChannels = 0;
623          String AudioRouting;          String AudioRouting;
624            int Mute = 0;
625            bool Solo = false;
626    
627          if (pEngineChannel) {          if (pEngineChannel) {
628              EngineName          = pEngineChannel->EngineName();              EngineName          = pEngineChannel->EngineName();
# Line 635  String LSCPServer::GetChannelInfo(uint u Line 638  String LSCPServer::GetChannelInfo(uint u
638                  if (AudioRouting != "") AudioRouting += ",";                  if (AudioRouting != "") AudioRouting += ",";
639                  AudioRouting += ToString(pEngineChannel->OutputChannel(chan));                  AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
640              }              }
641                Mute = pEngineChannel->GetMute();
642                Solo = pEngineChannel->GetSolo();
643          }          }
644    
645          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
# Line 647  String LSCPServer::GetChannelInfo(uint u Line 652  String LSCPServer::GetChannelInfo(uint u
652    
653          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
654          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
655          if (pSamplerChannel->GetMidiInputChannel() == MidiInputPort::midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
656          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
657    
658          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
659          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
660          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", InstrumentName);
661          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
662            result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
663            result.Add("SOLO", Solo);
664      }      }
665      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
666           result.Error(e);           result.Error(e);
# Line 1291  String LSCPServer::SetMIDIInputChannel(u Line 1298  String LSCPServer::SetMIDIInputChannel(u
1298      try {      try {
1299          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1300          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1301          pSamplerChannel->SetMidiInputChannel((MidiInputPort::midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1302      }      }
1303      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1304           result.Error(e);           result.Error(e);
# Line 1368  String LSCPServer::SetMIDIInput(uint MID Line 1375  String LSCPServer::SetMIDIInput(uint MID
1375          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1376          if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));          if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1377          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1378          pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputPort::midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
1379      }      }
1380      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1381           result.Error(e);           result.Error(e);
# Line 1397  String LSCPServer::SetVolume(double dVol Line 1404  String LSCPServer::SetVolume(double dVol
1404  }  }
1405    
1406  /**  /**
1407     * Will be called by the parser to mute/unmute particular sampler channel.
1408     */
1409    String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1410        dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1411        LSCPResultSet result;
1412        try {
1413            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1414            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1415    
1416            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1417            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1418    
1419            if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1420            else pEngineChannel->SetMute(1);
1421        } catch (LinuxSamplerException e) {
1422            result.Error(e);
1423        }
1424        return result.Produce();
1425    }
1426    
1427    /**
1428     * Will be called by the parser to solo particular sampler channel.
1429     */
1430    String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1431        dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1432        LSCPResultSet result;
1433        try {
1434            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1435            if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1436    
1437            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1438            if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1439    
1440            bool oldSolo = pEngineChannel->GetSolo();
1441            bool hadSoloChannel = HasSoloChannel();
1442            
1443            pEngineChannel->SetSolo(bSolo);
1444            
1445            if(!oldSolo && bSolo) {
1446                if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1447                if(!hadSoloChannel) MuteNonSoloChannels();
1448            }
1449            
1450            if(oldSolo && !bSolo) {
1451                if(!HasSoloChannel()) UnmuteChannels();
1452                else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1453            }
1454        } catch (LinuxSamplerException e) {
1455            result.Error(e);
1456        }
1457        return result.Produce();
1458    }
1459    
1460    /**
1461     * Determines whether there is at least one solo channel in the channel list.
1462     *
1463     * @returns true if there is at least one solo channel in the channel list,
1464     * false otherwise.
1465     */
1466    bool LSCPServer::HasSoloChannel() {
1467        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1468        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1469        for (; iter != channels.end(); iter++) {
1470            EngineChannel* c = iter->second->GetEngineChannel();
1471            if(c && c->GetSolo()) return true;
1472        }
1473    
1474        return false;
1475    }
1476    
1477    /**
1478     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1479     * with -1 which indicates that they are muted because of the presence
1480     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1481     * when there are no solo channels left.
1482     */
1483    void LSCPServer::MuteNonSoloChannels() {
1484        dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1485        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1486        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1487        for (; iter != channels.end(); iter++) {
1488            EngineChannel* c = iter->second->GetEngineChannel();
1489            if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1490        }
1491    }
1492    
1493    /**
1494     * Unmutes all channels that are muted because of the presence
1495     * of a solo channel(s).
1496     */
1497    void  LSCPServer::UnmuteChannels() {
1498        dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1499        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1500        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1501        for (; iter != channels.end(); iter++) {
1502            EngineChannel* c = iter->second->GetEngineChannel();
1503            if(c && c->GetMute() == -1) c->SetMute(0);
1504        }
1505    }
1506    
1507    /**
1508   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
1509   */   */
1510  String LSCPServer::ResetChannel(uint uiSamplerChannel) {  String LSCPServer::ResetChannel(uint uiSamplerChannel) {
# Line 1407  String LSCPServer::ResetChannel(uint uiS Line 1515  String LSCPServer::ResetChannel(uint uiS
1515          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1516          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1517          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1518          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");          pEngineChannel->Reset();
         pEngineChannel->GetEngine()->Reset();  
1519      }      }
1520      catch (LinuxSamplerException e) {      catch (LinuxSamplerException e) {
1521           result.Error(e);           result.Error(e);
# Line 1439  String LSCPServer::GetServerInfo() { Line 1546  String LSCPServer::GetServerInfo() {
1546      return result.Produce();      return result.Produce();
1547  }  }
1548    
1549    /**
1550     * Will be called by the parser to return the current number of all active voices.
1551     */
1552    String LSCPServer::GetTotalVoiceCount() {
1553        dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
1554        LSCPResultSet result;
1555        result.Add(pSampler->GetVoiceCount());
1556        return result.Produce();
1557    }
1558    
1559    /**
1560     * Will be called by the parser to return the maximum number of voices.
1561     */
1562    String LSCPServer::GetTotalVoiceCountMax() {
1563        dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
1564        LSCPResultSet result;
1565        result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);
1566        return result.Produce();
1567    }
1568    
1569  /**  /**
1570   * Will be called by the parser to subscribe a client (frontend) on the   * Will be called by the parser to subscribe a client (frontend) on the
1571   * server for receiving event messages.   * server for receiving event messages.

Legend:
Removed from v.660  
changed lines
  Added in v.778

  ViewVC Help
Powered by ViewVC