/[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 1350 by iliev, Sun Sep 16 23:06:10 2007 UTC revision 2324 by persson, Sun Mar 4 09:01:32 2012 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 - 2007 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2012 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"
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  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
# Line 35  Line 43 
43  #include "../engines/EngineChannelFactory.h"  #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 61  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, long int addr, short int port) : Thread(true, false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
111      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
112      SocketAddress.sin_addr.s_addr = addr;      SocketAddress.sin_addr.s_addr = addr;
113      SocketAddress.sin_port        = port;      SocketAddress.sin_port        = port;
# Line 87  LSCPServer::LSCPServer(Sampler* pSampler Line 133  LSCPServer::LSCPServer(Sampler* pSampler
133      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
134      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");      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");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
138      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");      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        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_instance_count, "EFFECT_INSTANCE_COUNT");
142        LSCPEvent::RegisterEvent(LSCPEvent::event_fx_instance_info, "EFFECT_INSTANCE_INFO");
143        LSCPEvent::RegisterEvent(LSCPEvent::event_send_fx_chain_count, "SEND_EFFECT_CHAIN_COUNT");
144        LSCPEvent::RegisterEvent(LSCPEvent::event_send_fx_chain_info, "SEND_EFFECT_CHAIN_INFO");
145      hSocket = -1;      hSocket = -1;
146  }  }
147    
148  LSCPServer::~LSCPServer() {  LSCPServer::~LSCPServer() {
149        CloseAllConnections();
150        InstrumentManager::StopBackgroundThread();
151    #if defined(WIN32)
152        if (hSocket >= 0) closesocket(hSocket);
153    #else
154      if (hSocket >= 0) close(hSocket);      if (hSocket >= 0) close(hSocket);
155    #endif
156    }
157    
158    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
159        this->pParent = pParent;
160    }
161    
162    LSCPServer::EventHandler::~EventHandler() {
163        std::vector<midi_listener_entry> l = channelMidiListeners;
164        channelMidiListeners.clear();
165        for (int i = 0; i < l.size(); i++)
166            delete l[i].pMidiListener;
167  }  }
168    
169  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
170      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
171  }  }
172    
173    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
174        pChannel->AddEngineChangeListener(this);
175    }
176    
177    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
178        if (!pChannel->GetEngineChannel()) return;
179        EngineToBeChanged(pChannel->Index());
180    }
181    
182    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
183        SamplerChannel* pSamplerChannel =
184            pParent->pSampler->GetSamplerChannel(ChannelId);
185        if (!pSamplerChannel) return;
186        EngineChannel* pEngineChannel =
187            pSamplerChannel->GetEngineChannel();
188        if (!pEngineChannel) return;
189        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
190            if ((*iter).pEngineChannel == pEngineChannel) {
191                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
192                pEngineChannel->Disconnect(pMidiListener);
193                channelMidiListeners.erase(iter);
194                delete pMidiListener;
195                return;
196            }
197        }
198    }
199    
200    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
201        SamplerChannel* pSamplerChannel =
202            pParent->pSampler->GetSamplerChannel(ChannelId);
203        if (!pSamplerChannel) return;
204        EngineChannel* pEngineChannel =
205            pSamplerChannel->GetEngineChannel();
206        if (!pEngineChannel) return;
207        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
208        pEngineChannel->Connect(pMidiListener);
209        midi_listener_entry entry = {
210            pSamplerChannel, pEngineChannel, pMidiListener
211        };
212        channelMidiListeners.push_back(entry);
213    }
214    
215  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
216      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
217  }  }
# Line 108  void LSCPServer::EventHandler::MidiDevic Line 220  void LSCPServer::EventHandler::MidiDevic
220      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
221  }  }
222    
223    void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
224        pDevice->RemoveMidiPortCountListener(this);
225        for (int i = 0; i < pDevice->PortCount(); ++i)
226            MidiPortToBeRemoved(pDevice->GetPort(i));
227    }
228    
229    void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
230        pDevice->AddMidiPortCountListener(this);
231        for (int i = 0; i < pDevice->PortCount(); ++i)
232            MidiPortAdded(pDevice->GetPort(i));
233    }
234    
235    void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
236        // yet unused
237    }
238    
239    void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
240        for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
241            if ((*iter).pPort == pPort) {
242                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
243                pPort->Disconnect(pMidiListener);
244                deviceMidiListeners.erase(iter);
245                delete pMidiListener;
246                return;
247            }
248        }
249    }
250    
251    void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
252        // find out the device ID
253        std::map<uint, MidiInputDevice*> devices =
254            pParent->pSampler->GetMidiInputDevices();
255        for (
256            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
257            iter != devices.end(); ++iter
258        ) {
259            if (iter->second == pPort->GetDevice()) { // found
260                VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
261                pPort->Connect(pMidiListener);
262                device_midi_listener_entry entry = {
263                    pPort, pMidiListener, iter->first
264                };
265                deviceMidiListeners.push_back(entry);
266                return;
267            }
268        }
269    }
270    
271  void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {  void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
272      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
273  }  }
# Line 144  void LSCPServer::EventHandler::TotalVoic Line 304  void LSCPServer::EventHandler::TotalVoic
304      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
305  }  }
306    
307    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
308        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
309    }
310    
311  #if HAVE_SQLITE3  #if HAVE_SQLITE3
312  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
313      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
# Line 178  void LSCPServer::DbInstrumentsEventHandl Line 342  void LSCPServer::DbInstrumentsEventHandl
342  }  }
343  #endif // HAVE_SQLITE3  #endif // HAVE_SQLITE3
344    
345    void LSCPServer::RemoveListeners() {
346        pSampler->RemoveChannelCountListener(&eventHandler);
347        pSampler->RemoveAudioDeviceCountListener(&eventHandler);
348        pSampler->RemoveMidiDeviceCountListener(&eventHandler);
349        pSampler->RemoveVoiceCountListener(&eventHandler);
350        pSampler->RemoveStreamCountListener(&eventHandler);
351        pSampler->RemoveBufferFillListener(&eventHandler);
352        pSampler->RemoveTotalStreamCountListener(&eventHandler);
353        pSampler->RemoveTotalVoiceCountListener(&eventHandler);
354        pSampler->RemoveFxSendCountListener(&eventHandler);
355        MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler);
356        MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler);
357        MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler);
358        MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler);
359    #if HAVE_SQLITE3
360        InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler);
361    #endif
362    }
363    
364  /**  /**
365   * Blocks the calling thread until the LSCP Server is initialized and   * Blocks the calling thread until the LSCP Server is initialized and
# Line 194  int LSCPServer::WaitUntilInitialized(lon Line 376  int LSCPServer::WaitUntilInitialized(lon
376  }  }
377    
378  int LSCPServer::Main() {  int LSCPServer::Main() {
379            #if defined(WIN32)
380            WSADATA wsaData;
381            int iResult;
382            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
383            if (iResult != 0) {
384                    std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
385                    exit(EXIT_FAILURE);
386            }
387            #endif
388      hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
389      if (hSocket < 0) {      if (hSocket < 0) {
390          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
# Line 207  int LSCPServer::Main() { Line 398  int LSCPServer::Main() {
398              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
399                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
400                      std::cerr << "gave up!" << std::endl;                      std::cerr << "gave up!" << std::endl;
401                        #if defined(WIN32)
402                        closesocket(hSocket);
403                        #else
404                      close(hSocket);                      close(hSocket);
405                        #endif
406                      //return -1;                      //return -1;
407                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
408                  }                  }
# Line 227  int LSCPServer::Main() { Line 422  int LSCPServer::Main() {
422      pSampler->AddVoiceCountListener(&eventHandler);      pSampler->AddVoiceCountListener(&eventHandler);
423      pSampler->AddStreamCountListener(&eventHandler);      pSampler->AddStreamCountListener(&eventHandler);
424      pSampler->AddBufferFillListener(&eventHandler);      pSampler->AddBufferFillListener(&eventHandler);
425        pSampler->AddTotalStreamCountListener(&eventHandler);
426      pSampler->AddTotalVoiceCountListener(&eventHandler);      pSampler->AddTotalVoiceCountListener(&eventHandler);
427      pSampler->AddFxSendCountListener(&eventHandler);      pSampler->AddFxSendCountListener(&eventHandler);
428      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
# Line 246  int LSCPServer::Main() { Line 442  int LSCPServer::Main() {
442      timeval timeout;      timeval timeout;
443    
444      while (true) {      while (true) {
445            #if CONFIG_PTHREAD_TESTCANCEL
446                    TestCancel();
447            #endif
448          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
449          {          {
450                EngineChannelFactory::EngineChannelsMutex.Lock();
451              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
452              std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();              std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
453              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
454              for (; itEngineChannel != itEnd; ++itEngineChannel) {              for (; itEngineChannel != itEnd; ++itEngineChannel) {
455                  if ((*itEngineChannel)->StatusChanged()) {                  if ((*itEngineChannel)->StatusChanged()) {
456                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex));                      SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index()));
457                  }                  }
458    
459                  for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {                  for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
460                      FxSend* fxs = (*itEngineChannel)->GetFxSend(i);                      FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
461                      if(fxs != NULL && fxs->IsInfoChanged()) {                      if(fxs != NULL && fxs->IsInfoChanged()) {
462                          int chn = (*itEngineChannel)->iSamplerChannelIndex;                          int chn = (*itEngineChannel)->GetSamplerChannel()->Index();
463                          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));                          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id()));
464                          fxs->SetInfoChanged(false);                          fxs->SetInfoChanged(false);
465                      }                      }
466                  }                  }
467              }              }
468                EngineChannelFactory::EngineChannelsMutex.Unlock();
469            }
470    
471            // check if MIDI data arrived on some engine channel
472            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
473                const EventHandler::midi_listener_entry entry =
474                    eventHandler.channelMidiListeners[i];
475                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
476                if (pMidiListener->NotesChanged()) {
477                    for (int iNote = 0; iNote < 128; iNote++) {
478                        if (pMidiListener->NoteChanged(iNote)) {
479                            const bool bActive = pMidiListener->NoteIsActive(iNote);
480                            LSCPServer::SendLSCPNotify(
481                                LSCPEvent(
482                                    LSCPEvent::event_channel_midi,
483                                    entry.pSamplerChannel->Index(),
484                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
485                                    iNote,
486                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
487                                            : pMidiListener->NoteOffVelocity(iNote)
488                                )
489                            );
490                        }
491                    }
492                }
493            }
494    
495            // check if MIDI data arrived on some MIDI device
496            for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
497                const EventHandler::device_midi_listener_entry entry =
498                    eventHandler.deviceMidiListeners[i];
499                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
500                if (pMidiListener->NotesChanged()) {
501                    for (int iNote = 0; iNote < 128; iNote++) {
502                        if (pMidiListener->NoteChanged(iNote)) {
503                            const bool bActive = pMidiListener->NoteIsActive(iNote);
504                            LSCPServer::SendLSCPNotify(
505                                LSCPEvent(
506                                    LSCPEvent::event_device_midi,
507                                    entry.uiDeviceID,
508                                    entry.pPort->GetPortNumber(),
509                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
510                                    iNote,
511                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
512                                            : pMidiListener->NoteOffVelocity(iNote)
513                                )
514                            );
515                        }
516                    }
517                }
518          }          }
519    
520          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
# Line 285  int LSCPServer::Main() { Line 535  int LSCPServer::Main() {
535    
536          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);          int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout);
537    
538          if (retval == 0)          if (retval == 0 || (retval == -1 && errno == EINTR))
539                  continue; //Nothing try again                  continue; //Nothing try again
540          if (retval == -1) {          if (retval == -1) {
541                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
542                    #if defined(WIN32)
543                    closesocket(hSocket);
544                    #else
545                  close(hSocket);                  close(hSocket);
546                    #endif
547                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
548          }          }
549    
# Line 301  int LSCPServer::Main() { Line 555  int LSCPServer::Main() {
555                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
556                  }                  }
557    
558                    #if defined(WIN32)
559                    u_long nonblock_io = 1;
560                    if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
561                      std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
562                      exit(EXIT_FAILURE);
563                    }
564            #else
565                    struct linger linger;
566                    linger.l_onoff = 1;
567                    linger.l_linger = 0;
568                    if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) {
569                        std::cerr << "LSCPServer: Failed to set SO_LINGER\n";
570                    }
571    
572                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
573                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
574                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
575                  }                  }
576                    #endif
577    
578                  // Parser initialization                  // Parser initialization
579                  yyparse_param_t yyparse_param;                  yyparse_param_t yyparse_param;
# Line 363  void LSCPServer::CloseConnection( std::v Line 632  void LSCPServer::CloseConnection( std::v
632          NotifyMutex.Lock();          NotifyMutex.Lock();
633          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
634          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
635            #if defined(WIN32)
636            closesocket(socket);
637            #else
638          close(socket);          close(socket);
639            #endif
640          NotifyMutex.Unlock();          NotifyMutex.Unlock();
641  }  }
642    
643    void LSCPServer::CloseAllConnections() {
644        std::vector<yyparse_param_t>::iterator iter = Sessions.begin();
645        while(iter != Sessions.end()) {
646            CloseConnection(iter);
647            iter = Sessions.begin();
648        }
649    }
650    
651    void LSCPServer::LockRTNotify() {
652        RTNotifyMutex.Lock();
653    }
654    
655    void LSCPServer::UnlockRTNotify() {
656        RTNotifyMutex.Unlock();
657    }
658    
659  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
660          int subs = 0;          int subs = 0;
661          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 442  bool LSCPServer::GetLSCPCommand( std::ve Line 731  bool LSCPServer::GetLSCPCommand( std::ve
731          char c;          char c;
732          int i = 0;          int i = 0;
733          while (true) {          while (true) {
734                    #if defined(WIN32)
735                    int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
736                    #else
737                  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
738                    #endif
739                  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
740                          CloseConnection(iter);                          CloseConnection(iter);
741                          break;                          break;
# Line 457  bool LSCPServer::GetLSCPCommand( std::ve Line 750  bool LSCPServer::GetLSCPCommand( std::ve
750                          }                          }
751                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
752                  }                  }
753                    #if defined(WIN32)
754                    if (result == SOCKET_ERROR) {
755                        int wsa_lasterror = WSAGetLastError();
756                            if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
757                                    return false;
758                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
759                            CloseConnection(iter);
760                            break;
761                    }
762                    #else
763                  if (result == -1) {                  if (result == -1) {
764                          if (errno == EAGAIN) //Would block, try again later.                          if (errno == EAGAIN) //Would block, try again later.
765                                  return false;                                  return false;
# Line 495  bool LSCPServer::GetLSCPCommand( std::ve Line 798  bool LSCPServer::GetLSCPCommand( std::ve
798                          CloseConnection(iter);                          CloseConnection(iter);
799                          break;                          break;
800                  }                  }
801                    #endif
802          }          }
803          return false;          return false;
804  }  }
# Line 768  String LSCPServer::GetEngineInfo(String Line 1072  String LSCPServer::GetEngineInfo(String
1072      LockRTNotify();      LockRTNotify();
1073      try {      try {
1074          Engine* pEngine = EngineFactory::Create(EngineName);          Engine* pEngine = EngineFactory::Create(EngineName);
1075          result.Add("DESCRIPTION", pEngine->Description());          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1076          result.Add("VERSION",     pEngine->Version());          result.Add("VERSION",     pEngine->Version());
1077          EngineFactory::Destroy(pEngine);          EngineFactory::Destroy(pEngine);
1078      }      }
# Line 841  String LSCPServer::GetChannelInfo(uint u Line 1145  String LSCPServer::GetChannelInfo(uint u
1145          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
1146          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
1147    
1148            // convert the filename into the correct encoding as defined for LSCP
1149            // (especially in terms of special characters -> escape sequences)
1150            if (InstrumentFileName != "NONE" && InstrumentFileName != "") {
1151    #if WIN32
1152                InstrumentFileName = Path::fromWindows(InstrumentFileName).toLscp();
1153    #else
1154                // assuming POSIX
1155                InstrumentFileName = Path::fromPosix(InstrumentFileName).toLscp();
1156    #endif
1157            }
1158    
1159          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE", InstrumentFileName);
1160          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
1161          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
1162          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
1163          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
1164          result.Add("SOLO", Solo);          result.Add("SOLO", Solo);
# Line 863  String LSCPServer::GetVoiceCount(uint ui Line 1178  String LSCPServer::GetVoiceCount(uint ui
1178      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
1179      LSCPResultSet result;      LSCPResultSet result;
1180      try {      try {
1181          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine loaded on sampler channel");  
1182          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1183          result.Add(pEngineChannel->GetEngine()->VoiceCount());          result.Add(pEngineChannel->GetEngine()->VoiceCount());
1184      }      }
# Line 884  String LSCPServer::GetStreamCount(uint u Line 1196  String LSCPServer::GetStreamCount(uint u
1196      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
1197      LSCPResultSet result;      LSCPResultSet result;
1198      try {      try {
1199          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1200          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1201          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());          result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
1202      }      }
# Line 905  String LSCPServer::GetBufferFill(fill_re Line 1214  String LSCPServer::GetBufferFill(fill_re
1214      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));      dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
1215      LSCPResultSet result;      LSCPResultSet result;
1216      try {      try {
1217          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1218          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");          if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel");
1219          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");          if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
1220          else {          else {
# Line 996  String LSCPServer::GetMidiInputDriverInf Line 1302  String LSCPServer::GetMidiInputDriverInf
1302              for (;iter != parameters.end(); iter++) {              for (;iter != parameters.end(); iter++) {
1303                  if (s != "") s += ",";                  if (s != "") s += ",";
1304                  s += iter->first;                  s += iter->first;
1305                    delete iter->second;
1306              }              }
1307              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1308          }          }
# Line 1020  String LSCPServer::GetAudioOutputDriverI Line 1327  String LSCPServer::GetAudioOutputDriverI
1327              for (;iter != parameters.end(); iter++) {              for (;iter != parameters.end(); iter++) {
1328                  if (s != "") s += ",";                  if (s != "") s += ",";
1329                  s += iter->first;                  s += iter->first;
1330                    delete iter->second;
1331              }              }
1332              result.Add("PARAMETERS", s);              result.Add("PARAMETERS", s);
1333          }          }
# Line 1050  String LSCPServer::GetMidiInputDriverPar Line 1358  String LSCPServer::GetMidiInputDriverPar
1358          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1359          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1360          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1361            delete pParameter;
1362      }      }
1363      catch (Exception e) {      catch (Exception e) {
1364          result.Error(e);          result.Error(e);
# Line 1077  String LSCPServer::GetAudioOutputDriverP Line 1386  String LSCPServer::GetAudioOutputDriverP
1386          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);          if (oRangeMin)      result.Add("RANGE_MIN",     *oRangeMin);
1387          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);          if (oRangeMax)      result.Add("RANGE_MAX",     *oRangeMax);
1388          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);          if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
1389            delete pParameter;
1390      }      }
1391      catch (Exception e) {      catch (Exception e) {
1392          result.Error(e);          result.Error(e);
# Line 1543  String LSCPServer::SetMIDIInputType(Stri Line 1853  String LSCPServer::SetMIDIInputType(Stri
1853              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);              pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1854              // Make it with at least one initial port.              // Make it with at least one initial port.
1855              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();              std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
             parameters["PORTS"]->SetValue("1");  
1856          }          }
1857          // Must have a device...          // Must have a device...
1858          if (pDevice == NULL)          if (pDevice == NULL)
# Line 1586  String LSCPServer::SetVolume(double dVol Line 1895  String LSCPServer::SetVolume(double dVol
1895      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));      dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1896      LSCPResultSet result;      LSCPResultSet result;
1897      try {      try {
1898          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1899          pEngineChannel->Volume(dVolume);          pEngineChannel->Volume(dVolume);
1900      }      }
1901      catch (Exception e) {      catch (Exception e) {
# Line 1605  String LSCPServer::SetChannelMute(bool b Line 1911  String LSCPServer::SetChannelMute(bool b
1911      dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));      dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel));
1912      LSCPResultSet result;      LSCPResultSet result;
1913      try {      try {
1914          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
   
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1915    
1916          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);          if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0);
1917          else pEngineChannel->SetMute(1);          else pEngineChannel->SetMute(1);
# Line 1626  String LSCPServer::SetChannelSolo(bool b Line 1928  String LSCPServer::SetChannelSolo(bool b
1928      dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));      dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel));
1929      LSCPResultSet result;      LSCPResultSet result;
1930      try {      try {
1931          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
   
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
1932    
1933          bool oldSolo = pEngineChannel->GetSolo();          bool oldSolo = pEngineChannel->GetSolo();
1934          bool hadSoloChannel = HasSoloChannel();          bool hadSoloChannel = HasSoloChannel();
# Line 1750  String LSCPServer::GetMidiInstrumentMapp Line 2048  String LSCPServer::GetMidiInstrumentMapp
2048      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n"));
2049      LSCPResultSet result;      LSCPResultSet result;
2050      try {      try {
2051          result.Add(MidiInstrumentMapper::Entries(MidiMapID).size());          result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID));
2052      } catch (Exception e) {      } catch (Exception e) {
2053          result.Error(e);          result.Error(e);
2054      }      }
# Line 1761  String LSCPServer::GetMidiInstrumentMapp Line 2059  String LSCPServer::GetMidiInstrumentMapp
2059  String LSCPServer::GetAllMidiInstrumentMappings() {  String LSCPServer::GetAllMidiInstrumentMappings() {
2060      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));      dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n"));
2061      LSCPResultSet result;      LSCPResultSet result;
2062      std::vector<int> maps = MidiInstrumentMapper::Maps();      try {
2063      int totalMappings = 0;          result.Add(MidiInstrumentMapper::GetInstrumentCount());
2064      for (int i = 0; i < maps.size(); i++) {      } catch (Exception e) {
2065          try {          result.Error(e);
             totalMappings += MidiInstrumentMapper::Entries(maps[i]).size();  
         } catch (Exception e) { /*NOOP*/ }  
