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 * |
26 |
#include "lscpevent.h" |
#include "lscpevent.h" |
27 |
|
|
28 |
#if defined(WIN32) |
#if defined(WIN32) |
29 |
|
#include <windows.h> |
30 |
#else |
#else |
31 |
#include <fcntl.h> |
#include <fcntl.h> |
32 |
#endif |
#endif |
101 |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
Mutex LSCPServer::SubscriptionMutex = Mutex(); |
102 |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
Mutex LSCPServer::RTNotifyMutex = Mutex(); |
103 |
|
|
104 |
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) { |
105 |
SocketAddress.sin_family = AF_INET; |
SocketAddress.sin_family = AF_INET; |
106 |
SocketAddress.sin_addr.s_addr = addr; |
SocketAddress.sin_addr.s_addr = addr; |
107 |
SocketAddress.sin_port = port; |
SocketAddress.sin_port = port; |
127 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
128 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
129 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
130 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); |
131 |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
132 |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
133 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI"); |
134 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI"); |
135 |
hSocket = -1; |
hSocket = -1; |
136 |
} |
} |
137 |
|
|
143 |
#endif |
#endif |
144 |
} |
} |
145 |
|
|
146 |
|
LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) { |
147 |
|
this->pParent = pParent; |
148 |
|
} |
149 |
|
|
150 |
|
LSCPServer::EventHandler::~EventHandler() { |
151 |
|
std::vector<midi_listener_entry> l = channelMidiListeners; |
152 |
|
channelMidiListeners.clear(); |
153 |
|
for (int i = 0; i < l.size(); i++) |
154 |
|
delete l[i].pMidiListener; |
155 |
|
} |
156 |
|
|
157 |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) { |
158 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount)); |
159 |
} |
} |
160 |
|
|
161 |
|
void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) { |
162 |
|
pChannel->AddEngineChangeListener(this); |
163 |
|
} |
164 |
|
|
165 |
|
void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) { |
166 |
|
if (!pChannel->GetEngineChannel()) return; |
167 |
|
EngineToBeChanged(pChannel->Index()); |
168 |
|
} |
169 |
|
|
170 |
|
void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) { |
171 |
|
SamplerChannel* pSamplerChannel = |
172 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
173 |
|
if (!pSamplerChannel) return; |
174 |
|
EngineChannel* pEngineChannel = |
175 |
|
pSamplerChannel->GetEngineChannel(); |
176 |
|
if (!pEngineChannel) return; |
177 |
|
for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) { |
178 |
|
if ((*iter).pEngineChannel == pEngineChannel) { |
179 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
180 |
|
pEngineChannel->Disconnect(pMidiListener); |
181 |
|
channelMidiListeners.erase(iter); |
182 |
|
delete pMidiListener; |
183 |
|
return; |
184 |
|
} |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
void LSCPServer::EventHandler::EngineChanged(int ChannelId) { |
189 |
|
SamplerChannel* pSamplerChannel = |
190 |
|
pParent->pSampler->GetSamplerChannel(ChannelId); |
191 |
|
if (!pSamplerChannel) return; |
192 |
|
EngineChannel* pEngineChannel = |
193 |
|
pSamplerChannel->GetEngineChannel(); |
194 |
|
if (!pEngineChannel) return; |
195 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
196 |
|
pEngineChannel->Connect(pMidiListener); |
197 |
|
midi_listener_entry entry = { |
198 |
|
pSamplerChannel, pEngineChannel, pMidiListener |
199 |
|
}; |
200 |
|
channelMidiListeners.push_back(entry); |
201 |
|
} |
202 |
|
|
203 |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) { |
204 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount)); |
205 |
} |
} |
208 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount)); |
209 |
} |
} |
210 |
|
|
211 |
|
void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) { |
212 |
|
pDevice->RemoveMidiPortCountListener(this); |
213 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
214 |
|
MidiPortToBeRemoved(pDevice->GetPort(i)); |
215 |
|
} |
216 |
|
|
217 |
|
void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) { |
218 |
|
pDevice->AddMidiPortCountListener(this); |
219 |
|
for (int i = 0; i < pDevice->PortCount(); ++i) |
220 |
|
MidiPortAdded(pDevice->GetPort(i)); |
221 |
|
} |
222 |
|
|
223 |
|
void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) { |
224 |
|
// yet unused |
225 |
|
} |
226 |
|
|
227 |
|
void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) { |
228 |
|
for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) { |
229 |
|
if ((*iter).pPort == pPort) { |
230 |
|
VirtualMidiDevice* pMidiListener = (*iter).pMidiListener; |
231 |
|
pPort->Disconnect(pMidiListener); |
232 |
|
deviceMidiListeners.erase(iter); |
233 |
|
delete pMidiListener; |
234 |
|
return; |
235 |
|
} |
236 |
|
} |
237 |
|
} |
238 |
|
|
239 |
|
void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) { |
240 |
|
// find out the device ID |
241 |
|
std::map<uint, MidiInputDevice*> devices = |
242 |
|
pParent->pSampler->GetMidiInputDevices(); |
243 |
|
for ( |
244 |
|
std::map<uint, MidiInputDevice*>::iterator iter = devices.begin(); |
245 |
|
iter != devices.end(); ++iter |
246 |
|
) { |
247 |
|
if (iter->second == pPort->GetDevice()) { // found |
248 |
|
VirtualMidiDevice* pMidiListener = new VirtualMidiDevice; |
249 |
|
pPort->Connect(pMidiListener); |
250 |
|
device_midi_listener_entry entry = { |
251 |
|
pPort, pMidiListener, iter->first |
252 |
|
}; |
253 |
|
deviceMidiListeners.push_back(entry); |
254 |
|
return; |
255 |
|
} |
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) { |
260 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount)); |
261 |
} |
} |
292 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
293 |
} |
} |
294 |
|
|
295 |
|
void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) { |
296 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount)); |
297 |
|
} |
298 |
|
|
299 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
300 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
301 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
392 |
pSampler->AddVoiceCountListener(&eventHandler); |
pSampler->AddVoiceCountListener(&eventHandler); |
393 |
pSampler->AddStreamCountListener(&eventHandler); |
pSampler->AddStreamCountListener(&eventHandler); |
394 |
pSampler->AddBufferFillListener(&eventHandler); |
pSampler->AddBufferFillListener(&eventHandler); |
395 |
|
pSampler->AddTotalStreamCountListener(&eventHandler); |
396 |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
397 |
pSampler->AddFxSendCountListener(&eventHandler); |
pSampler->AddFxSendCountListener(&eventHandler); |
398 |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
412 |
timeval timeout; |
timeval timeout; |
413 |
|
|
414 |
while (true) { |
while (true) { |
415 |
|
#if CONFIG_PTHREAD_TESTCANCEL |
416 |
|
TestCancel(); |
417 |
|
#endif |
418 |
// 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 |
419 |
{ |
{ |
420 |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances(); |
436 |
} |
} |
437 |
} |
} |
438 |
|
|
439 |
|
// check if MIDI data arrived on some engine channel |
440 |
|
for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) { |
441 |
|
const EventHandler::midi_listener_entry entry = |
442 |
|
eventHandler.channelMidiListeners[i]; |
443 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
444 |
|
if (pMidiListener->NotesChanged()) { |
445 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
446 |
|
if (pMidiListener->NoteChanged(iNote)) { |
447 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
448 |
|
LSCPServer::SendLSCPNotify( |
449 |
|
LSCPEvent( |
450 |
|
LSCPEvent::event_channel_midi, |
451 |
|
entry.pSamplerChannel->Index(), |
452 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
453 |
|
iNote, |
454 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
455 |
|
: pMidiListener->NoteOffVelocity(iNote) |
456 |
|
) |
457 |
|
); |
458 |
|
} |
459 |
|
} |
460 |
|
} |
461 |
|
} |
462 |
|
|
463 |
|
// check if MIDI data arrived on some MIDI device |
464 |
|
for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) { |
465 |
|
const EventHandler::device_midi_listener_entry entry = |
466 |
|
eventHandler.deviceMidiListeners[i]; |
467 |
|
VirtualMidiDevice* pMidiListener = entry.pMidiListener; |
468 |
|
if (pMidiListener->NotesChanged()) { |
469 |
|
for (int iNote = 0; iNote < 128; iNote++) { |
470 |
|
if (pMidiListener->NoteChanged(iNote)) { |
471 |
|
const bool bActive = pMidiListener->NoteIsActive(iNote); |
472 |
|
LSCPServer::SendLSCPNotify( |
473 |
|
LSCPEvent( |
474 |
|
LSCPEvent::event_device_midi, |
475 |
|
entry.uiDeviceID, |
476 |
|
entry.pPort->GetPortNumber(), |
477 |
|
std::string(bActive ? "NOTE_ON" : "NOTE_OFF"), |
478 |
|
iNote, |
479 |
|
bActive ? pMidiListener->NoteOnVelocity(iNote) |
480 |
|
: pMidiListener->NoteOffVelocity(iNote) |
481 |
|
) |
482 |
|
); |
483 |
|
} |
484 |
|
} |
485 |
|
} |
486 |
|
} |
487 |
|
|
488 |
//Now let's deliver late notifies (if any) |
//Now let's deliver late notifies (if any) |
489 |
NotifyBufferMutex.Lock(); |
NotifyBufferMutex.Lock(); |
490 |
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++) { |
529 |
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
530 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
531 |
} |
} |
532 |
#else |
#else |
533 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
534 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
535 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
601 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
602 |
} |
} |
603 |
|
|
604 |
|
void LSCPServer::LockRTNotify() { |
605 |
|
RTNotifyMutex.Lock(); |
606 |
|
} |
607 |
|
|
608 |
|
void LSCPServer::UnlockRTNotify() { |
609 |
|
RTNotifyMutex.Unlock(); |
610 |
|
} |
611 |
|
|
612 |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) { |
613 |
int subs = 0; |
int subs = 0; |
614 |
SubscriptionMutex.Lock(); |
SubscriptionMutex.Lock(); |
708 |
int wsa_lasterror = WSAGetLastError(); |
int wsa_lasterror = WSAGetLastError(); |
709 |
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
710 |
return false; |
return false; |
711 |
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
712 |
CloseConnection(iter); |
CloseConnection(iter); |
713 |
break; |
break; |
714 |
} |
} |
2516 |
} |
} |
2517 |
|
|
2518 |
/** |
/** |
2519 |
|
* Will be called by the parser to return the current number of all active streams. |
2520 |
|
*/ |
2521 |
|
String LSCPServer::GetTotalStreamCount() { |
2522 |
|
dmsg(2,("LSCPServer: GetTotalStreamCount()\n")); |
2523 |
|
LSCPResultSet result; |
2524 |
|
result.Add(pSampler->GetDiskStreamCount()); |
2525 |
|
return result.Produce(); |
2526 |
|
} |
2527 |
|
|
2528 |
|
/** |
2529 |
* Will be called by the parser to return the current number of all active voices. |
* Will be called by the parser to return the current number of all active voices. |
2530 |
*/ |
*/ |
2531 |
String LSCPServer::GetTotalVoiceCount() { |
String LSCPServer::GetTotalVoiceCount() { |
2564 |
} |
} |
2565 |
|
|
2566 |
String LSCPServer::GetFileInstruments(String Filename) { |
String LSCPServer::GetFileInstruments(String Filename) { |
2567 |
|
dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2568 |
LSCPResultSet result; |
LSCPResultSet result; |
2569 |
|
try { |
2570 |
|
VerifyFile(Filename); |
2571 |
|
} catch (Exception e) { |
2572 |
|
result.Error(e); |
2573 |
|
return result.Produce(); |
2574 |
|
} |
2575 |
// try to find a sampler engine that can handle the file |
// try to find a sampler engine that can handle the file |
2576 |
bool bFound = false; |
bool bFound = false; |
2577 |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2594 |
} |
} |
2595 |
if (pEngine) EngineFactory::Destroy(pEngine); |
if (pEngine) EngineFactory::Destroy(pEngine); |
2596 |
} |
} |
2597 |
|
|
2598 |
|
if (!bFound) result.Error("Unknown file format"); |
2599 |
return result.Produce(); |
return result.Produce(); |
2600 |
} |
} |
2601 |
|
|
2602 |
String LSCPServer::ListFileInstruments(String Filename) { |
String LSCPServer::ListFileInstruments(String Filename) { |
2603 |
|
dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2604 |
LSCPResultSet result; |
LSCPResultSet result; |
2605 |
|
try { |
2606 |
|
VerifyFile(Filename); |
2607 |
|
} catch (Exception e) { |
2608 |
|
result.Error(e); |
2609 |
|
return result.Produce(); |
2610 |
|
} |
2611 |
// try to find a sampler engine that can handle the file |
// try to find a sampler engine that can handle the file |
2612 |
bool bFound = false; |
bool bFound = false; |
2613 |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2635 |
} |
} |
2636 |
if (pEngine) EngineFactory::Destroy(pEngine); |
if (pEngine) EngineFactory::Destroy(pEngine); |
2637 |
} |
} |
2638 |
|
|
2639 |
|
if (!bFound) result.Error("Unknown file format"); |
2640 |
return result.Produce(); |
return result.Produce(); |
2641 |
} |
} |
2642 |
|
|
2643 |
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
2644 |
|
dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID)); |
2645 |
LSCPResultSet result; |
LSCPResultSet result; |
2646 |
|
try { |
2647 |
|
VerifyFile(Filename); |
2648 |
|
} catch (Exception e) { |
2649 |
|
result.Error(e); |
2650 |
|
return result.Produce(); |
2651 |
|
} |
2652 |
InstrumentManager::instrument_id_t id; |
InstrumentManager::instrument_id_t id; |
2653 |
id.FileName = Filename; |
id.FileName = Filename; |
2654 |
id.Index = InstrumentID; |
id.Index = InstrumentID; |
2655 |
// try to find a sampler engine that can handle the file |
// try to find a sampler engine that can handle the file |
2656 |
bool bFound = false; |
bool bFound = false; |
2657 |
|
bool bFatalErr = false; |
2658 |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2659 |
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) { |
2660 |
Engine* pEngine = NULL; |
Engine* pEngine = NULL; |
2661 |
try { |
try { |
2662 |
pEngine = EngineFactory::Create(engineTypes[i]); |
pEngine = EngineFactory::Create(engineTypes[i]); |
2663 |
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2664 |
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2665 |
if (pManager) { |
if (pManager) { |
2666 |
|
// check if the instrument index is valid |
2667 |
|
// FIXME: this won't work if an engine only supports parts of the instrument file |
2668 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2669 |
|
pManager->GetInstrumentFileContent(Filename); |
2670 |
|
if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) { |
2671 |
|
std::stringstream ss; |
2672 |
|
ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'"; |
2673 |
|
bFatalErr = true; |
2674 |
|
throw Exception(ss.str()); |
2675 |
|
} |
2676 |
|
// get the info of the requested instrument |
2677 |
InstrumentManager::instrument_info_t info = |
InstrumentManager::instrument_info_t info = |
2678 |
pManager->GetInstrumentInfo(id); |
pManager->GetInstrumentInfo(id); |
2679 |
// return detailed informations about the file |
// return detailed informations about the file |
2680 |
result.Add("NAME", info.InstrumentName); |
result.Add("NAME", info.InstrumentName); |
2681 |
result.Add("FORMAT_NAME", engineTypes[i]); |
result.Add("FORMAT_FAMILY", engineTypes[i]); |
2682 |
result.Add("FORMAT_VERSION", info.FormatVersion); |
result.Add("FORMAT_VERSION", info.FormatVersion); |
2683 |
result.Add("PRODUCT", info.Product); |
result.Add("PRODUCT", info.Product); |
2684 |
result.Add("ARTISTS", info.Artists); |
result.Add("ARTISTS", info.Artists); |
2686 |
bFound = true; |
bFound = true; |
2687 |
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2688 |
} catch (Exception e) { |
} catch (Exception e) { |
2689 |
// NOOP, as exception is thrown if engine doesn't support file |
// usually NOOP, as exception is thrown if engine doesn't support file |
2690 |
|
if (bFatalErr) result.Error(e); |
2691 |
} |
} |
2692 |
if (pEngine) EngineFactory::Destroy(pEngine); |
if (pEngine) EngineFactory::Destroy(pEngine); |
2693 |
} |
} |
2694 |
|
|
2695 |
|
if (!bFound && !bFatalErr) result.Error("Unknown file format"); |
2696 |
return result.Produce(); |
return result.Produce(); |
2697 |
} |
} |
2698 |
|
|
2699 |
|
void LSCPServer::VerifyFile(String Filename) { |
2700 |
|
#if WIN32 |
2701 |
|
WIN32_FIND_DATA win32FileAttributeData; |
2702 |
|
BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData ); |
2703 |
|
if (!res) { |
2704 |
|
std::stringstream ss; |
2705 |
|
ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError(); |
2706 |
|
throw Exception(ss.str()); |
2707 |
|
} |
2708 |
|
if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { |
2709 |
|
throw Exception("Directory is specified"); |
2710 |
|
} |
2711 |
|
#else |
2712 |
|
struct stat statBuf; |
2713 |
|
int res = stat(Filename.c_str(), &statBuf); |
2714 |
|
if (res) { |
2715 |
|
std::stringstream ss; |
2716 |
|
ss << "Fail to stat `" << Filename << "`: " << strerror(errno); |
2717 |
|
throw Exception(ss.str()); |
2718 |
|
} |
2719 |
|
|
2720 |
|
if (S_ISDIR(statBuf.st_mode)) { |
2721 |
|
throw Exception("Directory is specified"); |
2722 |
|
} |
2723 |
|
#endif |
2724 |
|
} |
2725 |
|
|
2726 |
/** |
/** |
2727 |
* 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 |
2728 |
* server for receiving event messages. |
* server for receiving event messages. |