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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 210 - (hide annotations) (download)
Sat Jul 24 12:33:49 2004 UTC (19 years, 8 months ago) by schoenebeck
File size: 56552 byte(s)
* implemented "SET ECHO" LSCP command

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

  ViewVC Help
Powered by ViewVC