/[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 2375 - (hide annotations) (download)
Thu Oct 4 17:45:22 2012 UTC (11 years, 5 months ago) by schoenebeck
File size: 154322 byte(s)
* Added new C++ API method Sampler::GetGlobalMaxVoices().
* Added new C++ API method Sampler::GetGlobalMaxStreams().
* Added new C++ API method Sampler::SetGlobalMaxVoices().
* Added new C++ API method Sampler::SetGlobalMaxStreams().
* CoreMIDI: remove ports that disappeared on the OS from
  the internal list of port bindings
* Fixed erroneous piping of error message in
  src/engines/common/VoiceBase.h (labelled "Disk stream not
  available in time").

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

  ViewVC Help
Powered by ViewVC