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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 475 - (show annotations) (download)
Thu Mar 17 23:56:56 2005 UTC (19 years ago) by schoenebeck
File size: 62194 byte(s)
* fixed segmentation fault on "GET CHANNEL INFO" commands which occured
  when an engine type was selected but no audio device connected yet to
  the sampler channel (fixes bug #4)

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

  ViewVC Help
Powered by ViewVC