3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
#include <algorithm> |
25 |
|
|
26 |
#include "lscpserver.h" |
#include "lscpserver.h" |
27 |
#include "lscpresultset.h" |
#include "lscpresultset.h" |
28 |
#include "lscpevent.h" |
#include "lscpevent.h" |
103 |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
104 |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
105 |
|
|
106 |
LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) { |
LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) { |
107 |
SocketAddress.sin_family = AF_INET; |
SocketAddress.sin_family = AF_INET; |
108 |
SocketAddress.sin_addr.s_addr = addr; |
SocketAddress.sin_addr.s_addr = addr; |
109 |
SocketAddress.sin_port = port; |
SocketAddress.sin_port = port; |
132 |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); |
133 |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
134 |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
135 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI"); |
136 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI"); |
137 |
hSocket = -1; |
hSocket = -1; |
138 |
} |
} |
139 |
|
|
145 |
#endif |
#endif |
146 |
} |
} |
147 |
|
|
148 |
|
LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) { |
149 |
|
this->pParent = pParent; |
150 |
|
} |
151 |
|
|
152 |
|
LSCPServer::EventHandler::~EventHandler() { |
153 |
|
std::vector<midi_listener_entry> l = channelMidiListeners; |
154 |
|
channelMidiListeners.clear(); |
155 |
|
for (int i = 0; i < l.size(); i++) |
156 |
|
delete l[i].pMidiListener; |
157 |
|
} |
158 |
|
|
159 |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
160 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
161 |
} |
} |
162 |
|
|
163 |
|
void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) { |
164 |
|
pChannel->AddEngineChangeListener(this); |
165 |
|
} |
166 |
|
|
167 |
|
void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) { |
168 |
|
if (!pChannel->GetEngineChannel()) return; |
169 |
|
EngineToBeChanged(pChannel->Index()); |
170 |
|
} |
171 |
|
|
172 |
|
void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) { |
173 |
|
SamplerChannel* pSamplerChannel = |
174 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
175 |
|
if (!pSamplerChannel) return; |
176 |
|
EngineChannel* pEngineChannel = |
177 |
|
pSamplerChannel->GetEngineChannel(); |
178 |
|
if (!pEngineChannel) return; |
179 |
|
for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) { |
180 |
|
if ((*iter).pEngineChannel == pEngineChannel) { |
181 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
182 |
|
pEngineChannel->Disconnect(pMidiListener); |
183 |
|
channelMidiListeners.erase(iter); |
184 |
|
delete pMidiListener; |
185 |
|
return; |
186 |
|
} |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
void LSCPServer::EventHandler::EngineChanged(int ChannelId) { |
191 |
|
SamplerChannel* pSamplerChannel = |
192 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
193 |
|
if (!pSamplerChannel) return; |
194 |
|
EngineChannel* pEngineChannel = |
195 |
|
pSamplerChannel->GetEngineChannel(); |
196 |
|
if (!pEngineChannel) return; |
197 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
198 |
|
pEngineChannel->Connect(pMidiListener); |
199 |
|
midi_listener_entry entry = { |
200 |
|
pSamplerChannel, pEngineChannel, pMidiListener |
201 |
|
}; |
202 |
|
channelMidiListeners.push_back(entry); |
203 |
|
} |
204 |
|
|
205 |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
206 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
207 |
} |
} |
210 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
211 |
} |
} |
212 |
|
|
213 |
|
void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
214 |
|
pDevice->RemoveMidiPortCountListener(this); |
215 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
216 |
|
MidiPortToBeRemoved(pDevice->GetPort(i)); |
217 |
|
} |
218 |
|
|
219 |
|
void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) { |
220 |
|
pDevice->AddMidiPortCountListener(this); |
221 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
222 |
|
MidiPortAdded(pDevice->GetPort(i)); |
223 |
|
} |
224 |
|
|
225 |
|
void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) { |
226 |
|
// yet unused |
227 |
|
} |
228 |
|
|
229 |
|
void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) { |
230 |
|
for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) { |
231 |
|
if ((*iter).pPort == pPort) { |
232 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
233 |
|
pPort->Disconnect(pMidiListener); |
234 |
|
deviceMidiListeners.erase(iter); |
235 |
|
delete pMidiListener; |
236 |
|
return; |
237 |
|
} |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) { |
242 |
|
// find out the device ID |
243 |
|
std::map<uint, MidiInputDevice*> devices = |
244 |
|
pParent->pSampler->GetMidiInputDevices(); |
245 |
|
for ( |
246 |
|
std::map<uint, MidiInputDevice*>::iterator iter = devices.begin(); |
247 |
|
iter != devices.end(); ++iter |
248 |
|
) { |
249 |
|
if (iter->second == pPort->GetDevice()) { // found |
250 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
251 |
|
pPort->Connect(pMidiListener); |
252 |
|
device_midi_listener_entry entry = { |
253 |
|
pPort, pMidiListener, iter->first |
254 |
|
}; |
255 |
|
deviceMidiListeners.push_back(entry); |
256 |
|
return; |
257 |
|
} |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
262 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
263 |
} |
} |
414 |
timeval timeout; |
timeval timeout; |
415 |
|
|
416 |
while (true) { |
while (true) { |
417 |
|
#if CONFIG_PTHREAD_TESTCANCEL |
418 |
|
TestCancel(); |
419 |
|
#endif |
420 |
// 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 |
421 |
{ |
{ |
422 |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
438 |
} |
} |
439 |
} |
} |
440 |
|
|
441 |
|
// check if MIDI data arrived on some engine channel |
442 |
|
for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) { |
443 |
|
const EventHandler::midi_listener_entry entry = |
444 |
|
eventHandler.channelMidiListeners[i]; |
445 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
446 |
|
if (pMidiListener->NotesChanged()) { |
447 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
448 |
|
if (pMidiListener->NoteChanged(iNote)) { |
449 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
450 |
|
LSCPServer::SendLSCPNotify( |
451 |
|
LSCPEvent( |
452 |
|
LSCPEvent::event_channel_midi, |
453 |
|
entry.pSamplerChannel->Index(), |
454 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
455 |
|
iNote, |
456 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
457 |
|
: pMidiListener->NoteOffVelocity(iNote) |
458 |
|
) |
459 |
|
); |
460 |
|
} |
461 |
|
} |
462 |
|
} |
463 |
|
} |
464 |
|
|
465 |
|
// check if MIDI data arrived on some MIDI device |
466 |
|
for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) { |
467 |
|
const EventHandler::device_midi_listener_entry entry = |
468 |
|
eventHandler.deviceMidiListeners[i]; |
469 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
470 |
|
if (pMidiListener->NotesChanged()) { |
471 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
472 |
|
if (pMidiListener->NoteChanged(iNote)) { |
473 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
474 |
|
LSCPServer::SendLSCPNotify( |
475 |
|
LSCPEvent( |
476 |
|
LSCPEvent::event_device_midi, |
477 |
|
entry.uiDeviceID, |
478 |
|
entry.pPort->GetPortNumber(), |
479 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
480 |
|
iNote, |
481 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
482 |
|
: pMidiListener->NoteOffVelocity(iNote) |
483 |
|
) |
484 |
|
); |
485 |
|
} |
486 |
|
} |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
//Now let's deliver late notifies (if any) |
//Now let's deliver late notifies (if any) |
491 |
NotifyBufferMutex.Lock(); |
NotifyBufferMutex.Lock(); |
492 |
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++) { |
2557 |
LSCPResultSet result; |
LSCPResultSet result; |
2558 |
try { |
try { |
2559 |
if (dVolume < 0) throw Exception("Volume may not be negative"); |
if (dVolume < 0) throw Exception("Volume may not be negative"); |
2560 |
GLOBAL_VOLUME = dVolume; // see common/global.cpp |
GLOBAL_VOLUME = dVolume; // see common/global_private.cpp |
2561 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME)); |
2562 |
} catch (Exception e) { |
} catch (Exception e) { |
2563 |
result.Error(e); |
result.Error(e); |