/[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 1200 - (hide annotations) (download)
Thu May 24 14:04:18 2007 UTC (16 years, 10 months ago) by iliev
File size: 108753 byte(s)
* Implemented instrument scanning in background
  and commands for monitoring the scan progress

1 schoenebeck 35 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 1009 * Copyright (C) 2005 - 2007 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     #include "lscpserver.h"
25 senkov 113 #include "lscpresultset.h"
26 senkov 170 #include "lscpevent.h"
27 schoenebeck 35
28 schoenebeck 411 #include <fcntl.h>
29    
30 iliev 1161 #if ! HAVE_SQLITE3
31     #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
32 senkov 397 #endif
33    
34 schoenebeck 411 #include "../engines/EngineFactory.h"
35 schoenebeck 660 #include "../engines/EngineChannelFactory.h"
36 schoenebeck 203 #include "../drivers/audio/AudioOutputDeviceFactory.h"
37     #include "../drivers/midi/MidiInputDeviceFactory.h"
38 schoenebeck 53
39 senkov 170 /**
40     * Below are a few static members of the LSCPServer class.
41     * The big assumption here is that LSCPServer is going to remain a singleton.
42     * These members are used to support client connections.
43     * Class handles multiple connections at the same time using select() and non-blocking recv()
44     * Commands are processed by a single LSCPServer thread.
45     * Notifications are delivered either by the thread that originated them
46     * or (if the resultset is currently in progress) by the LSCPServer thread
47     * after the resultset was sent out.
48     * This makes sure that resultsets can not be interrupted by notifications.
49     * This also makes sure that the thread sending notification is not blocked
50     * by the LSCPServer thread.
51     */
52     fd_set LSCPServer::fdSet;
53     int LSCPServer::currentSocket = -1;
54 schoenebeck 210 std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
55 senkov 170 std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
56     std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
57     std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
58     Mutex LSCPServer::NotifyMutex = Mutex();
59     Mutex LSCPServer::NotifyBufferMutex = Mutex();
60     Mutex LSCPServer::SubscriptionMutex = Mutex();
61 senkov 360 Mutex LSCPServer::RTNotifyMutex = Mutex();
62 senkov 170
63 senkov 667 LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) {
64     SocketAddress.sin_family = AF_INET;
65     SocketAddress.sin_addr.s_addr = addr;
66     SocketAddress.sin_port = port;
67 schoenebeck 53 this->pSampler = pSampler;
68 iliev 981 LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_count, "AUDIO_OUTPUT_DEVICE_COUNT");
69     LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_info, "AUDIO_OUTPUT_DEVICE_INFO");
70     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_count, "MIDI_INPUT_DEVICE_COUNT");
71     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_info, "MIDI_INPUT_DEVICE_INFO");
72 schoenebeck 556 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
73 senkov 170 LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
74     LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
75     LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
76 schoenebeck 556 LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
77 iliev 1108 LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
78     LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
79 iliev 981 LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
80     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
81     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
82     LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
83 iliev 1161 LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
84     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
85     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
86     LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
87 iliev 1200 LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
88 senkov 170 LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
89 iliev 778 LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
90 iliev 1108 LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
91 schoenebeck 475 hSocket = -1;
92 schoenebeck 35 }
93    
94 schoenebeck 475 LSCPServer::~LSCPServer() {
95     if (hSocket >= 0) close(hSocket);
96     }
97    
98 iliev 1133 void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
99 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
100     }
101    
102 iliev 1133 void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
103 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
104     }
105    
106 iliev 1133 void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
107 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
108     }
109    
110 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
111 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
112     }
113    
114 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
115 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
116     }
117    
118 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
119 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
120     }
121    
122 iliev 1133 void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
123 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
124     }
125    
126 iliev 1133 void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
127 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
128     }
129    
130 iliev 1133 void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
131 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
132     }
133    
134 iliev 1133 void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
135 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
136     }
137    
138 iliev 1133 void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
139 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
140     }
141    
142 iliev 1133 void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
143 iliev 1130 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
144     }
145    
146 iliev 1161 #if HAVE_SQLITE3
147     void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
148     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, Dir));
149     }
150 iliev 1130
151 iliev 1161 void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
152     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, Dir));
153     }
154    
155     void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
156     Dir = "'" + Dir + "'";
157     NewName = "'" + NewName + "'";
158     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
159     }
160    
161     void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
162     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, Dir));
163     }
164    
165     void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
166     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, Instr));
167     }
168 iliev 1200
169 iliev 1161 void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
170     Instr = "'" + Instr + "'";
171     NewName = "'" + NewName + "'";
172     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
173     }
174 iliev 1200
175     void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
176     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
177     }
178 iliev 1161 #endif // HAVE_SQLITE3
179    
180    
181 schoenebeck 211 /**
182     * Blocks the calling thread until the LSCP Server is initialized and
183     * accepting socket connections, if the server is already initialized then
184     * this method will return immediately.
185     * @param TimeoutSeconds - optional: max. wait time in seconds
186     * (default: 0s)
187     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
188     * (default: 0ns)
189     * @returns 0 on success, a value less than 0 if timeout exceeded
190     */
191     int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
192     return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
193     }
194    
195 schoenebeck 35 int LSCPServer::Main() {
196 schoenebeck 475 hSocket = socket(AF_INET, SOCK_STREAM, 0);
197 schoenebeck 35 if (hSocket < 0) {
198     std::cerr << "LSCPServer: Could not create server socket." << std::endl;
199 schoenebeck 53 //return -1;
200     exit(EXIT_FAILURE);
201 schoenebeck 35 }
202    
203     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
204 schoenebeck 227 std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
205     for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
206     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
207     if (trial > LSCP_SERVER_BIND_TIMEOUT) {
208     std::cerr << "gave up!" << std::endl;
209     close(hSocket);
210     //return -1;
211     exit(EXIT_FAILURE);
212     }
213     else sleep(1); // sleep 1s
214     }
215     else break; // success
216     }
217 schoenebeck 35 }
218    
219     listen(hSocket, 1);
220 schoenebeck 211 Initialized.Set(true);
221 iliev 1130
222     // Registering event listeners
223 iliev 1133 pSampler->AddChannelCountListener(&eventHandler);
224     pSampler->AddAudioDeviceCountListener(&eventHandler);
225     pSampler->AddMidiDeviceCountListener(&eventHandler);
226     pSampler->AddVoiceCountListener(&eventHandler);
227     pSampler->AddStreamCountListener(&eventHandler);
228     pSampler->AddBufferFillListener(&eventHandler);
229     pSampler->AddTotalVoiceCountListener(&eventHandler);
230     pSampler->AddFxSendCountListener(&eventHandler);
231     MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
232     MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
233     MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
234     MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
235 iliev 1161 #if HAVE_SQLITE3
236     InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
237     #endif
238 schoenebeck 35 // now wait for client connections and handle their requests
239     sockaddr_in client;
240     int length = sizeof(client);
241 senkov 170 FD_ZERO(&fdSet);
242     FD_SET(hSocket, &fdSet);
243     int maxSessions = hSocket;
244 schoenebeck 203
245 iliev 793 timeval timeout;
246    
247 schoenebeck 35 while (true) {
248 iliev 793 // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
249     {
250     std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
251     std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
252     std::set<EngineChannel*>::iterator itEnd = engineChannels.end();
253     for (; itEngineChannel != itEnd; ++itEngineChannel) {
254     if ((*itEngineChannel)->StatusChanged()) {
255     SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));
256     }
257 iliev 1108
258     for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
259     FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
260     if(fxs != NULL && fxs->IsInfoChanged()) {
261     int chn = (*itEngineChannel)->iSamplerChannelIndex;
262     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
263     fxs->SetInfoChanged(false);
264     }
265     }
266 iliev 793 }
267     }
268    
269     //Now let's deliver late notifies (if any)
270     NotifyBufferMutex.Lock();
271     for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
272 wylder 814 #ifdef MSG_NOSIGNAL
273 iliev 793 send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
274 wylder 814 #else
275     send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
276     #endif
277 iliev 793 }
278 persson 836 bufferedNotifies.clear();
279 iliev 793 NotifyBufferMutex.Unlock();
280    
281     fd_set selectSet = fdSet;
282     timeout.tv_sec = 0;
283     timeout.tv_usec = 100000;
284    
285     int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
286    
287 senkov 170 if (retval == 0)
288 senkov 198 continue; //Nothing try again
289 senkov 170 if (retval == -1) {
290     std::cerr << "LSCPServer: Socket select error." << std::endl;
291     close(hSocket);
292     exit(EXIT_FAILURE);
293     }
294 schoenebeck 203
295 senkov 170 //Accept new connections now (if any)
296     if (FD_ISSET(hSocket, &selectSet)) {
297     int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
298     if (socket < 0) {
299     std::cerr << "LSCPServer: Client connection failed." << std::endl;
300     exit(EXIT_FAILURE);
301     }
302 schoenebeck 35
303 senkov 170 if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
304     std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
305     exit(EXIT_FAILURE);
306     }
307 schoenebeck 35
308 schoenebeck 210 // Parser initialization
309     yyparse_param_t yyparse_param;
310     yyparse_param.pServer = this;
311     yyparse_param.hSession = socket;
312    
313     Sessions.push_back(yyparse_param);
314 senkov 170 FD_SET(socket, &fdSet);
315     if (socket > maxSessions)
316     maxSessions = socket;
317     dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
318     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
319     continue; //Maybe this was the only selected socket, better select again
320     }
321 schoenebeck 35
322 senkov 170 //Something was selected and it was not the hSocket, so it must be some command(s) coming.
323 schoenebeck 210 for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
324     if (FD_ISSET((*iter).hSession, &selectSet)) { //Was it this socket?
325 senkov 170 if (GetLSCPCommand(iter)) { //Have we read the entire command?
326     dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
327 schoenebeck 219 int dummy; // just a temporary hack to fulfill the restart() function prototype
328     restart(NULL, dummy); // restart the 'scanner'
329 schoenebeck 210 currentSocket = (*iter).hSession; //a hack
330 schoenebeck 475 dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
331 schoenebeck 210 if ((*iter).bVerbose) { // if echo mode enabled
332     AnswerClient(bufferedCommands[currentSocket]);
333     }
334     int result = yyparse(&(*iter));
335 senkov 170 currentSocket = -1; //continuation of a hack
336     dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
337     if (result == LSCP_QUIT) { //Was it a quit command by any chance?
338     CloseConnection(iter);
339     }
340     }
341     //socket may have been closed, iter may be invalid, get out of the loop for now.
342     //we'll be back if there is data.
343 schoenebeck 203 break;
344 senkov 170 }
345     }
346 schoenebeck 35 }
347     }
348    
349 schoenebeck 210 void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
350     int socket = (*iter).hSession;
351 senkov 170 dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
352     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
353 schoenebeck 210 Sessions.erase(iter);
354 senkov 170 FD_CLR(socket, &fdSet);
355     SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
356     for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
357     iter->second.remove(socket);
358     }
359     SubscriptionMutex.Unlock();
360     NotifyMutex.Lock();
361     bufferedCommands.erase(socket);
362     bufferedNotifies.erase(socket);
363     close(socket);
364     NotifyMutex.Unlock();
365     }
366    
367 senkov 360 int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
368     int subs = 0;
369     SubscriptionMutex.Lock();
370     for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
371     iter != events.end(); iter++)
372     {
373     subs += eventSubscriptions.count(*iter);
374     }
375     SubscriptionMutex.Unlock();
376     return subs;
377     }
378    
379 senkov 170 void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
380     SubscriptionMutex.Lock();
381     if (eventSubscriptions.count(event.GetType()) == 0) {
382     SubscriptionMutex.Unlock(); //Nobody is subscribed to this event
383     return;
384     }
385     std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
386     std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
387     String notify = event.Produce();
388    
389     while (true) {
390     if (NotifyMutex.Trylock()) {
391     for(;iter != end; iter++)
392 wylder 814 #ifdef MSG_NOSIGNAL
393 iliev 707 send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
394 wylder 814 #else
395     send(*iter, notify.c_str(), notify.size(), 0);
396     #endif
397 senkov 170 NotifyMutex.Unlock();
398     break;
399     } else {
400     if (NotifyBufferMutex.Trylock()) {
401     for(;iter != end; iter++)
402     bufferedNotifies[*iter] += notify;
403     NotifyBufferMutex.Unlock();
404     break;
405     }
406     }
407     }
408     SubscriptionMutex.Unlock();
409     }
410    
411     extern int GetLSCPCommand( void *buf, int max_size ) {
412     String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
413     if (command.size() == 0) { //Parser wants input but we have nothing.
414     strcpy((char*) buf, "\n"); //So give it an empty command
415     return 1; //to keep it happy.
416     }
417    
418     if (max_size < command.size()) {
419     std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
420     return 0; //This will never happen
421     }
422    
423     strcpy((char*) buf, command.c_str());
424     LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
425     return command.size();
426     }
427    
428 schoenebeck 35 /**
429 senkov 170 * Will be called to try to read the command from the socket
430     * If command is read, it will return true. Otherwise false is returned.
431     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
432     */
433 schoenebeck 210 bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
434     int socket = (*iter).hSession;
435 senkov 170 char c;
436     int i = 0;
437     while (true) {
438     int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
439     if (result == 0) { //socket was selected, so 0 here means client has closed the connection
440     CloseConnection(iter);
441     break;
442     }
443     if (result == 1) {
444 schoenebeck 203 if (c == '\r')
445 senkov 170 continue; //Ignore CR
446     if (c == '\n') {
447 senkov 184 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
448 iliev 907 bufferedCommands[socket] += "\r\n";
449 senkov 170 return true; //Complete command was read
450     }
451     bufferedCommands[socket] += c;
452     }
453     if (result == -1) {
454     if (errno == EAGAIN) //Would block, try again later.
455     return false;
456     switch(errno) {
457     case EBADF:
458     dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
459     break;
460     case ECONNREFUSED:
461     dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
462     break;
463     case ENOTCONN:
464     dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
465     break;
466     case ENOTSOCK:
467     dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
468     break;
469     case EAGAIN:
470     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"));
471 schoenebeck 203 break;
472     case EINTR:
473 senkov 170 dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
474 schoenebeck 203 break;
475     case EFAULT:
476     dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
477     break;
478     case EINVAL:
479     dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
480     break;
481     case ENOMEM:
482     dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
483     break;
484     default:
485     dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
486     break;
487     }
488 senkov 170 CloseConnection(iter);
489     break;
490     }
491     }
492     return false;
493     }
494    
495     /**
496 schoenebeck 35 * Will be called by the parser whenever it wants to send an answer to the
497     * client / frontend.
498     *
499     * @param ReturnMessage - message that will be send to the client
500     */
501     void LSCPServer::AnswerClient(String ReturnMessage) {
502     dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
503 senkov 170 if (currentSocket != -1) {
504     NotifyMutex.Lock();
505 wylder 814 #ifdef MSG_NOSIGNAL
506 iliev 707 send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
507 wylder 814 #else
508     send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
509     #endif
510 senkov 170 NotifyMutex.Unlock();
511     }
512 schoenebeck 35 }
513    
514 capela 143 /**
515     * Find a created audio output device index.
516     */
517     int LSCPServer::GetAudioOutputDeviceIndex ( AudioOutputDevice *pDevice )
518     {
519     // Search for the created device to get its index
520     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
521     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
522     for (; iter != devices.end(); iter++) {
523     if (iter->second == pDevice)
524     return iter->first;
525     }
526     // Not found.
527     return -1;
528     }
529    
530 senkov 155 /**
531     * Find a created midi input device index.
532     */
533     int LSCPServer::GetMidiInputDeviceIndex ( MidiInputDevice *pDevice )
534     {
535     // Search for the created device to get its index
536     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
537     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
538     for (; iter != devices.end(); iter++) {
539     if (iter->second == pDevice)
540     return iter->first;
541     }
542     // Not found.
543     return -1;
544     }
545    
546 schoenebeck 123 String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
547     dmsg(2,("LSCPServer: CreateAudioOutputDevice(Driver=%s)\n", Driver.c_str()));
548     LSCPResultSet result;
549     try {
550     AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
551     // search for the created device to get its index
552 capela 143 int index = GetAudioOutputDeviceIndex(pDevice);
553 schoenebeck 880 if (index == -1) throw Exception("Internal error: could not find created audio output device.");
554 schoenebeck 123 result = index; // success
555     }
556 schoenebeck 880 catch (Exception e) {
557 schoenebeck 123 result.Error(e);
558     }
559     return result.Produce();
560     }
561    
562 senkov 155 String LSCPServer::CreateMidiInputDevice(String Driver, std::map<String,String> Parameters) {
563     dmsg(2,("LSCPServer: CreateMidiInputDevice(Driver=%s)\n", Driver.c_str()));
564     LSCPResultSet result;
565     try {
566     MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
567     // search for the created device to get its index
568     int index = GetMidiInputDeviceIndex(pDevice);
569 schoenebeck 880 if (index == -1) throw Exception("Internal error: could not find created midi input device.");
570 senkov 155 result = index; // success
571     }
572 schoenebeck 880 catch (Exception e) {
573 senkov 155 result.Error(e);
574     }
575     return result.Produce();
576     }
577    
578 schoenebeck 123 String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
579     dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
580     LSCPResultSet result;
581     try {
582     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
583 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
584 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
585     pSampler->DestroyAudioOutputDevice(pDevice);
586     }
587 schoenebeck 880 catch (Exception e) {
588 schoenebeck 123 result.Error(e);
589     }
590     return result.Produce();
591     }
592    
593 senkov 155 String LSCPServer::DestroyMidiInputDevice(uint DeviceIndex) {
594     dmsg(2,("LSCPServer: DestroyMidiInputDevice(DeviceIndex=%d)\n", DeviceIndex));
595     LSCPResultSet result;
596     try {
597     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
598 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
599 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
600     pSampler->DestroyMidiInputDevice(pDevice);
601     }
602 schoenebeck 880 catch (Exception e) {
603 senkov 155 result.Error(e);
604     }
605     return result.Produce();
606     }
607    
608 iliev 1135 EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
609     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
610     if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
611    
612     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
613     if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
614    
615     return pEngineChannel;
616     }
617    
618 schoenebeck 35 /**
619     * Will be called by the parser to load an instrument.
620     */
621 capela 137 String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground) {
622 schoenebeck 53 dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));
623 senkov 120 LSCPResultSet result;
624 schoenebeck 53 try {
625     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
626 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
627 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
628 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel yet");
629 schoenebeck 223 if (!pSamplerChannel->GetAudioOutputDevice())
630 schoenebeck 880 throw Exception("No audio output device connected to sampler channel");
631 capela 137 if (bBackground) {
632 schoenebeck 947 InstrumentManager::instrument_id_t id;
633     id.FileName = Filename;
634     id.Index = uiInstrument;
635     InstrumentManager::LoadInstrumentInBackground(id, pEngineChannel);
636 capela 137 }
637 schoenebeck 392 else {
638 schoenebeck 411 // tell the engine channel which instrument to load
639     pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
640 schoenebeck 392 // actually start to load the instrument (blocks until completed)
641 schoenebeck 411 pEngineChannel->LoadInstrument();
642 schoenebeck 392 }
643 schoenebeck 53 }
644 schoenebeck 880 catch (Exception e) {
645 senkov 120 result.Error(e);
646 schoenebeck 53 }
647 senkov 120 return result.Produce();
648 schoenebeck 35 }
649    
650     /**
651 schoenebeck 411 * Will be called by the parser to assign a sampler engine type to a
652     * sampler channel.
653 schoenebeck 35 */
654 schoenebeck 411 String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
655 schoenebeck 705 dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
656 senkov 120 LSCPResultSet result;
657 schoenebeck 475 try {
658 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
659 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
660 senkov 360 LockRTNotify();
661 schoenebeck 411 pSamplerChannel->SetEngineType(EngineName);
662 schoenebeck 705 if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
663 senkov 360 UnlockRTNotify();
664 schoenebeck 53 }
665 schoenebeck 880 catch (Exception e) {
666 senkov 120 result.Error(e);
667 schoenebeck 53 }
668 senkov 120 return result.Produce();
669 schoenebeck 35 }
670    
671     /**
672     * Will be called by the parser to get the amount of sampler channels.
673     */
674     String LSCPServer::GetChannels() {
675     dmsg(2,("LSCPServer: GetChannels()\n"));
676 senkov 120 LSCPResultSet result;
677     result.Add(pSampler->SamplerChannels());
678     return result.Produce();
679 schoenebeck 35 }
680    
681     /**
682 schoenebeck 209 * Will be called by the parser to get the list of sampler channels.
683     */
684     String LSCPServer::ListChannels() {
685     dmsg(2,("LSCPServer: ListChannels()\n"));
686     String list;
687     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
688     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
689     for (; iter != channels.end(); iter++) {
690     if (list != "") list += ",";
691     list += ToString(iter->first);
692     }
693     LSCPResultSet result;
694     result.Add(list);
695     return result.Produce();
696     }
697    
698     /**
699 schoenebeck 35 * Will be called by the parser to add a sampler channel.
700     */
701     String LSCPServer::AddChannel() {
702     dmsg(2,("LSCPServer: AddChannel()\n"));
703 persson 841 LockRTNotify();
704 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
705 persson 841 UnlockRTNotify();
706 senkov 120 LSCPResultSet result(pSamplerChannel->Index());
707     return result.Produce();
708 schoenebeck 35 }
709    
710     /**
711     * Will be called by the parser to remove a sampler channel.
712     */
713 schoenebeck 53 String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
714     dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
715 senkov 120 LSCPResultSet result;
716 senkov 360 LockRTNotify();
717 schoenebeck 53 pSampler->RemoveSamplerChannel(uiSamplerChannel);
718 senkov 360 UnlockRTNotify();
719 senkov 120 return result.Produce();
720 schoenebeck 35 }
721    
722     /**
723 capela 527 * Will be called by the parser to get the amount of all available engines.
724 schoenebeck 35 */
725     String LSCPServer::GetAvailableEngines() {
726     dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
727 schoenebeck 905 LSCPResultSet result;
728     try {
729     int n = EngineFactory::AvailableEngineTypes().size();
730     result.Add(n);
731     }
732     catch (Exception e) {
733     result.Error(e);
734     }
735 senkov 120 return result.Produce();
736 schoenebeck 35 }
737    
738     /**
739 capela 527 * Will be called by the parser to get a list of all available engines.
740     */
741     String LSCPServer::ListAvailableEngines() {
742     dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
743 schoenebeck 905 LSCPResultSet result;
744     try {
745     String s = EngineFactory::AvailableEngineTypesAsString();
746     result.Add(s);
747     }
748     catch (Exception e) {
749     result.Error(e);
750     }
751 capela 527 return result.Produce();
752     }
753    
754     /**
755 schoenebeck 411 * Will be called by the parser to get descriptions for a particular
756     * sampler engine.
757 schoenebeck 35 */
758     String LSCPServer::GetEngineInfo(String EngineName) {
759     dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
760 senkov 120 LSCPResultSet result;
761 persson 841 LockRTNotify();
762 schoenebeck 53 try {
763 schoenebeck 411 Engine* pEngine = EngineFactory::Create(EngineName);
764     result.Add("DESCRIPTION", pEngine->Description());
765     result.Add("VERSION", pEngine->Version());
766 schoenebeck 660 EngineFactory::Destroy(pEngine);
767 schoenebeck 53 }
768 schoenebeck 880 catch (Exception e) {
769 senkov 120 result.Error(e);
770 schoenebeck 53 }
771 persson 841 UnlockRTNotify();
772 senkov 120 return result.Produce();
773 schoenebeck 35 }
774    
775     /**
776     * Will be called by the parser to get informations about a particular
777     * sampler channel.
778     */
779 schoenebeck 53 String LSCPServer::GetChannelInfo(uint uiSamplerChannel) {
780     dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", uiSamplerChannel));
781 senkov 120 LSCPResultSet result;
782 senkov 113 try {
783     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
784 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
785 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
786 schoenebeck 123
787 senkov 117 //Defaults values
788     String EngineName = "NONE";
789 schoenebeck 225 float Volume = 0.0f;
790 senkov 117 String InstrumentFileName = "NONE";
791 senkov 376 String InstrumentName = "NONE";
792 capela 133 int InstrumentIndex = -1;
793     int InstrumentStatus = -1;
794 schoenebeck 225 int AudioOutputChannels = 0;
795     String AudioRouting;
796 schoenebeck 705 int Mute = 0;
797     bool Solo = false;
798 iliev 1130 String MidiInstrumentMap = "NONE";
799 schoenebeck 123
800 schoenebeck 475 if (pEngineChannel) {
801     EngineName = pEngineChannel->EngineName();
802 schoenebeck 411 AudioOutputChannels = pEngineChannel->Channels();
803     Volume = pEngineChannel->Volume();
804     InstrumentStatus = pEngineChannel->InstrumentStatus();
805     InstrumentIndex = pEngineChannel->InstrumentIndex();
806     if (InstrumentIndex != -1) {
807     InstrumentFileName = pEngineChannel->InstrumentFileName();
808     InstrumentName = pEngineChannel->InstrumentName();
809     }
810     for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
811 schoenebeck 225 if (AudioRouting != "") AudioRouting += ",";
812 schoenebeck 411 AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
813 schoenebeck 225 }
814 schoenebeck 705 Mute = pEngineChannel->GetMute();
815     Solo = pEngineChannel->GetSolo();
816 schoenebeck 973 if (pEngineChannel->UsesNoMidiInstrumentMap())
817     MidiInstrumentMap = "NONE";
818     else if (pEngineChannel->UsesDefaultMidiInstrumentMap())
819     MidiInstrumentMap = "DEFAULT";
820     else
821     MidiInstrumentMap = ToString(pEngineChannel->GetMidiInstrumentMap());
822 senkov 113 }
823 senkov 117
824     result.Add("ENGINE_NAME", EngineName);
825     result.Add("VOLUME", Volume);
826    
827 capela 143 //Some not-so-hardcoded stuff to make GUI look good
828     result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
829 schoenebeck 225 result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
830     result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
831 senkov 113
832 capela 159 result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
833     result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
834 schoenebeck 675 if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
835 schoenebeck 274 else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
836 capela 159
837 senkov 117 result.Add("INSTRUMENT_FILE", InstrumentFileName);
838     result.Add("INSTRUMENT_NR", InstrumentIndex);
839 senkov 376 result.Add("INSTRUMENT_NAME", InstrumentName);
840 capela 133 result.Add("INSTRUMENT_STATUS", InstrumentStatus);
841 schoenebeck 705 result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
842     result.Add("SOLO", Solo);
843 schoenebeck 973 result.Add("MIDI_INSTRUMENT_MAP", MidiInstrumentMap);
844 senkov 113 }
845 schoenebeck 880 catch (Exception e) {
846 senkov 120 result.Error(e);
847 senkov 113 }
848 senkov 120 return result.Produce();
849 schoenebeck 35 }
850    
851     /**
852     * Will be called by the parser to get the amount of active voices on a
853     * particular sampler channel.
854     */
855 schoenebeck 53 String LSCPServer::GetVoiceCount(uint uiSamplerChannel) {
856     dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
857 senkov 120 LSCPResultSet result;
858 schoenebeck 53 try {
859     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
860 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
861 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
862 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine loaded on sampler channel");
863     if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
864 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->VoiceCount());
865 schoenebeck 53 }
866 schoenebeck 880 catch (Exception e) {
867 senkov 120 result.Error(e);
868 schoenebeck 53 }
869 senkov 120 return result.Produce();
870 schoenebeck 35 }
871    
872     /**
873     * Will be called by the parser to get the amount of active disk streams on a
874     * particular sampler channel.
875     */
876 schoenebeck 53 String LSCPServer::GetStreamCount(uint uiSamplerChannel) {
877     dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
878 senkov 120 LSCPResultSet result;
879 schoenebeck 53 try {
880     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
881 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
882 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
883 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
884     if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
885 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
886 schoenebeck 53 }
887 schoenebeck 880 catch (Exception e) {
888 senkov 120 result.Error(e);
889 schoenebeck 53 }
890 senkov 120 return result.Produce();
891 schoenebeck 35 }
892    
893     /**
894     * Will be called by the parser to get the buffer fill states of all disk
895     * streams on a particular sampler channel.
896     */
897 schoenebeck 53 String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel) {
898     dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
899 senkov 120 LSCPResultSet result;
900 schoenebeck 53 try {
901     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
902 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
903 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
904 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
905     if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
906 schoenebeck 411 if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
907 senkov 129 else {
908     switch (ResponseType) {
909     case fill_response_bytes:
910 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
911     break;
912 senkov 129 case fill_response_percentage:
913 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
914     break;
915 senkov 129 default:
916 schoenebeck 880 throw Exception("Unknown fill response type");
917 senkov 129 }
918     }
919 schoenebeck 53 }
920 schoenebeck 880 catch (Exception e) {
921 senkov 120 result.Error(e);
922 schoenebeck 53 }
923 senkov 120 return result.Produce();
924 schoenebeck 35 }
925    
926 schoenebeck 123 String LSCPServer::GetAvailableAudioOutputDrivers() {
927     dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
928 senkov 120 LSCPResultSet result;
929 schoenebeck 53 try {
930 capela 527 int n = AudioOutputDeviceFactory::AvailableDrivers().size();
931     result.Add(n);
932     }
933 schoenebeck 880 catch (Exception e) {
934 capela 527 result.Error(e);
935     }
936     return result.Produce();
937     }
938    
939     String LSCPServer::ListAvailableAudioOutputDrivers() {
940     dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
941     LSCPResultSet result;
942     try {
943 schoenebeck 123 String s = AudioOutputDeviceFactory::AvailableDriversAsString();
944     result.Add(s);
945 schoenebeck 53 }
946 schoenebeck 880 catch (Exception e) {
947 schoenebeck 123 result.Error(e);
948 schoenebeck 53 }
949 senkov 120 return result.Produce();
950 schoenebeck 35 }
951    
952 senkov 155 String LSCPServer::GetAvailableMidiInputDrivers() {
953     dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
954     LSCPResultSet result;
955     try {
956 capela 527 int n = MidiInputDeviceFactory::AvailableDrivers().size();
957     result.Add(n);
958     }
959 schoenebeck 880 catch (Exception e) {
960 capela 527 result.Error(e);
961     }
962     return result.Produce();
963     }
964    
965     String LSCPServer::ListAvailableMidiInputDrivers() {
966     dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
967     LSCPResultSet result;
968     try {
969 senkov 155 String s = MidiInputDeviceFactory::AvailableDriversAsString();
970     result.Add(s);
971     }
972 schoenebeck 880 catch (Exception e) {
973 senkov 155 result.Error(e);
974     }
975     return result.Produce();
976     }
977    
978     String LSCPServer::GetMidiInputDriverInfo(String Driver) {
979     dmsg(2,("LSCPServer: GetMidiInputDriverInfo(Driver=%s)\n",Driver.c_str()));
980     LSCPResultSet result;
981     try {
982     result.Add("DESCRIPTION", MidiInputDeviceFactory::GetDriverDescription(Driver));
983     result.Add("VERSION", MidiInputDeviceFactory::GetDriverVersion(Driver));
984    
985     std::map<String,DeviceCreationParameter*> parameters = MidiInputDeviceFactory::GetAvailableDriverParameters(Driver);
986     if (parameters.size()) { // if there are parameters defined for this driver
987     String s;
988     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
989     for (;iter != parameters.end(); iter++) {
990     if (s != "") s += ",";
991     s += iter->first;
992     }
993     result.Add("PARAMETERS", s);
994     }
995     }
996 schoenebeck 880 catch (Exception e) {
997 senkov 155 result.Error(e);
998     }
999     return result.Produce();
1000     }
1001    
1002 schoenebeck 123 String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
1003     dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
1004     LSCPResultSet result;
1005     try {
1006     result.Add("DESCRIPTION", AudioOutputDeviceFactory::GetDriverDescription(Driver));
1007     result.Add("VERSION", AudioOutputDeviceFactory::GetDriverVersion(Driver));
1008    
1009     std::map<String,DeviceCreationParameter*> parameters = AudioOutputDeviceFactory::GetAvailableDriverParameters(Driver);
1010     if (parameters.size()) { // if there are parameters defined for this driver
1011     String s;
1012     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1013     for (;iter != parameters.end(); iter++) {
1014     if (s != "") s += ",";
1015     s += iter->first;
1016     }
1017     result.Add("PARAMETERS", s);
1018     }
1019     }
1020 schoenebeck 880 catch (Exception e) {
1021 schoenebeck 123 result.Error(e);
1022     }
1023     return result.Produce();
1024     }
1025    
1026 senkov 155 String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1027 schoenebeck 226 dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1028 senkov 155 LSCPResultSet result;
1029     try {
1030     DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
1031     result.Add("TYPE", pParameter->Type());
1032     result.Add("DESCRIPTION", pParameter->Description());
1033 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
1034     result.Add("FIX", pParameter->Fix());
1035     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1036 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
1037     optional<String> oDefault = pParameter->Default(DependencyList);
1038     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
1039     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
1040     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1041     if (oDepends) result.Add("DEPENDS", *oDepends);
1042     if (oDefault) result.Add("DEFAULT", *oDefault);
1043     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
1044     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
1045     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1046 senkov 155 }
1047 schoenebeck 880 catch (Exception e) {
1048 senkov 155 result.Error(e);
1049     }
1050     return result.Produce();
1051     }
1052    
1053 schoenebeck 123 String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
1054 schoenebeck 226 dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
1055 schoenebeck 123 LSCPResultSet result;
1056     try {
1057     DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
1058     result.Add("TYPE", pParameter->Type());
1059     result.Add("DESCRIPTION", pParameter->Description());
1060 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
1061     result.Add("FIX", pParameter->Fix());
1062     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1063 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
1064     optional<String> oDefault = pParameter->Default(DependencyList);
1065     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
1066     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
1067     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
1068     if (oDepends) result.Add("DEPENDS", *oDepends);
1069     if (oDefault) result.Add("DEFAULT", *oDefault);
1070     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
1071     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
1072     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1073 schoenebeck 123 }
1074 schoenebeck 880 catch (Exception e) {
1075 schoenebeck 123 result.Error(e);
1076     }
1077     return result.Produce();
1078     }
1079    
1080     String LSCPServer::GetAudioOutputDeviceCount() {
1081     dmsg(2,("LSCPServer: GetAudioOutputDeviceCount()\n"));
1082     LSCPResultSet result;
1083     try {
1084     uint count = pSampler->AudioOutputDevices();
1085 senkov 138 result.Add(count); // success
1086 schoenebeck 123 }
1087 schoenebeck 880 catch (Exception e) {
1088 schoenebeck 123 result.Error(e);
1089     }
1090     return result.Produce();
1091     }
1092    
1093 senkov 155 String LSCPServer::GetMidiInputDeviceCount() {
1094     dmsg(2,("LSCPServer: GetMidiInputDeviceCount()\n"));
1095     LSCPResultSet result;
1096     try {
1097     uint count = pSampler->MidiInputDevices();
1098     result.Add(count); // success
1099     }
1100 schoenebeck 880 catch (Exception e) {
1101 senkov 155 result.Error(e);
1102     }
1103     return result.Produce();
1104     }
1105    
1106 schoenebeck 123 String LSCPServer::GetAudioOutputDevices() {
1107     dmsg(2,("LSCPServer: GetAudioOutputDevices()\n"));
1108     LSCPResultSet result;
1109     try {
1110     String s;
1111     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1112     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1113     for (; iter != devices.end(); iter++) {
1114     if (s != "") s += ",";
1115     s += ToString(iter->first);
1116     }
1117     result.Add(s);
1118     }
1119 schoenebeck 880 catch (Exception e) {
1120 schoenebeck 123 result.Error(e);
1121     }
1122     return result.Produce();
1123     }
1124    
1125 senkov 155 String LSCPServer::GetMidiInputDevices() {
1126     dmsg(2,("LSCPServer: GetMidiInputDevices()\n"));
1127     LSCPResultSet result;
1128     try {
1129     String s;
1130     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1131     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1132     for (; iter != devices.end(); iter++) {
1133     if (s != "") s += ",";
1134     s += ToString(iter->first);
1135     }
1136     result.Add(s);
1137     }
1138 schoenebeck 880 catch (Exception e) {
1139 senkov 155 result.Error(e);
1140     }
1141     return result.Produce();
1142     }
1143    
1144 schoenebeck 123 String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
1145     dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
1146     LSCPResultSet result;
1147     try {
1148     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1149 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1150 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
1151 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
1152 schoenebeck 123 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1153     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1154     for (; iter != parameters.end(); iter++) {
1155     result.Add(iter->first, iter->second->Value());
1156     }
1157     }
1158 schoenebeck 880 catch (Exception e) {
1159 schoenebeck 123 result.Error(e);
1160     }
1161     return result.Produce();
1162     }
1163    
1164 senkov 155 String LSCPServer::GetMidiInputDeviceInfo(uint DeviceIndex) {
1165     dmsg(2,("LSCPServer: GetMidiInputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
1166     LSCPResultSet result;
1167     try {
1168     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1169 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1170 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1171 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
1172 senkov 155 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1173     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
1174     for (; iter != parameters.end(); iter++) {
1175     result.Add(iter->first, iter->second->Value());
1176     }
1177     }
1178 schoenebeck 880 catch (Exception e) {
1179 senkov 155 result.Error(e);
1180     }
1181     return result.Produce();
1182     }
1183     String LSCPServer::GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex) {
1184     dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
1185     LSCPResultSet result;
1186     try {
1187 schoenebeck 223 // get MIDI input device
1188 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1189 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1190 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1191 schoenebeck 223
1192     // get MIDI port
1193 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1194 schoenebeck 880 if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1195 schoenebeck 223
1196     // return the values of all MIDI port parameters
1197 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1198     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1199 senkov 155 for (; iter != parameters.end(); iter++) {
1200     result.Add(iter->first, iter->second->Value());
1201     }
1202     }
1203 schoenebeck 880 catch (Exception e) {
1204 senkov 155 result.Error(e);
1205     }
1206     return result.Produce();
1207     }
1208    
1209 schoenebeck 123 String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
1210     dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
1211     LSCPResultSet result;
1212     try {
1213     // get audio output device
1214     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1215 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1216 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1217    
1218     // get audio channel
1219     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1220 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1221 schoenebeck 123
1222     // return the values of all audio channel parameters
1223     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1224     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1225     for (; iter != parameters.end(); iter++) {
1226     result.Add(iter->first, iter->second->Value());
1227     }
1228     }
1229 schoenebeck 880 catch (Exception e) {
1230 schoenebeck 123 result.Error(e);
1231     }
1232     return result.Produce();
1233     }
1234    
1235 senkov 185 String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
1236     dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
1237     LSCPResultSet result;
1238     try {
1239 schoenebeck 223 // get MIDI input device
1240     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1241 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no midi input device with index " + ToString(DeviceId) + ".");
1242 schoenebeck 223 MidiInputDevice* pDevice = devices[DeviceId];
1243 senkov 185
1244 schoenebeck 221 // get midi port
1245     MidiInputPort* pPort = pDevice->GetPort(PortId);
1246 schoenebeck 880 if (!pPort) throw Exception("Midi input device does not have port " + ToString(PortId) + ".");
1247 senkov 185
1248 schoenebeck 223 // get desired port parameter
1249     std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1250 schoenebeck 880 if (!parameters.count(ParameterName)) throw Exception("Midi port does not provide a parameter '" + ParameterName + "'.");
1251 schoenebeck 223 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1252 schoenebeck 203
1253 senkov 185 // return all fields of this audio channel parameter
1254     result.Add("TYPE", pParameter->Type());
1255     result.Add("DESCRIPTION", pParameter->Description());
1256     result.Add("FIX", pParameter->Fix());
1257     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1258 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1259     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1260     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1261 senkov 185 }
1262 schoenebeck 880 catch (Exception e) {
1263 senkov 185 result.Error(e);
1264     }
1265     return result.Produce();
1266     }
1267    
1268 schoenebeck 123 String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
1269     dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
1270     LSCPResultSet result;
1271     try {
1272     // get audio output device
1273     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1274 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1275 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1276    
1277     // get audio channel
1278     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1279 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1280 schoenebeck 123
1281     // get desired audio channel parameter
1282     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1283 schoenebeck 880 if (!parameters.count(ParameterName)) throw Exception("Audio channel does not provide a parameter '" + ParameterName + "'.");
1284 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1285    
1286     // return all fields of this audio channel parameter
1287     result.Add("TYPE", pParameter->Type());
1288     result.Add("DESCRIPTION", pParameter->Description());
1289     result.Add("FIX", pParameter->Fix());
1290     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1291 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1292     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1293     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1294 schoenebeck 123 }
1295 schoenebeck 880 catch (Exception e) {
1296 schoenebeck 123 result.Error(e);
1297     }
1298     return result.Produce();
1299     }
1300    
1301     String LSCPServer::SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal) {
1302     dmsg(2,("LSCPServer: SetAudioOutputChannelParameter(DeviceId=%d,ChannelId=%d,ParamKey=%s,ParamVal=%s)\n",DeviceId,ChannelId,ParamKey.c_str(),ParamVal.c_str()));
1303     LSCPResultSet result;
1304     try {
1305     // get audio output device
1306     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1307 schoenebeck 880 if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1308 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1309    
1310     // get audio channel
1311     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1312 schoenebeck 880 if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1313 schoenebeck 123
1314     // get desired audio channel parameter
1315     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1316 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("Audio channel does not provide a parameter '" + ParamKey + "'.");
1317 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1318    
1319     // set new channel parameter value
1320     pParameter->SetValue(ParamVal);
1321 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceId));
1322 schoenebeck 123 }
1323 schoenebeck 880 catch (Exception e) {
1324 schoenebeck 123 result.Error(e);
1325     }
1326     return result.Produce();
1327     }
1328    
1329     String LSCPServer::SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1330     dmsg(2,("LSCPServer: SetAudioOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1331     LSCPResultSet result;
1332     try {
1333     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1334 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1335 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
1336     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1337 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1338 schoenebeck 123 parameters[ParamKey]->SetValue(ParamVal);
1339 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceIndex));
1340 schoenebeck 123 }
1341 schoenebeck 880 catch (Exception e) {
1342 schoenebeck 123 result.Error(e);
1343     }
1344     return result.Produce();
1345     }
1346    
1347 senkov 155 String LSCPServer::SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1348     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1349     LSCPResultSet result;
1350     try {
1351     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1352 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1353 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1354     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1355 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1356 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1357 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1358 senkov 155 }
1359 schoenebeck 880 catch (Exception e) {
1360 senkov 155 result.Error(e);
1361     }
1362     return result.Produce();
1363     }
1364    
1365     String LSCPServer::SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal) {
1366     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1367     LSCPResultSet result;
1368     try {
1369 schoenebeck 223 // get MIDI input device
1370 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1371 schoenebeck 880 if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1372 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1373 schoenebeck 223
1374     // get MIDI port
1375 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1376 schoenebeck 880 if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1377 schoenebeck 223
1378     // set port parameter value
1379 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1380 schoenebeck 880 if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1381 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1382 iliev 981 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1383 senkov 155 }
1384 schoenebeck 880 catch (Exception e) {
1385 senkov 155 result.Error(e);
1386     }
1387     return result.Produce();
1388     }
1389    
1390 schoenebeck 35 /**
1391     * Will be called by the parser to change the audio output channel for
1392     * playback on a particular sampler channel.
1393     */
1394 schoenebeck 123 String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1395     dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));
1396 schoenebeck 225 LSCPResultSet result;
1397     try {
1398     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1399 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1400 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1401 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1402     if (!pSamplerChannel->GetAudioOutputDevice()) throw Exception("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1403 schoenebeck 411 pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1404 schoenebeck 225 }
1405 schoenebeck 880 catch (Exception e) {
1406 schoenebeck 225 result.Error(e);
1407     }
1408     return result.Produce();
1409 schoenebeck 35 }
1410    
1411 capela 159 String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1412     dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1413     LSCPResultSet result;
1414 persson 841 LockRTNotify();
1415 capela 159 try {
1416     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1417 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1418 capela 159 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1419 schoenebeck 880 if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));
1420 capela 159 AudioOutputDevice* pDevice = devices[AudioDeviceId];
1421     pSamplerChannel->SetAudioOutputDevice(pDevice);
1422     }
1423 schoenebeck 880 catch (Exception e) {
1424 capela 159 result.Error(e);
1425     }
1426 persson 841 UnlockRTNotify();
1427 capela 159 return result.Produce();
1428     }
1429    
1430 capela 143 String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1431     dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1432     LSCPResultSet result;
1433 persson 841 LockRTNotify();
1434 capela 143 try {
1435     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1436 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1437 capela 143 // Driver type name aliasing...
1438 schoenebeck 226 if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1439     if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1440 capela 143 // Check if there's one audio output device already created
1441     // for the intended audio driver type (AudioOutputDriver)...
1442     AudioOutputDevice *pDevice = NULL;
1443     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1444     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1445     for (; iter != devices.end(); iter++) {
1446     if ((iter->second)->Driver() == AudioOutputDriver) {
1447     pDevice = iter->second;
1448     break;
1449     }
1450     }
1451     // If it doesn't exist, create a new one with default parameters...
1452     if (pDevice == NULL) {
1453     std::map<String,String> params;
1454     pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1455     }
1456     // Must have a device...
1457     if (pDevice == NULL)
1458 schoenebeck 880 throw Exception("Internal error: could not create audio output device.");
1459 capela 143 // Set it as the current channel device...
1460     pSamplerChannel->SetAudioOutputDevice(pDevice);
1461     }
1462 schoenebeck 880 catch (Exception e) {
1463 capela 143 result.Error(e);
1464     }
1465 persson 841 UnlockRTNotify();
1466 capela 143 return result.Produce();
1467     }
1468    
1469 capela 159 String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1470     dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel));
1471 senkov 120 LSCPResultSet result;
1472 schoenebeck 53 try {
1473     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1474 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1475 capela 159 pSamplerChannel->SetMidiInputPort(MIDIPort);
1476 schoenebeck 53 }
1477 schoenebeck 880 catch (Exception e) {
1478 senkov 120 result.Error(e);
1479 schoenebeck 53 }
1480 senkov 120 return result.Produce();
1481 schoenebeck 53 }
1482    
1483 capela 159 String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1484     dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
1485 senkov 120 LSCPResultSet result;
1486 senkov 68 try {
1487     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1488 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1489 schoenebeck 675 pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1490 senkov 68 }
1491 schoenebeck 880 catch (Exception e) {
1492 senkov 120 result.Error(e);
1493 senkov 68 }
1494 senkov 120 return result.Produce();
1495 schoenebeck 35 }
1496    
1497 capela 159 String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1498     dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1499 schoenebeck 123 LSCPResultSet result;
1500     try {
1501 capela 159 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1502 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1503 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1504 schoenebeck 880 if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1505 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1506     pSamplerChannel->SetMidiInputDevice(pDevice);
1507 schoenebeck 123 }
1508 schoenebeck 880 catch (Exception e) {
1509 schoenebeck 123 result.Error(e);
1510     }
1511     return result.Produce();
1512     }
1513    
1514 capela 159 String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1515     dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1516     LSCPResultSet result;
1517     try {
1518     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1519 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1520 capela 159 // Driver type name aliasing...
1521 schoenebeck 226 if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1522 capela 159 // Check if there's one MIDI input device already created
1523     // for the intended MIDI driver type (MidiInputDriver)...
1524     MidiInputDevice *pDevice = NULL;
1525     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1526     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1527     for (; iter != devices.end(); iter++) {
1528     if ((iter->second)->Driver() == MidiInputDriver) {
1529     pDevice = iter->second;
1530     break;
1531     }
1532     }
1533     // If it doesn't exist, create a new one with default parameters...
1534     if (pDevice == NULL) {
1535     std::map<String,String> params;
1536     pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1537     // Make it with at least one initial port.
1538     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1539 schoenebeck 221 parameters["PORTS"]->SetValue("1");
1540 capela 159 }
1541     // Must have a device...
1542     if (pDevice == NULL)
1543 schoenebeck 880 throw Exception("Internal error: could not create MIDI input device.");
1544 capela 159 // Set it as the current channel device...
1545     pSamplerChannel->SetMidiInputDevice(pDevice);
1546     }
1547 schoenebeck 880 catch (Exception e) {
1548 capela 159 result.Error(e);
1549     }
1550     return result.Produce();
1551     }
1552    
1553 schoenebeck 35 /**
1554 capela 159 * Will be called by the parser to change the MIDI input device, port and channel on which
1555     * engine of a particular sampler channel should listen to.
1556     */
1557     String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1558     dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1559     LSCPResultSet result;
1560     try {
1561     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1562 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1563 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1564 schoenebeck 880 if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1565 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1566 schoenebeck 675 pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
1567 capela 159 }
1568 schoenebeck 880 catch (Exception e) {
1569 capela 159 result.Error(e);
1570     }
1571     return result.Produce();
1572     }
1573    
1574     /**
1575 schoenebeck 35 * Will be called by the parser to change the global volume factor on a
1576     * particular sampler channel.
1577     */
1578 schoenebeck 225 String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1579     dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1580 senkov 120 LSCPResultSet result;
1581 schoenebeck 53 try {
1582     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1583 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1584 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1585 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1586 schoenebeck 411 pEngineChannel->Volume(dVolume);
1587 schoenebeck 53 }
1588 schoenebeck 880 catch (Exception e) {
1589 senkov 120 result.Error(e);
1590 schoenebeck 53 }
1591 senkov 120 return result.Produce();
1592 schoenebeck 35 }
1593    
1594     /**
1595 schoenebeck 705 * Will be called by the parser to mute/unmute particular sampler channel.
1596     */
1597     String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1598     dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1599     LSCPResultSet result;
1600     try {
1601     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1602 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1603 schoenebeck 705
1604     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1605 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1606 schoenebeck 705
1607     if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1608     else pEngineChannel->SetMute(1);
1609 schoenebeck 880 } catch (Exception e) {
1610 schoenebeck 705 result.Error(e);
1611     }
1612     return result.Produce();
1613     }
1614    
1615     /**
1616     * Will be called by the parser to solo particular sampler channel.
1617     */
1618     String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1619     dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1620     LSCPResultSet result;
1621     try {
1622     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1623 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1624 schoenebeck 705
1625     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1626 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1627 schoenebeck 705
1628     bool oldSolo = pEngineChannel->GetSolo();
1629     bool hadSoloChannel = HasSoloChannel();
1630 schoenebeck 1009
1631 schoenebeck 705 pEngineChannel->SetSolo(bSolo);
1632 schoenebeck 1009
1633 schoenebeck 705 if(!oldSolo && bSolo) {
1634     if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1635     if(!hadSoloChannel) MuteNonSoloChannels();
1636     }
1637 schoenebeck 1009
1638 schoenebeck 705 if(oldSolo && !bSolo) {
1639     if(!HasSoloChannel()) UnmuteChannels();
1640     else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1641     }
1642 schoenebeck 880 } catch (Exception e) {
1643 schoenebeck 705 result.Error(e);
1644     }
1645     return result.Produce();
1646     }
1647    
1648     /**
1649     * Determines whether there is at least one solo channel in the channel list.
1650     *
1651     * @returns true if there is at least one solo channel in the channel list,
1652     * false otherwise.
1653     */
1654     bool LSCPServer::HasSoloChannel() {
1655     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1656     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1657     for (; iter != channels.end(); iter++) {
1658     EngineChannel* c = iter->second->GetEngineChannel();
1659     if(c && c->GetSolo()) return true;
1660     }
1661    
1662     return false;
1663     }
1664    
1665     /**
1666     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1667     * with -1 which indicates that they are muted because of the presence
1668     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1669     * when there are no solo channels left.
1670     */
1671     void LSCPServer::MuteNonSoloChannels() {
1672     dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1673     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1674     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1675     for (; iter != channels.end(); iter++) {
1676     EngineChannel* c = iter->second->GetEngineChannel();
1677     if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1678     }
1679     }
1680    
1681     /**
1682     * Unmutes all channels that are muted because of the presence
1683     * of a solo channel(s).
1684     */
1685     void LSCPServer::UnmuteChannels() {
1686     dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1687     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1688     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1689     for (; iter != channels.end(); iter++) {
1690     EngineChannel* c = iter->second->GetEngineChannel();
1691     if(c && c->GetMute() == -1) c->SetMute(0);
1692     }
1693     }
1694    
1695 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) {
1696 schoenebeck 947 dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1697    
1698     midi_prog_index_t idx;
1699 schoenebeck 973 idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1700     idx.midi_bank_lsb = MidiBank & 0x7f;
1701 schoenebeck 947 idx.midi_prog = MidiProg;
1702    
1703     MidiInstrumentMapper::entry_t entry;
1704     entry.EngineName = EngineType;
1705     entry.InstrumentFile = InstrumentFile;
1706     entry.InstrumentIndex = InstrumentIndex;
1707     entry.LoadMode = LoadMode;
1708     entry.Volume = Volume;
1709     entry.Name = Name;
1710    
1711     LSCPResultSet result;
1712     try {
1713 schoenebeck 1047 // PERSISTENT mapping commands might block for a long time, so in
1714     // that case we add/replace the mapping in another thread in case
1715     // the NON_MODAL argument was supplied, non persistent mappings
1716     // should return immediately, so we don't need to do that for them
1717     bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
1718 schoenebeck 973 MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
1719 schoenebeck 947 } catch (Exception e) {
1720     result.Error(e);
1721     }
1722     return result.Produce();
1723     }
1724    
1725 schoenebeck 973 String LSCPServer::RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
1726 schoenebeck 947 dmsg(2,("LSCPServer: RemoveMIDIInstrumentMapping()\n"));
1727    
1728     midi_prog_index_t idx;
1729 schoenebeck 973 idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1730     idx.midi_bank_lsb = MidiBank & 0x7f;
1731 schoenebeck 947 idx.midi_prog = MidiProg;
1732    
1733     LSCPResultSet result;
1734     try {
1735 schoenebeck 973 MidiInstrumentMapper::RemoveEntry(MidiMapID, idx);
1736 schoenebeck 947 } catch (Exception e) {
1737     result.Error(e);
1738     }
1739     return result.Produce();
1740     }
1741    
1742 schoenebeck 973 String LSCPServer::GetMidiInstrumentMappings(uint MidiMapID) {
1743     dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
1744 schoenebeck 947 LSCPResultSet result;
1745 schoenebeck 973 try {
1746     result.Add(MidiInstrumentMapper::Entries(MidiMapID).size());
1747     } catch (Exception e) {
1748     result.Error(e);
1749     }
1750 schoenebeck 947 return result.Produce();
1751     }
1752    
1753 schoenebeck 973
1754     String LSCPServer::GetAllMidiInstrumentMappings() {
1755     dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
1756     LSCPResultSet result;
1757     std::vector<int> maps = MidiInstrumentMapper::Maps();
1758     int totalMappings = 0;
1759     for (int i = 0; i < maps.size(); i++) {
1760     try {
1761     totalMappings += MidiInstrumentMapper::Entries(maps[i]).size();
1762     } catch (Exception e) { /*NOOP*/ }
1763     }
1764     result.Add(totalMappings);
1765     return result.Produce();
1766     }
1767    
1768     String LSCPServer::GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
1769 schoenebeck 947 dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
1770     LSCPResultSet result;
1771     try {
1772     midi_prog_index_t idx;
1773 schoenebeck 973 idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1774     idx.midi_bank_lsb = MidiBank & 0x7f;
1775 schoenebeck 947 idx.midi_prog = MidiProg;
1776    
1777 schoenebeck 973 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
1778 schoenebeck 947 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);
1779     if (iter == mappings.end()) result.Error("there is no map entry with that index");
1780     else { // found
1781     result.Add("NAME", iter->second.Name);
1782     result.Add("ENGINE_NAME", iter->second.EngineName);
1783     result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile);
1784     result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);
1785     String instrumentName;
1786     Engine* pEngine = EngineFactory::Create(iter->second.EngineName);
1787     if (pEngine) {
1788     if (pEngine->GetInstrumentManager()) {
1789     InstrumentManager::instrument_id_t instrID;
1790     instrID.FileName = iter->second.InstrumentFile;
1791     instrID.Index = iter->second.InstrumentIndex;
1792     instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
1793     }
1794     EngineFactory::Destroy(pEngine);
1795     }
1796     result.Add("INSTRUMENT_NAME", instrumentName);
1797     switch (iter->second.LoadMode) {
1798     case MidiInstrumentMapper::ON_DEMAND:
1799     result.Add("LOAD_MODE", "ON_DEMAND");
1800     break;
1801     case MidiInstrumentMapper::ON_DEMAND_HOLD:
1802     result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
1803     break;
1804     case MidiInstrumentMapper::PERSISTENT:
1805     result.Add("LOAD_MODE", "PERSISTENT");
1806     break;
1807     default:
1808     throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
1809     }
1810     result.Add("VOLUME", iter->second.Volume);
1811     }
1812     } catch (Exception e) {
1813     result.Error(e);
1814     }
1815     return result.Produce();
1816     }
1817    
1818 schoenebeck 973 String LSCPServer::ListMidiInstrumentMappings(uint MidiMapID) {
1819     dmsg(2,("LSCPServer: ListMidiInstrumentMappings()\n"));
1820 schoenebeck 947 LSCPResultSet result;
1821     try {
1822     String s;
1823 schoenebeck 973 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
1824 schoenebeck 947 std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
1825     for (; iter != mappings.end(); iter++) {
1826     if (s.size()) s += ",";
1827 schoenebeck 973 s += "{" + ToString(MidiMapID) + ","
1828 schoenebeck 1007 + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
1829 schoenebeck 973 + ToString(int(iter->first.midi_prog)) + "}";
1830 schoenebeck 947 }
1831     result.Add(s);
1832     } catch (Exception e) {
1833     result.Error(e);
1834     }
1835     return result.Produce();
1836     }
1837    
1838 schoenebeck 973 String LSCPServer::ListAllMidiInstrumentMappings() {
1839     dmsg(2,("LSCPServer: ListAllMidiInstrumentMappings()\n"));
1840     LSCPResultSet result;
1841     try {
1842     std::vector<int> maps = MidiInstrumentMapper::Maps();
1843     String s;
1844     for (int i = 0; i < maps.size(); i++) {
1845     std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(maps[i]);
1846     std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
1847     for (; iter != mappings.end(); iter++) {
1848     if (s.size()) s += ",";
1849     s += "{" + ToString(maps[i]) + ","
1850 schoenebeck 1009 + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
1851 schoenebeck 973 + ToString(int(iter->first.midi_prog)) + "}";
1852     }
1853     }
1854     result.Add(s);
1855     } catch (Exception e) {
1856     result.Error(e);
1857     }
1858     return result.Produce();
1859     }
1860    
1861     String LSCPServer::ClearMidiInstrumentMappings(uint MidiMapID) {
1862 schoenebeck 947 dmsg(2,("LSCPServer: ClearMidiInstrumentMappings()\n"));
1863     LSCPResultSet result;
1864     try {
1865 schoenebeck 973 MidiInstrumentMapper::RemoveAllEntries(MidiMapID);
1866 schoenebeck 947 } catch (Exception e) {
1867     result.Error(e);
1868     }
1869     return result.Produce();
1870     }
1871    
1872 schoenebeck 973 String LSCPServer::ClearAllMidiInstrumentMappings() {
1873     dmsg(2,("LSCPServer: ClearAllMidiInstrumentMappings()\n"));
1874     LSCPResultSet result;
1875     try {
1876     std::vector<int> maps = MidiInstrumentMapper::Maps();
1877     for (int i = 0; i < maps.size(); i++)
1878     MidiInstrumentMapper::RemoveAllEntries(maps[i]);
1879     } catch (Exception e) {
1880     result.Error(e);
1881     }
1882     return result.Produce();
1883     }
1884    
1885     String LSCPServer::AddMidiInstrumentMap(String MapName) {
1886     dmsg(2,("LSCPServer: AddMidiInstrumentMap()\n"));
1887     LSCPResultSet result;
1888     try {
1889     int MapID = MidiInstrumentMapper::AddMap(MapName);
1890     result = LSCPResultSet(MapID);
1891     } catch (Exception e) {
1892     result.Error(e);
1893     }
1894     return result.Produce();
1895     }
1896    
1897     String LSCPServer::RemoveMidiInstrumentMap(uint MidiMapID) {
1898     dmsg(2,("LSCPServer: RemoveMidiInstrumentMap()\n"));
1899     LSCPResultSet result;
1900     try {
1901     MidiInstrumentMapper::RemoveMap(MidiMapID);
1902     } catch (Exception e) {
1903     result.Error(e);
1904     }
1905     return result.Produce();
1906     }
1907    
1908     String LSCPServer::RemoveAllMidiInstrumentMaps() {
1909     dmsg(2,("LSCPServer: RemoveAllMidiInstrumentMaps()\n"));
1910     LSCPResultSet result;
1911     try {
1912     MidiInstrumentMapper::RemoveAllMaps();
1913     } catch (Exception e) {
1914     result.Error(e);
1915     }
1916     return result.Produce();
1917     }
1918    
1919     String LSCPServer::GetMidiInstrumentMaps() {
1920     dmsg(2,("LSCPServer: GetMidiInstrumentMaps()\n"));
1921     LSCPResultSet result;
1922     try {
1923     result.Add(MidiInstrumentMapper::Maps().size());
1924     } catch (Exception e) {
1925     result.Error(e);
1926     }
1927     return result.Produce();
1928     }
1929    
1930     String LSCPServer::ListMidiInstrumentMaps() {
1931     dmsg(2,("LSCPServer: ListMidiInstrumentMaps()\n"));
1932     LSCPResultSet result;
1933     try {
1934     std::vector<int> maps = MidiInstrumentMapper::Maps();
1935     String sList;
1936     for (int i = 0; i < maps.size(); i++) {
1937     if (sList != "") sList += ",";
1938     sList += ToString(maps[i]);
1939     }
1940     result.Add(sList);
1941     } catch (Exception e) {
1942     result.Error(e);
1943     }
1944     return result.Produce();
1945     }
1946    
1947     String LSCPServer::GetMidiInstrumentMap(uint MidiMapID) {
1948     dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
1949     LSCPResultSet result;
1950     try {
1951     result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID));
1952 iliev 1135 result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
1953 schoenebeck 973 } catch (Exception e) {
1954     result.Error(e);
1955     }
1956     return result.Produce();
1957     }
1958    
1959     String LSCPServer::SetMidiInstrumentMapName(uint MidiMapID, String NewName) {
1960     dmsg(2,("LSCPServer: SetMidiInstrumentMapName()\n"));
1961     LSCPResultSet result;
1962     try {
1963     MidiInstrumentMapper::RenameMap(MidiMapID, NewName);
1964     } catch (Exception e) {
1965     result.Error(e);
1966     }
1967     return result.Produce();
1968     }
1969    
1970 schoenebeck 705 /**
1971 schoenebeck 973 * Set the MIDI instrument map the given sampler channel shall use for
1972     * handling MIDI program change messages. There are the following two
1973     * special (negative) values:
1974     *
1975     * - (-1) : set to NONE (ignore program changes)
1976     * - (-2) : set to DEFAULT map
1977     */
1978     String LSCPServer::SetChannelMap(uint uiSamplerChannel, int MidiMapID) {
1979     dmsg(2,("LSCPServer: SetChannelMap()\n"));
1980     LSCPResultSet result;
1981     try {
1982     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1983     if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1984    
1985     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1986     if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
1987    
1988     if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
1989     else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
1990     else pEngineChannel->SetMidiInstrumentMap(MidiMapID);
1991     } catch (Exception e) {
1992     result.Error(e);
1993     }
1994     return result.Produce();
1995     }
1996    
1997 schoenebeck 1001 String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) {
1998     dmsg(2,("LSCPServer: CreateFxSend()\n"));
1999     LSCPResultSet result;
2000     try {
2001 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2002    
2003 schoenebeck 1001 FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2004     if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2005    
2006     result = LSCPResultSet(pFxSend->Id()); // success
2007     } catch (Exception e) {
2008     result.Error(e);
2009     }
2010     return result.Produce();
2011     }
2012    
2013     String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) {
2014     dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2015     LSCPResultSet result;
2016     try {
2017 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2018 schoenebeck 1001
2019     FxSend* pFxSend = NULL;
2020     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2021     if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2022     pFxSend = pEngineChannel->GetFxSend(i);
2023     break;
2024     }
2025     }
2026     if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2027     pEngineChannel->RemoveFxSend(pFxSend);
2028     } catch (Exception e) {
2029     result.Error(e);
2030     }
2031     return result.Produce();
2032     }
2033    
2034     String LSCPServer::GetFxSends(uint uiSamplerChannel) {
2035     dmsg(2,("LSCPServer: GetFxSends()\n"));
2036     LSCPResultSet result;
2037     try {
2038 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2039 schoenebeck 1001
2040     result.Add(pEngineChannel->GetFxSendCount());
2041     } catch (Exception e) {
2042     result.Error(e);
2043     }
2044     return result.Produce();
2045     }
2046    
2047     String LSCPServer::ListFxSends(uint uiSamplerChannel) {
2048     dmsg(2,("LSCPServer: ListFxSends()\n"));
2049     LSCPResultSet result;
2050     String list;
2051     try {
2052 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2053 schoenebeck 1001
2054     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2055     FxSend* pFxSend = pEngineChannel->GetFxSend(i);
2056     if (list != "") list += ",";
2057     list += ToString(pFxSend->Id());
2058     }
2059     result.Add(list);
2060     } catch (Exception e) {
2061     result.Error(e);
2062     }
2063     return result.Produce();
2064     }
2065    
2066 iliev 1135 FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2067     EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2068    
2069     FxSend* pFxSend = NULL;
2070     for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2071     if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2072     pFxSend = pEngineChannel->GetFxSend(i);
2073     break;
2074     }
2075     }
2076     if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2077     return pFxSend;
2078     }
2079    
2080 schoenebeck 1001 String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2081     dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2082     LSCPResultSet result;
2083     try {
2084 iliev 1135 EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2085     FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2086    
2087 schoenebeck 1001 // gather audio routing informations
2088     String AudioRouting;
2089     for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
2090     if (AudioRouting != "") AudioRouting += ",";
2091     AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2092     }
2093    
2094     // success
2095     result.Add("NAME", pFxSend->Name());
2096 schoenebeck 1026 result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2097     result.Add("LEVEL", ToString(pFxSend->Level()));
2098 schoenebeck 1001 result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2099     } catch (Exception e) {
2100     result.Error(e);
2101     }
2102     return result.Produce();
2103     }
2104    
2105 iliev 1135 String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2106     dmsg(2,("LSCPServer: SetFxSendName()\n"));
2107     LSCPResultSet result;
2108     try {
2109     FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2110    
2111     pFxSend->SetName(Name);
2112     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2113     } catch (Exception e) {
2114     result.Error(e);
2115     }
2116     return result.Produce();
2117     }
2118    
2119 schoenebeck 1001 String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2120     dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2121     LSCPResultSet result;
2122     try {
2123 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2124 schoenebeck 1001
2125     pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2126 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2127 schoenebeck 1001 } catch (Exception e) {
2128     result.Error(e);
2129     }
2130     return result.Produce();
2131     }
2132    
2133 schoenebeck 1026 String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2134     dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2135     LSCPResultSet result;
2136     try {
2137 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2138 schoenebeck 1026
2139     pFxSend->SetMidiController(MidiController);
2140 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2141 schoenebeck 1026 } catch (Exception e) {
2142     result.Error(e);
2143     }
2144     return result.Produce();
2145     }
2146    
2147     String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2148     dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2149     LSCPResultSet result;
2150     try {
2151 iliev 1135 FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2152 schoenebeck 1026
2153     pFxSend->SetLevel((float)dLevel);
2154 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2155 schoenebeck 1026 } catch (Exception e) {
2156     result.Error(e);
2157     }
2158     return result.Produce();
2159     }
2160    
2161 schoenebeck 973 /**
2162 schoenebeck 35 * Will be called by the parser to reset a particular sampler channel.
2163     */
2164 schoenebeck 53 String LSCPServer::ResetChannel(uint uiSamplerChannel) {
2165     dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
2166 senkov 120 LSCPResultSet result;
2167 schoenebeck 53 try {
2168     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2169 schoenebeck 880 if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2170 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2171 schoenebeck 880 if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
2172 schoenebeck 670 pEngineChannel->Reset();
2173 schoenebeck 53 }
2174 schoenebeck 880 catch (Exception e) {
2175 senkov 120 result.Error(e);
2176 schoenebeck 53 }
2177 senkov 120 return result.Produce();
2178 schoenebeck 35 }
2179    
2180     /**
2181 schoenebeck 212 * Will be called by the parser to reset the whole sampler.
2182     */
2183     String LSCPServer::ResetSampler() {
2184     dmsg(2,("LSCPServer: ResetSampler()\n"));
2185     pSampler->Reset();
2186     LSCPResultSet result;
2187     return result.Produce();
2188     }
2189    
2190     /**
2191 schoenebeck 563 * Will be called by the parser to return general informations about this
2192     * sampler.
2193     */
2194     String LSCPServer::GetServerInfo() {
2195     dmsg(2,("LSCPServer: GetServerInfo()\n"));
2196     LSCPResultSet result;
2197     result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");
2198 schoenebeck 570 result.Add("VERSION", VERSION);
2199 schoenebeck 947 result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2200 iliev 1161 #if HAVE_SQLITE3
2201     result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2202     #else
2203     result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2204     #endif
2205    
2206 schoenebeck 563 return result.Produce();
2207     }
2208    
2209     /**
2210 iliev 778 * Will be called by the parser to return the current number of all active voices.
2211     */
2212     String LSCPServer::GetTotalVoiceCount() {
2213     dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
2214     LSCPResultSet result;
2215     result.Add(pSampler->GetVoiceCount());
2216     return result.Produce();
2217     }
2218    
2219     /**
2220     * Will be called by the parser to return the maximum number of voices.
2221     */
2222     String LSCPServer::GetTotalVoiceCountMax() {
2223     dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
2224     LSCPResultSet result;
2225     result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);
2226     return result.Produce();
2227     }
2228    
2229 schoenebeck 1005 String LSCPServer::GetGlobalVolume() {
2230     LSCPResultSet result;
2231     result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp
2232     return result.Produce();
2233     }
2234    
2235     String LSCPServer::SetGlobalVolume(double dVolume) {
2236     LSCPResultSet result;
2237     try {
2238     if (dVolume < 0) throw Exception("Volume may not be negative");
2239     GLOBAL_VOLUME = dVolume; // see common/global.cpp
2240 iliev 1108 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
2241 schoenebeck 1005 } catch (Exception e) {
2242     result.Error(e);
2243     }
2244     return result.Produce();
2245     }
2246    
2247 iliev 778 /**
2248 schoenebeck 35 * Will be called by the parser to subscribe a client (frontend) on the
2249     * server for receiving event messages.
2250     */
2251 senkov 170 String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
2252     dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
2253     LSCPResultSet result;
2254     SubscriptionMutex.Lock();
2255     eventSubscriptions[type].push_back(currentSocket);
2256     SubscriptionMutex.Unlock();
2257     return result.Produce();
2258 schoenebeck 35 }
2259    
2260     /**
2261     * Will be called by the parser to unsubscribe a client on the server
2262     * for not receiving further event messages.
2263     */
2264 senkov 170 String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
2265     dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
2266     LSCPResultSet result;
2267     SubscriptionMutex.Lock();
2268     eventSubscriptions[type].remove(currentSocket);
2269     SubscriptionMutex.Unlock();
2270     return result.Produce();
2271 schoenebeck 35 }
2272 capela 133
2273 iliev 1161 String LSCPServer::AddDbInstrumentDirectory(String Dir) {
2274     dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
2275     LSCPResultSet result;
2276     #if HAVE_SQLITE3
2277     try {
2278     InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
2279     } catch (Exception e) {
2280     result.Error(e);
2281     }
2282     #else
2283     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2284     #endif
2285     return result.Produce();
2286 senkov 397 }
2287    
2288 iliev 1161 String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
2289     dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
2290 senkov 397 LSCPResultSet result;
2291 schoenebeck 401 #if HAVE_SQLITE3
2292 iliev 1161 try {
2293     InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
2294     } catch (Exception e) {
2295     result.Error(e);
2296     }
2297     #else
2298     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2299     #endif
2300     return result.Produce();
2301     }
2302 senkov 397
2303 iliev 1187 String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
2304     dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2305 iliev 1161 LSCPResultSet result;
2306     #if HAVE_SQLITE3
2307     try {
2308 iliev 1187 result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
2309 iliev 1161 } catch (Exception e) {
2310     result.Error(e);
2311 senkov 397 }
2312 iliev 1161 #else
2313     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2314     #endif
2315     return result.Produce();
2316     }
2317    
2318 iliev 1187 String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
2319     dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2320 iliev 1161 LSCPResultSet result;
2321     #if HAVE_SQLITE3
2322     try {
2323     String list;
2324 iliev 1187 StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
2325 iliev 1161
2326     for (int i = 0; i < dirs->size(); i++) {
2327     if (list != "") list += ",";
2328     list += "'" + dirs->at(i) + "'";
2329     }
2330    
2331     result.Add(list);
2332     } catch (Exception e) {
2333     result.Error(e);
2334 senkov 397 }
2335     #else
2336 iliev 1161 result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2337 senkov 397 #endif
2338     return result.Produce();
2339     }
2340    
2341 iliev 1161 String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
2342     dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
2343     LSCPResultSet result;
2344     #if HAVE_SQLITE3
2345     try {
2346     DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2347    
2348     result.Add("DESCRIPTION", info.Description);
2349     result.Add("CREATED", info.Created);
2350     result.Add("MODIFIED", info.Modified);
2351     } catch (Exception e) {
2352     result.Error(e);
2353     }
2354     #else
2355     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2356     #endif
2357     return result.Produce();
2358     }
2359    
2360     String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
2361     dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
2362     LSCPResultSet result;
2363     #if HAVE_SQLITE3
2364     try {
2365     InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
2366     } catch (Exception e) {
2367     result.Error(e);
2368     }
2369     #else
2370     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2371     #endif
2372     return result.Produce();
2373     }
2374    
2375     String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
2376     dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2377     LSCPResultSet result;
2378     #if HAVE_SQLITE3
2379     try {
2380     InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
2381     } catch (Exception e) {
2382     result.Error(e);
2383     }
2384     #else
2385     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2386     #endif
2387     return result.Produce();
2388     }
2389    
2390 iliev 1187 String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
2391     dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2392     LSCPResultSet result;
2393     #if HAVE_SQLITE3
2394     try {
2395     InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
2396     } catch (Exception e) {
2397     result.Error(e);
2398     }
2399     #else
2400     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2401     #endif
2402     return result.Produce();
2403     }
2404    
2405 iliev 1161 String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
2406     dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
2407     LSCPResultSet result;
2408     #if HAVE_SQLITE3
2409     try {
2410     InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
2411     } catch (Exception e) {
2412     result.Error(e);
2413     }
2414     #else
2415     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2416     #endif
2417     return result.Produce();
2418     }
2419    
2420 iliev 1200 String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
2421     dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
2422 iliev 1161 LSCPResultSet result;
2423     #if HAVE_SQLITE3
2424     try {
2425 iliev 1200 int id;
2426     InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2427     id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
2428     if (bBackground) result = id;
2429 iliev 1161 } catch (Exception e) {
2430     result.Error(e);
2431     }
2432     #else
2433     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2434     #endif
2435     return result.Produce();
2436     }
2437    
2438 iliev 1200 String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) {
2439     dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground));
2440 iliev 1161 LSCPResultSet result;
2441     #if HAVE_SQLITE3
2442     try {
2443 iliev 1200 int id;
2444     InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2445     if (ScanMode.compare("RECURSIVE") == 0) {
2446     id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground);
2447     } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
2448     id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground);
2449     } else if (ScanMode.compare("FLAT") == 0) {
2450     id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground);
2451     } else {
2452     throw Exception("Unknown scan mode: " + ScanMode);
2453     }
2454    
2455     if (bBackground) result = id;
2456 iliev 1161 } catch (Exception e) {
2457     result.Error(e);
2458     }
2459     #else
2460     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2461     #endif
2462     return result.Produce();
2463     }
2464    
2465     String LSCPServer::RemoveDbInstrument(String Instr) {
2466     dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
2467     LSCPResultSet result;
2468     #if HAVE_SQLITE3
2469     try {
2470     InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
2471     } catch (Exception e) {
2472     result.Error(e);
2473     }
2474     #else
2475     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2476     #endif
2477     return result.Produce();
2478     }
2479    
2480 iliev 1187 String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
2481     dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2482 iliev 1161 LSCPResultSet result;
2483     #if HAVE_SQLITE3
2484     try {
2485 iliev 1187 result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
2486 iliev 1161 } catch (Exception e) {
2487     result.Error(e);
2488     }
2489     #else
2490     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2491     #endif
2492     return result.Produce();
2493     }
2494    
2495 iliev 1187 String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
2496     dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2497 iliev 1161 LSCPResultSet result;
2498     #if HAVE_SQLITE3
2499     try {
2500     String list;
2501 iliev 1187 StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
2502 iliev 1161
2503     for (int i = 0; i < instrs->size(); i++) {
2504     if (list != "") list += ",";
2505     list += "'" + instrs->at(i) + "'";
2506     }
2507    
2508     result.Add(list);
2509     } catch (Exception e) {
2510     result.Error(e);
2511     }
2512     #else
2513     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2514     #endif
2515     return result.Produce();
2516     }
2517    
2518     String LSCPServer::GetDbInstrumentInfo(String Instr) {
2519     dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
2520     LSCPResultSet result;
2521     #if HAVE_SQLITE3
2522     try {
2523     DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
2524    
2525     result.Add("INSTRUMENT_FILE", info.InstrFile);
2526     result.Add("INSTRUMENT_NR", info.InstrNr);
2527     result.Add("FORMAT_FAMILY", info.FormatFamily);
2528     result.Add("FORMAT_VERSION", info.FormatVersion);
2529     result.Add("SIZE", (int)info.Size);
2530     result.Add("CREATED", info.Created);
2531     result.Add("MODIFIED", info.Modified);
2532     result.Add("DESCRIPTION", FilterEndlines(info.Description));
2533     result.Add("IS_DRUM", info.IsDrum);
2534     result.Add("PRODUCT", FilterEndlines(info.Product));
2535     result.Add("ARTISTS", FilterEndlines(info.Artists));
2536     result.Add("KEYWORDS", FilterEndlines(info.Keywords));
2537     } catch (Exception e) {
2538     result.Error(e);
2539     }
2540     #else
2541     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2542     #endif
2543     return result.Produce();
2544     }
2545    
2546 iliev 1200 String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
2547     dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
2548     LSCPResultSet result;
2549     #if HAVE_SQLITE3
2550     try {
2551     ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
2552    
2553     result.Add("FILES_TOTAL", job.FilesTotal);
2554     result.Add("FILES_SCANNED", job.FilesScanned);
2555     result.Add("SCANNING", job.Scanning);
2556     result.Add("STATUS", job.Status);
2557     } catch (Exception e) {
2558     result.Error(e);
2559     }
2560     #else
2561     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2562     #endif
2563     return result.Produce();
2564     }
2565    
2566 iliev 1161 String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
2567     dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
2568     LSCPResultSet result;
2569     #if HAVE_SQLITE3
2570     try {
2571     InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
2572     } catch (Exception e) {
2573     result.Error(e);
2574     }
2575     #else
2576     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2577     #endif
2578     return result.Produce();
2579     }
2580    
2581     String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
2582     dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
2583     LSCPResultSet result;
2584     #if HAVE_SQLITE3
2585     try {
2586     InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
2587     } catch (Exception e) {
2588     result.Error(e);
2589     }
2590     #else
2591     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2592     #endif
2593     return result.Produce();
2594     }
2595    
2596 iliev 1187 String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
2597     dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
2598     LSCPResultSet result;
2599     #if HAVE_SQLITE3
2600     try {
2601     InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
2602     } catch (Exception e) {
2603     result.Error(e);
2604     }
2605     #else
2606     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2607     #endif
2608     return result.Produce();
2609     }
2610    
2611 iliev 1161 String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
2612     dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
2613     LSCPResultSet result;
2614     #if HAVE_SQLITE3
2615     try {
2616     InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
2617     } catch (Exception e) {
2618     result.Error(e);
2619     }
2620     #else
2621     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2622     #endif
2623     return result.Produce();
2624     }
2625    
2626 iliev 1187 String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
2627     dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
2628     LSCPResultSet result;
2629     #if HAVE_SQLITE3
2630     try {
2631     SearchQuery Query;
2632     std::map<String,String>::iterator iter;
2633     for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
2634     if (iter->first.compare("NAME") == 0) {
2635     Query.Name = iter->second;
2636     } else if (iter->first.compare("CREATED") == 0) {
2637     Query.SetCreated(iter->second);
2638     } else if (iter->first.compare("MODIFIED") == 0) {
2639     Query.SetModified(iter->second);
2640     } else if (iter->first.compare("DESCRIPTION") == 0) {
2641     Query.Description = iter->second;
2642     } else {
2643     throw Exception("Unknown search criteria: " + iter->first);
2644     }
2645     }
2646 iliev 1161
2647 iliev 1187 String list;
2648     StringListPtr pDirectories =
2649     InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
2650    
2651     for (int i = 0; i < pDirectories->size(); i++) {
2652     if (list != "") list += ",";
2653     list += "'" + pDirectories->at(i) + "'";
2654     }
2655    
2656     result.Add(list);
2657     } catch (Exception e) {
2658     result.Error(e);
2659     }
2660     #else
2661     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2662     #endif
2663     return result.Produce();
2664     }
2665    
2666     String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
2667     dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
2668     LSCPResultSet result;
2669     #if HAVE_SQLITE3
2670     try {
2671     SearchQuery Query;
2672     std::map<String,String>::iterator iter;
2673     for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
2674     if (iter->first.compare("NAME") == 0) {
2675     Query.Name = iter->second;
2676     } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
2677     Query.SetFormatFamilies(iter->second);
2678     } else if (iter->first.compare("SIZE") == 0) {
2679     Query.SetSize(iter->second);
2680     } else if (iter->first.compare("CREATED") == 0) {
2681     Query.SetCreated(iter->second);
2682     } else if (iter->first.compare("MODIFIED") == 0) {
2683     Query.SetModified(iter->second);
2684     } else if (iter->first.compare("DESCRIPTION") == 0) {
2685     Query.Description = iter->second;
2686     } else if (iter->first.compare("IS_DRUM") == 0) {
2687     if (!strcasecmp(iter->second.c_str(), "true")) {
2688     Query.InstrType = SearchQuery::DRUM;
2689     } else {
2690     Query.InstrType = SearchQuery::CHROMATIC;
2691     }
2692     } else if (iter->first.compare("PRODUCT") == 0) {
2693     Query.Product = iter->second;
2694     } else if (iter->first.compare("ARTISTS") == 0) {
2695     Query.Artists = iter->second;
2696     } else if (iter->first.compare("KEYWORDS") == 0) {
2697     Query.Keywords = iter->second;
2698     } else {
2699     throw Exception("Unknown search criteria: " + iter->first);
2700     }
2701     }
2702    
2703     String list;
2704     StringListPtr pInstruments =
2705     InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
2706    
2707     for (int i = 0; i < pInstruments->size(); i++) {
2708     if (list != "") list += ",";
2709     list += "'" + pInstruments->at(i) + "'";
2710     }
2711    
2712     result.Add(list);
2713     } catch (Exception e) {
2714     result.Error(e);
2715     }
2716     #else
2717     result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2718     #endif
2719     return result.Produce();
2720     }
2721    
2722    
2723 schoenebeck 210 /**
2724     * Will be called by the parser to enable or disable echo mode; if echo
2725     * mode is enabled, all commands from the client will (immediately) be
2726     * echoed back to the client.
2727     */
2728     String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
2729     dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
2730     LSCPResultSet result;
2731     try {
2732     if (boolean_value == 0) pSession->bVerbose = false;
2733     else if (boolean_value == 1) pSession->bVerbose = true;
2734 schoenebeck 880 else throw Exception("Not a boolean value, must either be 0 or 1");
2735 schoenebeck 210 }
2736 schoenebeck 880 catch (Exception e) {
2737 schoenebeck 210 result.Error(e);
2738     }
2739     return result.Produce();
2740     }
2741 iliev 1161
2742     String LSCPServer::FilterEndlines(String s) {
2743     String s2 = s;
2744     for (int i = 0; i < s2.length(); i++) {
2745     if (s2.at(i) == '\r') s2.at(i) = ' ';
2746     else if (s2.at(i) == '\n') s2.at(i) = ' ';
2747     }
2748    
2749     return s2;
2750     }

  ViewVC Help
Powered by ViewVC