2066      }      }
     result.Add(totalMappings);  
2067      return result.Produce();      return result.Produce();
2068  }  }
2069    
# Line 1776  String LSCPServer::GetMidiInstrumentMapp Line 2071  String LSCPServer::GetMidiInstrumentMapp
2071      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));      dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n"));
2072      LSCPResultSet result;      LSCPResultSet result;
2073      try {      try {
2074          midi_prog_index_t idx;          MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg);
2075          idx.midi_bank_msb = (MidiBank >> 7) & 0x7f;          // convert the filename into the correct encoding as defined for LSCP
2076          idx.midi_bank_lsb = MidiBank & 0x7f;          // (especially in terms of special characters -> escape sequences)
2077          idx.midi_prog     = MidiProg;  #if WIN32
2078            const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp();
2079          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(MidiMapID);  #else
2080          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);          // assuming POSIX
2081          if (iter == mappings.end()) result.Error("there is no map entry with that index");          const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp();
2082          else { // found  #endif
2083              result.Add("NAME", iter->second.Name);  
2084              result.Add("ENGINE_NAME", iter->second.EngineName);          result.Add("NAME", _escapeLscpResponse(entry.Name));
2085              result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile);          result.Add("ENGINE_NAME", entry.EngineName);
2086              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);          result.Add("INSTRUMENT_FILE", instrumentFileName);
2087              String instrumentName;          result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex);
2088              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);          String instrumentName;
2089              if (pEngine) {          Engine* pEngine = EngineFactory::Create(entry.EngineName);
2090                  if (pEngine->GetInstrumentManager()) {          if (pEngine) {
2091                      InstrumentManager::instrument_id_t instrID;              if (pEngine->GetInstrumentManager()) {
2092                      instrID.FileName = iter->second.InstrumentFile;                  InstrumentManager::instrument_id_t instrID;
2093                      instrID.Index    = iter->second.InstrumentIndex;                  instrID.FileName = entry.InstrumentFile;
2094                      instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);                  instrID.Index    = entry.InstrumentIndex;
2095                  }                  instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID);
                 EngineFactory::Destroy(pEngine);  
2096              }              }
2097              result.Add("INSTRUMENT_NAME", instrumentName);              EngineFactory::Destroy(pEngine);
2098              switch (iter->second.LoadMode) {          }
2099                  case MidiInstrumentMapper::ON_DEMAND:          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
2100                      result.Add("LOAD_MODE", "ON_DEMAND");          switch (entry.LoadMode) {
2101                      break;              case MidiInstrumentMapper::ON_DEMAND:
2102                  case MidiInstrumentMapper::ON_DEMAND_HOLD:                  result.Add("LOAD_MODE", "ON_DEMAND");
2103                      result.Add("LOAD_MODE", "ON_DEMAND_HOLD");                  break;
2104                      break;              case MidiInstrumentMapper::ON_DEMAND_HOLD:
2105                  case MidiInstrumentMapper::PERSISTENT:                  result.Add("LOAD_MODE", "ON_DEMAND_HOLD");
2106                      result.Add("LOAD_MODE", "PERSISTENT");                  break;
2107                      break;              case MidiInstrumentMapper::PERSISTENT:
2108                  default:                  result.Add("LOAD_MODE", "PERSISTENT");
2109                      throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");                  break;
2110              }              default:
2111              result.Add("VOLUME", iter->second.Volume);                  throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!");
2112          }          }
2113            result.Add("VOLUME", entry.Volume);
2114      } catch (Exception e) {      } catch (Exception e) {
2115          result.Error(e);          result.Error(e);
2116      }      }
# Line 1955  String LSCPServer::GetMidiInstrumentMap( Line 2250  String LSCPServer::GetMidiInstrumentMap(
2250      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
2251      LSCPResultSet result;      LSCPResultSet result;
2252      try {      try {
2253          result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID));          result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
2254          result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);          result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2255      } catch (Exception e) {      } catch (Exception e) {
2256          result.Error(e);          result.Error(e);
# Line 1986  String LSCPServer::SetChannelMap(uint ui Line 2281  String LSCPServer::SetChannelMap(uint ui
2281      dmsg(2,("LSCPServer: SetChannelMap()\n"));      dmsg(2,("LSCPServer: SetChannelMap()\n"));
2282      LSCPResultSet result;      LSCPResultSet result;
2283      try {      try {
2284          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
   
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");  
2285    
2286          if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();          if      (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone();
2287          else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();          else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault();
# Line 2098  String LSCPServer::GetFxSendInfo(uint ui Line 2389  String LSCPServer::GetFxSendInfo(uint ui
2389              AudioRouting += ToString(pFxSend->DestinationChannel(chan));              AudioRouting += ToString(pFxSend->DestinationChannel(chan));
2390          }          }
2391    
2392            const String sEffectRouting =
2393                (pFxSend->DestinationEffectChain() >= 0 && pFxSend->DestinationEffectChainPosition() >= 0)
2394                    ? ToString(pFxSend->DestinationEffectChain()) + "," + ToString(pFxSend->DestinationEffectChainPosition())
2395                    : "NONE";
2396    
2397          // success          // success
2398          result.Add("NAME", pFxSend->Name());          result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2399          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2400          result.Add("LEVEL", ToString(pFxSend->Level()));          result.Add("LEVEL", ToString(pFxSend->Level()));
2401          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
2402            result.Add("EFFECT", sEffectRouting);
2403      } catch (Exception e) {      } catch (Exception e) {
2404          result.Error(e);          result.Error(e);
2405      }      }
# Line 2165  String LSCPServer::SetFxSendLevel(uint u Line 2462  String LSCPServer::SetFxSendLevel(uint u
2462      return result.Produce();      return result.Produce();
2463  }  }
2464    
2465    String LSCPServer::SetFxSendEffect(uint uiSamplerChannel, uint FxSendID, int iSendEffectChain, int iEffectChainPosition) {
2466        dmsg(2,("LSCPServer: SetFxSendEffect(%d,%d)\n", iSendEffectChain, iEffectChainPosition));
2467        LSCPResultSet result;
2468        try {
2469            FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2470    
2471            pFxSend->SetDestinationEffect(iSendEffectChain, iEffectChainPosition);
2472            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, uiSamplerChannel, FxSendID));
2473        } catch (Exception e) {
2474            result.Error(e);
2475        }
2476        return result.Produce();
2477    }
2478    
2479    String LSCPServer::GetAvailableEffects() {
2480        dmsg(2,("LSCPServer: GetAvailableEffects()\n"));
2481        LSCPResultSet result;
2482        try {
2483            int n = EffectFactory::AvailableEffectsCount();
2484            result.Add(n);
2485        }
2486        catch (Exception e) {
2487            result.Error(e);
2488        }
2489        return result.Produce();
2490    }
2491    
2492    String LSCPServer::ListAvailableEffects() {
2493        dmsg(2,("LSCPServer: ListAvailableEffects()\n"));
2494        LSCPResultSet result;
2495        String list;
2496        try {
2497            //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
2498            int n = EffectFactory::AvailableEffectsCount();
2499            for (int i = 0; i < n; i++) {
2500                if (i) list += ",";
2501                list += ToString(i);
2502            }
2503        }
2504        catch (Exception e) {
2505            result.Error(e);
2506        }
2507        result.Add(list);
2508        return result.Produce();
2509    }
2510    
2511    String LSCPServer::GetEffectInfo(int iEffectIndex) {
2512        dmsg(2,("LSCPServer: GetEffectInfo(%d)\n", iEffectIndex));
2513        LSCPResultSet result;
2514        try {
2515            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex);
2516            if (!pEffectInfo)
2517                throw Exception("There is no effect with index " + ToString(iEffectIndex));
2518    
2519            // convert the filename into the correct encoding as defined for LSCP
2520            // (especially in terms of special characters -> escape sequences)
2521    #if WIN32
2522            const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2523    #else
2524            // assuming POSIX
2525            const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2526    #endif
2527    
2528            result.Add("SYSTEM", pEffectInfo->EffectSystem());
2529            result.Add("MODULE", dllFileName);
2530            result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2531            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2532        }
2533        catch (Exception e) {
2534            result.Error(e);
2535        }
2536        return result.Produce();    
2537    }
2538    
2539    String LSCPServer::GetEffectInstanceInfo(int iEffectInstance) {
2540        dmsg(2,("LSCPServer: GetEffectInstanceInfo(%d)\n", iEffectInstance));
2541        LSCPResultSet result;
2542        try {
2543            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2544            if (!pEffect)
2545                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2546    
2547            EffectInfo* pEffectInfo = pEffect->GetEffectInfo();
2548    
2549            // convert the filename into the correct encoding as defined for LSCP
2550            // (especially in terms of special characters -> escape sequences)
2551    #if WIN32
2552            const String dllFileName = Path::fromWindows(pEffectInfo->Module()).toLscp();
2553    #else
2554            // assuming POSIX
2555            const String dllFileName = Path::fromPosix(pEffectInfo->Module()).toLscp();
2556    #endif
2557    
2558            result.Add("SYSTEM", pEffectInfo->EffectSystem());
2559            result.Add("MODULE", dllFileName);
2560            result.Add("NAME", _escapeLscpResponse(pEffectInfo->Name()));
2561            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectInfo->Description()));
2562            result.Add("INPUT_CONTROLS", ToString(pEffect->InputControlCount()));
2563        }
2564        catch (Exception e) {
2565            result.Error(e);
2566        }
2567        return result.Produce();
2568    }
2569    
2570    String LSCPServer::GetEffectInstanceInputControlInfo(int iEffectInstance, int iInputControlIndex) {
2571        dmsg(2,("LSCPServer: GetEffectInstanceInputControlInfo(%d,%d)\n", iEffectInstance, iInputControlIndex));
2572        LSCPResultSet result;
2573        try {
2574            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2575            if (!pEffect)
2576                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2577    
2578            EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2579            if (!pEffectControl)
2580                throw Exception(
2581                    "Effect instance " + ToString(iEffectInstance) +
2582                    " does not have an input control with index " +
2583                    ToString(iInputControlIndex)
2584                );
2585    
2586            result.Add("DESCRIPTION", _escapeLscpResponse(pEffectControl->Description()));
2587            result.Add("VALUE", pEffectControl->Value());
2588            if (pEffectControl->MinValue())
2589                 result.Add("RANGE_MIN", *pEffectControl->MinValue());
2590            if (pEffectControl->MaxValue())
2591                 result.Add("RANGE_MAX", *pEffectControl->MaxValue());
2592            if (!pEffectControl->Possibilities().empty())
2593                 result.Add("POSSIBILITIES", pEffectControl->Possibilities());
2594            if (pEffectControl->DefaultValue())
2595                 result.Add("DEFAULT", *pEffectControl->DefaultValue());
2596        } catch (Exception e) {
2597            result.Error(e);
2598        }
2599        return result.Produce();
2600    }
2601    
2602    String LSCPServer::SetEffectInstanceInputControlValue(int iEffectInstance, int iInputControlIndex, double dValue) {
2603        dmsg(2,("LSCPServer: SetEffectInstanceInputControlValue(%d,%d,%f)\n", iEffectInstance, iInputControlIndex, dValue));
2604        LSCPResultSet result;
2605        try {
2606            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2607            if (!pEffect)
2608                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2609    
2610            EffectControl* pEffectControl = pEffect->InputControl(iInputControlIndex);
2611            if (!pEffectControl)
2612                throw Exception(
2613                    "Effect instance " + ToString(iEffectInstance) +
2614                    " does not have an input control with index " +
2615                    ToString(iInputControlIndex)
2616                );
2617    
2618            pEffectControl->SetValue(dValue);
2619            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_info, iEffectInstance));
2620        } catch (Exception e) {
2621            result.Error(e);
2622        }
2623        return result.Produce();
2624    }
2625    
2626    String LSCPServer::CreateEffectInstance(int iEffectIndex) {
2627        dmsg(2,("LSCPServer: CreateEffectInstance(%d)\n", iEffectIndex));
2628        LSCPResultSet result;
2629        try {
2630            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(iEffectIndex);
2631            if (!pEffectInfo)
2632                throw Exception("There is no effect with index " + ToString(iEffectIndex));
2633            Effect* pEffect = EffectFactory::Create(pEffectInfo);
2634            result = pEffect->ID(); // success
2635            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount()));
2636        } catch (Exception e) {
2637            result.Error(e);
2638        }
2639        return result.Produce();
2640    }
2641    
2642    String LSCPServer::CreateEffectInstance(String effectSystem, String module, String effectName) {
2643        dmsg(2,("LSCPServer: CreateEffectInstance('%s','%s','%s')\n", effectSystem.c_str(), module.c_str(), effectName.c_str()));
2644        LSCPResultSet result;
2645        try {
2646            // to allow loading the same LSCP session file on different systems
2647            // successfully, probably with different effect plugin DLL paths or even
2648            // running completely different operating systems, we do the following
2649            // for finding the right effect:
2650            //
2651            // first try to search for an exact match of the effect plugin DLL
2652            // (a.k.a 'module'), to avoid picking the wrong DLL with the same
2653            // effect name ...
2654            EffectInfo* pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_MATCH_EXACTLY);
2655            // ... if no effect with exactly matchin DLL filename was found, then
2656            // try to lower the restrictions of matching the effect plugin DLL
2657            // filename and try again and again ...
2658            if (!pEffectInfo) {
2659                dmsg(2,("no exact module match, trying MODULE_IGNORE_PATH\n"));
2660                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH);
2661            }
2662            if (!pEffectInfo) {
2663                dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE\n"));
2664                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE);
2665            }
2666            if (!pEffectInfo) {
2667                dmsg(2,("no module match, trying MODULE_IGNORE_PATH | MODULE_IGNORE_CASE | MODULE_IGNORE_EXTENSION\n"));
2668                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_PATH | EffectFactory::MODULE_IGNORE_CASE | EffectFactory::MODULE_IGNORE_EXTENSION);
2669            }
2670            // ... if there was still no effect found, then completely ignore the
2671            // DLL plugin filename argument and just search for the matching effect
2672            // system type and effect name
2673            if (!pEffectInfo) {
2674                dmsg(2,("no module match, trying MODULE_IGNORE_ALL\n"));
2675                pEffectInfo = EffectFactory::GetEffectInfo(effectSystem, module, effectName, EffectFactory::MODULE_IGNORE_ALL);
2676            }
2677            if (!pEffectInfo)
2678                throw Exception("There is no such effect '" + effectSystem + "' '" + module + "' '" + effectName + "'");
2679    
2680            Effect* pEffect = EffectFactory::Create(pEffectInfo);
2681            result = LSCPResultSet(pEffect->ID());
2682            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount()));
2683        } catch (Exception e) {
2684            result.Error(e);
2685        }
2686        return result.Produce();
2687    }
2688    
2689    String LSCPServer::DestroyEffectInstance(int iEffectInstance) {
2690        dmsg(2,("LSCPServer: DestroyEffectInstance(%d)\n", iEffectInstance));
2691        LSCPResultSet result;
2692        try {
2693            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2694            if (!pEffect)
2695                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2696            EffectFactory::Destroy(pEffect);
2697            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_instance_count, EffectFactory::EffectInstancesCount()));
2698        } catch (Exception e) {
2699            result.Error(e);
2700        }
2701        return result.Produce();
2702    }
2703    
2704    String LSCPServer::GetEffectInstances() {
2705        dmsg(2,("LSCPServer: GetEffectInstances()\n"));
2706        LSCPResultSet result;
2707        try {
2708            int n = EffectFactory::EffectInstancesCount();
2709            result.Add(n);
2710        } catch (Exception e) {
2711            result.Error(e);
2712        }
2713        return result.Produce();
2714    }
2715    
2716    String LSCPServer::ListEffectInstances() {
2717        dmsg(2,("LSCPServer: ListEffectInstances()\n"));
2718        LSCPResultSet result;
2719        String list;
2720        try {
2721            int n = EffectFactory::EffectInstancesCount();
2722            for (int i = 0; i < n; i++) {
2723                Effect* pEffect = EffectFactory::GetEffectInstance(i);
2724                if (i) list += ",";
2725                list += ToString(pEffect->ID());
2726            }
2727        } catch (Exception e) {
2728            result.Error(e);
2729        }
2730        result.Add(list);
2731        return result.Produce();
2732    }
2733    
2734    String LSCPServer::GetSendEffectChains(int iAudioOutputDevice) {
2735        dmsg(2,("LSCPServer: GetSendEffectChains(%d)\n", iAudioOutputDevice));
2736        LSCPResultSet result;
2737        try {
2738            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2739            if (!devices.count(iAudioOutputDevice))
2740                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2741            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2742            int n = pDevice->SendEffectChainCount();
2743            result.Add(n);
2744        } catch (Exception e) {
2745            result.Error(e);
2746        }
2747        return result.Produce();
2748    }
2749    
2750    String LSCPServer::ListSendEffectChains(int iAudioOutputDevice) {
2751        dmsg(2,("LSCPServer: ListSendEffectChains(%d)\n", iAudioOutputDevice));
2752        LSCPResultSet result;
2753        String list;
2754        try {
2755            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2756            if (!devices.count(iAudioOutputDevice))
2757                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2758            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2759            int n = pDevice->SendEffectChainCount();
2760            for (int i = 0; i < n; i++) {
2761                EffectChain* pEffectChain = pDevice->SendEffectChain(i);
2762                if (i) list += ",";
2763                list += ToString(pEffectChain->ID());
2764            }
2765        } catch (Exception e) {
2766            result.Error(e);
2767        }
2768        result.Add(list);
2769        return result.Produce();
2770    }
2771    
2772    String LSCPServer::AddSendEffectChain(int iAudioOutputDevice) {
2773        dmsg(2,("LSCPServer: AddSendEffectChain(%d)\n", iAudioOutputDevice));
2774        LSCPResultSet result;
2775        try {
2776            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2777            if (!devices.count(iAudioOutputDevice))
2778                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2779            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2780            EffectChain* pEffectChain = pDevice->AddSendEffectChain();
2781            result = pEffectChain->ID();
2782            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_count, iAudioOutputDevice, pDevice->SendEffectChainCount()));
2783        } catch (Exception e) {
2784            result.Error(e);
2785        }
2786        return result.Produce();
2787    }
2788    
2789    String LSCPServer::RemoveSendEffectChain(int iAudioOutputDevice, int iSendEffectChain) {
2790        dmsg(2,("LSCPServer: RemoveSendEffectChain(%d,%d)\n", iAudioOutputDevice, iSendEffectChain));
2791        LSCPResultSet result;
2792        try {
2793            std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2794            if (!devices.count(iAudioOutputDevice))
2795                throw Exception("There is no audio output device with index " + ToString(iAudioOutputDevice) + ".");
2796    
2797            std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
2798            std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
2799            std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
2800            for (; itEngineChannel != itEnd; ++itEngineChannel) {
2801                AudioOutputDevice* pDev = (*itEngineChannel)->GetAudioOutputDevice();
2802                if (pDev != NULL && pDev->deviceId() == iAudioOutputDevice) {
2803                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
2804                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
2805                        if(fxs != NULL && fxs->DestinationEffectChain() == iSendEffectChain) {
2806                            throw Exception("The effect chain is still in use by channel " + ToString((*itEngineChannel)->GetSamplerChannel()->Index()));
2807                        }
2808                    }
2809                }
2810            }
2811    
2812            AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2813            for (int i = 0; i < pDevice->SendEffectChainCount(); i++) {
2814                EffectChain* pEffectChain = pDevice->SendEffectChain(i);
2815                if (pEffectChain->ID() == iSendEffectChain) {
2816                    pDevice->RemoveSendEffectChain(i);
2817                    LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_count, iAudioOutputDevice, pDevice->SendEffectChainCount()));
2818                    return result.Produce();
2819                }
2820            }
2821            throw Exception(
2822                "There is no send effect chain with ID " +
2823                ToString(iSendEffectChain) + " for audio output device " +
2824                ToString(iAudioOutputDevice) + "."
2825            );
2826        } catch (Exception e) {
2827            result.Error(e);
2828        }
2829        return result.Produce();
2830    }
2831    
2832    static EffectChain* _getSendEffectChain(Sampler* pSampler, int iAudioOutputDevice, int iSendEffectChain) throw (Exception) {
2833        std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
2834        if (!devices.count(iAudioOutputDevice))
2835            throw Exception(
2836                "There is no audio output device with index " +
2837                ToString(iAudioOutputDevice) + "."
2838            );
2839        AudioOutputDevice* pDevice = devices[iAudioOutputDevice];
2840        EffectChain* pEffectChain = pDevice->SendEffectChainByID(iSendEffectChain);
2841        if(pEffectChain != NULL) return pEffectChain;
2842        throw Exception(
2843            "There is no send effect chain with ID " +
2844            ToString(iSendEffectChain) + " for audio output device " +
2845            ToString(iAudioOutputDevice) + "."
2846        );
2847    }
2848    
2849    String LSCPServer::GetSendEffectChainInfo(int iAudioOutputDevice, int iSendEffectChain) {
2850        dmsg(2,("LSCPServer: GetSendEffectChainInfo(%d,%d)\n", iAudioOutputDevice, iSendEffectChain));
2851        LSCPResultSet result;
2852        try {
2853            EffectChain* pEffectChain =
2854                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2855            String sEffectSequence;
2856            for (int i = 0; i < pEffectChain->EffectCount(); i++) {
2857                if (i) sEffectSequence += ",";
2858                sEffectSequence += ToString(pEffectChain->GetEffect(i)->ID());
2859            }
2860            result.Add("EFFECT_COUNT", pEffectChain->EffectCount());
2861            result.Add("EFFECT_SEQUENCE", sEffectSequence);
2862        } catch (Exception e) {
2863            result.Error(e);
2864        }
2865        return result.Produce();
2866    }
2867    
2868    String LSCPServer::AppendSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectInstance) {
2869        dmsg(2,("LSCPServer: AppendSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectInstance));
2870        LSCPResultSet result;
2871        try {
2872            EffectChain* pEffectChain =
2873                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2874            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2875            if (!pEffect)
2876                throw Exception("There is no effect instance with ID " + ToString(iEffectInstance));
2877            pEffectChain->AppendEffect(pEffect);
2878            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount()));
2879        } catch (Exception e) {
2880            result.Error(e);
2881        }
2882        return result.Produce();
2883    }
2884    
2885    String LSCPServer::InsertSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition, int iEffectInstance) {
2886        dmsg(2,("LSCPServer: InsertSendEffectChainEffect(%d,%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition, iEffectInstance));
2887        LSCPResultSet result;
2888        try {
2889            EffectChain* pEffectChain =
2890                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2891            Effect* pEffect = EffectFactory::GetEffectInstanceByID(iEffectInstance);
2892            if (!pEffect)
2893                throw Exception("There is no effect instance with index " + ToString(iEffectInstance));
2894            pEffectChain->InsertEffect(pEffect, iEffectChainPosition);
2895            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount()));
2896        } catch (Exception e) {
2897            result.Error(e);
2898        }
2899        return result.Produce();
2900    }
2901    
2902    String LSCPServer::RemoveSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition) {
2903        dmsg(2,("LSCPServer: RemoveSendEffectChainEffect(%d,%d,%d)\n", iAudioOutputDevice, iSendEffectChain, iEffectChainPosition));
2904        LSCPResultSet result;
2905        try {
2906            EffectChain* pEffectChain =
2907                _getSendEffectChain(pSampler, iAudioOutputDevice, iSendEffectChain);
2908    
2909            std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
2910            std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
2911            std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
2912            for (; itEngineChannel != itEnd; ++itEngineChannel) {
2913                AudioOutputDevice* pDev = (*itEngineChannel)->GetAudioOutputDevice();
2914                if (pDev != NULL && pDev->deviceId() == iAudioOutputDevice) {
2915                    for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) {
2916                        FxSend* fxs = (*itEngineChannel)->GetFxSend(i);
2917                        if(fxs != NULL && fxs->DestinationEffectChain() == iSendEffectChain && fxs->DestinationEffectChainPosition() == iEffectChainPosition) {
2918                            throw Exception("The effect instance is still in use by channel " + ToString((*itEngineChannel)->GetSamplerChannel()->Index()));
2919                        }
2920                    }
2921                }
2922            }
2923    
2924            pEffectChain->RemoveEffect(iEffectChainPosition);
2925            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_send_fx_chain_info, iAudioOutputDevice, iSendEffectChain, pEffectChain->EffectCount()));
2926        } catch (Exception e) {
2927            result.Error(e);
2928        }
2929        return result.Produce();
2930    }
2931    
2932  String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {  String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2933      dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2934      LSCPResultSet result;      LSCPResultSet result;
2935      try {      try {
2936          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
2937          if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");          if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2938          Engine* pEngine = pEngineChannel->GetEngine();          Engine* pEngine = pEngineChannel->GetEngine();
2939          InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();          InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
# Line 2187  String LSCPServer::EditSamplerChannelIns Line 2948  String LSCPServer::EditSamplerChannelIns
2948      return result.Produce();      return result.Produce();
2949  }  }
2950    
2951    String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) {
2952        dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2));
2953        LSCPResultSet result;
2954        try {
2955            EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2956    
2957            if (Arg1 > 127 || Arg2 > 127) {
2958                throw Exception("Invalid MIDI message");
2959            }
2960    
2961            VirtualMidiDevice* pMidiDevice = NULL;
2962            std::vector<EventHandler::midi_listener_entry>::iterator iter = eventHandler.channelMidiListeners.begin();
2963            for (; iter != eventHandler.channelMidiListeners.end(); ++iter) {
2964                if ((*iter).pEngineChannel == pEngineChannel) {
2965                    pMidiDevice = (*iter).pMidiListener;
2966                    break;
2967                }
2968            }
2969            
2970            if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device");
2971    
2972            if (MidiMsg == "NOTE_ON") {
2973                pMidiDevice->SendNoteOnToDevice(Arg1, Arg2);
2974                bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2);
2975                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2976            } else if (MidiMsg == "NOTE_OFF") {
2977                pMidiDevice->SendNoteOffToDevice(Arg1, Arg2);
2978                bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2);
2979                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2980            } else if (MidiMsg == "CC") {
2981                pMidiDevice->SendCCToDevice(Arg1, Arg2);
2982                bool b = pMidiDevice->SendCCToSampler(Arg1, Arg2);
2983                if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2));
2984            } else {
2985                throw Exception("Unknown MIDI message type: " + MidiMsg);
2986            }
2987        } catch (Exception e) {
2988            result.Error(e);
2989        }
2990        return result.Produce();
2991    }
2992    
2993  /**  /**
2994   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
2995   */   */
# Line 2194  String LSCPServer::ResetChannel(uint uiS Line 2997  String LSCPServer::ResetChannel(uint uiS
2997      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
2998      LSCPResultSet result;      LSCPResultSet result;
2999      try {      try {
3000          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
         if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));  
         EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();  
         if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");  
