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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 981 by iliev, Sun Dec 17 22:35:01 2006 UTC revision 1882 by iliev, Wed Apr 1 15:21:13 2009 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005, 2006 Christian Schoenebeck                        *   *   Copyright (C) 2005 - 2009 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 21  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24    #include <algorithm>
25    #include <string>
26    
27    #include "../common/File.h"
28  #include "lscpserver.h"  #include "lscpserver.h"
29  #include "lscpresultset.h"  #include "lscpresultset.h"
30  #include "lscpevent.h"  #include "lscpevent.h"
 //#include "../common/global.h"  
31    
32    #if defined(WIN32)
33    #include <windows.h>
34    #else
35  #include <fcntl.h>  #include <fcntl.h>
36    #endif
37    
38  #if HAVE_SQLITE3  #if ! HAVE_SQLITE3
39  # include "sqlite3.h"  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
40  #endif  #endif
41    
42  #include "../engines/EngineFactory.h"  #include "../engines/EngineFactory.h"
# Line 37  Line 44 
44  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
45  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
46    
47    namespace LinuxSampler {
48    
49    /**
50     * Returns a copy of the given string where all special characters are
51     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
52     * to escape LSCP response fields in case the respective response field is
53     * actually defined as using escape sequences in the LSCP specs.
54     *
55     * @e Caution: DO NOT use this function for escaping path based responses,
56     * use the Path class (src/common/Path.h) for this instead!
57     */
58    static String _escapeLscpResponse(String txt) {
59        for (int i = 0; i < txt.length(); i++) {
60            const char c = txt.c_str()[i];
61            if (
62                !(c >= '0' && c <= '9') &&
63                !(c >= 'a' && c <= 'z') &&
64                !(c >= 'A' && c <= 'Z') &&
65                !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
66                !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
67                !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
68                !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
69                !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
70                !(c == '@') && !(c == '[') && !(c == ']') &&
71                !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
72                !(c == '|') && !(c == '}') && !(c == '~')
73            ) {
74                // convert the "special" character into a "\xHH" LSCP escape sequence
75                char buf[5];
76                snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
77                txt.replace(i, 1, buf);
78                i += 3;
79            }
80        }
81        return txt;
82    }
83    
84  /**  /**
85   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
86   * The big assumption here is that LSCPServer is going to remain a singleton.   * The big assumption here is that LSCPServer is going to remain a singleton.
# Line 53  Line 97 
97  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
98  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
99  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
100    std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
101  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
102  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
103  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
# Line 61  Mutex LSCPServer::NotifyBufferMutex = Mu Line 106  Mutex LSCPServer::NotifyBufferMutex = Mu
106  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
107  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
108    
109  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
110      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
111      SocketAddress.sin_addr.s_addr = addr;      SocketAddress.sin_addr.s_addr = addr;
112      SocketAddress.sin_port        = port;      SocketAddress.sin_port        = port;
# Line 75  LSCPServer::LSCPServer(Sampler* pSampler Line 120  LSCPServer::LSCPServer(Sampler* pSampler
120      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
121      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
122      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
123        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
124        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
125      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
126      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
127      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
128      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
129        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
130        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
131        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
132        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
133        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
134      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
135        LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
136      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
137        LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
138        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
139        LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
140      hSocket = -1;      hSocket = -1;
141  }  }
142    
143  LSCPServer::~LSCPServer() {  LSCPServer::~LSCPServer() {
144        CloseAllConnections();
145        InstrumentManager::StopBackgroundThread();
146    #if defined(WIN32)
147        if (hSocket >= 0) closesocket(hSocket);
148    #else
149      if (hSocket >= 0) close(hSocket);      if (hSocket >= 0) close(hSocket);
150    #endif
151    }
152    
153    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
154        this->pParent = pParent;
155    }
156    
157    LSCPServer::EventHandler::~EventHandler() {
158        std::vector<midi_listener_entry> l = channelMidiListeners;
159        channelMidiListeners.clear();
160        for (int i = 0; i < l.size(); i++)
161            delete l[i].pMidiListener;
162    }
163    
164    void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
165        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
166    }
167    
168    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
169        pChannel->AddEngineChangeListener(this);
170    }
171    
172    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
173        if (!pChannel->GetEngineChannel()) return;
174        EngineToBeChanged(pChannel->Index());
175    }
176    
177    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
178        SamplerChannel* pSamplerChannel =
179            pParent->pSampler->GetSamplerChannel(ChannelId);
180        if (!pSamplerChannel) return;
181        EngineChannel* pEngineChannel =
182            pSamplerChannel->GetEngineChannel();
183        if (!pEngineChannel) return;
184        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
185            if ((*iter).pEngineChannel == pEngineChannel) {
186                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
187                pEngineChannel->Disconnect(pMidiListener);
188                channelMidiListeners.erase(iter);
189                delete pMidiListener;
190                return;
191            }
192        }
193    }
194    
195    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
196        SamplerChannel* pSamplerChannel =
197            pParent->pSampler->GetSamplerChannel(ChannelId);
198        if (!pSamplerChannel) return;
199        EngineChannel* pEngineChannel =
200            pSamplerChannel->GetEngineChannel();
201        if (!pEngineChannel) return;
202        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
203        pEngineChannel->Connect(pMidiListener);
204        midi_listener_entry entry = {
205            pSamplerChannel, pEngineChannel, pMidiListener
206        };
207        channelMidiListeners.push_back(entry);
208    }
209    
210    void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
211        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
212    }
213    
214    void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
215        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
216    }
217    
218    void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
219        pDevice->RemoveMidiPortCountListener(this);
220        for (int i = 0; i < pDevice->PortCount(); ++i)
221            MidiPortToBeRemoved(pDevice->GetPort(i));
222    }
223    
224    void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
225        pDevice->AddMidiPortCountListener(this);
226        for (int i = 0; i < pDevice->PortCount(); ++i)
227            MidiPortAdded(pDevice->GetPort(i));
228    }
229    
230    void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
231        // yet unused
232    }
233    
234    void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
235        for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
236            if ((*iter).pPort == pPort) {
237                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
238                pPort->Disconnect(pMidiListener);
239                deviceMidiListeners.erase(iter);
240                delete pMidiListener;
241                return;
242            }
243        }
244    }
245    
246    void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
247        // find out the device ID
248        std::map<uint, MidiInputDevice*> devices =
249            pParent->pSampler->GetMidiInputDevices();
250        for (
251            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
252            iter != devices.end(); ++iter
253        ) {
254            if (iter->second == pPort->GetDevice()) { // found
255                VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
256                pPort->Connect(pMidiListener);
257                device_midi_listener_entry entry = {
258                    pPort, pMidiListener, iter->first
259                };
260                deviceMidiListeners.push_back(entry);
261                return;
262            }
263        }
264    }
265    
266    void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
267        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
268    }
269    
270    void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
271        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
272    }
273    
274    void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
275        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
276    }
277    
278    void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
279        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
280    }
281    
282    void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
283        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
284    }
285    
286    void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
287        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
288    }
289    
290    void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
291        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
292    }
293    
294    void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
295        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
296    }
297    
298    void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
299        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
300    }
301    
302    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
303        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
304    }
305    
306    #if HAVE_SQLITE3
307    void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
308        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
309    }
310    
311    void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
312        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
313    }
314    
315    void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
316        Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
317        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
318        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
319    }
320    
321    void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
322        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
323    }
324    
325    void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
326        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
327    }
328    
329    void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
330        Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
331        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
332        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
333    }
334    
335    void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
336        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
337    }
338    #endif // HAVE_SQLITE3
339    
340    void LSCPServer::RemoveListeners() {
341        pSampler->RemoveChannelCountListener(&eventHandler);
342        pSampler->RemoveAudioDeviceCountListener(&eventHandler);
343        pSampler->RemoveMidiDeviceCountListener(&eventHandler);
344        pSampler->RemoveVoiceCountListener(&eventHandler);
345        pSampler->RemoveStreamCountListener(&eventHandler);
346        pSampler->RemoveBufferFillListener(&eventHandler);
347        pSampler->RemoveTotalStreamCountListener(&eventHandler);
348        pSampler->RemoveTotalVoiceCountListener(&eventHandler);
349        pSampler->RemoveFxSendCountListener(&eventHandler);
350        MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler);
351        MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler);
352        MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler);
353        MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler);
354    #if HAVE_SQLITE3
355        InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler);
356    #endif
357  }  }
358    
359  /**  /**
# Line 103  int LSCPServer::WaitUntilInitialized(lon Line 371  int LSCPServer::WaitUntilInitialized(lon
371  }  }
372    
373  int LSCPServer::Main() {  int LSCPServer::Main() {
374            #if defined(WIN32)
375            WSADATA wsaData;
376            int iResult;
377            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
378            if (iResult != 0) {
379                    std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
380                    exit(EXIT_FAILURE);
381            }
382            #endif
383      hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
384      if (hSocket < 0) {      if (hSocket < 0) {
385          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
# Line 116  int LSCPServer::Main() { Line 393  int LSCPServer::Main() {
393              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
394                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
395                      std::cerr << "gave up!" << std::endl;                      std::cerr << "gave up!" << std::endl;
396                        #if defined(WIN32)
397                        closesocket(hSocket);
398                        #else
399                      close(hSocket);                      close(hSocket);
400                        #endif
401                      //return -1;                      //return -1;
402                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
403                  }                  }
# Line 129  int LSCPServer::Main() { Line 410  int LSCPServer::Main() {
410      listen(hSocket, 1);      listen(hSocket, 1);
411      Initialized.Set(true);      Initialized.Set(true);
412    
413        // Registering event listeners
414        pSampler->AddChannelCountListener(&eventHandler);
415        pSampler->AddAudioDeviceCountListener(&eventHandler);
416        pSampler->AddMidiDeviceCountListener(&eventHandler);
417        pSampler->AddVoiceCountListener(&eventHandler);
418        pSampler->AddStreamCountListener(&eventHandler);
419        pSampler->AddBufferFillListener(&eventHandler);
420        pSampler->AddTotalStreamCountListener(&eventHandler);
421        pSampler->AddTotalVoiceCountListener(&eventHandler);
422        pSampler->AddFxSendCountListener(&eventHandler);
423        MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
424        MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
425        MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
426        MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
427    #if HAVE_SQLITE3
428        InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
429    #endif
430      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
431      sockaddr_in client;      sockaddr_in client;
432      int length = sizeof(client);      int length = sizeof(client);
# Line 139  int LSCPServer::Main() { Line 437  int LSCPServer::Main() {
437      timeval timeout;      timeval timeout;
438    
439      while (true) {      while (true) {
440            #if CONFIG_PTHREAD_TESTCANCEL
441                    TestCancel();
442            #endif
443          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
444          {          {
445              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
# Line 146  int LSCPServer::Main() { Line 447  int LSCPServer::Main() {
447              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
448              for (; itEngineChannel != itEnd; ++itEngineChannel) {              for (; itEngineChannel != itEnd; ++itEngineChannel) {
449                  if ((*itEngineChannel)->StatusChanged()) {                  if ((*itEngineChannel)->StatusChanged()) {
450                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index()));
451                    }
452    
453                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
454                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
455                        if(fxs != NULL && fxs->IsInfoChanged()) {
456                            int chn = (*itEngineChannel)->GetSamplerChannel()->Index();
457                            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
458                            fxs->SetInfoChanged(false);
459                        }
460                    }
461                }
462            }
463    
464            // check if MIDI data arrived on some engine channel
465            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
466                const EventHandler::midi_listener_entry entry =
467                    eventHandler.channelMidiListeners[i];
468                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
469                if (pMidiListener->NotesChanged()) {
470                    for (int iNote = 0; iNote < 128; iNote++) {
471                        if (pMidiListener->NoteChanged(iNote)) {
472                            const bool bActive = pMidiListener->NoteIsActive(iNote);
473                            LSCPServer::SendLSCPNotify(
474                                LSCPEvent(
475                                    LSCPEvent::event_channel_midi,
476                                    entry.pSamplerChannel->Index(),
477                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
478                                    iNote,
479                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
480                                            : pMidiListener->NoteOffVelocity(iNote)
481                                )
482                            );
483                        }
484                    }
485                }
486            }
487    
488            // check if MIDI data arrived on some MIDI device
489            for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
490                const EventHandler::device_midi_listener_entry entry =
491                    eventHandler.deviceMidiListeners[i];
492                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
493                if (pMidiListener->NotesChanged()) {
494                    for (int iNote = 0; iNote < 128; iNote++) {
495                        if (pMidiListener->NoteChanged(iNote)) {
496                            const bool bActive = pMidiListener->NoteIsActive(iNote);
497                            LSCPServer::SendLSCPNotify(
498                                LSCPEvent(
499                                    LSCPEvent::event_device_midi,
500                                    entry.uiDeviceID,
501                                    entry.pPort->GetPortNumber(),
502                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
503                                    iNote,
504                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
505                                            : pMidiListener->NoteOffVelocity(iNote)
506                                )
507                            );
508                        }
509                  }                  }
510              }              }
511          }          }
# Line 169  int LSCPServer::Main() { Line 528  int LSCPServer::Main() {
528    
529          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
530    
531          if (retval == 0)          if (retval == 0 || (retval == -1 && errno == EINTR))
532                  continue; //Nothing try again                  continue; //Nothing try again
533          if (retval == -1) {          if (retval == -1) {
534                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
535                    #if defined(WIN32)
536                    closesocket(hSocket);
537                    #else
538                  close(hSocket);                  close(hSocket);
539                    #endif
540                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
541          }          }
542    
# Line 185  int LSCPServer::Main() { Line 548  int LSCPServer::Main() {
548                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
549                  }                  }
550    
551                    #if defined(WIN32)
552                    u_long nonblock_io = 1;
553                    if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
554                      std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
555                      exit(EXIT_FAILURE);
556                    }
557            #else
558                    struct linger linger;
559                    linger.l_onoff = 1;
560                    linger.l_linger = 0;
561                    if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) {
562                        std::cerr << "LSCPServer: Failed to set SO_LINGER\n";
563                    }
564    
565                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
566                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
567                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
568                  }                  }
569                    #endif
570    
571                  // Parser initialization                  // Parser initialization
572                  yyparse_param_t yyparse_param;                  yyparse_param_t yyparse_param;
# Line 212  int LSCPServer::Main() { Line 590  int LSCPServer::Main() {
590                                  int dummy; // just a temporary hack to fulfill the restart() function prototype                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
591                                  restart(NULL, dummy); // restart the 'scanner'                                  restart(NULL, dummy); // restart the 'scanner'
592                                  currentSocket = (*iter).hSession;  //a hack                                  currentSocket = (*iter).hSession;  //a hack
593                                    itCurrentSession = iter; // another hack
594                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
595                                  if ((*iter).bVerbose) { // if echo mode enabled                                  if ((*iter).bVerbose) { // if echo mode enabled
596                                      AnswerClient(bufferedCommands[currentSocket]);                                      AnswerClient(bufferedCommands[currentSocket]);
597                                  }                                  }
598                                  int result = yyparse(&(*iter));                                  int result = yyparse(&(*iter));
599                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
600                                    itCurrentSession = Sessions.end(); // hack as well
601                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
602                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?
603                                          CloseConnection(iter);                                          CloseConnection(iter);
# Line 245  void LSCPServer::CloseConnection( std::v Line 625  void LSCPServer::CloseConnection( std::v
625          NotifyMutex.Lock();          NotifyMutex.Lock();
626          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
627          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
628            #if defined(WIN32)
629            closesocket(socket);
630            #else
631          close(socket);          close(socket);
632            #endif
633          NotifyMutex.Unlock();          NotifyMutex.Unlock();
634  }  }
635    
636    void LSCPServer::CloseAllConnections() {
637        std::vector<yyparse_param_t>::iterator iter = Sessions.begin();
638        while(iter != Sessions.end()) {
639            CloseConnection(iter);
640            iter = Sessions.begin();
641        }
642    }
643    
644    void LSCPServer::LockRTNotify() {
645        RTNotifyMutex.Lock();
646    }
647    
648    void LSCPServer::UnlockRTNotify() {
649        RTNotifyMutex.Unlock();
650    }
651    
652  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
653          int subs = 0;          int subs = 0;
654          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 310  extern int GetLSCPCommand( void *buf, in Line 710  extern int GetLSCPCommand( void *buf, in
710          return command.size();          return command.size();
711  }  }
712    
713    extern yyparse_param_t* GetCurrentYaccSession() {
714        return &(*itCurrentSession);
715    }
716    
717  /**  /**
718   * Will be called to try to read the command from the socket   * Will be called to try to read the command from the socket
719   * If command is read, it will return true. Otherwise false is returned.   * If command is read, it will return true. Otherwise false is returned.
# Line 320  bool LSCPServer::GetLSCPCommand( std::ve Line 724  bool LSCPServer::GetLSCPCommand( std::ve
724          char c;          char c;
725          int i = 0;          int i = 0;
726          while (true) {          while (true) {
727                    #if defined(WIN32)
728                    int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
729                    #else
730                  int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now                  int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
731                    #endif
732                  if (result == 0) { //socket was selected, so 0 here means client has closed the connection                  if (result == 0) { //socket was selected, so 0 here means client has closed the connection
733                          CloseConnection(iter);                          CloseConnection(iter);
734                          break;                          break;
# Line 335  bool LSCPServer::GetLSCPCommand( std::ve Line 743  bool LSCPServer::GetLSCPCommand( std::ve
743                          }                          }
744                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
745                  }                  }
746                    #if defined(WIN32)
747                    if (result == SOCKET_ERROR) {
748                        int wsa_lasterror = WSAGetLastError();
749                            if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
750                                    return false;
751                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
752                            CloseConnection(iter);
753                            break;
754                    }
755                    #else
756                  if (result == -1) {                  if (result == -1) {
757                          if (errno == EAGAIN) //Would block, try again later.                          if (errno == EAGAIN) //Would block, try again later.
758                                  return false;                                  return false;
# Line 373  bool LSCPServer::GetLSCPCommand( std::ve Line 791  bool LSCPServer::GetLSCPCommand( std::ve
791                          CloseConnection(iter);                          CloseConnection(iter);
792                          break;                          break;
793                  }                  }
794                    #endif
795          }          }
796          return false;          return false;
797  }  }
# Line 490  String LSCPServer::DestroyMidiInputDevic Line 909  String LSCPServer::DestroyMidiInputDevic
909      return result.Produce();      return result.Produce();
910  }  }
911    
912    EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
913        SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
914        if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
915    
916        EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
917        if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
918    
919        return pEngineChannel;
920    }
921    
922  /**  /**
923   * Will be called by the parser to load an instrument.   * Will be called by the parser to load an instrument.
924   */   */
# Line 636  String LSCPServer::GetEngineInfo(String Line 1065  String LSCPServer::GetEngineInfo(String
1065      LockRTNotify();      LockRTNotify();
1066      try {      try {
1067          Engine* pEngine = EngineFactory::Create(EngineName);          Engine* pEngine = EngineFactory::Create(EngineName);
1068          result.Add("DESCRIPTION", pEngine->Description());          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1069          result.Add("VERSION",     pEngine->Version());          result.Add("VERSION",     pEngine->Version());
1070          EngineFactory::Destroy(pEngine);          EngineFactory::Destroy(pEngine);
1071      }      }
# Line 670  String LSCPServer::GetChannelInfo(uint u Line 1099  String LSCPServer::GetChannelInfo(uint u
1099          String AudioRouting;          String AudioRouting;
1100          int Mute = 0;          int Mute = 0;
1101          bool Solo = false;          bool Solo = false;
1102          String MidiInstrumentMap;          String MidiInstrumentMap = "NONE";
1103    
1104          if (pEngineChannel) {          if (pEngineChannel) {
1105              EngineName          = pEngineChannel->EngineName();              EngineName          = pEngineChannel->EngineName();
# Line 709  String LSCPServer::GetChannelInfo(uint u Line 1138  String LSCPServer::GetChannelInfo(uint u
1138          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
1139          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
1140    
1141            // convert the filename into the correct encoding as defined for LSCP
1142            // (especially in terms of special characters -> escape sequences)
1143            if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
1144    #if WIN32
1145                InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
1146    #else
1147                // assuming POSIX
1148                InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
1149    #endif
1150            }
1151    
1152          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
1153          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
1154          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
1155          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
1156          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
1157          result.Add("SOLO", Solo);          result.Add("SOLO", Solo);
# Line 731  String LSCPServer::GetVoiceCount(uint ui Line 1171  String LSCPServer::GetVoiceCount(uint ui
1171      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
1172      LSCPResultSet result;      LSCPResultSet result;
1173      try {      try {
1174          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine loaded on sampler channel");  
1175          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1176          result.Add(pEngineChannel->GetEngine()->VoiceCount());          result.Add(pEngineChannel->GetEngine()->VoiceCount());
1177      }      }
# Line 752  String LSCPServer::GetStreamCount(uint u Line 1189  String LSCPServer::GetStreamCount(uint u
1189      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
1190      LSCPResultSet result;      LSCPResultSet result;
1191      try {      try {
1192          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1193          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1194          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
1195      }      }
# Line 773  String LSCPServer::GetBufferFill(fill_re Line 1207  String LSCPServer::GetBufferFill(fill_re
1207      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
1208      LSCPResultSet result;      LSCPResultSet result;
1209      try {      try {
1210          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1211          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1212          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
1213          else {          else {
# Line 864  String LSCPServer::GetMidiInputDriverInf Line 1295  String LSCPServer::GetMidiInputDriverInf
1295              for (;iter != parameters.end(); iter++) {              for (;iter != parameters.end(); iter++) {
1296                  if (s != "") s += ",";                  if (s != "") s += ",";
1297                  s += iter->first;                  s += iter->first;
1298                    delete iter->second;
1299              }              }
1300              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1301          }          }
# Line 888  String LSCPServer::GetAudioOutputDriverI Line 1320  String LSCPServer::GetAudioOutputDriverI
1320              for (;iter != parameters.end(); iter++) {              for (;iter != parameters.end(); iter++) {
1321                  if (s != "") s += ",";                  if (s != "") s += ",";
1322                  s += iter->first;                  s += iter->first;
1323                    delete iter->second;
1324              }              }
1325              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1326          }          }
# Line 918  String LSCPServer::GetMidiInputDriverPar Line 1351  String LSCPServer::GetMidiInputDriverPar
1351          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1352          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1353          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1354            delete pParameter;
1355      }      }
1356      catch (Exception e) {      catch (Exception e) {
1357          result.Error(e);          result.Error(e);
# Line 945  String LSCPServer::GetAudioOutputDriverP Line 1379  String LSCPServer::GetAudioOutputDriverP
1379          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1380          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1381          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1382            delete pParameter;
1383      }      }
1384      catch (Exception e) {      catch (Exception e) {
1385          result.Error(e);          result.Error(e);
# Line 1454  String LSCPServer::SetVolume(double dVol Line 1889  String LSCPServer::SetVolume(double dVol
1889      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1890      LSCPResultSet result;      LSCPResultSet result;
1891      try {      try {
1892          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1893          pEngineChannel->Volume(dVolume);          pEngineChannel->Volume(dVolume);
1894      }      }
1895      catch (Exception e) {      catch (Exception e) {
# Line 1473  String LSCPServer::SetChannelMute(bool b Line 1905  String LSCPServer::SetChannelMute(bool b
1905      dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));      dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1906      LSCPResultSet result;      LSCPResultSet result;
1907      try {      try {
1908          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
   
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1909    
1910          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1911          else pEngineChannel->SetMute(1);          else pEngineChannel->SetMute(1);
# Line 1494  String LSCPServer::SetChannelSolo(bool b Line 1922  String LSCPServer::SetChannelSolo(bool b
1922      dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));      dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1923      LSCPResultSet result;      LSCPResultSet result;
1924      try {      try {
1925          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
   
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1926    
1927          bool oldSolo = pEngineChannel->GetSolo();          bool oldSolo = pEngineChannel->GetSolo();
1928          bool hadSoloChannel = HasSoloChannel();          bool hadSoloChannel = HasSoloChannel();
1929            
1930          pEngineChannel->SetSolo(bSolo);          pEngineChannel->SetSolo(bSolo);
1931            
1932          if(!oldSolo && bSolo) {          if(!oldSolo && bSolo) {
1933              if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);              if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1934              if(!hadSoloChannel) MuteNonSoloChannels();              if(!hadSoloChannel) MuteNonSoloChannels();
1935          }          }
1936            
1937          if(oldSolo && !bSolo) {          if(oldSolo && !bSolo) {
1938              if(!HasSoloChannel()) UnmuteChannels();              if(!HasSoloChannel()) UnmuteChannels();
1939              else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);              else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
# Line 1567  void  LSCPServer::UnmuteChannels() { Line 1991  void  LSCPServer::UnmuteChannels() {
1991      }      }
1992  }  }
1993    
1994  String LSCPServer::AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name) {  String LSCPServer::AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name, bool bModal) {
1995      dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));      dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1996    
1997      midi_prog_index_t idx;      midi_prog_index_t idx;
# Line 1585  String LSCPServer::AddOrReplaceMIDIInstr Line 2009  String LSCPServer::AddOrReplaceMIDIInstr
2009    
2010      LSCPResultSet result;      LSCPResultSet result;
2011      try {      try {
2012          // PERSISTENT mapping commands might bloock for a long time, so in          // PERSISTENT mapping commands might block for a long time, so in
2013          // that case we add/replace the mapping in another thread          // that case we add/replace the mapping in another thread in case
2014          bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT);          // the NON_MODAL argument was supplied, non persistent mappings
2015            // should return immediately, so we don't need to do that for them
2016            bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
2017          MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);          MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
2018      } catch (Exception e) {      } catch (Exception e) {
2019          result.Error(e);          result.Error(e);
# Line 1616  String LSCPServer::GetMidiInstrumentMapp Line 2042  String LSCPServer::GetMidiInstrumentMapp
2042      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2043      LSCPResultSet result;      LSCPResultSet result;
2044      try {      try {
2045          result.Add(MidiInstrumentMapper::Entries(MidiMapID).size());          result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID));
2046      } catch (Exception e) {      } catch (Exception e) {
2047          result.Error(e);          result.Error(e);
2048      }      }
# Line 1627  String LSCPServer::GetMidiInstrumentMapp Line 2053  String LSCPServer::GetMidiInstrumentMapp
2053  String LSCPServer::GetAllMidiInstrumentMappings() {  String LSCPServer::GetAllMidiInstrumentMappings() {
2054      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2055      LSCPResultSet result;      LSCPResultSet result;
2056      std::vector<int> maps = MidiInstrumentMapper::Maps();      try {
2057      int totalMappings = 0;          result.Add(MidiInstrumentMapper::GetInstrumentCount());
2058      for (int i = 0; i < maps.size(); i++) {      } catch (Exception e) {
2059          try {          result.Error(e);
             totalMappings += MidiInstrumentMapper::Entries(maps[i]).size();  
         } catch (Exception e) { /*NOOP*/ }  
2060      }      }
     result.Add(totalMappings);  
2061      return result.Produce();      return result.Produce();
2062  }  }
2063    
# Line 1642  String LSCPServer::GetMidiInstrumentMapp Line 2065  String LSCPServer::GetMidiInstrumentMapp
2065      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2066      LSCPResultSet result;      LSCPResultSet result;
2067      try {      try {
2068          midi_prog_index_t idx;          MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg);
2069          idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;          // convert the filename into the correct encoding as defined for LSCP
2070          idx.midi_bank_lsb = MidiBank & 0x7f;          // (especially in terms of special characters -> escape sequences)
2071          idx.midi_prog     = MidiProg;  #if WIN32
2072            const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp();
2073    #else
2074            // assuming POSIX
2075            const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp();
2076    #endif
2077    
2078          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);          result.Add("NAME", _escapeLscpResponse(entry.Name));
2079          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);          result.Add("ENGINE_NAME", entry.EngineName);
2080          if (iter == mappings.end()) result.Error("there is no map entry with that index");          result.Add("INSTRUMENT_FILE", instrumentFileName);
2081          else { // found          result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex);
2082              result.Add("NAME", iter->second.Name);          String instrumentName;
2083              result.Add("ENGINE_NAME", iter->second.EngineName);          Engine* pEngine = EngineFactory::Create(entry.EngineName);
2084              result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile);          if (pEngine) {
2085              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);              if (pEngine->GetInstrumentManager()) {
2086              String instrumentName;                  InstrumentManager::instrument_id_t instrID;
2087              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);                  instrID.FileName = entry.InstrumentFile;
2088              if (pEngine) {                  instrID.Index    = entry.InstrumentIndex;
2089                  if (pEngine->GetInstrumentManager()) {                  instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
                     InstrumentManager::instrument_id_t instrID;  
                     instrID.FileName = iter->second.InstrumentFile;  
                     instrID.Index    = iter->second.InstrumentIndex;  
                     instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);  
                 }  
                 EngineFactory::Destroy(pEngine);  
