/[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 401 - (show annotations) (download)
Tue Feb 22 14:09:26 2005 UTC (19 years ago) by schoenebeck
File size: 61664 byte(s)
* fixed build failure if libsqlite3 is not installed

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

  ViewVC Help
Powered by ViewVC