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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 527 - (hide annotations) (download)
Mon May 9 11:59:58 2005 UTC (17 years ago) by capela
File size: 63151 byte(s)
* [bug #9] Fixed for a LSCP command syntax convention
  consistency, regarding the enumeration of available
  sampler engines, Audio and MIDI drivers; this has
  affected the semantics of the following commands:
    GET AVAILABLE_ENGINES
    GET AVAILABLE_AUDIO_DRIVERS
    GET AVAILABLE_MIDI_DRIVERS
  which are now returning an integer count of engines
  and drivers, respectively, while the following
  new commands are now being introduced:
    LIST AVAILABLE_ENGINES
    LIST AVAILABLE_AUDIO_DRIVERS
    LIST AVAILABLE_MIDI_DRIVERS
  taking on the previous functionality, returning
  a comma separated list of names.

* LinuxSampler version bumped to 0.3.1.

1 schoenebeck 35 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 385 * Copyright (C) 2005 Christian Schoenebeck *
7 schoenebeck 35 * *
8 schoenebeck 385 * This library is free software; you can redistribute it and/or modify *
9 schoenebeck 35 * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13 schoenebeck 385 * This library is distributed in the hope that it will be useful, *
14 schoenebeck 35 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19 schoenebeck 385 * along with this library; if not, write to the Free Software *
20 schoenebeck 35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #include "lscpserver.h"
25 senkov 113 #include "lscpresultset.h"
26 senkov 170 #include "lscpevent.h"
27 senkov 397 //#include "../common/global.h"
28 schoenebeck 35
29 schoenebeck 411 #include <fcntl.h>
30    
31 schoenebeck 401 #if HAVE_SQLITE3
32     # include "sqlite3.h"
33 senkov 397 #endif
34    
35 schoenebeck 411 #include "../engines/EngineFactory.h"
36 schoenebeck 203 #include "../drivers/audio/AudioOutputDeviceFactory.h"
37     #include "../drivers/midi/MidiInputDeviceFactory.h"
38 schoenebeck 53
39 senkov 170 /**
40     * Below are a few static members of the LSCPServer class.
41     * The big assumption here is that LSCPServer is going to remain a singleton.
42     * These members are used to support client connections.
43     * Class handles multiple connections at the same time using select() and non-blocking recv()
44     * Commands are processed by a single LSCPServer thread.
45     * Notifications are delivered either by the thread that originated them
46     * or (if the resultset is currently in progress) by the LSCPServer thread
47     * after the resultset was sent out.
48     * This makes sure that resultsets can not be interrupted by notifications.
49     * This also makes sure that the thread sending notification is not blocked
50     * by the LSCPServer thread.
51     */
52     fd_set LSCPServer::fdSet;
53     int LSCPServer::currentSocket = -1;
54 schoenebeck 210 std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
55 senkov 170 std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
56     std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
57     std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
58     Mutex LSCPServer::NotifyMutex = Mutex();
59     Mutex LSCPServer::NotifyBufferMutex = Mutex();
60     Mutex LSCPServer::SubscriptionMutex = Mutex();
61 senkov 360 Mutex LSCPServer::RTNotifyMutex = Mutex();
62 senkov 170
63 schoenebeck 392 LSCPServer::LSCPServer(Sampler* pSampler) : Thread(true, false, 0, -4) {
64 schoenebeck 53 this->pSampler = pSampler;
65 senkov 170 LSCPEvent::RegisterEvent(LSCPEvent::event_channels, "CHANNELS");
66     LSCPEvent::RegisterEvent(LSCPEvent::event_voice_count, "VOICE_COUNT");
67     LSCPEvent::RegisterEvent(LSCPEvent::event_stream_count, "STREAM_COUNT");
68     LSCPEvent::RegisterEvent(LSCPEvent::event_buffer_fill, "BUFFER_FILL");
69     LSCPEvent::RegisterEvent(LSCPEvent::event_info, "INFO");
70     LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
71 schoenebeck 475 hSocket = -1;
72 schoenebeck 35 }
73    
74 schoenebeck 475 LSCPServer::~LSCPServer() {
75     if (hSocket >= 0) close(hSocket);
76     }
77    
78 schoenebeck 211 /**
79     * Blocks the calling thread until the LSCP Server is initialized and
80     * accepting socket connections, if the server is already initialized then
81     * this method will return immediately.
82     * @param TimeoutSeconds - optional: max. wait time in seconds
83     * (default: 0s)
84     * @param TimeoutNanoSeconds - optional: max wait time in nano seconds
85     * (default: 0ns)
86     * @returns 0 on success, a value less than 0 if timeout exceeded
87     */
88     int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) {
89     return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds);
90     }
91    
92 schoenebeck 35 int LSCPServer::Main() {
93 schoenebeck 475 hSocket = socket(AF_INET, SOCK_STREAM, 0);
94 schoenebeck 35 if (hSocket < 0) {
95     std::cerr << "LSCPServer: Could not create server socket." << std::endl;
96 schoenebeck 53 //return -1;
97     exit(EXIT_FAILURE);
98 schoenebeck 35 }
99    
100     SocketAddress.sin_family = AF_INET;
101     SocketAddress.sin_port = htons(LSCP_PORT);
102     SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
103    
104     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
105 schoenebeck 227 std::cerr << "LSCPServer: Could not bind server socket, retrying for " << ToString(LSCP_SERVER_BIND_TIMEOUT) << " seconds...";
106     for (int trial = 0; true; trial++) { // retry for LSCP_SERVER_BIND_TIMEOUT seconds
107     if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
108     if (trial > LSCP_SERVER_BIND_TIMEOUT) {
109     std::cerr << "gave up!" << std::endl;
110     close(hSocket);
111     //return -1;
112     exit(EXIT_FAILURE);
113     }
114     else sleep(1); // sleep 1s
115     }
116     else break; // success
117     }
118 schoenebeck 35 }
119    
120     listen(hSocket, 1);
121 schoenebeck 211 Initialized.Set(true);
122 schoenebeck 35
123     // now wait for client connections and handle their requests
124     sockaddr_in client;
125     int length = sizeof(client);
126 senkov 170 FD_ZERO(&fdSet);
127     FD_SET(hSocket, &fdSet);
128     int maxSessions = hSocket;
129 schoenebeck 203
130 schoenebeck 35 while (true) {
131 senkov 170 fd_set selectSet = fdSet;
132 senkov 198 int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL);
133 senkov 170 if (retval == 0)
134 senkov 198 continue; //Nothing try again
135 senkov 170 if (retval == -1) {
136     std::cerr << "LSCPServer: Socket select error." << std::endl;
137     close(hSocket);
138     exit(EXIT_FAILURE);
139     }
140 schoenebeck 203
141 senkov 170 //Accept new connections now (if any)
142     if (FD_ISSET(hSocket, &selectSet)) {
143     int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
144     if (socket < 0) {
145     std::cerr << "LSCPServer: Client connection failed." << std::endl;
146     exit(EXIT_FAILURE);
147     }
148 schoenebeck 35
149 senkov 170 if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
150     std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
151     exit(EXIT_FAILURE);
152     }
153 schoenebeck 35
154 schoenebeck 210 // Parser initialization
155     yyparse_param_t yyparse_param;
156     yyparse_param.pServer = this;
157     yyparse_param.hSession = socket;
158    
159     Sessions.push_back(yyparse_param);
160 senkov 170 FD_SET(socket, &fdSet);
161     if (socket > maxSessions)
162     maxSessions = socket;
163     dmsg(1,("LSCPServer: Client connection established on socket:%d.\n", socket));
164     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection established on socket", socket));
165     continue; //Maybe this was the only selected socket, better select again
166     }
167 schoenebeck 35
168 senkov 170 //Something was selected and it was not the hSocket, so it must be some command(s) coming.
169 schoenebeck 210 for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) {
170     if (FD_ISSET((*iter).hSession, &selectSet)) { //Was it this socket?
171 senkov 170 if (GetLSCPCommand(iter)) { //Have we read the entire command?
172     dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket));
173 schoenebeck 219 int dummy; // just a temporary hack to fulfill the restart() function prototype
174     restart(NULL, dummy); // restart the 'scanner'
175 schoenebeck 210 currentSocket = (*iter).hSession; //a hack
176 schoenebeck 475 dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
177 schoenebeck 210 if ((*iter).bVerbose) { // if echo mode enabled
178     AnswerClient(bufferedCommands[currentSocket]);
179     }
180     int result = yyparse(&(*iter));
181 senkov 170 currentSocket = -1; //continuation of a hack
182     dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
183     if (result == LSCP_QUIT) { //Was it a quit command by any chance?
184     CloseConnection(iter);
185     }
186     }
187     //socket may have been closed, iter may be invalid, get out of the loop for now.
188     //we'll be back if there is data.
189 schoenebeck 203 break;
190 senkov 170 }
191     }
192    
193     //Now let's deliver late notifies (if any)
194     NotifyBufferMutex.Lock();
195     for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
196     send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
197     bufferedNotifies.erase(iterNotify);
198     }
199     NotifyBufferMutex.Unlock();
200 schoenebeck 35 }
201     }
202    
203 schoenebeck 210 void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) {
204     int socket = (*iter).hSession;
205 senkov 170 dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket));
206     LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
207 schoenebeck 210 Sessions.erase(iter);
208 senkov 170 FD_CLR(socket, &fdSet);
209     SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)
210     for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
211     iter->second.remove(socket);
212     }
213     SubscriptionMutex.Unlock();
214     NotifyMutex.Lock();
215     bufferedCommands.erase(socket);
216     bufferedNotifies.erase(socket);
217     close(socket);
218     NotifyMutex.Unlock();
219     }
220    
221 senkov 360 int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
222     int subs = 0;
223     SubscriptionMutex.Lock();
224     for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
225     iter != events.end(); iter++)
226     {
227     subs += eventSubscriptions.count(*iter);
228     }
229     SubscriptionMutex.Unlock();
230     return subs;
231     }
232    
233 senkov 170 void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
234     SubscriptionMutex.Lock();
235     if (eventSubscriptions.count(event.GetType()) == 0) {
236     SubscriptionMutex.Unlock(); //Nobody is subscribed to this event
237     return;
238     }
239     std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
240     std::list<int>::iterator end = eventSubscriptions[event.GetType()].end();
241     String notify = event.Produce();
242    
243     while (true) {
244     if (NotifyMutex.Trylock()) {
245     for(;iter != end; iter++)
246     send(*iter, notify.c_str(), notify.size(), 0);
247     NotifyMutex.Unlock();
248     break;
249     } else {
250     if (NotifyBufferMutex.Trylock()) {
251     for(;iter != end; iter++)
252     bufferedNotifies[*iter] += notify;
253     NotifyBufferMutex.Unlock();
254     break;
255     }
256     }
257     }
258     SubscriptionMutex.Unlock();
259     }
260    
261     extern int GetLSCPCommand( void *buf, int max_size ) {
262     String command = LSCPServer::bufferedCommands[LSCPServer::currentSocket];
263     if (command.size() == 0) { //Parser wants input but we have nothing.
264     strcpy((char*) buf, "\n"); //So give it an empty command
265     return 1; //to keep it happy.
266     }
267    
268     if (max_size < command.size()) {
269     std::cerr << "getLSCPCommand: Flex buffer too small, ignoring the command." << std::endl;
270     return 0; //This will never happen
271     }
272    
273     strcpy((char*) buf, command.c_str());
274     LSCPServer::bufferedCommands.erase(LSCPServer::currentSocket);
275     return command.size();
276     }
277    
278 schoenebeck 35 /**
279 senkov 170 * Will be called to try to read the command from the socket
280     * If command is read, it will return true. Otherwise false is returned.
281     * In any case the received portion (complete or incomplete) is saved into bufferedCommand map.
282     */
283 schoenebeck 210 bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) {
284     int socket = (*iter).hSession;
285 senkov 170 char c;
286     int i = 0;
287     while (true) {
288     int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
289     if (result == 0) { //socket was selected, so 0 here means client has closed the connection
290     CloseConnection(iter);
291     break;
292     }
293     if (result == 1) {
294 schoenebeck 203 if (c == '\r')
295 senkov 170 continue; //Ignore CR
296     if (c == '\n') {
297 senkov 184 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket));
298 senkov 170 bufferedCommands[socket] += "\n";
299     return true; //Complete command was read
300     }
301     bufferedCommands[socket] += c;
302     }
303     if (result == -1) {
304     if (errno == EAGAIN) //Would block, try again later.
305     return false;
306     switch(errno) {
307     case EBADF:
308     dmsg(2,("LSCPScanner: The argument s is an invalid descriptor.\n"));
309     break;
310     case ECONNREFUSED:
311     dmsg(2,("LSCPScanner: A remote host refused to allow the network connection (typically because it is not running the requested service).\n"));
312     break;
313     case ENOTCONN:
314     dmsg(2,("LSCPScanner: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).\n"));
315     break;
316     case ENOTSOCK:
317     dmsg(2,("LSCPScanner: The argument s does not refer to a socket.\n"));
318     break;
319     case EAGAIN:
320     dmsg(2,("LSCPScanner: The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received.\n"));
321 schoenebeck 203 break;
322     case EINTR:
323 senkov 170 dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n"));
324 schoenebeck 203 break;
325     case EFAULT:
326     dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n"));
327     break;
328     case EINVAL:
329     dmsg(2,("LSCPScanner: Invalid argument passed.\n"));
330     break;
331     case ENOMEM:
332     dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n"));
333     break;
334     default:
335     dmsg(2,("LSCPScanner: Unknown recv() error.\n"));
336     break;
337     }
338 senkov 170 CloseConnection(iter);
339     break;
340     }
341     }
342     return false;
343     }
344    
345     /**
346 schoenebeck 35 * Will be called by the parser whenever it wants to send an answer to the
347     * client / frontend.
348     *
349     * @param ReturnMessage - message that will be send to the client
350     */
351     void LSCPServer::AnswerClient(String ReturnMessage) {
352     dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
353 senkov 170 if (currentSocket != -1) {
354     NotifyMutex.Lock();
355     send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
356     NotifyMutex.Unlock();
357     }
358 schoenebeck 35 }
359    
360 capela 143 /**
361     * Find a created audio output device index.
362     */
363     int LSCPServer::GetAudioOutputDeviceIndex ( AudioOutputDevice *pDevice )
364     {
365     // Search for the created device to get its index
366     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
367     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
368     for (; iter != devices.end(); iter++) {
369     if (iter->second == pDevice)
370     return iter->first;
371     }
372     // Not found.
373     return -1;
374     }
375    
376 senkov 155 /**
377     * Find a created midi input device index.
378     */
379     int LSCPServer::GetMidiInputDeviceIndex ( MidiInputDevice *pDevice )
380     {
381     // Search for the created device to get its index
382     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
383     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
384     for (; iter != devices.end(); iter++) {
385     if (iter->second == pDevice)
386     return iter->first;
387     }
388     // Not found.
389     return -1;
390     }
391    
392 schoenebeck 123 String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
393     dmsg(2,("LSCPServer: CreateAudioOutputDevice(Driver=%s)\n", Driver.c_str()));
394     LSCPResultSet result;
395     try {
396     AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
397     // search for the created device to get its index
398 capela 143 int index = GetAudioOutputDeviceIndex(pDevice);
399 schoenebeck 123 if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");
400     result = index; // success
401     }
402     catch (LinuxSamplerException e) {
403     result.Error(e);
404     }
405     return result.Produce();
406     }
407    
408 senkov 155 String LSCPServer::CreateMidiInputDevice(String Driver, std::map<String,String> Parameters) {
409     dmsg(2,("LSCPServer: CreateMidiInputDevice(Driver=%s)\n", Driver.c_str()));
410     LSCPResultSet result;
411     try {
412     MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
413     // search for the created device to get its index
414     int index = GetMidiInputDeviceIndex(pDevice);
415     if (index == -1) throw LinuxSamplerException("Internal error: could not find created midi input device.");
416     result = index; // success
417     }
418     catch (LinuxSamplerException e) {
419     result.Error(e);
420     }
421     return result.Produce();
422     }
423    
424 schoenebeck 123 String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
425     dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
426     LSCPResultSet result;
427     try {
428     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
429 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
430 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
431     pSampler->DestroyAudioOutputDevice(pDevice);
432     }
433     catch (LinuxSamplerException e) {
434     result.Error(e);
435     }
436     return result.Produce();
437     }
438    
439 senkov 155 String LSCPServer::DestroyMidiInputDevice(uint DeviceIndex) {
440     dmsg(2,("LSCPServer: DestroyMidiInputDevice(DeviceIndex=%d)\n", DeviceIndex));
441     LSCPResultSet result;
442     try {
443     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
444 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
445 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
446     pSampler->DestroyMidiInputDevice(pDevice);
447     }
448     catch (LinuxSamplerException e) {
449     result.Error(e);
450     }
451     return result.Produce();
452     }
453    
454 schoenebeck 35 /**
455     * Will be called by the parser to load an instrument.
456     */
457 capela 137 String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground) {
458 schoenebeck 53 dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));
459 senkov 120 LSCPResultSet result;
460 schoenebeck 53 try {
461     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
462 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
463 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
464     if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel yet");
465 schoenebeck 223 if (!pSamplerChannel->GetAudioOutputDevice())
466 schoenebeck 374 throw LinuxSamplerException("No audio output device connected to sampler channel");
467 capela 137 if (bBackground) {
468 schoenebeck 411 InstrumentLoader.StartNewLoad(Filename, uiInstrument, pEngineChannel);
469 capela 137 }
470 schoenebeck 392 else {
471 schoenebeck 411 // tell the engine channel which instrument to load
472     pEngineChannel->PrepareLoadInstrument(Filename.c_str(), uiInstrument);
473 schoenebeck 392 // actually start to load the instrument (blocks until completed)
474 schoenebeck 411 pEngineChannel->LoadInstrument();
475 schoenebeck 392 }
476 schoenebeck 53 }
477     catch (LinuxSamplerException e) {
478 senkov 120 result.Error(e);
479 schoenebeck 53 }
480 senkov 120 return result.Produce();
481 schoenebeck 35 }
482    
483     /**
484 schoenebeck 411 * Will be called by the parser to assign a sampler engine type to a
485     * sampler channel.
486 schoenebeck 35 */
487 schoenebeck 411 String LSCPServer::SetEngineType(String EngineName, uint uiSamplerChannel) {
488 schoenebeck 53 dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
489 senkov 120 LSCPResultSet result;
490 schoenebeck 475 try {
491 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
492 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
493 senkov 360 LockRTNotify();
494 schoenebeck 411 pSamplerChannel->SetEngineType(EngineName);
495 senkov 360 UnlockRTNotify();
496 schoenebeck 53 }
497     catch (LinuxSamplerException e) {
498 senkov 120 result.Error(e);
499 schoenebeck 53 }
500 senkov 120 return result.Produce();
501 schoenebeck 35 }
502    
503     /**
504     * Will be called by the parser to get the amount of sampler channels.
505     */
506     String LSCPServer::GetChannels() {
507     dmsg(2,("LSCPServer: GetChannels()\n"));
508 senkov 120 LSCPResultSet result;
509     result.Add(pSampler->SamplerChannels());
510     return result.Produce();
511 schoenebeck 35 }
512    
513     /**
514 schoenebeck 209 * Will be called by the parser to get the list of sampler channels.
515     */
516     String LSCPServer::ListChannels() {
517     dmsg(2,("LSCPServer: ListChannels()\n"));
518     String list;
519     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
520     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
521     for (; iter != channels.end(); iter++) {
522     if (list != "") list += ",";
523     list += ToString(iter->first);
524     }
525     LSCPResultSet result;
526     result.Add(list);
527     return result.Produce();
528     }
529    
530     /**
531 schoenebeck 35 * Will be called by the parser to add a sampler channel.
532     */
533     String LSCPServer::AddChannel() {
534     dmsg(2,("LSCPServer: AddChannel()\n"));
535 schoenebeck 53 SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
536 senkov 120 LSCPResultSet result(pSamplerChannel->Index());
537     return result.Produce();
538 schoenebeck 35 }
539    
540     /**
541     * Will be called by the parser to remove a sampler channel.
542     */
543 schoenebeck 53 String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
544     dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
545 senkov 120 LSCPResultSet result;
546 senkov 360 LockRTNotify();
547 schoenebeck 53 pSampler->RemoveSamplerChannel(uiSamplerChannel);
548 senkov 360 UnlockRTNotify();
549 senkov 120 return result.Produce();
550 schoenebeck 35 }
551    
552     /**
553 capela 527 * Will be called by the parser to get the amount of all available engines.
554 schoenebeck 35 */
555     String LSCPServer::GetAvailableEngines() {
556     dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
557 capela 527 LSCPResultSet result("1");
558 senkov 120 return result.Produce();
559 schoenebeck 35 }
560    
561     /**
562 capela 527 * Will be called by the parser to get a list of all available engines.
563     */
564     String LSCPServer::ListAvailableEngines() {
565     dmsg(2,("LSCPServer: ListAvailableEngines()\n"));
566     LSCPResultSet result("\'GIG\'");
567     return result.Produce();
568     }
569    
570     /**
571 schoenebeck 411 * Will be called by the parser to get descriptions for a particular
572     * sampler engine.
573 schoenebeck 35 */
574     String LSCPServer::GetEngineInfo(String EngineName) {
575     dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
576 senkov 120 LSCPResultSet result;
577 schoenebeck 53 try {
578 schoenebeck 411 Engine* pEngine = EngineFactory::Create(EngineName);
579     result.Add("DESCRIPTION", pEngine->Description());
580     result.Add("VERSION", pEngine->Version());
581     delete pEngine;
582 schoenebeck 53 }
583     catch (LinuxSamplerException e) {
584 senkov 120 result.Error(e);
585 schoenebeck 53 }
586 senkov 120 return result.Produce();
587 schoenebeck 35 }
588    
589     /**
590     * Will be called by the parser to get informations about a particular
591     * sampler channel.
592     */
593 schoenebeck 53 String LSCPServer::GetChannelInfo(uint uiSamplerChannel) {
594     dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", uiSamplerChannel));
595 senkov 120 LSCPResultSet result;
596 senkov 113 try {
597     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
598 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
599 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
600 schoenebeck 123
601 senkov 117 //Defaults values
602     String EngineName = "NONE";
603 schoenebeck 225 float Volume = 0.0f;
604 senkov 117 String InstrumentFileName = "NONE";
605 senkov 376 String InstrumentName = "NONE";
606 capela 133 int InstrumentIndex = -1;
607     int InstrumentStatus = -1;
608 schoenebeck 225 int AudioOutputChannels = 0;
609     String AudioRouting;
610 schoenebeck 123
611 schoenebeck 475 if (pEngineChannel) {
612     EngineName = pEngineChannel->EngineName();
613 schoenebeck 411 AudioOutputChannels = pEngineChannel->Channels();
614     Volume = pEngineChannel->Volume();
615     InstrumentStatus = pEngineChannel->InstrumentStatus();
616     InstrumentIndex = pEngineChannel->InstrumentIndex();
617     if (InstrumentIndex != -1) {
618     InstrumentFileName = pEngineChannel->InstrumentFileName();
619     InstrumentName = pEngineChannel->InstrumentName();
620     }
621     for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
622 schoenebeck 225 if (AudioRouting != "") AudioRouting += ",";
623 schoenebeck 411 AudioRouting += ToString(pEngineChannel->OutputChannel(chan));
624 schoenebeck 225 }
625 senkov 113 }
626 senkov 117
627     result.Add("ENGINE_NAME", EngineName);
628     result.Add("VOLUME", Volume);
629    
630 capela 143 //Some not-so-hardcoded stuff to make GUI look good
631     result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
632 schoenebeck 225 result.Add("AUDIO_OUTPUT_CHANNELS", AudioOutputChannels);
633     result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
634 senkov 113
635 capela 159 result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pSamplerChannel->GetMidiInputDevice()));
636     result.Add("MIDI_INPUT_PORT", pSamplerChannel->GetMidiInputPort());
637 schoenebeck 274 if (pSamplerChannel->GetMidiInputChannel() == MidiInputPort::midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
638     else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
639 capela 159
640 senkov 117 result.Add("INSTRUMENT_FILE", InstrumentFileName);
641     result.Add("INSTRUMENT_NR", InstrumentIndex);
642 senkov 376 result.Add("INSTRUMENT_NAME", InstrumentName);
643 capela 133 result.Add("INSTRUMENT_STATUS", InstrumentStatus);
644 senkov 113 }
645     catch (LinuxSamplerException e) {
646 senkov 120 result.Error(e);
647 senkov 113 }
648 senkov 120 return result.Produce();
649 schoenebeck 35 }
650    
651     /**
652     * Will be called by the parser to get the amount of active voices on a
653     * particular sampler channel.
654     */
655 schoenebeck 53 String LSCPServer::GetVoiceCount(uint uiSamplerChannel) {
656     dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
657 senkov 120 LSCPResultSet result;
658 schoenebeck 53 try {
659     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
660 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
661 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
662     if (!pEngineChannel) throw LinuxSamplerException("No engine loaded on sampler channel");
663     result.Add(pEngineChannel->GetEngine()->VoiceCount());
664 schoenebeck 53 }
665     catch (LinuxSamplerException e) {
666 senkov 120 result.Error(e);
667 schoenebeck 53 }
668 senkov 120 return result.Produce();
669 schoenebeck 35 }
670    
671     /**
672     * Will be called by the parser to get the amount of active disk streams on a
673     * particular sampler channel.
674     */
675 schoenebeck 53 String LSCPServer::GetStreamCount(uint uiSamplerChannel) {
676     dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
677 senkov 120 LSCPResultSet result;
678 schoenebeck 53 try {
679     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
680 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
681 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
682     if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
683     result.Add(pEngineChannel->GetEngine()->DiskStreamCount());
684 schoenebeck 53 }
685     catch (LinuxSamplerException e) {
686 senkov 120 result.Error(e);
687 schoenebeck 53 }
688 senkov 120 return result.Produce();
689 schoenebeck 35 }
690    
691     /**
692     * Will be called by the parser to get the buffer fill states of all disk
693     * streams on a particular sampler channel.
694     */
695 schoenebeck 53 String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel) {
696     dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
697 senkov 120 LSCPResultSet result;
698 schoenebeck 53 try {
699     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
700 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
701 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
702     if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
703     if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA");
704 senkov 129 else {
705     switch (ResponseType) {
706     case fill_response_bytes:
707 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillBytes());
708     break;
709 senkov 129 case fill_response_percentage:
710 schoenebeck 411 result.Add(pEngineChannel->GetEngine()->DiskStreamBufferFillPercentage());
711     break;
712 senkov 129 default:
713     throw LinuxSamplerException("Unknown fill response type");
714     }
715     }
716 schoenebeck 53 }
717     catch (LinuxSamplerException e) {
718 senkov 120 result.Error(e);
719 schoenebeck 53 }
720 senkov 120 return result.Produce();
721 schoenebeck 35 }
722    
723 schoenebeck 123 String LSCPServer::GetAvailableAudioOutputDrivers() {
724     dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
725 senkov 120 LSCPResultSet result;
726 schoenebeck 53 try {
727 capela 527 int n = AudioOutputDeviceFactory::AvailableDrivers().size();
728     result.Add(n);
729     }
730     catch (LinuxSamplerException e) {
731     result.Error(e);
732     }
733     return result.Produce();
734     }
735    
736     String LSCPServer::ListAvailableAudioOutputDrivers() {
737     dmsg(2,("LSCPServer: ListAvailableAudioOutputDrivers()\n"));
738     LSCPResultSet result;
739     try {
740 schoenebeck 123 String s = AudioOutputDeviceFactory::AvailableDriversAsString();
741     result.Add(s);
742 schoenebeck 53 }
743     catch (LinuxSamplerException e) {
744 schoenebeck 123 result.Error(e);
745 schoenebeck 53 }
746 senkov 120 return result.Produce();
747 schoenebeck 35 }
748    
749 senkov 155 String LSCPServer::GetAvailableMidiInputDrivers() {
750     dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
751     LSCPResultSet result;
752     try {
753 capela 527 int n = MidiInputDeviceFactory::AvailableDrivers().size();
754     result.Add(n);
755     }
756     catch (LinuxSamplerException e) {
757     result.Error(e);
758     }
759     return result.Produce();
760     }
761    
762     String LSCPServer::ListAvailableMidiInputDrivers() {
763     dmsg(2,("LSCPServer: ListAvailableMidiInputDrivers()\n"));
764     LSCPResultSet result;
765     try {
766 senkov 155 String s = MidiInputDeviceFactory::AvailableDriversAsString();
767     result.Add(s);
768     }
769     catch (LinuxSamplerException e) {
770     result.Error(e);
771     }
772     return result.Produce();
773     }
774    
775     String LSCPServer::GetMidiInputDriverInfo(String Driver) {
776     dmsg(2,("LSCPServer: GetMidiInputDriverInfo(Driver=%s)\n",Driver.c_str()));
777     LSCPResultSet result;
778     try {
779     result.Add("DESCRIPTION", MidiInputDeviceFactory::GetDriverDescription(Driver));
780     result.Add("VERSION", MidiInputDeviceFactory::GetDriverVersion(Driver));
781    
782     std::map<String,DeviceCreationParameter*> parameters = MidiInputDeviceFactory::GetAvailableDriverParameters(Driver);
783     if (parameters.size()) { // if there are parameters defined for this driver
784     String s;
785     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
786     for (;iter != parameters.end(); iter++) {
787     if (s != "") s += ",";
788     s += iter->first;
789     }
790     result.Add("PARAMETERS", s);
791     }
792     }
793     catch (LinuxSamplerException e) {
794     result.Error(e);
795     }
796     return result.Produce();
797     }
798    
799 schoenebeck 123 String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
800     dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
801     LSCPResultSet result;
802     try {
803     result.Add("DESCRIPTION", AudioOutputDeviceFactory::GetDriverDescription(Driver));
804     result.Add("VERSION", AudioOutputDeviceFactory::GetDriverVersion(Driver));
805    
806     std::map<String,DeviceCreationParameter*> parameters = AudioOutputDeviceFactory::GetAvailableDriverParameters(Driver);
807     if (parameters.size()) { // if there are parameters defined for this driver
808     String s;
809     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
810     for (;iter != parameters.end(); iter++) {
811     if (s != "") s += ",";
812     s += iter->first;
813     }
814     result.Add("PARAMETERS", s);
815     }
816     }
817     catch (LinuxSamplerException e) {
818     result.Error(e);
819     }
820     return result.Produce();
821     }
822    
823 senkov 155 String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
824 schoenebeck 226 dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
825 senkov 155 LSCPResultSet result;
826     try {
827     DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
828     result.Add("TYPE", pParameter->Type());
829     result.Add("DESCRIPTION", pParameter->Description());
830 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
831     result.Add("FIX", pParameter->Fix());
832     result.Add("MULTIPLICITY", pParameter->Multiplicity());
833 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
834     optional<String> oDefault = pParameter->Default(DependencyList);
835     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
836     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
837     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
838     if (oDepends) result.Add("DEPENDS", *oDepends);
839     if (oDefault) result.Add("DEFAULT", *oDefault);
840     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
841     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
842     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
843 senkov 155 }
844     catch (LinuxSamplerException e) {
845     result.Error(e);
846     }
847     return result.Produce();
848     }
849    
850 schoenebeck 123 String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
851 schoenebeck 226 dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s,DependencyListSize=%d)\n",Driver.c_str(),Parameter.c_str(),DependencyList.size()));
852 schoenebeck 123 LSCPResultSet result;
853     try {
854     DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
855     result.Add("TYPE", pParameter->Type());
856     result.Add("DESCRIPTION", pParameter->Description());
857 schoenebeck 223 result.Add("MANDATORY", pParameter->Mandatory());
858     result.Add("FIX", pParameter->Fix());
859     result.Add("MULTIPLICITY", pParameter->Multiplicity());
860 schoenebeck 226 optional<String> oDepends = pParameter->Depends();
861     optional<String> oDefault = pParameter->Default(DependencyList);
862     optional<String> oRangeMin = pParameter->RangeMin(DependencyList);
863     optional<String> oRangeMax = pParameter->RangeMax(DependencyList);
864     optional<String> oPossibilities = pParameter->Possibilities(DependencyList);
865     if (oDepends) result.Add("DEPENDS", *oDepends);
866     if (oDefault) result.Add("DEFAULT", *oDefault);
867     if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin);
868     if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax);
869     if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities);
870 schoenebeck 123 }
871     catch (LinuxSamplerException e) {
872     result.Error(e);
873     }
874     return result.Produce();
875     }
876    
877     String LSCPServer::GetAudioOutputDeviceCount() {
878     dmsg(2,("LSCPServer: GetAudioOutputDeviceCount()\n"));
879     LSCPResultSet result;
880     try {
881     uint count = pSampler->AudioOutputDevices();
882 senkov 138 result.Add(count); // success
883 schoenebeck 123 }
884     catch (LinuxSamplerException e) {
885     result.Error(e);
886     }
887     return result.Produce();
888     }
889    
890 senkov 155 String LSCPServer::GetMidiInputDeviceCount() {
891     dmsg(2,("LSCPServer: GetMidiInputDeviceCount()\n"));
892     LSCPResultSet result;
893     try {
894     uint count = pSampler->MidiInputDevices();
895     result.Add(count); // success
896     }
897     catch (LinuxSamplerException e) {
898     result.Error(e);
899     }
900     return result.Produce();
901     }
902    
903 schoenebeck 123 String LSCPServer::GetAudioOutputDevices() {
904     dmsg(2,("LSCPServer: GetAudioOutputDevices()\n"));
905     LSCPResultSet result;
906     try {
907     String s;
908     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
909     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
910     for (; iter != devices.end(); iter++) {
911     if (s != "") s += ",";
912     s += ToString(iter->first);
913     }
914     result.Add(s);
915     }
916     catch (LinuxSamplerException e) {
917     result.Error(e);
918     }
919     return result.Produce();
920     }
921    
922 senkov 155 String LSCPServer::GetMidiInputDevices() {
923     dmsg(2,("LSCPServer: GetMidiInputDevices()\n"));
924     LSCPResultSet result;
925     try {
926     String s;
927     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
928     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
929     for (; iter != devices.end(); iter++) {
930     if (s != "") s += ",";
931     s += ToString(iter->first);
932     }
933     result.Add(s);
934     }
935     catch (LinuxSamplerException e) {
936     result.Error(e);
937     }
938     return result.Produce();
939     }
940    
941 schoenebeck 123 String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
942     dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
943     LSCPResultSet result;
944     try {
945     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
946 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
947 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
948 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
949 schoenebeck 123 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
950     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
951     for (; iter != parameters.end(); iter++) {
952     result.Add(iter->first, iter->second->Value());
953     }
954     }
955     catch (LinuxSamplerException e) {
956     result.Error(e);
957     }
958     return result.Produce();
959     }
960    
961 senkov 155 String LSCPServer::GetMidiInputDeviceInfo(uint DeviceIndex) {
962     dmsg(2,("LSCPServer: GetMidiInputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
963     LSCPResultSet result;
964     try {
965     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
966 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
967 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
968 schoenebeck 221 result.Add("DRIVER", pDevice->Driver());
969 senkov 155 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
970     std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
971     for (; iter != parameters.end(); iter++) {
972     result.Add(iter->first, iter->second->Value());
973     }
974     }
975     catch (LinuxSamplerException e) {
976     result.Error(e);
977     }
978     return result.Produce();
979     }
980     String LSCPServer::GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex) {
981     dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
982     LSCPResultSet result;
983     try {
984 schoenebeck 223 // get MIDI input device
985 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
986 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
987 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
988 schoenebeck 223
989     // get MIDI port
990 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
991     if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
992 schoenebeck 223
993     // return the values of all MIDI port parameters
994 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
995     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
996 senkov 155 for (; iter != parameters.end(); iter++) {
997     result.Add(iter->first, iter->second->Value());
998     }
999     }
1000     catch (LinuxSamplerException e) {
1001     result.Error(e);
1002     }
1003     return result.Produce();
1004     }
1005    
1006 schoenebeck 123 String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
1007     dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
1008     LSCPResultSet result;
1009     try {
1010     // get audio output device
1011     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1012 schoenebeck 223 if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1013 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1014    
1015     // get audio channel
1016     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1017 schoenebeck 374 if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1018 schoenebeck 123
1019     // return the values of all audio channel parameters
1020     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1021     std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
1022     for (; iter != parameters.end(); iter++) {
1023     result.Add(iter->first, iter->second->Value());
1024     }
1025     }
1026     catch (LinuxSamplerException e) {
1027     result.Error(e);
1028     }
1029     return result.Produce();
1030     }
1031    
1032 senkov 185 String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) {
1033     dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str()));
1034     LSCPResultSet result;
1035     try {
1036 schoenebeck 223 // get MIDI input device
1037     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1038     if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + ".");
1039     MidiInputDevice* pDevice = devices[DeviceId];
1040 senkov 185
1041 schoenebeck 221 // get midi port
1042     MidiInputPort* pPort = pDevice->GetPort(PortId);
1043     if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + ".");
1044 senkov 185
1045 schoenebeck 223 // get desired port parameter
1046     std::map<String,DeviceRuntimeParameter*> parameters = pPort->PortParameters();
1047     if (!parameters.count(ParameterName)) throw LinuxSamplerException("Midi port does not provide a parameter '" + ParameterName + "'.");
1048     DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1049 schoenebeck 203
1050 senkov 185 // return all fields of this audio channel parameter
1051     result.Add("TYPE", pParameter->Type());
1052     result.Add("DESCRIPTION", pParameter->Description());
1053     result.Add("FIX", pParameter->Fix());
1054     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1055 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1056     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1057     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1058 senkov 185 }
1059     catch (LinuxSamplerException e) {
1060     result.Error(e);
1061     }
1062     return result.Produce();
1063     }
1064    
1065 schoenebeck 123 String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
1066     dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
1067     LSCPResultSet result;
1068     try {
1069     // get audio output device
1070     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1071 schoenebeck 223 if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1072 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1073    
1074     // get audio channel
1075     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1076 schoenebeck 374 if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1077 schoenebeck 123
1078     // get desired audio channel parameter
1079     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1080 schoenebeck 223 if (!parameters.count(ParameterName)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");
1081 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
1082    
1083     // return all fields of this audio channel parameter
1084     result.Add("TYPE", pParameter->Type());
1085     result.Add("DESCRIPTION", pParameter->Description());
1086     result.Add("FIX", pParameter->Fix());
1087     result.Add("MULTIPLICITY", pParameter->Multiplicity());
1088 schoenebeck 223 if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin());
1089     if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax());
1090     if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities());
1091 schoenebeck 123 }
1092     catch (LinuxSamplerException e) {
1093     result.Error(e);
1094     }
1095     return result.Produce();
1096     }
1097    
1098     String LSCPServer::SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal) {
1099     dmsg(2,("LSCPServer: SetAudioOutputChannelParameter(DeviceId=%d,ChannelId=%d,ParamKey=%s,ParamVal=%s)\n",DeviceId,ChannelId,ParamKey.c_str(),ParamVal.c_str()));
1100     LSCPResultSet result;
1101     try {
1102     // get audio output device
1103     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1104 schoenebeck 223 if (!devices.count(DeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
1105 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceId];
1106    
1107     // get audio channel
1108     AudioChannel* pChannel = pDevice->Channel(ChannelId);
1109 schoenebeck 374 if (!pChannel) throw LinuxSamplerException("Audio output device does not have audio channel " + ToString(ChannelId) + ".");
1110 schoenebeck 123
1111     // get desired audio channel parameter
1112     std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
1113 schoenebeck 223 if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");
1114 schoenebeck 123 DeviceRuntimeParameter* pParameter = parameters[ParamKey];
1115    
1116     // set new channel parameter value
1117     pParameter->SetValue(ParamVal);
1118     }
1119     catch (LinuxSamplerException e) {
1120     result.Error(e);
1121     }
1122     return result.Produce();
1123     }
1124    
1125     String LSCPServer::SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1126     dmsg(2,("LSCPServer: SetAudioOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1127     LSCPResultSet result;
1128     try {
1129     std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1130 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
1131 schoenebeck 123 AudioOutputDevice* pDevice = devices[DeviceIndex];
1132     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1133 schoenebeck 223 if (!parameters.count(ParamKey)) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1134 schoenebeck 123 parameters[ParamKey]->SetValue(ParamVal);
1135     }
1136     catch (LinuxSamplerException e) {
1137     result.Error(e);
1138     }
1139     return result.Produce();
1140     }
1141    
1142 senkov 155 String LSCPServer::SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
1143     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1144     LSCPResultSet result;
1145     try {
1146     std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1147 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1148 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1149     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1150 schoenebeck 223 if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
1151 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1152     }
1153     catch (LinuxSamplerException e) {
1154     result.Error(e);
1155     }
1156     return result.Produce();
1157     }
1158    
1159     String LSCPServer::SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal) {
1160     dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
1161     LSCPResultSet result;
1162     try {
1163 schoenebeck 223 // get MIDI input device
1164 senkov 155 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1165 schoenebeck 223 if (!devices.count(DeviceIndex)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(DeviceIndex) + ".");
1166 senkov 155 MidiInputDevice* pDevice = devices[DeviceIndex];
1167 schoenebeck 223
1168     // get MIDI port
1169 schoenebeck 221 MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
1170     if (!pMidiInputPort) throw LinuxSamplerException("There is no MIDI input port with index " + ToString(PortIndex) + ".");
1171 schoenebeck 223
1172     // set port parameter value
1173 schoenebeck 221 std::map<String,DeviceRuntimeParameter*> parameters = pMidiInputPort->PortParameters();
1174 schoenebeck 223 if (!parameters.count(ParamKey)) throw LinuxSamplerException("MIDI input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
1175 senkov 155 parameters[ParamKey]->SetValue(ParamVal);
1176     }
1177     catch (LinuxSamplerException e) {
1178     result.Error(e);
1179     }
1180     return result.Produce();
1181     }
1182    
1183 schoenebeck 35 /**
1184     * Will be called by the parser to change the audio output channel for
1185     * playback on a particular sampler channel.
1186     */
1187 schoenebeck 123 String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
1188     dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));
1189 schoenebeck 225 LSCPResultSet result;
1190     try {
1191     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1192 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1193 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1194     if (!pEngineChannel) throw LinuxSamplerException("No engine type yet assigned to sampler channel " + ToString(uiSamplerChannel));
1195 schoenebeck 374 if (!pSamplerChannel->GetAudioOutputDevice()) throw LinuxSamplerException("No audio output device connected to sampler channel " + ToString(uiSamplerChannel));
1196 schoenebeck 411 pEngineChannel->SetOutputChannel(ChannelAudioOutputChannel, AudioOutputDeviceInputChannel);
1197 schoenebeck 225 }
1198     catch (LinuxSamplerException e) {
1199     result.Error(e);
1200     }
1201     return result.Produce();
1202 schoenebeck 35 }
1203    
1204 capela 159 String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1205     dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1206     LSCPResultSet result;
1207     try {
1208     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1209 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1210 capela 159 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1211 schoenebeck 223 if (!devices.count(AudioDeviceId)) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
1212 capela 159 AudioOutputDevice* pDevice = devices[AudioDeviceId];
1213     pSamplerChannel->SetAudioOutputDevice(pDevice);
1214     }
1215     catch (LinuxSamplerException e) {
1216     result.Error(e);
1217     }
1218     return result.Produce();
1219     }
1220    
1221 capela 143 String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1222     dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1223     LSCPResultSet result;
1224     try {
1225     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1226 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1227 capela 143 // Driver type name aliasing...
1228 schoenebeck 226 if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1229     if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1230 capela 143 // Check if there's one audio output device already created
1231     // for the intended audio driver type (AudioOutputDriver)...
1232     AudioOutputDevice *pDevice = NULL;
1233     std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1234     std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1235     for (; iter != devices.end(); iter++) {
1236     if ((iter->second)->Driver() == AudioOutputDriver) {
1237     pDevice = iter->second;
1238     break;
1239     }
1240     }
1241     // If it doesn't exist, create a new one with default parameters...
1242     if (pDevice == NULL) {
1243     std::map<String,String> params;
1244     pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1245     }
1246     // Must have a device...
1247     if (pDevice == NULL)
1248     throw LinuxSamplerException("Internal error: could not create audio output device.");
1249     // Set it as the current channel device...
1250     pSamplerChannel->SetAudioOutputDevice(pDevice);
1251     }
1252     catch (LinuxSamplerException e) {
1253     result.Error(e);
1254     }
1255     return result.Produce();
1256     }
1257    
1258 capela 159 String LSCPServer::SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel) {
1259     dmsg(2,("LSCPServer: SetMIDIInputPort(MIDIPort=%d, SamplerChannel=%d)\n",MIDIPort,uiSamplerChannel));
1260 senkov 120 LSCPResultSet result;
1261 schoenebeck 53 try {
1262     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1263 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1264 capela 159 pSamplerChannel->SetMidiInputPort(MIDIPort);
1265 schoenebeck 53 }
1266     catch (LinuxSamplerException e) {
1267 senkov 120 result.Error(e);
1268 schoenebeck 53 }
1269 senkov 120 return result.Produce();
1270 schoenebeck 53 }
1271    
1272 capela 159 String LSCPServer::SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel) {
1273     dmsg(2,("LSCPServer: SetMIDIInputChannel(MIDIChannel=%d, SamplerChannel=%d)\n",MIDIChannel,uiSamplerChannel));
1274 senkov 120 LSCPResultSet result;
1275 senkov 68 try {
1276     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1277 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1278 schoenebeck 221 pSamplerChannel->SetMidiInputChannel((MidiInputPort::midi_chan_t) MIDIChannel);
1279 senkov 68 }
1280     catch (LinuxSamplerException e) {
1281 senkov 120 result.Error(e);
1282 senkov 68 }
1283 senkov 120 return result.Produce();
1284 schoenebeck 35 }
1285    
1286 capela 159 String LSCPServer::SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel) {
1287     dmsg(2,("LSCPServer: SetMIDIInputDevice(MIDIDeviceId=%d, SamplerChannel=%d)\n",MIDIDeviceId,uiSamplerChannel));
1288 schoenebeck 123 LSCPResultSet result;
1289     try {
1290 capela 159 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1291 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1292 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1293 schoenebeck 223 if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1294 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1295     pSamplerChannel->SetMidiInputDevice(pDevice);
1296 schoenebeck 123 }
1297     catch (LinuxSamplerException e) {
1298     result.Error(e);
1299     }
1300     return result.Produce();
1301     }
1302    
1303 capela 159 String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
1304     dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
1305     LSCPResultSet result;
1306     try {
1307     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1308 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1309 capela 159 // Driver type name aliasing...
1310 schoenebeck 226 if (MidiInputDriver == "Alsa") MidiInputDriver = "ALSA";
1311 capela 159 // Check if there's one MIDI input device already created
1312     // for the intended MIDI driver type (MidiInputDriver)...
1313     MidiInputDevice *pDevice = NULL;
1314     std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1315     std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
1316     for (; iter != devices.end(); iter++) {
1317     if ((iter->second)->Driver() == MidiInputDriver) {
1318     pDevice = iter->second;
1319     break;
1320     }
1321     }
1322     // If it doesn't exist, create a new one with default parameters...
1323     if (pDevice == NULL) {
1324     std::map<String,String> params;
1325     pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params);
1326     // Make it with at least one initial port.
1327     std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
1328 schoenebeck 221 parameters["PORTS"]->SetValue("1");
1329 capela 159 }
1330     // Must have a device...
1331     if (pDevice == NULL)
1332     throw LinuxSamplerException("Internal error: could not create MIDI input device.");
1333     // Set it as the current channel device...
1334     pSamplerChannel->SetMidiInputDevice(pDevice);
1335     }
1336     catch (LinuxSamplerException e) {
1337     result.Error(e);
1338     }
1339     return result.Produce();
1340     }
1341    
1342 schoenebeck 35 /**
1343 capela 159 * Will be called by the parser to change the MIDI input device, port and channel on which
1344     * engine of a particular sampler channel should listen to.
1345     */
1346     String LSCPServer::SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
1347     dmsg(2,("LSCPServer: SetMIDIInput(MIDIDeviceId=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDeviceId, MIDIPort, MIDIChannel, uiSamplerChannel));
1348     LSCPResultSet result;
1349     try {
1350     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1351 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1352 capela 159 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
1353 schoenebeck 223 if (!devices.count(MIDIDeviceId)) throw LinuxSamplerException("There is no MIDI input device with index " + ToString(MIDIDeviceId));
1354 capela 159 MidiInputDevice* pDevice = devices[MIDIDeviceId];
1355 schoenebeck 221 pSamplerChannel->SetMidiInput(pDevice, MIDIPort, (MidiInputPort::midi_chan_t) MIDIChannel);
1356 capela 159 }
1357     catch (LinuxSamplerException e) {
1358     result.Error(e);
1359     }
1360     return result.Produce();
1361     }
1362    
1363     /**
1364 schoenebeck 35 * Will be called by the parser to change the global volume factor on a
1365     * particular sampler channel.
1366     */
1367 schoenebeck 225 String LSCPServer::SetVolume(double dVolume, uint uiSamplerChannel) {
1368     dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel));
1369 senkov 120 LSCPResultSet result;
1370 schoenebeck 53 try {
1371     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1372 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1373 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1374     if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1375     pEngineChannel->Volume(dVolume);
1376 schoenebeck 53 }
1377     catch (LinuxSamplerException e) {
1378 senkov 120 result.Error(e);
1379 schoenebeck 53 }
1380 senkov 120 return result.Produce();
1381 schoenebeck 35 }
1382    
1383     /**
1384     * Will be called by the parser to reset a particular sampler channel.
1385     */
1386 schoenebeck 53 String LSCPServer::ResetChannel(uint uiSamplerChannel) {
1387     dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
1388 senkov 120 LSCPResultSet result;
1389 schoenebeck 53 try {
1390     SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1391 schoenebeck 374 if (!pSamplerChannel) throw LinuxSamplerException("Invalid sampler channel number " + ToString(uiSamplerChannel));
1392 schoenebeck 411 EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
1393     if (!pEngineChannel) throw LinuxSamplerException("No engine type assigned to sampler channel");
1394     pEngineChannel->GetEngine()->Reset();
1395 schoenebeck 53 }
1396     catch (LinuxSamplerException e) {
1397 senkov 120 result.Error(e);
1398 schoenebeck 53 }
1399 senkov 120 return result.Produce();
1400 schoenebeck 35 }
1401    
1402     /**
1403 schoenebeck 212 * Will be called by the parser to reset the whole sampler.
1404     */
1405     String LSCPServer::ResetSampler() {
1406     dmsg(2,("LSCPServer: ResetSampler()\n"));
1407     pSampler->Reset();
1408     LSCPResultSet result;
1409     return result.Produce();
1410     }
1411    
1412     /**
1413 schoenebeck 35 * Will be called by the parser to subscribe a client (frontend) on the
1414     * server for receiving event messages.
1415     */
1416 senkov 170 String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
1417     dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1418     LSCPResultSet result;
1419     SubscriptionMutex.Lock();
1420     eventSubscriptions[type].push_back(currentSocket);
1421     SubscriptionMutex.Unlock();
1422     return result.Produce();
1423 schoenebeck 35 }
1424    
1425     /**
1426     * Will be called by the parser to unsubscribe a client on the server
1427     * for not receiving further event messages.
1428     */
1429 senkov 170 String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
1430     dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
1431     LSCPResultSet result;
1432     SubscriptionMutex.Lock();
1433     eventSubscriptions[type].remove(currentSocket);
1434     SubscriptionMutex.Unlock();
1435     return result.Produce();
1436 schoenebeck 35 }
1437 capela 133
1438 senkov 397 static int select_callback(void * lscpResultSet, int argc,
1439     char **argv, char **azColName)
1440     {
1441     LSCPResultSet* resultSet = (LSCPResultSet*) lscpResultSet;
1442     resultSet->Add(argc, argv);
1443     return 0;
1444     }
1445    
1446     String LSCPServer::QueryDatabase(String query) {
1447     LSCPResultSet result;
1448 schoenebeck 401 #if HAVE_SQLITE3
1449 senkov 397 char* zErrMsg = NULL;
1450     sqlite3 *db;
1451     String selectStr = "SELECT " + query;
1452    
1453     int rc = sqlite3_open("linuxsampler.db", &db);
1454     if (rc == SQLITE_OK)
1455     {
1456     rc = sqlite3_exec(db, selectStr.c_str(), select_callback, &result, &zErrMsg);
1457     }
1458     if ( rc != SQLITE_OK )
1459     {
1460 senkov 398 result.Error(String(zErrMsg), rc);
1461 senkov 397 }
1462     sqlite3_close(db);
1463     #else
1464     result.Error(String("SQLITE3 was not installed when linuxsampler was built. SELECT statement is not available."), 0);
1465     #endif
1466     return result.Produce();
1467     }
1468    
1469 schoenebeck 210 /**
1470     * Will be called by the parser to enable or disable echo mode; if echo
1471     * mode is enabled, all commands from the client will (immediately) be
1472     * echoed back to the client.
1473     */
1474     String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) {
1475     dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value));
1476     LSCPResultSet result;
1477     try {
1478     if (boolean_value == 0) pSession->bVerbose = false;
1479     else if (boolean_value == 1) pSession->bVerbose = true;
1480     else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1");
1481     }
1482     catch (LinuxSamplerException e) {
1483     result.Error(e);
1484     }
1485     return result.Produce();
1486     }

  ViewVC Help
Powered by ViewVC