2090              }              }
2091              result.Add("INSTRUMENT_NAME", instrumentName);              EngineFactory::Destroy(pEngine);
2092              switch (iter->second.LoadMode) {          }
2093                  case MidiInstrumentMapper::ON_DEMAND:          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2094                      result.Add("LOAD_MODE", "ON_DEMAND");          switch (entry.LoadMode) {
2095                      break;              case MidiInstrumentMapper::ON_DEMAND:
2096                  case MidiInstrumentMapper::ON_DEMAND_HOLD:                  result.Add("LOAD_MODE", "ON_DEMAND");
2097                      result.Add("LOAD_MODE", "ON_DEMAND_HOLD");                  break;
2098                      break;              case MidiInstrumentMapper::ON_DEMAND_HOLD:
2099                  case MidiInstrumentMapper::PERSISTENT:                  result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2100                      result.Add("LOAD_MODE", "PERSISTENT");                  break;
2101                      break;              case MidiInstrumentMapper::PERSISTENT:
2102                  default:                  result.Add("LOAD_MODE", "PERSISTENT");
2103                      throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");                  break;
2104              }              default:
2105              result.Add("VOLUME", iter->second.Volume);                  throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2106          }          }
2107            result.Add("VOLUME", entry.Volume);
2108      } catch (Exception e) {      } catch (Exception e) {
2109          result.Error(e);          result.Error(e);
2110      }      }
# Line 1698  String LSCPServer::ListMidiInstrumentMap Line 2121  String LSCPServer::ListMidiInstrumentMap
2121          for (; iter != mappings.end(); iter++) {          for (; iter != mappings.end(); iter++) {
2122              if (s.size()) s += ",";              if (s.size()) s += ",";
2123              s += "{" + ToString(MidiMapID) + ","              s += "{" + ToString(MidiMapID) + ","
2124                       + ToString((int(iter->first.midi_bank_msb) << 7) & int(iter->first.midi_bank_lsb)) + ","                       + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2125                       + ToString(int(iter->first.midi_prog)) + "}";                       + ToString(int(iter->first.midi_prog)) + "}";
2126          }          }
2127          result.Add(s);          result.Add(s);
# Line 1720  String LSCPServer::ListAllMidiInstrument Line 2143  String LSCPServer::ListAllMidiInstrument
2143              for (; iter != mappings.end(); iter++) {              for (; iter != mappings.end(); iter++) {
2144                  if (s.size()) s += ",";                  if (s.size()) s += ",";
2145                  s += "{" + ToString(maps[i]) + ","                  s += "{" + ToString(maps[i]) + ","
2146                           + ToString((int(iter->first.midi_bank_msb) << 7) & int(iter->first.midi_bank_lsb)) + ","                           + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2147                           + ToString(int(iter->first.midi_prog)) + "}";                           + ToString(int(iter->first.midi_prog)) + "}";
2148              }              }
2149          }          }
# Line 1821  String LSCPServer::GetMidiInstrumentMap( Line 2244  String LSCPServer::GetMidiInstrumentMap(
2244      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2245      LSCPResultSet result;      LSCPResultSet result;
2246      try {      try {
2247          result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID));          result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2248            result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2249      } catch (Exception e) {      } catch (Exception e) {
2250          result.Error(e);          result.Error(e);
2251      }      }
# Line 1851  String LSCPServer::SetChannelMap(uint ui Line 2275  String LSCPServer::SetChannelMap(uint ui
2275      dmsg(2,("LSCPServer: SetChannelMap()\n"));      dmsg(2,("LSCPServer: SetChannelMap()\n"));
2276      LSCPResultSet result;      LSCPResultSet result;
2277      try {      try {
2278          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
   
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");  
2279    
2280          if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();          if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2281          else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();          else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
# Line 1866  String LSCPServer::SetChannelMap(uint ui Line 2286  String LSCPServer::SetChannelMap(uint ui
2286      return result.Produce();      return result.Produce();
2287  }  }
2288    
2289    String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) {
2290        dmsg(2,("LSCPServer: CreateFxSend()\n"));
2291        LSCPResultSet result;
2292        try {
2293            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2294    
2295            FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2296            if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2297    
2298            result = LSCPResultSet(pFxSend->Id()); // success
2299        } catch (Exception e) {
2300            result.Error(e);
2301        }
2302        return result.Produce();
2303    }
2304    
2305    String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) {
2306        dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2307        LSCPResultSet result;
2308        try {
2309            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2310    
2311            FxSend* pFxSend = NULL;
2312            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2313                if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2314                    pFxSend = pEngineChannel->GetFxSend(i);
2315                    break;
2316                }
2317            }
2318            if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2319            pEngineChannel->RemoveFxSend(pFxSend);
2320        } catch (Exception e) {
2321            result.Error(e);
2322        }
2323        return result.Produce();
2324    }
2325    
2326    String LSCPServer::GetFxSends(uint uiSamplerChannel) {
2327        dmsg(2,("LSCPServer: GetFxSends()\n"));
2328        LSCPResultSet result;
2329        try {
2330            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2331    
2332            result.Add(pEngineChannel->GetFxSendCount());
2333        } catch (Exception e) {
2334            result.Error(e);
2335        }
2336        return result.Produce();
2337    }
2338    
2339    String LSCPServer::ListFxSends(uint uiSamplerChannel) {
2340        dmsg(2,("LSCPServer: ListFxSends()\n"));
2341        LSCPResultSet result;
2342        String list;
2343        try {
2344            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2345    
2346            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2347                FxSend* pFxSend = pEngineChannel->GetFxSend(i);
2348                if (list != "") list += ",";
2349                list += ToString(pFxSend->Id());
2350            }
2351            result.Add(list);
2352        } catch (Exception e) {
2353            result.Error(e);
2354        }
2355        return result.Produce();
2356    }
2357    
2358    FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2359        EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2360    
2361        FxSend* pFxSend = NULL;
2362        for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2363            if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2364                pFxSend = pEngineChannel->GetFxSend(i);
2365                break;
2366            }
2367        }
2368        if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2369        return pFxSend;
2370    }
2371    
2372    String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2373        dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2374        LSCPResultSet result;
2375        try {
2376            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2377            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2378    
2379            // gather audio routing informations
2380            String AudioRouting;
2381            for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
2382                if (AudioRouting != "") AudioRouting += ",";
2383                AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2384            }
2385    
2386            // success
2387            result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2388            result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2389            result.Add("LEVEL", ToString(pFxSend->Level()));
2390            result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2391        } catch (Exception e) {
2392            result.Error(e);
2393        }
2394        return result.Produce();
2395    }
2396    
2397    String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2398        dmsg(2,("LSCPServer: SetFxSendName()\n"));
2399        LSCPResultSet result;
2400        try {
2401            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2402    
2403            pFxSend->SetName(Name);
2404            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2405        } catch (Exception e) {
2406            result.Error(e);
2407        }
2408        return result.Produce();
2409    }
2410    
2411    String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2412        dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2413        LSCPResultSet result;
2414        try {
2415            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2416    
2417            pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2418            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2419        } catch (Exception e) {
2420            result.Error(e);
2421        }
2422        return result.Produce();
2423    }
2424    
2425    String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2426        dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2427        LSCPResultSet result;
2428        try {
2429            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2430    
2431            pFxSend->SetMidiController(MidiController);
2432            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2433        } catch (Exception e) {
2434            result.Error(e);
2435        }
2436        return result.Produce();
2437    }
2438    
2439    String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2440        dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2441        LSCPResultSet result;
2442        try {
2443            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2444    
2445            pFxSend->SetLevel((float)dLevel);
2446            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2447        } catch (Exception e) {
2448            result.Error(e);
2449        }
2450        return result.Produce();
2451    }
2452    
2453    String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2454        dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2455        LSCPResultSet result;
2456        try {
2457            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2458            if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2459            Engine* pEngine = pEngineChannel->GetEngine();
2460            InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2461            if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2462            InstrumentManager::instrument_id_t instrumentID;
2463            instrumentID.FileName = pEngineChannel->InstrumentFileName();
2464            instrumentID.Index    = pEngineChannel->InstrumentIndex();
2465            pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2466        } catch (Exception e) {
2467            result.Error(e);
2468        }
2469        return result.Produce();
2470    }
2471    
2472    String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) {
2473        dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2));
2474        LSCPResultSet result;
2475        try {
2476            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2477    
2478            if (Arg1 > 127 || Arg2 > 127) {
2479                throw Exception("Invalid MIDI message");
2480            }
2481    
2482            VirtualMidiDevice* pMidiDevice = NULL;
2483            std::vector<EventHandler::midi_listener_entry>::iterator iter = eventHandler.channelMidiListeners.begin();
2484            for (; iter != eventHandler.channelMidiListeners.end(); ++iter) {
2485                if ((*iter).pEngineChannel == pEngineChannel) {
2486                    pMidiDevice = (*iter).pMidiListener;
2487                    break;
2488                }
2489            }
2490            
2491            if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device");
2492    
2493            if (MidiMsg == "NOTE_ON") {
2494                pMidiDevice->SendNoteOnToDevice(Arg1, Arg2);
2495                bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2);
2496                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2497            } else if (MidiMsg == "NOTE_OFF") {
2498                pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);
2499                bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);
2500                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2501            } else {
2502                throw Exception("Unknown MIDI message type: " + MidiMsg);
2503            }
2504        } catch (Exception e) {
2505            result.Error(e);
2506        }
2507        return result.Produce();
2508    }
2509    
2510  /**  /**
2511   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
2512   */   */
# Line 1873  String LSCPServer::ResetChannel(uint uiS Line 2514  String LSCPServer::ResetChannel(uint uiS
2514      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
2515      LSCPResultSet result;      LSCPResultSet result;
2516      try {      try {
2517          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
2518          pEngineChannel->Reset();          pEngineChannel->Reset();
2519      }      }
2520      catch (Exception e) {      catch (Exception e) {
# Line 1901  String LSCPServer::ResetSampler() { Line 2539  String LSCPServer::ResetSampler() {
2539   */   */
2540  String LSCPServer::GetServerInfo() {  String LSCPServer::GetServerInfo() {
2541      dmsg(2,("LSCPServer: GetServerInfo()\n"));      dmsg(2,("LSCPServer: GetServerInfo()\n"));
2542        const std::string description =
2543            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2544      LSCPResultSet result;      LSCPResultSet result;
2545      result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");      result.Add("DESCRIPTION", description);
2546      result.Add("VERSION", VERSION);      result.Add("VERSION", VERSION);
2547      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2548    #if HAVE_SQLITE3
2549        result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2550    #else
2551        result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2552    #endif
2553    
2554        return result.Produce();
2555    }
2556    
2557    /**
2558     * Will be called by the parser to return the current number of all active streams.
2559     */
2560    String LSCPServer::GetTotalStreamCount() {
2561        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2562        LSCPResultSet result;
2563        result.Add(pSampler->GetDiskStreamCount());
2564      return result.Produce();      return result.Produce();
2565  }  }
2566    
# Line 1924  String LSCPServer::GetTotalVoiceCount() Line 2580  String LSCPServer::GetTotalVoiceCount()
2580  String LSCPServer::GetTotalVoiceCountMax() {  String LSCPServer::GetTotalVoiceCountMax() {
2581      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
2582      LSCPResultSet result;      LSCPResultSet result;
2583      result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);      result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES);
2584        return result.Produce();
2585    }
2586    
2587    /**
2588     * Will be called by the parser to return the sampler global maximum
2589     * allowed number of voices.
2590     */
2591    String LSCPServer::GetGlobalMaxVoices() {
2592        dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));
2593        LSCPResultSet result;
2594        result.Add(GLOBAL_MAX_VOICES);
2595      return result.Produce();      return result.Produce();
2596  }  }
2597    
2598  /**  /**
2599     * Will be called by the parser to set the sampler global maximum number of
2600     * voices.
2601     */
2602    String LSCPServer::SetGlobalMaxVoices(int iVoices) {
2603        dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));
2604        LSCPResultSet result;
2605        try {
2606            if (iVoices < 1) throw Exception("Maximum voices may not be less than 1");
2607            GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp
2608            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
2609            if (engines.size() > 0) {
2610                std::set<Engine*>::iterator iter = engines.begin();
2611                std::set<Engine*>::iterator end  = engines.end();
2612                for (; iter != end; ++iter) {
2613                    (*iter)->SetMaxVoices(iVoices);
2614                }
2615            }
2616            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES));
2617        } catch (Exception e) {
2618            result.Error(e);
2619        }
2620        return result.Produce();
2621    }
2622    
2623    /**
2624     * Will be called by the parser to return the sampler global maximum
2625     * allowed number of disk streams.
2626     */
2627    String LSCPServer::GetGlobalMaxStreams() {
2628        dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));
2629        LSCPResultSet result;
2630        result.Add(GLOBAL_MAX_STREAMS);
2631        return result.Produce();
2632    }
2633    
2634    /**
2635     * Will be called by the parser to set the sampler global maximum number of
2636     * disk streams.
2637     */
2638    String LSCPServer::SetGlobalMaxStreams(int iStreams) {
2639        dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));
2640        LSCPResultSet result;
2641        try {
2642            if (iStreams < 0) throw Exception("Maximum disk streams may not be negative");
2643            GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp
2644            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
2645            if (engines.size() > 0) {
2646                std::set<Engine*>::iterator iter = engines.begin();
2647                std::set<Engine*>::iterator end  = engines.end();
2648                for (; iter != end; ++iter) {
2649                    (*iter)->SetMaxDiskStreams(iStreams);
2650                }
2651            }
2652            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS));
2653        } catch (Exception e) {
2654            result.Error(e);
2655        }
2656        return result.Produce();
2657    }
2658    
2659    String LSCPServer::GetGlobalVolume() {
2660        LSCPResultSet result;
2661        result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp
2662        return result.Produce();
2663    }
2664    
2665    String LSCPServer::SetGlobalVolume(double dVolume) {
2666        LSCPResultSet result;
2667        try {
2668            if (dVolume < 0) throw Exception("Volume may not be negative");
2669            GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
2670            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
2671        } catch (Exception e) {
2672            result.Error(e);
2673        }
2674        return result.Produce();
2675    }
2676    
2677    String LSCPServer::GetFileInstruments(String Filename) {
2678        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2679        LSCPResultSet result;
2680        try {
2681            VerifyFile(Filename);
2682        } catch (Exception e) {
2683            result.Error(e);
2684            return result.Produce();
2685        }
2686        // try to find a sampler engine that can handle the file
2687        bool bFound = false;
2688        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2689        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2690            Engine* pEngine = NULL;
2691            try {
2692                pEngine = EngineFactory::Create(engineTypes[i]);
2693                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2694                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2695                if (pManager) {
2696                    std::vector<InstrumentManager::instrument_id_t> IDs =
2697                        pManager->GetInstrumentFileContent(Filename);
2698                    // return the amount of instruments in the file
2699                    result.Add(IDs.size());
2700                    // no more need to ask other engine types
2701                    bFound = true;
2702                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2703            } catch (Exception e) {
2704                // NOOP, as exception is thrown if engine doesn't support file
2705            }
2706            if (pEngine) EngineFactory::Destroy(pEngine);
2707        }
2708    
2709        if (!bFound) result.Error("Unknown file format");
2710        return result.Produce();
2711    }
2712    
2713    String LSCPServer::ListFileInstruments(String Filename) {
2714        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2715        LSCPResultSet result;
2716        try {
2717            VerifyFile(Filename);
2718        } catch (Exception e) {
2719            result.Error(e);
2720            return result.Produce();
2721        }
2722        // try to find a sampler engine that can handle the file
2723        bool bFound = false;
2724        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2725        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2726            Engine* pEngine = NULL;
2727            try {
2728                pEngine = EngineFactory::Create(engineTypes[i]);
2729                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2730                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2731                if (pManager) {
2732                    std::vector<InstrumentManager::instrument_id_t> IDs =
2733                        pManager->GetInstrumentFileContent(Filename);
2734                    // return a list of IDs of the instruments in the file
2735                    String s;
2736                    for (int j = 0; j < IDs.size(); j++) {
2737                        if (s.size()) s += ",";
2738                        s += ToString(IDs[j].Index);
2739                    }
2740                    result.Add(s);
2741                    // no more need to ask other engine types
2742                    bFound = true;
2743                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2744            } catch (Exception e) {
2745                // NOOP, as exception is thrown if engine doesn't support file
2746            }
2747            if (pEngine) EngineFactory::Destroy(pEngine);
2748        }
2749    
2750        if (!bFound) result.Error("Unknown file format");
2751        return result.Produce();
2752    }
2753    
2754    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2755        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2756        LSCPResultSet result;
2757        try {
2758            VerifyFile(Filename);
2759        } catch (Exception e) {
2760            result.Error(e);
2761            return result.Produce();
2762        }
2763        InstrumentManager::instrument_id_t id;
2764        id.FileName = Filename;
2765        id.Index    = InstrumentID;
2766        // try to find a sampler engine that can handle the file
2767        bool bFound = false;
2768        bool bFatalErr = false;
2769        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2770        for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
2771            Engine* pEngine = NULL;
2772            try {
2773                pEngine = EngineFactory::Create(engineTypes[i]);
2774                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2775                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2776                if (pManager) {
2777                    // check if the instrument index is valid
2778                    // FIXME: this won't work if an engine only supports parts of the instrument file
2779                    std::vector<InstrumentManager::instrument_id_t> IDs =
2780                        pManager->GetInstrumentFileContent(Filename);
2781                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2782                        std::stringstream ss;
2783                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2784                        bFatalErr = true;
2785                        throw Exception(ss.str());
2786                    }
2787                    // get the info of the requested instrument
2788                    InstrumentManager::instrument_info_t info =
2789                        pManager->GetInstrumentInfo(id);
2790                    // return detailed informations about the file
2791                    result.Add("NAME", info.InstrumentName);
2792                    result.Add("FORMAT_FAMILY", engineTypes[i]);
2793                    result.Add("FORMAT_VERSION", info.FormatVersion);
2794                    result.Add("PRODUCT", info.Product);
2795                    result.Add("ARTISTS", info.Artists);
2796    
2797                    std::stringstream ss;
2798                    bool b = false;
2799                    for (int i = 0; i < 128; i++) {
2800                        if (info.KeyBindings[i]) {
2801                            if (b) ss << ',';
2802                            ss << i; b = true;
2803                        }
2804                    }
2805                    result.Add("KEY_BINDINGS", ss.str());
2806    
2807                    b = false;
2808                    std::stringstream ss2;
2809                    for (int i = 0; i < 128; i++) {
2810                        if (info.KeySwitchBindings[i]) {
2811                            if (b) ss2 << ',';
2812                            ss2 << i; b = true;
2813                        }
2814                    }
2815                    result.Add("KEYSWITCH_BINDINGS", ss2.str());
2816                    // no more need to ask other engine types
2817                    bFound = true;
2818                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2819            } catch (Exception e) {
2820                // usually NOOP, as exception is thrown if engine doesn't support file
2821                if (bFatalErr) result.Error(e);
2822            }
2823            if (pEngine) EngineFactory::Destroy(pEngine);
2824        }
2825    
2826        if (!bFound && !bFatalErr) result.Error("Unknown file format");
2827        return result.Produce();
2828    }
2829    
2830    void LSCPServer::VerifyFile(String Filename) {
2831        #if WIN32
2832        WIN32_FIND_DATA win32FileAttributeData;
2833        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2834        if (!res) {
2835            std::stringstream ss;
2836            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2837            throw Exception(ss.str());
2838        }
2839        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2840            throw Exception("Directory is specified");
2841        }
2842        #else
2843        File f(Filename);
2844        if(!f.Exist()) throw Exception(f.GetErrorMsg());
2845        if (f.IsDirectory()) throw Exception("Directory is specified");
2846        #endif
2847    }
2848    
2849    /**
2850   * Will be called by the parser to subscribe a client (frontend) on the   * Will be called by the parser to subscribe a client (frontend) on the
2851   * server for receiving event messages.   * server for receiving event messages.
2852   */   */
# Line 1954  String LSCPServer::UnsubscribeNotificati Line 2872  String LSCPServer::UnsubscribeNotificati
2872      return result.Produce();      return result.Produce();
2873  }  }
2874    
2875  static int select_callback(void * lscpResultSet, int argc,  String LSCPServer::AddDbInstrumentDirectory(String Dir) {
2876                          char **argv, char **azColName)      dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
2877  {      LSCPResultSet result;
2878      LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;  #if HAVE_SQLITE3
2879      resultSet->Add(argc, argv);      try {
2880      return 0;          InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
2881        } catch (Exception e) {
2882             result.Error(e);
2883        }
2884    #else
2885        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2886    #endif
2887        return result.Produce();
2888    }
2889    
2890    String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
2891        dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
2892        LSCPResultSet result;
2893    #if HAVE_SQLITE3
2894        try {
2895            InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
2896        } catch (Exception e) {
2897             result.Error(e);
2898        }
2899    #else
2900        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2901    #endif
2902        return result.Produce();
2903    }
2904    
2905    String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
2906        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2907        LSCPResultSet result;
2908    #if HAVE_SQLITE3
2909        try {
2910            result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
2911        } catch (Exception e) {
2912             result.Error(e);
2913        }
2914    #else
2915        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2916    #endif
2917        return result.Produce();
2918    }
2919    
2920    String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
2921        dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2922        LSCPResultSet result;
2923    #if HAVE_SQLITE3
2924        try {
2925            String list;
2926            StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
2927    
2928            for (int i = 0; i < dirs->size(); i++) {
2929                if (list != "") list += ",";
2930                list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
2931            }
2932    
2933            result.Add(list);
2934        } catch (Exception e) {
2935             result.Error(e);
2936        }
2937    #else
2938        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2939    #endif
2940        return result.Produce();
2941    }
2942    
2943    String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
2944        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
2945        LSCPResultSet result;
2946    #if HAVE_SQLITE3
2947        try {
2948            DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2949    
2950            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2951            result.Add("CREATED", info.Created);
2952            result.Add("MODIFIED", info.Modified);
2953        } catch (Exception e) {
2954             result.Error(e);
2955        }
2956    #else
2957        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2958    #endif
2959        return result.Produce();
2960    }
2961    
2962    String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
2963        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
2964        LSCPResultSet result;
2965    #if HAVE_SQLITE3
2966        try {
2967            InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
2968        } catch (Exception e) {
2969             result.Error(e);
2970        }
2971    #else
2972        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2973    #endif
2974        return result.Produce();
2975    }
2976    
2977    String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
2978        dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2979        LSCPResultSet result;
2980    #if HAVE_SQLITE3
2981        try {
2982            InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
2983        } catch (Exception e) {
2984             result.Error(e);
2985        }
2986    #else
2987        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2988    #endif
2989        return result.Produce();
2990    }
2991    
2992    String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
2993        dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2994        LSCPResultSet result;
2995    #if HAVE_SQLITE3
2996        try {
2997            InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
2998        } catch (Exception e) {
2999             result.Error(e);
3000        }
3001    #else
3002        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3003    #endif
3004        return result.Produce();
3005    }
3006    
3007    String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
3008        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
3009        LSCPResultSet result;
3010    #if HAVE_SQLITE3
3011        try {
3012            InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
3013        } catch (Exception e) {
3014             result.Error(e);
3015        }
3016    #else
3017        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3018    #endif
3019        return result.Produce();
3020    }
3021    
3022    String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
3023        dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
3024        LSCPResultSet result;
3025    #if HAVE_SQLITE3
3026        try {
3027            int id;
3028            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3029            id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
3030            if (bBackground) result = id;
3031        } catch (Exception e) {
3032             result.Error(e);
3033        }
3034    #else
3035        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3036    #endif
3037        return result.Produce();
3038    }
3039    
3040    String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) {
3041        dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir));
3042        LSCPResultSet result;
3043    #if HAVE_SQLITE3
3044        try {
3045            int id;
3046            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3047            if (ScanMode.compare("RECURSIVE") == 0) {
3048                id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir);
3049            } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
3050                id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir);
3051            } else if (ScanMode.compare("FLAT") == 0) {
3052                id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir);
3053            } else {
3054                throw Exception("Unknown scan mode: " + ScanMode);
3055            }
3056    
3057            if (bBackground) result = id;
3058        } catch (Exception e) {
3059             result.Error(e);
3060        }
3061    #else
3062        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3063    #endif
3064        return result.Produce();
3065    }
3066    
3067    String LSCPServer::RemoveDbInstrument(String Instr) {
3068        dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
3069        LSCPResultSet result;
3070    #if HAVE_SQLITE3
3071        try {
3072            InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
3073        } catch (Exception e) {
3074             result.Error(e);
3075        }
3076    #else
3077        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3078    #endif
3079        return result.Produce();
3080    }
3081    
3082    String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
3083        dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3084        LSCPResultSet result;
3085    #if HAVE_SQLITE3
3086        try {
3087            result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
3088        } catch (Exception e) {
3089             result.Error(e);
3090        }
3091    #else
3092        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3093    #endif
3094        return result.Produce();
3095    }
3096    
3097    String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
3098        dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3099        LSCPResultSet result;
3100    #if HAVE_SQLITE3
3101        try {
3102            String list;
3103            StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
3104    
3105            for (int i = 0; i < instrs->size(); i++) {
3106                if (list != "") list += ",";
3107                list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
3108            }
3109    
3110            result.Add(list);
3111        } catch (Exception e) {
3112             result.Error(e);
3113        }
3114    #else
3115        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3116    #endif
3117        return result.Produce();
3118    }
3119    
3120    String LSCPServer::GetDbInstrumentInfo(String Instr) {
3121        dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
3122        LSCPResultSet result;
3123    #if HAVE_SQLITE3
3124        try {
3125            DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
3126    
3127            result.Add("INSTRUMENT_FILE", info.InstrFile);
3128            result.Add("INSTRUMENT_NR", info.InstrNr);
3129            result.Add("FORMAT_FAMILY", info.FormatFamily);
3130            result.Add("FORMAT_VERSION", info.FormatVersion);
3131            result.Add("SIZE", (int)info.Size);
3132            result.Add("CREATED", info.Created);
3133            result.Add("MODIFIED", info.Modified);
3134            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3135            result.Add("IS_DRUM", info.IsDrum);
3136            result.Add("PRODUCT", _escapeLscpResponse(info.Product));
3137            result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
3138            result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
3139        } catch (Exception e) {
3140             result.Error(e);
3141        }
3142    #else
3143        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3144    #endif
3145        return result.Produce();
3146    }
3147    
3148    String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
3149        dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
3150        LSCPResultSet result;
3151    #if HAVE_SQLITE3
3152        try {
3153            ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
3154    
3155            result.Add("FILES_TOTAL", job.FilesTotal);
3156            result.Add("FILES_SCANNED", job.FilesScanned);
3157            result.Add("SCANNING", job.Scanning);
3158            result.Add("STATUS", job.Status);
3159        } catch (Exception e) {
3160             result.Error(e);
3161        }
3162    #else
3163        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3164    #endif
3165        return result.Produce();
3166    }
3167    
3168    String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
3169        dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
3170        LSCPResultSet result;
3171    #if HAVE_SQLITE3
3172        try {
3173            InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
3174        } catch (Exception e) {
3175             result.Error(e);
3176        }
3177    #else
3178        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3179    #endif
3180        return result.Produce();
3181    }
3182    
3183    String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
3184        dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3185        LSCPResultSet result;
3186    #if HAVE_SQLITE3
3187        try {
3188            InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
3189        } catch (Exception e) {
3190             result.Error(e);
3191        }
3192    #else
3193        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3194    #endif
3195        return result.Produce();
3196  }  }
3197    
3198  String LSCPServer::QueryDatabase(String query) {  String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
3199        dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3200      LSCPResultSet result;      LSCPResultSet result;
3201  #if HAVE_SQLITE3  #if HAVE_SQLITE3
3202      char* zErrMsg = NULL;      try {
3203      sqlite3 *db;          InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
3204      String selectStr = "SELECT " + query;      } catch (Exception e) {
3205             result.Error(e);
3206        }
3207    #else
3208        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3209    #endif
3210        return result.Produce();
3211    }
3212    
3213    String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
3214        dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
3215        LSCPResultSet result;
3216    #if HAVE_SQLITE3
3217        try {
3218            InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
3219        } catch (Exception e) {
3220             result.Error(e);
3221        }
3222    #else
3223        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3224    #endif
3225        return result.Produce();
3226    }
3227    
3228    String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) {
3229        dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str()));
3230        LSCPResultSet result;
3231    #if HAVE_SQLITE3
3232        try {
3233            InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath);
3234        } catch (Exception e) {
3235             result.Error(e);
3236        }
3237    #else
3238        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3239    #endif
3240        return result.Produce();
3241    }
3242    
3243    String LSCPServer::FindLostDbInstrumentFiles() {
3244        dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n"));
3245        LSCPResultSet result;
3246    #if HAVE_SQLITE3
3247        try {
3248            String list;
3249            StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles();
3250    
3251            for (int i = 0; i < pLostFiles->size(); i++) {
3252                if (list != "") list += ",";
3253                list += "'" + pLostFiles->at(i) + "'";
3254            }
3255    
3256            result.Add(list);
3257        } catch (Exception e) {
3258             result.Error(e);
3259        }
3260    #else
3261        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3262    #endif
3263        return result.Produce();
3264    }
3265    
3266      int rc = sqlite3_open("linuxsampler.db", &db);  String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
3267      if (rc == SQLITE_OK)      dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
3268      {      LSCPResultSet result;
3269              rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);  #if HAVE_SQLITE3
3270        try {
3271            SearchQuery Query;
3272            std::map<String,String>::iterator iter;
3273            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3274                if (iter->first.compare("NAME") == 0) {
3275                    Query.Name = iter->second;
3276                } else if (iter->first.compare("CREATED") == 0) {
3277                    Query.SetCreated(iter->second);
3278                } else if (iter->first.compare("MODIFIED") == 0) {
3279                    Query.SetModified(iter->second);
3280                } else if (iter->first.compare("DESCRIPTION") == 0) {
3281                    Query.Description = iter->second;
3282                } else {
3283                    throw Exception("Unknown search criteria: " + iter->first);
3284                }
3285            }
3286    
3287            String list;
3288            StringListPtr pDirectories =
3289                InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
3290    
3291            for (int i = 0; i < pDirectories->size(); i++) {
3292                if (list != "") list += ",";
3293                list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
3294            }
3295    
3296            result.Add(list);
3297        } catch (Exception e) {
3298             result.Error(e);
3299      }      }
3300      if ( rc != SQLITE_OK )  #else
3301      {      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3302              result.Error(String(zErrMsg), rc);  #endif
3303        return result.Produce();
3304    }
3305    
3306    String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
3307        dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
3308        LSCPResultSet result;
3309    #if HAVE_SQLITE3
3310        try {
3311            SearchQuery Query;
3312            std::map<String,String>::iterator iter;
3313            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3314                if (iter->first.compare("NAME") == 0) {
3315                    Query.Name = iter->second;
3316                } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
3317                    Query.SetFormatFamilies(iter->second);
3318                } else if (iter->first.compare("SIZE") == 0) {
3319                    Query.SetSize(iter->second);
3320                } else if (iter->first.compare("CREATED") == 0) {
3321                    Query.SetCreated(iter->second);
3322                } else if (iter->first.compare("MODIFIED") == 0) {
3323                    Query.SetModified(iter->second);
3324                } else if (iter->first.compare("DESCRIPTION") == 0) {
3325                    Query.Description = iter->second;
3326                } else if (iter->first.compare("IS_DRUM") == 0) {
3327                    if (!strcasecmp(iter->second.c_str(), "true")) {
3328                        Query.InstrType = SearchQuery::DRUM;
3329                    } else {
3330                        Query.InstrType = SearchQuery::CHROMATIC;
3331                    }
3332                } else if (iter->first.compare("PRODUCT") == 0) {
3333                     Query.Product = iter->second;
3334                } else if (iter->first.compare("ARTISTS") == 0) {
3335                     Query.Artists = iter->second;
3336                } else if (iter->first.compare("KEYWORDS") == 0) {
3337                     Query.Keywords = iter->second;
3338                } else {
3339                    throw Exception("Unknown search criteria: " + iter->first);
3340                }
3341            }
3342    
3343            String list;
3344            StringListPtr pInstruments =
3345                InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
3346    
3347            for (int i = 0; i < pInstruments->size(); i++) {
3348                if (list != "") list += ",";
3349                list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
3350            }
3351    
3352            result.Add(list);
3353        } catch (Exception e) {
3354             result.Error(e);
3355      }      }
     sqlite3_close(db);  
3356  #else  #else
3357      result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3358  #endif  #endif
3359      return result.Produce();      return result.Produce();
3360  }  }
3361    
3362    String LSCPServer::FormatInstrumentsDb() {
3363        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
3364        LSCPResultSet result;
3365    #if HAVE_SQLITE3
3366        try {
3367            InstrumentsDb::GetInstrumentsDb()->Format();
3368        } catch (Exception e) {
3369             result.Error(e);
3370        }
3371    #else
3372        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3373    #endif
3374        return result.Produce();
3375    }
3376    
3377    
3378  /**  /**
3379   * Will be called by the parser to enable or disable echo mode; if echo   * Will be called by the parser to enable or disable echo mode; if echo
3380   * mode is enabled, all commands from the client will (immediately) be   * mode is enabled, all commands from the client will (immediately) be
# Line 2003  String LSCPServer::SetEcho(yyparse_param Line 3393  String LSCPServer::SetEcho(yyparse_param
3393      }      }
3394      return result.Produce();      return result.Produce();
3395  }  }
3396    
3397    }

Legend:
Removed from v.981  
changed lines
  Added in v.1882

  ViewVC Help
Powered by ViewVC