--- linuxsampler/trunk/src/network/lscpserver.cpp 2004/08/25 22:00:33 226 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2005/02/12 23:48:50 376 @@ -50,6 +50,7 @@ Mutex LSCPServer::NotifyMutex = Mutex(); Mutex LSCPServer::NotifyBufferMutex = Mutex(); Mutex LSCPServer::SubscriptionMutex = Mutex(); +Mutex LSCPServer::RTNotifyMutex = Mutex(); LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) { this->pSampler = pSampler; @@ -88,10 +89,19 @@ SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { - std::cerr << "LSCPServer: Could not bind server socket." << std::endl; - close(hSocket); - //return -1; - exit(EXIT_FAILURE); + std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds..."; + for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds + if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) { + if (trial > LSCP_SERVER_BIND_TIMEOUT) { + std::cerr << "gave up!" << std::endl; + close(hSocket); + //return -1; + exit(EXIT_FAILURE); + } + else sleep(1); // sleep 1s + } + else break; // success + } } listen(hSocket, 1); @@ -194,6 +204,18 @@ NotifyMutex.Unlock(); } +int LSCPServer::EventSubscribers( std::list events ) { + int subs = 0; + SubscriptionMutex.Lock(); + for( std::list::iterator iter = events.begin(); + iter != events.end(); iter++) + { + subs += eventSubscriptions.count(*iter); + } + SubscriptionMutex.Unlock(); + return subs; +} + void LSCPServer::SendLSCPNotify( LSCPEvent event ) { SubscriptionMutex.Lock(); if (eventSubscriptions.count(event.GetType()) == 0) { @@ -423,11 +445,11 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); - if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); if (!pSamplerChannel->GetAudioOutputDevice()) - throw LinuxSamplerException("No audio output device on channel"); + throw LinuxSamplerException("No audio output device connected to sampler channel"); if (bBackground) { LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument); pLoadInstrument->StartThread(); @@ -451,8 +473,10 @@ if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig; else throw LinuxSamplerException("Unknown engine type"); SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); + LockRTNotify(); pSamplerChannel->LoadEngine(type); + UnlockRTNotify(); } catch (LinuxSamplerException e) { result.Error(e); @@ -503,7 +527,9 @@ String LSCPServer::RemoveChannel(uint uiSamplerChannel) { dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; + LockRTNotify(); pSampler->RemoveSamplerChannel(uiSamplerChannel); + UnlockRTNotify(); return result.Produce(); } @@ -546,13 +572,14 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); //Defaults values String EngineName = "NONE"; float Volume = 0.0f; String InstrumentFileName = "NONE"; + String InstrumentName = "NONE"; int InstrumentIndex = -1; int InstrumentStatus = -1; int AudioOutputChannels = 0; @@ -565,7 +592,10 @@ InstrumentStatus = pEngine->InstrumentStatus(); InstrumentIndex = pEngine->InstrumentIndex(); if (InstrumentIndex != -1) + { InstrumentFileName = pEngine->InstrumentFileName(); + InstrumentName = pEngine->InstrumentName(); + } for (int chan = 0; chan < pEngine->Channels(); chan++) { if (AudioRouting != "") AudioRouting += ","; AudioRouting += ToString(pEngine->OutputChannel(chan)); @@ -582,11 +612,12 @@ result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice())); result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort()); - if (pSamplerChannel->GetMidiInputChannel()) result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); - else result.Add("MIDI_INPUT_CHANNEL", "ALL"); + if (pSamplerChannel->GetMidiInputChannel() == MidiInputPort::midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL"); + else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel()); result.Add("INSTRUMENT_FILE", InstrumentFileName); result.Add("INSTRUMENT_NR", InstrumentIndex); + result.Add("INSTRUMENT_NAME", InstrumentName); result.Add("INSTRUMENT_STATUS", InstrumentStatus); } catch (LinuxSamplerException e) { @@ -604,9 +635,9 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); - if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); result.Add(pEngine->VoiceCount()); } catch (LinuxSamplerException e) { @@ -624,9 +655,9 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); - if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); result.Add(pEngine->DiskStreamCount()); } catch (LinuxSamplerException e) { @@ -644,9 +675,9 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); - if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); if (!pEngine->DiskStreamSupported()) result.Add("NA"); else { @@ -936,7 +967,7 @@ // get audio channel AudioChannel* pChannel = pDevice->Channel(ChannelId); - if (!pChannel) throw LinuxSamplerException("Audio ouotput device does not have channel " + ToString(ChannelId) + "."); + if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // return the values of all audio channel parameters std::map parameters = pChannel->ChannelParameters(); @@ -995,7 +1026,7 @@ // get audio channel AudioChannel* pChannel = pDevice->Channel(ChannelId); - if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + "."); + if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // get desired audio channel parameter std::map parameters = pChannel->ChannelParameters(); @@ -1028,7 +1059,7 @@ // get audio channel AudioChannel* pChannel = pDevice->Channel(ChannelId); - if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + "."); + if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + "."); // get desired audio channel parameter std::map parameters = pChannel->ChannelParameters(); @@ -1111,9 +1142,10 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); if (!pEngine) throw LinuxSamplerException("No engine deployed on sampler channel " + ToString(uiSamplerChannel)); + if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel)); pEngine->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel); } catch (LinuxSamplerException e) { @@ -1127,7 +1159,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); std::map devices = pSampler->GetAudioOutputDevices(); if (!devices.count(AudioDeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId)); AudioOutputDevice* pDevice = devices[AudioDeviceId]; @@ -1144,7 +1176,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA"; if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK"; @@ -1181,7 +1213,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); pSamplerChannel->SetMidiInputPort(MIDIPort); } catch (LinuxSamplerException e) { @@ -1195,7 +1227,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); pSamplerChannel->SetMidiInputChannel((MidiInputPort::midi_chan_t) MIDIChannel); } catch (LinuxSamplerException e) { @@ -1209,7 +1241,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); std::map devices = pSampler->GetMidiInputDevices(); if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); MidiInputDevice* pDevice = devices[MIDIDeviceId]; @@ -1226,7 +1258,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA"; // Check if there's one MIDI input device already created @@ -1269,7 +1301,7 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); std::map devices = pSampler->GetMidiInputDevices(); if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId)); MidiInputDevice* pDevice = devices[MIDIDeviceId]; @@ -1290,9 +1322,9 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); - if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); pEngine->Volume(dVolume); } catch (LinuxSamplerException e) { @@ -1309,9 +1341,9 @@ LSCPResultSet result; try { SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds"); + if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel)); Engine* pEngine = pSamplerChannel->GetEngine(); - if (!pEngine) throw LinuxSamplerException("No engine loaded on channel"); + if (!pEngine) throw LinuxSamplerException("No engine loaded on sampler channel"); pEngine->Reset(); } catch (LinuxSamplerException e) {