/[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 385 - (show annotations) (download)
Thu Feb 17 02:53:45 2005 UTC (19 years, 1 month ago) by schoenebeck
File size: 60550 byte(s)
* lscpserver: we now only use one instrument loader thread; commands for
  loading instruments in the background wait in a queue to be processed one
  by one to avoid possible race conditions and to improve I/O efficiency
* fixed possible race condition while streaming with multiple disk threads
  by using an own decompression buffer for each disk thread
* libgig: fixed some memory leaks caused by non virtual base constructors

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

  ViewVC Help
Powered by ViewVC