/[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 1007 by schoenebeck, Tue Jan 2 15:37:01 2007 UTC revision 1808 by persson, Sun Dec 14 15:43:18 2008 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 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 21  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24    #include <algorithm>
25    #include <string>
26    
27  #include "lscpserver.h"  #include "lscpserver.h"
28  #include "lscpresultset.h"  #include "lscpresultset.h"
29  #include "lscpevent.h"  #include "lscpevent.h"
 #include "../common/global.h"  
30    
31    #if defined(WIN32)
32    #include <windows.h>
33    #else
34  #include <fcntl.h>  #include <fcntl.h>
35    #endif
36    
37  #if HAVE_SQLITE3  #if ! HAVE_SQLITE3
38  # include "sqlite3.h"  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
39  #endif  #endif
40    
41  #include "../engines/EngineFactory.h"  #include "../engines/EngineFactory.h"
# Line 37  Line 43 
43  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
44  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
45    
46    namespace LinuxSampler {
47    
48    /**
49     * Returns a copy of the given string where all special characters are
50     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
51     * to escape LSCP response fields in case the respective response field is
52     * actually defined as using escape sequences in the LSCP specs.
53     *
54     * @e Caution: DO NOT use this function for escaping path based responses,
55     * use the Path class (src/common/Path.h) for this instead!
56     */
57    static String _escapeLscpResponse(String txt) {
58        for (int i = 0; i < txt.length(); i++) {
59            const char c = txt.c_str()[i];
60            if (
61                !(c >= '0' && c <= '9') &&
62                !(c >= 'a' && c <= 'z') &&
63                !(c >= 'A' && c <= 'Z') &&
64                !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
65                !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
66                !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
67                !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
68                !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
69                !(c == '@') && !(c == '[') && !(c == ']') &&
70                !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
71                !(c == '|') && !(c == '}') && !(c == '~')
72            ) {
73                // convert the "special" character into a "\xHH" LSCP escape sequence
74                char buf[5];
75                snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
76                txt.replace(i, 1, buf);
77                i += 3;
78            }
79        }
80        return txt;
81    }
82    
83  /**  /**
84   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
85   * 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 96 
96  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
97  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
98  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
99    std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
100  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
101  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
102  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 105  Mutex LSCPServer::NotifyBufferMutex = Mu
105  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
106  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
107    
108  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) {
109      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
110      SocketAddress.sin_addr.s_addr = addr;      SocketAddress.sin_addr.s_addr = addr;
111      SocketAddress.sin_port        = port;      SocketAddress.sin_port        = port;
# Line 75  LSCPServer::LSCPServer(Sampler* pSampler Line 119  LSCPServer::LSCPServer(Sampler* pSampler
119      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
120      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
121      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
122        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
123        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
124      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
125      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
126      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
127      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
128        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
129        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
130        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
131        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
132        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
133      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
134        LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
135      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
136        LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
137        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
138        LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
139      hSocket = -1;      hSocket = -1;
140  }  }
141    
142  LSCPServer::~LSCPServer() {  LSCPServer::~LSCPServer() {
143    #if defined(WIN32)
144        if (hSocket >= 0) closesocket(hSocket);
145    #else
146      if (hSocket >= 0) close(hSocket);      if (hSocket >= 0) close(hSocket);
147    #endif
148    }
149    
150    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
151        this->pParent = pParent;
152    }
153    
154    LSCPServer::EventHandler::~EventHandler() {
155        std::vector<midi_listener_entry> l = channelMidiListeners;
156        channelMidiListeners.clear();
157        for (int i = 0; i < l.size(); i++)
158            delete l[i].pMidiListener;
159    }
160    
161    void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
162        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
163    }
164    
165    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
166        pChannel->AddEngineChangeListener(this);
167    }
168    
169    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
170        if (!pChannel->GetEngineChannel()) return;
171        EngineToBeChanged(pChannel->Index());
172    }
173    
174    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
175        SamplerChannel* pSamplerChannel =
176            pParent->pSampler->GetSamplerChannel(ChannelId);
177        if (!pSamplerChannel) return;
178        EngineChannel* pEngineChannel =
179            pSamplerChannel->GetEngineChannel();
180        if (!pEngineChannel) return;
181        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
182            if ((*iter).pEngineChannel == pEngineChannel) {
183                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
184                pEngineChannel->Disconnect(pMidiListener);
185                channelMidiListeners.erase(iter);
186                delete pMidiListener;
187                return;
188            }
189        }
190    }
191    
192    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
193        SamplerChannel* pSamplerChannel =
194            pParent->pSampler->GetSamplerChannel(ChannelId);
195        if (!pSamplerChannel) return;
196        EngineChannel* pEngineChannel =
197            pSamplerChannel->GetEngineChannel();
198        if (!pEngineChannel) return;
199        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
200        pEngineChannel->Connect(pMidiListener);
201        midi_listener_entry entry = {
202            pSamplerChannel, pEngineChannel, pMidiListener
203        };
204        channelMidiListeners.push_back(entry);
205    }
206    
207    void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
208        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
209  }  }
210    
211    void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
212        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
213    }
214    
215    void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
216        pDevice->RemoveMidiPortCountListener(this);
217        for (int i = 0; i < pDevice->PortCount(); ++i)
218            MidiPortToBeRemoved(pDevice->GetPort(i));
219    }
220    
221    void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
222        pDevice->AddMidiPortCountListener(this);
223        for (int i = 0; i < pDevice->PortCount(); ++i)
224            MidiPortAdded(pDevice->GetPort(i));
225    }
226    
227    void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
228        // yet unused
229    }
230    
231    void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
232        for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
233            if ((*iter).pPort == pPort) {
234                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
235                pPort->Disconnect(pMidiListener);
236                deviceMidiListeners.erase(iter);
237                delete pMidiListener;
238                return;
239            }
240        }
241    }
242    
243    void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
244        // find out the device ID
245        std::map<uint, MidiInputDevice*> devices =
246            pParent->pSampler->GetMidiInputDevices();
247        for (
248            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
249            iter != devices.end(); ++iter
250        ) {
251            if (iter->second == pPort->GetDevice()) { // found
252                VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
253                pPort->Connect(pMidiListener);
254                device_midi_listener_entry entry = {
255                    pPort, pMidiListener, iter->first
256                };
257                deviceMidiListeners.push_back(entry);
258                return;
259            }
260        }
261    }
262    
263    void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
264        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
265    }
266    
267    void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
268        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
269    }
270    
271    void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
272        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
273    }
274    
275    void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
276        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
277    }
278    
279    void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
280        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
281    }
282    
283    void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
284        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
285    }
286    
287    void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
288        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
289    }
290    
291    void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
292        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
293    }
294    
295    void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
296        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
297    }
298    
299    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
300        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
301    }
302    
303    #if HAVE_SQLITE3
304    void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
305        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
306    }
307    
308    void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
309        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
310    }
311    
312    void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
313        Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
314        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
315        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
316    }
317    
318    void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
319        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
320    }
321    
322    void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
323        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
324    }
325    
326    void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
327        Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
328        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
329        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
330    }
331    
332    void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
333        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
334    }
335    #endif // HAVE_SQLITE3
336    
337    
338  /**  /**
339   * Blocks the calling thread until the LSCP Server is initialized and   * Blocks the calling thread until the LSCP Server is initialized and
340   * accepting socket connections, if the server is already initialized then   * accepting socket connections, if the server is already initialized then
# Line 103  int LSCPServer::WaitUntilInitialized(lon Line 350  int LSCPServer::WaitUntilInitialized(lon
350  }  }
351    
352  int LSCPServer::Main() {  int LSCPServer::Main() {
353            #if defined(WIN32)
354            WSADATA wsaData;
355            int iResult;
356            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
357            if (iResult != 0) {
358                    std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
359                    exit(EXIT_FAILURE);
360            }
361            #endif
362      hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
363      if (hSocket < 0) {      if (hSocket < 0) {
364          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 372  int LSCPServer::Main() {
372              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
373                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
374                      std::cerr << "gave up!" << std::endl;                      std::cerr << "gave up!" << std::endl;
375                        #if defined(WIN32)
376                        closesocket(hSocket);
377                        #else
378                      close(hSocket);                      close(hSocket);
379                        #endif
380                      //return -1;                      //return -1;
381                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
382                  }                  }
# Line 129  int LSCPServer::Main() { Line 389  int LSCPServer::Main() {
389      listen(hSocket, 1);      listen(hSocket, 1);
390      Initialized.Set(true);      Initialized.Set(true);
391    
392        // Registering event listeners
393        pSampler->AddChannelCountListener(&eventHandler);
394        pSampler->AddAudioDeviceCountListener(&eventHandler);
395        pSampler->AddMidiDeviceCountListener(&eventHandler);
396        pSampler->AddVoiceCountListener(&eventHandler);
397        pSampler->AddStreamCountListener(&eventHandler);
398        pSampler->AddBufferFillListener(&eventHandler);
399        pSampler->AddTotalStreamCountListener(&eventHandler);
400        pSampler->AddTotalVoiceCountListener(&eventHandler);
401        pSampler->AddFxSendCountListener(&eventHandler);
402        MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
403        MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
404        MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
405        MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
406    #if HAVE_SQLITE3
407        InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
408    #endif
409      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
410      sockaddr_in client;      sockaddr_in client;
411      int length = sizeof(client);      int length = sizeof(client);
# Line 139  int LSCPServer::Main() { Line 416  int LSCPServer::Main() {
416      timeval timeout;      timeval timeout;
417    
418      while (true) {      while (true) {
419            #if CONFIG_PTHREAD_TESTCANCEL
420                    TestCancel();
421            #endif
422          // 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
423          {          {
424              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
# Line 146  int LSCPServer::Main() { Line 426  int LSCPServer::Main() {
426              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
427              for (; itEngineChannel != itEnd; ++itEngineChannel) {              for (; itEngineChannel != itEnd; ++itEngineChannel) {
428                  if ((*itEngineChannel)->StatusChanged()) {                  if ((*itEngineChannel)->StatusChanged()) {
429                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index()));
430                    }
431    
432                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
433                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
434                        if(fxs != NULL && fxs->IsInfoChanged()) {
435                            int chn = (*itEngineChannel)->GetSamplerChannel()->Index();
436                            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
437                            fxs->SetInfoChanged(false);
438                        }
439                    }
440                }
441            }
442    
443            // check if MIDI data arrived on some engine channel
444            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
445                const EventHandler::midi_listener_entry entry =
446                    eventHandler.channelMidiListeners[i];
447                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
448                if (pMidiListener->NotesChanged()) {
449                    for (int iNote = 0; iNote < 128; iNote++) {
450                        if (pMidiListener->NoteChanged(iNote)) {
451                            const bool bActive = pMidiListener->NoteIsActive(iNote);
452                            LSCPServer::SendLSCPNotify(
453                                LSCPEvent(
454                                    LSCPEvent::event_channel_midi,
455                                    entry.pSamplerChannel->Index(),
456                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
457                                    iNote,
458                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
459                                            : pMidiListener->NoteOffVelocity(iNote)
460                                )
461                            );
462                        }
463                    }
464                }
465            }
466    
467            // check if MIDI data arrived on some MIDI device
468            for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
469                const EventHandler::device_midi_listener_entry entry =
470                    eventHandler.deviceMidiListeners[i];
471                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
472                if (pMidiListener->NotesChanged()) {
473                    for (int iNote = 0; iNote < 128; iNote++) {
474                        if (pMidiListener->NoteChanged(iNote)) {
475                            const bool bActive = pMidiListener->NoteIsActive(iNote);
476                            LSCPServer::SendLSCPNotify(
477                                LSCPEvent(
478                                    LSCPEvent::event_device_midi,
479                                    entry.uiDeviceID,
480                                    entry.pPort->GetPortNumber(),
481                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
482                                    iNote,
483                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
484                                            : pMidiListener->NoteOffVelocity(iNote)
485                                )
486                            );
487                        }
488                  }                  }
489              }              }
490          }          }
# Line 169  int LSCPServer::Main() { Line 507  int LSCPServer::Main() {
507    
508          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
509    
510          if (retval == 0)          if (retval == 0 || (retval == -1 && errno == EINTR))
511                  continue; //Nothing try again                  continue; //Nothing try again
512          if (retval == -1) {          if (retval == -1) {
513                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
514                    #if defined(WIN32)
515                    closesocket(hSocket);
516                    #else
517                  close(hSocket);                  close(hSocket);
518                    #endif
519                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
520          }          }
521    
# Line 185  int LSCPServer::Main() { Line 527  int LSCPServer::Main() {
527                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
528                  }                  }
529    
530                    #if defined(WIN32)
531                    u_long nonblock_io = 1;
532                    if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
533                      std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
534                      exit(EXIT_FAILURE);
535                    }
536            #else
537                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
538                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
539                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
540                  }                  }
541                    #endif
542    
543                  // Parser initialization                  // Parser initialization
544                  yyparse_param_t yyparse_param;                  yyparse_param_t yyparse_param;
# Line 212  int LSCPServer::Main() { Line 562  int LSCPServer::Main() {
562                                  int dummy; // just a temporary hack to fulfill the restart() function prototype                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
563                                  restart(NULL, dummy); // restart the 'scanner'                                  restart(NULL, dummy); // restart the 'scanner'
564                                  currentSocket = (*iter).hSession;  //a hack                                  currentSocket = (*iter).hSession;  //a hack
565                                    itCurrentSession = iter; // another hack
566                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
567                                  if ((*iter).bVerbose) { // if echo mode enabled                                  if ((*iter).bVerbose) { // if echo mode enabled
568                                      AnswerClient(bufferedCommands[currentSocket]);                                      AnswerClient(bufferedCommands[currentSocket]);
569                                  }                                  }
570                                  int result = yyparse(&(*iter));                                  int result = yyparse(&(*iter));
571                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
572                                    itCurrentSession = Sessions.end(); // hack as well
573                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
574                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?
575                                          CloseConnection(iter);                                          CloseConnection(iter);
# Line 245  void LSCPServer::CloseConnection( std::v Line 597  void LSCPServer::CloseConnection( std::v
597          NotifyMutex.Lock();          NotifyMutex.Lock();
598          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
599          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
600            #if defined(WIN32)
601            closesocket(socket);
602            #else
603          close(socket);          close(socket);
604            #endif
605          NotifyMutex.Unlock();          NotifyMutex.Unlock();
606  }  }
607    
608    void LSCPServer::LockRTNotify() {
609        RTNotifyMutex.Lock();
610    }
611    
612    void LSCPServer::UnlockRTNotify() {
613        RTNotifyMutex.Unlock();
614    }
615    
616  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
617          int subs = 0;          int subs = 0;
618          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 310  extern int GetLSCPCommand( void *buf, in Line 674  extern int GetLSCPCommand( void *buf, in
674          return command.size();          return command.size();
675  }  }
676    
677    extern yyparse_param_t* GetCurrentYaccSession() {
678        return &(*itCurrentSession);
679    }
680    
681  /**  /**
682   * Will be called to try to read the command from the socket   * Will be called to try to read the command from the socket
683   * 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 688  bool LSCPServer::GetLSCPCommand( std::ve
688          char c;          char c;
689          int i = 0;          int i = 0;
690          while (true) {          while (true) {
691                    #if defined(WIN32)
692                    int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
693                    #else
694                  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
695                    #endif
696                  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
697                          CloseConnection(iter);                          CloseConnection(iter);
698                          break;                          break;
# Line 335  bool LSCPServer::GetLSCPCommand( std::ve Line 707  bool LSCPServer::GetLSCPCommand( std::ve
707                          }                          }
708                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
709                  }                  }
710                    #if defined(WIN32)
711                    if (result == SOCKET_ERROR) {
712                        int wsa_lasterror = WSAGetLastError();
713                            if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
714                                    return false;
715                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
716                            CloseConnection(iter);
717                            break;
718                    }
719                    #else
720                  if (result == -1) {                  if (result == -1) {
721                          if (errno == EAGAIN) //Would block, try again later.                          if (errno == EAGAIN) //Would block, try again later.
722                                  return false;                                  return false;
# Line 373  bool LSCPServer::GetLSCPCommand( std::ve Line 755  bool LSCPServer::GetLSCPCommand( std::ve
755                          CloseConnection(iter);                          CloseConnection(iter);
756                          break;                          break;
757                  }                  }
758                    #endif
759          }          }
760          return false;          return false;
761  }  }
# Line 490  String LSCPServer::DestroyMidiInputDevic Line 873  String LSCPServer::DestroyMidiInputDevic
873      return result.Produce();      return result.Produce();
874  }  }
875    
876    EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
877        SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
878        if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
879    
880        EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
881        if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
882    
883        return pEngineChannel;
884    }
885    
886  /**  /**
887   * Will be called by the parser to load an instrument.   * Will be called by the parser to load an instrument.
888   */   */
# Line 636  String LSCPServer::GetEngineInfo(String Line 1029  String LSCPServer::GetEngineInfo(String
1029      LockRTNotify();      LockRTNotify();
1030      try {      try {
1031          Engine* pEngine = EngineFactory::Create(EngineName);          Engine* pEngine = EngineFactory::Create(EngineName);
1032          result.Add("DESCRIPTION", pEngine->Description());          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1033          result.Add("VERSION",     pEngine->Version());          result.Add("VERSION",     pEngine->Version());
1034          EngineFactory::Destroy(pEngine);          EngineFactory::Destroy(pEngine);
1035      }      }
# Line 670  String LSCPServer::GetChannelInfo(uint u Line 1063  String LSCPServer::GetChannelInfo(uint u
1063          String AudioRouting;          String AudioRouting;
1064          int Mute = 0;          int Mute = 0;
1065          bool Solo = false;          bool Solo = false;
1066          String MidiInstrumentMap;          String MidiInstrumentMap = "NONE";
1067    
1068          if (pEngineChannel) {          if (pEngineChannel) {
1069              EngineName          = pEngineChannel->EngineName();              EngineName          = pEngineChannel->EngineName();
# Line 709  String LSCPServer::GetChannelInfo(uint u Line 1102  String LSCPServer::GetChannelInfo(uint u
1102          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
1103          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
1104    
1105            // convert the filename into the correct encoding as defined for LSCP
1106            // (especially in terms of special characters -> escape sequences)
1107            if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
1108    #if WIN32
1109                InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
1110    #else
1111                // assuming POSIX
1112                InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
1113    #endif
1114            }
1115    
1116          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
1117          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
1118          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
1119          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
1120          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
1121          result.Add("SOLO", Solo);          result.Add("SOLO", Solo);
# Line 731  String LSCPServer::GetVoiceCount(uint ui Line 1135  String LSCPServer::GetVoiceCount(uint ui
1135      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
1136      LSCPResultSet result;      LSCPResultSet result;
1137      try {      try {
1138          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");  
1139          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");
1140          result.Add(pEngineChannel->GetEngine()->VoiceCount());          result.Add(pEngineChannel->GetEngine()->VoiceCount());
1141      }      }
# Line 752  String LSCPServer::GetStreamCount(uint u Line 1153  String LSCPServer::GetStreamCount(uint u
1153      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
1154      LSCPResultSet result;      LSCPResultSet result;
1155      try {      try {
1156          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");  
1157          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");
1158          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
1159      }      }
# Line 773  String LSCPServer::GetBufferFill(fill_re Line 1171  String LSCPServer::GetBufferFill(fill_re
1171      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, 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 type assigned to 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          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
1177          else {          else {
# Line 1454  String LSCPServer::SetVolume(double dVol Line 1849  String LSCPServer::SetVolume(double dVol
1849      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1850      LSCPResultSet result;      LSCPResultSet result;
1851      try {      try {
1852          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");  
1853          pEngineChannel->Volume(dVolume);          pEngineChannel->Volume(dVolume);
1854      }      }
1855      catch (Exception e) {      catch (Exception e) {
# Line 1473  String LSCPServer::SetChannelMute(bool b Line 1865  String LSCPServer::SetChannelMute(bool b
1865      dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));      dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1866      LSCPResultSet result;      LSCPResultSet result;
1867      try {      try {
1868          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");  
1869    
1870          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1871          else pEngineChannel->SetMute(1);          else pEngineChannel->SetMute(1);
# Line 1494  String LSCPServer::SetChannelSolo(bool b Line 1882  String LSCPServer::SetChannelSolo(bool b
1882      dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));      dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1883      LSCPResultSet result;      LSCPResultSet result;
1884      try {      try {
1885          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");  
1886    
1887          bool oldSolo = pEngineChannel->GetSolo();          bool oldSolo = pEngineChannel->GetSolo();
1888          bool hadSoloChannel = HasSoloChannel();          bool hadSoloChannel = HasSoloChannel();
1889            
1890          pEngineChannel->SetSolo(bSolo);          pEngineChannel->SetSolo(bSolo);
1891            
1892          if(!oldSolo && bSolo) {          if(!oldSolo && bSolo) {
1893              if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);              if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1894              if(!hadSoloChannel) MuteNonSoloChannels();              if(!hadSoloChannel) MuteNonSoloChannels();
1895          }          }
1896            
1897          if(oldSolo && !bSolo) {          if(oldSolo && !bSolo) {
1898              if(!HasSoloChannel()) UnmuteChannels();              if(!HasSoloChannel()) UnmuteChannels();
1899              else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);              else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
# Line 1567  void  LSCPServer::UnmuteChannels() { Line 1951  void  LSCPServer::UnmuteChannels() {
1951      }      }
1952  }  }
1953    
1954  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) {
1955      dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));      dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1956    
1957      midi_prog_index_t idx;      midi_prog_index_t idx;
# Line 1585  String LSCPServer::AddOrReplaceMIDIInstr Line 1969  String LSCPServer::AddOrReplaceMIDIInstr
1969    
1970      LSCPResultSet result;      LSCPResultSet result;
1971      try {      try {
1972          // PERSISTENT mapping commands might bloock for a long time, so in          // PERSISTENT mapping commands might block for a long time, so in
1973          // that case we add/replace the mapping in another thread          // that case we add/replace the mapping in another thread in case
1974          bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT);          // the NON_MODAL argument was supplied, non persistent mappings
1975            // should return immediately, so we don't need to do that for them
1976            bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
1977          MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);          MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
1978      } catch (Exception e) {      } catch (Exception e) {
1979          result.Error(e);          result.Error(e);
# Line 1616  String LSCPServer::GetMidiInstrumentMapp Line 2002  String LSCPServer::GetMidiInstrumentMapp
2002      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2003      LSCPResultSet result;      LSCPResultSet result;
2004      try {      try {
2005          result.Add(MidiInstrumentMapper::Entries(MidiMapID).size());          result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID));
2006      } catch (Exception e) {      } catch (Exception e) {
2007          result.Error(e);          result.Error(e);
2008      }      }
# Line 1627  String LSCPServer::GetMidiInstrumentMapp Line 2013  String LSCPServer::GetMidiInstrumentMapp
2013  String LSCPServer::GetAllMidiInstrumentMappings() {  String LSCPServer::GetAllMidiInstrumentMappings() {
2014      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2015      LSCPResultSet result;      LSCPResultSet result;
2016      std::vector<int> maps = MidiInstrumentMapper::Maps();      try {
2017      int totalMappings = 0;          result.Add(MidiInstrumentMapper::GetInstrumentCount());
2018      for (int i = 0; i < maps.size(); i++) {      } catch (Exception e) {
2019          try {          result.Error(e);
             totalMappings += MidiInstrumentMapper::Entries(maps[i]).size();  
         } catch (Exception e) { /*NOOP*/ }  
2020      }      }
     result.Add(totalMappings);  
2021      return result.Produce();      return result.Produce();
2022  }  }
2023    
# Line 1642  String LSCPServer::GetMidiInstrumentMapp Line 2025  String LSCPServer::GetMidiInstrumentMapp
2025      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2026      LSCPResultSet result;      LSCPResultSet result;
2027      try {      try {
2028          midi_prog_index_t idx;          MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg);
2029          idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;          // convert the filename into the correct encoding as defined for LSCP
2030          idx.midi_bank_lsb = MidiBank & 0x7f;          // (especially in terms of special characters -> escape sequences)
2031          idx.midi_prog     = MidiProg;  #if WIN32
2032            const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp();
2033    #else
2034            // assuming POSIX
2035            const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp();
2036    #endif
2037    
2038          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);          result.Add("NAME", _escapeLscpResponse(entry.Name));
2039          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);          result.Add("ENGINE_NAME", entry.EngineName);
2040          if (iter == mappings.end()) result.Error("there is no map entry with that index");          result.Add("INSTRUMENT_FILE", instrumentFileName);
2041          else { // found          result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex);
2042              result.Add("NAME", iter->second.Name);          String instrumentName;
2043              result.Add("ENGINE_NAME", iter->second.EngineName);          Engine* pEngine = EngineFactory::Create(entry.EngineName);
2044              result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile);          if (pEngine) {
2045              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);              if (pEngine->GetInstrumentManager()) {
2046              String instrumentName;                  InstrumentManager::instrument_id_t instrID;
2047              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);                  instrID.FileName = entry.InstrumentFile;
2048              if (pEngine) {                  instrID.Index    = entry.InstrumentIndex;
2049                  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);  
             }  
             result.Add("INSTRUMENT_NAME", instrumentName);  
             switch (iter->second.LoadMode) {  
                 case MidiInstrumentMapper::ON_DEMAND:  
                     result.Add("LOAD_MODE", "ON_DEMAND");  
                     break;  
                 case MidiInstrumentMapper::ON_DEMAND_HOLD:  
                     result.Add("LOAD_MODE", "ON_DEMAND_HOLD");  
                     break;  
                 case MidiInstrumentMapper::PERSISTENT:  
                     result.Add("LOAD_MODE", "PERSISTENT");  
                     break;  
                 default:  
                     throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");  
2050              }              }
2051              result.Add("VOLUME", iter->second.Volume);              EngineFactory::Destroy(pEngine);
2052            }
2053            result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2054            switch (entry.LoadMode) {
2055                case MidiInstrumentMapper::ON_DEMAND:
2056                    result.Add("LOAD_MODE", "ON_DEMAND");
2057                    break;
2058                case MidiInstrumentMapper::ON_DEMAND_HOLD:
2059                    result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2060                    break;
2061                case MidiInstrumentMapper::PERSISTENT:
2062                    result.Add("LOAD_MODE", "PERSISTENT");
2063                    break;
2064                default:
2065                    throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2066          }          }
2067            result.Add("VOLUME", entry.Volume);
2068      } catch (Exception e) {      } catch (Exception e) {
2069          result.Error(e);          result.Error(e);
2070      }      }
# Line 1720  String LSCPServer::ListAllMidiInstrument Line 2103  String LSCPServer::ListAllMidiInstrument
2103              for (; iter != mappings.end(); iter++) {              for (; iter != mappings.end(); iter++) {
2104                  if (s.size()) s += ",";                  if (s.size()) s += ",";
2105                  s += "{" + ToString(maps[i]) + ","                  s += "{" + ToString(maps[i]) + ","
2106                           + 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)) + ","
2107                           + ToString(int(iter->first.midi_prog)) + "}";                           + ToString(int(iter->first.midi_prog)) + "}";
2108              }              }
2109          }          }
# Line 1821  String LSCPServer::GetMidiInstrumentMap( Line 2204  String LSCPServer::GetMidiInstrumentMap(
2204      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2205      LSCPResultSet result;      LSCPResultSet result;
2206      try {      try {
2207          result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID));          result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2208            result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2209      } catch (Exception e) {      } catch (Exception e) {
2210          result.Error(e);          result.Error(e);
2211      }      }
# Line 1851  String LSCPServer::SetChannelMap(uint ui Line 2235  String LSCPServer::SetChannelMap(uint ui
2235      dmsg(2,("LSCPServer: SetChannelMap()\n"));      dmsg(2,("LSCPServer: SetChannelMap()\n"));
2236      LSCPResultSet result;      LSCPResultSet result;
2237      try {      try {
2238          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");  
2239    
2240          if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();          if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2241          else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();          else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
# Line 1870  String LSCPServer::CreateFxSend(uint uiS Line 2250  String LSCPServer::CreateFxSend(uint uiS
2250      dmsg(2,("LSCPServer: CreateFxSend()\n"));      dmsg(2,("LSCPServer: CreateFxSend()\n"));
2251      LSCPResultSet result;      LSCPResultSet result;
2252      try {      try {
2253          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");  
2254    
2255          FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);          FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2256          if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");          if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
# Line 1890  String LSCPServer::DestroyFxSend(uint ui Line 2266  String LSCPServer::DestroyFxSend(uint ui
2266      dmsg(2,("LSCPServer: DestroyFxSend()\n"));      dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2267      LSCPResultSet result;      LSCPResultSet result;
2268      try {      try {
2269          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");  
2270    
2271          FxSend* pFxSend = NULL;          FxSend* pFxSend = NULL;
2272          for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {          for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
# Line 1915  String LSCPServer::GetFxSends(uint uiSam Line 2287  String LSCPServer::GetFxSends(uint uiSam
2287      dmsg(2,("LSCPServer: GetFxSends()\n"));      dmsg(2,("LSCPServer: GetFxSends()\n"));
2288      LSCPResultSet result;      LSCPResultSet result;
2289      try {      try {
2290          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");  
2291    
2292          result.Add(pEngineChannel->GetFxSendCount());          result.Add(pEngineChannel->GetFxSendCount());
2293      } catch (Exception e) {      } catch (Exception e) {
# Line 1933  String LSCPServer::ListFxSends(uint uiSa Line 2301  String LSCPServer::ListFxSends(uint uiSa
2301      LSCPResultSet result;      LSCPResultSet result;
2302      String list;      String list;
2303      try {      try {
2304          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");  
2305    
2306          for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {          for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2307              FxSend* pFxSend = pEngineChannel->GetFxSend(i);              FxSend* pFxSend = pEngineChannel->GetFxSend(i);
# Line 1951  String LSCPServer::ListFxSends(uint uiSa Line 2315  String LSCPServer::ListFxSends(uint uiSa
2315      return result.Produce();      return result.Produce();
2316  }  }
2317    
2318    FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2319        EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2320    
2321        FxSend* pFxSend = NULL;
2322        for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2323            if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2324                pFxSend = pEngineChannel->GetFxSend(i);
2325                break;
2326            }
2327        }
2328        if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2329        return pFxSend;
2330    }
2331    
2332  String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {  String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2333      dmsg(2,("LSCPServer: GetFxSendInfo()\n"));      dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2334      LSCPResultSet result;      LSCPResultSet result;
2335      try {      try {
2336          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2337          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));          FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
   
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");  
   
         FxSend* pFxSend = NULL;  
         for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {  
             if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {  
                 pFxSend = pEngineChannel->GetFxSend(i);  
                 break;  
             }  
         }  
         if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");  
2338    
2339          // gather audio routing informations          // gather audio routing informations
2340          String AudioRouting;          String AudioRouting;
# Line 1978  String LSCPServer::GetFxSendInfo(uint ui Line 2344  String LSCPServer::GetFxSendInfo(uint ui
2344          }          }
2345    
2346          // success          // success
2347          result.Add("NAME", pFxSend->Name());          result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2348            result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2349            result.Add("LEVEL", ToString(pFxSend->Level()));
2350          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2351      } catch (Exception e) {      } catch (Exception e) {
2352          result.Error(e);          result.Error(e);
# Line 1986  String LSCPServer::GetFxSendInfo(uint ui Line 2354  String LSCPServer::GetFxSendInfo(uint ui
2354      return result.Produce();      return result.Produce();
2355  }  }
2356    
2357    String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2358        dmsg(2,("LSCPServer: SetFxSendName()\n"));
2359        LSCPResultSet result;
2360        try {
2361            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2362    
2363            pFxSend->SetName(Name);
2364            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2365        } catch (Exception e) {
2366            result.Error(e);
2367        }
2368        return result.Produce();
2369    }
2370    
2371  String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {  String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2372      dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));      dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2373      LSCPResultSet result;      LSCPResultSet result;
2374      try {      try {
2375          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
2376    
2377          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2378          if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2379        } catch (Exception e) {
2380            result.Error(e);
2381        }
2382        return result.Produce();
2383    }
2384    
2385          FxSend* pFxSend = NULL;  String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2386          for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {      dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2387              if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {      LSCPResultSet result;
2388                  pFxSend = pEngineChannel->GetFxSend(i);      try {
2389            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2390    
2391            pFxSend->SetMidiController(MidiController);
2392            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2393        } catch (Exception e) {
2394            result.Error(e);
2395        }
2396        return result.Produce();
2397    }
2398    
2399    String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2400        dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2401        LSCPResultSet result;
2402        try {
2403            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2404    
2405            pFxSend->SetLevel((float)dLevel);
2406            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2407        } catch (Exception e) {
2408            result.Error(e);
2409        }
2410        return result.Produce();
2411    }
2412    
2413    String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2414        dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2415        LSCPResultSet result;
2416        try {
2417            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2418            if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2419            Engine* pEngine = pEngineChannel->GetEngine();
2420            InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2421            if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2422            InstrumentManager::instrument_id_t instrumentID;
2423            instrumentID.FileName = pEngineChannel->InstrumentFileName();
2424            instrumentID.Index    = pEngineChannel->InstrumentIndex();
2425            pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2426        } catch (Exception e) {
2427            result.Error(e);
2428        }
2429        return result.Produce();
2430    }
2431    
2432    String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) {
2433        dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2));
2434        LSCPResultSet result;
2435        try {
2436            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2437    
2438            if (Arg1 > 127 || Arg2 > 127) {
2439                throw Exception("Invalid MIDI message");
2440            }
2441    
2442            VirtualMidiDevice* pMidiDevice = NULL;
2443            std::vector<EventHandler::midi_listener_entry>::iterator iter = eventHandler.channelMidiListeners.begin();
2444            for (; iter != eventHandler.channelMidiListeners.end(); ++iter) {
2445                if ((*iter).pEngineChannel == pEngineChannel) {
2446                    pMidiDevice = (*iter).pMidiListener;
2447                  break;                  break;
2448              }              }
2449          }          }
2450          if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");          
2451            if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device");
2452    
2453          pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);          if (MidiMsg == "NOTE_ON") {
2454                pMidiDevice->SendNoteOnToDevice(Arg1, Arg2);
2455                bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2);
2456                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2457            } else if (MidiMsg == "NOTE_OFF") {
2458                pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);
2459                bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);
2460                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2461            } else {
2462                throw Exception("Unknown MIDI message type: " + MidiMsg);
2463            }
2464      } catch (Exception e) {      } catch (Exception e) {
2465          result.Error(e);          result.Error(e);
2466      }      }
# Line 2019  String LSCPServer::ResetChannel(uint uiS Line 2474  String LSCPServer::ResetChannel(uint uiS
2474      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
2475      LSCPResultSet result;      LSCPResultSet result;
2476      try {      try {
2477          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");  
2478          pEngineChannel->Reset();          pEngineChannel->Reset();
2479      }      }
2480      catch (Exception e) {      catch (Exception e) {
# Line 2047  String LSCPServer::ResetSampler() { Line 2499  String LSCPServer::ResetSampler() {
2499   */   */
2500  String LSCPServer::GetServerInfo() {  String LSCPServer::GetServerInfo() {
2501      dmsg(2,("LSCPServer: GetServerInfo()\n"));      dmsg(2,("LSCPServer: GetServerInfo()\n"));
2502        const std::string description =
2503            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2504      LSCPResultSet result;      LSCPResultSet result;
2505      result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");      result.Add("DESCRIPTION", description);
2506      result.Add("VERSION", VERSION);      result.Add("VERSION", VERSION);
2507      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2508    #if HAVE_SQLITE3
2509        result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2510    #else
2511        result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2512    #endif
2513    
2514        return result.Produce();
2515    }
2516    
2517    /**
2518     * Will be called by the parser to return the current number of all active streams.
2519     */
2520    String LSCPServer::GetTotalStreamCount() {
2521        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2522        LSCPResultSet result;
2523        result.Add(pSampler->GetDiskStreamCount());
2524      return result.Produce();      return result.Produce();
2525  }  }
2526    
# Line 2070  String LSCPServer::GetTotalVoiceCount() Line 2540  String LSCPServer::GetTotalVoiceCount()
2540  String LSCPServer::GetTotalVoiceCountMax() {  String LSCPServer::GetTotalVoiceCountMax() {
2541      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
2542      LSCPResultSet result;      LSCPResultSet result;
2543      result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);      result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES);
2544        return result.Produce();
2545    }
2546    
2547    /**
2548     * Will be called by the parser to return the sampler global maximum
2549     * allowed number of voices.
2550     */
2551    String LSCPServer::GetGlobalMaxVoices() {
2552        dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));
2553        LSCPResultSet result;
2554        result.Add(GLOBAL_MAX_VOICES);
2555        return result.Produce();
2556    }
2557    
2558    /**
2559     * Will be called by the parser to set the sampler global maximum number of
2560     * voices.
2561     */
2562    String LSCPServer::SetGlobalMaxVoices(int iVoices) {
2563        dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));
2564        LSCPResultSet result;
2565        try {
2566            if (iVoices < 1) throw Exception("Maximum voices may not be less than 1");
2567            GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp
2568            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
2569            if (engines.size() > 0) {
2570                std::set<Engine*>::iterator iter = engines.begin();
2571                std::set<Engine*>::iterator end  = engines.end();
2572                for (; iter != end; ++iter) {
2573                    (*iter)->SetMaxVoices(iVoices);
2574                }
2575            }
2576            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES));
2577        } catch (Exception e) {
2578            result.Error(e);
2579        }
2580        return result.Produce();
2581    }
2582    
2583    /**
2584     * Will be called by the parser to return the sampler global maximum
2585     * allowed number of disk streams.
2586     */
2587    String LSCPServer::GetGlobalMaxStreams() {
2588        dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));
2589        LSCPResultSet result;
2590        result.Add(GLOBAL_MAX_STREAMS);
2591        return result.Produce();
2592    }
2593    
2594    /**
2595     * Will be called by the parser to set the sampler global maximum number of
2596     * disk streams.
2597     */
2598    String LSCPServer::SetGlobalMaxStreams(int iStreams) {
2599        dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));
2600        LSCPResultSet result;
2601        try {
2602            if (iStreams < 0) throw Exception("Maximum disk streams may not be negative");
2603            GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp
2604            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
2605            if (engines.size() > 0) {
2606                std::set<Engine*>::iterator iter = engines.begin();
2607                std::set<Engine*>::iterator end  = engines.end();
2608                for (; iter != end; ++iter) {
2609                    (*iter)->SetMaxDiskStreams(iStreams);
2610                }
2611            }
2612            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS));
2613        } catch (Exception e) {
2614            result.Error(e);
2615        }
2616      return result.Produce();      return result.Produce();
2617  }  }
2618    
# Line 2084  String LSCPServer::SetGlobalVolume(doubl Line 2626  String LSCPServer::SetGlobalVolume(doubl
2626      LSCPResultSet result;      LSCPResultSet result;
2627      try {      try {
2628          if (dVolume < 0) throw Exception("Volume may not be negative");          if (dVolume < 0) throw Exception("Volume may not be negative");
2629          GLOBAL_VOLUME = dVolume; // see common/global.cpp          GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
2630            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
2631      } catch (Exception e) {      } catch (Exception e) {
2632          result.Error(e);          result.Error(e);
2633      }      }
2634      return result.Produce();      return result.Produce();
2635  }  }
2636    
2637    String LSCPServer::GetFileInstruments(String Filename) {
2638        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2639        LSCPResultSet result;
2640        try {
2641            VerifyFile(Filename);
2642        } catch (Exception e) {
2643            result.Error(e);
2644            return result.Produce();
2645        }
2646        // try to find a sampler engine that can handle the file
2647        bool bFound = false;
2648        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2649        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2650            Engine* pEngine = NULL;
2651            try {
2652                pEngine = EngineFactory::Create(engineTypes[i]);
2653                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2654                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2655                if (pManager) {
2656                    std::vector<InstrumentManager::instrument_id_t> IDs =
2657                        pManager->GetInstrumentFileContent(Filename);
2658                    // return the amount of instruments in the file
2659                    result.Add(IDs.size());
2660                    // no more need to ask other engine types
2661                    bFound = true;
2662                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2663            } catch (Exception e) {
2664                // NOOP, as exception is thrown if engine doesn't support file
2665            }
2666            if (pEngine) EngineFactory::Destroy(pEngine);
2667        }
2668    
2669        if (!bFound) result.Error("Unknown file format");
2670        return result.Produce();
2671    }
2672    
2673    String LSCPServer::ListFileInstruments(String Filename) {
2674        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2675        LSCPResultSet result;
2676        try {
2677            VerifyFile(Filename);
2678        } catch (Exception e) {
2679            result.Error(e);
2680            return result.Produce();
2681        }
2682        // try to find a sampler engine that can handle the file
2683        bool bFound = false;
2684        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2685        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2686            Engine* pEngine = NULL;
2687            try {
2688                pEngine = EngineFactory::Create(engineTypes[i]);
2689                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2690                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2691                if (pManager) {
2692                    std::vector<InstrumentManager::instrument_id_t> IDs =
2693                        pManager->GetInstrumentFileContent(Filename);
2694                    // return a list of IDs of the instruments in the file
2695                    String s;
2696                    for (int j = 0; j < IDs.size(); j++) {
2697                        if (s.size()) s += ",";
2698                        s += ToString(IDs[j].Index);
2699                    }
2700                    result.Add(s);
2701                    // no more need to ask other engine types
2702                    bFound = true;
2703                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2704            } catch (Exception e) {
2705                // NOOP, as exception is thrown if engine doesn't support file
2706            }
2707            if (pEngine) EngineFactory::Destroy(pEngine);
2708        }
2709    
2710        if (!bFound) result.Error("Unknown file format");
2711        return result.Produce();
2712    }
2713    
2714    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2715        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2716        LSCPResultSet result;
2717        try {
2718            VerifyFile(Filename);
2719        } catch (Exception e) {
2720            result.Error(e);
2721            return result.Produce();
2722        }
2723        InstrumentManager::instrument_id_t id;
2724        id.FileName = Filename;
2725        id.Index    = InstrumentID;
2726        // try to find a sampler engine that can handle the file
2727        bool bFound = false;
2728        bool bFatalErr = false;
2729        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2730        for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
2731            Engine* pEngine = NULL;
2732            try {
2733                pEngine = EngineFactory::Create(engineTypes[i]);
2734                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2735                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2736                if (pManager) {
2737                    // check if the instrument index is valid
2738                    // FIXME: this won't work if an engine only supports parts of the instrument file
2739                    std::vector<InstrumentManager::instrument_id_t> IDs =
2740                        pManager->GetInstrumentFileContent(Filename);
2741                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2742                        std::stringstream ss;
2743                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2744                        bFatalErr = true;
2745                        throw Exception(ss.str());
2746                    }
2747                    // get the info of the requested instrument
2748                    InstrumentManager::instrument_info_t info =
2749                        pManager->GetInstrumentInfo(id);
2750                    // return detailed informations about the file
2751                    result.Add("NAME", info.InstrumentName);
2752                    result.Add("FORMAT_FAMILY", engineTypes[i]);
2753                    result.Add("FORMAT_VERSION", info.FormatVersion);
2754                    result.Add("PRODUCT", info.Product);
2755                    result.Add("ARTISTS", info.Artists);
2756    
2757                    std::stringstream ss;
2758                    bool b = false;
2759                    for (int i = 0; i < 128; i++) {
2760                        if (info.KeyBindings[i]) {
2761                            if (b) ss << ',';
2762                            ss << i; b = true;
2763                        }
2764                    }
2765                    result.Add("KEY_BINDINGS", ss.str());
2766    
2767                    b = false;
2768                    std::stringstream ss2;
2769                    for (int i = 0; i < 128; i++) {
2770                        if (info.KeySwitchBindings[i]) {
2771                            if (b) ss2 << ',';
2772                            ss2 << i; b = true;
2773                        }
2774                    }
2775                    result.Add("KEYSWITCH_BINDINGS", ss2.str());
2776                    // no more need to ask other engine types
2777                    bFound = true;
2778                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2779            } catch (Exception e) {
2780                // usually NOOP, as exception is thrown if engine doesn't support file
2781                if (bFatalErr) result.Error(e);
2782            }
2783            if (pEngine) EngineFactory::Destroy(pEngine);
2784        }
2785    
2786        if (!bFound && !bFatalErr) result.Error("Unknown file format");
2787        return result.Produce();
2788    }
2789    
2790    void LSCPServer::VerifyFile(String Filename) {
2791        #if WIN32
2792        WIN32_FIND_DATA win32FileAttributeData;
2793        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2794        if (!res) {
2795            std::stringstream ss;
2796            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2797            throw Exception(ss.str());
2798        }
2799        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2800            throw Exception("Directory is specified");
2801        }
2802        #else
2803        struct stat statBuf;
2804        int res = stat(Filename.c_str(), &statBuf);
2805        if (res) {
2806            std::stringstream ss;
2807            ss << "Fail to stat `" << Filename << "`: " << strerror(errno);
2808            throw Exception(ss.str());
2809        }
2810    
2811        if (S_ISDIR(statBuf.st_mode)) {
2812            throw Exception("Directory is specified");
2813        }
2814        #endif
2815    }
2816    
2817  /**  /**
2818   * 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
2819   * server for receiving event messages.   * server for receiving event messages.
# Line 2117  String LSCPServer::UnsubscribeNotificati Line 2840  String LSCPServer::UnsubscribeNotificati
2840      return result.Produce();      return result.Produce();
2841  }  }
2842    
2843  static int select_callback(void * lscpResultSet, int argc,  String LSCPServer::AddDbInstrumentDirectory(String Dir) {
2844                          char **argv, char **azColName)      dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
2845  {      LSCPResultSet result;
2846      LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;  #if HAVE_SQLITE3
2847      resultSet->Add(argc, argv);      try {
2848      return 0;          InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
2849        } catch (Exception e) {
2850             result.Error(e);
2851        }
2852    #else
2853        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2854    #endif
2855        return result.Produce();
2856    }
2857    
2858    String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
2859        dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
2860        LSCPResultSet result;
2861    #if HAVE_SQLITE3
2862        try {
2863            InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
2864        } catch (Exception e) {
2865             result.Error(e);
2866        }
2867    #else
2868        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2869    #endif
2870        return result.Produce();
2871    }
2872    
2873    String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
2874        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2875        LSCPResultSet result;
2876    #if HAVE_SQLITE3
2877        try {
2878            result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
2879        } catch (Exception e) {
2880             result.Error(e);
2881        }
2882    #else
2883        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2884    #endif
2885        return result.Produce();
2886    }
2887    
2888    String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
2889        dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2890        LSCPResultSet result;
2891    #if HAVE_SQLITE3
2892        try {
2893            String list;
2894            StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
2895    
2896            for (int i = 0; i < dirs->size(); i++) {
2897                if (list != "") list += ",";
2898                list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
2899            }
2900    
2901            result.Add(list);
2902        } catch (Exception e) {
2903             result.Error(e);
2904        }
2905    #else
2906        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2907    #endif
2908        return result.Produce();
2909    }
2910    
2911    String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
2912        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
2913        LSCPResultSet result;
2914    #if HAVE_SQLITE3
2915        try {
2916            DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2917    
2918            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2919            result.Add("CREATED", info.Created);
2920            result.Add("MODIFIED", info.Modified);
2921        } catch (Exception e) {
2922             result.Error(e);
2923        }
2924    #else
2925        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2926    #endif
2927        return result.Produce();
2928    }
2929    
2930    String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
2931        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
2932        LSCPResultSet result;
2933    #if HAVE_SQLITE3
2934        try {
2935            InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
2936        } catch (Exception e) {
2937             result.Error(e);
2938        }
2939    #else
2940        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2941    #endif
2942        return result.Produce();
2943    }
2944    
2945    String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
2946        dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2947        LSCPResultSet result;
2948    #if HAVE_SQLITE3
2949        try {
2950            InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
2951        } catch (Exception e) {
2952             result.Error(e);
2953        }
2954    #else
2955        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2956    #endif
2957        return result.Produce();
2958    }
2959    
2960    String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
2961        dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2962        LSCPResultSet result;
2963    #if HAVE_SQLITE3
2964        try {
2965            InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
2966        } catch (Exception e) {
2967             result.Error(e);
2968        }
2969    #else
2970        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2971    #endif
2972        return result.Produce();
2973    }
2974    
2975    String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
2976        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
2977        LSCPResultSet result;
2978    #if HAVE_SQLITE3
2979        try {
2980            InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
2981        } catch (Exception e) {
2982             result.Error(e);
2983        }
2984    #else
2985        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2986    #endif
2987        return result.Produce();
2988    }
2989    
2990    String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
2991        dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
2992        LSCPResultSet result;
2993    #if HAVE_SQLITE3
2994        try {
2995            int id;
2996            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2997            id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
2998            if (bBackground) result = id;
2999        } catch (Exception e) {
3000             result.Error(e);
3001        }
3002    #else
3003        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3004    #endif
3005        return result.Produce();
3006    }
3007    
3008    String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) {
3009        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));
3010        LSCPResultSet result;
3011    #if HAVE_SQLITE3
3012        try {
3013            int id;
3014            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3015            if (ScanMode.compare("RECURSIVE") == 0) {
3016                id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir);
3017            } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
3018                id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir);
3019            } else if (ScanMode.compare("FLAT") == 0) {
3020                id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir);
3021            } else {
3022                throw Exception("Unknown scan mode: " + ScanMode);
3023            }
3024    
3025            if (bBackground) result = id;
3026        } catch (Exception e) {
3027             result.Error(e);
3028        }
3029    #else
3030        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3031    #endif
3032        return result.Produce();
3033    }
3034    
3035    String LSCPServer::RemoveDbInstrument(String Instr) {
3036        dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
3037        LSCPResultSet result;
3038    #if HAVE_SQLITE3
3039        try {
3040            InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
3041        } catch (Exception e) {
3042             result.Error(e);
3043        }
3044    #else
3045        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3046    #endif
3047        return result.Produce();
3048    }
3049    
3050    String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
3051        dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3052        LSCPResultSet result;
3053    #if HAVE_SQLITE3
3054        try {
3055            result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
3056        } catch (Exception e) {
3057             result.Error(e);
3058        }
3059    #else
3060        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3061    #endif
3062        return result.Produce();
3063    }
3064    
3065    String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
3066        dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3067        LSCPResultSet result;
3068    #if HAVE_SQLITE3
3069        try {
3070            String list;
3071            StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
3072    
3073            for (int i = 0; i < instrs->size(); i++) {
3074                if (list != "") list += ",";
3075                list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
3076            }
3077    
3078            result.Add(list);
3079        } catch (Exception e) {
3080             result.Error(e);
3081        }
3082    #else
3083        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3084    #endif
3085        return result.Produce();
3086    }
3087    
3088    String LSCPServer::GetDbInstrumentInfo(String Instr) {
3089        dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
3090        LSCPResultSet result;
3091    #if HAVE_SQLITE3
3092        try {
3093            DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
3094    
3095            result.Add("INSTRUMENT_FILE", info.InstrFile);
3096            result.Add("INSTRUMENT_NR", info.InstrNr);
3097            result.Add("FORMAT_FAMILY", info.FormatFamily);
3098            result.Add("FORMAT_VERSION", info.FormatVersion);
3099            result.Add("SIZE", (int)info.Size);
3100            result.Add("CREATED", info.Created);
3101            result.Add("MODIFIED", info.Modified);
3102            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3103            result.Add("IS_DRUM", info.IsDrum);
3104            result.Add("PRODUCT", _escapeLscpResponse(info.Product));
3105            result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
3106            result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
3107        } catch (Exception e) {
3108             result.Error(e);
3109        }
3110    #else
3111        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3112    #endif
3113        return result.Produce();
3114    }
3115    
3116    String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
3117        dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
3118        LSCPResultSet result;
3119    #if HAVE_SQLITE3
3120        try {
3121            ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
3122    
3123            result.Add("FILES_TOTAL", job.FilesTotal);
3124            result.Add("FILES_SCANNED", job.FilesScanned);
3125            result.Add("SCANNING", job.Scanning);
3126            result.Add("STATUS", job.Status);
3127        } catch (Exception e) {
3128             result.Error(e);
3129        }
3130    #else
3131        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3132    #endif
3133        return result.Produce();
3134    }
3135    
3136    String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
3137        dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
3138        LSCPResultSet result;
3139    #if HAVE_SQLITE3
3140        try {
3141            InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
3142        } catch (Exception e) {
3143             result.Error(e);
3144        }
3145    #else
3146        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3147    #endif
3148        return result.Produce();
3149    }
3150    
3151    String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
3152        dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3153        LSCPResultSet result;
3154    #if HAVE_SQLITE3
3155        try {
3156            InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
3157        } catch (Exception e) {
3158             result.Error(e);
3159        }
3160    #else
3161        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3162    #endif
3163        return result.Produce();
3164    }
3165    
3166    String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
3167        dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3168        LSCPResultSet result;
3169    #if HAVE_SQLITE3
3170        try {
3171            InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
3172        } catch (Exception e) {
3173             result.Error(e);
3174        }
3175    #else
3176        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3177    #endif
3178        return result.Produce();
3179    }
3180    
3181    String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
3182        dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
3183        LSCPResultSet result;
3184    #if HAVE_SQLITE3
3185        try {
3186            InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
3187        } catch (Exception e) {
3188             result.Error(e);
3189        }
3190    #else
3191        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3192    #endif
3193        return result.Produce();
3194    }
3195    
3196    String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) {
3197        dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str()));
3198        LSCPResultSet result;
3199    #if HAVE_SQLITE3
3200        try {
3201            InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath);
3202        } catch (Exception e) {
3203             result.Error(e);
3204        }
3205    #else
3206        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3207    #endif
3208        return result.Produce();
3209    }
3210    
3211    String LSCPServer::FindLostDbInstrumentFiles() {
3212        dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n"));
3213        LSCPResultSet result;
3214    #if HAVE_SQLITE3
3215        try {
3216            String list;
3217            StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles();
3218    
3219            for (int i = 0; i < pLostFiles->size(); i++) {
3220                if (list != "") list += ",";
3221                list += "'" + pLostFiles->at(i) + "'";
3222            }
3223    
3224            result.Add(list);
3225        } catch (Exception e) {
3226             result.Error(e);
3227        }
3228    #else
3229        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3230    #endif
3231        return result.Produce();
3232    }
3233    
3234    String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
3235        dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
3236        LSCPResultSet result;
3237    #if HAVE_SQLITE3
3238        try {
3239            SearchQuery Query;
3240            std::map<String,String>::iterator iter;
3241            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3242                if (iter->first.compare("NAME") == 0) {
3243                    Query.Name = iter->second;
3244                } else if (iter->first.compare("CREATED") == 0) {
3245                    Query.SetCreated(iter->second);
3246                } else if (iter->first.compare("MODIFIED") == 0) {
3247                    Query.SetModified(iter->second);
3248                } else if (iter->first.compare("DESCRIPTION") == 0) {
3249                    Query.Description = iter->second;
3250                } else {
3251                    throw Exception("Unknown search criteria: " + iter->first);
3252                }
3253            }
3254    
3255            String list;
3256            StringListPtr pDirectories =
3257                InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
3258    
3259            for (int i = 0; i < pDirectories->size(); i++) {
3260                if (list != "") list += ",";
3261                list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
3262            }
3263    
3264            result.Add(list);
3265        } catch (Exception e) {
3266             result.Error(e);
3267        }
3268    #else
3269        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3270    #endif
3271        return result.Produce();
3272  }  }
3273    
3274  String LSCPServer::QueryDatabase(String query) {  String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
3275        dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
3276      LSCPResultSet result;      LSCPResultSet result;
3277  #if HAVE_SQLITE3  #if HAVE_SQLITE3
3278      char* zErrMsg = NULL;      try {
3279      sqlite3 *db;          SearchQuery Query;
3280      String selectStr = "SELECT " + query;          std::map<String,String>::iterator iter;
3281            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3282                if (iter->first.compare("NAME") == 0) {
3283                    Query.Name = iter->second;
3284                } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
3285                    Query.SetFormatFamilies(iter->second);
3286                } else if (iter->first.compare("SIZE") == 0) {
3287                    Query.SetSize(iter->second);
3288                } else if (iter->first.compare("CREATED") == 0) {
3289                    Query.SetCreated(iter->second);
3290                } else if (iter->first.compare("MODIFIED") == 0) {
3291                    Query.SetModified(iter->second);
3292                } else if (iter->first.compare("DESCRIPTION") == 0) {
3293                    Query.Description = iter->second;
3294                } else if (iter->first.compare("IS_DRUM") == 0) {
3295                    if (!strcasecmp(iter->second.c_str(), "true")) {
3296                        Query.InstrType = SearchQuery::DRUM;
3297                    } else {
3298                        Query.InstrType = SearchQuery::CHROMATIC;
3299                    }
3300                } else if (iter->first.compare("PRODUCT") == 0) {
3301                     Query.Product = iter->second;
3302                } else if (iter->first.compare("ARTISTS") == 0) {
3303                     Query.Artists = iter->second;
3304                } else if (iter->first.compare("KEYWORDS") == 0) {
3305                     Query.Keywords = iter->second;
3306                } else {
3307                    throw Exception("Unknown search criteria: " + iter->first);
3308                }
3309            }
3310    
3311      int rc = sqlite3_open("linuxsampler.db", &db);          String list;
3312      if (rc == SQLITE_OK)          StringListPtr pInstruments =
3313      {              InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
3314              rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);  
3315            for (int i = 0; i < pInstruments->size(); i++) {
3316                if (list != "") list += ",";
3317                list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
3318            }
3319    
3320            result.Add(list);
3321        } catch (Exception e) {
3322             result.Error(e);
3323      }      }
3324      if ( rc != SQLITE_OK )  #else
3325      {      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3326              result.Error(String(zErrMsg), rc);  #endif
3327        return result.Produce();
3328    }
3329    
3330    String LSCPServer::FormatInstrumentsDb() {
3331        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
3332        LSCPResultSet result;
3333    #if HAVE_SQLITE3
3334        try {
3335            InstrumentsDb::GetInstrumentsDb()->Format();
3336        } catch (Exception e) {
3337             result.Error(e);
3338      }      }
     sqlite3_close(db);  
3339  #else  #else
3340      result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3341  #endif  #endif
3342      return result.Produce();      return result.Produce();
3343  }  }
3344    
3345    
3346  /**  /**
3347   * 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
3348   * mode is enabled, all commands from the client will (immediately) be   * mode is enabled, all commands from the client will (immediately) be
# Line 2166  String LSCPServer::SetEcho(yyparse_param Line 3361  String LSCPServer::SetEcho(yyparse_param
3361      }      }
3362      return result.Produce();      return result.Produce();
3363  }  }
3364    
3365    }

Legend:
Removed from v.1007  
changed lines
  Added in v.1808

  ViewVC Help
Powered by ViewVC