/[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 556 by schoenebeck, Sat May 21 01:10:12 2005 UTC revision 2135 by schoenebeck, Thu Sep 30 20:00:43 2010 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 - 2009 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 21  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24    #include <algorithm>
25    #include <string>
26    
27    #include "../common/File.h"
28  #include "lscpserver.h"  #include "lscpserver.h"
29  #include "lscpresultset.h"  #include "lscpresultset.h"
30  #include "lscpevent.h"  #include "lscpevent.h"
 //#include "../common/global.h"  
31    
32    #if defined(WIN32)
33    #include <windows.h>
34    #else
35  #include <fcntl.h>  #include <fcntl.h>
36    #endif
37    
38  #if HAVE_SQLITE3  #if ! HAVE_SQLITE3
39  # include "sqlite3.h"  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
40  #endif  #endif
41    
42  #include "../engines/EngineFactory.h"  #include "../engines/EngineFactory.h"
43    #include "../engines/EngineChannelFactory.h"
44  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
45  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
46    #include "../effects/EffectFactory.h"
47    
48    namespace LinuxSampler {
49    
50    /**
51     * Returns a copy of the given string where all special characters are
52     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
53     * to escape LSCP response fields in case the respective response field is
54     * actually defined as using escape sequences in the LSCP specs.
55     *
56     * @e Caution: DO NOT use this function for escaping path based responses,
57     * use the Path class (src/common/Path.h) for this instead!
58     */
59    static String _escapeLscpResponse(String txt) {
60        for (int i = 0; i < txt.length(); i++) {
61            const char c = txt.c_str()[i];
62            if (
63                !(c >= '0' && c <= '9') &&
64                !(c >= 'a' && c <= 'z') &&
65                !(c >= 'A' && c <= 'Z') &&
66                !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
67                !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
68                !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
69                !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
70                !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
71                !(c == '@') && !(c == '[') && !(c == ']') &&
72                !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
73                !(c == '|') && !(c == '}') && !(c == '~')
74            ) {
75                // convert the "special" character into a "\xHH" LSCP escape sequence
76                char buf[5];
77                snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
78                txt.replace(i, 1, buf);
79                i += 3;
80            }
81        }
82        return txt;
83    }
84    
85  /**  /**
86   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
# Line 52  Line 98 
98  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
99  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
100  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
101    std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
102  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
103  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
104  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
# Line 60  Mutex LSCPServer::NotifyBufferMutex = Mu Line 107  Mutex LSCPServer::NotifyBufferMutex = Mu
107  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
108  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
109    
110  LSCPServer::LSCPServer(Sampler* pSampler) : Thread(true, false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
111        SocketAddress.sin_family      = AF_INET;
112        SocketAddress.sin_addr.s_addr = addr;
113        SocketAddress.sin_port        = port;
114      this->pSampler = pSampler;      this->pSampler = pSampler;
115        LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_count, "AUDIO_OUTPUT_DEVICE_COUNT");
116        LSCPEvent::RegisterEvent(LSCPEvent::event_audio_device_info, "AUDIO_OUTPUT_DEVICE_INFO");
117        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_count, "MIDI_INPUT_DEVICE_COUNT");
118        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_device_info, "MIDI_INPUT_DEVICE_INFO");
119      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_count, "CHANNEL_COUNT");
120      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
121      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
122      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");      LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
123      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_channel_info, "CHANNEL_INFO");
124        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_count, "FX_SEND_COUNT");
125        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_send_info, "FX_SEND_INFO");
126        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_count, "MIDI_INSTRUMENT_MAP_COUNT");
127        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_map_info, "MIDI_INSTRUMENT_MAP_INFO");
128        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_count, "MIDI_INSTRUMENT_COUNT");
129        LSCPEvent::RegisterEvent(LSCPEvent::event_midi_instr_info, "MIDI_INSTRUMENT_INFO");
130        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_count, "DB_INSTRUMENT_DIRECTORY_COUNT");
131        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
132        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
133        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
134        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
135      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
136        LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
137        LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
138        LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
139        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
140        LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
141      hSocket = -1;      hSocket = -1;
142  }  }
143    
144  LSCPServer::~LSCPServer() {  LSCPServer::~LSCPServer() {
145        CloseAllConnections();
146        InstrumentManager::StopBackgroundThread();
147    #if defined(WIN32)
148        if (hSocket >= 0) closesocket(hSocket);
149    #else
150      if (hSocket >= 0) close(hSocket);      if (hSocket >= 0) close(hSocket);
151    #endif
152    }
153    
154    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
155        this->pParent = pParent;
156    }
157    
158    LSCPServer::EventHandler::~EventHandler() {
159        std::vector<midi_listener_entry> l = channelMidiListeners;
160        channelMidiListeners.clear();
161        for (int i = 0; i < l.size(); i++)
162            delete l[i].pMidiListener;
163    }
164    
165    void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
166        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
167    }
168    
169    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
170        pChannel->AddEngineChangeListener(this);
171    }
172    
173    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
174        if (!pChannel->GetEngineChannel()) return;
175        EngineToBeChanged(pChannel->Index());
176    }
177    
178    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
179        SamplerChannel* pSamplerChannel =
180            pParent->pSampler->GetSamplerChannel(ChannelId);
181        if (!pSamplerChannel) return;
182        EngineChannel* pEngineChannel =
183            pSamplerChannel->GetEngineChannel();
184        if (!pEngineChannel) return;
185        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
186            if ((*iter).pEngineChannel == pEngineChannel) {
187                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
188                pEngineChannel->Disconnect(pMidiListener);
189                channelMidiListeners.erase(iter);
190                delete pMidiListener;
191                return;
192            }
193        }
194    }
195    
196    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
197        SamplerChannel* pSamplerChannel =
198            pParent->pSampler->GetSamplerChannel(ChannelId);
199        if (!pSamplerChannel) return;
200        EngineChannel* pEngineChannel =
201            pSamplerChannel->GetEngineChannel();
202        if (!pEngineChannel) return;
203        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
204        pEngineChannel->Connect(pMidiListener);
205        midi_listener_entry entry = {
206            pSamplerChannel, pEngineChannel, pMidiListener
207        };
208        channelMidiListeners.push_back(entry);
209    }
210    
211    void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
212        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
213    }
214    
215    void LSCPServer::EventHandler::MidiDeviceCountChanged(int NewCount) {
216        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
217    }
218    
219    void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
220        pDevice->RemoveMidiPortCountListener(this);
221        for (int i = 0; i < pDevice->PortCount(); ++i)
222            MidiPortToBeRemoved(pDevice->GetPort(i));
223    }
224    
225    void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
226        pDevice->AddMidiPortCountListener(this);
227        for (int i = 0; i < pDevice->PortCount(); ++i)
228            MidiPortAdded(pDevice->GetPort(i));
229    }
230    
231    void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
232        // yet unused
233    }
234    
235    void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
236        for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
237            if ((*iter).pPort == pPort) {
238                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
239                pPort->Disconnect(pMidiListener);
240                deviceMidiListeners.erase(iter);
241                delete pMidiListener;
242                return;
243            }
244        }
245    }
246    
247    void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
248        // find out the device ID
249        std::map<uint, MidiInputDevice*> devices =
250            pParent->pSampler->GetMidiInputDevices();
251        for (
252            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
253            iter != devices.end(); ++iter
254        ) {
255            if (iter->second == pPort->GetDevice()) { // found
256                VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
257                pPort->Connect(pMidiListener);
258                device_midi_listener_entry entry = {
259                    pPort, pMidiListener, iter->first
260                };
261                deviceMidiListeners.push_back(entry);
262                return;
263            }
264        }
265    }
266    
267    void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
268        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
269    }
270    
271    void LSCPServer::EventHandler::MidiInstrumentInfoChanged(int MapId, int Bank, int Program) {
272        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, MapId, Bank, Program));
273    }
274    
275    void LSCPServer::EventHandler::MidiInstrumentMapCountChanged(int NewCount) {
276        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, NewCount));
277    }
278    
279    void LSCPServer::EventHandler::MidiInstrumentMapInfoChanged(int MapId) {
280        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, MapId));
281    }
282    
283    void LSCPServer::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
284        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_count, ChannelId, NewCount));
285    }
286    
287    void LSCPServer::EventHandler::VoiceCountChanged(int ChannelId, int NewCount) {
288        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_voice_count, ChannelId, NewCount));
289    }
290    
291    void LSCPServer::EventHandler::StreamCountChanged(int ChannelId, int NewCount) {
292        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_stream_count, ChannelId, NewCount));
293    }
294    
295    void LSCPServer::EventHandler::BufferFillChanged(int ChannelId, String FillData) {
296        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_buffer_fill, ChannelId, FillData));
297    }
298    
299    void LSCPServer::EventHandler::TotalVoiceCountChanged(int NewCount) {
300        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
301    }
302    
303    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
304        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
305    }
306    
307    #if HAVE_SQLITE3
308    void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
309        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
310    }
311    
312    void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
313        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
314    }
315    
316    void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
317        Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
318        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
319        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
320    }
321    
322    void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
323        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
324    }
325    
326    void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
327        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
328    }
329    
330    void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
331        Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
332        NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
333        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
334    }
335    
336    void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
337        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
338    }
339    #endif // HAVE_SQLITE3
340    
341    void LSCPServer::RemoveListeners() {
342        pSampler->RemoveChannelCountListener(&eventHandler);
343        pSampler->RemoveAudioDeviceCountListener(&eventHandler);
344        pSampler->RemoveMidiDeviceCountListener(&eventHandler);
345        pSampler->RemoveVoiceCountListener(&eventHandler);
346        pSampler->RemoveStreamCountListener(&eventHandler);
347        pSampler->RemoveBufferFillListener(&eventHandler);
348        pSampler->RemoveTotalStreamCountListener(&eventHandler);
349        pSampler->RemoveTotalVoiceCountListener(&eventHandler);
350        pSampler->RemoveFxSendCountListener(&eventHandler);
351        MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler);
352        MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler);
353        MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler);
354        MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler);
355    #if HAVE_SQLITE3
356        InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler);
357    #endif
358  }  }
359    
360  /**  /**
# Line 90  int LSCPServer::WaitUntilInitialized(lon Line 372  int LSCPServer::WaitUntilInitialized(lon
372  }  }
373    
374  int LSCPServer::Main() {  int LSCPServer::Main() {
375            #if defined(WIN32)
376            WSADATA wsaData;
377            int iResult;
378            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
379            if (iResult != 0) {
380                    std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
381                    exit(EXIT_FAILURE);
382            }
383            #endif
384      hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
385      if (hSocket < 0) {      if (hSocket < 0) {
386          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
# Line 97  int LSCPServer::Main() { Line 388  int LSCPServer::Main() {
388          exit(EXIT_FAILURE);          exit(EXIT_FAILURE);
389      }      }
390    
     SocketAddress.sin_family      = AF_INET;  
     SocketAddress.sin_port        = htons(LSCP_PORT);  
     SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);  
   
391      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {      if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
392          std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";          std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
393          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds          for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
394              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
395                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
396                      std::cerr << "gave up!" << std::endl;                      std::cerr << "gave up!" << std::endl;
397                        #if defined(WIN32)
398                        closesocket(hSocket);
399                        #else
400                      close(hSocket);                      close(hSocket);
401                        #endif
402                      //return -1;                      //return -1;
403                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
404                  }                  }
# Line 120  int LSCPServer::Main() { Line 411  int LSCPServer::Main() {
411      listen(hSocket, 1);      listen(hSocket, 1);
412      Initialized.Set(true);      Initialized.Set(true);
413    
414        // Registering event listeners
415        pSampler->AddChannelCountListener(&eventHandler);
416        pSampler->AddAudioDeviceCountListener(&eventHandler);
417        pSampler->AddMidiDeviceCountListener(&eventHandler);
418        pSampler->AddVoiceCountListener(&eventHandler);
419        pSampler->AddStreamCountListener(&eventHandler);
420        pSampler->AddBufferFillListener(&eventHandler);
421        pSampler->AddTotalStreamCountListener(&eventHandler);
422        pSampler->AddTotalVoiceCountListener(&eventHandler);
423        pSampler->AddFxSendCountListener(&eventHandler);
424        MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
425        MidiInstrumentMapper::AddMidiInstrumentInfoListener(&eventHandler);
426        MidiInstrumentMapper::AddMidiInstrumentMapCountListener(&eventHandler);
427        MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(&eventHandler);
428    #if HAVE_SQLITE3
429        InstrumentsDb::GetInstrumentsDb()->AddInstrumentsDbListener(&dbInstrumentsEventHandler);
430    #endif
431      // now wait for client connections and handle their requests      // now wait for client connections and handle their requests
432      sockaddr_in client;      sockaddr_in client;
433      int length = sizeof(client);      int length = sizeof(client);
# Line 127  int LSCPServer::Main() { Line 435  int LSCPServer::Main() {
435      FD_SET(hSocket, &fdSet);      FD_SET(hSocket, &fdSet);
436      int maxSessions = hSocket;      int maxSessions = hSocket;
437    
438        timeval timeout;
439    
440      while (true) {      while (true) {
441          fd_set selectSet = fdSet;          #if CONFIG_PTHREAD_TESTCANCEL
442          int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);                  TestCancel();
443          if (retval == 0)          #endif
444            // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
445            {
446                std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
447                std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
448                std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
449                for (; itEngineChannel != itEnd; ++itEngineChannel) {
450                    if ((*itEngineChannel)->StatusChanged()) {
451                        SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index()));
452                    }
453    
454                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
455                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
456                        if(fxs != NULL && fxs->IsInfoChanged()) {
457                            int chn = (*itEngineChannel)->GetSamplerChannel()->Index();
458                            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
459                            fxs->SetInfoChanged(false);
460                        }
461                    }
462                }
463            }
464    
465            // check if MIDI data arrived on some engine channel
466            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
467                const EventHandler::midi_listener_entry entry =
468                    eventHandler.channelMidiListeners[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_channel_midi,
477                                    entry.pSamplerChannel->Index(),
478                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
479                                    iNote,
480                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
481                                            : pMidiListener->NoteOffVelocity(iNote)
482                                )
483                            );
484                        }
485                    }
486                }
487            }
488    
489            // check if MIDI data arrived on some MIDI device
490            for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
491                const EventHandler::device_midi_listener_entry entry =
492                    eventHandler.deviceMidiListeners[i];
493                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
494                if (pMidiListener->NotesChanged()) {
495                    for (int iNote = 0; iNote < 128; iNote++) {
496                        if (pMidiListener->NoteChanged(iNote)) {
497                            const bool bActive = pMidiListener->NoteIsActive(iNote);
498                            LSCPServer::SendLSCPNotify(
499                                LSCPEvent(
500                                    LSCPEvent::event_device_midi,
501                                    entry.uiDeviceID,
502                                    entry.pPort->GetPortNumber(),
503                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
504                                    iNote,
505                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
506                                            : pMidiListener->NoteOffVelocity(iNote)
507                                )
508                            );
509                        }
510                    }
511                }
512            }
513    
514            //Now let's deliver late notifies (if any)
515            NotifyBufferMutex.Lock();
516            for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
517    #ifdef MSG_NOSIGNAL
518                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
519    #else
520                    send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
521    #endif
522            }
523            bufferedNotifies.clear();
524            NotifyBufferMutex.Unlock();
525    
526            fd_set selectSet = fdSet;
527            timeout.tv_sec  = 0;
528            timeout.tv_usec = 100000;
529    
530            int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
531    
532            if (retval == 0 || (retval == -1 && errno == EINTR))
533                  continue; //Nothing try again                  continue; //Nothing try again
534          if (retval == -1) {          if (retval == -1) {
535                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
536                    #if defined(WIN32)
537                    closesocket(hSocket);
538                    #else
539                  close(hSocket);                  close(hSocket);
540                    #endif
541                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
542          }          }
543    
# Line 146  int LSCPServer::Main() { Line 549  int LSCPServer::Main() {
549                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
550                  }                  }
551    
552                    #if defined(WIN32)
553                    u_long nonblock_io = 1;
554                    if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
555                      std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
556                      exit(EXIT_FAILURE);
557                    }
558            #else
559                    struct linger linger;
560                    linger.l_onoff = 1;
561                    linger.l_linger = 0;
562                    if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) {
563                        std::cerr << "LSCPServer: Failed to set SO_LINGER\n";
564                    }
565    
566                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
567                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
568                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
569                  }                  }
570                    #endif
571    
572                  // Parser initialization                  // Parser initialization
573                  yyparse_param_t yyparse_param;                  yyparse_param_t yyparse_param;
# Line 173  int LSCPServer::Main() { Line 591  int LSCPServer::Main() {
591                                  int dummy; // just a temporary hack to fulfill the restart() function prototype                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
592                                  restart(NULL, dummy); // restart the 'scanner'                                  restart(NULL, dummy); // restart the 'scanner'
593                                  currentSocket = (*iter).hSession;  //a hack                                  currentSocket = (*iter).hSession;  //a hack
594                                    itCurrentSession = iter; // another hack
595                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
596                                  if ((*iter).bVerbose) { // if echo mode enabled                                  if ((*iter).bVerbose) { // if echo mode enabled
597                                      AnswerClient(bufferedCommands[currentSocket]);                                      AnswerClient(bufferedCommands[currentSocket]);
598                                  }                                  }
599                                  int result = yyparse(&(*iter));                                  int result = yyparse(&(*iter));
600                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
601                                    itCurrentSession = Sessions.end(); // hack as well
602                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
603                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?
604                                          CloseConnection(iter);                                          CloseConnection(iter);
# Line 189  int LSCPServer::Main() { Line 609  int LSCPServer::Main() {
609                          break;                          break;
610                  }                  }
611          }          }
   
         //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(), 0);  
                 bufferedNotifies.erase(iterNotify);  
         }  
         NotifyBufferMutex.Unlock();  
612      }      }
613  }  }
614    
# Line 214  void LSCPServer::CloseConnection( std::v Line 626  void LSCPServer::CloseConnection( std::v
626          NotifyMutex.Lock();          NotifyMutex.Lock();
627          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
628          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
629            #if defined(WIN32)
630            closesocket(socket);
631            #else
632          close(socket);          close(socket);
633            #endif
634          NotifyMutex.Unlock();          NotifyMutex.Unlock();
635  }  }
636    
637    void LSCPServer::CloseAllConnections() {
638        std::vector<yyparse_param_t>::iterator iter = Sessions.begin();
639        while(iter != Sessions.end()) {
640            CloseConnection(iter);
641            iter = Sessions.begin();
642        }
643    }
644    
645    void LSCPServer::LockRTNotify() {
646        RTNotifyMutex.Lock();
647    }
648    
649    void LSCPServer::UnlockRTNotify() {
650        RTNotifyMutex.Unlock();
651    }
652    
653  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
654          int subs = 0;          int subs = 0;
655          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 243  void LSCPServer::SendLSCPNotify( LSCPEve Line 675  void LSCPServer::SendLSCPNotify( LSCPEve
675          while (true) {          while (true) {
676                  if (NotifyMutex.Trylock()) {                  if (NotifyMutex.Trylock()) {
677                          for(;iter != end; iter++)                          for(;iter != end; iter++)
678    #ifdef MSG_NOSIGNAL
679                                    send(*iter, notify.c_str(), notify.size(), MSG_NOSIGNAL);
680    #else
681                                  send(*iter, notify.c_str(), notify.size(), 0);                                  send(*iter, notify.c_str(), notify.size(), 0);
682    #endif
683                          NotifyMutex.Unlock();                          NotifyMutex.Unlock();
684                          break;                          break;
685                  } else {                  } else {
# Line 275  extern int GetLSCPCommand( void *buf, in Line 711  extern int GetLSCPCommand( void *buf, in
711          return command.size();          return command.size();
712  }  }
713    
714    extern yyparse_param_t* GetCurrentYaccSession() {
715        return &(*itCurrentSession);
716    }
717    
718  /**  /**
719   * Will be called to try to read the command from the socket   * Will be called to try to read the command from the socket
720   * 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 285  bool LSCPServer::GetLSCPCommand( std::ve Line 725  bool LSCPServer::GetLSCPCommand( std::ve
725          char c;          char c;
726          int i = 0;          int i = 0;
727          while (true) {          while (true) {
728                    #if defined(WIN32)
729                    int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
730                    #else
731                  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
732                    #endif
733                  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
734                          CloseConnection(iter);                          CloseConnection(iter);
735                          break;                          break;
# Line 295  bool LSCPServer::GetLSCPCommand( std::ve Line 739  bool LSCPServer::GetLSCPCommand( std::ve
739                                  continue; //Ignore CR                                  continue; //Ignore CR
740                          if (c == '\n') {                          if (c == '\n') {
741                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));                                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
742                                  bufferedCommands[socket] += "\n";                                  bufferedCommands[socket] += "\r\n";
743                                  return true; //Complete command was read                                  return true; //Complete command was read
744                          }                          }
745                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
746                  }                  }
747                    #if defined(WIN32)
748                    if (result == SOCKET_ERROR) {
749                        int wsa_lasterror = WSAGetLastError();
750                            if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
751                                    return false;
752                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
753                            CloseConnection(iter);
754                            break;
755                    }
756                    #else
757                  if (result == -1) {                  if (result == -1) {
758                          if (errno == EAGAIN) //Would block, try again later.                          if (errno == EAGAIN) //Would block, try again later.
759                                  return false;                                  return false;
# Line 338  bool LSCPServer::GetLSCPCommand( std::ve Line 792  bool LSCPServer::GetLSCPCommand( std::ve
792                          CloseConnection(iter);                          CloseConnection(iter);
793                          break;                          break;
794                  }                  }
795                    #endif
796          }          }
797          return false;          return false;
798  }  }
# Line 352  void LSCPServer::AnswerClient(String Ret Line 807  void LSCPServer::AnswerClient(String Ret
807      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
808      if (currentSocket != -1) {      if (currentSocket != -1) {
809              NotifyMutex.Lock();              NotifyMutex.Lock();
810    #ifdef MSG_NOSIGNAL
811                send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
812    #else
813              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
814    #endif
815              NotifyMutex.Unlock();              NotifyMutex.Unlock();
816      }      }
817  }  }
# Line 396  String LSCPServer::CreateAudioOutputDevi Line 855  String LSCPServer::CreateAudioOutputDevi
855          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);          AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
856          // search for the created device to get its index          // search for the created device to get its index
857          int index = GetAudioOutputDeviceIndex(pDevice);          int index = GetAudioOutputDeviceIndex(pDevice);
858          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.");
859          result = index; // success          result = index; // success
860      }      }
861      catch (LinuxSamplerException e) {      catch (Exception e) {
862          result.Error(e);          result.Error(e);
863      }      }
864      return result.Produce();      return result.Produce();
# Line 412  String LSCPServer::CreateMidiInputDevice Line 871  String LSCPServer::CreateMidiInputDevice
871          MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);          MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
872          // search for the created device to get its index          // search for the created device to get its index
873          int index = GetMidiInputDeviceIndex(pDevice);          int index = GetMidiInputDeviceIndex(pDevice);
874          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.");
875          result = index; // success          result = index; // success
876      }      }
877      catch (LinuxSamplerException e) {      catch (Exception e) {
878          result.Error(e);          result.Error(e);
879      }      }
880      return result.Produce();      return result.Produce();
# Line 426  String LSCPServer::DestroyAudioOutputDev Line 885  String LSCPServer::DestroyAudioOutputDev
885      LSCPResultSet result;      LSCPResultSet result;
886      try {      try {
887          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
888          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) + ".");
889          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
890          pSampler->DestroyAudioOutputDevice(pDevice);          pSampler->DestroyAudioOutputDevice(pDevice);
891      }      }
892      catch (LinuxSamplerException e) {      catch (Exception e) {
893          result.Error(e);          result.Error(e);
894      }      }
895      return result.Produce();      return result.Produce();
# Line 441  String LSCPServer::DestroyMidiInputDevic Line 900  String LSCPServer::DestroyMidiInputDevic
900      LSCPResultSet result;      LSCPResultSet result;
901      try {      try {
902          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
903          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) + ".");
904          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
905          pSampler->DestroyMidiInputDevice(pDevice);          pSampler->DestroyMidiInputDevice(pDevice);
906      }      }
907      catch (LinuxSamplerException e) {      catch (Exception e) {
908          result.Error(e);          result.Error(e);
909      }      }
910      return result.Produce();      return result.Produce();
911  }  }
912    
913    EngineChannel* LSCPServer::GetEngineChannel(uint uiSamplerChannel) {
914        SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
915        if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
916    
917        EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
918        if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
919    
920        return pEngineChannel;
921    }
922    
923  /**  /**
924   * Will be called by the parser to load an instrument.   * Will be called by the parser to load an instrument.
925   */   */
# Line 459  String LSCPServer::LoadInstrument(String Line 928  String LSCPServer::LoadInstrument(String
928      LSCPResultSet result;      LSCPResultSet result;
929      try {      try {
930          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
931          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
932          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
933          if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet");          if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel yet");
934          if (!pSamplerChannel->GetAudioOutputDevice())          if (!pSamplerChannel->GetAudioOutputDevice())
935              throw LinuxSamplerException("No audio output device connected to sampler channel");              throw Exception("No audio output device connected to sampler channel");
936          if (bBackground) {          if (bBackground) {
937              InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel);              InstrumentManager::instrument_id_t id;
938                id.FileName = Filename;
939                id.Index    = uiInstrument;
940                InstrumentManager::LoadInstrumentInBackground(id, pEngineChannel);
941          }          }
942          else {          else {
943              // tell the engine channel which instrument to load              // tell the engine channel which instrument to load
# Line 474  String LSCPServer::LoadInstrument(String Line 946  String LSCPServer::LoadInstrument(String
946              pEngineChannel->LoadInstrument();              pEngineChannel->LoadInstrument();
947          }          }
948      }      }
949      catch (LinuxSamplerException e) {      catch (Exception e) {
950           result.Error(e);           result.Error(e);
951      }      }
952      return result.Produce();      return result.Produce();
# Line 485  String LSCPServer::LoadInstrument(String Line 957  String LSCPServer::LoadInstrument(String
957   * sampler channel.   * sampler channel.
958   */   */
959  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {  String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
960      dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));      dmsg(2,("LSCPServer: SetEngineType(EngineName=%s,uiSamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
961      LSCPResultSet result;      LSCPResultSet result;
962      try {      try {
963          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
964          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
965          LockRTNotify();          LockRTNotify();
966          pSamplerChannel->SetEngineType(EngineName);          pSamplerChannel->SetEngineType(EngineName);
967            if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
968          UnlockRTNotify();          UnlockRTNotify();
969      }      }
970      catch (LinuxSamplerException e) {      catch (Exception e) {
971           result.Error(e);           result.Error(e);
972      }      }
973      return result.Produce();      return result.Produce();
# Line 532  String LSCPServer::ListChannels() { Line 1005  String LSCPServer::ListChannels() {
1005   */   */
1006  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
1007      dmsg(2,("LSCPServer: AddChannel()\n"));      dmsg(2,("LSCPServer: AddChannel()\n"));
1008        LockRTNotify();
1009      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
1010        UnlockRTNotify();
1011      LSCPResultSet result(pSamplerChannel->Index());      LSCPResultSet result(pSamplerChannel->Index());
1012      return result.Produce();      return result.Produce();
1013  }  }
# Line 554  String LSCPServer::RemoveChannel(uint ui Line 1029  String LSCPServer::RemoveChannel(uint ui
1029   */   */
1030  String LSCPServer::GetAvailableEngines() {  String LSCPServer::GetAvailableEngines() {
1031      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));      dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
1032      LSCPResultSet result("1");      LSCPResultSet result;
1033        try {
1034            int n = EngineFactory::AvailableEngineTypes().size();
1035            result.Add(n);
1036        }
1037        catch (Exception e) {
1038            result.Error(e);
1039        }
1040      return result.Produce();      return result.Produce();
1041  }  }
1042    
# Line 563  String LSCPServer::GetAvailableEngines() Line 1045  String LSCPServer::GetAvailableEngines()
1045   */   */
1046  String LSCPServer::ListAvailableEngines() {  String LSCPServer::ListAvailableEngines() {
1047      dmsg(2,("LSCPServer: ListAvailableEngines()\n"));      dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
1048      LSCPResultSet result("\'GIG\'");      LSCPResultSet result;
1049        try {
1050            String s = EngineFactory::AvailableEngineTypesAsString();
1051            result.Add(s);
1052        }
1053        catch (Exception e) {
1054            result.Error(e);
1055        }
1056      return result.Produce();      return result.Produce();
1057  }  }
1058    
# Line 574  String LSCPServer::ListAvailableEngines( Line 1063  String LSCPServer::ListAvailableEngines(
1063  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
1064      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
1065      LSCPResultSet result;      LSCPResultSet result;
1066        LockRTNotify();
1067      try {      try {
1068          Engine* pEngine = EngineFactory::Create(EngineName);          Engine* pEngine = EngineFactory::Create(EngineName);
1069          result.Add("DESCRIPTION", pEngine->Description());          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1070          result.Add("VERSION",     pEngine->Version());          result.Add("VERSION",     pEngine->Version());
1071          delete pEngine;          EngineFactory::Destroy(pEngine);
1072      }      }
1073      catch (LinuxSamplerException e) {      catch (Exception e) {
1074           result.Error(e);           result.Error(e);
1075      }      }
1076        UnlockRTNotify();
1077      return result.Produce();      return result.Produce();
1078  }  }
1079    
# Line 595  String LSCPServer::GetChannelInfo(uint u Line 1086  String LSCPServer::GetChannelInfo(uint u
1086      LSCPResultSet result;      LSCPResultSet result;
1087      try {      try {
1088          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1089          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1090          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1091    
1092          //Defaults values          //Defaults values
# Line 607  String LSCPServer::GetChannelInfo(uint u Line 1098  String LSCPServer::GetChannelInfo(uint u
1098          int InstrumentStatus = -1;          int InstrumentStatus = -1;
1099          int AudioOutputChannels = 0;          int AudioOutputChannels = 0;
1100          String AudioRouting;          String AudioRouting;
1101            int Mute = 0;
1102            bool Solo = false;
1103            String MidiInstrumentMap = "NONE";
1104    
1105          if (pEngineChannel) {          if (pEngineChannel) {
1106              EngineName          = pEngineChannel->EngineName();              EngineName          = pEngineChannel->EngineName();
# Line 622  String LSCPServer::GetChannelInfo(uint u Line 1116  String LSCPServer::GetChannelInfo(uint u
1116                  if (AudioRouting != "") AudioRouting += ",";                  if (AudioRouting != "") AudioRouting += ",";
1117                  AudioRouting += ToString(pEngineChannel->OutputChannel(chan));                  AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
1118              }              }
1119                Mute = pEngineChannel->GetMute();
1120                Solo = pEngineChannel->GetSolo();
1121                if (pEngineChannel->UsesNoMidiInstrumentMap())
1122                    MidiInstrumentMap = "NONE";
1123                else if (pEngineChannel->UsesDefaultMidiInstrumentMap())
1124                    MidiInstrumentMap = "DEFAULT";
1125                else
1126                    MidiInstrumentMap = ToString(pEngineChannel->GetMidiInstrumentMap());
1127          }          }
1128    
1129          result.Add("ENGINE_NAME", EngineName);          result.Add("ENGINE_NAME", EngineName);
# Line 634  String LSCPServer::GetChannelInfo(uint u Line 1136  String LSCPServer::GetChannelInfo(uint u
1136    
1137          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));          result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
1138          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());          result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
1139          if (pSamplerChannel->GetMidiInputChannel() == MidiInputPort::midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
1140          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
1141    
1142            // convert the filename into the correct encoding as defined for LSCP
1143            // (especially in terms of special characters -> escape sequences)
1144            if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
1145    #if WIN32
1146                InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
1147    #else
1148                // assuming POSIX
1149                InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
1150    #endif
1151            }
1152    
1153          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
1154          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
1155          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
1156          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
1157            result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
1158            result.Add("SOLO", Solo);
1159            result.Add("MIDI_INSTRUMENT_MAP", MidiInstrumentMap);
1160      }      }
1161      catch (LinuxSamplerException e) {      catch (Exception e) {
1162           result.Error(e);           result.Error(e);
1163      }      }
1164      return result.Produce();      return result.Produce();
# Line 656  String LSCPServer::GetVoiceCount(uint ui Line 1172  String LSCPServer::GetVoiceCount(uint ui
1172      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
1173      LSCPResultSet result;      LSCPResultSet result;
1174      try {      try {
1175          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1176          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel");  
1177          result.Add(pEngineChannel->GetEngine()->VoiceCount());          result.Add(pEngineChannel->GetEngine()->VoiceCount());
1178      }      }
1179      catch (LinuxSamplerException e) {      catch (Exception e) {
1180           result.Error(e);           result.Error(e);
1181      }      }
1182      return result.Produce();      return result.Produce();
# Line 676  String LSCPServer::GetStreamCount(uint u Line 1190  String LSCPServer::GetStreamCount(uint u
1190      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
1191      LSCPResultSet result;      LSCPResultSet result;
1192      try {      try {
1193          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1194          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");  
1195          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
1196      }      }
1197      catch (LinuxSamplerException e) {      catch (Exception e) {
1198           result.Error(e);           result.Error(e);
1199      }      }
1200      return result.Produce();      return result.Produce();
# Line 696  String LSCPServer::GetBufferFill(fill_re Line 1208  String LSCPServer::GetBufferFill(fill_re
1208      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
1209      LSCPResultSet result;      LSCPResultSet result;
1210      try {      try {
1211          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1212          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");  
1213          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
1214          else {          else {
1215              switch (ResponseType) {              switch (ResponseType) {
# Line 710  String LSCPServer::GetBufferFill(fill_re Line 1220  String LSCPServer::GetBufferFill(fill_re
1220                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());                      result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
1221                      break;                      break;
1222                  default:                  default:
1223                      throw LinuxSamplerException("Unknown fill response type");                      throw Exception("Unknown fill response type");
1224              }              }
1225          }          }
1226      }      }
1227      catch (LinuxSamplerException e) {      catch (Exception e) {
1228           result.Error(e);           result.Error(e);
1229      }      }
1230      return result.Produce();      return result.Produce();
# Line 727  String LSCPServer::GetAvailableAudioOutp Line 1237  String LSCPServer::GetAvailableAudioOutp
1237          int n = AudioOutputDeviceFactory::AvailableDrivers().size();          int n = AudioOutputDeviceFactory::AvailableDrivers().size();
1238          result.Add(n);          result.Add(n);
1239      }      }
1240      catch (LinuxSamplerException e) {      catch (Exception e) {
1241          result.Error(e);          result.Error(e);
1242      }      }
1243      return result.Produce();      return result.Produce();
# Line 740  String LSCPServer::ListAvailableAudioOut Line 1250  String LSCPServer::ListAvailableAudioOut
1250          String s = AudioOutputDeviceFactory::AvailableDriversAsString();          String s = AudioOutputDeviceFactory::AvailableDriversAsString();
1251          result.Add(s);          result.Add(s);
1252      }      }
1253      catch (LinuxSamplerException e) {      catch (Exception e) {
1254          result.Error(e);          result.Error(e);
1255      }      }
1256      return result.Produce();      return result.Produce();
# Line 753  String LSCPServer::GetAvailableMidiInput Line 1263  String LSCPServer::GetAvailableMidiInput
1263          int n = MidiInputDeviceFactory::AvailableDrivers().size();          int n = MidiInputDeviceFactory::AvailableDrivers().size();
1264          result.Add(n);          result.Add(n);
1265      }      }
1266      catch (LinuxSamplerException e) {      catch (Exception e) {
1267          result.Error(e);          result.Error(e);
1268      }      }
1269      return result.Produce();      return result.Produce();
# Line 766  String LSCPServer::ListAvailableMidiInpu Line 1276  String LSCPServer::ListAvailableMidiInpu
1276          String s = MidiInputDeviceFactory::AvailableDriversAsString();          String s = MidiInputDeviceFactory::AvailableDriversAsString();
1277          result.Add(s);          result.Add(s);
1278      }      }
1279      catch (LinuxSamplerException e) {      catch (Exception e) {
1280          result.Error(e);          result.Error(e);
1281      }      }
1282      return result.Produce();      return result.Produce();
# Line 786  String LSCPServer::GetMidiInputDriverInf Line 1296  String LSCPServer::GetMidiInputDriverInf
1296              for (;iter != parameters.end(); iter++) {              for (;iter != parameters.end(); iter++) {
1297                  if (s != "") s += ",";                  if (s != "") s += ",";
1298                  s += iter->first;                  s += iter->first;
1299                    delete iter->second;
1300              }              }
1301              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1302          }          }
1303      }      }
1304      catch (LinuxSamplerException e) {      catch (Exception e) {
1305          result.Error(e);          result.Error(e);
1306      }      }
1307      return result.Produce();      return result.Produce();
# Line 810  String LSCPServer::GetAudioOutputDriverI Line 1321  String LSCPServer::GetAudioOutputDriverI
1321              for (;iter != parameters.end(); iter++) {              for (;iter != parameters.end(); iter++) {
1322                  if (s != "") s += ",";                  if (s != "") s += ",";
1323                  s += iter->first;                  s += iter->first;
1324                    delete iter->second;
1325              }              }
1326              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1327          }          }
1328      }      }
1329      catch (LinuxSamplerException e) {      catch (Exception e) {
1330          result.Error(e);          result.Error(e);
1331      }      }
1332      return result.Produce();      return result.Produce();
# Line 840  String LSCPServer::GetMidiInputDriverPar Line 1352  String LSCPServer::GetMidiInputDriverPar
1352          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1353          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1354          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1355            delete pParameter;
1356      }      }
1357      catch (LinuxSamplerException e) {      catch (Exception e) {
1358          result.Error(e);          result.Error(e);
1359      }      }
1360      return result.Produce();      return result.Produce();
# Line 867  String LSCPServer::GetAudioOutputDriverP Line 1380  String LSCPServer::GetAudioOutputDriverP
1380          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1381          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1382          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1383            delete pParameter;
1384      }      }
1385      catch (LinuxSamplerException e) {      catch (Exception e) {
1386          result.Error(e);          result.Error(e);
1387      }      }
1388      return result.Produce();      return result.Produce();
# Line 881  String LSCPServer::GetAudioOutputDeviceC Line 1395  String LSCPServer::GetAudioOutputDeviceC
1395          uint count = pSampler->AudioOutputDevices();          uint count = pSampler->AudioOutputDevices();
1396          result.Add(count); // success          result.Add(count); // success
1397      }      }
1398      catch (LinuxSamplerException e) {      catch (Exception e) {
1399          result.Error(e);          result.Error(e);
1400      }      }
1401      return result.Produce();      return result.Produce();
# Line 894  String LSCPServer::GetMidiInputDeviceCou Line 1408  String LSCPServer::GetMidiInputDeviceCou
1408          uint count = pSampler->MidiInputDevices();          uint count = pSampler->MidiInputDevices();
1409          result.Add(count); // success          result.Add(count); // success
1410      }      }
1411      catch (LinuxSamplerException e) {      catch (Exception e) {
1412          result.Error(e);          result.Error(e);
1413      }      }
1414      return result.Produce();      return result.Produce();
# Line 913  String LSCPServer::GetAudioOutputDevices Line 1427  String LSCPServer::GetAudioOutputDevices
1427          }          }
1428          result.Add(s);          result.Add(s);
1429      }      }
1430      catch (LinuxSamplerException e) {      catch (Exception e) {
1431          result.Error(e);          result.Error(e);
1432      }      }
1433      return result.Produce();      return result.Produce();
# Line 932  String LSCPServer::GetMidiInputDevices() Line 1446  String LSCPServer::GetMidiInputDevices()
1446          }          }
1447          result.Add(s);          result.Add(s);
1448      }      }
1449      catch (LinuxSamplerException e) {      catch (Exception e) {
1450          result.Error(e);          result.Error(e);
1451      }      }
1452      return result.Produce();      return result.Produce();
# Line 943  String LSCPServer::GetAudioOutputDeviceI Line 1457  String LSCPServer::GetAudioOutputDeviceI
1457      LSCPResultSet result;      LSCPResultSet result;
1458      try {      try {
1459          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1460          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) + ".");
1461          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1462          result.Add("DRIVER", pDevice->Driver());          result.Add("DRIVER", pDevice->Driver());
1463          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
# Line 952  String LSCPServer::GetAudioOutputDeviceI Line 1466  String LSCPServer::GetAudioOutputDeviceI
1466              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1467          }          }
1468      }      }
1469      catch (LinuxSamplerException e) {      catch (Exception e) {
1470          result.Error(e);          result.Error(e);
1471      }      }
1472      return result.Produce();      return result.Produce();
# Line 963  String LSCPServer::GetMidiInputDeviceInf Line 1477  String LSCPServer::GetMidiInputDeviceInf
1477      LSCPResultSet result;      LSCPResultSet result;
1478      try {      try {
1479          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1480          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) + ".");
1481          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1482          result.Add("DRIVER", pDevice->Driver());          result.Add("DRIVER", pDevice->Driver());
1483          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
# Line 972  String LSCPServer::GetMidiInputDeviceInf Line 1486  String LSCPServer::GetMidiInputDeviceInf
1486              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1487          }          }
1488      }      }
1489      catch (LinuxSamplerException e) {      catch (Exception e) {
1490          result.Error(e);          result.Error(e);
1491      }      }
1492      return result.Produce();      return result.Produce();
# Line 983  String LSCPServer::GetMidiInputPortInfo( Line 1497  String LSCPServer::GetMidiInputPortInfo(
1497      try {      try {
1498          // get MIDI input device          // get MIDI input device
1499          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1500          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) + ".");
1501          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1502    
1503          // get MIDI port          // get MIDI port
1504          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1505          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) + ".");
1506    
1507          // return the values of all MIDI port parameters          // return the values of all MIDI port parameters
1508          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
# Line 997  String LSCPServer::GetMidiInputPortInfo( Line 1511  String LSCPServer::GetMidiInputPortInfo(
1511              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1512          }          }
1513      }      }
1514      catch (LinuxSamplerException e) {      catch (Exception e) {
1515          result.Error(e);          result.Error(e);
1516      }      }
1517      return result.Produce();      return result.Produce();
# Line 1009  String LSCPServer::GetAudioOutputChannel Line 1523  String LSCPServer::GetAudioOutputChannel
1523      try {      try {
1524          // get audio output device          // get audio output device
1525          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1526          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) + ".");
1527          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1528    
1529          // get audio channel          // get audio channel
1530          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1531          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) + ".");
1532    
1533          // return the values of all audio channel parameters          // return the values of all audio channel parameters
1534          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
# Line 1023  String LSCPServer::GetAudioOutputChannel Line 1537  String LSCPServer::GetAudioOutputChannel
1537              result.Add(iter->first, iter->second->Value());              result.Add(iter->first, iter->second->Value());
1538          }          }
1539      }      }
1540      catch (LinuxSamplerException e) {      catch (Exception e) {
1541          result.Error(e);          result.Error(e);
1542      }      }
1543      return result.Produce();      return result.Produce();
# Line 1035  String LSCPServer::GetMidiInputPortParam Line 1549  String LSCPServer::GetMidiInputPortParam
1549      try {      try {
1550          // get MIDI input device          // get MIDI input device
1551          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1552          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) + ".");
1553          MidiInputDevice* pDevice = devices[DeviceId];          MidiInputDevice* pDevice = devices[DeviceId];
1554    
1555          // get midi port          // get midi port
1556          MidiInputPort* pPort = pDevice->GetPort(PortId);          MidiInputPort* pPort = pDevice->GetPort(PortId);
1557          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) + ".");
1558    
1559          // get desired port parameter          // get desired port parameter
1560          std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1561          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 + "'.");
1562          DeviceRuntimeParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1563    
1564          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
# Line 1056  String LSCPServer::GetMidiInputPortParam Line 1570  String LSCPServer::GetMidiInputPortParam
1570          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1571          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1572      }      }
1573      catch (LinuxSamplerException e) {      catch (Exception e) {
1574          result.Error(e);          result.Error(e);
1575      }      }
1576      return result.Produce();      return result.Produce();
# Line 1068  String LSCPServer::GetAudioOutputChannel Line 1582  String LSCPServer::GetAudioOutputChannel
1582      try {      try {
1583          // get audio output device          // get audio output device
1584          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1585          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) + ".");
1586          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1587    
1588          // get audio channel          // get audio channel
1589          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1590          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) + ".");
1591    
1592          // get desired audio channel parameter          // get desired audio channel parameter
1593          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1594          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 + "'.");
1595          DeviceRuntimeParameter* pParameter = parameters[ParameterName];          DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1596    
1597          // return all fields of this audio channel parameter          // return all fields of this audio channel parameter
# Line 1089  String LSCPServer::GetAudioOutputChannel Line 1603  String LSCPServer::GetAudioOutputChannel
1603          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());          if (pParameter->RangeMax())      result.Add("RANGE_MAX",     *pParameter->RangeMax());
1604          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());          if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1605      }      }
1606      catch (LinuxSamplerException e) {      catch (Exception e) {
1607          result.Error(e);          result.Error(e);
1608      }      }
1609      return result.Produce();      return result.Produce();
# Line 1101  String LSCPServer::SetAudioOutputChannel Line 1615  String LSCPServer::SetAudioOutputChannel
1615      try {      try {
1616          // get audio output device          // get audio output device
1617          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1618          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) + ".");
1619          AudioOutputDevice* pDevice = devices[DeviceId];          AudioOutputDevice* pDevice = devices[DeviceId];
1620    
1621          // get audio channel          // get audio channel
1622          AudioChannel* pChannel = pDevice->Channel(ChannelId);          AudioChannel* pChannel = pDevice->Channel(ChannelId);
1623          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) + ".");
1624    
1625          // get desired audio channel parameter          // get desired audio channel parameter
1626          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1627          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 + "'.");
1628          DeviceRuntimeParameter* pParameter = parameters[ParamKey];          DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1629    
1630          // set new channel parameter value          // set new channel parameter value
1631          pParameter->SetValue(ParamVal);          pParameter->SetValue(ParamVal);
1632            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceId));
1633      }      }
1634      catch (LinuxSamplerException e) {      catch (Exception e) {
1635          result.Error(e);          result.Error(e);
1636      }      }
1637      return result.Produce();      return result.Produce();
# Line 1127  String LSCPServer::SetAudioOutputDeviceP Line 1642  String LSCPServer::SetAudioOutputDeviceP
1642      LSCPResultSet result;      LSCPResultSet result;
1643      try {      try {
1644          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1645          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) + ".");
1646          AudioOutputDevice* pDevice = devices[DeviceIndex];          AudioOutputDevice* pDevice = devices[DeviceIndex];
1647          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1648          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 + "'");
1649          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1650            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_info, DeviceIndex));
1651      }      }
1652      catch (LinuxSamplerException e) {      catch (Exception e) {
1653          result.Error(e);          result.Error(e);
1654      }      }
1655      return result.Produce();      return result.Produce();
# Line 1144  String LSCPServer::SetMidiInputDevicePar Line 1660  String LSCPServer::SetMidiInputDevicePar
1660      LSCPResultSet result;      LSCPResultSet result;
1661      try {      try {
1662          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1663          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) + ".");
1664          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1665          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();          std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1666          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 + "'");
1667          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1668            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1669      }      }
1670      catch (LinuxSamplerException e) {      catch (Exception e) {
1671          result.Error(e);          result.Error(e);
1672      }      }
1673      return result.Produce();      return result.Produce();
# Line 1162  String LSCPServer::SetMidiInputPortParam Line 1679  String LSCPServer::SetMidiInputPortParam
1679      try {      try {
1680          // get MIDI input device          // get MIDI input device
1681          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1682          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) + ".");
1683          MidiInputDevice* pDevice = devices[DeviceIndex];          MidiInputDevice* pDevice = devices[DeviceIndex];
1684    
1685          // get MIDI port          // get MIDI port
1686          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);          MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1687          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) + ".");
1688    
1689          // set port parameter value          // set port parameter value
1690          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();          std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1691          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 + "'");
1692          parameters[ParamKey]->SetValue(ParamVal);          parameters[ParamKey]->SetValue(ParamVal);
1693            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_info, DeviceIndex));
1694      }      }
1695      catch (LinuxSamplerException e) {      catch (Exception e) {
1696          result.Error(e);          result.Error(e);
1697      }      }
1698      return result.Produce();      return result.Produce();
# Line 1189  String LSCPServer::SetAudioOutputChannel Line 1707  String LSCPServer::SetAudioOutputChannel
1707      LSCPResultSet result;      LSCPResultSet result;
1708      try {      try {
1709          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1710          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1711          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();          EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1712          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));
1713          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));
1714          pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);          pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1715      }      }
1716      catch (LinuxSamplerException e) {      catch (Exception e) {
1717           result.Error(e);           result.Error(e);
1718      }      }
1719      return result.Produce();      return result.Produce();
# Line 1204  String LSCPServer::SetAudioOutputChannel Line 1722  String LSCPServer::SetAudioOutputChannel
1722  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1723      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1724      LSCPResultSet result;      LSCPResultSet result;
1725        LockRTNotify();
1726      try {      try {
1727          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1728          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1729          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1730          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));
1731          AudioOutputDevice* pDevice = devices[AudioDeviceId];          AudioOutputDevice* pDevice = devices[AudioDeviceId];
1732          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1733      }      }
1734      catch (LinuxSamplerException e) {      catch (Exception e) {
1735           result.Error(e);           result.Error(e);
1736      }      }
1737        UnlockRTNotify();
1738      return result.Produce();      return result.Produce();
1739  }  }
1740    
1741  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1742      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));
1743      LSCPResultSet result;      LSCPResultSet result;
1744        LockRTNotify();
1745      try {      try {
1746          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1747          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1748          // Driver type name aliasing...          // Driver type name aliasing...
1749          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1750          if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";          if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
# Line 1245  String LSCPServer::SetAudioOutputType(St Line 1766  String LSCPServer::SetAudioOutputType(St
1766          }          }
1767          // Must have a device...          // Must have a device...
1768          if (pDevice == NULL)          if (pDevice == NULL)
1769              throw LinuxSamplerException("Internal error: could not create audio output device.");              throw Exception("Internal error: could not create audio output device.");
1770          // Set it as the current channel device...          // Set it as the current channel device...
1771          pSamplerChannel->SetAudioOutputDevice(pDevice);          pSamplerChannel->SetAudioOutputDevice(pDevice);
1772      }      }
1773      catch (LinuxSamplerException e) {      catch (Exception e) {
1774           result.Error(e);           result.Error(e);
1775      }      }
1776        UnlockRTNotify();
1777      return result.Produce();      return result.Produce();
1778  }  }
1779    
# Line 1260  String LSCPServer::SetMIDIInputPort(uint Line 1782  String LSCPServer::SetMIDIInputPort(uint
1782      LSCPResultSet result;      LSCPResultSet result;
1783      try {      try {
1784          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1785          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1786          pSamplerChannel->SetMidiInputPort(MIDIPort);          pSamplerChannel->SetMidiInputPort(MIDIPort);
1787      }      }
1788      catch (LinuxSamplerException e) {      catch (Exception e) {
1789           result.Error(e);           result.Error(e);
1790      }      }
1791      return result.Produce();      return result.Produce();
# Line 1274  String LSCPServer::SetMIDIInputChannel(u Line 1796  String LSCPServer::SetMIDIInputChannel(u
1796      LSCPResultSet result;      LSCPResultSet result;
1797      try {      try {
1798          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1799          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1800          pSamplerChannel->SetMidiInputChannel((MidiInputPort::midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInputChannel((midi_chan_t) MIDIChannel);
1801      }      }
1802      catch (LinuxSamplerException e) {      catch (Exception e) {
1803           result.Error(e);           result.Error(e);
1804      }      }
1805      return result.Produce();      return result.Produce();
# Line 1288  String LSCPServer::SetMIDIInputDevice(ui Line 1810  String LSCPServer::SetMIDIInputDevice(ui
1810      LSCPResultSet result;      LSCPResultSet result;
1811      try {      try {
1812          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1813          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1814          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1815          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));
1816          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1817          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1818      }      }
1819      catch (LinuxSamplerException e) {      catch (Exception e) {
1820           result.Error(e);           result.Error(e);
1821      }      }
1822      return result.Produce();      return result.Produce();
# Line 1305  String LSCPServer::SetMIDIInputType(Stri Line 1827  String LSCPServer::SetMIDIInputType(Stri
1827      LSCPResultSet result;      LSCPResultSet result;
1828      try {      try {
1829          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1830          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1831          // Driver type name aliasing...          // Driver type name aliasing...
1832          if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";          if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1833          // Check if there's one MIDI input device already created          // Check if there's one MIDI input device already created
# Line 1325  String LSCPServer::SetMIDIInputType(Stri Line 1847  String LSCPServer::SetMIDIInputType(Stri
1847              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1848              // Make it with at least one initial port.              // Make it with at least one initial port.
1849              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
             parameters["PORTS"]->SetValue("1");  
1850          }          }
1851          // Must have a device...          // Must have a device...
1852          if (pDevice == NULL)          if (pDevice == NULL)
1853              throw LinuxSamplerException("Internal error: could not create MIDI input device.");              throw Exception("Internal error: could not create MIDI input device.");
1854          // Set it as the current channel device...          // Set it as the current channel device...
1855          pSamplerChannel->SetMidiInputDevice(pDevice);          pSamplerChannel->SetMidiInputDevice(pDevice);
1856      }      }
1857      catch (LinuxSamplerException e) {      catch (Exception e) {
1858           result.Error(e);           result.Error(e);
1859      }      }
1860      return result.Produce();      return result.Produce();
# Line 1348  String LSCPServer::SetMIDIInput(uint MID Line 1869  String LSCPServer::SetMIDIInput(uint MID
1869      LSCPResultSet result;      LSCPResultSet result;
1870      try {      try {
1871          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1872          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1873          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devices =  pSampler->GetMidiInputDevices();
1874          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));
1875          MidiInputDevice* pDevice = devices[MIDIDeviceId];          MidiInputDevice* pDevice = devices[MIDIDeviceId];
1876          pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputPort::midi_chan_t) MIDIChannel);          pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (midi_chan_t) MIDIChannel);
1877      }      }
1878      catch (LinuxSamplerException e) {      catch (Exception e) {
1879           result.Error(e);           result.Error(e);
1880      }      }
1881      return result.Produce();      return result.Produce();
# Line 1368  String LSCPServer::SetVolume(double dVol Line 1889  String LSCPServer::SetVolume(double dVol
1889      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1890      LSCPResultSet result;      LSCPResultSet result;
1891      try {      try {
1892          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");  
1893          pEngineChannel->Volume(dVolume);          pEngineChannel->Volume(dVolume);
1894      }      }
1895      catch (LinuxSamplerException e) {      catch (Exception e) {
1896           result.Error(e);           result.Error(e);
1897      }      }
1898      return result.Produce();      return result.Produce();
1899  }  }
1900    
1901  /**  /**
1902     * Will be called by the parser to mute/unmute particular sampler channel.
1903     */
1904    String LSCPServer::SetChannelMute(bool bMute, uint uiSamplerChannel) {
1905        dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1906        LSCPResultSet result;
1907        try {
1908            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1909    
1910            if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1911            else pEngineChannel->SetMute(1);
1912        } catch (Exception e) {
1913            result.Error(e);
1914        }
1915        return result.Produce();
1916    }
1917    
1918    /**
1919     * Will be called by the parser to solo particular sampler channel.
1920     */
1921    String LSCPServer::SetChannelSolo(bool bSolo, uint uiSamplerChannel) {
1922        dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1923        LSCPResultSet result;
1924        try {
1925            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
1926    
1927            bool oldSolo = pEngineChannel->GetSolo();
1928            bool hadSoloChannel = HasSoloChannel();
1929    
1930            pEngineChannel->SetSolo(bSolo);
1931    
1932            if(!oldSolo && bSolo) {
1933                if(pEngineChannel->GetMute() == -1) pEngineChannel->SetMute(0);
1934                if(!hadSoloChannel) MuteNonSoloChannels();
1935            }
1936    
1937            if(oldSolo && !bSolo) {
1938                if(!HasSoloChannel()) UnmuteChannels();
1939                else if(!pEngineChannel->GetMute()) pEngineChannel->SetMute(-1);
1940            }
1941        } catch (Exception e) {
1942            result.Error(e);
1943        }
1944        return result.Produce();
1945    }
1946    
1947    /**
1948     * Determines whether there is at least one solo channel in the channel list.
1949     *
1950     * @returns true if there is at least one solo channel in the channel list,
1951     * false otherwise.
1952     */
1953    bool LSCPServer::HasSoloChannel() {
1954        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1955        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1956        for (; iter != channels.end(); iter++) {
1957            EngineChannel* c = iter->second->GetEngineChannel();
1958            if(c && c->GetSolo()) return true;
1959        }
1960    
1961        return false;
1962    }
1963    
1964    /**
1965     * Mutes all unmuted non-solo channels. Notice that the channels are muted
1966     * with -1 which indicates that they are muted because of the presence
1967     * of a solo channel(s). Channels muted with -1 will be automatically unmuted
1968     * when there are no solo channels left.
1969     */
1970    void LSCPServer::MuteNonSoloChannels() {
1971        dmsg(2,("LSCPServer: MuteNonSoloChannels()\n"));
1972        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1973        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1974        for (; iter != channels.end(); iter++) {
1975            EngineChannel* c = iter->second->GetEngineChannel();
1976            if(c && !c->GetSolo() && !c->GetMute()) c->SetMute(-1);
1977        }
1978    }
1979    
1980    /**
1981     * Unmutes all channels that are muted because of the presence
1982     * of a solo channel(s).
1983     */
1984    void  LSCPServer::UnmuteChannels() {
1985        dmsg(2,("LSCPServer: UnmuteChannels()\n"));
1986        std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
1987        std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
1988        for (; iter != channels.end(); iter++) {
1989            EngineChannel* c = iter->second->GetEngineChannel();
1990            if(c && c->GetMute() == -1) c->SetMute(0);
1991        }
1992    }
1993    
1994    String LSCPServer::AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name, bool bModal) {
1995        dmsg(2,("LSCPServer: AddOrReplaceMIDIInstrumentMapping()\n"));
1996    
1997        midi_prog_index_t idx;
1998        idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
1999        idx.midi_bank_lsb = MidiBank & 0x7f;
2000        idx.midi_prog     = MidiProg;
2001    
2002        MidiInstrumentMapper::entry_t entry;
2003        entry.EngineName      = EngineType;
2004        entry.InstrumentFile  = InstrumentFile;
2005        entry.InstrumentIndex = InstrumentIndex;
2006        entry.LoadMode        = LoadMode;
2007        entry.Volume          = Volume;
2008        entry.Name            = Name;
2009    
2010        LSCPResultSet result;
2011        try {
2012            // PERSISTENT mapping commands might block for a long time, so in
2013            // that case we add/replace the mapping in another thread in case
2014            // the NON_MODAL argument was supplied, non persistent mappings
2015            // should return immediately, so we don't need to do that for them
2016            bool bInBackground = (entry.LoadMode == MidiInstrumentMapper::PERSISTENT && !bModal);
2017            MidiInstrumentMapper::AddOrReplaceEntry(MidiMapID, idx, entry, bInBackground);
2018        } catch (Exception e) {
2019            result.Error(e);
2020        }
2021        return result.Produce();
2022    }
2023    
2024    String LSCPServer::RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2025        dmsg(2,("LSCPServer: RemoveMIDIInstrumentMapping()\n"));
2026    
2027        midi_prog_index_t idx;
2028        idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;
2029        idx.midi_bank_lsb = MidiBank & 0x7f;
2030        idx.midi_prog     = MidiProg;
2031    
2032        LSCPResultSet result;
2033        try {
2034            MidiInstrumentMapper::RemoveEntry(MidiMapID, idx);
2035        } catch (Exception e) {
2036            result.Error(e);
2037        }
2038        return result.Produce();
2039    }
2040    
2041    String LSCPServer::GetMidiInstrumentMappings(uint MidiMapID) {
2042        dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2043        LSCPResultSet result;
2044        try {
2045            result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID));
2046        } catch (Exception e) {
2047            result.Error(e);
2048        }
2049        return result.Produce();
2050    }
2051    
2052    
2053    String LSCPServer::GetAllMidiInstrumentMappings() {
2054        dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2055        LSCPResultSet result;
2056        try {
2057            result.Add(MidiInstrumentMapper::GetInstrumentCount());
2058        } catch (Exception e) {
2059            result.Error(e);
2060        }
2061        return result.Produce();
2062    }
2063    
2064    String LSCPServer::GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg) {
2065        dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2066        LSCPResultSet result;
2067        try {
2068            MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg);
2069            // convert the filename into the correct encoding as defined for LSCP
2070            // (especially in terms of special characters -> escape sequences)
2071    #if WIN32
2072            const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp();
2073    #else
2074            // assuming POSIX
2075            const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp();
2076    #endif
2077    
2078            result.Add("NAME", _escapeLscpResponse(entry.Name));
2079            result.Add("ENGINE_NAME", entry.EngineName);
2080            result.Add("INSTRUMENT_FILE", instrumentFileName);
2081            result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex);
2082            String instrumentName;
2083            Engine* pEngine = EngineFactory::Create(entry.EngineName);
2084            if (pEngine) {
2085                if (pEngine->GetInstrumentManager()) {
2086                    InstrumentManager::instrument_id_t instrID;
2087                    instrID.FileName = entry.InstrumentFile;
2088                    instrID.Index    = entry.InstrumentIndex;
2089                    instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
2090                }
2091                EngineFactory::Destroy(pEngine);
2092            }
2093            result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2094            switch (entry.LoadMode) {
2095                case MidiInstrumentMapper::ON_DEMAND:
2096                    result.Add("LOAD_MODE", "ON_DEMAND");
2097                    break;
2098                case MidiInstrumentMapper::ON_DEMAND_HOLD:
2099                    result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2100                    break;
2101                case MidiInstrumentMapper::PERSISTENT:
2102                    result.Add("LOAD_MODE", "PERSISTENT");
2103                    break;
2104                default:
2105                    throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2106            }
2107            result.Add("VOLUME", entry.Volume);
2108        } catch (Exception e) {
2109            result.Error(e);
2110        }
2111        return result.Produce();
2112    }
2113    
2114    String LSCPServer::ListMidiInstrumentMappings(uint MidiMapID) {
2115        dmsg(2,("LSCPServer: ListMidiInstrumentMappings()\n"));
2116        LSCPResultSet result;
2117        try {
2118            String s;
2119            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);
2120            std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2121            for (; iter != mappings.end(); iter++) {
2122                if (s.size()) s += ",";
2123                s += "{" + ToString(MidiMapID) + ","
2124                         + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2125                         + ToString(int(iter->first.midi_prog)) + "}";
2126            }
2127            result.Add(s);
2128        } catch (Exception e) {
2129            result.Error(e);
2130        }
2131        return result.Produce();
2132    }
2133    
2134    String LSCPServer::ListAllMidiInstrumentMappings() {
2135        dmsg(2,("LSCPServer: ListAllMidiInstrumentMappings()\n"));
2136        LSCPResultSet result;
2137        try {
2138            std::vector<int> maps = MidiInstrumentMapper::Maps();
2139            String s;
2140            for (int i = 0; i < maps.size(); i++) {
2141                std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(maps[i]);
2142                std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin();
2143                for (; iter != mappings.end(); iter++) {
2144                    if (s.size()) s += ",";
2145                    s += "{" + ToString(maps[i]) + ","
2146                             + ToString((int(iter->first.midi_bank_msb) << 7) | int(iter->first.midi_bank_lsb)) + ","
2147                             + ToString(int(iter->first.midi_prog)) + "}";
2148                }
2149            }
2150            result.Add(s);
2151        } catch (Exception e) {
2152            result.Error(e);
2153        }
2154        return result.Produce();
2155    }
2156    
2157    String LSCPServer::ClearMidiInstrumentMappings(uint MidiMapID) {
2158        dmsg(2,("LSCPServer: ClearMidiInstrumentMappings()\n"));
2159        LSCPResultSet result;
2160        try {
2161            MidiInstrumentMapper::RemoveAllEntries(MidiMapID);
2162        } catch (Exception e) {
2163            result.Error(e);
2164        }
2165        return result.Produce();
2166    }
2167    
2168    String LSCPServer::ClearAllMidiInstrumentMappings() {
2169        dmsg(2,("LSCPServer: ClearAllMidiInstrumentMappings()\n"));
2170        LSCPResultSet result;
2171        try {
2172            std::vector<int> maps = MidiInstrumentMapper::Maps();
2173            for (int i = 0; i < maps.size(); i++)
2174                MidiInstrumentMapper::RemoveAllEntries(maps[i]);
2175        } catch (Exception e) {
2176            result.Error(e);
2177        }
2178        return result.Produce();
2179    }
2180    
2181    String LSCPServer::AddMidiInstrumentMap(String MapName) {
2182        dmsg(2,("LSCPServer: AddMidiInstrumentMap()\n"));
2183        LSCPResultSet result;
2184        try {
2185            int MapID = MidiInstrumentMapper::AddMap(MapName);
2186            result = LSCPResultSet(MapID);
2187        } catch (Exception e) {
2188            result.Error(e);
2189        }
2190        return result.Produce();
2191    }
2192    
2193    String LSCPServer::RemoveMidiInstrumentMap(uint MidiMapID) {
2194        dmsg(2,("LSCPServer: RemoveMidiInstrumentMap()\n"));
2195        LSCPResultSet result;
2196        try {
2197            MidiInstrumentMapper::RemoveMap(MidiMapID);
2198        } catch (Exception e) {
2199            result.Error(e);
2200        }
2201        return result.Produce();
2202    }
2203    
2204    String LSCPServer::RemoveAllMidiInstrumentMaps() {
2205        dmsg(2,("LSCPServer: RemoveAllMidiInstrumentMaps()\n"));
2206        LSCPResultSet result;
2207        try {
2208            MidiInstrumentMapper::RemoveAllMaps();
2209        } catch (Exception e) {
2210            result.Error(e);
2211        }
2212        return result.Produce();
2213    }
2214    
2215    String LSCPServer::GetMidiInstrumentMaps() {
2216        dmsg(2,("LSCPServer: GetMidiInstrumentMaps()\n"));
2217        LSCPResultSet result;
2218        try {
2219            result.Add(MidiInstrumentMapper::Maps().size());
2220        } catch (Exception e) {
2221            result.Error(e);
2222        }
2223        return result.Produce();
2224    }
2225    
2226    String LSCPServer::ListMidiInstrumentMaps() {
2227        dmsg(2,("LSCPServer: ListMidiInstrumentMaps()\n"));
2228        LSCPResultSet result;
2229        try {
2230            std::vector<int> maps = MidiInstrumentMapper::Maps();
2231            String sList;
2232            for (int i = 0; i < maps.size(); i++) {
2233                if (sList != "") sList += ",";
2234                sList += ToString(maps[i]);
2235            }
2236            result.Add(sList);
2237        } catch (Exception e) {
2238            result.Error(e);
2239        }
2240        return result.Produce();
2241    }
2242    
2243    String LSCPServer::GetMidiInstrumentMap(uint MidiMapID) {
2244        dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2245        LSCPResultSet result;
2246        try {
2247            result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2248            result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2249        } catch (Exception e) {
2250            result.Error(e);
2251        }
2252        return result.Produce();
2253    }
2254    
2255    String LSCPServer::SetMidiInstrumentMapName(uint MidiMapID, String NewName) {
2256        dmsg(2,("LSCPServer: SetMidiInstrumentMapName()\n"));
2257        LSCPResultSet result;
2258        try {
2259            MidiInstrumentMapper::RenameMap(MidiMapID, NewName);
2260        } catch (Exception e) {
2261            result.Error(e);
2262        }
2263        return result.Produce();
2264    }
2265    
2266    /**
2267     * Set the MIDI instrument map the given sampler channel shall use for
2268     * handling MIDI program change messages. There are the following two
2269     * special (negative) values:
2270     *
2271     *    - (-1) :  set to NONE (ignore program changes)
2272     *    - (-2) :  set to DEFAULT map
2273     */
2274    String LSCPServer::SetChannelMap(uint uiSamplerChannel, int MidiMapID) {
2275        dmsg(2,("LSCPServer: SetChannelMap()\n"));
2276        LSCPResultSet result;
2277        try {
2278            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2279    
2280            if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2281            else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
2282            else                      pEngineChannel->SetMidiInstrumentMap(MidiMapID);
2283        } catch (Exception e) {
2284            result.Error(e);
2285        }
2286        return result.Produce();
2287    }
2288    
2289    String LSCPServer::CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name) {
2290        dmsg(2,("LSCPServer: CreateFxSend()\n"));
2291        LSCPResultSet result;
2292        try {
2293            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2294    
2295            FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2296            if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2297    
2298            result = LSCPResultSet(pFxSend->Id()); // success
2299        } catch (Exception e) {
2300            result.Error(e);
2301        }
2302        return result.Produce();
2303    }
2304    
2305    String LSCPServer::DestroyFxSend(uint uiSamplerChannel, uint FxSendID) {
2306        dmsg(2,("LSCPServer: DestroyFxSend()\n"));
2307        LSCPResultSet result;
2308        try {
2309            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2310    
2311            FxSend* pFxSend = NULL;
2312            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2313                if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2314                    pFxSend = pEngineChannel->GetFxSend(i);
2315                    break;
2316                }
2317            }
2318            if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2319            pEngineChannel->RemoveFxSend(pFxSend);
2320        } catch (Exception e) {
2321            result.Error(e);
2322        }
2323        return result.Produce();
2324    }
2325    
2326    String LSCPServer::GetFxSends(uint uiSamplerChannel) {
2327        dmsg(2,("LSCPServer: GetFxSends()\n"));
2328        LSCPResultSet result;
2329        try {
2330            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2331    
2332            result.Add(pEngineChannel->GetFxSendCount());
2333        } catch (Exception e) {
2334            result.Error(e);
2335        }
2336        return result.Produce();
2337    }
2338    
2339    String LSCPServer::ListFxSends(uint uiSamplerChannel) {
2340        dmsg(2,("LSCPServer: ListFxSends()\n"));
2341        LSCPResultSet result;
2342        String list;
2343        try {
2344            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2345    
2346            for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2347                FxSend* pFxSend = pEngineChannel->GetFxSend(i);
2348                if (list != "") list += ",";
2349                list += ToString(pFxSend->Id());
2350            }
2351            result.Add(list);
2352        } catch (Exception e) {
2353            result.Error(e);
2354        }
2355        return result.Produce();
2356    }
2357    
2358    FxSend* LSCPServer::GetFxSend(uint uiSamplerChannel, uint FxSendID) {
2359        EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2360    
2361        FxSend* pFxSend = NULL;
2362        for (int i = 0; i < pEngineChannel->GetFxSendCount(); i++) {
2363            if (pEngineChannel->GetFxSend(i)->Id() == FxSendID) {
2364                pFxSend = pEngineChannel->GetFxSend(i);
2365                break;
2366            }
2367        }
2368        if (!pFxSend) throw Exception("There is no FxSend with that ID on the given sampler channel");
2369        return pFxSend;
2370    }
2371    
2372    String LSCPServer::GetFxSendInfo(uint uiSamplerChannel, uint FxSendID) {
2373        dmsg(2,("LSCPServer: GetFxSendInfo()\n"));
2374        LSCPResultSet result;
2375        try {
2376            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2377            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2378    
2379            // gather audio routing informations
2380            String AudioRouting;
2381            for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
2382                if (AudioRouting != "") AudioRouting += ",";
2383                AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2384            }
2385    
2386            // success
2387            result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2388            result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2389            result.Add("LEVEL", ToString(pFxSend->Level()));
2390            result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2391        } catch (Exception e) {
2392            result.Error(e);
2393        }
2394        return result.Produce();
2395    }
2396    
2397    String LSCPServer::SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name) {
2398        dmsg(2,("LSCPServer: SetFxSendName()\n"));
2399        LSCPResultSet result;
2400        try {
2401            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2402    
2403            pFxSend->SetName(Name);
2404            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2405        } catch (Exception e) {
2406            result.Error(e);
2407        }
2408        return result.Produce();
2409    }
2410    
2411    String LSCPServer::SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel) {
2412        dmsg(2,("LSCPServer: SetFxSendAudioOutputChannel()\n"));
2413        LSCPResultSet result;
2414        try {
2415            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2416    
2417            pFxSend->SetDestinationChannel(FxSendChannel, DeviceChannel);
2418            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2419        } catch (Exception e) {
2420            result.Error(e);
2421        }
2422        return result.Produce();
2423    }
2424    
2425    String LSCPServer::SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController) {
2426        dmsg(2,("LSCPServer: SetFxSendMidiController()\n"));
2427        LSCPResultSet result;
2428        try {
2429            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2430    
2431            pFxSend->SetMidiController(MidiController);
2432            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2433        } catch (Exception e) {
2434            result.Error(e);
2435        }
2436        return result.Produce();
2437    }
2438    
2439    String LSCPServer::SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel) {
2440        dmsg(2,("LSCPServer: SetFxSendLevel()\n"));
2441        LSCPResultSet result;
2442        try {
2443            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2444    
2445            pFxSend->SetLevel((float)dLevel);
2446            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2447        } catch (Exception e) {
2448            result.Error(e);
2449        }
2450        return result.Produce();
2451    }
2452    
2453    String LSCPServer::GetAvailableEffects() {
2454        dmsg(2,("LSCPServer: GetAvailableEffects()\n"));
2455        LSCPResultSet result;
2456        try {
2457            int n = EffectFactory::AvailableEffectsCount();
2458            result.Add(n);
2459        }
2460        catch (Exception e) {
2461            result.Error(e);
2462        }
2463        return result.Produce();
2464    }
2465    
2466    String LSCPServer::ListAvailableEffects() {
2467        dmsg(2,("LSCPServer: ListAvailableEffects()\n"));
2468        LSCPResultSet result;
2469        String list;
2470        try {
2471            //FIXME: for now we simply enumerate from 0 .. EffectFactory::AvailableEffectsCount() here, in future we should use unique IDs for effects during the whole sampler session. This issue comes into game when the user forces a reload of available effect plugins
2472            int n = EffectFactory::AvailableEffectsCount();
2473            for (int i = 0; i < n; i++) {
2474                if (i) list += ",";
2475                list += ToString(i);
2476            }
2477        }
2478        catch (Exception e) {
2479            result.Error(e);
2480        }
2481        result.Add(list);
2482        return result.Produce();
2483    }
2484    
2485    String LSCPServer::GetEffectInfo(int iEffectIndex) {
2486        dmsg(2,("LSCPServer: GetEffectInfo(%d)\n", iEffectIndex));
2487        LSCPResultSet result;
2488        try {
2489            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex);
2490            if (!pEffectInfo)
2491                throw Exception("There is no effect with index " + ToString(iEffectIndex));
2492    
2493            // convert the filename into the correct encoding as defined for LSCP
2494            // (especially in terms of special characters -> escape sequences)
2495    #if WIN32
2496            const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2497    #else
2498            // assuming POSIX
2499            const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2500    #endif
2501    
2502            result.Add("SYSTEM", pEffectInfo->EffectSystem());
2503            result.Add("MODULE", dllFileName);
2504            result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2505            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2506        }
2507        catch (Exception e) {
2508            result.Error(e);
2509        }
2510        return result.Produce();    
2511    }
2512    
2513    String LSCPServer::GetEffectInstanceInfo(int iEffectInstanceIndex) {
2514        dmsg(2,("LSCPServer: GetEffectInstanceInfo(%d)\n", iEffectInstanceIndex));
2515        LSCPResultSet result;
2516        try {
2517            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstanceIndex);
2518            if (!pEffect)
2519                throw Exception("There is no effect instance with index " + ToString(iEffectInstanceIndex));
2520    
2521            EffectInfo* pEffectInfo = pEffect->GetEffectInfo();
2522    
2523            // convert the filename into the correct encoding as defined for LSCP
2524            // (especially in terms of special characters -> escape sequences)
2525    #if WIN32
2526            const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2527    #else
2528            // assuming POSIX
2529            const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2530    #endif
2531    
2532            result.Add("SYSTEM", pEffectInfo->EffectSystem());
2533            result.Add("MODULE", dllFileName);
2534            result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2535            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2536            result.Add("INPUT_CONTROLS", ToString(pEffect->InputControlCount()));
2537        }
2538        catch (Exception e) {
2539            result.Error(e);
2540        }
2541        return result.Produce();
2542    }
2543    
2544    String LSCPServer::GetEffectInstanceInputControlInfo(int iEffectInstanceIndex, int iInputControlIndex) {
2545        dmsg(2,("LSCPServer: GetEffectInstanceInputControlInfo(%d,%d)\n", iEffectInstanceIndex, iInputControlIndex));
2546        LSCPResultSet result;
2547        try {
2548            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstanceIndex);
2549            if (!pEffect)
2550                throw Exception("There is no effect instance with index " + ToString(iEffectInstanceIndex));
2551    
2552            EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2553            if (!pEffectControl)
2554                throw Exception(
2555                    "Effect instance " + ToString(iEffectInstanceIndex) +
2556                    " does not have an input control with index " +
2557                    ToString(iInputControlIndex)
2558                );
2559    
2560            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectControl->Description()));
2561            result.Add("VALUE", pEffectControl->Value());
2562            if (pEffectControl->MinValue())
2563                 result.Add("RANGE_MIN", *pEffectControl->MinValue());
2564            if (pEffectControl->MaxValue())
2565                 result.Add("RANGE_MAX", *pEffectControl->MaxValue());
2566            if (!pEffectControl->Possibilities().empty())
2567                 result.Add("POSSIBILITIES", pEffectControl->Possibilities());
2568            if (pEffectControl->DefaultValue())
2569                 result.Add("DEFAULT", *pEffectControl->DefaultValue());
2570        } catch (Exception e) {
2571            result.Error(e);
2572        }
2573        return result.Produce();
2574    }
2575    
2576    String LSCPServer::SetEffectInstanceInputControl(int iEffectInstanceIndex, int iInputControlIndex, double dValue) {
2577        dmsg(2,("LSCPServer: SetEffectInstanceInputControl(%d,%d,%f)\n", iEffectInstanceIndex, iInputControlIndex, dValue));
2578        LSCPResultSet result;
2579        try {
2580            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstanceIndex);
2581            if (!pEffect)
2582                throw Exception("There is no effect instance with index " + ToString(iEffectInstanceIndex));
2583    
2584            EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2585            if (!pEffectControl)
2586                throw Exception(
2587                    "Effect instance " + ToString(iEffectInstanceIndex) +
2588                    " does not have an input control with index " +
2589                    ToString(iInputControlIndex)
2590                );
2591    
2592            pEffectControl->SetValue(dValue);
2593        } catch (Exception e) {
2594            result.Error(e);
2595        }
2596        return result.Produce();
2597    }
2598    
2599    String LSCPServer::CreateEffectInstance(int index) {
2600        dmsg(2,("LSCPServer: CreateEffectInstance()\n"));
2601        LSCPResultSet result;
2602        try {
2603            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(index);
2604            if (!pEffectInfo)
2605                throw Exception("There is no effect with index " + ToString(index));
2606            Effect* pEffect = EffectFactory::Create(pEffectInfo);
2607            result.Add(pEffect->ID());
2608        } catch (Exception e) {
2609            result.Error(e);
2610        }
2611        return result.Produce();
2612    }
2613    
2614    String LSCPServer::CreateEffectInstance(String effectSystem, String module, String effectName) {
2615        dmsg(2,("LSCPServer: CreateEffectInstance()\n"));
2616        LSCPResultSet result;
2617        try {
2618            // to allow loading the same LSCP session file on different systems
2619            // successfully, probably with different effect plugin DLL paths or even
2620            // running completely different operating systems, we do the following
2621            // for finding the right effect:
2622            //
2623            // first try to search for an exact match of the effect plugin DLL
2624            // (a.k.a 'module'), to avoid picking the wrong DLL with the same
2625            // effect name ...
2626            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_MATCH_EXACTLY);
2627            // ... if no effect with exactly matchin DLL filename was found, then
2628            // try to lower the restrictions of matching the effect plugin DLL
2629            // filename and try again and again ...
2630            if (!pEffectInfo) {
2631                dmsg(2,("no exact module match, trying MODULE_IGNORE_PATH\n"));
2632                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH);
2633            }
2634            if (!pEffectInfo) {
2635                dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE\n"));
2636                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE);
2637            }
2638            if (!pEffectInfo) {
2639                dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE | MODULE_IGNORE_EXTENSION\n"));
2640                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE | EffectFactory::MODULE_IGNORE_EXTENSION);
2641            }
2642            // ... if there was still no effect found, then completely ignore the
2643            // DLL plugin filename argument and just search for the matching effect
2644            // system type and effect name
2645            if (!pEffectInfo) {
2646                dmsg(2,("no module match, trying MODULE_IGNORE_ALL\n"));
2647                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_ALL);
2648            }
2649            if (!pEffectInfo)
2650                throw Exception("There is no such effect '" + effectSystem + "' '" + module + "' '" + effectName + "'");
2651    
2652            Effect* pEffect = EffectFactory::Create(pEffectInfo);
2653            result = LSCPResultSet(pEffect->ID());
2654        } catch (Exception e) {
2655            result.Error(e);
2656        }
2657        return result.Produce();
2658    }
2659    
2660    String LSCPServer::DestroyEffectInstance(int iEffectID) {
2661        dmsg(2,("LSCPServer: DestroyEffectInstance(%d)\n", iEffectID));
2662        LSCPResultSet result;
2663        try {
2664            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectID);
2665            if (!pEffect)
2666                throw Exception("There is no effect instance with ID " + ToString(iEffectID));
2667            EffectFactory::Destroy(pEffect);
2668        } catch (Exception e) {
2669            result.Error(e);
2670        }
2671        return result.Produce();
2672    }
2673    
2674    String LSCPServer::GetEffectInstances() {
2675        dmsg(2,("LSCPServer: GetEffectInstances()\n"));
2676        LSCPResultSet result;
2677        try {
2678            int n = EffectFactory::EffectInstancesCount();
2679            result.Add(n);
2680        } catch (Exception e) {
2681            result.Error(e);
2682        }
2683        return result.Produce();
2684    }
2685    
2686    String LSCPServer::ListEffectInstances() {
2687        dmsg(2,("LSCPServer: ListEffectInstances()\n"));
2688        LSCPResultSet result;
2689        String list;
2690        try {
2691            int n = EffectFactory::EffectInstancesCount();
2692            for (int i = 0; i < n; i++) {
2693                Effect* pEffect = EffectFactory::GetEffectInstance(i);
2694                if (i) list += ",";
2695                list += ToString(pEffect->ID());
2696            }
2697        } catch (Exception e) {
2698            result.Error(e);
2699        }
2700        result.Add(list);
2701        return result.Produce();
2702    }
2703    
2704    String LSCPServer::GetMasterEffectChains(int iAudioOutputDevice) {
2705        dmsg(2,("LSCPServer: GetMasterEffectChains(%d)\n", iAudioOutputDevice));
2706        LSCPResultSet result;
2707        try {
2708            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2709            if (!devices.count(iAudioOutputDevice))
2710                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2711            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2712            int n = pDevice->MasterEffectChainCount();
2713            result.Add(n);
2714        } catch (Exception e) {
2715            result.Error(e);
2716        }
2717        return result.Produce();
2718    }
2719    
2720    String LSCPServer::ListMasterEffectChains(int iAudioOutputDevice) {
2721        dmsg(2,("LSCPServer: ListMasterEffectChains(%d)\n", iAudioOutputDevice));
2722        LSCPResultSet result;
2723        String list;
2724        try {
2725            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2726            if (!devices.count(iAudioOutputDevice))
2727                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2728            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2729            int n = pDevice->MasterEffectChainCount();
2730            for (int i = 0; i < n; i++) {
2731                EffectChain* pEffectChain = pDevice->MasterEffectChain(i);
2732                if (i) list += ",";
2733                list += ToString(pEffectChain->ID());
2734            }
2735        } catch (Exception e) {
2736            result.Error(e);
2737        }
2738        result.Add(list);
2739        return result.Produce();
2740    }
2741    
2742    String LSCPServer::AddMasterEffectChain(int iAudioOutputDevice) {
2743        dmsg(2,("LSCPServer: AddMasterEffectChain(%d)\n", iAudioOutputDevice));
2744        LSCPResultSet result;
2745        try {
2746            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2747            if (!devices.count(iAudioOutputDevice))
2748                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2749            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2750            EffectChain* pEffectChain = pDevice->AddMasterEffectChain();
2751            result.Add(pEffectChain->ID());
2752        } catch (Exception e) {
2753            result.Error(e);
2754        }
2755        return result.Produce();
2756    }
2757    
2758    String LSCPServer::RemoveMasterEffectChain(int iAudioOutputDevice, int iMasterEffectChain) {
2759        dmsg(2,("LSCPServer: RemoveMasterEffectChain(%d,%d)\n", iAudioOutputDevice, iMasterEffectChain));
2760        LSCPResultSet result;
2761        try {
2762            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2763            if (!devices.count(iAudioOutputDevice))
2764                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2765            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2766            for (int i = 0; i < pDevice->MasterEffectChainCount(); i++) {
2767                EffectChain* pEffectChain = pDevice->MasterEffectChain(i);
2768                if (pEffectChain->ID() == iMasterEffectChain) {
2769                    pDevice->RemoveMasterEffectChain(i);
2770                    return result.Produce();
2771                }
2772            }
2773            throw Exception(
2774                "There is no master effect chain with ID " +
2775                ToString(iMasterEffectChain) + " for audio output device " +
2776                ToString(iAudioOutputDevice) + "."
2777            );
2778        } catch (Exception e) {
2779            result.Error(e);
2780        }
2781        return result.Produce();
2782    }
2783    
2784    static EffectChain* _getMasterEffectChain(Sampler* pSampler, int iAudioOutputDevice, int iMasterEffectChain) throw (Exception) {
2785        std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2786        if (!devices.count(iAudioOutputDevice))
2787            throw Exception(
2788                "There is no audio output device with index " +
2789                ToString(iAudioOutputDevice) + "."
2790            );
2791        AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2792        for (int i = 0; i < pDevice->MasterEffectChainCount(); i++) {
2793            EffectChain* pEffectChain = pDevice->MasterEffectChain(i);
2794            if (pEffectChain->ID() == iMasterEffectChain) {
2795                return pEffectChain;
2796            }
2797        }
2798        throw Exception(
2799            "There is no master effect chain with ID " +
2800            ToString(iMasterEffectChain) + " for audio output device " +
2801            ToString(iAudioOutputDevice) + "."
2802        );
2803    }
2804    
2805    String LSCPServer::GetMasterEffectChainInfo(int iAudioOutputDevice, int iMasterEffectChain) {
2806        dmsg(2,("LSCPServer: GetMasterEffectChainInfo(%d,%d)\n", iAudioOutputDevice, iMasterEffectChain));
2807        LSCPResultSet result;
2808        try {
2809            EffectChain* pEffectChain =
2810                _getMasterEffectChain(pSampler, iAudioOutputDevice, iMasterEffectChain);
2811            String sEffectSequence;
2812            for (int i = 0; i < pEffectChain->EffectCount(); i++) {
2813                if (i) sEffectSequence += ",";
2814                sEffectSequence += ToString(pEffectChain->GetEffect(i)->ID());
2815            }
2816            result.Add("EFFECT_COUNT", pEffectChain->EffectCount());
2817            result.Add("EFFECT_SEQUENCE", sEffectSequence);
2818        } catch (Exception e) {
2819            result.Error(e);
2820        }
2821        return result.Produce();
2822    }
2823    
2824    String LSCPServer::AppendMasterEffectChainEffect(int iAudioOutputDevice, int iMasterEffectChain, int iEffectInstance) {
2825        dmsg(2,("LSCPServer: AppendMasterEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iMasterEffectChain, iEffectInstance));
2826        LSCPResultSet result;
2827        try {
2828            EffectChain* pEffectChain =
2829                _getMasterEffectChain(pSampler, iAudioOutputDevice, iMasterEffectChain);
2830            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2831            if (!pEffect)
2832                throw Exception("There is no effect instance with index " + ToString(iEffectInstance));
2833            pEffectChain->AppendEffect(pEffect);
2834        } catch (Exception e) {
2835            result.Error(e);
2836        }
2837        return result.Produce();
2838    }
2839    
2840    String LSCPServer::InsertMasterEffectChainEffect(int iAudioOutputDevice, int iMasterEffectChain, int iEffectInstance, int iEffectChainPosition) {
2841        dmsg(2,("LSCPServer: InsertMasterEffectChainEffect(%d,%d,%d,%d)\n", iAudioOutputDevice, iMasterEffectChain, iEffectInstance, iEffectChainPosition));
2842        LSCPResultSet result;
2843        try {
2844            EffectChain* pEffectChain =
2845                _getMasterEffectChain(pSampler, iAudioOutputDevice, iMasterEffectChain);
2846            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2847            if (!pEffect)
2848                throw Exception("There is no effect instance with index " + ToString(iEffectInstance));
2849            pEffectChain->InsertEffect(pEffect, iEffectChainPosition);
2850        } catch (Exception e) {
2851            result.Error(e);
2852        }
2853        return result.Produce();
2854    }
2855    
2856    String LSCPServer::RemoveMasterEffectChainEffect(int iAudioOutputDevice, int iMasterEffectChain, int iEffectInstance) {
2857        dmsg(2,("LSCPServer: RemoveMasterEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iMasterEffectChain, iEffectInstance));
2858        LSCPResultSet result;
2859        try {
2860            EffectChain* pEffectChain =
2861                _getMasterEffectChain(pSampler, iAudioOutputDevice, iMasterEffectChain);
2862            for (int i = 0; i < pEffectChain->EffectCount(); i++) {
2863                Effect* pEffect = pEffectChain->GetEffect(i);
2864                if (pEffect->ID() == iEffectInstance) {
2865                    pEffectChain->RemoveEffect(i);
2866                    return result.Produce();
2867                }
2868            }
2869            throw Exception(
2870                "There is no effect instance with index " +
2871                ToString(iEffectInstance)
2872            );
2873        } catch (Exception e) {
2874            result.Error(e);
2875        }
2876        return result.Produce();
2877    }
2878    
2879    String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2880        dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2881        LSCPResultSet result;
2882        try {
2883            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2884            if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2885            Engine* pEngine = pEngineChannel->GetEngine();
2886            InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2887            if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2888            InstrumentManager::instrument_id_t instrumentID;
2889            instrumentID.FileName = pEngineChannel->InstrumentFileName();
2890            instrumentID.Index    = pEngineChannel->InstrumentIndex();
2891            pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2892        } catch (Exception e) {
2893            result.Error(e);
2894        }
2895        return result.Produce();
2896    }
2897    
2898    String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) {
2899        dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2));
2900        LSCPResultSet result;
2901        try {
2902            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2903    
2904            if (Arg1 > 127 || Arg2 > 127) {
2905                throw Exception("Invalid MIDI message");
2906            }
2907    
2908            VirtualMidiDevice* pMidiDevice = NULL;
2909            std::vector<EventHandler::midi_listener_entry>::iterator iter = eventHandler.channelMidiListeners.begin();
2910            for (; iter != eventHandler.channelMidiListeners.end(); ++iter) {
2911                if ((*iter).pEngineChannel == pEngineChannel) {
2912                    pMidiDevice = (*iter).pMidiListener;
2913                    break;
2914                }
2915            }
2916            
2917            if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device");
2918    
2919            if (MidiMsg == "NOTE_ON") {
2920                pMidiDevice->SendNoteOnToDevice(Arg1, Arg2);
2921                bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2);
2922                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2923            } else if (MidiMsg == "NOTE_OFF") {
2924                pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);
2925                bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);
2926                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2927            } else if (MidiMsg == "CC") {
2928                pMidiDevice->SendCCToDevice(Arg1, Arg2);
2929                bool b = pMidiDevice->SendCCToSampler(Arg1, Arg2);
2930                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2931            } else {
2932                throw Exception("Unknown MIDI message type: " + MidiMsg);
2933            }
2934        } catch (Exception e) {
2935            result.Error(e);
2936        }
2937        return result.Produce();
2938    }
2939    
2940    /**
2941   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
2942   */   */
2943  String LSCPServer::ResetChannel(uint uiSamplerChannel) {  String LSCPServer::ResetChannel(uint uiSamplerChannel) {
2944      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
2945      LSCPResultSet result;      LSCPResultSet result;
2946      try {      try {
2947          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2948          if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));          pEngineChannel->Reset();
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");  
         pEngineChannel->GetEngine()->Reset();  
2949      }      }
2950      catch (LinuxSamplerException e) {      catch (Exception e) {
2951           result.Error(e);           result.Error(e);
2952      }      }
2953      return result.Produce();      return result.Produce();
# Line 1410  String LSCPServer::ResetSampler() { Line 2964  String LSCPServer::ResetSampler() {
2964  }  }
2965    
2966  /**  /**
2967     * Will be called by the parser to return general informations about this
2968     * sampler.
2969     */
2970    String LSCPServer::GetServerInfo() {
2971        dmsg(2,("LSCPServer: GetServerInfo()\n"));
2972        const std::string description =
2973            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2974        LSCPResultSet result;
2975        result.Add("DESCRIPTION", description);
2976        result.Add("VERSION", VERSION);
2977        result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2978    #if HAVE_SQLITE3
2979        result.Add("INSTRUMENTS_DB_SUPPORT", "yes");
2980    #else
2981        result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2982    #endif
2983    
2984        return result.Produce();
2985    }
2986    
2987    /**
2988     * Will be called by the parser to return the current number of all active streams.
2989     */
2990    String LSCPServer::GetTotalStreamCount() {
2991        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2992        LSCPResultSet result;
2993        result.Add(pSampler->GetDiskStreamCount());
2994        return result.Produce();
2995    }
2996    
2997    /**
2998     * Will be called by the parser to return the current number of all active voices.
2999     */
3000    String LSCPServer::GetTotalVoiceCount() {
3001        dmsg(2,("LSCPServer: GetTotalVoiceCount()\n"));
3002        LSCPResultSet result;
3003        result.Add(pSampler->GetVoiceCount());
3004        return result.Produce();
3005    }
3006    
3007    /**
3008     * Will be called by the parser to return the maximum number of voices.
3009     */
3010    String LSCPServer::GetTotalVoiceCountMax() {
3011        dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
3012        LSCPResultSet result;
3013        result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES);
3014        return result.Produce();
3015    }
3016    
3017    /**
3018     * Will be called by the parser to return the sampler global maximum
3019     * allowed number of voices.
3020     */
3021    String LSCPServer::GetGlobalMaxVoices() {
3022        dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));
3023        LSCPResultSet result;
3024        result.Add(GLOBAL_MAX_VOICES);
3025        return result.Produce();
3026    }
3027    
3028    /**
3029     * Will be called by the parser to set the sampler global maximum number of
3030     * voices.
3031     */
3032    String LSCPServer::SetGlobalMaxVoices(int iVoices) {
3033        dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));
3034        LSCPResultSet result;
3035        try {
3036            if (iVoices < 1) throw Exception("Maximum voices may not be less than 1");
3037            GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp
3038            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
3039            if (engines.size() > 0) {
3040                std::set<Engine*>::iterator iter = engines.begin();
3041                std::set<Engine*>::iterator end  = engines.end();
3042                for (; iter != end; ++iter) {
3043                    (*iter)->SetMaxVoices(iVoices);
3044                }
3045            }
3046            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES));
3047        } catch (Exception e) {
3048            result.Error(e);
3049        }
3050        return result.Produce();
3051    }
3052    
3053    /**
3054     * Will be called by the parser to return the sampler global maximum
3055     * allowed number of disk streams.
3056     */
3057    String LSCPServer::GetGlobalMaxStreams() {
3058        dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));
3059        LSCPResultSet result;
3060        result.Add(GLOBAL_MAX_STREAMS);
3061        return result.Produce();
3062    }
3063    
3064    /**
3065     * Will be called by the parser to set the sampler global maximum number of
3066     * disk streams.
3067     */
3068    String LSCPServer::SetGlobalMaxStreams(int iStreams) {
3069        dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));
3070        LSCPResultSet result;
3071        try {
3072            if (iStreams < 0) throw Exception("Maximum disk streams may not be negative");
3073            GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp
3074            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
3075            if (engines.size() > 0) {
3076                std::set<Engine*>::iterator iter = engines.begin();
3077                std::set<Engine*>::iterator end  = engines.end();
3078                for (; iter != end; ++iter) {
3079                    (*iter)->SetMaxDiskStreams(iStreams);
3080                }
3081            }
3082            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS));
3083        } catch (Exception e) {
3084            result.Error(e);
3085        }
3086        return result.Produce();
3087    }
3088    
3089    String LSCPServer::GetGlobalVolume() {
3090        LSCPResultSet result;
3091        result.Add(ToString(GLOBAL_VOLUME)); // see common/global.cpp
3092        return result.Produce();
3093    }
3094    
3095    String LSCPServer::SetGlobalVolume(double dVolume) {
3096        LSCPResultSet result;
3097        try {
3098            if (dVolume < 0) throw Exception("Volume may not be negative");
3099            GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
3100            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
3101        } catch (Exception e) {
3102            result.Error(e);
3103        }
3104        return result.Produce();
3105    }
3106    
3107    String LSCPServer::GetFileInstruments(String Filename) {
3108        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
3109        LSCPResultSet result;
3110        try {
3111            VerifyFile(Filename);
3112        } catch (Exception e) {
3113            result.Error(e);
3114            return result.Produce();
3115        }
3116        // try to find a sampler engine that can handle the file
3117        bool bFound = false;
3118        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3119        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
3120            Engine* pEngine = NULL;
3121            try {
3122                pEngine = EngineFactory::Create(engineTypes[i]);
3123                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3124                InstrumentManager* pManager = pEngine->GetInstrumentManager();
3125                if (pManager) {
3126                    std::vector<InstrumentManager::instrument_id_t> IDs =
3127                        pManager->GetInstrumentFileContent(Filename);
3128                    // return the amount of instruments in the file
3129                    result.Add(IDs.size());
3130                    // no more need to ask other engine types
3131                    bFound = true;
3132                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3133            } catch (Exception e) {
3134                // NOOP, as exception is thrown if engine doesn't support file
3135            }
3136            if (pEngine) EngineFactory::Destroy(pEngine);
3137        }
3138    
3139        if (!bFound) result.Error("Unknown file format");
3140        return result.Produce();
3141    }
3142    
3143    String LSCPServer::ListFileInstruments(String Filename) {
3144        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
3145        LSCPResultSet result;
3146        try {
3147            VerifyFile(Filename);
3148        } catch (Exception e) {
3149            result.Error(e);
3150            return result.Produce();
3151        }
3152        // try to find a sampler engine that can handle the file
3153        bool bFound = false;
3154        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3155        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
3156            Engine* pEngine = NULL;
3157            try {
3158                pEngine = EngineFactory::Create(engineTypes[i]);
3159                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3160                InstrumentManager* pManager = pEngine->GetInstrumentManager();
3161                if (pManager) {
3162                    std::vector<InstrumentManager::instrument_id_t> IDs =
3163                        pManager->GetInstrumentFileContent(Filename);
3164                    // return a list of IDs of the instruments in the file
3165                    String s;
3166                    for (int j = 0; j < IDs.size(); j++) {
3167                        if (s.size()) s += ",";
3168                        s += ToString(IDs[j].Index);
3169                    }
3170                    result.Add(s);
3171                    // no more need to ask other engine types
3172                    bFound = true;
3173                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3174            } catch (Exception e) {
3175                // NOOP, as exception is thrown if engine doesn't support file
3176            }
3177            if (pEngine) EngineFactory::Destroy(pEngine);
3178        }
3179    
3180        if (!bFound) result.Error("Unknown file format");
3181        return result.Produce();
3182    }
3183    
3184    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
3185        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
3186        LSCPResultSet result;
3187        try {
3188            VerifyFile(Filename);
3189        } catch (Exception e) {
3190            result.Error(e);
3191            return result.Produce();
3192        }
3193        InstrumentManager::instrument_id_t id;
3194        id.FileName = Filename;
3195        id.Index    = InstrumentID;
3196        // try to find a sampler engine that can handle the file
3197        bool bFound = false;
3198        bool bFatalErr = false;
3199        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3200        for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
3201            Engine* pEngine = NULL;
3202            try {
3203                pEngine = EngineFactory::Create(engineTypes[i]);
3204                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3205                InstrumentManager* pManager = pEngine->GetInstrumentManager();
3206                if (pManager) {
3207                    // check if the instrument index is valid
3208                    // FIXME: this won't work if an engine only supports parts of the instrument file
3209                    std::vector<InstrumentManager::instrument_id_t> IDs =
3210                        pManager->GetInstrumentFileContent(Filename);
3211                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
3212                        std::stringstream ss;
3213                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
3214                        bFatalErr = true;
3215                        throw Exception(ss.str());
3216                    }
3217                    // get the info of the requested instrument
3218                    InstrumentManager::instrument_info_t info =
3219                        pManager->GetInstrumentInfo(id);
3220                    // return detailed informations about the file
3221                    result.Add("NAME", info.InstrumentName);
3222                    result.Add("FORMAT_FAMILY", engineTypes[i]);
3223                    result.Add("FORMAT_VERSION", info.FormatVersion);
3224                    result.Add("PRODUCT", info.Product);
3225                    result.Add("ARTISTS", info.Artists);
3226    
3227                    std::stringstream ss;
3228                    bool b = false;
3229                    for (int i = 0; i < 128; i++) {
3230                        if (info.KeyBindings[i]) {
3231                            if (b) ss << ',';
3232                            ss << i; b = true;
3233                        }
3234                    }
3235                    result.Add("KEY_BINDINGS", ss.str());
3236    
3237                    b = false;
3238                    std::stringstream ss2;
3239                    for (int i = 0; i < 128; i++) {
3240                        if (info.KeySwitchBindings[i]) {
3241                            if (b) ss2 << ',';
3242                            ss2 << i; b = true;
3243                        }
3244                    }
3245                    result.Add("KEYSWITCH_BINDINGS", ss2.str());
3246                    // no more need to ask other engine types
3247                    bFound = true;
3248                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3249            } catch (Exception e) {
3250                // usually NOOP, as exception is thrown if engine doesn't support file
3251                if (bFatalErr) result.Error(e);
3252            }
3253            if (pEngine) EngineFactory::Destroy(pEngine);
3254        }
3255    
3256        if (!bFound && !bFatalErr) result.Error("Unknown file format");
3257        return result.Produce();
3258    }
3259    
3260    void LSCPServer::VerifyFile(String Filename) {
3261        #if WIN32
3262        WIN32_FIND_DATA win32FileAttributeData;
3263        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
3264        if (!res) {
3265            std::stringstream ss;
3266            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
3267            throw Exception(ss.str());
3268        }
3269        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
3270            throw Exception("Directory is specified");
3271        }
3272        #else
3273        File f(Filename);
3274        if(!f.Exist()) throw Exception(f.GetErrorMsg());
3275        if (f.IsDirectory()) throw Exception("Directory is specified");
3276        #endif
3277    }
3278    
3279    /**
3280   * 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
3281   * server for receiving event messages.   * server for receiving event messages.
3282   */   */
# Line 1435  String LSCPServer::UnsubscribeNotificati Line 3302  String LSCPServer::UnsubscribeNotificati
3302      return result.Produce();      return result.Produce();
3303  }  }
3304    
3305  static int select_callback(void * lscpResultSet, int argc,  String LSCPServer::AddDbInstrumentDirectory(String Dir) {
3306                          char **argv, char **azColName)      dmsg(2,("LSCPServer: AddDbInstrumentDirectory(Dir=%s)\n", Dir.c_str()));
3307  {      LSCPResultSet result;
3308      LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;  #if HAVE_SQLITE3
3309      resultSet->Add(argc, argv);      try {
3310      return 0;          InstrumentsDb::GetInstrumentsDb()->AddDirectory(Dir);
3311        } catch (Exception e) {
3312             result.Error(e);
3313        }
3314    #else
3315        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3316    #endif
3317        return result.Produce();
3318    }
3319    
3320    String LSCPServer::RemoveDbInstrumentDirectory(String Dir, bool Force) {
3321        dmsg(2,("LSCPServer: RemoveDbInstrumentDirectory(Dir=%s,Force=%d)\n", Dir.c_str(), Force));
3322        LSCPResultSet result;
3323    #if HAVE_SQLITE3
3324        try {
3325            InstrumentsDb::GetInstrumentsDb()->RemoveDirectory(Dir, Force);
3326        } catch (Exception e) {
3327             result.Error(e);
3328        }
3329    #else
3330        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3331    #endif
3332        return result.Produce();
3333    }
3334    
3335    String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
3336        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3337        LSCPResultSet result;
3338    #if HAVE_SQLITE3
3339        try {
3340            result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
3341        } catch (Exception e) {
3342             result.Error(e);
3343        }
3344    #else
3345        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3346    #endif
3347        return result.Produce();
3348    }
3349    
3350    String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
3351        dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3352        LSCPResultSet result;
3353    #if HAVE_SQLITE3
3354        try {
3355            String list;
3356            StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
3357    
3358            for (int i = 0; i < dirs->size(); i++) {
3359                if (list != "") list += ",";
3360                list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
3361            }
3362    
3363            result.Add(list);
3364        } catch (Exception e) {
3365             result.Error(e);
3366        }
3367    #else
3368        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3369    #endif
3370        return result.Produce();
3371    }
3372    
3373    String LSCPServer::GetDbInstrumentDirectoryInfo(String Dir) {
3374        dmsg(2,("LSCPServer: GetDbInstrumentDirectoryInfo(Dir=%s)\n", Dir.c_str()));
3375        LSCPResultSet result;
3376    #if HAVE_SQLITE3
3377        try {
3378            DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
3379    
3380            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3381            result.Add("CREATED", info.Created);
3382            result.Add("MODIFIED", info.Modified);
3383        } catch (Exception e) {
3384             result.Error(e);
3385        }
3386    #else
3387        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3388    #endif
3389        return result.Produce();
3390    }
3391    
3392    String LSCPServer::SetDbInstrumentDirectoryName(String Dir, String Name) {
3393        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryName(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
3394        LSCPResultSet result;
3395    #if HAVE_SQLITE3
3396        try {
3397            InstrumentsDb::GetInstrumentsDb()->RenameDirectory(Dir, Name);
3398        } catch (Exception e) {
3399             result.Error(e);
3400        }
3401    #else
3402        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3403    #endif
3404        return result.Produce();
3405    }
3406    
3407    String LSCPServer::MoveDbInstrumentDirectory(String Dir, String Dst) {
3408        dmsg(2,("LSCPServer: MoveDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
3409        LSCPResultSet result;
3410    #if HAVE_SQLITE3
3411        try {
3412            InstrumentsDb::GetInstrumentsDb()->MoveDirectory(Dir, Dst);
3413        } catch (Exception e) {
3414             result.Error(e);
3415        }
3416    #else
3417        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3418    #endif
3419        return result.Produce();
3420    }
3421    
3422    String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
3423        dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
3424        LSCPResultSet result;
3425    #if HAVE_SQLITE3
3426        try {
3427            InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
3428        } catch (Exception e) {
3429             result.Error(e);
3430        }
3431    #else
3432        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3433    #endif
3434        return result.Produce();
3435    }
3436    
3437    String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
3438        dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
3439        LSCPResultSet result;
3440    #if HAVE_SQLITE3
3441        try {
3442            InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
3443        } catch (Exception e) {
3444             result.Error(e);
3445        }
3446    #else
3447        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3448    #endif
3449        return result.Produce();
3450    }
3451    
3452    String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
3453        dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
3454        LSCPResultSet result;
3455    #if HAVE_SQLITE3
3456        try {
3457            int id;
3458            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3459            id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
3460            if (bBackground) result = id;
3461        } catch (Exception e) {
3462             result.Error(e);
3463        }
3464    #else
3465        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3466    #endif
3467        return result.Produce();
3468    }
3469    
3470    String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) {
3471        dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir));
3472        LSCPResultSet result;
3473    #if HAVE_SQLITE3
3474        try {
3475            int id;
3476            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3477            if (ScanMode.compare("RECURSIVE") == 0) {
3478                id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir);
3479            } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
3480                id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir);
3481            } else if (ScanMode.compare("FLAT") == 0) {
3482                id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir);
3483            } else {
3484                throw Exception("Unknown scan mode: " + ScanMode);
3485            }
3486    
3487            if (bBackground) result = id;
3488        } catch (Exception e) {
3489             result.Error(e);
3490        }
3491    #else
3492        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3493    #endif
3494        return result.Produce();
3495    }
3496    
3497    String LSCPServer::RemoveDbInstrument(String Instr) {
3498        dmsg(2,("LSCPServer: RemoveDbInstrument(Instr=%s)\n", Instr.c_str()));
3499        LSCPResultSet result;
3500    #if HAVE_SQLITE3
3501        try {
3502            InstrumentsDb::GetInstrumentsDb()->RemoveInstrument(Instr);
3503        } catch (Exception e) {
3504             result.Error(e);
3505        }
3506    #else
3507        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3508    #endif
3509        return result.Produce();
3510    }
3511    
3512    String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
3513        dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3514        LSCPResultSet result;
3515    #if HAVE_SQLITE3
3516        try {
3517            result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
3518        } catch (Exception e) {
3519             result.Error(e);
3520        }
3521    #else
3522        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3523    #endif
3524        return result.Produce();
3525    }
3526    
3527    String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
3528        dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
3529        LSCPResultSet result;
3530    #if HAVE_SQLITE3
3531        try {
3532            String list;
3533            StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
3534    
3535            for (int i = 0; i < instrs->size(); i++) {
3536                if (list != "") list += ",";
3537                list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
3538            }
3539    
3540            result.Add(list);
3541        } catch (Exception e) {
3542             result.Error(e);
3543        }
3544    #else
3545        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3546    #endif
3547        return result.Produce();
3548    }
3549    
3550    String LSCPServer::GetDbInstrumentInfo(String Instr) {
3551        dmsg(2,("LSCPServer: GetDbInstrumentInfo(Instr=%s)\n", Instr.c_str()));
3552        LSCPResultSet result;
3553    #if HAVE_SQLITE3
3554        try {
3555            DbInstrument info = InstrumentsDb::GetInstrumentsDb()->GetInstrumentInfo(Instr);
3556    
3557            result.Add("INSTRUMENT_FILE", info.InstrFile);
3558            result.Add("INSTRUMENT_NR", info.InstrNr);
3559            result.Add("FORMAT_FAMILY", info.FormatFamily);
3560            result.Add("FORMAT_VERSION", info.FormatVersion);
3561            result.Add("SIZE", (int)info.Size);
3562            result.Add("CREATED", info.Created);
3563            result.Add("MODIFIED", info.Modified);
3564            result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3565            result.Add("IS_DRUM", info.IsDrum);
3566            result.Add("PRODUCT", _escapeLscpResponse(info.Product));
3567            result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
3568            result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
3569        } catch (Exception e) {
3570             result.Error(e);
3571        }
3572    #else
3573        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3574    #endif
3575        return result.Produce();
3576    }
3577    
3578    String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
3579        dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
3580        LSCPResultSet result;
3581    #if HAVE_SQLITE3
3582        try {
3583            ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
3584    
3585            result.Add("FILES_TOTAL", job.FilesTotal);
3586            result.Add("FILES_SCANNED", job.FilesScanned);
3587            result.Add("SCANNING", job.Scanning);
3588            result.Add("STATUS", job.Status);
3589        } catch (Exception e) {
3590             result.Error(e);
3591        }
3592    #else
3593        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3594    #endif
3595        return result.Produce();
3596    }
3597    
3598    String LSCPServer::SetDbInstrumentName(String Instr, String Name) {
3599        dmsg(2,("LSCPServer: SetDbInstrumentName(Instr=%s,Name=%s)\n", Instr.c_str(), Name.c_str()));
3600        LSCPResultSet result;
3601    #if HAVE_SQLITE3
3602        try {
3603            InstrumentsDb::GetInstrumentsDb()->RenameInstrument(Instr, Name);
3604        } catch (Exception e) {
3605             result.Error(e);
3606        }
3607    #else
3608        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3609    #endif
3610        return result.Produce();
3611    }
3612    
3613    String LSCPServer::MoveDbInstrument(String Instr, String Dst) {
3614        dmsg(2,("LSCPServer: MoveDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3615        LSCPResultSet result;
3616    #if HAVE_SQLITE3
3617        try {
3618            InstrumentsDb::GetInstrumentsDb()->MoveInstrument(Instr, Dst);
3619        } catch (Exception e) {
3620             result.Error(e);
3621        }
3622    #else
3623        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3624    #endif
3625        return result.Produce();
3626    }
3627    
3628    String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
3629        dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
3630        LSCPResultSet result;
3631    #if HAVE_SQLITE3
3632        try {
3633            InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
3634        } catch (Exception e) {
3635             result.Error(e);
3636        }
3637    #else
3638        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3639    #endif
3640        return result.Produce();
3641    }
3642    
3643    String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
3644        dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
3645        LSCPResultSet result;
3646    #if HAVE_SQLITE3
3647        try {
3648            InstrumentsDb::GetInstrumentsDb()->SetInstrumentDescription(Instr, Desc);
3649        } catch (Exception e) {
3650             result.Error(e);
3651        }
3652    #else
3653        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3654    #endif
3655        return result.Produce();
3656  }  }
3657    
3658  String LSCPServer::QueryDatabase(String query) {  String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) {
3659        dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str()));
3660      LSCPResultSet result;      LSCPResultSet result;
3661  #if HAVE_SQLITE3  #if HAVE_SQLITE3
3662      char* zErrMsg = NULL;      try {
3663      sqlite3 *db;          InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath);
3664      String selectStr = "SELECT " + query;      } catch (Exception e) {
3665             result.Error(e);
3666        }
3667    #else
3668        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3669    #endif
3670        return result.Produce();
3671    }
3672    
3673      int rc = sqlite3_open("linuxsampler.db", &db);  String LSCPServer::FindLostDbInstrumentFiles() {
3674      if (rc == SQLITE_OK)      dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n"));
3675      {      LSCPResultSet result;
3676              rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);  #if HAVE_SQLITE3
3677        try {
3678            String list;
3679            StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles();
3680    
3681            for (int i = 0; i < pLostFiles->size(); i++) {
3682                if (list != "") list += ",";
3683                list += "'" + pLostFiles->at(i) + "'";
3684            }
3685    
3686            result.Add(list);
3687        } catch (Exception e) {
3688             result.Error(e);
3689      }      }
3690      if ( rc != SQLITE_OK )  #else
3691      {      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3692              result.Error(String(zErrMsg), rc);  #endif
3693        return result.Produce();
3694    }
3695    
3696    String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
3697        dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
3698        LSCPResultSet result;
3699    #if HAVE_SQLITE3
3700        try {
3701            SearchQuery Query;
3702            std::map<String,String>::iterator iter;
3703            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3704                if (iter->first.compare("NAME") == 0) {
3705                    Query.Name = iter->second;
3706                } else if (iter->first.compare("CREATED") == 0) {
3707                    Query.SetCreated(iter->second);
3708                } else if (iter->first.compare("MODIFIED") == 0) {
3709                    Query.SetModified(iter->second);
3710                } else if (iter->first.compare("DESCRIPTION") == 0) {
3711                    Query.Description = iter->second;
3712                } else {
3713                    throw Exception("Unknown search criteria: " + iter->first);
3714                }
3715            }
3716    
3717            String list;
3718            StringListPtr pDirectories =
3719                InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
3720    
3721            for (int i = 0; i < pDirectories->size(); i++) {
3722                if (list != "") list += ",";
3723                list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
3724            }
3725    
3726            result.Add(list);
3727        } catch (Exception e) {
3728             result.Error(e);
3729        }
3730    #else
3731        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3732    #endif
3733        return result.Produce();
3734    }
3735    
3736    String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
3737        dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
3738        LSCPResultSet result;
3739    #if HAVE_SQLITE3
3740        try {
3741            SearchQuery Query;
3742            std::map<String,String>::iterator iter;
3743            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
3744                if (iter->first.compare("NAME") == 0) {
3745                    Query.Name = iter->second;
3746                } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
3747                    Query.SetFormatFamilies(iter->second);
3748                } else if (iter->first.compare("SIZE") == 0) {
3749                    Query.SetSize(iter->second);
3750                } else if (iter->first.compare("CREATED") == 0) {
3751                    Query.SetCreated(iter->second);
3752                } else if (iter->first.compare("MODIFIED") == 0) {
3753                    Query.SetModified(iter->second);
3754                } else if (iter->first.compare("DESCRIPTION") == 0) {
3755                    Query.Description = iter->second;
3756                } else if (iter->first.compare("IS_DRUM") == 0) {
3757                    if (!strcasecmp(iter->second.c_str(), "true")) {
3758                        Query.InstrType = SearchQuery::DRUM;
3759                    } else {
3760                        Query.InstrType = SearchQuery::CHROMATIC;
3761                    }
3762                } else if (iter->first.compare("PRODUCT") == 0) {
3763                     Query.Product = iter->second;
3764                } else if (iter->first.compare("ARTISTS") == 0) {
3765                     Query.Artists = iter->second;
3766                } else if (iter->first.compare("KEYWORDS") == 0) {
3767                     Query.Keywords = iter->second;
3768                } else {
3769                    throw Exception("Unknown search criteria: " + iter->first);
3770                }
3771            }
3772    
3773            String list;
3774            StringListPtr pInstruments =
3775                InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
3776    
3777            for (int i = 0; i < pInstruments->size(); i++) {
3778                if (list != "") list += ",";
3779                list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
3780            }
3781    
3782            result.Add(list);
3783        } catch (Exception e) {
3784             result.Error(e);
3785        }
3786    #else
3787        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3788    #endif
3789        return result.Produce();
3790    }
3791    
3792    String LSCPServer::FormatInstrumentsDb() {
3793        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
3794        LSCPResultSet result;
3795    #if HAVE_SQLITE3
3796        try {
3797            InstrumentsDb::GetInstrumentsDb()->Format();
3798        } catch (Exception e) {
3799             result.Error(e);
3800      }      }
     sqlite3_close(db);  
3801  #else  #else
3802      result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);      result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3803  #endif  #endif
3804      return result.Produce();      return result.Produce();
3805  }  }
3806    
3807    
3808  /**  /**
3809   * 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
3810   * mode is enabled, all commands from the client will (immediately) be   * mode is enabled, all commands from the client will (immediately) be
# Line 1477  String LSCPServer::SetEcho(yyparse_param Line 3816  String LSCPServer::SetEcho(yyparse_param
3816      try {      try {
3817          if      (boolean_value == 0) pSession->bVerbose = false;          if      (boolean_value == 0) pSession->bVerbose = false;
3818          else if (boolean_value == 1) pSession->bVerbose = true;          else if (boolean_value == 1) pSession->bVerbose = true;
3819          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");
3820      }      }
3821      catch (LinuxSamplerException e) {      catch (Exception e) {
3822           result.Error(e);           result.Error(e);
3823      }      }
3824      return result.Produce();      return result.Produce();
3825  }  }
3826    
3827    }

Legend:
Removed from v.556  
changed lines
  Added in v.2135

  ViewVC Help
Powered by ViewVC