/[svn]/liblscp/trunk/src/client.c
ViewVC logotype

Annotation of /liblscp/trunk/src/client.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 978 - (hide annotations) (download)
Sun Dec 17 20:29:45 2006 UTC (17 years, 3 months ago) by capela
File MIME type: text/plain
File size: 65846 byte(s)
* Preparation for final 0.5.0 release.

1 capela 107 // client.c
2     //
3     /****************************************************************************
4     liblscp - LinuxSampler Control Protocol API
5 capela 869 Copyright (C) 2004-2006, rncbc aka Rui Nuno Capela. All rights reserved.
6 capela 107
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Lesser General Public
9     License as published by the Free Software Foundation; either
10     version 2.1 of the License, or (at your option) any later version.
11    
12     This library 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 GNU
15     Lesser General Public License for more details.
16    
17 capela 921 You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 capela 107
21     *****************************************************************************/
22    
23     #include "common.h"
24    
25     // Default timeout value (in milliseconds).
26     #define LSCP_TIMEOUT_MSECS 500
27    
28    
29     // Local prototypes.
30    
31 capela 144 static void _lscp_client_evt_proc (void *pvClient);
32 capela 107
33 capela 144 static lscp_status_t _lscp_client_evt_connect (lscp_client_t *pClient);
34     static lscp_status_t _lscp_client_evt_request (lscp_client_t *pClient, int iSubscribe, lscp_event_t event);
35 capela 107
36 capela 144
37 capela 107 //-------------------------------------------------------------------------
38 capela 132 // Event service (datagram oriented).
39 capela 107
40 capela 132 static void _lscp_client_evt_proc ( void *pvClient )
41 capela 107 {
42 capela 952 lscp_client_t *pClient = (lscp_client_t *) pvClient;
43 capela 562
44 capela 952 fd_set fds; // File descriptor list for select().
45     int fd, fdmax; // Maximum file descriptor number.
46     struct timeval tv; // For specifying a timeout value.
47     int iSelect; // Holds select return status.
48     int iTimeout;
49 capela 562
50 capela 952 char achBuffer[LSCP_BUFSIZ];
51     int cchBuffer;
52     const char *pszSeps = ":\r\n";
53     char * pszToken;
54     char * pch;
55     int cchToken;
56     lscp_event_t event;
57 capela 107
58     #ifdef DEBUG
59 capela 952 fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n");
60 capela 107 #endif
61    
62 capela 952 while (pClient->evt.iState) {
63 capela 177
64 capela 952 // Prepare for waiting on select...
65     fd = (int) pClient->evt.sock;
66     FD_ZERO(&fds);
67     FD_SET((unsigned int) fd, &fds);
68     fdmax = fd;
69 capela 177
70 capela 952 // Use the timeout (x10) select feature ...
71     iTimeout = 10 * pClient->iTimeout;
72     if (iTimeout >= 1000) {
73     tv.tv_sec = iTimeout / 1000;
74     iTimeout -= tv.tv_sec * 1000;
75     }
76     else tv.tv_sec = 0;
77     tv.tv_usec = iTimeout * 1000;
78 capela 177
79 capela 952 // Wait for event...
80     iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv);
81     if (iSelect > 0 && FD_ISSET(fd, &fds)) {
82     // May recv now...
83     cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0);
84     if (cchBuffer > 0) {
85     // Make sure received buffer it's null terminated.
86     achBuffer[cchBuffer] = (char) 0;
87     // Parse for the notification event message...
88     pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY".
89     if (strcasecmp(pszToken, "NOTIFY") == 0) {
90     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
91     event = lscp_event_from_text(pszToken);
92     // And pick the rest of data...
93     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
94     cchToken = (pszToken == NULL ? 0 : strlen(pszToken));
95     // Double-check if we're really up to it...
96     if (pClient->events & event) {
97     // Invoke the client event callback...
98     if ((*pClient->pfnCallback)(
99     pClient,
100     event,
101     pszToken,
102     cchToken,
103     pClient->pvData) != LSCP_OK) {
104     pClient->evt.iState = 0;
105     }
106     }
107     }
108     } else {
109     lscp_socket_perror("_lscp_client_evt_proc: recv");
110     pClient->evt.iState = 0;
111     }
112     } // Check if select has in error.
113     else if (iSelect < 0) {
114     lscp_socket_perror("_lscp_client_evt_proc: select");
115     pClient->evt.iState = 0;
116     }
117 capela 562
118 capela 952 // Finally, always signal the event.
119     lscp_cond_signal(pClient->cond);
120     }
121 capela 107
122     #ifdef DEBUG
123 capela 952 fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n");
124 capela 107 #endif
125     }
126    
127    
128     //-------------------------------------------------------------------------
129 capela 144 // Event subscription helpers.
130    
131     // Open the event service socket connection.
132     static lscp_status_t _lscp_client_evt_connect ( lscp_client_t *pClient )
133     {
134 capela 952 lscp_socket_t sock;
135     struct sockaddr_in addr;
136     int cAddr;
137 capela 144 #if defined(WIN32)
138 capela 952 int iSockOpt = (-1);
139 capela 144 #endif
140    
141 capela 952 // Prepare the event connection socket...
142     sock = socket(AF_INET, SOCK_STREAM, 0);
143     if (sock == INVALID_SOCKET) {
144     lscp_socket_perror("_lscp_client_evt_connect: socket");
145     return LSCP_FAILED;
146     }
147 capela 144
148     #if defined(WIN32)
149 capela 952 if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)
150     lscp_socket_perror("lscp_client_evt_connect: setsockopt(SO_DONTLINGER)");
151 capela 144 #endif
152    
153     #ifdef DEBUG
154 capela 952 lscp_socket_getopts("_lscp_client_evt_connect:", sock);
155 capela 144 #endif
156    
157 capela 952 // Use same address of the command connection.
158     cAddr = sizeof(struct sockaddr_in);
159     memmove((char *) &addr, &(pClient->cmd.addr), cAddr);
160 capela 144
161 capela 952 // Start the connection...
162     if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {
163     lscp_socket_perror("_lscp_client_evt_connect: connect");
164     closesocket(sock);
165     return LSCP_FAILED;
166     }
167 capela 562
168 capela 952 // Set our socket agent struct...
169     lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr);
170 capela 562
171 capela 952 // And finally the service thread...
172     return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0);
173 capela 144 }
174    
175    
176     // Subscribe to a single event.
177     static lscp_status_t _lscp_client_evt_request ( lscp_client_t *pClient, int iSubscribe, lscp_event_t event )
178     {
179 capela 952 const char *pszEvent;
180     char szQuery[LSCP_BUFSIZ];
181     int cchQuery;
182 capela 144
183 capela 952 if (pClient == NULL)
184     return LSCP_FAILED;
185 capela 144
186 capela 952 // Which (single) event?
187     pszEvent = lscp_event_to_text(event);
188     if (pszEvent == NULL)
189     return LSCP_FAILED;
190 capela 146
191 capela 952 // Build the query string...
192     cchQuery = sprintf(szQuery, "%sSUBSCRIBE %s\n\n", (iSubscribe == 0 ? "UN" : ""), pszEvent);
193     // Just send data, forget result...
194     if (send(pClient->evt.sock, szQuery, cchQuery, 0) < cchQuery) {
195     lscp_socket_perror("_lscp_client_evt_request: send");
196     return LSCP_FAILED;
197     }
198 capela 144
199 capela 952 // Wait on response.
200     lscp_cond_wait(pClient->cond, pClient->mutex);
201 capela 562
202 capela 952 // Update as naively as we can...
203     if (iSubscribe)
204     pClient->events |= event;
205     else
206     pClient->events &= ~event;
207 capela 144
208 capela 952 return LSCP_OK;
209 capela 144 }
210    
211    
212     //-------------------------------------------------------------------------
213 capela 107 // Client versioning teller fuunction.
214    
215    
216     /** Retrieve the current client library version string. */
217     const char* lscp_client_package (void) { return LSCP_PACKAGE; }
218    
219     /** Retrieve the current client library version string. */
220     const char* lscp_client_version (void) { return LSCP_VERSION; }
221    
222     /** Retrieve the current client library build timestamp string. */
223     const char* lscp_client_build (void) { return __DATE__ " " __TIME__; }
224    
225    
226     //-------------------------------------------------------------------------
227     // Client socket functions.
228    
229     /**
230     * Create a client instance, estabilishing a connection to a server hostname,
231     * which must be listening on the given port. A client callback function is
232     * also supplied for server notification event handling.
233     *
234     * @param pszHost Hostname of the linuxsampler listening server.
235     * @param iPort Port number of the linuxsampler listening server.
236     * @param pfnCallback Callback function to receive event notifications.
237     * @param pvData User context opaque data, that will be passed
238     * to the callback function.
239     *
240     * @returns The new client instance pointer if successfull, which shall be
241     * used on all subsequent client calls, NULL otherwise.
242     */
243     lscp_client_t* lscp_client_create ( const char *pszHost, int iPort, lscp_client_proc_t pfnCallback, void *pvData )
244     {
245 capela 952 lscp_client_t *pClient;
246     struct hostent *pHost;
247     lscp_socket_t sock;
248     struct sockaddr_in addr;
249     int cAddr;
250 capela 144 #if defined(WIN32)
251 capela 952 int iSockOpt = (-1);
252 capela 144 #endif
253 capela 107
254 capela 952 if (pfnCallback == NULL) {
255     fprintf(stderr, "lscp_client_create: Invalid client callback function.\n");
256     return NULL;
257     }
258 capela 107
259 capela 952 pHost = gethostbyname(pszHost);
260     if (pHost == NULL) {
261     lscp_socket_herror("lscp_client_create: gethostbyname");
262     return NULL;
263     }
264 capela 107
265 capela 952 // Allocate client descriptor...
266 capela 107
267 capela 952 pClient = (lscp_client_t *) malloc(sizeof(lscp_client_t));
268     if (pClient == NULL) {
269     fprintf(stderr, "lscp_client_create: Out of memory.\n");
270     return NULL;
271     }
272     memset(pClient, 0, sizeof(lscp_client_t));
273 capela 107
274 capela 952 pClient->pfnCallback = pfnCallback;
275     pClient->pvData = pvData;
276 capela 107
277     #ifdef DEBUG
278 capela 952 fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort);
279 capela 107 #endif
280    
281 capela 952 // Prepare the command connection socket...
282 capela 107
283 capela 952 sock = socket(AF_INET, SOCK_STREAM, 0);
284     if (sock == INVALID_SOCKET) {
285     lscp_socket_perror("lscp_client_create: cmd: socket");
286     free(pClient);
287     return NULL;
288     }
289 capela 107
290     #if defined(WIN32)
291 capela 952 if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR)
292     lscp_socket_perror("lscp_client_create: cmd: setsockopt(SO_DONTLINGER)");
293 capela 107 #endif
294    
295     #ifdef DEBUG
296 capela 952 lscp_socket_getopts("lscp_client_create: cmd", sock);
297 capela 107 #endif
298    
299 capela 952 cAddr = sizeof(struct sockaddr_in);
300     memset((char *) &addr, 0, cAddr);
301     addr.sin_family = pHost->h_addrtype;
302     memmove((char *) &(addr.sin_addr), pHost->h_addr, pHost->h_length);
303     addr.sin_port = htons((short) iPort);
304 capela 107
305 capela 952 if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) {
306     lscp_socket_perror("lscp_client_create: cmd: connect");
307     closesocket(sock);
308     free(pClient);
309     return NULL;
310     }
311 capela 107
312 capela 952 // Initialize the command socket agent struct...
313     lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr);
314 capela 107
315     #ifdef DEBUG
316 capela 952 fprintf(stderr, "lscp_client_create: cmd: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->cmd.sock, inet_ntoa(pClient->cmd.addr.sin_addr), ntohs(pClient->cmd.addr.sin_port));
317 capela 107 #endif
318    
319 capela 952 // Initialize the event service socket struct...
320     lscp_socket_agent_init(&(pClient->evt), INVALID_SOCKET, NULL, 0);
321     // No events subscribed, yet.
322     pClient->events = LSCP_EVENT_NONE;
323     // Initialize cached members.
324     pClient->audio_drivers = NULL;
325     pClient->midi_drivers = NULL;
326     pClient->audio_devices = NULL;
327     pClient->midi_devices = NULL;
328     pClient->engines = NULL;
329     pClient->channels = NULL;
330     pClient->midi_instruments = NULL;
331 capela 975 pClient->midi_maps = NULL;
332     pClient->midi_map_name = NULL;
333 capela 952 lscp_driver_info_init(&(pClient->audio_driver_info));
334     lscp_driver_info_init(&(pClient->midi_driver_info));
335     lscp_device_info_init(&(pClient->audio_device_info));
336     lscp_device_info_init(&(pClient->midi_device_info));
337     lscp_param_info_init(&(pClient->audio_param_info));
338     lscp_param_info_init(&(pClient->midi_param_info));
339     lscp_device_port_info_init(&(pClient->audio_channel_info));
340     lscp_device_port_info_init(&(pClient->midi_port_info));
341     lscp_param_info_init(&(pClient->audio_channel_param_info));
342     lscp_param_info_init(&(pClient->midi_port_param_info));
343     lscp_server_info_init(&(pClient->server_info));
344     lscp_engine_info_init(&(pClient->engine_info));
345     lscp_channel_info_init(&(pClient->channel_info));
346     lscp_midi_instrument_info_init(&(pClient->midi_instrument_info));
347     // Initialize error stuff.
348     pClient->pszResult = NULL;
349     pClient->iErrno = -1;
350     // Stream usage stuff.
351     pClient->buffer_fill = NULL;
352     pClient->iStreamCount = 0;
353     // Default timeout value.
354     pClient->iTimeout = LSCP_TIMEOUT_MSECS;
355 capela 626 pClient->iTimeoutCount = 0;
356 capela 107
357 capela 952 // Initialize the transaction mutex.
358     lscp_mutex_init(pClient->mutex);
359     lscp_cond_init(pClient->cond);
360 capela 107
361 capela 952 // Finally we've some success...
362     return pClient;
363 capela 107 }
364    
365    
366     /**
367     * Wait for a client instance to terminate graciously.
368     *
369     * @param pClient Pointer to client instance structure.
370     */
371     lscp_status_t lscp_client_join ( lscp_client_t *pClient )
372     {
373 capela 952 if (pClient == NULL)
374     return LSCP_FAILED;
375 capela 107
376     #ifdef DEBUG
377 capela 952 fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient);
378 capela 107 #endif
379    
380 capela 132 // lscp_socket_agent_join(&(pClient->evt));
381 capela 952 lscp_socket_agent_join(&(pClient->cmd));
382 capela 107
383 capela 952 return LSCP_OK;
384 capela 107 }
385    
386    
387     /**
388     * Terminate and destroy a client instance.
389     *
390     * @param pClient Pointer to client instance structure.
391     *
392     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
393     */
394     lscp_status_t lscp_client_destroy ( lscp_client_t *pClient )
395     {
396 capela 952 if (pClient == NULL)
397     return LSCP_FAILED;
398 capela 107
399     #ifdef DEBUG
400 capela 952 fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient);
401 capela 107 #endif
402    
403 capela 952 // Lock this section up.
404     lscp_mutex_lock(pClient->mutex);
405 capela 562
406 capela 952 // Free up all cached members.
407     lscp_midi_instrument_info_free(&(pClient->midi_instrument_info));
408     lscp_channel_info_free(&(pClient->channel_info));
409     lscp_engine_info_free(&(pClient->engine_info));
410     lscp_server_info_free(&(pClient->server_info));
411     lscp_param_info_free(&(pClient->midi_port_param_info));
412     lscp_param_info_free(&(pClient->audio_channel_param_info));
413     lscp_device_port_info_free(&(pClient->midi_port_info));
414     lscp_device_port_info_free(&(pClient->audio_channel_info));
415     lscp_param_info_free(&(pClient->midi_param_info));
416     lscp_param_info_free(&(pClient->audio_param_info));
417     lscp_device_info_free(&(pClient->midi_device_info));
418     lscp_device_info_free(&(pClient->audio_device_info));
419     lscp_driver_info_free(&(pClient->midi_driver_info));
420     lscp_driver_info_free(&(pClient->audio_driver_info));
421     // Free available engine table.
422     lscp_szsplit_destroy(pClient->audio_drivers);
423     lscp_szsplit_destroy(pClient->midi_drivers);
424     lscp_isplit_destroy(pClient->audio_devices);
425     lscp_isplit_destroy(pClient->midi_devices);
426     lscp_szsplit_destroy(pClient->engines);
427     lscp_isplit_destroy(pClient->channels);
428     lscp_midi_instruments_destroy(pClient->midi_instruments);
429 capela 975 lscp_isplit_destroy(pClient->midi_maps);
430     if (pClient->midi_map_name)
431     free(pClient->midi_map_name);
432 capela 952 // Make them null.
433     pClient->audio_drivers = NULL;
434     pClient->midi_drivers = NULL;
435     pClient->audio_devices = NULL;
436     pClient->midi_devices = NULL;
437     pClient->engines = NULL;
438     pClient->channels = NULL;
439     pClient->midi_instruments = NULL;
440 capela 975 pClient->midi_maps = NULL;
441     pClient->midi_map_name = NULL;
442 capela 952 // Free result error stuff.
443     lscp_client_set_result(pClient, NULL, 0);
444     // Free stream usage stuff.
445     if (pClient->buffer_fill)
446     free(pClient->buffer_fill);
447     pClient->buffer_fill = NULL;
448     pClient->iStreamCount = 0;
449     pClient->iTimeout = 0;
450 capela 107
451 capela 952 // Free socket agents.
452     lscp_socket_agent_free(&(pClient->evt));
453     lscp_socket_agent_free(&(pClient->cmd));
454 capela 107
455 capela 952 // Last but not least, free good ol'transaction mutex.
456     lscp_mutex_unlock(pClient->mutex);
457     lscp_mutex_destroy(pClient->mutex);
458     lscp_cond_destroy(pClient->cond);
459 capela 107
460 capela 952 free(pClient);
461 capela 107
462 capela 952 return LSCP_OK;
463 capela 107 }
464    
465    
466     /**
467     * Set the client transaction timeout interval.
468     *
469     * @param pClient Pointer to client instance structure.
470     * @param iTimeout Transaction timeout in milliseconds.
471     *
472     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
473     */
474     lscp_status_t lscp_client_set_timeout ( lscp_client_t *pClient, int iTimeout )
475     {
476 capela 952 if (pClient == NULL)
477     return LSCP_FAILED;
478     if (iTimeout < 0)
479     return LSCP_FAILED;
480 capela 107
481 capela 952 pClient->iTimeout = iTimeout;
482     return LSCP_OK;
483 capela 107 }
484    
485    
486     /**
487     * Get the client transaction timeout interval.
488     *
489     * @param pClient Pointer to client instance structure.
490     *
491     * @returns The current timeout value milliseconds, -1 in case of failure.
492     */
493     int lscp_client_get_timeout ( lscp_client_t *pClient )
494     {
495 capela 952 if (pClient == NULL)
496     return -1;
497 capela 107
498 capela 952 return pClient->iTimeout;
499 capela 107 }
500    
501    
502     //-------------------------------------------------------------------------
503     // Client common protocol functions.
504    
505     /**
506     * Submit a command query line string to the server. The query string
507     * must be cr/lf and null terminated. Besides the return code, the
508     * specific server response to the command request is made available
509     * by the @ref lscp_client_get_result and @ref lscp_client_get_errno
510     * function calls.
511     *
512     * @param pClient Pointer to client instance structure.
513     * @param pszQuery Command request line to be sent to server,
514     * must be cr/lf and null terminated.
515     *
516     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
517     */
518     lscp_status_t lscp_client_query ( lscp_client_t *pClient, const char *pszQuery )
519     {
520 capela 952 lscp_status_t ret;
521 capela 562
522 capela 975 if (pClient == NULL)
523     return LSCP_FAILED;
524    
525 capela 952 // Lock this section up.
526     lscp_mutex_lock(pClient->mutex);
527 capela 107
528 capela 952 // Just make the now guarded call.
529     ret = lscp_client_call(pClient, pszQuery, 0);
530 capela 562
531 capela 952 // Unlock this section down.
532     lscp_mutex_unlock(pClient->mutex);
533 capela 562
534 capela 952 return ret;
535 capela 107 }
536    
537     /**
538     * Get the last received result string. In case of error or warning,
539     * this is the text of the error or warning message issued.
540     *
541     * @param pClient Pointer to client instance structure.
542     *
543     * @returns A pointer to the literal null-terminated result string as
544     * of the last command request.
545     */
546     const char *lscp_client_get_result ( lscp_client_t *pClient )
547     {
548 capela 952 if (pClient == NULL)
549     return NULL;
550 capela 107
551 capela 952 return pClient->pszResult;
552 capela 107 }
553    
554    
555     /**
556     * Get the last error/warning number received.
557     *
558     * @param pClient Pointer to client instance structure.
559     *
560     * @returns The numerical value of the last error or warning
561     * response code received.
562     */
563     int lscp_client_get_errno ( lscp_client_t *pClient )
564     {
565 capela 952 if (pClient == NULL)
566     return -1;
567 capela 107
568 capela 952 return pClient->iErrno;
569 capela 107 }
570    
571    
572     //-------------------------------------------------------------------------
573     // Client registration protocol functions.
574    
575     /**
576 capela 144 * Register frontend for receiving event messages:
577 capela 562 * SUBSCRIBE CHANNEL_COUNT | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
578 capela 144 * | CHANNEL_INFO | MISCELLANEOUS
579 capela 107 *
580     * @param pClient Pointer to client instance structure.
581 capela 144 * @param events Bit-wise OR'ed event flags to subscribe.
582 capela 107 *
583     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
584     */
585 capela 144 lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient, lscp_event_t events )
586 capela 107 {
587 capela 952 lscp_status_t ret = LSCP_FAILED;
588 capela 107
589 capela 952 if (pClient == NULL)
590     return LSCP_FAILED;
591 capela 107
592 capela 952 // Lock this section up.
593     lscp_mutex_lock(pClient->mutex);
594 capela 144
595 capela 952 // If applicable, start the alternate connection...
596     if (pClient->events == LSCP_EVENT_NONE)
597     ret = _lscp_client_evt_connect(pClient);
598 capela 562
599 capela 952 // Send the subscription commands.
600     if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))
601     ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_COUNT);
602     if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
603     ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT);
604     if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
605     ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT);
606     if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
607     ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL);
608     if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
609     ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO);
610     if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
611     ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS);
612 capela 107
613 capela 952 // Unlock this section down.
614     lscp_mutex_unlock(pClient->mutex);
615 capela 132
616 capela 952 return ret;
617 capela 107 }
618    
619    
620     /**
621 capela 144 * Deregister frontend from receiving UDP event messages anymore:
622 capela 562 * SUBSCRIBE CHANNEL_COUNT | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
623 capela 144 * | CHANNEL_INFO | MISCELLANEOUS
624 capela 107 *
625     * @param pClient Pointer to client instance structure.
626 capela 144 * @param events Bit-wise OR'ed event flags to unsubscribe.
627 capela 107 *
628     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
629     */
630 capela 144 lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient, lscp_event_t events )
631 capela 107 {
632 capela 952 lscp_status_t ret = LSCP_OK;
633 capela 107
634 capela 952 if (pClient == NULL)
635     return LSCP_FAILED;
636 capela 107
637 capela 952 // Lock this section up.
638     lscp_mutex_lock(pClient->mutex);
639 capela 132
640 capela 952 // Send the unsubscription commands.
641     if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT))
642     ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_COUNT);
643     if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
644     ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT);
645     if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
646     ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT);
647     if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
648     ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL);
649     if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
650     ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO);
651     if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
652     ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS);
653 capela 107
654 capela 952 // If necessary, close the alternate connection...
655     if (pClient->events == LSCP_EVENT_NONE)
656     lscp_socket_agent_free(&(pClient->evt));
657 capela 144
658 capela 952 // Unlock this section down.
659     lscp_mutex_unlock(pClient->mutex);
660 capela 132
661 capela 952 return ret;
662 capela 107 }
663    
664    
665 capela 213 /**
666     * Getting current subscribed events.
667     *
668     * @param pClient Pointer to client instance structure.
669     *
670     * @returns The current subscrived bit-wise OR'ed event flags.
671     */
672     lscp_event_t lscp_client_get_events ( lscp_client_t *pClient )
673     {
674 capela 952 if (pClient == NULL)
675     return LSCP_EVENT_NONE;
676 capela 213
677 capela 952 return pClient->events;
678 capela 213 }
679    
680    
681 capela 107 //-------------------------------------------------------------------------
682     // Client command protocol functions.
683    
684     /**
685     * Loading an instrument:
686     * LOAD INSTRUMENT <filename> <instr-index> <sampler-channel>
687     *
688     * @param pClient Pointer to client instance structure.
689     * @param pszFileName Instrument file name.
690     * @param iInstrIndex Instrument index number.
691     * @param iSamplerChannel Sampler Channel.
692     *
693     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
694     */
695     lscp_status_t lscp_load_instrument ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )
696     {
697 capela 952 char szQuery[LSCP_BUFSIZ];
698 capela 107
699 capela 952 if (pszFileName == NULL || iSamplerChannel < 0)
700     return LSCP_FAILED;
701 capela 107
702 capela 952 sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
703     return lscp_client_query(pClient, szQuery);
704 capela 107 }
705    
706    
707     /**
708 capela 144 * Loading an instrument in the background (non modal):
709     * LOAD INSTRUMENT NON_MODAL <filename> <instr-index> <sampler-channel>
710     *
711     * @param pClient Pointer to client instance structure.
712     * @param pszFileName Instrument file name.
713     * @param iInstrIndex Instrument index number.
714     * @param iSamplerChannel Sampler Channel.
715     *
716     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
717     */
718     lscp_status_t lscp_load_instrument_non_modal ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )
719     {
720 capela 952 char szQuery[LSCP_BUFSIZ];
721 capela 144
722 capela 952 if (pszFileName == NULL || iSamplerChannel < 0)
723     return LSCP_FAILED;
724 capela 144
725 capela 952 sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
726     return lscp_client_query(pClient, szQuery);
727 capela 144 }
728    
729    
730     /**
731 capela 107 * Loading a sampler engine:
732     * LOAD ENGINE <engine-name> <sampler-channel>
733     *
734     * @param pClient Pointer to client instance structure.
735     * @param pszEngineName Engine name.
736     * @param iSamplerChannel Sampler channel number.
737     *
738     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
739     */
740     lscp_status_t lscp_load_engine ( lscp_client_t *pClient, const char *pszEngineName, int iSamplerChannel )
741     {
742 capela 952 char szQuery[LSCP_BUFSIZ];
743 capela 107
744 capela 952 if (pszEngineName == NULL || iSamplerChannel < 0)
745     return LSCP_FAILED;
746 capela 107
747 capela 952 sprintf(szQuery, "LOAD ENGINE %s %d\r\n", pszEngineName, iSamplerChannel);
748     return lscp_client_query(pClient, szQuery);
749 capela 107 }
750    
751    
752     /**
753     * Current number of sampler channels:
754     * GET CHANNELS
755     *
756     * @param pClient Pointer to client instance structure.
757     *
758     * @returns The current total number of sampler channels on success,
759     * -1 otherwise.
760     */
761     int lscp_get_channels ( lscp_client_t *pClient )
762     {
763 capela 952 int iChannels = -1;
764 capela 132
765 capela 975 if (pClient == NULL)
766     return -1;
767    
768 capela 952 // Lock this section up.
769     lscp_mutex_lock(pClient->mutex);
770 capela 132
771 capela 952 if (lscp_client_call(pClient, "GET CHANNELS\r\n", 0) == LSCP_OK)
772     iChannels = atoi(lscp_client_get_result(pClient));
773 capela 132
774 capela 952 // Unlock this section doen.
775     lscp_mutex_unlock(pClient->mutex);
776 capela 132
777 capela 952 return iChannels;
778 capela 107 }
779    
780    
781     /**
782 capela 125 * List current sampler channels number identifiers:
783     * LIST CHANNELS
784     *
785     * @param pClient Pointer to client instance structure.
786     *
787     * @returns An array of the sampler channels identifiers as positive integers,
788     * terminated with -1 on success, NULL otherwise.
789     */
790     int *lscp_list_channels ( lscp_client_t *pClient )
791     {
792 capela 952 const char *pszSeps = ",";
793 capela 125
794 capela 952 if (pClient == NULL)
795     return NULL;
796 capela 562
797 capela 952 // Lock this section up.
798     lscp_mutex_lock(pClient->mutex);
799 capela 132
800 capela 952 if (pClient->channels) {
801     lscp_isplit_destroy(pClient->channels);
802     pClient->channels = NULL;
803     }
804 capela 125
805 capela 952 if (lscp_client_call(pClient, "LIST CHANNELS\r\n", 0) == LSCP_OK)
806     pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps);
807 capela 125
808 capela 952 // Unlock this section down.
809     lscp_mutex_unlock(pClient->mutex);
810 capela 132
811 capela 952 return pClient->channels;
812 capela 125 }
813    
814    
815     /**
816 capela 107 * Adding a new sampler channel:
817     * ADD CHANNEL
818     *
819     * @param pClient Pointer to client instance structure.
820     *
821     * @returns The new sampler channel number identifier,
822     * or -1 in case of failure.
823     */
824     int lscp_add_channel ( lscp_client_t *pClient )
825     {
826 capela 952 int iSamplerChannel = -1;
827 capela 132
828 capela 975 if (pClient == NULL)
829     return -1;
830    
831 capela 952 // Lock this section up.
832     lscp_mutex_lock(pClient->mutex);
833 capela 132
834 capela 952 if (lscp_client_call(pClient, "ADD CHANNEL\r\n", 0) == LSCP_OK)
835     iSamplerChannel = atoi(lscp_client_get_result(pClient));
836 capela 562
837 capela 952 // Unlock this section down.
838     lscp_mutex_unlock(pClient->mutex);
839 capela 132
840 capela 952 return iSamplerChannel;
841 capela 107 }
842    
843    
844     /**
845     * Removing a sampler channel:
846     * REMOVE CHANNEL <sampler-channel>
847     *
848     * @param pClient Pointer to client instance structure.
849     * @param iSamplerChannel Sampler channel number.
850     *
851     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
852     */
853     lscp_status_t lscp_remove_channel ( lscp_client_t *pClient, int iSamplerChannel )
854     {
855 capela 952 char szQuery[LSCP_BUFSIZ];
856 capela 107
857 capela 952 if (iSamplerChannel < 0)
858     return LSCP_FAILED;
859 capela 107
860 capela 952 sprintf(szQuery, "REMOVE CHANNEL %d\r\n", iSamplerChannel);
861     return lscp_client_query(pClient, szQuery);
862 capela 107 }
863    
864    
865     /**
866 capela 523 * Getting all available engines count:
867 capela 107 * GET AVAILABLE_ENGINES
868     *
869     * @param pClient Pointer to client instance structure.
870     *
871 capela 523 * @returns The current total number of sampler engines on success,
872     * -1 otherwise.
873     */
874     int lscp_get_available_engines ( lscp_client_t *pClient )
875     {
876 capela 952 int iAvailableEngines = -1;
877 capela 523
878 capela 975 if (pClient == NULL)
879     return -1;
880    
881 capela 952 // Lock this section up.
882     lscp_mutex_lock(pClient->mutex);
883 capela 523
884 capela 952 if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n", 0) == LSCP_OK)
885     iAvailableEngines = atoi(lscp_client_get_result(pClient));
886 capela 523
887 capela 952 // Unlock this section down.
888     lscp_mutex_unlock(pClient->mutex);
889 capela 523
890 capela 952 return iAvailableEngines;
891 capela 523 }
892    
893    
894     /**
895     * Getting all available engines:
896     * LIST AVAILABLE_ENGINES
897     *
898     * @param pClient Pointer to client instance structure.
899     *
900 capela 107 * @returns A NULL terminated array of engine name strings,
901     * or NULL in case of failure.
902     */
903 capela 523 const char **lscp_list_available_engines ( lscp_client_t *pClient )
904 capela 107 {
905 capela 952 const char *pszSeps = ",";
906 capela 107
907 capela 975 if (pClient == NULL)
908     return NULL;
909    
910 capela 952 // Lock this section up.
911     lscp_mutex_lock(pClient->mutex);
912 capela 132
913 capela 952 if (pClient->engines) {
914     lscp_szsplit_destroy(pClient->engines);
915     pClient->engines = NULL;
916     }
917 capela 107
918 capela 952 if (lscp_client_call(pClient, "LIST AVAILABLE_ENGINES\r\n", 0) == LSCP_OK)
919     pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);
920 capela 107
921 capela 952 // Unlock this section down.
922     lscp_mutex_unlock(pClient->mutex);
923 capela 132
924 capela 952 return (const char **) pClient->engines;
925 capela 107 }
926    
927    
928     /**
929     * Getting information about an engine.
930     * GET ENGINE INFO <engine-name>
931     *
932     * @param pClient Pointer to client instance structure.
933     * @param pszEngineName Engine name.
934     *
935     * @returns A pointer to a @ref lscp_engine_info_t structure, with all the
936     * information of the given sampler engine, or NULL in case of failure.
937     */
938     lscp_engine_info_t *lscp_get_engine_info ( lscp_client_t *pClient, const char *pszEngineName )
939     {
940 capela 952 lscp_engine_info_t *pEngineInfo;
941     char szQuery[LSCP_BUFSIZ];
942     const char *pszResult;
943     const char *pszSeps = ":";
944     const char *pszCrlf = "\r\n";
945     char *pszToken;
946     char *pch;
947 capela 107
948 capela 975 if (pClient == NULL)
949     return NULL;
950 capela 952 if (pszEngineName == NULL)
951     return NULL;
952 capela 107
953 capela 952 // Lock this section up.
954     lscp_mutex_lock(pClient->mutex);
955 capela 132
956 capela 952 pEngineInfo = &(pClient->engine_info);
957     lscp_engine_info_reset(pEngineInfo);
958 capela 107
959 capela 952 sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName);
960     if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
961     pszResult = lscp_client_get_result(pClient);
962     pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
963     while (pszToken) {
964     if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
965     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
966     if (pszToken)
967     lscp_unquote_dup(&(pEngineInfo->description), &pszToken);
968     }
969     else if (strcasecmp(pszToken, "VERSION") == 0) {
970     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
971     if (pszToken)
972     lscp_unquote_dup(&(pEngineInfo->version), &pszToken);
973     }
974     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
975     }
976     }
977     else pEngineInfo = NULL;
978 capela 562
979 capela 952 // Unlock this section down.
980     lscp_mutex_unlock(pClient->mutex);
981 capela 107
982 capela 952 return pEngineInfo;
983 capela 107 }
984    
985    
986     /**
987     * Getting sampler channel informations:
988     * GET CHANNEL INFO <sampler-channel>
989     *
990     * @param pClient Pointer to client instance structure.
991     * @param iSamplerChannel Sampler channel number.
992     *
993     * @returns A pointer to a @ref lscp_channel_info_t structure, with all the
994     * information of the given sampler channel, or NULL in case of failure.
995     */
996     lscp_channel_info_t *lscp_get_channel_info ( lscp_client_t *pClient, int iSamplerChannel )
997     {
998 capela 952 lscp_channel_info_t *pChannelInfo;
999     char szQuery[LSCP_BUFSIZ];
1000     const char *pszResult;
1001     const char *pszSeps = ":";
1002     const char *pszCrlf = "\r\n";
1003     char *pszToken;
1004     char *pch;
1005 capela 107
1006 capela 975 if (pClient == NULL)
1007     return NULL;
1008 capela 952 if (iSamplerChannel < 0)
1009     return NULL;
1010 capela 107
1011 capela 952 // Lock this section up.
1012     lscp_mutex_lock(pClient->mutex);
1013 capela 562
1014 capela 952 pChannelInfo = &(pClient->channel_info);
1015     lscp_channel_info_reset(pChannelInfo);
1016 capela 107
1017 capela 952 sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);
1018     if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
1019     pszResult = lscp_client_get_result(pClient);
1020     pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1021     while (pszToken) {
1022     if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
1023     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1024     if (pszToken)
1025     lscp_unquote_dup(&(pChannelInfo->engine_name), &pszToken);
1026     }
1027     else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {
1028     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1029     if (pszToken)
1030     pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken));
1031     }
1032     else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) {
1033     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1034     if (pszToken)
1035     pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken));
1036     }
1037     else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {
1038     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1039     if (pszToken) {
1040     if (pChannelInfo->audio_routing)
1041     lscp_szsplit_destroy(pChannelInfo->audio_routing);
1042     pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");
1043     }
1044     }
1045     else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
1046     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1047     if (pszToken)
1048     lscp_unquote_dup(&(pChannelInfo->instrument_file), &pszToken);
1049     }
1050     else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
1051     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1052     if (pszToken)
1053     pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));
1054     }
1055     else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) {
1056     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1057     if (pszToken)
1058     lscp_unquote_dup(&(pChannelInfo->instrument_name), &pszToken);
1059     }
1060     else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) {
1061     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1062     if (pszToken)
1063     pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken));
1064     }
1065     else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) {
1066     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1067     if (pszToken)
1068     pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken));
1069     }
1070     else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) {
1071     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1072     if (pszToken)
1073     pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken));
1074     }
1075     else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {
1076     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1077     if (pszToken) {
1078     pszToken = lscp_ltrim(pszToken);
1079     if (strcasecmp(pszToken, "ALL") == 0)
1080     pChannelInfo->midi_channel = LSCP_MIDI_CHANNEL_ALL;
1081     else
1082     pChannelInfo->midi_channel = atoi(pszToken);
1083     }
1084     }
1085 capela 975 else if (strcasecmp(pszToken, "MIDI_INSTRUMENT_MAP") == 0) {
1086     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1087     if (pszToken) {
1088     pszToken = lscp_ltrim(pszToken);
1089     if (strcasecmp(pszToken, "NONE") == 0)
1090     pChannelInfo->midi_map = LSCP_MIDI_MAP_NONE;
1091     else
1092     if (strcasecmp(pszToken, "DEFAULT") == 0)
1093     pChannelInfo->midi_map = LSCP_MIDI_MAP_DEFAULT;
1094     else
1095     pChannelInfo->midi_map = atoi(pszToken);
1096     }
1097     }
1098 capela 952 else if (strcasecmp(pszToken, "VOLUME") == 0) {
1099     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1100     if (pszToken)
1101     pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken));
1102     }
1103     else if (strcasecmp(pszToken, "MUTE") == 0) {
1104     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1105     if (pszToken)
1106     pChannelInfo->mute = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0);
1107     }
1108     else if (strcasecmp(pszToken, "SOLO") == 0) {
1109     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1110     if (pszToken)
1111     pChannelInfo->solo = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0);
1112     }
1113     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1114     }
1115     }
1116     else pChannelInfo = NULL;
1117 capela 562
1118 capela 952 // Unlock this section up.
1119     lscp_mutex_unlock(pClient->mutex);
1120 capela 107
1121 capela 952 return pChannelInfo;
1122 capela 107 }
1123    
1124    
1125     /**
1126     * Current number of active voices:
1127     * GET CHANNEL VOICE_COUNT <sampler-channel>
1128     *
1129     * @param pClient Pointer to client instance structure.
1130     * @param iSamplerChannel Sampler channel number.
1131     *
1132     * @returns The number of voices currently active, -1 in case of failure.
1133     */
1134     int lscp_get_channel_voice_count ( lscp_client_t *pClient, int iSamplerChannel )
1135     {
1136 capela 952 char szQuery[LSCP_BUFSIZ];
1137     int iVoiceCount = -1;
1138 capela 107
1139 capela 975 if (pClient == NULL)
1140     return -1;
1141 capela 952 if (iSamplerChannel < 0)
1142 capela 975 return -1;
1143 capela 107
1144 capela 952 // Lock this section up.
1145     lscp_mutex_lock(pClient->mutex);
1146 capela 132
1147 capela 952 sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel);
1148     if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
1149     iVoiceCount = atoi(lscp_client_get_result(pClient));
1150 capela 107
1151 capela 952 // Unlock this section down.
1152     lscp_mutex_unlock(pClient->mutex);
1153 capela 132
1154 capela 952 return iVoiceCount;
1155 capela 107 }
1156    
1157    
1158     /**
1159     * Current number of active disk streams:
1160     * GET CHANNEL STREAM_COUNT <sampler-channel>
1161     *
1162 capela 167 * @param pClient Pointer to client instance structure.
1163     * @param iSamplerChannel Sampler channel number.
1164     *
1165 capela 107 * @returns The number of active disk streams on success, -1 otherwise.
1166     */
1167     int lscp_get_channel_stream_count ( lscp_client_t *pClient, int iSamplerChannel )
1168     {
1169 capela 952 char szQuery[LSCP_BUFSIZ];
1170     int iStreamCount = -1;
1171 capela 107
1172 capela 975 if (pClient == NULL)
1173     return -1;
1174 capela 952 if (iSamplerChannel < 0)
1175 capela 975 return -1;
1176 capela 107
1177 capela 952 // Lock this section up.
1178     lscp_mutex_lock(pClient->mutex);
1179 capela 132
1180 capela 952 sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel);
1181     if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
1182     iStreamCount = atoi(lscp_client_get_result(pClient));
1183 capela 107
1184 capela 952 // Unlock this section down.
1185     lscp_mutex_unlock(pClient->mutex);
1186 capela 132
1187 capela 952 return iStreamCount;
1188 capela 107 }
1189    
1190    
1191     /**
1192 capela 167 * Current least usage of active disk streams.
1193     *
1194     * @param pClient Pointer to client instance structure.
1195     * @param iSamplerChannel Sampler channel number.
1196     *
1197     * @returns The usage percentage of the least filled active disk stream
1198     * on success, -1 otherwise.
1199     */
1200     int lscp_get_channel_stream_usage ( lscp_client_t *pClient, int iSamplerChannel )
1201     {
1202 capela 952 char szQuery[LSCP_BUFSIZ];
1203     int iStreamUsage = -1;
1204     const char *pszResult;
1205     const char *pszSeps = "[]%,";
1206     char *pszToken;
1207     char *pch;
1208     int iStream;
1209     int iPercent;
1210 capela 167
1211 capela 975 if (pClient == NULL)
1212     return -1;
1213 capela 952 if (iSamplerChannel < 0)
1214 capela 975 return -1;
1215 capela 167
1216 capela 952 // Lock this section up.
1217     lscp_mutex_lock(pClient->mutex);
1218 capela 167
1219 capela 952 iStream = 0;
1220     sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel);
1221     if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) {
1222     pszResult = lscp_client_get_result(pClient);
1223     pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1224     while (pszToken) {
1225     if (*pszToken) {
1226     // Skip stream id.
1227     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1228     if (pszToken == NULL)
1229     break;
1230     // Get least buffer fill percentage.
1231     iPercent = atol(pszToken);
1232     if (iStreamUsage > iPercent || iStream == 0)
1233     iStreamUsage = iPercent;
1234     iStream++;
1235     }
1236     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1237     }
1238     }
1239 capela 167
1240 capela 952 // Unlock this section down.
1241     lscp_mutex_unlock(pClient->mutex);
1242 capela 167
1243 capela 952 return iStreamUsage;
1244 capela 167 }
1245    
1246    
1247     /**
1248 capela 107 * Current fill state of disk stream buffers:
1249     * GET CHANNEL BUFFER_FILL {BYTES|PERCENTAGE} <sampler-channel>
1250     *
1251     * @param pClient Pointer to client instance structure.
1252     * @param usage_type Usage type to be returned, either
1253     * @ref LSCP_USAGE_BYTES, or
1254     * @ref LSCP_USAGE_PERCENTAGE.
1255     * @param iSamplerChannel Sampler channel number.
1256     *
1257     * @returns A pointer to a @ref lscp_buffer_fill_t structure, with the
1258     * information of the current disk stream buffer fill usage, for the given
1259     * sampler channel, or NULL in case of failure.
1260     */
1261     lscp_buffer_fill_t *lscp_get_channel_buffer_fill ( lscp_client_t *pClient, lscp_usage_t usage_type, int iSamplerChannel )
1262     {
1263 capela 952 lscp_buffer_fill_t *pBufferFill;
1264     char szQuery[LSCP_BUFSIZ];
1265     int iStreamCount;
1266     const char *pszUsageType = (usage_type == LSCP_USAGE_BYTES ? "BYTES" : "PERCENTAGE");
1267     const char *pszResult;
1268     const char *pszSeps = "[]%,";
1269     char *pszToken;
1270     char *pch;
1271     int iStream;
1272 capela 107
1273 capela 952 // Retrieve a channel stream estimation.
1274     iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel);
1275 capela 975 if (iStreamCount < 0)
1276 capela 952 return NULL;
1277 capela 132
1278 capela 952 // Lock this section up.
1279     lscp_mutex_lock(pClient->mutex);
1280 capela 132
1281 capela 952 // Check if we need to reallocate the stream usage array.
1282     if (pClient->iStreamCount != iStreamCount) {
1283     if (pClient->buffer_fill)
1284     free(pClient->buffer_fill);
1285     if (iStreamCount > 0)
1286     pClient->buffer_fill = (lscp_buffer_fill_t *) malloc(iStreamCount * sizeof(lscp_buffer_fill_t));
1287     else
1288     pClient->buffer_fill = NULL;
1289     pClient->iStreamCount = iStreamCount;
1290     }
1291 capela 107
1292 capela 952 // Get buffer fill usage...
1293     pBufferFill = pClient->buffer_fill;
1294     if (pBufferFill && iStreamCount > 0) {
1295     iStream = 0;
1296     pBufferFill = pClient->buffer_fill;
1297     sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel);
1298     if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) {
1299     pszResult = lscp_client_get_result(pClient);
1300     pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1301     while (pszToken && iStream < pClient->iStreamCount) {
1302     if (*pszToken) {
1303     pBufferFill[iStream].stream_id = atol(pszToken);
1304     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1305     if (pszToken == NULL)
1306     break;
1307     pBufferFill[iStream].stream_usage = atol(pszToken);
1308     iStream++;
1309     }
1310     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1311     }
1312     } // Reset the usage, whatever it was before.
1313     else while (iStream < pClient->iStreamCount)
1314     pBufferFill[iStream++].stream_usage = 0;
1315     }
1316 capela 562
1317 capela 952 // Unlock this section down.
1318     lscp_mutex_unlock(pClient->mutex);
1319 capela 107
1320 capela 952 return pBufferFill;
1321 capela 107 }
1322    
1323    
1324     /**
1325     * Setting audio output type:
1326     * SET CHANNEL AUDIO_OUTPUT_TYPE <sampler-channel> <audio-output-type>
1327     *
1328     * @param pClient Pointer to client instance structure.
1329     * @param iSamplerChannel Sampler channel number.
1330     * @param pszAudioDriver Audio output driver type (e.g. "ALSA" or "JACK").
1331 capela 946 *
1332     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1333 capela 107 */
1334     lscp_status_t lscp_set_channel_audio_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszAudioDriver )
1335     {
1336 capela 952 char szQuery[LSCP_BUFSIZ];
1337 capela 107
1338 capela 952 if (iSamplerChannel < 0 || pszAudioDriver == NULL)
1339     return LSCP_FAILED;
1340 capela 107
1341 capela 952 sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_TYPE %d %s\r\n", iSamplerChannel, pszAudioDriver);
1342     return lscp_client_query(pClient, szQuery);
1343 capela 107 }
1344    
1345    
1346     /**
1347 capela 144 * Setting audio output device:
1348     * SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id>
1349     *
1350     * @param pClient Pointer to client instance structure.
1351     * @param iSamplerChannel Sampler channel number.
1352     * @param iAudioDevice Audio output device number identifier.
1353 capela 946 *
1354     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1355 capela 144 */
1356     lscp_status_t lscp_set_channel_audio_device ( lscp_client_t *pClient, int iSamplerChannel, int iAudioDevice )
1357     {
1358 capela 952 char szQuery[LSCP_BUFSIZ];
1359 capela 144
1360 capela 952 if (iSamplerChannel < 0 || iAudioDevice < 0)
1361     return LSCP_FAILED;
1362 capela 144
1363 capela 952 sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice);
1364     return lscp_client_query(pClient, szQuery);
1365 capela 144 }
1366    
1367    
1368     /**
1369 capela 107 * Setting audio output channel:
1370     * SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-output-chan> <audio-input-chan>
1371     *
1372     * @param pClient Pointer to client instance structure.
1373     * @param iSamplerChannel Sampler channel number.
1374     * @param iAudioOut Audio output device channel to be routed from.
1375     * @param iAudioIn Audio output device channel to be routed into.
1376     *
1377     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1378     */
1379     lscp_status_t lscp_set_channel_audio_channel ( lscp_client_t *pClient, int iSamplerChannel, int iAudioOut, int iAudioIn )
1380     {
1381 capela 952 char szQuery[LSCP_BUFSIZ];
1382 capela 107
1383 capela 952 if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)
1384     return LSCP_FAILED;
1385 capela 107
1386 capela 952 sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNEL %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn);
1387     return lscp_client_query(pClient, szQuery);
1388 capela 107 }
1389    
1390    
1391     /**
1392     * Setting MIDI input type:
1393     * SET CHANNEL MIDI_INPUT_TYPE <sampler-channel> <midi-input-type>
1394     *
1395     * @param pClient Pointer to client instance structure.
1396     * @param iSamplerChannel Sampler channel number.
1397     * @param pszMidiDriver MIDI input driver type (e.g. "ALSA").
1398     *
1399     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1400     */
1401     lscp_status_t lscp_set_channel_midi_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszMidiDriver )
1402     {
1403 capela 952 char szQuery[LSCP_BUFSIZ];
1404 capela 107
1405 capela 952 if (iSamplerChannel < 0 || pszMidiDriver == NULL)
1406     return LSCP_FAILED;
1407 capela 107
1408 capela 952 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_TYPE %d %s\r\n", iSamplerChannel, pszMidiDriver);
1409     return lscp_client_query(pClient, szQuery);
1410 capela 107 }
1411    
1412    
1413     /**
1414 capela 144 * Setting MIDI input device:
1415     * SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id>
1416     *
1417     * @param pClient Pointer to client instance structure.
1418     * @param iSamplerChannel Sampler channel number.
1419     * @param iMidiDevice MIDI input device number identifier.
1420 capela 946 *
1421     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1422 capela 144 */
1423     lscp_status_t lscp_set_channel_midi_device ( lscp_client_t *pClient, int iSamplerChannel, int iMidiDevice )
1424     {
1425 capela 952 char szQuery[LSCP_BUFSIZ];
1426 capela 144
1427 capela 952 if (iSamplerChannel < 0 || iMidiDevice < 0)
1428     return LSCP_FAILED;
1429 capela 144
1430 capela 952 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice);
1431     return lscp_client_query(pClient, szQuery);
1432 capela 144 }
1433    
1434    
1435     /**
1436 capela 107 * Setting MIDI input port:
1437     * SET CHANNEL MIDI_INPUT_PORT <sampler-channel> <midi-input-port>
1438     *
1439     * @param pClient Pointer to client instance structure.
1440     * @param iSamplerChannel Sampler channel number.
1441     * @param iMidiPort MIDI input driver virtual port number.
1442     *
1443     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1444     */
1445     lscp_status_t lscp_set_channel_midi_port ( lscp_client_t *pClient, int iSamplerChannel, int iMidiPort )
1446     {
1447 capela 952 char szQuery[LSCP_BUFSIZ];
1448 capela 107
1449 capela 952 if (iSamplerChannel < 0 || iMidiPort < 0)
1450     return LSCP_FAILED;
1451 capela 107
1452 capela 952 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_PORT %d %d\r\n", iSamplerChannel, iMidiPort);
1453     return lscp_client_query(pClient, szQuery);
1454 capela 107 }
1455    
1456    
1457     /**
1458     * Setting MIDI input channel:
1459     * SET CHANNEL MIDI_INPUT_CHANNEL <sampler-channel> <midi-input-chan>
1460     *
1461     * @param pClient Pointer to client instance structure.
1462     * @param iSamplerChannel Sampler channel number.
1463 capela 278 * @param iMidiChannel MIDI channel address number to listen (0-15) or
1464 capela 975 * @ref LSCP_MIDI_CHANNEL_ALL (16) to listen on all channels.
1465 capela 107 *
1466     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1467     */
1468     lscp_status_t lscp_set_channel_midi_channel ( lscp_client_t *pClient, int iSamplerChannel, int iMidiChannel )
1469     {
1470 capela 952 char szQuery[LSCP_BUFSIZ];
1471 capela 107
1472 capela 952 if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)
1473     return LSCP_FAILED;
1474 capela 107
1475 capela 952 if (iMidiChannel == LSCP_MIDI_CHANNEL_ALL)
1476     sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);
1477     else
1478     sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);
1479     return lscp_client_query(pClient, szQuery);
1480 capela 107 }
1481    
1482    
1483     /**
1484 capela 975 * Setting MIDI instrument map:
1485     * SET CHANNEL MIDI_INSTRUMENT_MAP <sampler-channel> <midi-map>
1486     *
1487     * @param pClient Pointer to client instance structure.
1488     * @param iSamplerChannel Sampler channel number.
1489     * @param iMidiMap MIDI instrument map number, or either
1490     * @ref LSCP_MIDI_MAP_NONE or
1491     * @ref LSCP_MIDI_MAP_DEFAULT .
1492     *
1493     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1494     */
1495     lscp_status_t lscp_set_channel_midi_map ( lscp_client_t *pClient, int iSamplerChannel, int iMidiMap )
1496     {
1497     char szQuery[LSCP_BUFSIZ];
1498    
1499     if (iSamplerChannel < 0)
1500     return LSCP_FAILED;
1501    
1502     sprintf(szQuery, "SET CHANNEL MIDI_INSTRUMENT_MAP %d ", iSamplerChannel);
1503     if (iMidiMap == LSCP_MIDI_MAP_NONE)
1504     strcat(szQuery , "NONE");
1505     else
1506     if (iMidiMap == LSCP_MIDI_MAP_DEFAULT)
1507     strcat(szQuery , "DEFAULT");
1508     else
1509     sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
1510    
1511     strcat(szQuery, "\r\n");
1512    
1513     return lscp_client_query(pClient, szQuery);
1514     }
1515    
1516    
1517     /**
1518 capela 107 * Setting channel volume:
1519     * SET CHANNEL VOLUME <sampler-channel> <volume>
1520     *
1521     * @param pClient Pointer to client instance structure.
1522     * @param iSamplerChannel Sampler channel number.
1523     * @param fVolume Sampler channel volume as a positive floating point
1524     * number, where a value less than 1.0 for attenuation,
1525     * and greater than 1.0 for amplification.
1526     *
1527     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1528     */
1529     lscp_status_t lscp_set_channel_volume ( lscp_client_t *pClient, int iSamplerChannel, float fVolume )
1530     {
1531 capela 952 char szQuery[LSCP_BUFSIZ];
1532 capela 107
1533 capela 952 if (iSamplerChannel < 0 || fVolume < 0.0)
1534     return LSCP_FAILED;
1535 capela 107
1536 capela 952 sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume);
1537     return lscp_client_query(pClient, szQuery);
1538 capela 107 }
1539    
1540    
1541     /**
1542 capela 735 * Muting a sampler channel:
1543     * SET CHANNEL MUTE <sampler-channel> <mute>
1544     *
1545     * @param pClient Pointer to client instance structure.
1546     * @param iSamplerChannel Sampler channel number.
1547     * @param iMute Sampler channel mute state as a boolean value,
1548     * either 1 (one) to mute the channel or 0 (zero)
1549     * to unmute the channel.
1550     *
1551     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1552     */
1553     lscp_status_t lscp_set_channel_mute ( lscp_client_t *pClient, int iSamplerChannel, int iMute )
1554     {
1555 capela 952 char szQuery[LSCP_BUFSIZ];
1556 capela 735
1557 capela 952 if (iSamplerChannel < 0 || iMute < 0 || iMute > 1)
1558     return LSCP_FAILED;
1559 capela 735
1560 capela 952 sprintf(szQuery, "SET CHANNEL MUTE %d %d\r\n", iSamplerChannel, iMute);
1561     return lscp_client_query(pClient, szQuery);
1562 capela 735 }
1563    
1564    
1565     /**
1566     * Soloing a sampler channel:
1567     * SET CHANNEL SOLO <sampler-channel> <solo>
1568     *
1569     * @param pClient Pointer to client instance structure.
1570     * @param iSamplerChannel Sampler channel number.
1571     * @param iSolo Sampler channel solo state as a boolean value,
1572     * either 1 (one) to solo the channel or 0 (zero)
1573     * to unsolo the channel.
1574     *
1575     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1576     */
1577     lscp_status_t lscp_set_channel_solo ( lscp_client_t *pClient, int iSamplerChannel, int iSolo )
1578     {
1579 capela 952 char szQuery[LSCP_BUFSIZ];
1580 capela 735
1581 capela 952 if (iSamplerChannel < 0 || iSolo < 0 || iSolo > 1)
1582     return LSCP_FAILED;
1583 capela 735
1584 capela 952 sprintf(szQuery, "SET CHANNEL SOLO %d %d\r\n", iSamplerChannel, iSolo);
1585     return lscp_client_query(pClient, szQuery);
1586 capela 735 }
1587    
1588    
1589     /**
1590 capela 107 * Resetting a sampler channel:
1591     * RESET CHANNEL <sampler-channel>
1592     *
1593     * @param pClient Pointer to client instance structure.
1594     * @param iSamplerChannel Sampler channel number.
1595     *
1596     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1597     */
1598     lscp_status_t lscp_reset_channel ( lscp_client_t *pClient, int iSamplerChannel )
1599     {
1600 capela 952 char szQuery[LSCP_BUFSIZ];
1601 capela 107
1602 capela 952 if (iSamplerChannel < 0)
1603     return LSCP_FAILED;
1604 capela 107
1605 capela 952 sprintf(szQuery, "RESET CHANNEL %d\r\n", iSamplerChannel);
1606     return lscp_client_query(pClient, szQuery);
1607 capela 107 }
1608    
1609    
1610 capela 213 /**
1611     * Resetting the sampler:
1612     * RESET
1613     *
1614     * @param pClient Pointer to client instance structure.
1615     *
1616     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1617     */
1618     lscp_status_t lscp_reset_sampler ( lscp_client_t *pClient )
1619     {
1620 capela 952 return lscp_client_query(pClient, "RESET\r\n");
1621 capela 213 }
1622    
1623    
1624 capela 564 /**
1625     * Getting information about the server.
1626     * GET SERVER INFO
1627     *
1628     * @param pClient Pointer to client instance structure.
1629     *
1630     * @returns A pointer to a @ref lscp_server_info_t structure, with all the
1631     * information of the current connected server, or NULL in case of failure.
1632     */
1633     lscp_server_info_t *lscp_get_server_info ( lscp_client_t *pClient )
1634     {
1635 capela 952 lscp_server_info_t *pServerInfo;
1636     const char *pszResult;
1637     const char *pszSeps = ":";
1638     const char *pszCrlf = "\r\n";
1639     char *pszToken;
1640     char *pch;
1641 capela 564
1642 capela 975 if (pClient == NULL)
1643     return NULL;
1644    
1645 capela 952 // Lock this section up.
1646     lscp_mutex_lock(pClient->mutex);
1647 capela 564
1648 capela 952 pServerInfo = &(pClient->server_info);
1649     lscp_server_info_reset(pServerInfo);
1650 capela 564
1651 capela 952 if (lscp_client_call(pClient, "GET SERVER INFO\r\n", 1) == LSCP_OK) {
1652     pszResult = lscp_client_get_result(pClient);
1653     pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1654     while (pszToken) {
1655     if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
1656     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1657     if (pszToken)
1658     lscp_unquote_dup(&(pServerInfo->description), &pszToken);
1659     }
1660     else if (strcasecmp(pszToken, "VERSION") == 0) {
1661     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1662     if (pszToken)
1663     lscp_unquote_dup(&(pServerInfo->version), &pszToken);
1664     }
1665 capela 977 else if (strcasecmp(pszToken, "PROTOCOL_VERSION") == 0) {
1666     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1667     if (pszToken)
1668     lscp_unquote_dup(&(pServerInfo->protocol_version), &pszToken);
1669     }
1670 capela 952 pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1671     }
1672     }
1673     else pServerInfo = NULL;
1674 capela 564
1675 capela 952 // Unlock this section down.
1676     lscp_mutex_unlock(pClient->mutex);
1677 capela 564
1678 capela 952 return pServerInfo;
1679 capela 564 }
1680    
1681    
1682 capela 946 /**
1683     * Current total number of active voices:
1684     * GET TOTAL_VOICE_COUNT
1685     *
1686     * @param pClient Pointer to client instance structure.
1687     *
1688     * @returns The total number of voices currently active,
1689     * -1 in case of failure.
1690     */
1691     int lscp_get_total_voice_count ( lscp_client_t *pClient )
1692     {
1693 capela 952 int iVoiceCount = -1;
1694 capela 946
1695 capela 975 if (pClient == NULL)
1696     return -1;
1697    
1698 capela 952 // Lock this section up.
1699     lscp_mutex_lock(pClient->mutex);
1700 capela 946
1701 capela 952 if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT\r\n", 0) == LSCP_OK)
1702     iVoiceCount = atoi(lscp_client_get_result(pClient));
1703 capela 946
1704 capela 952 // Unlock this section down.
1705     lscp_mutex_unlock(pClient->mutex);
1706 capela 946
1707 capela 952 return iVoiceCount;
1708 capela 946 }
1709    
1710    
1711     /**
1712     * Maximum amount of active voices:
1713     * GET TOTAL_VOICE_COUNT_MAX
1714     *
1715     * @param pClient Pointer to client instance structure.
1716     *
1717     * @returns The maximum amount of voices currently active,
1718     * -1 in case of failure.
1719     */
1720     int lscp_get_total_voice_count_max ( lscp_client_t *pClient )
1721     {
1722 capela 952 int iVoiceCount = -1;
1723 capela 946
1724 capela 975 if (pClient == NULL)
1725     return -1;
1726    
1727 capela 952 // Lock this section up.
1728     lscp_mutex_lock(pClient->mutex);
1729 capela 946
1730 capela 952 if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT_MAX\r\n", 0) == LSCP_OK)
1731     iVoiceCount = atoi(lscp_client_get_result(pClient));
1732 capela 946
1733 capela 952 // Unlock this section down.
1734     lscp_mutex_unlock(pClient->mutex);
1735 capela 946
1736 capela 952 return iVoiceCount;
1737 capela 946 }
1738    
1739    
1740     /**
1741 capela 975 * Create a new MIDI instrument map:
1742     * ADD MIDI_INSTRUMENT_MAP [<name>]
1743     *
1744     * @param pClient Pointer to client instance structure.
1745     * @param pszMapName MIDI instrument map name (optional)
1746     *
1747     * @returns The new MIDI instrument map number identifier,
1748     * or -1 in case of failure.
1749     */
1750     int lscp_add_midi_instrument_map ( lscp_client_t *pClient, const char *pszMapName )
1751     {
1752     int iMidiMap = -1;
1753     char szQuery[LSCP_BUFSIZ];
1754    
1755     if (pClient == NULL)
1756     return -1;
1757    
1758     // Lock this section up.
1759     lscp_mutex_lock(pClient->mutex);
1760    
1761     strcpy(szQuery, "ADD MIDI_INSTRUMENT_MAP");
1762    
1763     if (pszMapName)
1764     sprintf(szQuery + strlen(szQuery), " '%s'", pszMapName);
1765    
1766     strcat(szQuery, "\r\n");
1767    
1768     if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
1769     iMidiMap = atoi(lscp_client_get_result(pClient));
1770    
1771     // Unlock this section down.
1772     lscp_mutex_unlock(pClient->mutex);
1773    
1774     return iMidiMap;
1775     }
1776    
1777    
1778     /**
1779     * Delete one particular or all MIDI instrument maps:
1780     * REMOVE MIDI_INSTRUMENT_MAP <midi-map>
1781     *
1782     * @param pClient Pointer to client instance structure.
1783     * @param iMidiMap MIDI instrument map number.
1784     *
1785     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1786     */
1787     lscp_status_t lscp_remove_midi_instrument_map ( lscp_client_t *pClient, int iMidiMap )
1788     {
1789     char szQuery[LSCP_BUFSIZ];
1790    
1791     if (iMidiMap < 0)
1792     return LSCP_FAILED;
1793    
1794     sprintf(szQuery, "REMOVE MIDI_INSTRUMENT_MAP %d\r\n", iMidiMap);
1795    
1796     return lscp_client_query(pClient, szQuery);
1797     }
1798    
1799    
1800     /**
1801     * Get amount of existing MIDI instrument maps:
1802     * GET MIDI_INSTRUMENT_MAPS
1803     *
1804     * @param pClient Pointer to client instance structure.
1805     *
1806     * @returns The current total number of MIDI instrument maps
1807     * on success, -1 otherwise.
1808     */
1809     int lscp_get_midi_instrument_maps ( lscp_client_t *pClient )
1810     {
1811     int iMidiMaps = -1;
1812    
1813     if (pClient == NULL)
1814     return -1;
1815    
1816     // Lock this section up.
1817     lscp_mutex_lock(pClient->mutex);
1818    
1819     if (lscp_client_call(pClient, "GET MIDI_INSTRUMENT_MAPS\r\n", 0) == LSCP_OK)
1820     iMidiMaps = atoi(lscp_client_get_result(pClient));
1821    
1822     // Unlock this section doen.
1823     lscp_mutex_unlock(pClient->mutex);
1824    
1825     return iMidiMaps;
1826     }
1827    
1828    
1829     /**
1830     * Getting all created MIDI instrument maps:
1831     * LIST MIDI_INSTRUMENT_MAPS
1832     *
1833     * @param pClient Pointer to client instance structure.
1834     *
1835     * @returns An array of the MIDI instrument map identifiers as positive
1836     * integers, terminated with -1 on success, NULL otherwise.
1837     */
1838     int *lscp_list_midi_instrument_maps ( lscp_client_t *pClient )
1839     {
1840     const char *pszSeps = ",";
1841    
1842     if (pClient == NULL)
1843     return NULL;
1844    
1845     // Lock this section up.
1846     lscp_mutex_lock(pClient->mutex);
1847    
1848     if (pClient->midi_maps) {
1849     lscp_isplit_destroy(pClient->midi_maps);
1850     pClient->midi_maps = NULL;
1851     }
1852    
1853     if (lscp_client_call(pClient, "LIST MIDI_INSTRUMENT_MAPS\r\n", 0) == LSCP_OK)
1854     pClient->midi_maps = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps);
1855    
1856     // Unlock this section down.
1857     lscp_mutex_unlock(pClient->mutex);
1858    
1859     return pClient->midi_maps;
1860     }
1861    
1862    
1863     /**
1864     * Getting a MIDI instrument map name:
1865     * GET MIDI_INSTRUMENT_MAP INFO <midi-map>
1866     *
1867     * @param pClient Pointer to client instance structure.
1868     * @param iMidiMap MIDI instrument map number.
1869     *
1870     * @returns The MIDI instrument map name on success, NULL on failure.
1871     */
1872     const char *lscp_get_midi_instrument_map_name ( lscp_client_t *pClient, int iMidiMap )
1873     {
1874     char szQuery[LSCP_BUFSIZ];
1875     const char *pszResult;
1876     const char *pszSeps = ":";
1877     const char *pszCrlf = "\r\n";
1878     char *pszToken;
1879     char *pch;
1880    
1881     if (pClient == NULL)
1882     return NULL;
1883     if (iMidiMap < 0)
1884     return NULL;
1885    
1886     // Lock this section up.
1887     lscp_mutex_lock(pClient->mutex);
1888    
1889     if (pClient->midi_map_name) {
1890     free(pClient->midi_map_name);
1891     pClient->midi_map_name = NULL;
1892     }
1893    
1894     sprintf(szQuery, "GET MIDI_INSTRUMENT_MAP INFO %d\r\n", iMidiMap);
1895 capela 978 if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) {
1896 capela 975 pszResult = lscp_client_get_result(pClient);
1897     pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1898     while (pszToken) {
1899     if (strcasecmp(pszToken, "NAME") == 0) {
1900     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
1901     if (pszToken)
1902     lscp_unquote_dup(&(pClient->midi_map_name), &pszToken);
1903     }
1904     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1905     }
1906     }
1907    
1908     // Unlock this section down.
1909     lscp_mutex_unlock(pClient->mutex);
1910    
1911     return pClient->midi_map_name;
1912     }
1913    
1914    
1915     /**
1916     * Renaming a MIDI instrument map:
1917     * SET MIDI_INSTRUMENT_MAP NAME <midi-map> <map-name>
1918     *
1919     * @param pClient Pointer to client instance structure.
1920     * @param iMidiMap MIDI instrument map number.
1921     * @param pszMapName MIDI instrument map name.
1922     *
1923     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1924     */
1925     lscp_status_t lscp_set_midi_instrument_map_name ( lscp_client_t *pClient, int iMidiMap, const char *pszMapName )
1926     {
1927     char szQuery[LSCP_BUFSIZ];
1928    
1929     if (iMidiMap < 0)
1930     return LSCP_FAILED;
1931     if (pszMapName == NULL)
1932     return LSCP_FAILED;
1933    
1934     sprintf(szQuery, "SET MIDI_INSTRUMENT_MAP NAME %d '%s'\r\n",
1935     iMidiMap, pszMapName);
1936    
1937     return lscp_client_query(pClient, szQuery);
1938     }
1939    
1940    
1941     /**
1942 capela 946 * Create or replace a MIDI instrumnet map entry:
1943 capela 975 * MAP MIDI_INSTRUMENT <midi-map> <midi-bank> <midi-prog>
1944     * <engine-name> <filename> <instr-index> <volume> [<load-mode> [<name>]}
1945 capela 946 *
1946     * @param pClient Pointer to client instance structure.
1947     * @param pMidiInstr MIDI instrument bank and program parameter key.
1948     * @param pszEngineName Engine name.
1949     * @param pszFileName Instrument file name.
1950     * @param iInstrIndex Instrument index number.
1951     * @param fVolume Reflects the master volume of the instrument as
1952     * a positive floating point number, where a value
1953     * less than 1.0 for attenuation, and greater than
1954     * 1.0 for amplification.
1955     * @param load_mode Instrument load life-time strategy, either
1956     * @ref LSCP_LOAD_DEFAULT, or
1957     * @ref LSCP_LOAD_ON_DEMAND, or
1958     * @ref LSCP_LOAD_ON_DEMAND_HOLD, or
1959     * @ref LSCP_LOAD_PERSISTENT.
1960 capela 975 * @param pszName Instrument custom name for the map entry (optional).
1961 capela 946 *
1962     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1963     */
1964     lscp_status_t lscp_map_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr, const char *pszEngineName, const char *pszFileName, int iInstrIndex, float fVolume, lscp_load_mode_t load_mode, const char *pszName )
1965     {
1966 capela 952 char szQuery[LSCP_BUFSIZ];
1967 capela 963
1968 capela 975 if (pMidiInstr->map < 0)
1969 capela 952 return LSCP_FAILED;
1970 capela 975 if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383)
1971 capela 952 return LSCP_FAILED;
1972 capela 975 if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127)
1973 capela 952 return LSCP_FAILED;
1974     if (pszEngineName == NULL || pszFileName == NULL)
1975     return LSCP_FAILED;
1976 capela 963
1977 capela 952 if (fVolume < 0.0f)
1978     fVolume = 1.0f;
1979 capela 963
1980 capela 952 sprintf(szQuery, "MAP MIDI_INSTRUMENT %d %d %d %s '%s' %d %g",
1981 capela 975 pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog,
1982 capela 952 pszEngineName, pszFileName, iInstrIndex, fVolume);
1983 capela 963
1984 capela 952 switch (load_mode) {
1985     case LSCP_LOAD_PERSISTENT:
1986     strcat(szQuery, " PERSISTENT");
1987     break;
1988     case LSCP_LOAD_ON_DEMAND_HOLD:
1989     strcat(szQuery, " ON_DEMAND_HOLD");
1990     break;
1991     case LSCP_LOAD_ON_DEMAND:
1992 capela 963 strcat(szQuery, " ON_DEMAND");
1993 capela 952 break;
1994     case LSCP_LOAD_DEFAULT:
1995     default:
1996     break;
1997     }
1998 capela 963
1999 capela 952 if (pszName)
2000     sprintf(szQuery + strlen(szQuery), " '%s'", pszName);
2001 capela 963
2002 capela 952 strcat(szQuery, "\r\n");
2003 capela 963
2004 capela 952 return lscp_client_query(pClient, szQuery);
2005 capela 946 }
2006    
2007    
2008     /**
2009     * Remove an entry from the MIDI instrument map:
2010 capela 975 * UNMAP MIDI_INSTRUMENT <midi-map> <midi-bank> <midi-prog>
2011 capela 946 *
2012     * @param pClient Pointer to client instance structure.
2013     * @param pMidiInstr MIDI instrument bank and program parameter key.
2014     *
2015     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
2016     */
2017     lscp_status_t lscp_unmap_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr )
2018     {
2019 capela 952 char szQuery[LSCP_BUFSIZ];
2020 capela 946
2021 capela 975 if (pMidiInstr->map < 0)
2022 capela 952 return LSCP_FAILED;
2023 capela 975 if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383)
2024 capela 952 return LSCP_FAILED;
2025 capela 975 if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127)
2026 capela 952 return LSCP_FAILED;
2027 capela 946
2028 capela 952 sprintf(szQuery, "UNMAP MIDI_INSTRUMENT %d %d %d\r\n",
2029 capela 975 pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog);
2030 capela 946
2031 capela 952 return lscp_client_query(pClient, szQuery);
2032 capela 946 }
2033    
2034    
2035     /**
2036     * Get the total count of MIDI instrument map entries:
2037 capela 975 * GET MIDI_INSTRUMENTS ALL|<midi-map>
2038 capela 946 *
2039     * @param pClient Pointer to client instance structure.
2040 capela 975 * @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL .
2041 capela 946 *
2042     * @returns The current total number of MIDI instrument map entries
2043     * on success, -1 otherwise.
2044     */
2045 capela 975 int lscp_get_midi_instruments ( lscp_client_t *pClient, int iMidiMap )
2046 capela 946 {
2047 capela 952 int iInstruments = -1;
2048 capela 975 char szQuery[LSCP_BUFSIZ];
2049 capela 946
2050 capela 975 if (pClient == NULL)
2051     return -1;
2052    
2053 capela 952 // Lock this section up.
2054     lscp_mutex_lock(pClient->mutex);
2055 capela 946
2056 capela 975 strcpy(szQuery, "GET MIDI_INSTRUMENTS ");
2057    
2058     if (iMidiMap < 0)
2059     strcat(szQuery, "ALL");
2060     else
2061     sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
2062    
2063     strcat(szQuery, "\r\n");
2064    
2065     if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
2066 capela 952 iInstruments = atoi(lscp_client_get_result(pClient));
2067 capela 946
2068 capela 952 // Unlock this section down.
2069     lscp_mutex_unlock(pClient->mutex);
2070 capela 946
2071 capela 952 return iInstruments;
2072 capela 946 }
2073    
2074    
2075     /**
2076 capela 948 * Getting indeces of all MIDI instrument map entries:
2077 capela 975 * LIST MIDI_INSTRUMENTS ALL|<midi-map>
2078 capela 948 *
2079     * @param pClient Pointer to client instance structure.
2080 capela 975 * @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL .
2081 capela 948 *
2082     * @returns An array of @ref lscp_midi_instrument_t, terminated with the
2083     * {-1,-1,-1} triplet, NULL otherwise.
2084     */
2085 capela 975 lscp_midi_instrument_t *lscp_list_midi_instruments ( lscp_client_t *pClient, int iMidiMap )
2086 capela 948 {
2087 capela 975 char szQuery[LSCP_BUFSIZ];
2088    
2089 capela 952 if (pClient == NULL)
2090     return NULL;
2091 capela 948
2092 capela 952 // Lock this section up.
2093     lscp_mutex_lock(pClient->mutex);
2094 capela 948
2095 capela 952 if (pClient->midi_instruments) {
2096     lscp_midi_instruments_destroy(pClient->midi_instruments);
2097     pClient->midi_instruments = NULL;
2098     }
2099 capela 948
2100 capela 975 strcpy(szQuery, "LIST MIDI_INSTRUMENTS ");
2101    
2102     if (iMidiMap < 0)
2103     strcat(szQuery, "ALL");
2104     else
2105     sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
2106    
2107     strcat(szQuery, "\r\n");
2108    
2109     if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK)
2110 capela 952 pClient->midi_instruments = lscp_midi_instruments_create(lscp_client_get_result(pClient));
2111 capela 948
2112 capela 952 // Unlock this section down.
2113     lscp_mutex_unlock(pClient->mutex);
2114 capela 948
2115 capela 952 return pClient->midi_instruments;
2116 capela 948 }
2117    
2118    
2119     /**
2120 capela 946 * Getting information about a MIDI instrument map entry:
2121 capela 975 * GET MIDI_INSTRUMENT INFO <midi-map> <midi-bank> <midi-prog>
2122 capela 946 *
2123     * @param pClient Pointer to client instance structure.
2124     * @param pMidiInstr MIDI instrument bank and program parameter key.
2125     *
2126     * @returns A pointer to a @ref lscp_midi_instrument_info_t structure,
2127     * with all the information of the given MIDI instrument map entry,
2128     * or NULL in case of failure.
2129     */
2130     lscp_midi_instrument_info_t *lscp_get_midi_instrument_info ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr )
2131     {
2132 capela 952 lscp_midi_instrument_info_t *pInstrInfo;
2133     char szQuery[LSCP_BUFSIZ];
2134     const char *pszResult;
2135     const char *pszSeps = ":";
2136     const char *pszCrlf = "\r\n";
2137     char *pszToken;
2138     char *pch;
2139 capela 963
2140 capela 975 if (pClient == NULL)
2141 capela 952 return NULL;
2142 capela 975 if (pMidiInstr->map < 0)
2143 capela 952 return NULL;
2144 capela 975 if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383)
2145 capela 952 return NULL;
2146 capela 975 if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127)
2147     return NULL;
2148 capela 963
2149 capela 952 // Lock this section up.
2150     lscp_mutex_lock(pClient->mutex);
2151    
2152     pInstrInfo = &(pClient->midi_instrument_info);
2153     lscp_midi_instrument_info_reset(pInstrInfo);
2154 capela 963
2155 capela 952 sprintf(szQuery, "GET MIDI_INSTRUMENT INFO %d %d %d\r\n",
2156 capela 975 pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog);
2157 capela 952 if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) {
2158     pszResult = lscp_client_get_result(pClient);
2159     pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
2160     while (pszToken) {
2161     if (strcasecmp(pszToken, "NAME") == 0) {
2162     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2163     if (pszToken)
2164     lscp_unquote_dup(&(pInstrInfo->name), &pszToken);
2165     }
2166     else if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
2167     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2168     if (pszToken)
2169     lscp_unquote_dup(&(pInstrInfo->engine_name), &pszToken);
2170     }
2171     else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
2172     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2173     if (pszToken)
2174     lscp_unquote_dup(&(pInstrInfo->instrument_file), &pszToken);
2175     }
2176     else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
2177     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2178     if (pszToken)
2179     pInstrInfo->instrument_nr = atoi(lscp_ltrim(pszToken));
2180     }
2181     else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) {
2182     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2183     if (pszToken)
2184     lscp_unquote_dup(&(pInstrInfo->instrument_name), &pszToken);
2185     }
2186     else if (strcasecmp(pszToken, "LOAD_MODE") == 0) {
2187     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2188     if (pszToken) {
2189     pszToken = lscp_ltrim(pszToken);
2190     if (strcasecmp(pszToken, "ON_DEMAND") == 0)
2191     pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND;
2192     else
2193     if (strcasecmp(pszToken, "ON_DEMAND_HOLD") == 0)
2194     pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND_HOLD;
2195     else
2196     if (strcasecmp(pszToken, "PERSISTENT") == 0)
2197     pInstrInfo->load_mode = LSCP_LOAD_PERSISTENT;
2198     else
2199     pInstrInfo->load_mode = LSCP_LOAD_DEFAULT;
2200     }
2201     }
2202     else if (strcasecmp(pszToken, "VOLUME") == 0) {
2203     pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
2204     if (pszToken)
2205     pInstrInfo->volume = (float) atof(lscp_ltrim(pszToken));
2206     }
2207     pszToken = lscp_strtok(NULL, pszSeps, &(pch));
2208     }
2209     }
2210     else pInstrInfo = NULL;
2211 capela 963
2212 capela 952 // Unlock this section down.
2213     lscp_mutex_unlock(pClient->mutex);
2214 capela 963
2215 capela 952 return pInstrInfo;
2216 capela 946 }
2217    
2218    
2219     /**
2220     * Clear the MIDI instrumnet map:
2221 capela 975 * CLEAR MIDI_INSTRUMENTS ALL|<midi-map>
2222 capela 946 *
2223 capela 975 * @param pClient Pointer to client instance structure.
2224     * @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL .
2225 capela 946 *
2226     * @returns LSCP_OK on success, LSCP_FAILED otherwise.
2227     */
2228 capela 975 lscp_status_t lscp_clear_midi_instruments ( lscp_client_t *pClient, int iMidiMap )
2229 capela 946 {
2230 capela 975 char szQuery[LSCP_BUFSIZ];
2231    
2232     strcpy(szQuery, "CLEAR MIDI_INSTRUMENTS ");
2233    
2234     if (iMidiMap < 0)
2235     strcat(szQuery, "ALL");
2236     else
2237     sprintf(szQuery + strlen(szQuery), "%d", iMidiMap);
2238    
2239     strcat(szQuery, "\r\n");
2240    
2241     return lscp_client_query(pClient, szQuery);
2242 capela 946 }
2243    
2244    
2245 capela 107 // end of client.c

  ViewVC Help
Powered by ViewVC