/[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 841 - (hide annotations) (download)
Sat Mar 4 16:23:53 2006 UTC (18 years ago) by persson
File size: 70259 byte(s)
* fixed concurrency problems between the main thread doing
  lscp notifications and the lscp thread
* bug fix: the LFOs were not properly initialized

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

  ViewVC Help
Powered by ViewVC