/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/network/lscpserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2137 - (hide annotations) (download)
Mon Oct 4 12:20:23 2010 UTC (13 years, 6 months ago) by schoenebeck
File size: 151595 byte(s)
* revised previously added new LSCP commands regarding effect handling:
  renamed "master effects" to "send effects", since this is the actual
  correct common term for those effects
* also corrected the names regarding "send effects" in the respective
  methods of the "FxSsnd" class and "AudioOutputDevice" class of the
  sampler's C++ API, the old methods are still available but marked as
  deprecated and scheduled for removal
* added LSCP command "SET FX_SEND SEND_EFFECT <sampler_channel>
  <fx_send_id> <effect_chain> <chain_pos>"
* added LSCP command "REMOVE FX_SEND SEND_EFFECT <sampler_channel>
  <fx_send_id>"
* added a list of common known LADSPA paths (for Windows and POSIX) which
  will be automatically checked for being used as LADSPA plugin directory
  (if the user did not set the LADSPA_PATH environment variable explicitly)
* bumped version to 1.0.0.cvs8

1 schoenebeck 35 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 2137 * Copyright (C) 2005 - 2010 Christian Schoenebeck *
7 schoenebeck 35 * *
8 schoenebeck 385 * This library is free software; you can redistribute it and/or modify *
9 schoenebeck 35 * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13 schoenebeck 385 * This library is distributed in the hope that it will be useful, *
14 schoenebeck 35 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19 schoenebeck 385 * along with this library; if not, write to the Free Software *
20 schoenebeck 35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24 persson 1713 #include <algorithm>
25 iliev 1771 #include <string>
26 persson 1713
27 iliev 1835 #include "../common/File.h"
28 schoenebeck 35 #include "lscpserver.h"
29 senkov 113 #include "lscpresultset.h"
30 senkov 170 #include "lscpevent.h"
31 schoenebeck 35
32 senoner 1481 #if defined(WIN32)
33 senoner 1537 #include <windows.h>
34 senoner 1481 #else
35 schoenebeck 411 #include <fcntl.h>
36 senoner 1481 #endif
37 schoenebeck 411
38 iliev 1161 #if ! HAVE_SQLITE3
39     #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
40 senkov 397 #endif
41    
42 schoenebeck 411 #include "../engines/EngineFactory.h"
43 schoenebeck 660 #include "../engines/EngineChannelFactory.h"
44 schoenebeck 203 #include "../drivers/audio/AudioOutputDeviceFactory.h"
45     #include "../drivers/midi/MidiInputDeviceFactory.h"
46 schoenebeck 2135 #include "../effects/EffectFactory.h"
47 schoenebeck 53
48 persson 1765 namespace LinuxSampler {
49 schoenebeck 1399
50 senkov 170 /**
51 schoenebeck 1399 * Returns a copy of the given string where all special characters are
52     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
53     * to escape LSCP response fields in case the respective response field is
54     * actually defined as using escape sequences in the LSCP specs.
55     *
56     * @e Caution: DO NOT use this function for escaping path based responses,
57     * use the Path class (src/common/Path.h) for this instead!
58     */
59     static String _escapeLscpResponse(String txt) {
60     for (int i = 0; i < txt.length(); i++) {
61     const char c = txt.c_str()[i];
62     if (
63     !(c >= '0' && c <= '9') &&
64     !(c >= 'a' && c <= 'z') &&
65     !(c >= 'A' && c <= 'Z') &&
66     !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
67     !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
68     !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
69     !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
70     !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
71 iliev 1403 !(c == '@') && !(c == '[') && !(c == ']') &&
72 schoenebeck 1399 !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
73     !(c == '|') && !(c == '}') && !(c == '~')
74     ) {
75     // convert the "special" character into a "\xHH" LSCP escape sequence
76     char buf[5];
77     snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
78     txt.replace(i, 1, buf);
79     i += 3;
80     }
81     }
82     return txt;
83     }
84    
85     /**
86 senkov 170 * Below are a few static members of the LSCPServer class.
87     * The big assumption here is that LSCPServer is going to remain a singleton.
88     * These members are used to support client connections.
89     * Class handles multiple connections at the same time using select() and non-blocking recv()
90     * Commands are processed by a single LSCPServer thread.
91     * Notifications are delivered either by the thread that originated them
92     * or (if the resultset is currently in progress) by the LSCPServer thread
93     * after the resultset was sent out.
94     * This makes sure that resultsets can not be interrupted by notifications.
95     * This also makes sure that the thread sending notification is not blocked
96     * by the LSCPServer thread.
97     */
98     fd_set LSCPServer::fdSet;
99     int LSCPServer::currentSocket = -1;
100 schoenebeck 210 std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
101 schoenebeck 1252 std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
102 senkov 170 std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
103     std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
104     std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
105     Mutex LSCPServer::NotifyMutex = Mutex();
106     Mutex LSCPServer::NotifyBufferMutex = Mutex();
107     Mutex LSCPServer::SubscriptionMutex = Mutex();
108 senkov 360 Mutex LSCPServer::RTNotifyMutex = Mutex();
109 senkov 170
110 schoenebeck 1686 LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
111 senkov 667 SocketAddress.sin_family = AF_INET;
112     SocketAddress.sin_addr.s_addr = addr;
113     SocketAddress.sin_port = port;
114 schoenebeck 53 this->pSampler = pSampler;
115 iliev 981 LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_count, "AUDIO_OUTPUT_DEVICE_COUNT");
116     LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_info, "AUDIO_OUTPUT_DEVICE_INFO");
117     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_count, "MIDI_INPUT_DEVICE_COUNT");
118     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_info, "MIDI_INPUT_DEVICE_INFO");
119 schoenebeck 556 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
120 senkov 170 LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
121     LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
122     LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
123 schoenebeck 556 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
124 iliev 1108 LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
125     LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
126 iliev 981 LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
127     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
128     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
129     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
130 iliev 1161 LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
131     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
132     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
133     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
134 iliev 1200 LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
135 senkov 170 LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
136 iliev 1541 LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
137 iliev 778 LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
138 iliev 1108 LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
139 schoenebeck 1686 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
140 schoenebeck 1695 LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
141 schoenebeck 475 hSocket = -1;
142 schoenebeck 35 }
143    
144 schoenebeck 475 LSCPServer::~LSCPServer() {
145 iliev 1848 CloseAllConnections();
146 persson 1850 InstrumentManager::StopBackgroundThread();
147 senoner 1481 #if defined(WIN32)
148     if (hSocket >= 0) closesocket(hSocket);
149     #else
150 schoenebeck 475 if (hSocket >= 0) close(hSocket);
151 senoner 1481 #endif
152 schoenebeck 475 }
153    
154 schoenebeck 1686 LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
155     this->pParent = pParent;
156     }
157    
158     LSCPServer::EventHandler::~EventHandler() {
159     std::vector<midi_listener_entry> l = channelMidiListeners;
160     channelMidiListeners.clear();
161     for (int i = 0; i < l.size(); i++)
162     delete l[i].pMidiListener;
163     }
164    
165 iliev 1133 void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
166 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
167     }
168    
169 schoenebeck 1686 void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
170     pChannel->AddEngineChangeListener(this);
171     }
172    
173     void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
174     if (!pChannel->GetEngineChannel()) return;
175     EngineToBeChanged(pChannel->Index());
176     }
177    
178     void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
179     SamplerChannel* pSamplerChannel =
180     pParent->pSampler->GetSamplerChannel(ChannelId);
181     if (!pSamplerChannel) return;
182     EngineChannel* pEngineChannel =
183     pSamplerChannel->GetEngineChannel();
184     if (!pEngineChannel) return;
185     for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
186     if ((*iter).pEngineChannel == pEngineChannel) {
187     VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
188     pEngineChannel->Disconnect(pMidiListener);
189     channelMidiListeners.erase(iter);
190     delete pMidiListener;
191     return;
192     }
193     }
194     }
195    
196     void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
197     SamplerChannel* pSamplerChannel =
198     pParent->pSampler->GetSamplerChannel(ChannelId);
199     if (!pSamplerChannel) return;
200     EngineChannel* pEngineChannel =
201     pSamplerChannel->GetEngineChannel();
202     if (!pEngineChannel) return;
203     VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
204     pEngineChannel->Connect(pMidiListener);
205     midi_listener_entry entry = {
206     pSamplerChannel, pEngineChannel, pMidiListener
207     };
208     channelMidiListeners.push_back(entry);
209     }
210    
211 iliev 1133 void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
212 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
213     }
214    
215 iliev 1133 void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
216 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
217     }
218    
219 schoenebeck 1695 void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
220     pDevice->RemoveMidiPortCountListener(this);
221     for (int i = 0; i < pDevice->PortCount(); ++i)
222     MidiPortToBeRemoved(pDevice->GetPort(i));
223     }
224    
225     void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
226     pDevice->AddMidiPortCountListener(this);
227     for (int i = 0; i < pDevice->PortCount(); ++i)
228     MidiPortAdded(pDevice->GetPort(i));
229     }
230    
231     void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
232     // yet unused
233     }
234    
235     void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
236     for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
237     if ((*iter).pPort == pPort) {
238     VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
239     pPort->Disconnect(pMidiListener);
240     deviceMidiListeners.erase(iter);
241     delete pMidiListener;
242     return;
243     }
244     }
245     }
246    
247     void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
248     // find out the device ID
249     std::map<uint, MidiInputDevice*> devices =
250     pParent->pSampler->GetMidiInputDevices();
251     for (
252     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
253     iter != devices.end(); ++iter
254     ) {
255     if (iter->second == pPort->GetDevice()) { // found
256     VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
257     pPort->Connect(pMidiListener);
258     device_midi_listener_entry entry = {
259     pPort, pMidiListener, iter->first
260     };
261     deviceMidiListeners.push_back(entry);
262     return;
263     }
264     }
265     }
266    
267 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
268 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
269     }
270    
271 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
272 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
273     }
274    
275 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
276 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
277     }
278    
279 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
280 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
281     }
282    
283 iliev 1133 void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
284 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
285     }
286    
287 iliev 1133 void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
288 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
289     }
290    
291 iliev 1133 void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
292 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
293     }
294    
295 iliev 1133 void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
296 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
297     }
298    
299 iliev 1133 void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
300 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
301     }
302    
303 iliev 1541 void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
304     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
305     }
306    
307 iliev 1161 #if HAVE_SQLITE3
308     void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
309 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
310 iliev 1161 }
311 iliev 1130
312 iliev 1161 void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
313 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
314 iliev 1161 }
315    
316     void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
317 iliev 1345 Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
318 iliev 1350 NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
319 iliev 1161 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
320     }
321    
322     void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
323 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
324 iliev 1161 }
325    
326     void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
327 iliev 1345 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
328 iliev 1161 }
329 iliev 1200
330 iliev 1161 void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
331 iliev 1345 Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
332 iliev 1350 NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
333 iliev 1161 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
334     }
335 iliev 1200
336     void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
337     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
338     }
339 iliev 1161 #endif // HAVE_SQLITE3
340    
341 iliev 1835 void LSCPServer::RemoveListeners() {
342     pSampler->RemoveChannelCountListener(&eventHandler);
343     pSampler->RemoveAudioDeviceCountListener(&eventHandler);
344     pSampler->RemoveMidiDeviceCountListener(&eventHandler);
345     pSampler->RemoveVoiceCountListener(&eventHandler);
346     pSampler->RemoveStreamCountListener(&eventHandler);
347     pSampler->RemoveBufferFillListener(&eventHandler);
348     pSampler->RemoveTotalStreamCountListener(&eventHandler);
349     pSampler->RemoveTotalVoiceCountListener(&eventHandler);
350     pSampler->RemoveFxSendCountListener(&eventHandler);
351     MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler);
352     MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler);
353     MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler);
354     MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler);
355     #if HAVE_SQLITE3
356     InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler);
357     #endif
358     }
359 iliev 1161
360 schoenebeck 211 /**
361     * Blocks the calling thread until the LSCP Server is initialized and
362     * accepting socket connections, if the server is already initialized then
363     * this method will return immediately.
364     * @param TimeoutSeconds - optional: max. wait time in seconds
365     * (default: 0s)
366     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
367     * (default: 0ns)
368     * @returns 0 on success, a value less than 0 if timeout exceeded
369     */
370     int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
371     return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
372     }
373    
374 schoenebeck 35 int LSCPServer::Main() {
375 senoner 1481 #if defined(WIN32)
376     WSADATA wsaData;
377     int iResult;
378     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
379     if (iResult != 0) {
380     std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
381     exit(EXIT_FAILURE);
382     }
383     #endif
384 schoenebeck 475 hSocket = socket(AF_INET, SOCK_STREAM, 0);
385 schoenebeck 35 if (hSocket < 0) {
386     std::cerr << "LSCPServer: Could not create server socket." << std::endl;
387 schoenebeck 53 //return -1;
388     exit(EXIT_FAILURE);
389 schoenebeck 35 }
390    
391     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
392 schoenebeck 227 std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
393     for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
394     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
395     if (trial > LSCP_SERVER_BIND_TIMEOUT) {
396     std::cerr << "gave up!" << std::endl;
397 senoner 1481 #if defined(WIN32)
398     closesocket(hSocket);
399     #else
400 schoenebeck 227 close(hSocket);
401 senoner 1481 #endif
402 schoenebeck 227 //return -1;
403     exit(EXIT_FAILURE);
404     }
405     else sleep(1); // sleep 1s
406     }
407     else break; // success
408     }
409 schoenebeck 35 }
410    
411     listen(hSocket, 1);
412 schoenebeck 211 Initialized.Set(true);
413 schoenebeck 1252
414 iliev 1130 // Registering event listeners
415 iliev 1133 pSampler->AddChannelCountListener(&eventHandler);
416     pSampler->AddAudioDeviceCountListener(&eventHandler);
417     pSampler->AddMidiDeviceCountListener(&eventHandler);
418     pSampler->AddVoiceCountListener(&eventHandler);
419     pSampler->AddStreamCountListener(&eventHandler);
420     pSampler->AddBufferFillListener(&eventHandler);
421 iliev 1541 pSampler->AddTotalStreamCountListener(&eventHandler);
422 iliev 1133 pSampler->AddTotalVoiceCountListener(&eventHandler);
423     pSampler->AddFxSendCountListener(&eventHandler);
424     MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
425     MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
426     MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
427     MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
428 iliev 1161 #if HAVE_SQLITE3
429     InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
430     #endif
431 schoenebeck 35 // now wait for client connections and handle their requests
432     sockaddr_in client;
433     int length = sizeof(client);
434 senkov 170 FD_ZERO(&fdSet);
435     FD_SET(hSocket, &fdSet);
436     int maxSessions = hSocket;
437 schoenebeck 203
438 iliev 793 timeval timeout;
439    
440 schoenebeck 35 while (true) {
441 nagata 1649 #if CONFIG_PTHREAD_TESTCANCEL
442     TestCancel();
443     #endif
444 iliev 793 // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
445     {
446     std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
447     std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
448     std::set<EngineChannel*>::iterator itEnd = engineChannels.end();
449     for (; itEngineChannel != itEnd; ++itEngineChannel) {
450     if ((*itEngineChannel)->StatusChanged()) {
451 iliev 1761 SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index()));
452 iliev 793 }
453 iliev 1108
454     for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
455     FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
456     if(fxs != NULL && fxs->IsInfoChanged()) {
457 iliev 1761 int chn = (*itEngineChannel)->GetSamplerChannel()->Index();
458 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
459     fxs->SetInfoChanged(false);
460     }
461     }
462 iliev 793 }
463     }
464    
465 schoenebeck 1686 // check if MIDI data arrived on some engine channel
466     for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
467     const EventHandler::midi_listener_entry entry =
468     eventHandler.channelMidiListeners[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_channel_midi,
477     entry.pSamplerChannel->Index(),
478     std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
479     iNote,
480     bActive ? pMidiListener->NoteOnVelocity(iNote)
481     : pMidiListener->NoteOffVelocity(iNote)
482     )
483     );
484     }
485     }
486     }
487     }
488    
489 schoenebeck 1695 // check if MIDI data arrived on some MIDI device
490     for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
491     const EventHandler::device_midi_listener_entry entry =
492     eventHandler.deviceMidiListeners[i];
493     VirtualMidiDevice* pMidiListener = entry.pMidiListener;
494     if (pMidiListener->NotesChanged()) {
495     for (int iNote = 0; iNote < 128; iNote++) {
496     if (pMidiListener->NoteChanged(iNote)) {
497     const bool bActive = pMidiListener->NoteIsActive(iNote);
498     LSCPServer::SendLSCPNotify(
499     LSCPEvent(
500     LSCPEvent::event_device_midi,
501     entry.uiDeviceID,
502     entry.pPort->GetPortNumber(),
503     std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
504     iNote,
505     bActive ? pMidiListener->NoteOnVelocity(iNote)
506     : pMidiListener->NoteOffVelocity(iNote)
507     )
508     );
509     }
510     }
511     }
512     }
513    
514 iliev 793 //Now let's deliver late notifies (if any)
515     NotifyBufferMutex.Lock();
516     for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
517 wylder 814 #ifdef MSG_NOSIGNAL
518 iliev 793 send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
519 wylder 814 #else
520     send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
521     #endif
522 iliev 793 }
523 persson 836 bufferedNotifies.clear();
524 iliev 793 NotifyBufferMutex.Unlock();
525    
526     fd_set selectSet = fdSet;
527     timeout.tv_sec = 0;
528     timeout.tv_usec = 100000;
529    
530     int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
531    
532 persson 1808 if (retval == 0 || (retval == -1 && errno == EINTR))
533 senkov 198 continue; //Nothing try again
534 senkov 170 if (retval == -1) {
535     std::cerr << "LSCPServer: Socket select error." << std::endl;
536 senoner 1481 #if defined(WIN32)
537     closesocket(hSocket);
538     #else
539 senkov 170 close(hSocket);
540 senoner 1481 #endif
541 senkov 170 exit(EXIT_FAILURE);
542     }
543 schoenebeck 203
544 senkov 170 //Accept new connections now (if any)
545     if (FD_ISSET(hSocket, &selectSet)) {
546     int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
547     if (socket < 0) {
548     std::cerr << "LSCPServer: Client connection failed." << std::endl;
549     exit(EXIT_FAILURE);
550     }
551 schoenebeck 35
552 senoner 1481 #if defined(WIN32)
553     u_long nonblock_io = 1;
554     if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
555     std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
556     exit(EXIT_FAILURE);
557     }
558 schoenebeck 1536 #else
559 iliev 1882 struct linger linger;
560     linger.l_onoff = 1;
561     linger.l_linger = 0;
562     if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) {
563     std::cerr << "LSCPServer: Failed to set SO_LINGER\n";
564     }
565    
566 senkov 170 if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
567     std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
568     exit(EXIT_FAILURE);
569     }
570 senoner 1481 #endif
571 schoenebeck 35
572 schoenebeck 210 // Parser initialization
573     yyparse_param_t yyparse_param;
574     yyparse_param.pServer = this;
575     yyparse_param.hSession = socket;
576    
577     Sessions.push_back(yyparse_param);
578 senkov 170 FD_SET(socket, &fdSet);
579     if (socket > maxSessions)
580     maxSessions = socket;
581     dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
582     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
583     continue; //Maybe this was the only selected socket, better select again
584     }
585 schoenebeck 35
586 senkov 170 //Something was selected and it was not the hSocket, so it must be some command(s) coming.
587 schoenebeck 210 for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
588     if (FD_ISSET((*iter).hSession, &selectSet)) { //Was it this socket?
589 senkov 170 if (GetLSCPCommand(iter)) { //Have we read the entire command?
590     dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
591 schoenebeck 219 int dummy; // just a temporary hack to fulfill the restart() function prototype
592     restart(NULL, dummy); // restart the 'scanner'
593 schoenebeck 210 currentSocket = (*iter).hSession; //a hack
594 schoenebeck 1252 itCurrentSession = iter; // another hack
595 schoenebeck 475 dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
596 schoenebeck 210 if ((*iter).bVerbose) { // if echo mode enabled
597     AnswerClient(bufferedCommands[currentSocket]);
598     }
599     int result = yyparse(&(*iter));
600 senkov 170 currentSocket = -1; //continuation of a hack
601 schoenebeck 1252 itCurrentSession = Sessions.end(); // hack as well
602 senkov 170 dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
603     if (result == LSCP_QUIT) { //Was it a quit command by any chance?
604     CloseConnection(iter);
605     }
606     }
607     //socket may have been closed, iter may be invalid, get out of the loop for now.
608     //we'll be back if there is data.
609 schoenebeck 203 break;
610 senkov 170 }
611     }
612 schoenebeck 35 }
613     }
614    
615 schoenebeck 210 void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
616     int socket = (*iter).hSession;
617 senkov 170 dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
618     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
619 schoenebeck 210 Sessions.erase(iter);
620 senkov 170 FD_CLR(socket, &fdSet);
621     SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
622     for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
623     iter->second.remove(socket);
624     }
625     SubscriptionMutex.Unlock();
626     NotifyMutex.Lock();
627     bufferedCommands.erase(socket);
628     bufferedNotifies.erase(socket);
629 senoner 1481 #if defined(WIN32)
630     closesocket(socket);
631     #else
632 senkov 170 close(socket);
633 senoner 1481 #endif
634 senkov 170 NotifyMutex.Unlock();
635     }
636    
637 iliev 1848 void LSCPServer::CloseAllConnections() {
638     std::vector<yyparse_param_t>::iterator iter = Sessions.begin();
639     while(iter != Sessions.end()) {
640     CloseConnection(iter);
641     iter = Sessions.begin();
642     }
643     }
644    
645 schoenebeck 1551 void LSCPServer::LockRTNotify() {
646     RTNotifyMutex.Lock();
647     }
648    
649     void LSCPServer::UnlockRTNotify() {
650     RTNotifyMutex.Unlock();
651     }
652    
653 senkov 360 int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
654     int subs = 0;
655     SubscriptionMutex.Lock();
656     for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
657     iter != events.end(); iter++)
658     {
659     subs += eventSubscriptions.count(*iter);
660     }
661     SubscriptionMutex.Unlock();
662     return subs;
663     }
664    
665 senkov 170 void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
666     SubscriptionMutex.Lock();
667     if (eventSubscriptions.count(event.GetType()) == 0) {
668     SubscriptionMutex.Unlock(); //Nobody is subscribed to this event
669     return;
670     }
671     std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
672     std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
673     String notify = event.Produce();
674    
675     while (true) {
676     if (NotifyMutex.Trylock()) {
677     for(;iter != end; iter++)
678 wylder 814 #ifdef MSG_NOSIGNAL
679 iliev 707 send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
680 wylder 814 #else
681     send(*iter, notify.c_str(), notify.size(), 0);
682     #endif
683 senkov 170 NotifyMutex.Unlock();
684     break;
685     } else {
686     if (NotifyBufferMutex.Trylock()) {
687     for(;iter != end; iter++)
688     bufferedNotifies[*iter] += notify;
689     NotifyBufferMutex.Unlock();
690     break;
691     }
692     }
693     }
694     SubscriptionMutex.Unlock();
695     }
696    
697     extern int GetLSCPCommand( void *buf, int max_size ) {
698     String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
699     if (command.size() == 0) { //Parser wants input but we have nothing.
700     strcpy((char*) buf, "\n"); //So give it an empty command
701     return 1; //to keep it happy.
702     }
703    
704     if (max_size < command.size()) {
705     std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
706     return 0; //This will never happen
707     }
708    
709     strcpy((char*) buf, command.c_str());
710     LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
711     return command.size();
712     }
713    
714 schoenebeck 1252 extern yyparse_param_t* GetCurrentYaccSession() {
715     return &(*itCurrentSession);
716     }
717    
718 schoenebeck 35 /**
719 senkov 170 * Will be called to try to read the command from the socket
720     * If command is read, it will return true. Otherwise false is returned.
721     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
722     */
723 schoenebeck 210 bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
724     int socket = (*iter).hSession;
725 senkov 170 char c;
726     int i = 0;
727     while (true) {
728 senoner 1481 #if defined(WIN32)
729     int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
730     #else
731 senkov 170 int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
732 senoner 1481 #endif
733 senkov 170 if (result == 0) { //socket was selected, so 0 here means client has closed the connection
734     CloseConnection(iter);
735     break;
736     }
737     if (result == 1) {
738 schoenebeck 203 if (c == '\r')
739 senkov 170 continue; //Ignore CR
740     if (c == '\n') {
741 senkov 184 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
742 iliev 907 bufferedCommands[socket] += "\r\n";
743 senkov 170 return true; //Complete command was read
744     }
745     bufferedCommands[socket] += c;
746     }
747 senoner 1481 #if defined(WIN32)
748     if (result == SOCKET_ERROR) {
749     int wsa_lasterror = WSAGetLastError();
750     if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
751     return false;
752 schoenebeck 1536 dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
753 senoner 1481 CloseConnection(iter);
754     break;
755     }
756     #else
757 senkov 170 if (result == -1) {
758     if (errno == EAGAIN) //Would block, try again later.
759     return false;
760     switch(errno) {
761     case EBADF:
762     dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
763     break;
764     case ECONNREFUSED:
765     dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
766     break;
767     case ENOTCONN:
768     dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
769     break;
770     case ENOTSOCK:
771     dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
772     break;
773     case EAGAIN:
774     dmsg(2,("LSCPScanner: The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received.\n"));
775 schoenebeck 203 break;
776     case EINTR:
777 senkov 170 dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
778 schoenebeck 203 break;
779     case EFAULT:
780     dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
781     break;
782     case EINVAL:
783     dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
784     break;
785     case ENOMEM:
786     dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
787     break;
788     default:
789     dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
790     break;
791     }
792 senkov 170 CloseConnection(iter);
793     break;
794     }
795 senoner 1481 #endif
796 senkov 170 }
797     return false;
798     }
799    
800     /**
801 schoenebeck 35 * Will be called by the parser whenever it wants to send an answer to the
802     * client / frontend.
803     *
804     * @param ReturnMessage - message that will be send to the client
805     */
806     void LSCPServer::AnswerClient(String ReturnMessage) {
807     dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
808 senkov 170 if (currentSocket != -1) {
809     NotifyMutex.Lock();
810 wylder 814 #ifdef MSG_NOSIGNAL
811 iliev 707 send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
812 wylder 814 #else
813     send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
814     #endif
815 senkov 170 NotifyMutex.Unlock();
816     }
817 schoenebeck 35 }
818    
819 capela 143 /**
820     * Find a created audio output device index.
821     */
822     int LSCPServer::GetAudioOutputDeviceIndex ( AudioOutputDevice *pDevice )
823     {
824     // Search for the created device to get its index
825     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
826     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
827     for (; iter != devices.end(); iter++) {
828     if (iter->second == pDevice)
829     return iter->first;
830     }
831     // Not found.
832     return -1;
833     }
834    
835 senkov 155 /**
836     * Find a created midi input device index.
837     */
838     int LSCPServer::GetMidiInputDeviceIndex ( MidiInputDevice *pDevice )
839     {
840     // Search for the created device to get its index
841     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
842     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
843     for (; iter != devices.end(); iter++) {
844     if (iter->second == pDevice)
845     return iter->first;
846     }
847     // Not found.
848     return -1;
849     }
850    
851 schoenebeck 123 String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
852     dmsg(2,("LSCPServer: CreateAudioOutputDevice(Driver=%s)\n", Driver.c_str()));
853     LSCPResultSet result;
854     try {
855     AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
856     // search for the created device to get its index
857 capela 143 int index = GetAudioOutputDeviceIndex(pDevice);
858 schoenebeck 880 if (index == -1) throw Exception("Internal error: could not find created audio output device.");
859 schoenebeck 123 result = index; // success
860     }
861 schoenebeck 880 catch (Exception e) {
862 schoenebeck 123 result.Error(e);
863     }
864     return result.Produce();
865     }
866    
867 senkov 155 String LSCPServer::CreateMidiInputDevice(String Driver, std::map<String,String> Parameters) {
868     dmsg(2,("LSCPServer: CreateMidiInputDevice(Driver=%s)\n", Driver.c_str()));
869     LSCPResultSet result;
870     try {
871     MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
872     // search for the created device to get its index
873     int index = GetMidiInputDeviceIndex(pDevice);
874 schoenebeck 880 if (index == -1) throw Exception("Internal error: could not find created midi input device.");
875 senkov 155 result = index; // success
876     }
877 schoenebeck 880 catch (Exception e) {
878 senkov 155 result.Error(e);
879     }
880     return result.Produce();
881     }
882    
883 schoenebeck 123 String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
884     dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
885     LSCPResultSet result;
886     try {
887     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
888 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
889 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
890     pSampler->DestroyAudioOutputDevice(pDevice);
891     }
892 schoenebeck 880 catch (Exception e) {
893 schoenebeck 123 result.Error(e);
894     }
895     return result.Produce();
896     }
897    
898 senkov 155 String LSCPServer::DestroyMidiInputDevice(uint DeviceIndex) {
899     dmsg(2,("LSCPServer: DestroyMidiInputDevice(DeviceIndex=%d)\n", DeviceIndex));
900     LSCPResultSet result;
901     try {
902     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
903 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
904 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
905     pSampler->DestroyMidiInputDevice(pDevice);
906     }
907 schoenebeck 880 catch (Exception e) {
908 senkov 155 result.Error(e);
909     }
910     return result.Produce();
911     }
912    
913 iliev 1135 EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
914     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
915     if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
916    
917     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
918     if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
919    
920 schoenebeck 1252 return pEngineChannel;
921 iliev 1135 }
922    
923 schoenebeck 35 /**
924     * Will be called by the parser to load an instrument.
925     */
926 capela 137 String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground) {
927 schoenebeck 53 dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));
928 senkov 120 LSCPResultSet result;
929 schoenebeck 53 try {
930     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
931 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
932 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
933 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel yet");
934 schoenebeck 223 if (!pSamplerChannel->GetAudioOutputDevice())
935 schoenebeck 880 throw Exception("No audio output device connected to sampler channel");
936 capela 137 if (bBackground) {
937 schoenebeck 947 InstrumentManager::instrument_id_t id;
938     id.FileName = Filename;
939     id.Index = uiInstrument;
940     InstrumentManager::LoadInstrumentInBackground(id, pEngineChannel);
941 capela 137 }
942 schoenebeck 392 else {
943 schoenebeck 411 // tell the engine channel which instrument to load
944     pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
945 schoenebeck 392 // actually start to load the instrument (blocks until completed)
946 schoenebeck 411 pEngineChannel->LoadInstrument();
947 schoenebeck 392 }
948 schoenebeck 53 }
949 schoenebeck 880 catch (Exception e) {
950 senkov 120 result.Error(e);
951 schoenebeck 53 }
952 senkov 120 return result.Produce();
953 schoenebeck 35 }
954    
955     /**
956 schoenebeck 411 * Will be called by the parser to assign a sampler engine type to a
957     * sampler channel.
958 schoenebeck 35 */
959 schoenebeck 411 String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
960 schoenebeck 705 dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
961 senkov 120 LSCPResultSet result;
962 schoenebeck 475 try {
963 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
964 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
965 senkov 360 LockRTNotify();
966 schoenebeck 411 pSamplerChannel->SetEngineType(EngineName);
967 schoenebeck 705 if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
968 senkov 360 UnlockRTNotify();
969 schoenebeck 53 }
970 schoenebeck 880 catch (Exception e) {
971 senkov 120 result.Error(e);
972 schoenebeck 53 }
973 senkov 120 return result.Produce();
974 schoenebeck 35 }
975    
976     /**
977     * Will be called by the parser to get the amount of sampler channels.
978     */
979     String LSCPServer::GetChannels() {
980     dmsg(2,("LSCPServer: GetChannels()\n"));
981 senkov 120 LSCPResultSet result;
982     result.Add(pSampler->SamplerChannels());
983     return result.Produce();
984 schoenebeck 35 }
985    
986     /**
987 schoenebeck 209 * Will be called by the parser to get the list of sampler channels.
988     */
989     String LSCPServer::ListChannels() {
990     dmsg(2,("LSCPServer: ListChannels()\n"));
991     String list;
992     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
993     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
994     for (; iter != channels.end(); iter++) {
995     if (list != "") list += ",";
996     list += ToString(iter->first);
997     }
998     LSCPResultSet result;
999     result.Add(list);
1000     return result.Produce();
1001     }
1002    
1003     /**
1004 schoenebeck 35 * Will be called by the parser to add a sampler channel.
1005     */
1006     String LSCPServer::AddChannel() {
1007     dmsg(2,("LSCPServer: AddChannel()\n"));
1008 persson 841 LockRTNotify();
1009 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
1010 persson 841 UnlockRTNotify();
1011 senkov 120 LSCPResultSet result(pSamplerChannel->Index());
1012     return result.Produce();
1013 schoenebeck 35 }
1014    
1015     /**
1016     * Will be called by the parser to remove a sampler channel.
1017     */
1018 schoenebeck 53 String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
1019     dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
1020 senkov 120 LSCPResultSet result;
1021 senkov 360 LockRTNotify();
1022 schoenebeck 53 pSampler->RemoveSamplerChannel(uiSamplerChannel);
1023 senkov 360 UnlockRTNotify();
1024 senkov 120 return result.Produce();
1025 schoenebeck 35 }
1026    
1027     /**
1028 capela 527 * Will be called by the parser to get the amount of all available engines.
1029 schoenebeck 35 */
1030     String LSCPServer::GetAvailableEngines() {
1031     dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
1032 schoenebeck 905 LSCPResultSet result;
1033     try {
1034     int n = EngineFactory::AvailableEngineTypes().size();
1035     result.Add(n);
1036     }
1037     catch (Exception e) {
1038     result.Error(e);
1039     }
1040 senkov 120 return result.Produce();
1041 schoenebeck 35 }
1042    
1043     /**
1044 capela 527 * Will be called by the parser to get a list of all available engines.
1045     */
1046     String LSCPServer::ListAvailableEngines() {
1047     dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
1048 schoenebeck 905 LSCPResultSet result;
1049     try {
1050     String s = EngineFactory::AvailableEngineTypesAsString();
1051     result.Add(s);
1052     }
1053     catch (Exception e) {
1054     result.Error(e);
1055     }
1056 capela 527 return result.Produce();
1057     }
1058    
1059     /**
1060 schoenebeck 411 * Will be called by the parser to get descriptions for a particular
1061     * sampler engine.
1062 schoenebeck 35 */
1063     String LSCPServer::GetEngineInfo(String EngineName) {
1064     dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
1065 senkov 120 LSCPResultSet result;
1066 persson 841 LockRTNotify();
1067 schoenebeck 53 try {
1068 schoenebeck 411 Engine* pEngine = EngineFactory::Create(EngineName);
1069 schoenebeck 1399 result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1070 schoenebeck 411 result.Add("VERSION", pEngine->Version());
1071 schoenebeck 660 EngineFactory::Destroy(pEngine);
1072 schoenebeck 53 }
1073 schoenebeck 880 catch (Exception e) {
1074 senkov 120 result.Error(e);
1075 schoenebeck 53 }
1076 persson 841 UnlockRTNotify();
1077 senkov 120 return result.Produce();
1078 schoenebeck 35 }
1079    
1080     /**
1081     * Will be called by the parser to get informations about a particular
1082     * sampler channel.
1083     */
1084 schoenebeck 53 String LSCPServer::GetChannelInfo(uint uiSamplerChannel) {
1085     dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", uiSamplerChannel));
1086 senkov 120 LSCPResultSet result;
1087 senkov 113 try {
1088     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1089 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1090 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1091 schoenebeck 123
1092 senkov 117 //Defaults values
1093     String EngineName = "NONE";
1094 schoenebeck 225 float Volume = 0.0f;
1095 senkov 117 String InstrumentFileName = "NONE";
1096 senkov 376 String InstrumentName = "NONE";
1097 capela 133 int InstrumentIndex = -1;
1098     int InstrumentStatus = -1;
1099 schoenebeck 225 int AudioOutputChannels = 0;
1100     String AudioRouting;
1101 schoenebeck 705 int Mute = 0;
1102     bool Solo = false;
1103 iliev 1130 String MidiInstrumentMap = "NONE";
1104 schoenebeck 123
1105 schoenebeck 475 if (pEngineChannel) {
1106     EngineName = pEngineChannel->EngineName();
1107 schoenebeck 411 AudioOutputChannels = pEngineChannel->Channels();
1108     Volume = pEngineChannel->Volume();
1109     InstrumentStatus = pEngineChannel->InstrumentStatus();
1110     InstrumentIndex = pEngineChannel->InstrumentIndex();
1111     if (InstrumentIndex != -1) {
1112     InstrumentFileName = pEngineChannel->InstrumentFileName();
1113     InstrumentName = pEngineChannel->InstrumentName();
1114     }
1115     for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
1116 schoenebeck 225 if (AudioRouting != "") AudioRouting += ",";
1117 schoenebeck 411 AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
1118 schoenebeck 225 }
1119 schoenebeck 705 Mute = pEngineChannel->GetMute();
1120     Solo = pEngineChannel->GetSolo();
1121 schoenebeck 973 if (pEngineChannel->UsesNoMidiInstrumentMap())
1122     MidiInstrumentMap = "NONE";
1123     else if (pEngineChannel->UsesDefaultMidiInstrumentMap())
1124     MidiInstrumentMap = "DEFAULT";
1125     else
1126     MidiInstrumentMap = ToString(pEngineChannel->GetMidiInstrumentMap());
1127 senkov 113 }
1128 senkov 117
1129     result.Add("ENGINE_NAME", EngineName);
1130     result.Add("VOLUME", Volume);
1131    
1132 capela 143 //Some not-so-hardcoded stuff to make GUI look good
1133     result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
1134 schoenebeck 225 result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
1135     result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
1136 senkov 113
1137 capela 159 result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
1138     result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
1139 schoenebeck 675 if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
1140 schoenebeck 274 else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
1141 capela 159
1142 schoenebeck 1471 // convert the filename into the correct encoding as defined for LSCP
1143     // (especially in terms of special characters -> escape sequences)
1144     if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
1145     #if WIN32
1146     InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
1147     #else
1148     // assuming POSIX
1149     InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
1150     #endif
1151     }
1152    
1153     result.Add("INSTRUMENT_FILE", InstrumentFileName);
1154 senkov 117 result.Add("INSTRUMENT_NR", InstrumentIndex);
1155 schoenebeck 1399 result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
1156 capela 133 result.Add("INSTRUMENT_STATUS", InstrumentStatus);
1157 schoenebeck 705 result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
1158     result.Add("SOLO", Solo);
1159 schoenebeck 973 result.Add("MIDI_INSTRUMENT_MAP", MidiInstrumentMap);
1160 senkov 113 }
1161 schoenebeck 880 catch (Exception e) {
1162 senkov 120 result.Error(e);
1163 senkov 113 }
1164 senkov 120 return result.Produce();
1165 schoenebeck 35 }
1166    
1167     /**
1168     * Will be called by the parser to get the amount of active voices on a
1169     * particular sampler channel.
1170     */
1171 schoenebeck 53 String LSCPServer::GetVoiceCount(uint uiSamplerChannel) {
1172     dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
1173 senkov 120 LSCPResultSet result;
1174 schoenebeck 53 try {
1175 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1176 schoenebeck 880 if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1177 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->VoiceCount());
1178 schoenebeck 53 }
1179 schoenebeck 880 catch (Exception e) {
1180 senkov 120 result.Error(e);
1181 schoenebeck 53 }
1182 senkov 120 return result.Produce();
1183 schoenebeck 35 }
1184    
1185     /**
1186     * Will be called by the parser to get the amount of active disk streams on a
1187     * particular sampler channel.
1188     */
1189 schoenebeck 53 String LSCPServer::GetStreamCount(uint uiSamplerChannel) {
1190     dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
1191 senkov 120 LSCPResultSet result;
1192 schoenebeck 53 try {
1193 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1194 schoenebeck 880 if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1195 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
1196 schoenebeck 53 }
1197 schoenebeck 880 catch (Exception e) {
1198 senkov 120 result.Error(e);
1199 schoenebeck 53 }
1200 senkov 120 return result.Produce();
1201 schoenebeck 35 }
1202    
1203     /**
1204     * Will be called by the parser to get the buffer fill states of all disk
1205     * streams on a particular sampler channel.
1206     */
1207 schoenebeck 53 String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel) {
1208     dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
1209 senkov 120 LSCPResultSet result;
1210 schoenebeck 53 try {
1211 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1212 schoenebeck 880 if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1213 schoenebeck 411 if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
1214 senkov 129 else {
1215     switch (ResponseType) {
1216     case fill_response_bytes:
1217 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
1218     break;
1219 senkov 129 case fill_response_percentage:
1220 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
1221     break;
1222 senkov 129 default:
1223 schoenebeck 880 throw Exception("Unknown fill response type");
1224 senkov 129 }
1225     }
1226 schoenebeck 53 }
1227 schoenebeck 880 catch (Exception e) {
1228 senkov 120 result.Error(e);
1229 schoenebeck 53 }
1230 senkov 120 return result.Produce();
1231 schoenebeck 35 }
1232    
1233 schoenebeck 123 String LSCPServer::GetAvailableAudioOutputDrivers() {
1234     dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
1235 senkov 120 LSCPResultSet result;
1236 schoenebeck 53 try {
1237 capela 527 int n = AudioOutputDeviceFactory::AvailableDrivers().size();
1238     result.Add(n);
1239     }
1240 schoenebeck 880 catch (Exception e) {
1241 capela 527 result.Error(e);
1242     }
1243     return result.Produce();
1244     }
1245    
1246     String LSCPServer::ListAvailableAudioOutputDrivers() {
1247     dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
1248     LSCPResultSet result;
1249     try {
1250 schoenebeck 123 String s = AudioOutputDeviceFactory::AvailableDriversAsString();
1251     result.Add(s);
1252 schoenebeck 53 }
1253 schoenebeck 880 catch (Exception e) {
1254 schoenebeck 123 result.Error(e);
1255 schoenebeck 53 }
1256 senkov 120 return result.Produce();
1257 schoenebeck 35 }
1258    
1259 senkov 155 String LSCPServer::GetAvailableMidiInputDrivers() {
1260     dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
1261     LSCPResultSet result;
1262     try {
1263 capela 527 int n = MidiInputDeviceFactory::AvailableDrivers().size();
1264     result.Add(n);
1265     }
1266 schoenebeck 880 catch (Exception e) {
1267 capela 527 result.Error(e);
1268     }
1269     return result.Produce();
1270     }
1271    
1272     String LSCPServer::ListAvailableMidiInputDrivers() {
1273     dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
1274     LSCPResultSet result;
1275     try {
1276 senkov 155 String s = MidiInputDeviceFactory::AvailableDriversAsString();
1277     result.Add(s);
1278     }
1279 schoenebeck 880 catch (Exception e) {
1280 senkov 155 result.Error(e);
1281     }
1282     return result.Produce();
1283     }
1284    
1285     String LSCPServer::GetMidiInputDriverInfo(String Driver) {
1286     dmsg(2,("LSCPServer: GetMidiInputDriverInfo(Driver=%s)\n",Driver.c_str()));
1287     LSCPResultSet result;
1288     try {
1289     result.Add("DESCRIPTION", MidiInputDeviceFactory::GetDriverDescription(Driver));
1290     result.Add("VERSION", MidiInputDeviceFactory::GetDriverVersion(Driver));
1291    
1292     std::map<String,DeviceCreationParameter*> parameters = MidiInputDeviceFactory::GetAvailableDriverParameters(Driver);
1293     if (parameters.size()) { // if there are parameters defined for this driver
1294     String s;
1295     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1296     for (;iter != parameters.end(); iter++) {
1297     if (s != "") s += ",";
1298     s += iter->first;
1299 iliev 1835 delete iter->second;
1300 senkov 155 }
1301     result.Add("PARAMETERS", s);
1302     }
1303     }
1304 schoenebeck 880 catch (Exception e) {
1305 senkov 155 result.Error(e);
1306     }
1307     return result.Produce();
1308     }
1309    
1310 schoenebeck 123 String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
1311     dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
1312     LSCPResultSet result;
1313     try {
1314     result.Add("DESCRIPTION", AudioOutputDeviceFactory::GetDriverDescription(Driver));
1315     result.Add("VERSION", AudioOutputDeviceFactory::GetDriverVersion(Driver));
1316    
1317     std::map<String,DeviceCreationParameter*> parameters = AudioOutputDeviceFactory::GetAvailableDriverParameters(Driver);
1318     if (parameters.size()) { // if there are parameters defined for this driver
1319     String s;
1320     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1321     for (;iter != parameters.end(); iter++) {
1322     if (s != "") s += ",";
1323     s += iter->first;
1324 iliev 1835 delete iter->second;
1325 schoenebeck 123 }
1326     result.Add("PARAMETERS", s);
1327     }
1328     }
1329 schoenebeck 880 catch (Exception e) {
1330 schoenebeck 123 result.Error(e);
1331     }
1332     return result.Produce();
1333     }
1334    
1335 senkov 155 String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1336 schoenebeck 226 dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1337 senkov 155 LSCPResultSet result;
1338     try {
1339     DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
1340     result.Add("TYPE", pParameter->Type());
1341     result.Add("DESCRIPTION", pParameter->Description());
1342 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
1343     result.Add("FIX", pParameter->Fix());
1344     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1345 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
1346     optional<String> oDefault = pParameter->Default(DependencyList);
1347     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
1348     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
1349     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1350     if (oDepends) result.Add("DEPENDS", *oDepends);
1351     if (oDefault) result.Add("DEFAULT", *oDefault);
1352     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
1353     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
1354     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1355 iliev 1835 delete pParameter;
1356 senkov 155 }
1357 schoenebeck 880 catch (Exception e) {
1358 senkov 155 result.Error(e);
1359     }
1360     return result.Produce();
1361     }
1362    
1363 schoenebeck 123 String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1364 schoenebeck 226 dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1365 schoenebeck 123 LSCPResultSet result;
1366     try {
1367     DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
1368     result.Add("TYPE", pParameter->Type());
1369     result.Add("DESCRIPTION", pParameter->Description());
1370 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
1371     result.Add("FIX", pParameter->Fix());
1372     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1373 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
1374     optional<String> oDefault = pParameter->Default(DependencyList);
1375     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
1376     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
1377     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1378     if (oDepends) result.Add("DEPENDS", *oDepends);
1379     if (oDefault) result.Add("DEFAULT", *oDefault);
1380     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
1381     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
1382     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1383 iliev 1835 delete pParameter;
1384 schoenebeck 123 }
1385 schoenebeck 880 catch (Exception e) {
1386 schoenebeck 123 result.Error(e);
1387     }
1388     return result.Produce();
1389     }
1390    
1391     String LSCPServer::GetAudioOutputDeviceCount() {
1392     dmsg(2,("LSCPServer: GetAudioOutputDeviceCount()\n"));
1393     LSCPResultSet result;
1394     try {
1395     uint count = pSampler->AudioOutputDevices();
1396 senkov 138 result.Add(count); // success
1397 schoenebeck 123 }
1398 schoenebeck 880 catch (Exception e) {
1399 schoenebeck 123 result.Error(e);
1400     }
1401     return result.Produce();
1402     }
1403    
1404 senkov 155 String LSCPServer::GetMidiInputDeviceCount() {
1405     dmsg(2,("LSCPServer: GetMidiInputDeviceCount()\n"));
1406     LSCPResultSet result;
1407     try {
1408     uint count = pSampler->MidiInputDevices();
1409     result.Add(count); // success
1410     }
1411 schoenebeck 880 catch (Exception e) {
1412 senkov 155 result.Error(e);
1413     }
1414     return result.Produce();
1415     }
1416    
1417 schoenebeck 123 String LSCPServer::GetAudioOutputDevices() {
1418     dmsg(2,("LSCPServer: GetAudioOutputDevices()\n"));
1419     LSCPResultSet result;
1420     try {
1421     String s;
1422     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1423     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1424     for (; iter != devices.end(); iter++) {
1425     if (s != "") s += ",";
1426     s += ToString(iter->first);
1427     }
1428     result.Add(s);
1429     }
1430 schoenebeck 880 catch (Exception e) {
1431 schoenebeck 123 result.Error(e);
1432     }
1433     return result.Produce();
1434     }
1435    
1436 senkov 155 String LSCPServer::GetMidiInputDevices() {
1437     dmsg(2,("LSCPServer: GetMidiInputDevices()\n"));
1438     LSCPResultSet result;
1439     try {
1440     String s;
1441     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1442     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1443     for (; iter != devices.end(); iter++) {
1444     if (s != "") s += ",";
1445     s += ToString(iter->first);
1446     }
1447     result.Add(s);
1448     }
1449 schoenebeck 880 catch (Exception e) {
1450 senkov 155 result.Error(e);
1451     }
1452     return result.Produce();
1453     }
1454    
1455 schoenebeck 123 String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
1456     dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
1457     LSCPResultSet result;
1458     try {
1459     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1460 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1461 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
1462 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
1463 schoenebeck 123 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1464     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1465     for (; iter != parameters.end(); iter++) {
1466     result.Add(iter->first, iter->second->Value());
1467     }
1468     }
1469 schoenebeck 880 catch (Exception e) {
1470 schoenebeck 123 result.Error(e);
1471     }
1472     return result.Produce();
1473     }
1474    
1475 senkov 155 String LSCPServer::GetMidiInputDeviceInfo(uint DeviceIndex) {
1476     dmsg(2,("LSCPServer: GetMidiInputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
1477     LSCPResultSet result;
1478     try {
1479     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1480 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1481 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1482 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
1483 senkov 155 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1484     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1485     for (; iter != parameters.end(); iter++) {
1486     result.Add(iter->first, iter->second->Value());
1487     }
1488     }
1489 schoenebeck 880 catch (Exception e) {
1490 senkov 155 result.Error(e);
1491     }
1492     return result.Produce();
1493     }
1494     String LSCPServer::GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex) {
1495     dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
1496     LSCPResultSet result;
1497     try {
1498 schoenebeck 223 // get MIDI input device
1499 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1500 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1501 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1502 schoenebeck 223
1503     // get MIDI port
1504 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1505 schoenebeck 880 if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1506 schoenebeck 223
1507     // return the values of all MIDI port parameters
1508 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1509     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1510 senkov 155 for (; iter != parameters.end(); iter++) {
1511     result.Add(iter->first, iter->second->Value());
1512     }
1513     }
1514 schoenebeck 880 catch (Exception e) {
1515 senkov 155 result.Error(e);
1516     }
1517     return result.Produce();
1518     }
1519    
1520 schoenebeck 123 String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
1521     dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
1522     LSCPResultSet result;
1523     try {
1524     // get audio output device
1525     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1526 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1527 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1528    
1529     // get audio channel
1530     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1531 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1532 schoenebeck 123
1533     // return the values of all audio channel parameters
1534     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1535     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1536     for (; iter != parameters.end(); iter++) {
1537     result.Add(iter->first, iter->second->Value());
1538     }
1539     }
1540 schoenebeck 880 catch (Exception e) {
1541 schoenebeck 123 result.Error(e);
1542     }
1543     return result.Produce();
1544     }
1545    
1546 senkov 185 String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
1547     dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
1548     LSCPResultSet result;
1549     try {
1550 schoenebeck 223 // get MIDI input device
1551     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1552 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no midi input device with index " + ToString(DeviceId) + ".");
1553 schoenebeck 223 MidiInputDevice* pDevice = devices[DeviceId];
1554 senkov 185
1555 schoenebeck 221 // get midi port
1556     MidiInputPort* pPort = pDevice->GetPort(PortId);
1557 schoenebeck 880 if (!pPort) throw Exception("Midi input device does not have port " + ToString(PortId) + ".");
1558 senkov 185
1559 schoenebeck 223 // get desired port parameter
1560     std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1561 schoenebeck 880 if (!parameters.count(ParameterName)) throw Exception("Midi port does not provide a parameter '" + ParameterName + "'.");
1562 schoenebeck 223 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1563 schoenebeck 203
1564 senkov 185 // return all fields of this audio channel parameter
1565     result.Add("TYPE", pParameter->Type());
1566     result.Add("DESCRIPTION", pParameter->Description());
1567     result.Add("FIX", pParameter->Fix());
1568     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1569 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1570     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1571     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1572 senkov 185 }
1573 schoenebeck 880 catch (Exception e) {
1574 senkov 185 result.Error(e);
1575     }
1576     return result.Produce();
1577     }
1578    
1579 schoenebeck 123 String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
1580     dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
1581     LSCPResultSet result;
1582     try {
1583     // get audio output device
1584     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1585 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1586 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1587    
1588     // get audio channel
1589     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1590 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1591 schoenebeck 123
1592     // get desired audio channel parameter
1593     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1594 schoenebeck 880 if (!parameters.count(ParameterName)) throw Exception("Audio channel does not provide a parameter '" + ParameterName + "'.");
1595 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1596    
1597     // return all fields of this audio channel parameter
1598     result.Add("TYPE", pParameter->Type());
1599     result.Add("DESCRIPTION", pParameter->Description());
1600     result.Add("FIX", pParameter->Fix());
1601     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1602 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1603     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1604     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1605 schoenebeck 123 }
1606 schoenebeck 880 catch (Exception e) {
1607 schoenebeck 123 result.Error(e);
1608     }
1609     return result.Produce();
1610     }
1611    
1612     String LSCPServer::SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal) {
1613     dmsg(2,("LSCPServer: SetAudioOutputChannelParameter(DeviceId=%d,ChannelId=%d,ParamKey=%s,ParamVal=%s)\n",DeviceId,ChannelId,ParamKey.c_str(),ParamVal.c_str()));
1614     LSCPResultSet result;
1615     try {
1616     // get audio output device
1617     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1618 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1619 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1620    
1621     // get audio channel
1622     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1623 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1624 schoenebeck 123
1625     // get desired audio channel parameter
1626     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1627 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("Audio channel does not provide a parameter '" + ParamKey + "'.");
1628 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1629    
1630     // set new channel parameter value
1631     pParameter->SetValue(ParamVal);
1632 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceId));
1633 schoenebeck 123 }
1634 schoenebeck 880 catch (Exception e) {
1635 schoenebeck 123 result.Error(e);
1636     }
1637     return result.Produce();
1638     }
1639    
1640     String LSCPServer::SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1641     dmsg(2,("LSCPServer: SetAudioOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1642     LSCPResultSet result;
1643     try {
1644     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1645 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1646 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
1647     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1648 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1649 schoenebeck 123 parameters[ParamKey]->SetValue(ParamVal);
1650 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceIndex));
1651 schoenebeck 123 }
1652 schoenebeck 880 catch (Exception e) {
1653 schoenebeck 123 result.Error(e);
1654     }
1655     return result.Produce();
1656     }
1657    
1658 senkov 155 String LSCPServer::SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1659     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1660     LSCPResultSet result;
1661     try {
1662     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1663 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1664 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1665     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1666 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1667 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1668 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1669 senkov 155 }
1670 schoenebeck 880 catch (Exception e) {
1671 senkov 155 result.Error(e);
1672     }
1673     return result.Produce();
1674     }
1675    
1676     String LSCPServer::SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal) {
1677     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1678     LSCPResultSet result;
1679     try {
1680 schoenebeck 223 // get MIDI input device
1681 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1682 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1683 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1684 schoenebeck 223
1685     // get MIDI port
1686 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1687 schoenebeck 880 if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1688 schoenebeck 223
1689     // set port parameter value
1690 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1691 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1692 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1693 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1694 senkov 155 }
1695 schoenebeck 880 catch (Exception e) {
1696 senkov 155 result.Error(e);
1697     }
1698     return result.Produce();
1699     }
1700    
1701 schoenebeck 35 /**
1702     * Will be called by the parser to change the audio output channel for
1703     * playback on a particular sampler channel.
1704     */
1705 schoenebeck 123 String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1706     dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));
1707 schoenebeck 225 LSCPResultSet result;
1708     try {
1709     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1710 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1711 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1712 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1713     if (!pSamplerChannel->GetAudioOutputDevice()) throw Exception("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1714 schoenebeck 411 pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1715 schoenebeck 225 }
1716 schoenebeck 880 catch (Exception e) {
1717 schoenebeck 225 result.Error(e);
1718     }
1719     return result.Produce();
1720 schoenebeck 35 }
1721    
1722 capela 159 String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1723     dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1724     LSCPResultSet result;
1725 persson 841 LockRTNotify();
1726 capela 159 try {
1727     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1728 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1729 capela 159 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1730 schoenebeck 880 if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));
1731 capela 159 AudioOutputDevice* pDevice = devices[AudioDeviceId];
1732     pSamplerChannel->SetAudioOutputDevice(pDevice);
1733     }
1734 schoenebeck 880 catch (Exception e) {
1735 capela 159 result.Error(e);
1736     }
1737 persson 841 UnlockRTNotify();
1738 capela 159 return result.Produce();
1739     }
1740    
1741 capela 143 String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1742     dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1743     LSCPResultSet result;
1744 persson 841 LockRTNotify();
1745 capela 143 try {
1746     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1747 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1748 capela 143 // Driver type name aliasing...
1749 schoenebeck 226 if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1750     if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1751 capela 143 // Check if there's one audio output device already created
1752     // for the intended audio driver type (AudioOutputDriver)...
1753     AudioOutputDevice *pDevice = NULL;
1754     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1755     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1756     for (; iter != devices.end(); iter++) {
1757     if ((iter->second)->Driver() == AudioOutputDriver) {
1758     pDevice = iter->second;
1759     break;
1760     }
1761     }
1762     // If it doesn't exist, create a new one with default parameters...
1763     if (pDevice == NULL) {
1764     std::map<String,String> params;
1765     pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1766     }
1767     // Must have a device...
1768     if (pDevice == NULL)
1769 schoenebeck 880 throw Exception("Internal error: could not create audio output device.");
1770 capela 143 // Set it as the current channel device...
1771     pSamplerChannel->SetAudioOutputDevice(pDevice);
1772     }
1773 schoenebeck 880 catch (Exception e) {
1774 capela 143 result.Error(e);
1775     }
1776 persson 841 UnlockRTNotify();
1777 capela 143 return result.Produce();
1778     }
1779    
1780 capela 159 String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1781     dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel));
1782 senkov 120 LSCPResultSet result;
1783 schoenebeck 53 try {
1784     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1785 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1786 capela 159 pSamplerChannel->SetMidiInputPort(MIDIPort);
1787 schoenebeck 53 }
1788 schoenebeck 880 catch (Exception e) {
1789 senkov 120 result.Error(e);
1790 schoenebeck 53 }
1791 senkov 120 return result.Produce();
1792 schoenebeck 53 }
1793    
1794 capela 159 String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1795     dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
1796 senkov 120 LSCPResultSet result;
1797 senkov 68 try {
1798     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1799 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1800 schoenebeck 675 pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1801 senkov 68 }
1802 schoenebeck 880 catch (Exception e) {
1803 senkov 120 result.Error(e);
1804 senkov 68 }
1805 senkov 120 return result.Produce();
1806 schoenebeck 35 }
1807    
1808 capela 159 String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1809     dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1810 schoenebeck 123 LSCPResultSet result;
1811     try {
1812 capela 159 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1813 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1814 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1815 schoenebeck 880 if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1816 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1817     pSamplerChannel->SetMidiInputDevice(pDevice);
1818 schoenebeck 123 }
1819 schoenebeck 880 catch (Exception e) {
1820 schoenebeck 123 result.Error(e);
1821     }
1822     return result.Produce();
1823     }
1824    
1825 capela 159 String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1826     dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1827     LSCPResultSet result;
1828     try {
1829     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1830 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1831 capela 159 // Driver type name aliasing...
1832 schoenebeck 226 if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1833 capela 159 // Check if there's one MIDI input device already created
1834     // for the intended MIDI driver type (MidiInputDriver)...
1835     MidiInputDevice *pDevice = NULL;
1836     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1837     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1838     for (; iter != devices.end(); iter++) {
1839     if ((iter->second)->Driver() == MidiInputDriver) {
1840     pDevice = iter->second;
1841     break;
1842     }
1843     }
1844     // If it doesn't exist, create a new one with default parameters...
1845     if (pDevice == NULL) {
1846     std::map<String,String> params;
1847     pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1848     // Make it with at least one initial port.
1849     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1850     }
1851     // Must have a device...
1852     if (pDevice == NULL)
1853 schoenebeck 880 throw Exception("Internal error: could not create MIDI input device.");
1854 capela 159 // Set it as the current channel device...
1855     pSamplerChannel->SetMidiInputDevice(pDevice);
1856     }
1857 schoenebeck 880 catch (Exception e) {
1858 capela 159 result.Error(e);
1859     }
1860     return result.Produce();
1861     }
1862    
1863 schoenebeck 35 /**
1864 capela 159 * Will be called by the parser to change the MIDI input device, port and channel on which
1865     * engine of a particular sampler channel should listen to.
1866     */
1867     String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1868     dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1869     LSCPResultSet result;
1870     try {
1871     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1872 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1873 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1874 schoenebeck 880 if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1875 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1876 schoenebeck 675 pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
1877 capela 159 }
1878 schoenebeck 880 catch (Exception e) {
1879 capela 159 result.Error(e);
1880     }
1881     return result.Produce();
1882     }
1883    
1884     /**
1885 schoenebeck 35 * Will be called by the parser to change the global volume factor on a
1886     * particular sampler channel.
1887     */
1888 schoenebeck 225 String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1889     dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1890 senkov 120 LSCPResultSet result;
1891 schoenebeck 53 try {
1892 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1893 schoenebeck 411 pEngineChannel->Volume(dVolume);
1894 schoenebeck 53 }
1895 schoenebeck 880 catch (Exception e) {
1896 senkov 120 result.Error(e);
1897 schoenebeck 53 }
1898 senkov 120 return result.Produce();
1899 schoenebeck 35 }
1900    
1901     /**
1902 schoenebeck 705 * Will be called by the parser to mute/unmute particular sampler channel.
1903     */
1904     String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1905     dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1906     LSCPResultSet result;
1907     try {
1908 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1909 schoenebeck 705
1910     if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1911     else pEngineChannel->SetMute(1);
1912 schoenebeck 880 } catch (Exception e) {
1913 schoenebeck 705 result.Error(e);
1914     }
1915     return result.Produce();
1916     }
1917    
1918     /**
1919     * Will be called by the parser to solo particular sampler channel.
1920     */
1921     String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1922     dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1923     LSCPResultSet result;
1924     try {
1925 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1926 schoenebeck 705
1927     bool oldSolo = pEngineChannel->GetSolo();
1928     bool hadSoloChannel = HasSoloChannel();
1929 schoenebeck 1009
1930 schoenebeck 705 pEngineChannel->SetSolo(bSolo);
1931 schoenebeck 1009
1932 schoenebeck 705 if(!oldSolo && bSolo) {
1933     if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1934     if(!hadSoloChannel) MuteNonSoloChannels();
1935     }
1936 schoenebeck 1009
1937 schoenebeck 705 if(oldSolo && !bSolo) {
1938     if(!HasSoloChannel()) UnmuteChannels();
1939     else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1940     }
1941 schoenebeck 880 } catch (Exception e) {
1942 schoenebeck 705 result.Error(e);
1943     }
1944     return result.Produce();
1945     }
1946    
1947     /**
1948     * Determines whether there is at least one solo channel in the channel list.
1949     *
1950     * @returns true if there is at least one solo channel in the channel list,
1951     * false otherwise.
1952     */
1953     bool LSCPServer::HasSoloChannel() {
1954     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1955     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1956     for (; iter != channels.end(); iter++) {
1957     EngineChannel* c = iter->second->GetEngineChannel();
1958     if(c && c->GetSolo()) return true;
1959     }
1960    
1961     return false;
1962     }
1963    
1964     /**
1965     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1966     * with -1 which indicates that they are muted because of the presence
1967     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1968     * when there are no solo channels left.
1969     */
1970     void LSCPServer::MuteNonSoloChannels() {
1971     dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1972     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1973     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1974     for (; iter != channels.end(); iter++) {
1975     EngineChannel* c = iter->second->GetEngineChannel();
1976     if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1977     }
1978     }
1979    
1980     /**
1981     * Unmutes all channels that are muted because of the presence
1982     * of a solo channel(s).
1983     */
1984     void LSCPServer::UnmuteChannels() {
1985     dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1986     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1987     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1988     for (; iter != channels.end(); iter++) {
1989     EngineChannel* c = iter->second->GetEngineChannel();
1990     if(c && c->GetMute() == -1) c->SetMute(0);
1991     }
1992     }
1993    
1994 schoenebeck 1047 String LSCPServer::AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name, bool bModal) {
1995 schoenebeck 947 dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1996    
1997     midi_prog_index_t idx;
1998 schoenebeck 973 idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1999     idx.midi_bank_lsb = MidiBank & 0x7f;
2000 schoenebeck 947 idx.midi_prog = MidiProg;
2001    
2002     MidiInstrumentMapper::entry_t entry;
2003     entry.EngineName = EngineType;
2004     entry.InstrumentFile = InstrumentFile;
2005     entry.InstrumentIndex = InstrumentIndex;
2006     entry.LoadMode = LoadMode;
2007     entry.Volume = Volume;
2008     entry.Name = Name;
2009    
2010     LSCPResultSet result;
2011     try {
2012 schoenebeck 1047 // PERSISTENT mapping commands might block for a long time, so in
2013     // that case we add/replace the mapping in another thread in case
2014     // the NON_MODAL argument was supplied, non persistent mappings
2015     // should return immediately, so we don't need to do that for them
2016     bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
2017 schoenebeck 973 MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
2018 schoenebeck 947 } catch (Exception e) {
2019     result.Error(e);
2020     }
2021     return result.Produce();
2022     }
2023    
2024 schoenebeck 973 String LSCPServer::RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2025 schoenebeck 947 dmsg(2,("LSCPServer: RemoveMIDIInstrumentMapping()\n"));
2026    
2027     midi_prog_index_t idx;
2028 schoenebeck 973 idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
2029     idx.midi_bank_lsb = MidiBank & 0x7f;
2030 schoenebeck 947 idx.midi_prog = MidiProg;
2031    
2032     LSCPResultSet result;
2033     try {
2034 schoenebeck 973 MidiInstrumentMapper::RemoveEntry(MidiMapID, idx);
2035 schoenebeck 947 } catch (Exception e) {
2036     result.Error(e);
2037     }
2038     return result.Produce();
2039     }
2040    
2041 schoenebeck 973 String LSCPServer::GetMidiInstrumentMappings(uint MidiMapID) {
2042     dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2043 schoenebeck 947 LSCPResultSet result;
2044 schoenebeck 973 try {
2045 iliev 1763 result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID));
2046 schoenebeck 973 } catch (Exception e) {
2047     result.Error(e);
2048     }
2049 schoenebeck 947 return result.Produce();
2050     }
2051    
2052 schoenebeck 973
2053     String LSCPServer::GetAllMidiInstrumentMappings() {
2054     dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2055     LSCPResultSet result;
2056 iliev 1763 try {
2057     result.Add(MidiInstrumentMapper::GetInstrumentCount());
2058     } catch (Exception e) {
2059     result.Error(e);
2060 schoenebeck 973 }
2061     return result.Produce();
2062     }
2063    
2064     String LSCPServer::GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2065 schoenebeck 947 dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2066     LSCPResultSet result;
2067     try {
2068 iliev 1763 MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg);
2069     // convert the filename into the correct encoding as defined for LSCP
2070     // (especially in terms of special characters -> escape sequences)
2071 schoenebeck 1471 #if WIN32
2072 iliev 1763 const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp();
2073 schoenebeck 1471 #else
2074 iliev 1763 // assuming POSIX
2075     const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp();
2076 schoenebeck 1471 #endif
2077    
2078 iliev 1763 result.Add("NAME", _escapeLscpResponse(entry.Name));
2079     result.Add("ENGINE_NAME", entry.EngineName);
2080     result.Add("INSTRUMENT_FILE", instrumentFileName);
2081     result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex);
2082     String instrumentName;
2083     Engine* pEngine = EngineFactory::Create(entry.EngineName);
2084     if (pEngine) {
2085     if (pEngine->GetInstrumentManager()) {
2086     InstrumentManager::instrument_id_t instrID;
2087     instrID.FileName = entry.InstrumentFile;
2088     instrID.Index = entry.InstrumentIndex;
2089     instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
2090 schoenebeck 947 }
2091 iliev 1763 EngineFactory::Destroy(pEngine);
2092 schoenebeck 947 }
2093 iliev 1763 result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2094     switch (entry.LoadMode) {
2095     case MidiInstrumentMapper::ON_DEMAND:
2096     result.Add("LOAD_MODE", "ON_DEMAND");
2097     break;
2098     case MidiInstrumentMapper::ON_DEMAND_HOLD:
2099     result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2100     break;
2101     case MidiInstrumentMapper::PERSISTENT:
2102     result.Add("LOAD_MODE", "PERSISTENT");
2103     break;
2104     default:
2105     throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2106     }
2107     result.Add("VOLUME", entry.Volume);
2108 schoenebeck 947 } catch (Exception e) {
2109     result.Error(e);
2110     }
2111     return result.Produce();
2112     }
2113    
2114 schoenebeck 973 String LSCPServer::ListMidiInstrumentMappings(uint MidiMapID) {
2115     dmsg(2,("LSCPServer: ListMidiInstrumentMappings()\n"));
2116 schoenebeck 947 LSCPResultSet result;
2117     try {
2118     String s;
2119 schoenebeck 973 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
2120 schoenebeck 947 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2121     for (; iter != mappings.end(); iter++) {
2122     if (s.size()) s += ",";
2123 schoenebeck 973 s += "{" + ToString(MidiMapID) + ","
2124 schoenebeck 1007 + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2125 schoenebeck 973 + ToString(int(iter->first.midi_prog)) + "}";
2126 schoenebeck 947 }
2127     result.Add(s);
2128     } catch (Exception e) {
2129     result.Error(e);
2130     }
2131     return result.Produce();
2132     }
2133    
2134 schoenebeck 973 String LSCPServer::ListAllMidiInstrumentMappings() {
2135     dmsg(2,("LSCPServer: ListAllMidiInstrumentMappings()\n"));
2136     LSCPResultSet result;
2137     try {
2138     std::vector<int> maps = MidiInstrumentMapper::Maps();
2139     String s;
2140     for (int i = 0; i < maps.size(); i++) {
2141     std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(maps[i]);
2142     std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2143     for (; iter != mappings.end(); iter++) {
2144     if (s.size()) s += ",";
2145     s += "{" + ToString(maps[i]) + ","
2146 schoenebeck 1009 + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2147 schoenebeck 973 + ToString(int(iter->first.midi_prog)) + "}";
2148     }
2149     }
2150     result.Add(s);
2151     } catch (Exception e) {
2152     result.Error(e);
2153     }
2154     return result.Produce();
2155     }
2156    
2157     String LSCPServer::ClearMidiInstrumentMappings(uint MidiMapID) {
2158 schoenebeck 947 dmsg(2,("LSCPServer: ClearMidiInstrumentMappings()\n"));
2159     LSCPResultSet result;
2160     try {
2161 schoenebeck 973 MidiInstrumentMapper::RemoveAllEntries(MidiMapID);
2162 schoenebeck 947 } catch (Exception e) {
2163     result.Error(e);
2164     }
2165     return result.Produce();
2166     }
2167    
2168 schoenebeck 973 String LSCPServer::ClearAllMidiInstrumentMappings() {
2169     dmsg(2,("LSCPServer: ClearAllMidiInstrumentMappings()\n"));
2170     LSCPResultSet result;
2171     try {
2172     std::vector<int> maps = MidiInstrumentMapper::Maps();
2173     for (int i = 0; i < maps.size(); i++)
2174     MidiInstrumentMapper::RemoveAllEntries(maps[i]);
2175     } catch (Exception e) {
2176     result.Error(e);
2177     }
2178     return result.Produce();
2179     }
2180    
2181     String LSCPServer::AddMidiInstrumentMap(String MapName) {
2182     dmsg(2,("LSCPServer: AddMidiInstrumentMap()\n"));
2183     LSCPResultSet result;
2184     try {
2185     int MapID = MidiInstrumentMapper::AddMap(MapName);
2186     result = LSCPResultSet(MapID);
2187     } catch (Exception e) {
2188     result.Error(e);
2189     }
2190     return result.Produce();
2191     }
2192    
2193     String LSCPServer::RemoveMidiInstrumentMap(uint MidiMapID) {
2194     dmsg(2,("LSCPServer: RemoveMidiInstrumentMap()\n"));
2195     LSCPResultSet result;
2196     try {
2197     MidiInstrumentMapper::RemoveMap(MidiMapID);
2198     } catch (Exception e) {
2199     result.Error(e);
2200     }
2201     return result.Produce();
2202     }
2203    
2204     String LSCPServer::RemoveAllMidiInstrumentMaps() {
2205     dmsg(2,("LSCPServer: RemoveAllMidiInstrumentMaps()\n"));
2206     LSCPResultSet result;
2207     try {
2208     MidiInstrumentMapper::RemoveAllMaps();
2209     } catch (Exception e) {
2210     result.Error(e);
2211     }
2212     return result.Produce();
2213     }
2214    
2215     String LSCPServer::GetMidiInstrumentMaps() {
2216     dmsg(2,("LSCPServer: GetMidiInstrumentMaps()\n"));
2217     LSCPResultSet result;
2218     try {
2219     result.Add(MidiInstrumentMapper::Maps().size());
2220     } catch (Exception e) {
2221     result.Error(e);
2222     }
2223     return result.Produce();
2224     }
2225    
2226     String LSCPServer::ListMidiInstrumentMaps() {
2227     dmsg(2,("LSCPServer: ListMidiInstrumentMaps()\n"));
2228     LSCPResultSet result;
2229     try {
2230     std::vector<int> maps = MidiInstrumentMapper::Maps();
2231     String sList;
2232     for (int i = 0; i < maps.size(); i++) {
2233     if (sList != "") sList += ",";
2234     sList += ToString(maps[i]);
2235     }
2236     result.Add(sList);
2237     } catch (Exception e) {
2238     result.Error(e);
2239     }
2240     return result.Produce();
2241     }
2242    
2243     String LSCPServer::GetMidiInstrumentMap(uint MidiMapID) {
2244     dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2245     LSCPResultSet result;
2246     try {
2247 schoenebeck 1399 result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2248 iliev 1135 result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2249 schoenebeck 973 } catch (Exception e) {
2250     result.Error(e);
2251     }
2252     return result.Produce();
2253     }
2254    
2255     String LSCPServer::SetMidiInstrumentMapName(uint MidiMapID, String NewName) {
2256     dmsg(2,("LSCPServer: SetMidiInstrumentMapName()\n"));
2257     LSCPResultSet result;
2258     try {
2259     MidiInstrumentMapper::RenameMap(MidiMapID, NewName);
2260     } catch (Exception e) {
2261     result.Error(e);
2262     }
2263     return result.Produce();
2264     }
2265    
2266 schoenebeck 705 /**
2267 schoenebeck 973 * Set the MIDI instrument map the given sampler channel shall use for
2268     * handling MIDI program change messages. There are the following two
2269     * special (negative) values:
2270     *
2271     * - (-1) : set to NONE (ignore program changes)
2272     * - (-2) : set to DEFAULT map
2273     */
2274     String LSCPServer::SetChannelMap(uint uiSamplerChannel, int MidiMapID) {
2275     dmsg(2,("LSCPServer: SetChannelMap()\n"));
2276     LSCPResultSet result;
2277     try {
2278 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2279 schoenebeck 973
2280     if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2281     else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
2282     else pEngineChannel->SetMidiInstrumentMap(MidiMapID);
2283     } catch (Exception e) {
2284     result.Error(e);
2285     }
2286     return result.Produce();
2287     }
2288    
2289 schoenebeck 1001 String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) {
2290     dmsg(2,("LSCPServer: CreateFxSend()\n"));
2291     LSCPResultSet result;
2292     try {
2293 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2294 schoenebeck 1252
2295 schoenebeck 1001 FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2296     if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2297    
2298     result = LSCPResultSet(pFxSend->Id()); // success
2299     } catch (Exception e) {
2300     result.Error(e);
2301     }
2302     return result.Produce();
2303     }
2304    
2305     String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) {
2306     dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2307     LSCPResultSet result;
2308     try {
2309 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2310 schoenebeck 1001
2311     FxSend* pFxSend = NULL;
2312     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2313     if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2314     pFxSend = pEngineChannel->GetFxSend(i);
2315     break;
2316     }
2317     }
2318     if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2319     pEngineChannel->RemoveFxSend(pFxSend);
2320     } catch (Exception e) {
2321     result.Error(e);
2322     }
2323     return result.Produce();
2324     }
2325    
2326     String LSCPServer::GetFxSends(uint uiSamplerChannel) {
2327     dmsg(2,("LSCPServer: GetFxSends()\n"));
2328     LSCPResultSet result;
2329     try {
2330 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2331 schoenebeck 1001
2332     result.Add(pEngineChannel->GetFxSendCount());
2333     } catch (Exception e) {
2334     result.Error(e);
2335     }
2336     return result.Produce();
2337     }
2338    
2339     String LSCPServer::ListFxSends(uint uiSamplerChannel) {
2340     dmsg(2,("LSCPServer: ListFxSends()\n"));
2341     LSCPResultSet result;
2342     String list;
2343     try {
2344 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2345 schoenebeck 1001
2346     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2347     FxSend* pFxSend = pEngineChannel->GetFxSend(i);
2348     if (list != "") list += ",";
2349     list += ToString(pFxSend->Id());
2350     }
2351     result.Add(list);
2352     } catch (Exception e) {
2353     result.Error(e);
2354     }
2355     return result.Produce();
2356     }
2357    
2358 iliev 1135 FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2359     EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2360    
2361     FxSend* pFxSend = NULL;
2362     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2363     if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2364     pFxSend = pEngineChannel->GetFxSend(i);
2365     break;
2366     }
2367     }
2368     if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2369     return pFxSend;
2370     }
2371    
2372 schoenebeck 1001 String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2373     dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2374     LSCPResultSet result;
2375     try {
2376 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2377     FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2378 schoenebeck 1252
2379 schoenebeck 1001 // gather audio routing informations
2380     String AudioRouting;
2381     for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
2382     if (AudioRouting != "") AudioRouting += ",";
2383     AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2384     }
2385    
2386 schoenebeck 2137 const String sEffectRouting =
2387     (pFxSend->DestinationEffectChain() >= 0 && pFxSend->DestinationEffectChainPosition() >= 0)
2388     ? ToString(pFxSend->DestinationEffectChain()) + "," + ToString(pFxSend->DestinationEffectChainPosition())
2389     : "NONE";
2390    
2391 schoenebeck 1001 // success
2392 schoenebeck 1399 result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2393 schoenebeck 1026 result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2394     result.Add("LEVEL", ToString(pFxSend->Level()));
2395 schoenebeck 1001 result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2396 schoenebeck 2137 result.Add("SEND_EFFECT", sEffectRouting);
2397 schoenebeck 1001 } catch (Exception e) {
2398     result.Error(e);
2399     }
2400     return result.Produce();
2401     }
2402    
2403 iliev 1135 String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2404     dmsg(2,("LSCPServer: SetFxSendName()\n"));
2405     LSCPResultSet result;
2406     try {
2407     FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2408    
2409     pFxSend->SetName(Name);
2410     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2411     } catch (Exception e) {
2412     result.Error(e);
2413     }
2414     return result.Produce();
2415     }
2416    
2417 schoenebeck 1001 String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2418     dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2419     LSCPResultSet result;
2420     try {
2421 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2422 schoenebeck 1001
2423     pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2424 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2425 schoenebeck 1001 } catch (Exception e) {
2426     result.Error(e);
2427     }
2428     return result.Produce();
2429     }
2430    
2431 schoenebeck 1026 String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2432     dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2433     LSCPResultSet result;
2434     try {
2435 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2436 schoenebeck 1026
2437     pFxSend->SetMidiController(MidiController);
2438 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2439 schoenebeck 1026 } catch (Exception e) {
2440     result.Error(e);
2441     }
2442     return result.Produce();
2443     }
2444    
2445     String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2446     dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2447     LSCPResultSet result;
2448     try {
2449 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2450 schoenebeck 1026
2451     pFxSend->SetLevel((float)dLevel);
2452 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2453 schoenebeck 1026 } catch (Exception e) {
2454     result.Error(e);
2455     }
2456     return result.Produce();
2457     }
2458    
2459 schoenebeck 2137 String LSCPServer::SetFxSendEffect(uint uiSamplerChannel, uint FxSendID, int iSendEffectChain, int iEffectChainPosition) {
2460     dmsg(2,("LSCPServer: SetFxSendEffect(%d,%d)\n", iSendEffectChain, iEffectChainPosition));
2461     LSCPResultSet result;
2462     try {
2463     FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2464    
2465     pFxSend->SetDestinationEffect(iSendEffectChain, iEffectChainPosition);
2466     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2467     } catch (Exception e) {
2468     result.Error(e);
2469     }
2470     return result.Produce();
2471     }
2472    
2473 schoenebeck 2135 String LSCPServer::GetAvailableEffects() {
2474     dmsg(2,("LSCPServer: GetAvailableEffects()\n"));
2475     LSCPResultSet result;
2476     try {
2477     int n = EffectFactory::AvailableEffectsCount();
2478     result.Add(n);
2479     }
2480     catch (Exception e) {
2481     result.Error(e);
2482     }
2483     return result.Produce();
2484     }
2485    
2486     String LSCPServer::ListAvailableEffects() {
2487     dmsg(2,("LSCPServer: ListAvailableEffects()\n"));
2488     LSCPResultSet result;
2489     String list;
2490     try {
2491     //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
2492     int n = EffectFactory::AvailableEffectsCount();
2493     for (int i = 0; i < n; i++) {
2494     if (i) list += ",";
2495     list += ToString(i);
2496     }
2497     }
2498     catch (Exception e) {
2499     result.Error(e);
2500     }
2501     result.Add(list);
2502     return result.Produce();
2503     }
2504    
2505     String LSCPServer::GetEffectInfo(int iEffectIndex) {
2506     dmsg(2,("LSCPServer: GetEffectInfo(%d)\n", iEffectIndex));
2507     LSCPResultSet result;
2508     try {
2509     EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex);
2510     if (!pEffectInfo)
2511     throw Exception("There is no effect with index " + ToString(iEffectIndex));
2512    
2513     // convert the filename into the correct encoding as defined for LSCP
2514     // (especially in terms of special characters -> escape sequences)
2515     #if WIN32
2516     const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2517     #else
2518     // assuming POSIX
2519     const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2520     #endif
2521    
2522     result.Add("SYSTEM", pEffectInfo->EffectSystem());
2523     result.Add("MODULE", dllFileName);
2524     result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2525     result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2526     }
2527     catch (Exception e) {
2528     result.Error(e);
2529     }
2530     return result.Produce();
2531     }
2532    
2533 schoenebeck 2137 String LSCPServer::GetEffectInstanceInfo(int iEffectInstance) {
2534     dmsg(2,("LSCPServer: GetEffectInstanceInfo(%d)\n", iEffectInstance));
2535 schoenebeck 2135 LSCPResultSet result;
2536     try {
2537 schoenebeck 2137 Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2538 schoenebeck 2135 if (!pEffect)
2539 schoenebeck 2137 throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2540 schoenebeck 2135
2541     EffectInfo* pEffectInfo = pEffect->GetEffectInfo();
2542    
2543     // convert the filename into the correct encoding as defined for LSCP
2544     // (especially in terms of special characters -> escape sequences)
2545     #if WIN32
2546     const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2547     #else
2548     // assuming POSIX
2549     const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2550     #endif
2551    
2552     result.Add("SYSTEM", pEffectInfo->EffectSystem());
2553     result.Add("MODULE", dllFileName);
2554     result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2555     result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2556     result.Add("INPUT_CONTROLS", ToString(pEffect->InputControlCount()));
2557     }
2558     catch (Exception e) {
2559     result.Error(e);
2560     }
2561     return result.Produce();
2562     }
2563    
2564 schoenebeck 2137 String LSCPServer::GetEffectInstanceInputControlInfo(int iEffectInstance, int iInputControlIndex) {
2565     dmsg(2,("LSCPServer: GetEffectInstanceInputControlInfo(%d,%d)\n", iEffectInstance, iInputControlIndex));
2566 schoenebeck 2135 LSCPResultSet result;
2567     try {
2568 schoenebeck 2137 Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2569 schoenebeck 2135 if (!pEffect)
2570 schoenebeck 2137 throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2571 schoenebeck 2135
2572     EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2573     if (!pEffectControl)
2574     throw Exception(
2575 schoenebeck 2137 "Effect instance " + ToString(iEffectInstance) +
2576 schoenebeck 2135 " does not have an input control with index " +
2577     ToString(iInputControlIndex)
2578     );
2579    
2580     result.Add("DESCRIPTION", _escapeLscpResponse(pEffectControl->Description()));
2581     result.Add("VALUE", pEffectControl->Value());
2582     if (pEffectControl->MinValue())
2583     result.Add("RANGE_MIN", *pEffectControl->MinValue());
2584     if (pEffectControl->MaxValue())
2585     result.Add("RANGE_MAX", *pEffectControl->MaxValue());
2586     if (!pEffectControl->Possibilities().empty())
2587     result.Add("POSSIBILITIES", pEffectControl->Possibilities());
2588     if (pEffectControl->DefaultValue())
2589     result.Add("DEFAULT", *pEffectControl->DefaultValue());
2590     } catch (Exception e) {
2591     result.Error(e);
2592     }
2593     return result.Produce();
2594     }
2595    
2596 schoenebeck 2137 String LSCPServer::SetEffectInstanceInputControlValue(int iEffectInstance, int iInputControlIndex, double dValue) {
2597     dmsg(2,("LSCPServer: SetEffectInstanceInputControlValue(%d,%d,%f)\n", iEffectInstance, iInputControlIndex, dValue));
2598 schoenebeck 2135 LSCPResultSet result;
2599     try {
2600 schoenebeck 2137 Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2601 schoenebeck 2135 if (!pEffect)
2602 schoenebeck 2137 throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2603 schoenebeck 2135
2604     EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2605     if (!pEffectControl)
2606     throw Exception(
2607 schoenebeck 2137 "Effect instance " + ToString(iEffectInstance) +
2608 schoenebeck 2135 " does not have an input control with index " +
2609     ToString(iInputControlIndex)
2610     );
2611    
2612     pEffectControl->SetValue(dValue);
2613     } catch (Exception e) {
2614     result.Error(e);
2615     }
2616     return result.Produce();
2617     }
2618    
2619 schoenebeck 2137 String LSCPServer::CreateEffectInstance(int iEffectIndex) {
2620     dmsg(2,("LSCPServer: CreateEffectInstance(%d)\n", iEffectIndex));
2621 schoenebeck 2135 LSCPResultSet result;
2622     try {
2623 schoenebeck 2137 EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex);
2624 schoenebeck 2135 if (!pEffectInfo)
2625 schoenebeck 2137 throw Exception("There is no effect with index " + ToString(iEffectIndex));
2626 schoenebeck 2135 Effect* pEffect = EffectFactory::Create(pEffectInfo);
2627     result.Add(pEffect->ID());
2628     } catch (Exception e) {
2629     result.Error(e);
2630     }
2631     return result.Produce();
2632     }
2633    
2634     String LSCPServer::CreateEffectInstance(String effectSystem, String module, String effectName) {
2635 schoenebeck 2137 dmsg(2,("LSCPServer: CreateEffectInstance('%s','%s','%s')\n", effectSystem.c_str(), module.c_str(), effectName.c_str()));
2636 schoenebeck 2135 LSCPResultSet result;
2637     try {
2638     // to allow loading the same LSCP session file on different systems
2639     // successfully, probably with different effect plugin DLL paths or even
2640     // running completely different operating systems, we do the following
2641     // for finding the right effect:
2642     //
2643     // first try to search for an exact match of the effect plugin DLL
2644     // (a.k.a 'module'), to avoid picking the wrong DLL with the same
2645     // effect name ...
2646     EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_MATCH_EXACTLY);
2647     // ... if no effect with exactly matchin DLL filename was found, then
2648     // try to lower the restrictions of matching the effect plugin DLL
2649     // filename and try again and again ...
2650     if (!pEffectInfo) {
2651     dmsg(2,("no exact module match, trying MODULE_IGNORE_PATH\n"));
2652     pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH);
2653     }
2654     if (!pEffectInfo) {
2655     dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE\n"));
2656     pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE);
2657     }
2658     if (!pEffectInfo) {
2659     dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE | MODULE_IGNORE_EXTENSION\n"));
2660     pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE | EffectFactory::MODULE_IGNORE_EXTENSION);
2661     }
2662     // ... if there was still no effect found, then completely ignore the
2663     // DLL plugin filename argument and just search for the matching effect
2664     // system type and effect name
2665     if (!pEffectInfo) {
2666     dmsg(2,("no module match, trying MODULE_IGNORE_ALL\n"));
2667     pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_ALL);
2668     }
2669     if (!pEffectInfo)
2670     throw Exception("There is no such effect '" + effectSystem + "' '" + module + "' '" + effectName + "'");
2671    
2672     Effect* pEffect = EffectFactory::Create(pEffectInfo);
2673     result = LSCPResultSet(pEffect->ID());
2674     } catch (Exception e) {
2675     result.Error(e);
2676     }
2677     return result.Produce();
2678     }
2679    
2680 schoenebeck 2137 String LSCPServer::DestroyEffectInstance(int iEffectInstance) {
2681     dmsg(2,("LSCPServer: DestroyEffectInstance(%d)\n", iEffectInstance));
2682 schoenebeck 2135 LSCPResultSet result;
2683     try {
2684 schoenebeck 2137 Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2685 schoenebeck 2135 if (!pEffect)
2686 schoenebeck 2137 throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2687 schoenebeck 2135 EffectFactory::Destroy(pEffect);
2688     } catch (Exception e) {
2689     result.Error(e);
2690     }
2691     return result.Produce();
2692     }
2693    
2694     String LSCPServer::GetEffectInstances() {
2695     dmsg(2,("LSCPServer: GetEffectInstances()\n"));
2696     LSCPResultSet result;
2697     try {
2698     int n = EffectFactory::EffectInstancesCount();
2699     result.Add(n);
2700     } catch (Exception e) {
2701     result.Error(e);
2702     }
2703     return result.Produce();
2704     }
2705    
2706     String LSCPServer::ListEffectInstances() {
2707     dmsg(2,("LSCPServer: ListEffectInstances()\n"));
2708     LSCPResultSet result;
2709     String list;
2710     try {
2711     int n = EffectFactory::EffectInstancesCount();
2712     for (int i = 0; i < n; i++) {
2713     Effect* pEffect = EffectFactory::GetEffectInstance(i);
2714     if (i) list += ",";
2715     list += ToString(pEffect->ID());
2716     }
2717     } catch (Exception e) {
2718     result.Error(e);
2719     }
2720     result.Add(list);
2721     return result.Produce();
2722     }
2723    
2724 schoenebeck 2137 String LSCPServer::GetSendEffectChains(int iAudioOutputDevice) {
2725     dmsg(2,("LSCPServer: GetSendEffectChains(%d)\n", iAudioOutputDevice));
2726 schoenebeck 2135 LSCPResultSet result;
2727     try {
2728     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2729     if (!devices.count(iAudioOutputDevice))
2730     throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2731     AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2732 schoenebeck 2137 int n = pDevice->SendEffectChainCount();
2733 schoenebeck 2135 result.Add(n);
2734     } catch (Exception e) {
2735     result.Error(e);
2736     }
2737     return result.Produce();
2738     }
2739    
2740 schoenebeck 2137 String LSCPServer::ListSendEffectChains(int iAudioOutputDevice) {
2741     dmsg(2,("LSCPServer: ListSendEffectChains(%d)\n", iAudioOutputDevice));
2742 schoenebeck 2135 LSCPResultSet result;
2743     String list;
2744     try {
2745     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2746     if (!devices.count(iAudioOutputDevice))
2747     throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2748     AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2749 schoenebeck 2137 int n = pDevice->SendEffectChainCount();
2750 schoenebeck 2135 for (int i = 0; i < n; i++) {
2751 schoenebeck 2137 EffectChain* pEffectChain = pDevice->SendEffectChain(i);
2752 schoenebeck 2135 if (i) list += ",";
2753     list += ToString(pEffectChain->ID());
2754     }
2755     } catch (Exception e) {
2756     result.Error(e);
2757     }
2758     result.Add(list);
2759     return result.Produce();
2760     }
2761    
2762 schoenebeck 2137 String LSCPServer::AddSendEffectChain(int iAudioOutputDevice) {
2763     dmsg(2,("LSCPServer: AddSendEffectChain(%d)\n", iAudioOutputDevice));
2764 schoenebeck 2135 LSCPResultSet result;
2765     try {
2766     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2767     if (!devices.count(iAudioOutputDevice))
2768     throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2769     AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2770 schoenebeck 2137 EffectChain* pEffectChain = pDevice->AddSendEffectChain();
2771 schoenebeck 2135 result.Add(pEffectChain->ID());
2772     } catch (Exception e) {
2773     result.Error(e);
2774     }
2775     return result.Produce();
2776     }
2777    
2778 schoenebeck 2137 String LSCPServer::RemoveSendEffectChain(int iAudioOutputDevice, int iSendEffectChain) {
2779     dmsg(2,("LSCPServer: RemoveSendEffectChain(%d,%d)\n", iAudioOutputDevice, iSendEffectChain));
2780 schoenebeck 2135 LSCPResultSet result;
2781     try {
2782     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2783     if (!devices.count(iAudioOutputDevice))
2784     throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2785     AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2786 schoenebeck 2137 for (int i = 0; i < pDevice->SendEffectChainCount(); i++) {
2787     EffectChain* pEffectChain = pDevice->SendEffectChain(i);
2788     if (pEffectChain->ID() == iSendEffectChain) {
2789     pDevice->RemoveSendEffectChain(i);
2790 schoenebeck 2135 return result.Produce();
2791     }
2792     }
2793     throw Exception(
2794 schoenebeck 2137 "There is no send effect chain with ID " +
2795     ToString(iSendEffectChain) + " for audio output device " +
2796 schoenebeck 2135 ToString(iAudioOutputDevice) + "."
2797     );
2798     } catch (Exception e) {
2799     result.Error(e);
2800     }
2801     return result.Produce();
2802     }
2803    
2804 schoenebeck 2137 static EffectChain* _getSendEffectChain(Sampler* pSampler, int iAudioOutputDevice, int iSendEffectChain) throw (Exception) {
2805 schoenebeck 2135 std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2806     if (!devices.count(iAudioOutputDevice))
2807     throw Exception(
2808     "There is no audio output device with index " +
2809     ToString(iAudioOutputDevice) + "."
2810     );
2811     AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2812 schoenebeck 2137 for (int i = 0; i < pDevice->SendEffectChainCount(); i++) {
2813     EffectChain* pEffectChain = pDevice->SendEffectChain(i);
2814     if (pEffectChain->ID() == iSendEffectChain) {
2815 schoenebeck 2135 return pEffectChain;
2816     }
2817     }
2818     throw Exception(
2819 schoenebeck 2137 "There is no send effect chain with ID " +
2820     ToString(iSendEffectChain) + " for audio output device " +
2821 schoenebeck 2135 ToString(iAudioOutputDevice) + "."
2822     );
2823     }
2824    
2825 schoenebeck 2137 String LSCPServer::GetSendEffectChainInfo(int iAudioOutputDevice, int iSendEffectChain) {
2826     dmsg(2,("LSCPServer: GetSendEffectChainInfo(%d,%d)\n", iAudioOutputDevice, iSendEffectChain));
2827 schoenebeck 2135 LSCPResultSet result;
2828     try {
2829     EffectChain* pEffectChain =
2830 schoenebeck 2137 _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2831 schoenebeck 2135 String sEffectSequence;
2832     for (int i = 0; i < pEffectChain->EffectCount(); i++) {
2833     if (i) sEffectSequence += ",";
2834     sEffectSequence += ToString(pEffectChain->GetEffect(i)->ID());
2835     }
2836     result.Add("EFFECT_COUNT", pEffectChain->EffectCount());
2837     result.Add("EFFECT_SEQUENCE", sEffectSequence);
2838     } catch (Exception e) {
2839     result.Error(e);
2840     }
2841     return result.Produce();
2842     }
2843    
2844 schoenebeck 2137 String LSCPServer::AppendSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectInstance) {
2845     dmsg(2,("LSCPServer: AppendSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectInstance));
2846 schoenebeck 2135 LSCPResultSet result;
2847     try {
2848     EffectChain* pEffectChain =
2849 schoenebeck 2137 _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2850 schoenebeck 2135 Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2851     if (!pEffect)
2852 schoenebeck 2137 throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2853 schoenebeck 2135 pEffectChain->AppendEffect(pEffect);
2854     } catch (Exception e) {
2855     result.Error(e);
2856     }
2857     return result.Produce();
2858     }
2859    
2860 schoenebeck 2137 String LSCPServer::InsertSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition, int iEffectInstance) {
2861     dmsg(2,("LSCPServer: InsertSendEffectChainEffect(%d,%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition, iEffectInstance));
2862 schoenebeck 2135 LSCPResultSet result;
2863     try {
2864     EffectChain* pEffectChain =
2865 schoenebeck 2137 _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2866 schoenebeck 2135 Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2867     if (!pEffect)
2868     throw Exception("There is no effect instance with index " + ToString(iEffectInstance));
2869     pEffectChain->InsertEffect(pEffect, iEffectChainPosition);
2870     } catch (Exception e) {
2871     result.Error(e);
2872     }
2873     return result.Produce();
2874     }
2875    
2876 schoenebeck 2137 String LSCPServer::RemoveSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition) {
2877     dmsg(2,("LSCPServer: RemoveSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition));
2878 schoenebeck 2135 LSCPResultSet result;
2879     try {
2880     EffectChain* pEffectChain =
2881 schoenebeck 2137 _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2882     pEffectChain->RemoveEffect(iEffectChainPosition);
2883 schoenebeck 2135 } catch (Exception e) {
2884     result.Error(e);
2885     }
2886     return result.Produce();
2887     }
2888    
2889 schoenebeck 1212 String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2890     dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2891     LSCPResultSet result;
2892     try {
2893 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2894 iliev 1337 if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2895 schoenebeck 1212 Engine* pEngine = pEngineChannel->GetEngine();
2896     InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2897     if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2898     InstrumentManager::instrument_id_t instrumentID;
2899     instrumentID.FileName = pEngineChannel->InstrumentFileName();
2900     instrumentID.Index = pEngineChannel->InstrumentIndex();
2901     pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2902     } catch (Exception e) {
2903     result.Error(e);
2904     }
2905     return result.Produce();
2906     }
2907    
2908 iliev 1771 String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) {
2909     dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2));
2910     LSCPResultSet result;
2911     try {
2912     EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2913    
2914     if (Arg1 > 127 || Arg2 > 127) {
2915     throw Exception("Invalid MIDI message");
2916     }
2917    
2918     VirtualMidiDevice* pMidiDevice = NULL;
2919     std::vector<EventHandler::midi_listener_entry>::iterator iter = eventHandler.channelMidiListeners.begin();
2920     for (; iter != eventHandler.channelMidiListeners.end(); ++iter) {
2921     if ((*iter).pEngineChannel == pEngineChannel) {
2922     pMidiDevice = (*iter).pMidiListener;
2923     break;
2924     }
2925     }
2926    
2927     if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device");
2928    
2929     if (MidiMsg == "NOTE_ON") {
2930 iliev 1774 pMidiDevice->SendNoteOnToDevice(Arg1, Arg2);
2931 iliev 1771 bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2);
2932     if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2933     } else if (MidiMsg == "NOTE_OFF") {
2934 iliev 1774 pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);
2935 iliev 1771 bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);
2936     if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2937 schoenebeck 2025 } else if (MidiMsg == "CC") {
2938     pMidiDevice->SendCCToDevice(Arg1, Arg2);
2939     bool b = pMidiDevice->SendCCToSampler(Arg1, Arg2);
2940     if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2941 iliev 1771 } else {
2942     throw Exception("Unknown MIDI message type: " + MidiMsg);
2943     }
2944     } catch (Exception e) {
2945     result.Error(e);
2946     }
2947     return result.Produce();
2948     }
2949    
2950 schoenebeck 973 /**
2951 schoenebeck 35 * Will be called by the parser to reset a particular sampler channel.
2952     */
2953 schoenebeck 53 String LSCPServer::ResetChannel(uint uiSamplerChannel) {
2954     dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
2955 senkov 120 LSCPResultSet result;
2956 schoenebeck 53 try {
2957 iliev 1771 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2958 schoenebeck 670 pEngineChannel->Reset();
2959 schoenebeck 53 }
2960 schoenebeck 880 catch (Exception e) {
2961 senkov 120 result.Error(e);
2962 schoenebeck 53 }
2963 senkov 120 return result.Produce();
2964 schoenebeck 35 }
2965    
2966     /**
2967 schoenebeck 212 * Will be called by the parser to reset the whole sampler.
2968     */
2969     String LSCPServer::ResetSampler() {
2970     dmsg(2,("LSCPServer: ResetSampler()\n"));
2971     pSampler->Reset();
2972     LSCPResultSet result;
2973     return result.Produce();
2974     }
2975    
2976     /**
2977 schoenebeck 563 * Will be called by the parser to return general informations about this
2978     * sampler.
2979     */
2980     String LSCPServer::GetServerInfo() {
2981     dmsg(2,("LSCPServer: GetServerInfo()\n"));
2982 schoenebeck 1399 const std::string description =
2983     _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2984 schoenebeck 563 LSCPResultSet result;
2985 schoenebeck 1399 result.Add("DESCRIPTION", description);
2986 schoenebeck 570 result.Add("VERSION", VERSION);
2987 schoenebeck 947 result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2988 iliev 1161 #if HAVE_SQLITE3
2989     result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2990     #else
2991     result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2992     #endif
2993 schoenebeck 1252
2994 schoenebeck 563 return result.Produce();
2995     }
2996    
2997     /**
2998 iliev 1541 * Will be called by the parser to return the current number of all active streams.
2999     */
3000     String LSCPServer::GetTotalStreamCount() {
3001     dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
3002     LSCPResultSet result;
3003     result.Add(pSampler->GetDiskStreamCount());
3004     return result.Produce();
3005     }
3006    
3007     /**
3008 iliev 778 * Will be called by the parser to return the current number of all active voices.
3009     */
3010     String LSCPServer::GetTotalVoiceCount() {
3011     dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
3012     LSCPResultSet result;
3013     result.Add(pSampler->GetVoiceCount());
3014     return result.Produce();
3015     }
3016    
3017     /**
3018     * Will be called by the parser to return the maximum number of voices.
3019     */
3020     String LSCPServer::GetTotalVoiceCountMax() {
3021     dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
3022     LSCPResultSet result;
3023 schoenebeck 1800 result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES);
3024 iliev 778 return result.Produce();
3025     }
3026    
3027 schoenebeck 1800 /**
3028     * Will be called by the parser to return the sampler global maximum
3029     * allowed number of voices.
3030     */
3031     String LSCPServer::GetGlobalMaxVoices() {
3032     dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));
3033     LSCPResultSet result;
3034     result.Add(GLOBAL_MAX_VOICES);
3035     return result.Produce();
3036     }
3037    
3038     /**
3039     * Will be called by the parser to set the sampler global maximum number of
3040     * voices.
3041     */
3042     String LSCPServer::SetGlobalMaxVoices(int iVoices) {
3043     dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));
3044     LSCPResultSet result;
3045     try {
3046     if (iVoices < 1) throw Exception("Maximum voices may not be less than 1");
3047     GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp
3048     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
3049     if (engines.size() > 0) {
3050     std::set<Engine*>::iterator iter = engines.begin();
3051     std::set<Engine*>::iterator end = engines.end();
3052     for (; iter != end; ++iter) {
3053     (*iter)->SetMaxVoices(iVoices);
3054     }
3055     }
3056     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES));
3057     } catch (Exception e) {
3058     result.Error(e);
3059     }
3060     return result.Produce();
3061     }
3062    
3063     /**
3064     * Will be called by the parser to return the sampler global maximum
3065     * allowed number of disk streams.
3066     */
3067     String LSCPServer::GetGlobalMaxStreams() {
3068     dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));
3069     LSCPResultSet result;
3070     result.Add(GLOBAL_MAX_STREAMS);
3071     return result.Produce();
3072     }
3073    
3074     /**
3075     * Will be called by the parser to set the sampler global maximum number of
3076     * disk streams.
3077     */
3078     String LSCPServer::SetGlobalMaxStreams(int iStreams) {
3079     dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));
3080     LSCPResultSet result;
3081     try {
3082     if (iStreams < 0) throw Exception("Maximum disk streams may not be negative");
3083     GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp
3084     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
3085     if (engines.size() > 0) {
3086     std::set<Engine*>::iterator iter = engines.begin();
3087     std::set<Engine*>::iterator end = engines.end();
3088     for (; iter != end; ++iter) {
3089     (*iter)->SetMaxDiskStreams(iStreams);
3090     }
3091     }
3092     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS));
3093     } catch (Exception e) {
3094     result.Error(e);
3095     }
3096     return result.Produce();
3097     }
3098    
3099 schoenebeck 1005 String LSCPServer::GetGlobalVolume() {
3100     LSCPResultSet result;
3101     result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp
3102     return result.Produce();
3103     }
3104    
3105     String LSCPServer::SetGlobalVolume(double dVolume) {
3106     LSCPResultSet result;
3107     try {
3108     if (dVolume < 0) throw Exception("Volume may not be negative");
3109 schoenebeck 1724 GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
3110 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
3111 schoenebeck 1005 } catch (Exception e) {
3112     result.Error(e);
3113     }
3114     return result.Produce();
3115     }
3116    
3117 schoenebeck 1525 String LSCPServer::GetFileInstruments(String Filename) {
3118 iliev 1535 dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
3119 schoenebeck 1525 LSCPResultSet result;
3120 iliev 1535 try {
3121     VerifyFile(Filename);
3122     } catch (Exception e) {
3123     result.Error(e);
3124     return result.Produce();
3125     }
3126 schoenebeck 1525 // try to find a sampler engine that can handle the file
3127     bool bFound = false;
3128     std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3129     for (int i = 0; !bFound && i < engineTypes.size(); i++) {
3130     Engine* pEngine = NULL;
3131     try {
3132     pEngine = EngineFactory::Create(engineTypes[i]);
3133     if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3134     InstrumentManager* pManager = pEngine->GetInstrumentManager();
3135     if (pManager) {
3136     std::vector<InstrumentManager::instrument_id_t> IDs =
3137     pManager->GetInstrumentFileContent(Filename);
3138     // return the amount of instruments in the file
3139     result.Add(IDs.size());
3140     // no more need to ask other engine types
3141     bFound = true;
3142     } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3143     } catch (Exception e) {
3144     // NOOP, as exception is thrown if engine doesn't support file
3145     }
3146     if (pEngine) EngineFactory::Destroy(pEngine);
3147     }
3148 iliev 1535
3149     if (!bFound) result.Error("Unknown file format");
3150 schoenebeck 1525 return result.Produce();
3151     }
3152    
3153     String LSCPServer::ListFileInstruments(String Filename) {
3154 iliev 1535 dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
3155 schoenebeck 1525 LSCPResultSet result;
3156 iliev 1535 try {
3157     VerifyFile(Filename);
3158     } catch (Exception e) {
3159     result.Error(e);
3160     return result.Produce();
3161     }
3162 schoenebeck 1525 // try to find a sampler engine that can handle the file
3163     bool bFound = false;
3164     std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3165     for (int i = 0; !bFound && i < engineTypes.size(); i++) {
3166     Engine* pEngine = NULL;
3167     try {
3168     pEngine = EngineFactory::Create(engineTypes[i]);
3169     if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3170     InstrumentManager* pManager = pEngine->GetInstrumentManager();
3171     if (pManager) {
3172     std::vector<InstrumentManager::instrument_id_t> IDs =
3173     pManager->GetInstrumentFileContent(Filename);
3174     // return a list of IDs of the instruments in the file
3175     String s;
3176     for (int j = 0; j < IDs.size(); j++) {
3177     if (s.size()) s += ",";
3178     s += ToString(IDs[j].Index);
3179     }
3180     result.Add(s);
3181     // no more need to ask other engine types
3182     bFound = true;
3183     } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3184     } catch (Exception e) {
3185     // NOOP, as exception is thrown if engine doesn't support file
3186     }
3187     if (pEngine) EngineFactory::Destroy(pEngine);
3188     }
3189 iliev 1535
3190     if (!bFound) result.Error("Unknown file format");
3191 schoenebeck 1525 return result.Produce();
3192     }
3193    
3194     String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
3195 iliev 1535 dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
3196 schoenebeck 1525 LSCPResultSet result;
3197 iliev 1535 try {
3198     VerifyFile(Filename);
3199     } catch (Exception e) {
3200     result.Error(e);
3201     return result.Produce();
3202     }
3203 schoenebeck 1525 InstrumentManager::instrument_id_t id;
3204     id.FileName = Filename;
3205     id.Index = InstrumentID;
3206     // try to find a sampler engine that can handle the file
3207     bool bFound = false;
3208 schoenebeck 1536 bool bFatalErr = false;
3209 schoenebeck 1525 std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3210 schoenebeck 1536 for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
3211 schoenebeck 1525 Engine* pEngine = NULL;
3212     try {
3213     pEngine = EngineFactory::Create(engineTypes[i]);
3214     if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3215     InstrumentManager* pManager = pEngine->GetInstrumentManager();
3216     if (pManager) {
3217 schoenebeck 1536 // check if the instrument index is valid
3218     // FIXME: this won't work if an engine only supports parts of the instrument file
3219     std::vector<InstrumentManager::instrument_id_t> IDs =
3220     pManager->GetInstrumentFileContent(Filename);
3221     if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
3222     std::stringstream ss;
3223     ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
3224     bFatalErr = true;
3225     throw Exception(ss.str());
3226     }
3227     // get the info of the requested instrument
3228 schoenebeck 1525 InstrumentManager::instrument_info_t info =
3229     pManager->GetInstrumentInfo(id);
3230     // return detailed informations about the file
3231     result.Add("NAME", info.InstrumentName);
3232 iliev 1535 result.Add("FORMAT_FAMILY", engineTypes[i]);
3233 schoenebeck 1525 result.Add("FORMAT_VERSION", info.FormatVersion);
3234     result.Add("PRODUCT", info.Product);
3235     result.Add("ARTISTS", info.Artists);
3236 iliev 1771
3237     std::stringstream ss;
3238     bool b = false;
3239     for (int i = 0; i < 128; i++) {
3240     if (info.KeyBindings[i]) {
3241     if (b) ss << ',';
3242     ss << i; b = true;
3243     }
3244     }
3245     result.Add("KEY_BINDINGS", ss.str());
3246    
3247 iliev 1774 b = false;
3248 iliev 1771 std::stringstream ss2;
3249     for (int i = 0; i < 128; i++) {
3250     if (info.KeySwitchBindings[i]) {
3251     if (b) ss2 << ',';
3252     ss2 << i; b = true;
3253     }
3254     }
3255     result.Add("KEYSWITCH_BINDINGS", ss2.str());
3256 schoenebeck 1525 // no more need to ask other engine types
3257     bFound = true;
3258     } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3259     } catch (Exception e) {
3260 schoenebeck 1536 // usually NOOP, as exception is thrown if engine doesn't support file
3261     if (bFatalErr) result.Error(e);
3262 schoenebeck 1525 }
3263     if (pEngine) EngineFactory::Destroy(pEngine);
3264     }
3265 iliev 1535
3266 schoenebeck 1536 if (!bFound && !bFatalErr) result.Error("Unknown file format");
3267 schoenebeck 1525 return result.Produce();
3268     }
3269    
3270 iliev 1535 void LSCPServer::VerifyFile(String Filename) {
3271 senoner 1537 #if WIN32
3272     WIN32_FIND_DATA win32FileAttributeData;
3273     BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
3274     if (!res) {
3275     std::stringstream ss;
3276     ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
3277     throw Exception(ss.str());
3278     }
3279     if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
3280     throw Exception("Directory is specified");
3281     }
3282 schoenebeck 1551 #else
3283 iliev 1835 File f(Filename);
3284     if(!f.Exist()) throw Exception(f.GetErrorMsg());
3285     if (f.IsDirectory()) throw Exception("Directory is specified");
3286 senoner 1537 #endif
3287 iliev 1535 }
3288    
3289 iliev 778 /**
3290 schoenebeck 35 * Will be called by the parser to subscribe a client (frontend) on the
3291     * server for receiving event messages.
3292     */
3293 senkov 170 String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
3294     dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
3295     LSCPResultSet result;
3296     SubscriptionMutex.Lock();
3297     eventSubscriptions[type].push_back(currentSocket);
3298     SubscriptionMutex.Unlock();
3299     return result.Produce();
3300 schoenebeck 35 }
3301    
3302     /**
3303     * Will be called by the parser to unsubscribe a client on the server
3304     * for not receiving further event messages.
3305     */
3306 senkov 170 String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
3307     dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
3308     LSCPResultSet result;
3309     SubscriptionMutex.Lock();
3310     eventSubscriptions[type].remove(currentSocket);
3311     SubscriptionMutex.Unlock();
3312     return result.Produce();
3313 schoenebeck 35 }
3314 capela 133
3315 iliev 1161 String LSCPServer::AddDbInstrumentDirectory(String Dir) {
3316     dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
3317     LSCPResultSet result;
3318     #if HAVE_SQLITE3
3319     try {
3320     InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
3321     } catch (Exception e) {
3322     result.Error(e);
3323     }
3324     #else
3325     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3326     #endif
3327     return result.Produce();
3328 senkov 397 }
3329    
3330 iliev 1161 String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
3331     dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
3332 senkov 397 LSCPResultSet result;
3333 schoenebeck 401 #if HAVE_SQLITE3
3334 iliev 1161 try {
3335     InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
3336     } catch (Exception e) {
3337     result.Error(e);
3338     }
3339     #else
3340     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3341     #endif
3342     return result.Produce();
3343     }
3344 senkov 397
3345 iliev 1187 String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
3346     dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3347 iliev 1161 LSCPResultSet result;
3348     #if HAVE_SQLITE3
3349     try {
3350 iliev 1187 result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
3351 iliev 1161 } catch (Exception e) {
3352     result.Error(e);
3353 senkov 397 }
3354 iliev 1161 #else
3355     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3356     #endif
3357     return result.Produce();
3358     }
3359    
3360 iliev 1187 String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
3361     dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3362 iliev 1161 LSCPResultSet result;
3363     #if HAVE_SQLITE3
3364     try {
3365     String list;
3366 iliev 1187 StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
3367 iliev 1161
3368     for (int i = 0; i < dirs->size(); i++) {
3369     if (list != "") list += ",";
3370 iliev 1350 list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
3371 iliev 1161 }
3372    
3373     result.Add(list);
3374     } catch (Exception e) {
3375     result.Error(e);
3376 senkov 397 }
3377     #else
3378 iliev 1161 result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3379 senkov 397 #endif
3380     return result.Produce();
3381     }
3382    
3383 iliev 1161 String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
3384     dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
3385     LSCPResultSet result;
3386     #if HAVE_SQLITE3
3387     try {
3388     DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
3389    
3390 iliev 1403 result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3391 iliev 1161 result.Add("CREATED", info.Created);
3392     result.Add("MODIFIED", info.Modified);
3393     } catch (Exception e) {
3394     result.Error(e);
3395     }
3396     #else
3397     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3398     #endif
3399     return result.Produce();
3400     }
3401    
3402     String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
3403     dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
3404     LSCPResultSet result;
3405     #if HAVE_SQLITE3
3406     try {
3407     InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
3408     } catch (Exception e) {
3409     result.Error(e);
3410     }
3411     #else
3412     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3413     #endif
3414     return result.Produce();
3415     }
3416    
3417     String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
3418     dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
3419     LSCPResultSet result;
3420     #if HAVE_SQLITE3
3421     try {
3422     InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
3423     } catch (Exception e) {
3424     result.Error(e);
3425     }
3426     #else
3427     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3428     #endif
3429     return result.Produce();
3430     }
3431    
3432 iliev 1187 String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
3433     dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
3434     LSCPResultSet result;
3435     #if HAVE_SQLITE3
3436     try {
3437     InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
3438     } catch (Exception e) {
3439     result.Error(e);
3440     }
3441     #else
3442     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3443     #endif
3444     return result.Produce();
3445     }
3446    
3447 iliev 1161 String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
3448     dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
3449     LSCPResultSet result;
3450     #if HAVE_SQLITE3
3451     try {
3452     InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
3453     } catch (Exception e) {
3454     result.Error(e);
3455     }
3456     #else
3457     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3458     #endif
3459     return result.Produce();
3460     }
3461    
3462 iliev 1200 String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
3463     dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
3464 iliev 1161 LSCPResultSet result;
3465     #if HAVE_SQLITE3
3466     try {
3467 iliev 1200 int id;
3468     InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3469     id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
3470     if (bBackground) result = id;
3471 iliev 1161 } catch (Exception e) {
3472     result.Error(e);
3473     }
3474     #else
3475     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3476     #endif
3477     return result.Produce();
3478     }
3479    
3480 iliev 1781 String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) {
3481     dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir));
3482 iliev 1161 LSCPResultSet result;
3483     #if HAVE_SQLITE3
3484     try {
3485 iliev 1200 int id;
3486     InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3487     if (ScanMode.compare("RECURSIVE") == 0) {
3488 iliev 1781 id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir);
3489 iliev 1200 } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
3490 iliev 1781 id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir);
3491 iliev 1200 } else if (ScanMode.compare("FLAT") == 0) {
3492 iliev 1781 id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir);
3493 iliev 1200 } else {
3494     throw Exception("Unknown scan mode: " + ScanMode);
3495     }
3496 schoenebeck 1252
3497 iliev 1200 if (bBackground) result = id;
3498 iliev 1161 } catch (Exception e) {
3499     result.Error(e);
3500     }
3501     #else
3502     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3503     #endif
3504     return result.Produce();
3505     }
3506    
3507     String LSCPServer::RemoveDbInstrument(String Instr) {
3508     dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
3509     LSCPResultSet result;
3510     #if HAVE_SQLITE3
3511     try {
3512     InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
3513     } catch (Exception e) {
3514     result.Error(e);
3515     }
3516     #else
3517     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3518     #endif
3519     return result.Produce();
3520     }
3521    
3522 iliev 1187 String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
3523     dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3524 iliev 1161 LSCPResultSet result;
3525     #if HAVE_SQLITE3
3526     try {
3527 iliev 1187 result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
3528 iliev 1161 } catch (Exception e) {
3529     result.Error(e);
3530     }
3531     #else
3532     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3533     #endif
3534     return result.Produce();
3535     }
3536    
3537 iliev 1187 String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
3538     dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3539 iliev 1161 LSCPResultSet result;
3540     #if HAVE_SQLITE3
3541     try {
3542     String list;
3543 iliev 1187 StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
3544 iliev 1161
3545     for (int i = 0; i < instrs->size(); i++) {
3546     if (list != "") list += ",";
3547 iliev 1350 list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
3548 iliev 1161 }
3549    
3550     result.Add(list);
3551     } catch (Exception e) {
3552     result.Error(e);
3553     }
3554     #else
3555     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3556     #endif
3557     return result.Produce();
3558     }
3559    
3560     String LSCPServer::GetDbInstrumentInfo(String Instr) {
3561     dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
3562     LSCPResultSet result;
3563     #if HAVE_SQLITE3
3564     try {
3565     DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
3566    
3567     result.Add("INSTRUMENT_FILE", info.InstrFile);
3568     result.Add("INSTRUMENT_NR", info.InstrNr);
3569     result.Add("FORMAT_FAMILY", info.FormatFamily);
3570     result.Add("FORMAT_VERSION", info.FormatVersion);
3571     result.Add("SIZE", (int)info.Size);
3572     result.Add("CREATED", info.Created);
3573     result.Add("MODIFIED", info.Modified);
3574 iliev 1403 result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3575 iliev 1161 result.Add("IS_DRUM", info.IsDrum);
3576 iliev 1403 result.Add("PRODUCT", _escapeLscpResponse(info.Product));
3577     result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
3578     result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
3579 iliev 1161 } catch (Exception e) {
3580     result.Error(e);
3581     }
3582     #else
3583     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3584     #endif
3585     return result.Produce();
3586     }
3587    
3588 iliev 1200 String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
3589     dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
3590     LSCPResultSet result;
3591     #if HAVE_SQLITE3
3592     try {
3593     ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
3594    
3595     result.Add("FILES_TOTAL", job.FilesTotal);
3596     result.Add("FILES_SCANNED", job.FilesScanned);
3597     result.Add("SCANNING", job.Scanning);
3598     result.Add("STATUS", job.Status);
3599     } catch (Exception e) {
3600     result.Error(e);
3601     }
3602     #else
3603     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3604     #endif
3605     return result.Produce();
3606     }
3607    
3608 iliev 1161 String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
3609     dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
3610     LSCPResultSet result;
3611     #if HAVE_SQLITE3
3612     try {
3613     InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
3614     } catch (Exception e) {
3615     result.Error(e);
3616     }
3617     #else
3618     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3619     #endif
3620     return result.Produce();
3621     }
3622    
3623     String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
3624     dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3625     LSCPResultSet result;
3626     #if HAVE_SQLITE3
3627     try {
3628     InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
3629     } catch (Exception e) {
3630     result.Error(e);
3631     }
3632     #else
3633     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3634     #endif
3635     return result.Produce();
3636     }
3637    
3638 iliev 1187 String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
3639     dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3640     LSCPResultSet result;
3641     #if HAVE_SQLITE3
3642     try {
3643     InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
3644     } catch (Exception e) {
3645     result.Error(e);
3646     }
3647     #else
3648     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3649     #endif
3650     return result.Produce();
3651     }
3652    
3653 iliev 1161 String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
3654     dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
3655     LSCPResultSet result;
3656     #if HAVE_SQLITE3
3657     try {
3658     InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
3659     } catch (Exception e) {
3660     result.Error(e);
3661     }
3662     #else
3663     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3664     #endif
3665     return result.Produce();
3666     }
3667    
3668 iliev 1727 String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) {
3669     dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str()));
3670     LSCPResultSet result;
3671     #if HAVE_SQLITE3
3672     try {
3673     InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath);
3674     } catch (Exception e) {
3675     result.Error(e);
3676     }
3677     #else
3678     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3679     #endif
3680     return result.Produce();
3681     }
3682    
3683     String LSCPServer::FindLostDbInstrumentFiles() {
3684     dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n"));
3685     LSCPResultSet result;
3686     #if HAVE_SQLITE3
3687     try {
3688     String list;
3689     StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles();
3690    
3691     for (int i = 0; i < pLostFiles->size(); i++) {
3692     if (list != "") list += ",";
3693     list += "'" + pLostFiles->at(i) + "'";
3694     }
3695    
3696     result.Add(list);
3697     } catch (Exception e) {
3698     result.Error(e);
3699     }
3700     #else
3701     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3702     #endif
3703     return result.Produce();
3704     }
3705    
3706 iliev 1187 String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
3707     dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
3708     LSCPResultSet result;
3709     #if HAVE_SQLITE3
3710     try {
3711     SearchQuery Query;
3712     std::map<String,String>::iterator iter;
3713     for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3714     if (iter->first.compare("NAME") == 0) {
3715     Query.Name = iter->second;
3716     } else if (iter->first.compare("CREATED") == 0) {
3717     Query.SetCreated(iter->second);
3718     } else if (iter->first.compare("MODIFIED") == 0) {
3719     Query.SetModified(iter->second);
3720     } else if (iter->first.compare("DESCRIPTION") == 0) {
3721     Query.Description = iter->second;
3722     } else {
3723     throw Exception("Unknown search criteria: " + iter->first);
3724     }
3725     }
3726 iliev 1161
3727 iliev 1187 String list;
3728     StringListPtr pDirectories =
3729     InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
3730    
3731     for (int i = 0; i < pDirectories->size(); i++) {
3732     if (list != "") list += ",";
3733 iliev 1345 list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
3734 iliev 1187 }
3735    
3736     result.Add(list);
3737     } catch (Exception e) {
3738     result.Error(e);
3739     }
3740     #else
3741     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3742     #endif
3743     return result.Produce();
3744     }
3745    
3746     String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
3747     dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
3748     LSCPResultSet result;
3749     #if HAVE_SQLITE3
3750     try {
3751     SearchQuery Query;
3752     std::map<String,String>::iterator iter;
3753     for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3754     if (iter->first.compare("NAME") == 0) {
3755     Query.Name = iter->second;
3756     } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
3757     Query.SetFormatFamilies(iter->second);
3758     } else if (iter->first.compare("SIZE") == 0) {
3759     Query.SetSize(iter->second);
3760     } else if (iter->first.compare("CREATED") == 0) {
3761     Query.SetCreated(iter->second);
3762     } else if (iter->first.compare("MODIFIED") == 0) {
3763     Query.SetModified(iter->second);
3764     } else if (iter->first.compare("DESCRIPTION") == 0) {
3765     Query.Description = iter->second;
3766     } else if (iter->first.compare("IS_DRUM") == 0) {
3767     if (!strcasecmp(iter->second.c_str(), "true")) {
3768     Query.InstrType = SearchQuery::DRUM;
3769     } else {
3770     Query.InstrType = SearchQuery::CHROMATIC;
3771     }
3772     } else if (iter->first.compare("PRODUCT") == 0) {
3773     Query.Product = iter->second;
3774     } else if (iter->first.compare("ARTISTS") == 0) {
3775     Query.Artists = iter->second;
3776     } else if (iter->first.compare("KEYWORDS") == 0) {
3777     Query.Keywords = iter->second;
3778     } else {
3779     throw Exception("Unknown search criteria: " + iter->first);
3780     }
3781     }
3782    
3783     String list;
3784     StringListPtr pInstruments =
3785     InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
3786    
3787     for (int i = 0; i < pInstruments->size(); i++) {
3788     if (list != "") list += ",";
3789 iliev 1345 list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
3790 iliev 1187 }
3791    
3792     result.Add(list);
3793     } catch (Exception e) {
3794     result.Error(e);
3795     }
3796     #else
3797     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3798     #endif
3799     return result.Produce();
3800     }
3801    
3802 iliev 1353 String LSCPServer::FormatInstrumentsDb() {
3803     dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
3804     LSCPResultSet result;
3805     #if HAVE_SQLITE3
3806     try {
3807     InstrumentsDb::GetInstrumentsDb()->Format();
3808     } catch (Exception e) {
3809     result.Error(e);
3810     }
3811     #else
3812     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3813     #endif
3814     return result.Produce();
3815     }
3816 iliev 1187
3817 iliev 1353
3818 schoenebeck 210 /**
3819     * Will be called by the parser to enable or disable echo mode; if echo
3820     * mode is enabled, all commands from the client will (immediately) be
3821     * echoed back to the client.
3822     */
3823     String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
3824     dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
3825     LSCPResultSet result;
3826     try {
3827     if (boolean_value == 0) pSession->bVerbose = false;
3828     else if (boolean_value == 1) pSession->bVerbose = true;
3829 schoenebeck 880 else throw Exception("Not a boolean value, must either be 0 or 1");
3830 schoenebeck 210 }
3831 schoenebeck 880 catch (Exception e) {
3832 schoenebeck 210 result.Error(e);
3833     }
3834     return result.Produce();
3835     }
3836 persson 1765
3837     }

  ViewVC Help
Powered by ViewVC