/[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 778 by iliev, Fri Sep 23 06:58:26 2005 UTC revision 1724 by schoenebeck, Tue Apr 22 08:52:54 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 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    
26  #include "lscpserver.h"  #include "lscpserver.h"
27  #include "lscpresultset.h"  #include "lscpresultset.h"
28  #include "lscpevent.h"  #include "lscpevent.h"
 //#include "../common/global.h"  
29    
30    #if defined(WIN32)
31    #include <windows.h>
32    #else
33  #include <fcntl.h>  #include <fcntl.h>
34    #endif
35    
36  #if HAVE_SQLITE3  #if ! HAVE_SQLITE3
37  # include "sqlite3.h"  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
38  #endif  #endif
39    
40  #include "../engines/EngineFactory.h"  #include "../engines/EngineFactory.h"
# Line 37  Line 42 
42  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
43  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
44    
45    
46    /**
47     * Returns a copy of the given string where all special characters are
48     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
49     * to escape LSCP response fields in case the respective response field is
50     * actually defined as using escape sequences in the LSCP specs.
51     *
52     * @e Caution: DO NOT use this function for escaping path based responses,
53     * use the Path class (src/common/Path.h) for this instead!
54     */
55    static String _escapeLscpResponse(String txt) {
56        for (int i = 0; i < txt.length(); i++) {
57            const char c = txt.c_str()[i];
58            if (
59                !(c >= '0' && c <= '9') &&
60                !(c >= 'a' && c <= 'z') &&
61                !(c >= 'A' && c <= 'Z') &&
62                !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
63                !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
64                !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
65                !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
66                !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
67                !(c == '@') && !(c == '[') && !(c == ']') &&
68                !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
69                !(c == '|') && !(c == '}') && !(c == '~')
70            ) {
71                // convert the "special" character into a "\xHH" LSCP escape sequence
72                char buf[5];
73                snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
74                txt.replace(i, 1, buf);
75                i += 3;
76            }
77        }
78        return txt;
79    }
80    
81  /**  /**
82   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
83   * 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 94 
94  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
95  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
96  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
97    std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
98  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
99  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
100  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 103  Mutex LSCPServer::NotifyBufferMutex = Mu
103  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
104  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
105    
106  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) {
107      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
108      SocketAddress.sin_addr.s_addr = addr;      SocketAddress.sin_addr.s_addr = addr;
109      SocketAddress.sin_port        = port;      SocketAddress.sin_port        = port;
110      this->pSampler = pSampler;      this->pSampler = pSampler;
111        LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_count, "AUDIO_OUTPUT_DEVICE_COUNT");
112        LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_info, "AUDIO_OUTPUT_DEVICE_INFO");
113        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_count, "MIDI_INPUT_DEVICE_COUNT");
114        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_info, "MIDI_INPUT_DEVICE_INFO");
115      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
116      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
117      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
118      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
119      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
120        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
121        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
122        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
123        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
124        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
125        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
126        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
127        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
128        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
129        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
130        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
131      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
132        LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
133      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
134        LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
135        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
136        LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
137      hSocket = -1;      hSocket = -1;
138  }  }
139    
140  LSCPServer::~LSCPServer() {  LSCPServer::~LSCPServer() {
141    #if defined(WIN32)
142        if (hSocket >= 0) closesocket(hSocket);
143    #else
144      if (hSocket >= 0) close(hSocket);      if (hSocket >= 0) close(hSocket);
145    #endif
146    }
147    
148    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
149        this->pParent = pParent;
150    }
151    
152    LSCPServer::EventHandler::~EventHandler() {
153        std::vector<midi_listener_entry> l = channelMidiListeners;
154        channelMidiListeners.clear();
155        for (int i = 0; i < l.size(); i++)
156            delete l[i].pMidiListener;
157    }
158    
159    void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
160        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
161    }
162    
163    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
164        pChannel->AddEngineChangeListener(this);
165    }
166    
167    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
168        if (!pChannel->GetEngineChannel()) return;
169        EngineToBeChanged(pChannel->Index());
170    }
171    
172    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
173        SamplerChannel* pSamplerChannel =
174            pParent->pSampler->GetSamplerChannel(ChannelId);
175        if (!pSamplerChannel) return;
176        EngineChannel* pEngineChannel =
177            pSamplerChannel->GetEngineChannel();
178        if (!pEngineChannel) return;
179        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
180            if ((*iter).pEngineChannel == pEngineChannel) {
181                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
182                pEngineChannel->Disconnect(pMidiListener);
183                channelMidiListeners.erase(iter);
184                delete pMidiListener;
185                return;
186            }
187        }
188    }
189    
190    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
191        SamplerChannel* pSamplerChannel =
192            pParent->pSampler->GetSamplerChannel(ChannelId);
193        if (!pSamplerChannel) return;
194        EngineChannel* pEngineChannel =
195            pSamplerChannel->GetEngineChannel();
196        if (!pEngineChannel) return;
197        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
198        pEngineChannel->Connect(pMidiListener);
199        midi_listener_entry entry = {
200            pSamplerChannel, pEngineChannel, pMidiListener
201        };
202        channelMidiListeners.push_back(entry);
203    }
204    
205    void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
206        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
207    }
208    
209    void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
210        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
211    }
212    
213    void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
214        pDevice->RemoveMidiPortCountListener(this);
215        for (int i = 0; i < pDevice->PortCount(); ++i)
216            MidiPortToBeRemoved(pDevice->GetPort(i));
217    }
218    
219    void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
220        pDevice->AddMidiPortCountListener(this);
221        for (int i = 0; i < pDevice->PortCount(); ++i)
222            MidiPortAdded(pDevice->GetPort(i));
223    }
224    
225    void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
226        // yet unused
227    }
228    
229    void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
230        for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
231            if ((*iter).pPort == pPort) {
232                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
233                pPort->Disconnect(pMidiListener);
234                deviceMidiListeners.erase(iter);
235                delete pMidiListener;
236                return;
237            }
238        }
239    }
240    
241    void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
242        // find out the device ID
243        std::map<uint, MidiInputDevice*> devices =
244            pParent->pSampler->GetMidiInputDevices();
245        for (
246            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
247            iter != devices.end(); ++iter
248        ) {
249            if (iter->second == pPort->GetDevice()) { // found
250                VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
251                pPort->Connect(pMidiListener);
252                device_midi_listener_entry entry = {
253                    pPort, pMidiListener, iter->first
254                };
255                deviceMidiListeners.push_back(entry);
256                return;
257            }
258        }
259    }
260    
261    void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
262        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
263    }
264    
265    void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
266        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
267    }
268    
269    void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
270        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
271    }
272    
273    void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
274        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
275    }
276    
277    void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
278        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
279    }
280    
281    void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
282        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
283    }
284    
285    void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
286        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
287    }
288    
289    void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
290        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
291    }
292    
293    void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
294        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
295    }
296    
297    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
298        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
299  }  }
300    
301    #if HAVE_SQLITE3
302    void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
303        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
304    }
305    
306    void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
307        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
308    }
309    
310    void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
311        Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
312        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
313        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
314    }
315    
316    void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
317        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
318    }
319    
320    void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
321        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
322    }
323    
324    void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
325        Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
326        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
327        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
328    }
329    
330    void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
331        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
332    }
333    #endif // HAVE_SQLITE3
334    
335    
336  /**  /**
337   * Blocks the calling thread until the LSCP Server is initialized and   * Blocks the calling thread until the LSCP Server is initialized and
338   * accepting socket connections, if the server is already initialized then   * accepting socket connections, if the server is already initialized then
# Line 95  int LSCPServer::WaitUntilInitialized(lon Line 348  int LSCPServer::WaitUntilInitialized(lon
348  }  }
349    
350  int LSCPServer::Main() {  int LSCPServer::Main() {
351            #if defined(WIN32)
352            WSADATA wsaData;
353            int iResult;
354            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
355            if (iResult != 0) {
356                    std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
357                    exit(EXIT_FAILURE);
358            }
359            #endif
360      hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
361      if (hSocket < 0) {      if (hSocket < 0) {
362          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
# Line 108  int LSCPServer::Main() { Line 370  int LSCPServer::Main() {
370              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
371                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
372                      std::cerr << "gave up!" << std::endl;                      std::cerr << "gave up!" << std::endl;
373                        #if defined(WIN32)
374                        closesocket(hSocket);
375                        #else
376                      close(hSocket);                      close(hSocket);
377                        #endif
378                      //return -1;                      //return -1;
379                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
380                  }                  }
# Line 121  int LSCPServer::Main() { Line 387  int LSCPServer::Main() {
387      listen(hSocket, 1);      listen(hSocket, 1);
388      Initialized.Set(true);      Initialized.Set(true);
389    
390        // Registering event listeners
391        pSampler->AddChannelCountListener(&eventHandler);
392        pSampler->AddAudioDeviceCountListener(&eventHandler);
393        pSampler->AddMidiDeviceCountListener(&eventHandler);
394        pSampler->AddVoiceCountListener(&eventHandler);
395        pSampler->AddStreamCountListener(&eventHandler);
396        pSampler->AddBufferFillListener(&eventHandler);
397        pSampler->AddTotalStreamCountListener(&eventHandler);
398        pSampler->AddTotalVoiceCountListener(&eventHandler);
399        pSampler->AddFxSendCountListener(&eventHandler);
400        MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
401        MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
402        MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
403        MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
404    #if HAVE_SQLITE3
405        InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
406    #endif
407      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
408      sockaddr_in client;      sockaddr_in client;
409      int length = sizeof(client);      int length = sizeof(client);
# Line 128  int LSCPServer::Main() { Line 411  int LSCPServer::Main() {
411      FD_SET(hSocket, &fdSet);      FD_SET(hSocket, &fdSet);
412      int maxSessions = hSocket;      int maxSessions = hSocket;
413    
414        timeval timeout;
415    
416      while (true) {      while (true) {
417          fd_set selectSet = fdSet;          #if CONFIG_PTHREAD_TESTCANCEL
418          int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);                  TestCancel();
419            #endif
420            // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
421            {
422                std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
423                std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
424                std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
425                for (; itEngineChannel != itEnd; ++itEngineChannel) {
426                    if ((*itEngineChannel)->StatusChanged()) {
427                        SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));
428                    }
429    
430                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
431                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
432                        if(fxs != NULL && fxs->IsInfoChanged()) {
433                            int chn = (*itEngineChannel)->iSamplerChannelIndex;
434                            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
435                            fxs->SetInfoChanged(false);
436                        }
437                    }
438                }
439            }
440    
441            // check if MIDI data arrived on some engine channel
442            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
443                const EventHandler::midi_listener_entry entry =
444                    eventHandler.channelMidiListeners[i];
445                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
446                if (pMidiListener->NotesChanged()) {
447                    for (int iNote = 0; iNote < 128; iNote++) {
448                        if (pMidiListener->NoteChanged(iNote)) {
449                            const bool bActive = pMidiListener->NoteIsActive(iNote);
450                            LSCPServer::SendLSCPNotify(
451                                LSCPEvent(
452                                    LSCPEvent::event_channel_midi,
453                                    entry.pSamplerChannel->Index(),
454                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
455                                    iNote,
456                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
457                                            : pMidiListener->NoteOffVelocity(iNote)
458                                )
459                            );
460                        }
461                    }
462                }
463            }
464    
465            // check if MIDI data arrived on some MIDI device
466            for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
467                const EventHandler::device_midi_listener_entry entry =
468                    eventHandler.deviceMidiListeners[i];
469                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
470                if (pMidiListener->NotesChanged()) {
471                    for (int iNote = 0; iNote < 128; iNote++) {
472                        if (pMidiListener->NoteChanged(iNote)) {
473                            const bool bActive = pMidiListener->NoteIsActive(iNote);
474                            LSCPServer::SendLSCPNotify(
475                                LSCPEvent(
476                                    LSCPEvent::event_device_midi,
477                                    entry.uiDeviceID,
478                                    entry.pPort->GetPortNumber(),
479                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
480                                    iNote,
481                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
482                                            : pMidiListener->NoteOffVelocity(iNote)
483                                )
484                            );
485                        }
486                    }
487                }
488            }
489    
490            //Now let's deliver late notifies (if any)
491            NotifyBufferMutex.Lock();
492            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
493    #ifdef MSG_NOSIGNAL
494                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
495    #else
496                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
497    #endif
498            }
499            bufferedNotifies.clear();
500            NotifyBufferMutex.Unlock();
501    
502            fd_set selectSet = fdSet;
503            timeout.tv_sec  = 0;
504            timeout.tv_usec = 100000;
505    
506            int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
507    
508          if (retval == 0)          if (retval == 0)
509                  continue; //Nothing try again                  continue; //Nothing try again
510          if (retval == -1) {          if (retval == -1) {
511                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
512                    #if defined(WIN32)
513                    closesocket(hSocket);
514                    #else
515                  close(hSocket);                  close(hSocket);
516                    #endif
517                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
518          }          }
519    
# Line 147  int LSCPServer::Main() { Line 525  int LSCPServer::Main() {
525                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
526                  }                  }
527    
528                    #if defined(WIN32)
529                    u_long nonblock_io = 1;
530                    if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
531                      std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
532                      exit(EXIT_FAILURE);
533                    }
534            #else
535                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
536                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
537                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
538                  }                  }
539                    #endif
540    
541                  // Parser initialization                  // Parser initialization
542                  yyparse_param_t yyparse_param;                  yyparse_param_t yyparse_param;
# Line 174  int LSCPServer::Main() { Line 560  int LSCPServer::Main() {
560                                  int dummy; // just a temporary hack to fulfill the restart() function prototype                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
561                                  restart(NULL, dummy); // restart the 'scanner'                                  restart(NULL, dummy); // restart the 'scanner'
562                                  currentSocket = (*iter).hSession;  //a hack                                  currentSocket = (*iter).hSession;  //a hack
563                                    itCurrentSession = iter; // another hack
564                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
565                                  if ((*iter).bVerbose) { // if echo mode enabled                                  if ((*iter).bVerbose) { // if echo mode enabled
566                                      AnswerClient(bufferedCommands[currentSocket]);                                      AnswerClient(bufferedCommands[currentSocket]);
567                                  }                                  }
568                                  int result = yyparse(&(*iter));                                  int result = yyparse(&(*iter));
569                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
570                                    itCurrentSession = Sessions.end(); // hack as well
571                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
572                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?
573                                          CloseConnection(iter);                                          CloseConnection(iter);
# Line 190  int LSCPServer::Main() { Line 578  int LSCPServer::Main() {
578                          break;                          break;
579                  }                  }
580          }          }
   
         // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers  
         {  
             std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();  
             std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();  
             std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();  
             for (; itEngineChannel != itEnd; ++itEngineChannel) {  
                 if ((*itEngineChannel)->StatusChanged()) {  
                     SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));  
                 }  
             }  
         }  
   
         //Now let's deliver late notifies (if any)  
         NotifyBufferMutex.Lock();  
         for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {  
                 send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);  
                 bufferedNotifies.erase(iterNotify);  
         }  
         NotifyBufferMutex.Unlock();  
581      }      }
582  }  }
583    
# Line 227  void LSCPServer::CloseConnection( std::v Line 595  void LSCPServer::CloseConnection( std::v
595          NotifyMutex.Lock();          NotifyMutex.Lock();
596          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
597          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
598            #if defined(WIN32)
599            closesocket(socket);
600            #else
601          close(socket);          close(socket);
602            #endif
603          NotifyMutex.Unlock();          NotifyMutex.Unlock();
604  }  }
605    
606    void LSCPServer::LockRTNotify() {
607        RTNotifyMutex.Lock();
608    }
609    
610    void LSCPServer::UnlockRTNotify() {
611        RTNotifyMutex.Unlock();
612    }
613    
614  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
615          int subs = 0;          int subs = 0;
616          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 256  void LSCPServer::SendLSCPNotify( LSCPEve Line 636  void LSCPServer::SendLSCPNotify( LSCPEve
636          while (true) {          while (true) {
637                  if (NotifyMutex.Trylock()) {                  if (NotifyMutex.Trylock()) {
638                          for(;iter != end; iter++)                          for(;iter != end; iter++)
639    #ifdef MSG_NOSIGNAL
640                                  send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);                                  send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
641    #else
642                                    send(*iter, notify.c_str(), notify.size(), 0);
643    #endif
644                          NotifyMutex.Unlock();                          NotifyMutex.Unlock();
645                          break;                          break;
646                  } else {                  } else {
# Line 288  extern int GetLSCPCommand( void *buf, in Line 672  extern int GetLSCPCommand( void *buf, in
672          return command.size();          return command.size();
673  }  }
674    
675    extern yyparse_param_t* GetCurrentYaccSession() {
676        return &(*itCurrentSession);
677    }
678    
679  /**  /**
680   * Will be called to try to read the command from the socket   * Will be called to try to read the command from the socket
681   * 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 298  bool LSCPServer::GetLSCPCommand( std::ve Line 686  bool LSCPServer::GetLSCPCommand( std::ve
686          char c;          char c;
687          int i = 0;          int i = 0;
688          while (true) {          while (true) {
689                    #if defined(WIN32)
690                    int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
691                    #else
692                  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
693                    #endif
694                  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
695                          CloseConnection(iter);                          CloseConnection(iter);
696                          break;                          break;
# Line 308  bool LSCPServer::GetLSCPCommand( std::ve Line 700  bool LSCPServer::GetLSCPCommand( std::ve
700                                  continue; //Ignore CR                                  continue; //Ignore CR
701                          if (c == '\n') {                          if (c == '\n') {
702                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
703                                  bufferedCommands[socket] += "\n";                                  bufferedCommands[socket] += "\r\n";
704                                  return true; //Complete command was read                                  return true; //Complete command was read
705                          }                          }
706                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
707                  }                  }
708                    #if defined(WIN32)
709                    if (result == SOCKET_ERROR) {
710                        int wsa_lasterror = WSAGetLastError();
711                            if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
712                                    return false;
713                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
714                            CloseConnection(iter);
715                            break;
716                    }
717                    #else
718                  if (result == -1) {                  if (result == -1) {
719                          if (errno == EAGAIN) //Would block, try again later.                          if (errno == EAGAIN) //Would block, try again later.
720                                  return false;                                  return false;
# Line 351  bool LSCPServer::GetLSCPCommand( std::ve Line 753  bool LSCPServer::GetLSCPCommand( std::ve
753                          CloseConnection(iter);                          CloseConnection(iter);
754                          break;                          break;
755                  }                  }
756                    #endif
757          }          }
758          return false;          return false;
759  }  }
# Line 365  void LSCPServer::AnswerClient(String Ret Line 768  void LSCPServer::AnswerClient(String Ret
768      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
769      if (currentSocket != -1) {      if (currentSocket != -1) {
770              NotifyMutex.Lock();              NotifyMutex.Lock();
771    #ifdef MSG_NOSIGNAL
772              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
773    #else
774                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
775    #endif
776              NotifyMutex.Unlock();              NotifyMutex.Unlock();
777      }      }
778  }  }
# Line 409  String LSCPServer::CreateAudioOutputDevi Line 816  String LSCPServer::CreateAudioOutputDevi
816          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
817          // search for the created device to get its index          // search for the created device to get its index
818          int index = GetAudioOutputDeviceIndex(pDevice);          int index = GetAudioOutputDeviceIndex(pDevice);
819          if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");          if (index == -1) throw Exception("Internal error: could not find created audio output device.");
820          result = index; // success          result = index; // success
821      }      }
822      catch (LinuxSamplerException e) {      catch (Exception e) {
823          result.Error(e);          result.Error(e);
824      }      }
825      return result.Produce();      return result.Produce();
# Line 425  String LSCPServer::CreateMidiInputDevice Line 832  String LSCPServer::CreateMidiInputDevice
832          MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);          MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
833          // search for the created device to get its index          // search for the created device to get its index
834          int index = GetMidiInputDeviceIndex(pDevice);          int index = GetMidiInputDeviceIndex(pDevice);
835          if (index == -1) throw LinuxSamplerException("Internal error: could not find created midi input device.");          if (index == -1) throw Exception("Internal error: could not find created midi input device.");
836          result = index; // success          result = index; // success
837      }      }
838      catch (LinuxSamplerException e) {      catch (Exception e) {
839          result.Error(e);          result.Error(e);
840      }      }
841      return result.Produce();      return result.Produce();
# Line 439  String LSCPServer::DestroyAudioOutputDev Line 846  String LSCPServer::DestroyAudioOutputDev
846      LSCPResultSet result;      LSCPResultSet result;
847      try {      try {
848          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
849          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
850          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
851          pSampler->DestroyAudioOutputDevice(pDevice);          pSampler->DestroyAudioOutputDevice(pDevice);
852      }      }
853      catch (LinuxSamplerException e) {      catch (Exception e) {
854          result.Error(e);          result.Error(e);
855      }      }
856      return result.Produce();      return result.Produce();
# Line 454  String LSCPServer::DestroyMidiInputDevic Line 861  String LSCPServer::DestroyMidiInputDevic
861      LSCPResultSet result;      LSCPResultSet result;
862      try {      try {
863          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
864          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
865          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
866          pSampler->DestroyMidiInputDevice(pDevice);          pSampler->DestroyMidiInputDevice(pDevice);
867      }      }
868      catch (LinuxSamplerException e) {      catch (Exception e) {
869          result.Error(e);          result.Error(e);
870      }      }
871      return result.Produce();      return result.Produce();
872  }  }
873    
874    EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
875        SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
876        if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
877    
878        EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
879        if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
880    
881        return pEngineChannel;
882    }
883    
884  /**  /**
885   * Will be called by the parser to load an instrument.   * Will be called by the parser to load an instrument.
886   */   */
# Line 472  String LSCPServer::LoadInstrument(String Line 889  String LSCPServer::LoadInstrument(String
889      LSCPResultSet result;      LSCPResultSet result;
890      try {      try {
891          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
892          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
893          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
894          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel yet");
895          if (!pSamplerChannel->GetAudioOutputDevice())          if (!pSamplerChannel->GetAudioOutputDevice())
896              throw LinuxSamplerException("No audio output device connected to sampler channel");              throw Exception("No audio output device connected to sampler channel");
897          if (bBackground) {          if (bBackground) {
898              InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel);              InstrumentManager::instrument_id_t id;
899                id.FileName = Filename;
900                id.Index    = uiInstrument;
901                InstrumentManager::LoadInstrumentInBackground(id, pEngineChannel);
902          }          }
903          else {          else {
904              // tell the engine channel which instrument to load              // tell the engine channel which instrument to load
# Line 487  String LSCPServer::LoadInstrument(String Line 907  String LSCPServer::LoadInstrument(String
907              pEngineChannel->LoadInstrument();              pEngineChannel->LoadInstrument();
908          }          }
909      }      }
910      catch (LinuxSamplerException e) {      catch (Exception e) {
911           result.Error(e);           result.Error(e);
912      }      }
913      return result.Produce();      return result.Produce();
# Line 502  String LSCPServer::SetEngineType(String Line 922  String LSCPServer::SetEngineType(String
922      LSCPResultSet result;      LSCPResultSet result;
923      try {      try {
924          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
925          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
926          LockRTNotify();          LockRTNotify();
927          pSamplerChannel->SetEngineType(EngineName);          pSamplerChannel->SetEngineType(EngineName);
928          if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);          if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
929          UnlockRTNotify();          UnlockRTNotify();
930      }      }
931      catch (LinuxSamplerException e) {      catch (Exception e) {
932           result.Error(e);           result.Error(e);
933      }      }
934      return result.Produce();      return result.Produce();
# Line 546  String LSCPServer::ListChannels() { Line 966  String LSCPServer::ListChannels() {
966   */   */
967  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
968      dmsg(2,("LSCPServer: AddChannel()\n"));      dmsg(2,("LSCPServer: AddChannel()\n"));
969        LockRTNotify();
970      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
971        UnlockRTNotify();
972      LSCPResultSet result(pSamplerChannel->Index());      LSCPResultSet result(pSamplerChannel->Index());
973      return result.Produce();      return result.Produce();
974  }  }
# Line 568  String LSCPServer::RemoveChannel(uint ui Line 990  String LSCPServer::RemoveChannel(uint ui
990   */   */
991  String LSCPServer::GetAvailableEngines() {  String LSCPServer::GetAvailableEngines() {
992      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
993      LSCPResultSet result("1");      LSCPResultSet result;
994        try {
995            int n = EngineFactory::AvailableEngineTypes().size();
996            result.Add(n);
997        }
998        catch (Exception e) {
999            result.Error(e);
1000        }
1001      return result.Produce();      return result.Produce();
1002  }  }
1003    
# Line 577  String LSCPServer::GetAvailableEngines() Line 1006  String LSCPServer::GetAvailableEngines()
1006   */   */
1007  String LSCPServer::ListAvailableEngines() {  String LSCPServer::ListAvailableEngines() {
1008      dmsg(2,("LSCPServer: ListAvailableEngines()\n"));      dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
1009      LSCPResultSet result("\'GIG\'");      LSCPResultSet result;
1010        try {
1011            String s = EngineFactory::AvailableEngineTypesAsString();
1012            result.Add(s);
1013        }
1014        catch (Exception e) {
1015            result.Error(e);
1016        }
1017      return result.Produce();      return result.Produce();
1018  }  }
1019    
# Line 588  String LSCPServer::ListAvailableEngines( Line 1024  String LSCPServer::ListAvailableEngines(
1024  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
1025      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
1026      LSCPResultSet result;      LSCPResultSet result;
1027        LockRTNotify();
1028      try {      try {
1029          Engine* pEngine = EngineFactory::Create(EngineName);          Engine* pEngine = EngineFactory::Create(EngineName);
1030          result.Add("DESCRIPTION", pEngine->Description());          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1031          result.Add("VERSION",     pEngine->Version());          result.Add("VERSION",     pEngine->Version());
1032          EngineFactory::Destroy(pEngine);          EngineFactory::Destroy(pEngine);
1033      }      }
1034      catch (LinuxSamplerException e) {      catch (Exception e) {
1035           result.Error(e);           result.Error(e);
1036      }      }
1037        UnlockRTNotify();
1038      return result.Produce();      return result.Produce();
1039  }  }
1040    
# Line 609  String LSCPServer::GetChannelInfo(uint u Line 1047  String LSCPServer::GetChannelInfo(uint u
1047      LSCPResultSet result;      LSCPResultSet result;
1048      try {      try {
1049          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1050          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1051          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1052    
1053          //Defaults values          //Defaults values
# Line 623  String LSCPServer::GetChannelInfo(uint u Line 1061  String LSCPServer::GetChannelInfo(uint u
1061          String AudioRouting;          String AudioRouting;
1062          int Mute = 0;          int Mute = 0;
1063          bool Solo = false;          bool Solo = false;
1064            String MidiInstrumentMap = "NONE";
1065    
1066          if (pEngineChannel) {          if (pEngineChannel) {
1067              EngineName          = pEngineChannel->EngineName();              EngineName          = pEngineChannel->EngineName();
# Line 640  String LSCPServer::GetChannelInfo(uint u Line 1079  String LSCPServer::GetChannelInfo(uint u
1079              }              }
1080              Mute = pEngineChannel->GetMute();              Mute = pEngineChannel->GetMute();
1081              Solo = pEngineChannel->GetSolo();              Solo = pEngineChannel->GetSolo();
1082                if (pEngineChannel->UsesNoMidiInstrumentMap())
1083                    MidiInstrumentMap = "NONE";
1084                else if (pEngineChannel->UsesDefaultMidiInstrumentMap())
1085                    MidiInstrumentMap = "DEFAULT";
1086                else
1087                    MidiInstrumentMap = ToString(pEngineChannel->GetMidiInstrumentMap());
1088          }          }
1089    
1090          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
# Line 655  String LSCPServer::GetChannelInfo(uint u Line 1100  String LSCPServer::GetChannelInfo(uint u
1100          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
1101          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
1102    
1103            // convert the filename into the correct encoding as defined for LSCP
1104            // (especially in terms of special characters -> escape sequences)
1105            if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
1106    #if WIN32
1107                InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
1108    #else
1109                // assuming POSIX
1110                InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
1111    #endif
1112            }
1113    
1114          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
1115          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
1116          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
1117          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
1118          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
1119          result.Add("SOLO", Solo);          result.Add("SOLO", Solo);
1120            result.Add("MIDI_INSTRUMENT_MAP", MidiInstrumentMap);
1121      }      }
1122      catch (LinuxSamplerException e) {      catch (Exception e) {
1123           result.Error(e);           result.Error(e);
1124      }      }
1125      return result.Produce();      return result.Produce();
# Line 677  String LSCPServer::GetVoiceCount(uint ui Line 1134  String LSCPServer::GetVoiceCount(uint ui
1134      LSCPResultSet result;      LSCPResultSet result;
1135      try {      try {
1136          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1137          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1138          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1139          if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel");          if (!pEngineChannel) throw Exception("No engine loaded on sampler channel");
1140          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1141          result.Add(pEngineChannel->GetEngine()->VoiceCount());          result.Add(pEngineChannel->GetEngine()->VoiceCount());
1142      }      }
1143      catch (LinuxSamplerException e) {      catch (Exception e) {
1144           result.Error(e);           result.Error(e);
1145      }      }
1146      return result.Produce();      return result.Produce();
# Line 698  String LSCPServer::GetStreamCount(uint u Line 1155  String LSCPServer::GetStreamCount(uint u
1155      LSCPResultSet result;      LSCPResultSet result;
1156      try {      try {
1157          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1158          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1159          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1160          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1161          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1162          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
1163      }      }
1164      catch (LinuxSamplerException e) {      catch (Exception e) {
1165           result.Error(e);           result.Error(e);
1166      }      }
1167      return result.Produce();      return result.Produce();
# Line 719  String LSCPServer::GetBufferFill(fill_re Line 1176  String LSCPServer::GetBufferFill(fill_re
1176      LSCPResultSet result;      LSCPResultSet result;
1177      try {      try {
1178          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1179          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1180          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1181          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1182          if (!pEngineChannel->GetEngine()) throw LinuxSamplerException("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1183          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
1184          else {          else {
1185              switch (ResponseType) {              switch (ResponseType) {
# Line 733  String LSCPServer::GetBufferFill(fill_re Line 1190  String LSCPServer::GetBufferFill(fill_re
1190                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
1191                      break;                      break;
1192                  default:                  default:
1193                      throw LinuxSamplerException("Unknown fill response type");                      throw Exception("Unknown fill response type");
1194              }              }
1195          }          }
1196      }      }
1197      catch (LinuxSamplerException e) {      catch (Exception e) {
1198           result.Error(e);           result.Error(e);
1199      }      }
1200      return result.Produce();      return result.Produce();
# Line 750  String LSCPServer::GetAvailableAudioOutp Line 1207  String LSCPServer::GetAvailableAudioOutp
1207          int n = AudioOutputDeviceFactory::AvailableDrivers().size();          int n = AudioOutputDeviceFactory::AvailableDrivers().size();
1208          result.Add(n);          result.Add(n);
1209      }      }
1210      catch (LinuxSamplerException e) {      catch (Exception e) {
1211          result.Error(e);          result.Error(e);
1212      }      }
1213      return result.Produce();      return result.Produce();
# Line 763  String LSCPServer::ListAvailableAudioOut Line 1220  String LSCPServer::ListAvailableAudioOut
1220          String s = AudioOutputDeviceFactory::AvailableDriversAsString();          String s = AudioOutputDeviceFactory::AvailableDriversAsString();
1221          result.Add(s);          result.Add(s);
1222      }      }
1223      catch (LinuxSamplerException e) {      catch (Exception e) {
1224          result.Error(e);          result.Error(e);
1225      }      }
1226      return result.Produce();      return result.Produce();
# Line 776  String LSCPServer::GetAvailableMidiInput Line 1233  String LSCPServer::GetAvailableMidiInput
1233          int n = MidiInputDeviceFactory::AvailableDrivers().size();          int n = MidiInputDeviceFactory::AvailableDrivers().size();
1234          result.Add(n);          result.Add(n);
1235      }      }
1236      catch (LinuxSamplerException e) {      catch (Exception e) {
1237          result.Error(e);          result.Error(e);
1238      }      }
1239      return result.Produce();      return result.Produce();
# Line 789  String LSCPServer::ListAvailableMidiInpu Line 1246  String LSCPServer::ListAvailableMidiInpu
1246          String s = MidiInputDeviceFactory::AvailableDriversAsString();          String s = MidiInputDeviceFactory::AvailableDriversAsString();
1247          result.Add(s);          result.Add(s);
1248      }      }
1249      catch (LinuxSamplerException e) {      catch (Exception e) {
1250          result.Error(e);          result.Error(e);
1251      }      }
1252      return result.Produce();      return result.Produce();
# Line 813  String LSCPServer::GetMidiInputDriverInf Line 1270  String LSCPServer::GetMidiInputDriverInf
1270              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1271          }          }
1272      }      }
1273      catch (LinuxSamplerException e) {      catch (Exception e) {
1274          result.Error(e);          result.Error(e);
1275      }      }
1276      return result.Produce();      return result.Produce();
# Line 837  String LSCPServer::GetAudioOutputDriverI Line 1294  String LSCPServer::GetAudioOutputDriverI
1294              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1295          }          }
1296      }      }
1297      catch (LinuxSamplerException e) {      catch (Exception e) {
1298          result.Error(e);          result.Error(e);
1299      }      }
1300      return result.Produce();      return result.Produce();
# Line 864  String LSCPServer::GetMidiInputDriverPar Line 1321  String LSCPServer::GetMidiInputDriverPar
1321          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1322          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1323      }      }
1324      catch (LinuxSamplerException e) {      catch (Exception e) {
1325          result.Error(e);          result.Error(e);
1326      }      }
1327      return result.Produce();      return result.Produce();
# Line 891  String LSCPServer::GetAudioOutputDriverP Line 1348  String LSCPServer::GetAudioOutputDriverP
1348          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1349          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1350      }      }
1351      catch (LinuxSamplerException e) {      catch (Exception e) {
1352          result.Error(e);          result.Error(e);
1353      }      }
1354      return result.Produce();      return result.Produce();
# Line 904  String LSCPServer::GetAudioOutputDeviceC Line 1361  String LSCPServer::GetAudioOutputDeviceC
1361          uint count = pSampler->AudioOutputDevices();          uint count = pSampler->AudioOutputDevices();
1362          result.Add(count); // success          result.Add(count); // success
1363      }      }
1364      catch (LinuxSamplerException e) {      catch (Exception e) {
1365          result.Error(e);          result.Error(e);
1366      }      }
1367      return result.Produce();      return result.Produce();
# Line 917  String LSCPServer::GetMidiInputDeviceCou Line 1374  String LSCPServer::GetMidiInputDeviceCou
1374          uint count = pSampler->MidiInputDevices();          uint count = pSampler->MidiInputDevices();
1375          result.Add(count); // success          result.Add(count); // success
1376      }      }
1377      catch (LinuxSamplerException e) {      catch (Exception e) {
1378          result.Error(e);          result.Error(e);
1379      }      }
1380      return result.Produce();      return result.Produce();
# Line 936  String LSCPServer::GetAudioOutputDevices Line 1393  String LSCPServer::GetAudioOutputDevices
1393          }          }
1394          result.Add(s);          result.Add(s);
1395      }      }
1396      catch (LinuxSamplerException e) {      catch (Exception e) {
1397          result.Error(e);          result.Error(e);
1398      }      }
1399      return result.Produce();      return result.Produce();
# Line 955  String LSCPServer::GetMidiInputDevices() Line 1412  String LSCPServer::GetMidiInputDevices()
1412          }          }
1413          result.Add(s);          result.Add(s);
1414      }      }
1415      catch (LinuxSamplerException e) {      catch (Exception e) {
1416          result.Error(e);          result.Error(e);
1417      }      }
1418      return result.Produce();      return result.Produce();
# Line 966  String LSCPServer::GetAudioOutputDeviceI Line 1423  String LSCPServer::GetAudioOutputDeviceI
1423      LSCPResultSet result;      LSCPResultSet result;
1424      try {      try {
1425          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1426          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1427          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1428          result.Add("DRIVER", pDevice->Driver());          result.Add("DRIVER", pDevice->Driver());
1429          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
# Line 975  String LSCPServer::GetAudioOutputDeviceI Line 1432  String LSCPServer::GetAudioOutputDeviceI
1432              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1433          }          }
1434      }      }
1435      catch (LinuxSamplerException e) {      catch (Exception e) {
1436          result.Error(e);          result.Error(e);
1437      }      }
1438      return result.Produce();      return result.Produce();
# Line 986  String LSCPServer::GetMidiInputDeviceInf Line 1443  String LSCPServer::GetMidiInputDeviceInf
1443      LSCPResultSet result;      LSCPResultSet result;
1444      try {      try {
1445          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1446          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1447          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1448          result.Add("DRIVER", pDevice->Driver());          result.Add("DRIVER", pDevice->Driver());
1449          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
# Line 995  String LSCPServer::GetMidiInputDeviceInf Line 1452  String LSCPServer::GetMidiInputDeviceInf
1452              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1453          }          }
1454      }      }
1455      catch (LinuxSamplerException e) {      catch (Exception e) {
1456          result.Error(e);          result.Error(e);
1457      }      }
1458      return result.Produce();      return result.Produce();
# Line 1006  String LSCPServer::GetMidiInputPortInfo( Line 1463  String LSCPServer::GetMidiInputPortInfo(
1463      try {      try {
1464          // get MIDI input device          // get MIDI input device
1465          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1466          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1467          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1468    
1469          // get MIDI port          // get MIDI port
1470          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1471          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1472    
1473          // return the values of all MIDI port parameters          // return the values of all MIDI port parameters
1474          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
# Line 1020  String LSCPServer::GetMidiInputPortInfo( Line 1477  String LSCPServer::GetMidiInputPortInfo(
1477              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1478          }          }
1479      }      }
1480      catch (LinuxSamplerException e) {      catch (Exception e) {
1481          result.Error(e);          result.Error(e);
1482      }      }
1483      return result.Produce();      return result.Produce();
# Line 1032  String LSCPServer::GetAudioOutputChannel Line 1489  String LSCPServer::GetAudioOutputChannel
1489      try {      try {
1490          // get audio output device          // get audio output device
1491          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1492          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1493          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1494    
1495          // get audio channel          // get audio channel
1496          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1497          if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");          if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1498    
1499          // return the values of all audio channel parameters          // return the values of all audio channel parameters
1500          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
# Line 1046  String LSCPServer::GetAudioOutputChannel Line 1503  String LSCPServer::GetAudioOutputChannel
1503              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1504          }          }
1505      }      }
1506      catch (LinuxSamplerException e) {      catch (Exception e) {
1507          result.Error(e);          result.Error(e);
1508      }      }
1509      return result.Produce();      return result.Produce();
# Line 1058  String LSCPServer::GetMidiInputPortParam Line 1515  String LSCPServer::GetMidiInputPortParam
1515      try {      try {
1516          // get MIDI input device          // get MIDI input device
1517          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1518          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no midi input device with index " + ToString(DeviceId) + ".");
1519          MidiInputDevice* pDevice = devices[DeviceId];          MidiInputDevice* pDevice = devices[DeviceId];
1520    
1521          // get midi port          // get midi port
1522          MidiInputPort* pPort = pDevice->GetPort(PortId);          MidiInputPort* pPort = pDevice->GetPort(PortId);
1523          if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");          if (!pPort) throw Exception("Midi input device does not have port " + ToString(PortId) + ".");
1524    
1525          // get desired port parameter          // get desired port parameter
1526          std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1527          if (!parameters.count(ParameterName)) throw LinuxSamplerException("Midi port does not provide a parameter '" + ParameterName + "'.");          if (!parameters.count(ParameterName)) throw Exception("Midi port does not provide a parameter '" + ParameterName + "'.");
1528          DeviceRuntimeParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1529    
1530          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
# Line 1079  String LSCPServer::GetMidiInputPortParam Line 1536  String LSCPServer::GetMidiInputPortParam
1536          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1537          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1538      }      }
1539      catch (LinuxSamplerException e) {      catch (Exception e) {
1540          result.Error(e);          result.Error(e);
1541      }      }
1542      return result.Produce();      return result.Produce();
# Line 1091  String LSCPServer::GetAudioOutputChannel Line 1548  String LSCPServer::GetAudioOutputChannel
1548      try {      try {
1549          // get audio output device          // get audio output device
1550          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1551          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1552          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1553    
1554          // get audio channel          // get audio channel
1555          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1556          if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");          if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1557    
1558          // get desired audio channel parameter          // get desired audio channel parameter
1559          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1560          if (!parameters.count(ParameterName)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");          if (!parameters.count(ParameterName)) throw Exception("Audio channel does not provide a parameter '" + ParameterName + "'.");
1561          DeviceRuntimeParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1562    
1563          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
# Line 1112  String LSCPServer::GetAudioOutputChannel Line 1569  String LSCPServer::GetAudioOutputChannel
1569          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1570          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1571      }      }
1572      catch (LinuxSamplerException e) {      catch (Exception e) {
1573          result.Error(e);          result.Error(e);
1574      }      }
1575      return result.Produce();      return result.Produce();
# Line 1124  String LSCPServer::SetAudioOutputChannel Line 1581  String LSCPServer::SetAudioOutputChannel
1581      try {      try {
1582          // get audio output device          // get audio output device
1583          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1584          if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");          if (!devices.count(DeviceId)) throw Exception("There is no audio output device with index " + ToString(DeviceId) + ".");
1585          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1586    
1587          // get audio channel          // get audio channel
1588          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1589          if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");          if (!pChannel) throw Exception("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1590    
1591          // get desired audio channel parameter          // get desired audio channel parameter
1592          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1593          if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");          if (!parameters.count(ParamKey)) throw Exception("Audio channel does not provide a parameter '" + ParamKey + "'.");
1594          DeviceRuntimeParameter* pParameter = parameters[ParamKey];          DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1595    
1596          // set new channel parameter value          // set new channel parameter value
1597          pParameter->SetValue(ParamVal);          pParameter->SetValue(ParamVal);
1598            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceId));
1599      }      }
1600      catch (LinuxSamplerException e) {      catch (Exception e) {
1601          result.Error(e);          result.Error(e);
1602      }      }
1603      return result.Produce();      return result.Produce();
# Line 1150  String LSCPServer::SetAudioOutputDeviceP Line 1608  String LSCPServer::SetAudioOutputDeviceP
1608      LSCPResultSet result;      LSCPResultSet result;
1609      try {      try {
1610          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1611          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1612          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1613          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1614          if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");          if (!parameters.count(ParamKey)) throw Exception("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1615          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1616            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceIndex));
1617      }      }
1618      catch (LinuxSamplerException e) {      catch (Exception e) {
1619          result.Error(e);          result.Error(e);
1620      }      }
1621      return result.Produce();      return result.Produce();
# Line 1167  String LSCPServer::SetMidiInputDevicePar Line 1626  String LSCPServer::SetMidiInputDevicePar
1626      LSCPResultSet result;      LSCPResultSet result;
1627      try {      try {
1628          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1629          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1630          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1631          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1632          if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");          if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1633          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1634            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1635      }      }
1636      catch (LinuxSamplerException e) {      catch (Exception e) {
1637          result.Error(e);          result.Error(e);
1638      }      }
1639      return result.Produce();      return result.Produce();
# Line 1185  String LSCPServer::SetMidiInputPortParam Line 1645  String LSCPServer::SetMidiInputPortParam
1645      try {      try {
1646          // get MIDI input device          // get MIDI input device
1647          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1648          if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");          if (!devices.count(DeviceIndex)) throw Exception("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1649          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1650    
1651          // get MIDI port          // get MIDI port
1652          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1653          if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");          if (!pMidiInputPort) throw Exception("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1654    
1655          // set port parameter value          // set port parameter value
1656          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1657          if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");          if (!parameters.count(ParamKey)) throw Exception("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1658          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1659            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1660      }      }
1661      catch (LinuxSamplerException e) {      catch (Exception e) {
1662          result.Error(e);          result.Error(e);
1663      }      }
1664      return result.Produce();      return result.Produce();
# Line 1212  String LSCPServer::SetAudioOutputChannel Line 1673  String LSCPServer::SetAudioOutputChannel
1673      LSCPResultSet result;      LSCPResultSet result;
1674      try {      try {
1675          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1676          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1677          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1678          if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));          if (!pEngineChannel) throw Exception("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1679          if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));          if (!pSamplerChannel->GetAudioOutputDevice()) throw Exception("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1680          pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);          pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1681      }      }
1682      catch (LinuxSamplerException e) {      catch (Exception e) {
1683           result.Error(e);           result.Error(e);
1684      }      }
1685      return result.Produce();      return result.Produce();
# Line 1227  String LSCPServer::SetAudioOutputChannel Line 1688  String LSCPServer::SetAudioOutputChannel
1688  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1689      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1690      LSCPResultSet result;      LSCPResultSet result;
1691        LockRTNotify();
1692      try {      try {
1693          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1694          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1695          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1696          if (!devices.count(AudioDeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));          if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));
1697          AudioOutputDevice* pDevice = devices[AudioDeviceId];          AudioOutputDevice* pDevice = devices[AudioDeviceId];
1698          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1699      }      }
1700      catch (LinuxSamplerException e) {      catch (Exception e) {
1701           result.Error(e);           result.Error(e);
1702      }      }
1703        UnlockRTNotify();
1704      return result.Produce();      return result.Produce();
1705  }  }
1706    
1707  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1708      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1709      LSCPResultSet result;      LSCPResultSet result;
1710        LockRTNotify();
1711      try {      try {
1712          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1713          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1714          // Driver type name aliasing...          // Driver type name aliasing...
1715          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1716          if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";          if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
# Line 1268  String LSCPServer::SetAudioOutputType(St Line 1732  String LSCPServer::SetAudioOutputType(St
1732          }          }
1733          // Must have a device...          // Must have a device...
1734          if (pDevice == NULL)          if (pDevice == NULL)
1735              throw LinuxSamplerException("Internal error: could not create audio output device.");              throw Exception("Internal error: could not create audio output device.");
1736          // Set it as the current channel device...          // Set it as the current channel device...
1737          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1738      }      }
1739      catch (LinuxSamplerException e) {      catch (Exception e) {
1740           result.Error(e);           result.Error(e);
1741      }      }
1742        UnlockRTNotify();
1743      return result.Produce();      return result.Produce();
1744  }  }
1745    
# Line 1283  String LSCPServer::SetMIDIInputPort(uint Line 1748  String LSCPServer::SetMIDIInputPort(uint
1748      LSCPResultSet result;      LSCPResultSet result;
1749      try {      try {
1750          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1751          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1752          pSamplerChannel->SetMidiInputPort(MIDIPort);          pSamplerChannel->SetMidiInputPort(MIDIPort);
1753      }      }
1754      catch (LinuxSamplerException e) {      catch (Exception e) {
1755           result.Error(e);           result.Error(e);
1756      }      }
1757      return result.Produce();      return result.Produce();
# Line 1297  String LSCPServer::SetMIDIInputChannel(u Line 1762  String LSCPServer::SetMIDIInputChannel(u
1762      LSCPResultSet result;      LSCPResultSet result;
1763      try {      try {
1764          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1765          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1766          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1767      }      }
1768      catch (LinuxSamplerException e) {      catch (Exception e) {
1769           result.Error(e);           result.Error(e);
1770      }      }
1771      return result.Produce();      return result.Produce();
# Line 1311  String LSCPServer::SetMIDIInputDevice(ui Line 1776  String LSCPServer::SetMIDIInputDevice(ui
1776      LSCPResultSet result;      LSCPResultSet result;
1777      try {      try {
1778          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1779          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1780          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1781          if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));          if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1782          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1783          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1784      }      }
1785      catch (LinuxSamplerException e) {      catch (Exception e) {
1786           result.Error(e);           result.Error(e);
1787      }      }
1788      return result.Produce();      return result.Produce();
# Line 1328  String LSCPServer::SetMIDIInputType(Stri Line 1793  String LSCPServer::SetMIDIInputType(Stri
1793      LSCPResultSet result;      LSCPResultSet result;
1794      try {      try {
1795          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1796          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1797          // Driver type name aliasing...          // Driver type name aliasing...
1798          if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";          if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1799          // Check if there's one MIDI input device already created          // Check if there's one MIDI input device already created
# Line 1352  String LSCPServer::SetMIDIInputType(Stri Line 1817  String LSCPServer::SetMIDIInputType(Stri
1817          }          }
1818          // Must have a device...          // Must have a device...
1819          if (pDevice == NULL)          if (pDevice == NULL)
1820              throw LinuxSamplerException("Internal error: could not create MIDI input device.");              throw Exception("Internal error: could not create MIDI input device.");
1821          // Set it as the current channel device...          // Set it as the current channel device...
1822          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1823      }      }
1824      catch (LinuxSamplerException e) {      catch (Exception e) {
1825           result.Error(e);           result.Error(e);
1826      }      }
1827      return result.Produce();      return result.Produce();
# Line 1371  String LSCPServer::SetMIDIInput(uint MID Line 1836  String LSCPServer::SetMIDIInput(uint MID
1836      LSCPResultSet result;      LSCPResultSet result;
1837      try {      try {
1838          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1839          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1840          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1841          if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));          if (!devices.count(MIDIDeviceId)) throw Exception("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1842          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1843          pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
1844      }      }
1845      catch (LinuxSamplerException e) {      catch (Exception e) {
1846           result.Error(e);           result.Error(e);
1847      }      }
1848      return result.Produce();      return result.Produce();
# Line 1392  String LSCPServer::SetVolume(double dVol Line 1857  String LSCPServer::SetVolume(double dVol
1857      LSCPResultSet result;      LSCPResultSet result;
1858      try {      try {
1859          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1860          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1861          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1862          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1863          pEngineChannel->Volume(dVolume);          pEngineChannel->Volume(dVolume);
1864      }      }
1865      catch (LinuxSamplerException e) {      catch (Exception e) {
1866           result.Error(e);           result.Error(e);
1867      }      }
1868      return result.Produce();      return result.Produce();
# Line 1411  String LSCPServer::SetChannelMute(bool b Line 1876  String LSCPServer::SetChannelMute(bool b
1876      LSCPResultSet result;      LSCPResultSet result;
1877      try {      try {
1878          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1879          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1880    
1881          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1882          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1883    
1884          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1885          else pEngineChannel->SetMute(1);          else pEngineChannel->SetMute(1);
1886      } catch (LinuxSamplerException e) {      } catch (Exception e) {
1887          result.Error(e);          result.Error(e);
1888      }      }
1889      return result.Produce();      return result.Produce();
# Line 1432  String LSCPServer::SetChannelSolo(bool b Line 1897  String LSCPServer::SetChannelSolo(bool b
1897      LSCPResultSet result;      LSCPResultSet result;
1898      try {      try {
1899          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1900          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1901    
1902          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1903          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
1904    
1905          bool oldSolo = pEngineChannel->GetSolo();          bool oldSolo = pEngineChannel->GetSolo();
1906          bool hadSoloChannel = HasSoloChannel();          bool hadSoloChannel = HasSoloChannel();
1907            
1908          pEngineChannel->SetSolo(bSolo);          pEngineChannel->SetSolo(bSolo);
1909            
1910          if(!oldSolo && bSolo) {          if(!oldSolo && bSolo) {
1911              if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);              if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1912              if(!hadSoloChannel) MuteNonSoloChannels();              if(!hadSoloChannel) MuteNonSoloChannels();
1913          }          }
1914            
1915          if(oldSolo && !bSolo) {          if(oldSolo && !bSolo) {
1916              if(!HasSoloChannel()) UnmuteChannels();              if(!HasSoloChannel()) UnmuteChannels();
1917              else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);              else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1918          }          }
1919      } catch (LinuxSamplerException e) {      } catch (Exception e) {
1920          result.Error(e);          result.Error(e);
1921      }      }
1922      return result.Produce();      return result.Produce();
# Line 1504  void  LSCPServer::UnmuteChannels() { Line 1969  void  LSCPServer::UnmuteChannels() {
1969      }      }
1970  }  }
1971    
1972    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) {
1973        dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1974    
1975        midi_prog_index_t idx;
1976        idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1977        idx.midi_bank_lsb = MidiBank & 0x7f;
1978        idx.midi_prog     = MidiProg;
1979    
1980        MidiInstrumentMapper::entry_t entry;
1981        entry.EngineName      = EngineType;
1982        entry.InstrumentFile  = InstrumentFile;
1983        entry.InstrumentIndex = InstrumentIndex;
1984        entry.LoadMode        = LoadMode;
1985        entry.Volume          = Volume;
1986        entry.Name            = Name;
1987    
1988        LSCPResultSet result;
1989        try {
1990            // PERSISTENT mapping commands might block for a long time, so in
1991            // that case we add/replace the mapping in another thread in case
1992            // the NON_MODAL argument was supplied, non persistent mappings
1993            // should return immediately, so we don't need to do that for them
1994            bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
1995            MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
1996        } catch (Exception e) {
1997            result.Error(e);
1998        }
1999        return result.Produce();
2000    }
2001    
2002    String LSCPServer::RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2003        dmsg(2,("LSCPServer: RemoveMIDIInstrumentMapping()\n"));
2004    
2005        midi_prog_index_t idx;
2006        idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
2007        idx.midi_bank_lsb = MidiBank & 0x7f;
2008        idx.midi_prog     = MidiProg;
2009    
2010        LSCPResultSet result;
2011        try {
2012            MidiInstrumentMapper::RemoveEntry(MidiMapID, idx);
2013        } catch (Exception e) {
2014            result.Error(e);
2015        }
2016        return result.Produce();
2017    }
2018    
2019    String LSCPServer::GetMidiInstrumentMappings(uint MidiMapID) {
2020        dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2021        LSCPResultSet result;
2022        try {
2023            result.Add(MidiInstrumentMapper::Entries(MidiMapID).size());
2024        } catch (Exception e) {
2025            result.Error(e);
2026        }
2027        return result.Produce();
2028    }
2029    
2030    
2031    String LSCPServer::GetAllMidiInstrumentMappings() {
2032        dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2033        LSCPResultSet result;
2034        std::vector<int> maps = MidiInstrumentMapper::Maps();
2035        int totalMappings = 0;
2036        for (int i = 0; i < maps.size(); i++) {
2037            try {
2038                totalMappings += MidiInstrumentMapper::Entries(maps[i]).size();
2039            } catch (Exception e) { /*NOOP*/ }
2040        }
2041        result.Add(totalMappings);
2042        return result.Produce();
2043    }
2044    
2045    String LSCPServer::GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2046        dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2047        LSCPResultSet result;
2048        try {
2049            midi_prog_index_t idx;
2050            idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
2051            idx.midi_bank_lsb = MidiBank & 0x7f;
2052            idx.midi_prog     = MidiProg;
2053    
2054            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
2055            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);
2056            if (iter == mappings.end()) result.Error("there is no map entry with that index");
2057            else { // found
2058    
2059                // convert the filename into the correct encoding as defined for LSCP
2060                // (especially in terms of special characters -> escape sequences)
2061    #if WIN32
2062                const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp();
2063    #else
2064                // assuming POSIX
2065                const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp();
2066    #endif
2067    
2068                result.Add("NAME", _escapeLscpResponse(iter->second.Name));
2069                result.Add("ENGINE_NAME", iter->second.EngineName);
2070                result.Add("INSTRUMENT_FILE", instrumentFileName);
2071                result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);
2072                String instrumentName;
2073                Engine* pEngine = EngineFactory::Create(iter->second.EngineName);
2074                if (pEngine) {
2075                    if (pEngine->GetInstrumentManager()) {
2076                        InstrumentManager::instrument_id_t instrID;
2077                        instrID.FileName = iter->second.InstrumentFile;
2078                        instrID.Index    = iter->second.InstrumentIndex;
2079                        instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
2080                    }
2081                    EngineFactory::Destroy(pEngine);
2082                }
2083                result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2084                switch (iter->second.LoadMode) {
2085                    case MidiInstrumentMapper::ON_DEMAND:
2086                        result.Add("LOAD_MODE", "ON_DEMAND");
2087                        break;
2088                    case MidiInstrumentMapper::ON_DEMAND_HOLD:
2089                        result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2090                        break;
2091                    case MidiInstrumentMapper::PERSISTENT:
2092                        result.Add("LOAD_MODE", "PERSISTENT");
2093                        break;
2094                    default:
2095                        throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2096                }
2097                result.Add("VOLUME", iter->second.Volume);
2098            }
2099        } catch (Exception e) {
2100            result.Error(e);
2101        }
2102        return result.Produce();
2103    }
2104    
2105    String LSCPServer::ListMidiInstrumentMappings(uint MidiMapID) {
2106        dmsg(2,("LSCPServer: ListMidiInstrumentMappings()\n"));
2107        LSCPResultSet result;
2108        try {
2109            String s;
2110            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
2111            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2112            for (; iter != mappings.end(); iter++) {
2113                if (s.size()) s += ",";
2114                s += "{" + ToString(MidiMapID) + ","
2115                         + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2116                         + ToString(int(iter->first.midi_prog)) + "}";
2117            }
2118            result.Add(s);
2119        } catch (Exception e) {
2120            result.Error(e);
2121        }
2122        return result.Produce();
2123    }
2124    
2125    String LSCPServer::ListAllMidiInstrumentMappings() {
2126        dmsg(2,("LSCPServer: ListAllMidiInstrumentMappings()\n"));
2127        LSCPResultSet result;
2128        try {
2129            std::vector<int> maps = MidiInstrumentMapper::Maps();
2130            String s;
2131            for (int i = 0; i < maps.size(); i++) {
2132                std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(maps[i]);
2133                std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2134                for (; iter != mappings.end(); iter++) {
2135                    if (s.size()) s += ",";
2136                    s += "{" + ToString(maps[i]) + ","
2137                             + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2138                             + ToString(int(iter->first.midi_prog)) + "}";
2139                }
2140            }
2141            result.Add(s);
2142        } catch (Exception e) {
2143            result.Error(e);
2144        }
2145        return result.Produce();
2146    }
2147    
2148    String LSCPServer::ClearMidiInstrumentMappings(uint MidiMapID) {
2149        dmsg(2,("LSCPServer: ClearMidiInstrumentMappings()\n"));
2150        LSCPResultSet result;
2151        try {
2152            MidiInstrumentMapper::RemoveAllEntries(MidiMapID);
2153        } catch (Exception e) {
2154            result.Error(e);
2155        }
2156        return result.Produce();
2157    }
2158    
2159    String LSCPServer::ClearAllMidiInstrumentMappings() {
2160        dmsg(2,("LSCPServer: ClearAllMidiInstrumentMappings()\n"));
2161        LSCPResultSet result;
2162        try {
2163            std::vector<int> maps = MidiInstrumentMapper::Maps();
2164            for (int i = 0; i < maps.size(); i++)
2165                MidiInstrumentMapper::RemoveAllEntries(maps[i]);
2166        } catch (Exception e) {
2167            result.Error(e);
2168        }
2169        return result.Produce();
2170    }
2171    
2172    String LSCPServer::AddMidiInstrumentMap(String MapName) {
2173        dmsg(2,("LSCPServer: AddMidiInstrumentMap()\n"));
2174        LSCPResultSet result;
2175        try {
2176            int MapID = MidiInstrumentMapper::AddMap(MapName);
2177            result = LSCPResultSet(MapID);
2178        } catch (Exception e) {
2179            result.Error(e);
2180        }
2181        return result.Produce();
2182    }
2183    
2184    String LSCPServer::RemoveMidiInstrumentMap(uint MidiMapID) {
2185        dmsg(2,("LSCPServer: RemoveMidiInstrumentMap()\n"));
2186        LSCPResultSet result;
2187        try {
2188            MidiInstrumentMapper::RemoveMap(MidiMapID);
2189        } catch (Exception e) {
2190            result.Error(e);
2191        }
2192        return result.Produce();
2193    }
2194    
2195    String LSCPServer::RemoveAllMidiInstrumentMaps() {
2196        dmsg(2,("LSCPServer: RemoveAllMidiInstrumentMaps()\n"));
2197        LSCPResultSet result;
2198        try {
2199            MidiInstrumentMapper::RemoveAllMaps();
2200        } catch (Exception e) {
2201            result.Error(e);
2202        }
2203        return result.Produce();
2204    }
2205    
2206    String LSCPServer::GetMidiInstrumentMaps() {
2207        dmsg(2,("LSCPServer: GetMidiInstrumentMaps()\n"));
2208        LSCPResultSet result;
2209        try {
2210            result.Add(MidiInstrumentMapper::Maps().size());
2211        } catch (Exception e) {
2212            result.Error(e);
2213        }
2214        return result.Produce();
2215    }
2216    
2217    String LSCPServer::ListMidiInstrumentMaps() {
2218        dmsg(2,("LSCPServer: ListMidiInstrumentMaps()\n"));
2219        LSCPResultSet result;
2220        try {
2221            std::vector<int> maps = MidiInstrumentMapper::Maps();
2222            String sList;
2223            for (int i = 0; i < maps.size(); i++) {
2224                if (sList != "") sList += ",";
2225                sList += ToString(maps[i]);
2226            }
2227            result.Add(sList);
2228        } catch (Exception e) {
2229            result.Error(e);
2230        }
2231        return result.Produce();
2232    }
2233    
2234    String LSCPServer::GetMidiInstrumentMap(uint MidiMapID) {
2235        dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2236        LSCPResultSet result;
2237        try {
2238            result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2239            result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2240        } catch (Exception e) {
2241            result.Error(e);
2242        }
2243        return result.Produce();
2244    }
2245    
2246    String LSCPServer::SetMidiInstrumentMapName(uint MidiMapID, String NewName) {
2247        dmsg(2,("LSCPServer: SetMidiInstrumentMapName()\n"));
2248        LSCPResultSet result;
2249        try {
2250            MidiInstrumentMapper::RenameMap(MidiMapID, NewName);
2251        } catch (Exception e) {
2252            result.Error(e);
2253        }
2254        return result.Produce();
2255    }
2256    
2257    /**
2258     * Set the MIDI instrument map the given sampler channel shall use for
2259     * handling MIDI program change messages. There are the following two
2260     * special (negative) values:
2261     *
2262     *    - (-1) :  set to NONE (ignore program changes)
2263     *    - (-2) :  set to DEFAULT map
2264     */
2265    String LSCPServer::SetChannelMap(uint uiSamplerChannel, int MidiMapID) {
2266        dmsg(2,("LSCPServer: SetChannelMap()\n"));
2267        LSCPResultSet result;
2268        try {
2269            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2270            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2271    
2272            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2273            if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
2274    
2275            if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2276            else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
2277            else                      pEngineChannel->SetMidiInstrumentMap(MidiMapID);
2278        } catch (Exception e) {
2279            result.Error(e);
2280        }
2281        return result.Produce();
2282    }
2283    
2284    String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) {
2285        dmsg(2,("LSCPServer: CreateFxSend()\n"));
2286        LSCPResultSet result;
2287        try {
2288            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2289    
2290            FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2291            if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2292    
2293            result = LSCPResultSet(pFxSend->Id()); // success
2294        } catch (Exception e) {
2295            result.Error(e);
2296        }
2297        return result.Produce();
2298    }
2299    
2300    String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) {
2301        dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2302        LSCPResultSet result;
2303        try {
2304            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2305    
2306            FxSend* pFxSend = NULL;
2307            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2308                if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2309                    pFxSend = pEngineChannel->GetFxSend(i);
2310                    break;
2311                }
2312            }
2313            if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2314            pEngineChannel->RemoveFxSend(pFxSend);
2315        } catch (Exception e) {
2316            result.Error(e);
2317        }
2318        return result.Produce();
2319    }
2320    
2321    String LSCPServer::GetFxSends(uint uiSamplerChannel) {
2322        dmsg(2,("LSCPServer: GetFxSends()\n"));
2323        LSCPResultSet result;
2324        try {
2325            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2326    
2327            result.Add(pEngineChannel->GetFxSendCount());
2328        } catch (Exception e) {
2329            result.Error(e);
2330        }
2331        return result.Produce();
2332    }
2333    
2334    String LSCPServer::ListFxSends(uint uiSamplerChannel) {
2335        dmsg(2,("LSCPServer: ListFxSends()\n"));
2336        LSCPResultSet result;
2337        String list;
2338        try {
2339            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2340    
2341            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2342                FxSend* pFxSend = pEngineChannel->GetFxSend(i);
2343                if (list != "") list += ",";
2344                list += ToString(pFxSend->Id());
2345            }
2346            result.Add(list);
2347        } catch (Exception e) {
2348            result.Error(e);
2349        }
2350        return result.Produce();
2351    }
2352    
2353    FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2354        EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2355    
2356        FxSend* pFxSend = NULL;
2357        for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2358            if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2359                pFxSend = pEngineChannel->GetFxSend(i);
2360                break;
2361            }
2362        }
2363        if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2364        return pFxSend;
2365    }
2366    
2367    String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2368        dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2369        LSCPResultSet result;
2370        try {
2371            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2372            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2373    
2374            // gather audio routing informations
2375            String AudioRouting;
2376            for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
2377                if (AudioRouting != "") AudioRouting += ",";
2378                AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2379            }
2380    
2381            // success
2382            result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2383            result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2384            result.Add("LEVEL", ToString(pFxSend->Level()));
2385            result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2386        } catch (Exception e) {
2387            result.Error(e);
2388        }
2389        return result.Produce();
2390    }
2391    
2392    String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2393        dmsg(2,("LSCPServer: SetFxSendName()\n"));
2394        LSCPResultSet result;
2395        try {
2396            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2397    
2398            pFxSend->SetName(Name);
2399            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2400        } catch (Exception e) {
2401            result.Error(e);
2402        }
2403        return result.Produce();
2404    }
2405    
2406    String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2407        dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2408        LSCPResultSet result;
2409        try {
2410            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2411    
2412            pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2413            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2414        } catch (Exception e) {
2415            result.Error(e);
2416        }
2417        return result.Produce();
2418    }
2419    
2420    String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2421        dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2422        LSCPResultSet result;
2423        try {
2424            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2425    
2426            pFxSend->SetMidiController(MidiController);
2427            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2428        } catch (Exception e) {
2429            result.Error(e);
2430        }
2431        return result.Produce();
2432    }
2433    
2434    String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2435        dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2436        LSCPResultSet result;
2437        try {
2438            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2439    
2440            pFxSend->SetLevel((float)dLevel);
2441            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2442        } catch (Exception e) {
2443            result.Error(e);
2444        }
2445        return result.Produce();
2446    }
2447    
2448    String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2449        dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2450        LSCPResultSet result;
2451        try {
2452            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2453            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2454            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2455            if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
2456            if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2457            Engine* pEngine = pEngineChannel->GetEngine();
2458            InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2459            if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2460            InstrumentManager::instrument_id_t instrumentID;
2461            instrumentID.FileName = pEngineChannel->InstrumentFileName();
2462            instrumentID.Index    = pEngineChannel->InstrumentIndex();
2463            pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2464        } catch (Exception e) {
2465            result.Error(e);
2466        }
2467        return result.Produce();
2468    }
2469    
2470  /**  /**
2471   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
2472   */   */
# Line 1512  String LSCPServer::ResetChannel(uint uiS Line 2475  String LSCPServer::ResetChannel(uint uiS
2475      LSCPResultSet result;      LSCPResultSet result;
2476      try {      try {
2477          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2478          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2479          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2480          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
2481          pEngineChannel->Reset();          pEngineChannel->Reset();
2482      }      }
2483      catch (LinuxSamplerException e) {      catch (Exception e) {
2484           result.Error(e);           result.Error(e);
2485      }      }
2486      return result.Produce();      return result.Produce();
# Line 1539  String LSCPServer::ResetSampler() { Line 2502  String LSCPServer::ResetSampler() {
2502   */   */
2503  String LSCPServer::GetServerInfo() {  String LSCPServer::GetServerInfo() {
2504      dmsg(2,("LSCPServer: GetServerInfo()\n"));      dmsg(2,("LSCPServer: GetServerInfo()\n"));
2505        const std::string description =
2506            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2507      LSCPResultSet result;      LSCPResultSet result;
2508      result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");      result.Add("DESCRIPTION", description);
2509      result.Add("VERSION", VERSION);      result.Add("VERSION", VERSION);
2510      result.Add("PROTOCOL_VERSION", "1.0");      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2511    #if HAVE_SQLITE3
2512        result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2513    #else
2514        result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2515    #endif
2516    
2517        return result.Produce();
2518    }
2519    
2520    /**
2521     * Will be called by the parser to return the current number of all active streams.
2522     */
2523    String LSCPServer::GetTotalStreamCount() {
2524        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2525        LSCPResultSet result;
2526        result.Add(pSampler->GetDiskStreamCount());
2527      return result.Produce();      return result.Produce();
2528  }  }
2529    
# Line 1566  String LSCPServer::GetTotalVoiceCountMax Line 2547  String LSCPServer::GetTotalVoiceCountMax
2547      return result.Produce();      return result.Produce();
2548  }  }
2549    
2550    String LSCPServer::GetGlobalVolume() {
2551        LSCPResultSet result;
2552        result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp
2553        return result.Produce();
2554    }
2555    
2556    String LSCPServer::SetGlobalVolume(double dVolume) {
2557        LSCPResultSet result;
2558        try {
2559            if (dVolume < 0) throw Exception("Volume may not be negative");
2560            GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
2561            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
2562        } catch (Exception e) {
2563            result.Error(e);
2564        }
2565        return result.Produce();
2566    }
2567    
2568    String LSCPServer::GetFileInstruments(String Filename) {
2569        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2570        LSCPResultSet result;
2571        try {
2572            VerifyFile(Filename);
2573        } catch (Exception e) {
2574            result.Error(e);
2575            return result.Produce();
2576        }
2577        // try to find a sampler engine that can handle the file
2578        bool bFound = false;
2579        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2580        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2581            Engine* pEngine = NULL;
2582            try {
2583                pEngine = EngineFactory::Create(engineTypes[i]);
2584                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2585                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2586                if (pManager) {
2587                    std::vector<InstrumentManager::instrument_id_t> IDs =
2588                        pManager->GetInstrumentFileContent(Filename);
2589                    // return the amount of instruments in the file
2590                    result.Add(IDs.size());
2591                    // no more need to ask other engine types
2592                    bFound = true;
2593                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2594            } catch (Exception e) {
2595                // NOOP, as exception is thrown if engine doesn't support file
2596            }
2597            if (pEngine) EngineFactory::Destroy(pEngine);
2598        }
2599    
2600        if (!bFound) result.Error("Unknown file format");
2601        return result.Produce();
2602    }
2603    
2604    String LSCPServer::ListFileInstruments(String Filename) {
2605        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2606        LSCPResultSet result;
2607        try {
2608            VerifyFile(Filename);
2609        } catch (Exception e) {
2610            result.Error(e);
2611            return result.Produce();
2612        }
2613        // try to find a sampler engine that can handle the file
2614        bool bFound = false;
2615        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2616        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2617            Engine* pEngine = NULL;
2618            try {
2619                pEngine = EngineFactory::Create(engineTypes[i]);
2620                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2621                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2622                if (pManager) {
2623                    std::vector<InstrumentManager::instrument_id_t> IDs =
2624                        pManager->GetInstrumentFileContent(Filename);
2625                    // return a list of IDs of the instruments in the file
2626                    String s;
2627                    for (int j = 0; j < IDs.size(); j++) {
2628                        if (s.size()) s += ",";
2629                        s += ToString(IDs[j].Index);
2630                    }
2631                    result.Add(s);
2632                    // no more need to ask other engine types
2633                    bFound = true;
2634                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2635            } catch (Exception e) {
2636                // NOOP, as exception is thrown if engine doesn't support file
2637            }
2638            if (pEngine) EngineFactory::Destroy(pEngine);
2639        }
2640    
2641        if (!bFound) result.Error("Unknown file format");
2642        return result.Produce();
2643    }
2644    
2645    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2646        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2647        LSCPResultSet result;
2648        try {
2649            VerifyFile(Filename);
2650        } catch (Exception e) {
2651            result.Error(e);
2652            return result.Produce();
2653        }
2654        InstrumentManager::instrument_id_t id;
2655        id.FileName = Filename;
2656        id.Index    = InstrumentID;
2657        // try to find a sampler engine that can handle the file
2658        bool bFound = false;
2659        bool bFatalErr = false;
2660        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2661        for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
2662            Engine* pEngine = NULL;
2663            try {
2664                pEngine = EngineFactory::Create(engineTypes[i]);
2665                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2666                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2667                if (pManager) {
2668                    // check if the instrument index is valid
2669                    // FIXME: this won't work if an engine only supports parts of the instrument file
2670                    std::vector<InstrumentManager::instrument_id_t> IDs =
2671                        pManager->GetInstrumentFileContent(Filename);
2672                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2673                        std::stringstream ss;
2674                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2675                        bFatalErr = true;
2676                        throw Exception(ss.str());
2677                    }
2678                    // get the info of the requested instrument
2679                    InstrumentManager::instrument_info_t info =
2680                        pManager->GetInstrumentInfo(id);
2681                    // return detailed informations about the file
2682                    result.Add("NAME", info.InstrumentName);
2683                    result.Add("FORMAT_FAMILY", engineTypes[i]);
2684                    result.Add("FORMAT_VERSION", info.FormatVersion);
2685                    result.Add("PRODUCT", info.Product);
2686                    result.Add("ARTISTS", info.Artists);
2687                    // no more need to ask other engine types
2688                    bFound = true;
2689                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2690            } catch (Exception e) {
2691                // usually NOOP, as exception is thrown if engine doesn't support file
2692                if (bFatalErr) result.Error(e);
2693            }
2694            if (pEngine) EngineFactory::Destroy(pEngine);
2695        }
2696    
2697        if (!bFound && !bFatalErr) result.Error("Unknown file format");
2698        return result.Produce();
2699    }
2700    
2701    void LSCPServer::VerifyFile(String Filename) {
2702        #if WIN32
2703        WIN32_FIND_DATA win32FileAttributeData;
2704        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2705        if (!res) {
2706            std::stringstream ss;
2707            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2708            throw Exception(ss.str());
2709        }
2710        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2711            throw Exception("Directory is specified");
2712        }
2713        #else
2714        struct stat statBuf;
2715        int res = stat(Filename.c_str(), &statBuf);
2716        if (res) {
2717            std::stringstream ss;
2718            ss << "Fail to stat `" << Filename << "`: " << strerror(errno);
2719            throw Exception(ss.str());
2720        }
2721    
2722        if (S_ISDIR(statBuf.st_mode)) {
2723            throw Exception("Directory is specified");
2724        }
2725        #endif
2726    }
2727    
2728  /**  /**
2729   * 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
2730   * server for receiving event messages.   * server for receiving event messages.
# Line 1592  String LSCPServer::UnsubscribeNotificati Line 2751  String LSCPServer::UnsubscribeNotificati
2751      return result.Produce();      return result.Produce();
2752  }  }
2753    
2754  static int select_callback(void * lscpResultSet, int argc,  String LSCPServer::AddDbInstrumentDirectory(String Dir) {
2755                          char **argv, char **azColName)      dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
2756  {      LSCPResultSet result;
2757      LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;  #if HAVE_SQLITE3
2758      resultSet->Add(argc, argv);      try {
2759      return 0;          InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
2760        } catch (Exception e) {
2761             result.Error(e);
2762        }
2763    #else
2764        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2765    #endif
2766        return result.Produce();
2767    }
2768    
2769    String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
2770        dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
2771        LSCPResultSet result;
2772    #if HAVE_SQLITE3
2773        try {
2774            InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
2775        } catch (Exception e) {
2776             result.Error(e);
2777        }
2778    #else
2779        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2780    #endif
2781        return result.Produce();
2782    }
2783    
2784    String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
2785        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2786        LSCPResultSet result;
2787    #if HAVE_SQLITE3
2788        try {
2789            result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
2790        } catch (Exception e) {
2791             result.Error(e);
2792        }
2793    #else
2794        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2795    #endif
2796        return result.Produce();
2797    }
2798    
2799    String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
2800        dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2801        LSCPResultSet result;
2802    #if HAVE_SQLITE3
2803        try {
2804            String list;
2805            StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
2806    
2807            for (int i = 0; i < dirs->size(); i++) {
2808                if (list != "") list += ",";
2809                list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
2810            }
2811    
2812            result.Add(list);
2813        } catch (Exception e) {
2814             result.Error(e);
2815        }
2816    #else
2817        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2818    #endif
2819        return result.Produce();
2820    }
2821    
2822    String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
2823        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
2824        LSCPResultSet result;
2825    #if HAVE_SQLITE3
2826        try {
2827            DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2828    
2829            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2830            result.Add("CREATED", info.Created);
2831            result.Add("MODIFIED", info.Modified);
2832        } catch (Exception e) {
2833             result.Error(e);
2834        }
2835    #else
2836        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2837    #endif
2838        return result.Produce();
2839    }
2840    
2841    String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
2842        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
2843        LSCPResultSet result;
2844    #if HAVE_SQLITE3
2845        try {
2846            InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
2847        } catch (Exception e) {
2848             result.Error(e);
2849        }
2850    #else
2851        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2852    #endif
2853        return result.Produce();
2854    }
2855    
2856    String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
2857        dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2858        LSCPResultSet result;
2859    #if HAVE_SQLITE3
2860        try {
2861            InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
2862        } catch (Exception e) {
2863             result.Error(e);
2864        }
2865    #else
2866        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2867    #endif
2868        return result.Produce();
2869    }
2870    
2871    String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
2872        dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2873        LSCPResultSet result;
2874    #if HAVE_SQLITE3
2875        try {
2876            InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
2877        } catch (Exception e) {
2878             result.Error(e);
2879        }
2880    #else
2881        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2882    #endif
2883        return result.Produce();
2884    }
2885    
2886    String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
2887        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
2888        LSCPResultSet result;
2889    #if HAVE_SQLITE3
2890        try {
2891            InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
2892        } catch (Exception e) {
2893             result.Error(e);
2894        }
2895    #else
2896        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2897    #endif
2898        return result.Produce();
2899    }
2900    
2901    String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
2902        dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
2903        LSCPResultSet result;
2904    #if HAVE_SQLITE3
2905        try {
2906            int id;
2907            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2908            id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
2909            if (bBackground) result = id;
2910        } catch (Exception e) {
2911             result.Error(e);
2912        }
2913    #else
2914        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2915    #endif
2916        return result.Produce();
2917    }
2918    
2919    String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) {
2920        dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground));
2921        LSCPResultSet result;
2922    #if HAVE_SQLITE3
2923        try {
2924            int id;
2925            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2926            if (ScanMode.compare("RECURSIVE") == 0) {
2927               id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground);
2928            } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
2929               id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground);
2930            } else if (ScanMode.compare("FLAT") == 0) {
2931               id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground);
2932            } else {
2933                throw Exception("Unknown scan mode: " + ScanMode);
2934            }
2935    
2936            if (bBackground) result = id;
2937        } catch (Exception e) {
2938             result.Error(e);
2939        }
2940    #else
2941        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2942    #endif
2943        return result.Produce();
2944    }
2945    
2946    String LSCPServer::RemoveDbInstrument(String Instr) {
2947        dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
2948        LSCPResultSet result;
2949    #if HAVE_SQLITE3
2950        try {
2951            InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
2952        } catch (Exception e) {
2953             result.Error(e);
2954        }
2955    #else
2956        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2957    #endif
2958        return result.Produce();
2959    }
2960    
2961    String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
2962        dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2963        LSCPResultSet result;
2964    #if HAVE_SQLITE3
2965        try {
2966            result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
2967        } catch (Exception e) {
2968             result.Error(e);
2969        }
2970    #else
2971        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2972    #endif
2973        return result.Produce();
2974  }  }
2975    
2976  String LSCPServer::QueryDatabase(String query) {  String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
2977        dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2978      LSCPResultSet result;      LSCPResultSet result;
2979  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2980      char* zErrMsg = NULL;      try {
2981      sqlite3 *db;          String list;
2982      String selectStr = "SELECT " + query;          StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
2983    
2984            for (int i = 0; i < instrs->size(); i++) {
2985                if (list != "") list += ",";
2986                list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
2987            }
2988    
2989      int rc = sqlite3_open("linuxsampler.db", &db);          result.Add(list);
2990      if (rc == SQLITE_OK)      } catch (Exception e) {
2991      {           result.Error(e);
             rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);  
2992      }      }
2993      if ( rc != SQLITE_OK )  #else
2994      {      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2995              result.Error(String(zErrMsg), rc);  #endif
2996        return result.Produce();
2997    }
2998    
2999    String LSCPServer::GetDbInstrumentInfo(String Instr) {
3000        dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
3001        LSCPResultSet result;
3002    #if HAVE_SQLITE3
3003        try {
3004            DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
3005    
3006            result.Add("INSTRUMENT_FILE", info.InstrFile);
3007            result.Add("INSTRUMENT_NR", info.InstrNr);
3008            result.Add("FORMAT_FAMILY", info.FormatFamily);
3009            result.Add("FORMAT_VERSION", info.FormatVersion);
3010            result.Add("SIZE", (int)info.Size);
3011            result.Add("CREATED", info.Created);
3012            result.Add("MODIFIED", info.Modified);
3013            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3014            result.Add("IS_DRUM", info.IsDrum);
3015            result.Add("PRODUCT", _escapeLscpResponse(info.Product));
3016            result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
3017            result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
3018        } catch (Exception e) {
3019             result.Error(e);
3020      }      }
     sqlite3_close(db);  
3021  #else  #else
3022      result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3023  #endif  #endif
3024      return result.Produce();      return result.Produce();
3025  }  }
3026    
3027    String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
3028        dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
3029        LSCPResultSet result;
3030    #if HAVE_SQLITE3
3031        try {
3032            ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
3033    
3034            result.Add("FILES_TOTAL", job.FilesTotal);
3035            result.Add("FILES_SCANNED", job.FilesScanned);
3036            result.Add("SCANNING", job.Scanning);
3037            result.Add("STATUS", job.Status);
3038        } catch (Exception e) {
3039             result.Error(e);
3040        }
3041    #else
3042        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3043    #endif
3044        return result.Produce();
3045    }
3046    
3047    String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
3048        dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
3049        LSCPResultSet result;
3050    #if HAVE_SQLITE3
3051        try {
3052            InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
3053        } catch (Exception e) {
3054             result.Error(e);
3055        }
3056    #else
3057        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3058    #endif
3059        return result.Produce();
3060    }
3061    
3062    String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
3063        dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3064        LSCPResultSet result;
3065    #if HAVE_SQLITE3
3066        try {
3067            InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
3068        } catch (Exception e) {
3069             result.Error(e);
3070        }
3071    #else
3072        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3073    #endif
3074        return result.Produce();
3075    }
3076    
3077    String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
3078        dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3079        LSCPResultSet result;
3080    #if HAVE_SQLITE3
3081        try {
3082            InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
3083        } catch (Exception e) {
3084             result.Error(e);
3085        }
3086    #else
3087        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3088    #endif
3089        return result.Produce();
3090    }
3091    
3092    String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
3093        dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
3094        LSCPResultSet result;
3095    #if HAVE_SQLITE3
3096        try {
3097            InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
3098        } catch (Exception e) {
3099             result.Error(e);
3100        }
3101    #else
3102        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3103    #endif
3104        return result.Produce();
3105    }
3106    
3107    String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
3108        dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
3109        LSCPResultSet result;
3110    #if HAVE_SQLITE3
3111        try {
3112            SearchQuery Query;
3113            std::map<String,String>::iterator iter;
3114            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3115                if (iter->first.compare("NAME") == 0) {
3116                    Query.Name = iter->second;
3117                } else if (iter->first.compare("CREATED") == 0) {
3118                    Query.SetCreated(iter->second);
3119                } else if (iter->first.compare("MODIFIED") == 0) {
3120                    Query.SetModified(iter->second);
3121                } else if (iter->first.compare("DESCRIPTION") == 0) {
3122                    Query.Description = iter->second;
3123                } else {
3124                    throw Exception("Unknown search criteria: " + iter->first);
3125                }
3126            }
3127    
3128            String list;
3129            StringListPtr pDirectories =
3130                InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
3131    
3132            for (int i = 0; i < pDirectories->size(); i++) {
3133                if (list != "") list += ",";
3134                list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
3135            }
3136    
3137            result.Add(list);
3138        } catch (Exception e) {
3139             result.Error(e);
3140        }
3141    #else
3142        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3143    #endif
3144        return result.Produce();
3145    }
3146    
3147    String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
3148        dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
3149        LSCPResultSet result;
3150    #if HAVE_SQLITE3
3151        try {
3152            SearchQuery Query;
3153            std::map<String,String>::iterator iter;
3154            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3155                if (iter->first.compare("NAME") == 0) {
3156                    Query.Name = iter->second;
3157                } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
3158                    Query.SetFormatFamilies(iter->second);
3159                } else if (iter->first.compare("SIZE") == 0) {
3160                    Query.SetSize(iter->second);
3161                } else if (iter->first.compare("CREATED") == 0) {
3162                    Query.SetCreated(iter->second);
3163                } else if (iter->first.compare("MODIFIED") == 0) {
3164                    Query.SetModified(iter->second);
3165                } else if (iter->first.compare("DESCRIPTION") == 0) {
3166                    Query.Description = iter->second;
3167                } else if (iter->first.compare("IS_DRUM") == 0) {
3168                    if (!strcasecmp(iter->second.c_str(), "true")) {
3169                        Query.InstrType = SearchQuery::DRUM;
3170                    } else {
3171                        Query.InstrType = SearchQuery::CHROMATIC;
3172                    }
3173                } else if (iter->first.compare("PRODUCT") == 0) {
3174                     Query.Product = iter->second;
3175                } else if (iter->first.compare("ARTISTS") == 0) {
3176                     Query.Artists = iter->second;
3177                } else if (iter->first.compare("KEYWORDS") == 0) {
3178                     Query.Keywords = iter->second;
3179                } else {
3180                    throw Exception("Unknown search criteria: " + iter->first);
3181                }
3182            }
3183    
3184            String list;
3185            StringListPtr pInstruments =
3186                InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
3187    
3188            for (int i = 0; i < pInstruments->size(); i++) {
3189                if (list != "") list += ",";
3190                list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
3191            }
3192    
3193            result.Add(list);
3194        } catch (Exception e) {
3195             result.Error(e);
3196        }
3197    #else
3198        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3199    #endif
3200        return result.Produce();
3201    }
3202    
3203    String LSCPServer::FormatInstrumentsDb() {
3204        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
3205        LSCPResultSet result;
3206    #if HAVE_SQLITE3
3207        try {
3208            InstrumentsDb::GetInstrumentsDb()->Format();
3209        } catch (Exception e) {
3210             result.Error(e);
3211        }
3212    #else
3213        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3214    #endif
3215        return result.Produce();
3216    }
3217    
3218    
3219  /**  /**
3220   * 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
3221   * mode is enabled, all commands from the client will (immediately) be   * mode is enabled, all commands from the client will (immediately) be
# Line 1634  String LSCPServer::SetEcho(yyparse_param Line 3227  String LSCPServer::SetEcho(yyparse_param
3227      try {      try {
3228          if      (boolean_value == 0) pSession->bVerbose = false;          if      (boolean_value == 0) pSession->bVerbose = false;
3229          else if (boolean_value == 1) pSession->bVerbose = true;          else if (boolean_value == 1) pSession->bVerbose = true;
3230          else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1");          else throw Exception("Not a boolean value, must either be 0 or 1");
3231      }      }
3232      catch (LinuxSamplerException e) {      catch (Exception e) {
3233           result.Error(e);           result.Error(e);
3234      }      }
3235      return result.Produce();      return result.Produce();

Legend:
Removed from v.778  
changed lines
  Added in v.1724

  ViewVC Help
Powered by ViewVC