3001          pEngineChannel->Reset();          pEngineChannel->Reset();
3002      }      }
3003      catch (Exception e) {      catch (Exception e) {
# Line 2222  String LSCPServer::ResetSampler() { Line 3022  String LSCPServer::ResetSampler() {
3022   */   */
3023  String LSCPServer::GetServerInfo() {  String LSCPServer::GetServerInfo() {
3024      dmsg(2,("LSCPServer: GetServerInfo()\n"));      dmsg(2,("LSCPServer: GetServerInfo()\n"));
3025        const std::string description =
3026            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
3027      LSCPResultSet result;      LSCPResultSet result;
3028      result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");      result.Add("DESCRIPTION", description);
3029      result.Add("VERSION", VERSION);      result.Add("VERSION", VERSION);
3030      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
3031  #if HAVE_SQLITE3  #if HAVE_SQLITE3
# Line 2236  String LSCPServer::GetServerInfo() { Line 3038  String LSCPServer::GetServerInfo() {
3038  }  }
3039    
3040  /**  /**
3041     * Will be called by the parser to return the current number of all active streams.
3042     */
3043    String LSCPServer::GetTotalStreamCount() {
3044        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
3045        LSCPResultSet result;
3046        result.Add(pSampler->GetDiskStreamCount());
3047        return result.Produce();
3048    }
3049    
3050    /**
3051   * Will be called by the parser to return the current number of all active voices.   * Will be called by the parser to return the current number of all active voices.
3052   */   */
3053  String LSCPServer::GetTotalVoiceCount() {  String LSCPServer::GetTotalVoiceCount() {
# Line 2251  String LSCPServer::GetTotalVoiceCount() Line 3063  String LSCPServer::GetTotalVoiceCount()
3063  String LSCPServer::GetTotalVoiceCountMax() {  String LSCPServer::GetTotalVoiceCountMax() {
3064      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));      dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n"));
3065      LSCPResultSet result;      LSCPResultSet result;
3066      result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES);      result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES);
3067        return result.Produce();
3068    }
3069    
3070    /**
3071     * Will be called by the parser to return the sampler global maximum
3072     * allowed number of voices.
3073     */
3074    String LSCPServer::GetGlobalMaxVoices() {
3075        dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n"));
3076        LSCPResultSet result;
3077        result.Add(GLOBAL_MAX_VOICES);
3078        return result.Produce();
3079    }
3080    
3081    /**
3082     * Will be called by the parser to set the sampler global maximum number of
3083     * voices.
3084     */
3085    String LSCPServer::SetGlobalMaxVoices(int iVoices) {
3086        dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices));
3087        LSCPResultSet result;
3088        try {
3089            if (iVoices < 1) throw Exception("Maximum voices may not be less than 1");
3090            GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp
3091            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
3092            if (engines.size() > 0) {
3093                std::set<Engine*>::iterator iter = engines.begin();
3094                std::set<Engine*>::iterator end  = engines.end();
3095                for (; iter != end; ++iter) {
3096                    (*iter)->SetMaxVoices(iVoices);
3097                }
3098            }
3099            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES));
3100        } catch (Exception e) {
3101            result.Error(e);
3102        }
3103        return result.Produce();
3104    }
3105    
3106    /**
3107     * Will be called by the parser to return the sampler global maximum
3108     * allowed number of disk streams.
3109     */
3110    String LSCPServer::GetGlobalMaxStreams() {
3111        dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n"));
3112        LSCPResultSet result;
3113        result.Add(GLOBAL_MAX_STREAMS);
3114        return result.Produce();
3115    }
3116    
3117    /**
3118     * Will be called by the parser to set the sampler global maximum number of
3119     * disk streams.
3120     */
3121    String LSCPServer::SetGlobalMaxStreams(int iStreams) {
3122        dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams));
3123        LSCPResultSet result;
3124        try {
3125            if (iStreams < 0) throw Exception("Maximum disk streams may not be negative");
3126            GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp
3127            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
3128            if (engines.size() > 0) {
3129                std::set<Engine*>::iterator iter = engines.begin();
3130                std::set<Engine*>::iterator end  = engines.end();
3131                for (; iter != end; ++iter) {
3132                    (*iter)->SetMaxDiskStreams(iStreams);
3133                }
3134            }
3135            LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS));
3136        } catch (Exception e) {
3137            result.Error(e);
3138        }
3139      return result.Produce();      return result.Produce();
3140  }  }
3141    
# Line 2265  String LSCPServer::SetGlobalVolume(doubl Line 3149  String LSCPServer::SetGlobalVolume(doubl
3149      LSCPResultSet result;      LSCPResultSet result;
3150      try {      try {
3151          if (dVolume < 0) throw Exception("Volume may not be negative");          if (dVolume < 0) throw Exception("Volume may not be negative");
3152          GLOBAL_VOLUME = dVolume; // see common/global.cpp          GLOBAL_VOLUME = dVolume; // see common/global_private.cpp
3153          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOLUME", GLOBAL_VOLUME));
3154      } catch (Exception e) {      } catch (Exception e) {
3155          result.Error(e);          result.Error(e);
# Line 2273  String LSCPServer::SetGlobalVolume(doubl Line 3157  String LSCPServer::SetGlobalVolume(doubl
3157      return result.Produce();      return result.Produce();
3158  }  }
3159    
3160    String LSCPServer::GetFileInstruments(String Filename) {
3161        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
3162        LSCPResultSet result;
3163        try {
3164            VerifyFile(Filename);
3165        } catch (Exception e) {
3166            result.Error(e);
3167            return result.Produce();
3168        }
3169        // try to find a sampler engine that can handle the file
3170        bool bFound = false;
3171        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3172        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
3173            Engine* pEngine = NULL;
3174            try {
3175                pEngine = EngineFactory::Create(engineTypes[i]);
3176                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3177                InstrumentManager* pManager = pEngine->GetInstrumentManager();
3178                if (pManager) {
3179                    std::vector<InstrumentManager::instrument_id_t> IDs =
3180                        pManager->GetInstrumentFileContent(Filename);
3181                    // return the amount of instruments in the file
3182                    result.Add(IDs.size());
3183                    // no more need to ask other engine types
3184                    bFound = true;
3185                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3186            } catch (Exception e) {
3187                // NOOP, as exception is thrown if engine doesn't support file
3188            }
3189            if (pEngine) EngineFactory::Destroy(pEngine);
3190        }
3191    
3192        if (!bFound) result.Error("Unknown file format");
3193        return result.Produce();
3194    }
3195    
3196    String LSCPServer::ListFileInstruments(String Filename) {
3197        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
3198        LSCPResultSet result;
3199        try {
3200            VerifyFile(Filename);
3201        } catch (Exception e) {
3202            result.Error(e);
3203            return result.Produce();
3204        }
3205        // try to find a sampler engine that can handle the file
3206        bool bFound = false;
3207        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3208        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
3209            Engine* pEngine = NULL;
3210            try {
3211                pEngine = EngineFactory::Create(engineTypes[i]);
3212                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3213                InstrumentManager* pManager = pEngine->GetInstrumentManager();
3214                if (pManager) {
3215                    std::vector<InstrumentManager::instrument_id_t> IDs =
3216                        pManager->GetInstrumentFileContent(Filename);
3217                    // return a list of IDs of the instruments in the file
3218                    String s;
3219                    for (int j = 0; j < IDs.size(); j++) {
3220                        if (s.size()) s += ",";
3221                        s += ToString(IDs[j].Index);
3222                    }
3223                    result.Add(s);
3224                    // no more need to ask other engine types
3225                    bFound = true;
3226                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3227            } catch (Exception e) {
3228                // NOOP, as exception is thrown if engine doesn't support file
3229            }
3230            if (pEngine) EngineFactory::Destroy(pEngine);
3231        }
3232    
3233        if (!bFound) result.Error("Unknown file format");
3234        return result.Produce();
3235    }
3236    
3237    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
3238        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
3239        LSCPResultSet result;
3240        try {
3241            VerifyFile(Filename);
3242        } catch (Exception e) {
3243            result.Error(e);
3244            return result.Produce();
3245        }
3246        InstrumentManager::instrument_id_t id;
3247        id.FileName = Filename;
3248        id.Index    = InstrumentID;
3249        // try to find a sampler engine that can handle the file
3250        bool bFound = false;
3251        bool bFatalErr = false;
3252        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
3253        for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
3254            Engine* pEngine = NULL;
3255            try {
3256                pEngine = EngineFactory::Create(engineTypes[i]);
3257                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
3258                InstrumentManager* pManager = pEngine->GetInstrumentManager();
3259                if (pManager) {
3260                    // check if the instrument index is valid
3261                    // FIXME: this won't work if an engine only supports parts of the instrument file
3262                    std::vector<InstrumentManager::instrument_id_t> IDs =
3263                        pManager->GetInstrumentFileContent(Filename);
3264                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
3265                        std::stringstream ss;
3266                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
3267                        bFatalErr = true;
3268                        throw Exception(ss.str());
3269                    }
3270                    // get the info of the requested instrument
3271                    InstrumentManager::instrument_info_t info =
3272                        pManager->GetInstrumentInfo(id);
3273                    // return detailed informations about the file
3274                    result.Add("NAME", info.InstrumentName);
3275                    result.Add("FORMAT_FAMILY", engineTypes[i]);
3276                    result.Add("FORMAT_VERSION", info.FormatVersion);
3277                    result.Add("PRODUCT", info.Product);
3278                    result.Add("ARTISTS", info.Artists);
3279    
3280                    std::stringstream ss;
3281                    bool b = false;
3282                    for (int i = 0; i < 128; i++) {
3283                        if (info.KeyBindings[i]) {
3284                            if (b) ss << ',';
3285                            ss << i; b = true;
3286                        }
3287                    }
3288                    result.Add("KEY_BINDINGS", ss.str());
3289    
3290                    b = false;
3291                    std::stringstream ss2;
3292                    for (int i = 0; i < 128; i++) {
3293                        if (info.KeySwitchBindings[i]) {
3294                            if (b) ss2 << ',';
3295                            ss2 << i; b = true;
3296                        }
3297                    }
3298                    result.Add("KEYSWITCH_BINDINGS", ss2.str());
3299                    // no more need to ask other engine types
3300                    bFound = true;
3301                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
3302            } catch (Exception e) {
3303                // usually NOOP, as exception is thrown if engine doesn't support file
3304                if (bFatalErr) result.Error(e);
3305            }
3306            if (pEngine) EngineFactory::Destroy(pEngine);
3307        }
3308    
3309        if (!bFound && !bFatalErr) result.Error("Unknown file format");
3310        return result.Produce();
3311    }
3312    
3313    void LSCPServer::VerifyFile(String Filename) {
3314        #if WIN32
3315        WIN32_FIND_DATA win32FileAttributeData;
3316        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
3317        if (!res) {
3318            std::stringstream ss;
3319            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
3320            throw Exception(ss.str());
3321        }
3322        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
3323            throw Exception("Directory is specified");
3324        }
3325        #else
3326        File f(Filename);
3327        if(!f.Exist()) throw Exception(f.GetErrorMsg());
3328        if (f.IsDirectory()) throw Exception("Directory is specified");
3329        #endif
3330    }
3331    
3332  /**  /**
3333   * 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
3334   * server for receiving event messages.   * server for receiving event messages.
# Line 2374  String LSCPServer::GetDbInstrumentDirect Line 3430  String LSCPServer::GetDbInstrumentDirect
3430      try {      try {
3431          DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);          DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
3432    
3433          result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description));          result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3434          result.Add("CREATED", info.Created);          result.Add("CREATED", info.Created);
3435          result.Add("MODIFIED", info.Modified);          result.Add("MODIFIED", info.Modified);
3436      } catch (Exception e) {      } catch (Exception e) {
# Line 2464  String LSCPServer::AddDbInstruments(Stri Line 3520  String LSCPServer::AddDbInstruments(Stri
3520      return result.Produce();      return result.Produce();
3521  }  }
3522    
3523  String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) {  String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) {
3524      dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground));      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));
3525      LSCPResultSet result;      LSCPResultSet result;
3526  #if HAVE_SQLITE3  #if HAVE_SQLITE3
3527      try {      try {
3528          int id;          int id;
3529          InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();          InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
3530          if (ScanMode.compare("RECURSIVE") == 0) {          if (ScanMode.compare("RECURSIVE") == 0) {
3531             id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground);              id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir);
3532          } else if (ScanMode.compare("NON_RECURSIVE") == 0) {          } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
3533             id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground);              id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir);
3534          } else if (ScanMode.compare("FLAT") == 0) {          } else if (ScanMode.compare("FLAT") == 0) {
3535             id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground);              id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir);
3536          } else {          } else {
3537              throw Exception("Unknown scan mode: " + ScanMode);              throw Exception("Unknown scan mode: " + ScanMode);
3538          }          }
# Line 2558  String LSCPServer::GetDbInstrumentInfo(S Line 3614  String LSCPServer::GetDbInstrumentInfo(S
3614          result.Add("SIZE", (int)info.Size);          result.Add("SIZE", (int)info.Size);
3615          result.Add("CREATED", info.Created);          result.Add("CREATED", info.Created);
3616          result.Add("MODIFIED", info.Modified);          result.Add("MODIFIED", info.Modified);
3617          result.Add("DESCRIPTION", InstrumentsDb::toEscapedText(info.Description));          result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
3618          result.Add("IS_DRUM", info.IsDrum);          result.Add("IS_DRUM", info.IsDrum);
3619          result.Add("PRODUCT", InstrumentsDb::toEscapedText(info.Product));          result.Add("PRODUCT", _escapeLscpResponse(info.Product));
3620          result.Add("ARTISTS", InstrumentsDb::toEscapedText(info.Artists));          result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
3621          result.Add("KEYWORDS", InstrumentsDb::toEscapedText(info.Keywords));          result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
3622      } catch (Exception e) {      } catch (Exception e) {
3623           result.Error(e);           result.Error(e);
3624      }      }
# Line 2652  String LSCPServer::SetDbInstrumentDescri Line 3708  String LSCPServer::SetDbInstrumentDescri
3708      return result.Produce();      return result.Produce();
3709  }  }
3710    
3711    String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) {
3712        dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str()));
3713        LSCPResultSet result;
3714    #if HAVE_SQLITE3
3715        try {
3716            InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath);
3717        } catch (Exception e) {
3718             result.Error(e);
3719        }
3720    #else
3721        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3722    #endif
3723        return result.Produce();
3724    }
3725    
3726    String LSCPServer::FindLostDbInstrumentFiles() {
3727        dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n"));
3728        LSCPResultSet result;
3729    #if HAVE_SQLITE3
3730        try {
3731            String list;
3732            StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles();
3733    
3734            for (int i = 0; i < pLostFiles->size(); i++) {
3735                if (list != "") list += ",";
3736                list += "'" + pLostFiles->at(i) + "'";
3737            }
3738    
3739            result.Add(list);
3740        } catch (Exception e) {
3741             result.Error(e);
3742        }
3743    #else
3744        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3745    #endif
3746        return result.Produce();
3747    }
3748    
3749  String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {  String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
3750      dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));      dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
3751      LSCPResultSet result;      LSCPResultSet result;
# Line 2748  String LSCPServer::FindDbInstruments(Str Line 3842  String LSCPServer::FindDbInstruments(Str
3842      return result.Produce();      return result.Produce();
3843  }  }
3844    
3845    String LSCPServer::FormatInstrumentsDb() {
3846        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
3847        LSCPResultSet result;
3848    #if HAVE_SQLITE3
3849        try {
3850            InstrumentsDb::GetInstrumentsDb()->Format();
3851        } catch (Exception e) {
3852             result.Error(e);
3853        }
3854    #else
3855        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
3856    #endif
3857        return result.Produce();
3858    }
3859    
3860    
3861  /**  /**
3862   * 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
# Line 2767  String LSCPServer::SetEcho(yyparse_param Line 3876  String LSCPServer::SetEcho(yyparse_param
3876      }      }
3877      return result.Produce();      return result.Produce();
3878  }  }
3879    
3880    }

Legend:
Removed from v.1350  
changed lines
  Added in v.2324

  ViewVC Help
Powered by ViewVC