/[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 398 - (hide annotations) (download)
Mon Feb 21 04:35:31 2005 UTC (19 years, 1 month ago) by senkov
File size: 61669 byte(s)
removed debug from previous checkin.

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

  ViewVC Help
Powered by ViewVC