/[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 667 - (hide annotations) (download)
Sun Jun 19 21:22:34 2005 UTC (18 years, 9 months ago) by senkov
File size: 64844 byte(s)
* added cmdline options for lscp address and port

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

  ViewVC Help
Powered by ViewVC