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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 179 - (show annotations) (download)
Tue Jul 6 16:24:41 2004 UTC (15 years, 8 months ago) by capela
File MIME type: text/plain
File size: 45953 byte(s)
Some minor and probable memory leak resolutions.

1 // client.c
2 //
3 /****************************************************************************
4 liblscp - LinuxSampler Control Protocol API
5 Copyright (C) 2004, rncbc aka Rui Nuno Capela. All rights reserved.
6
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 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
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 static void _lscp_client_evt_proc (void *pvClient);
32
33 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
36
37 //-------------------------------------------------------------------------
38 // Event service (datagram oriented).
39
40 static void _lscp_client_evt_proc ( void *pvClient )
41 {
42 lscp_client_t *pClient = (lscp_client_t *) pvClient;
43
44 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
50 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
58 #ifdef DEBUG
59 fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n");
60 #endif
61
62 while (pClient->evt.iState) {
63
64 // 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
70 // 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
79 // 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_call: select");
115 pClient->evt.iState = 0;
116 }
117
118 // Finally, always signal the event.
119 lscp_cond_signal(pClient->cond);
120 }
121
122 #ifdef DEBUG
123 fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n");
124 #endif
125 }
126
127
128 //-------------------------------------------------------------------------
129 // 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 lscp_socket_t sock;
135 struct sockaddr_in addr;
136 int cAddr;
137 #if defined(WIN32)
138 int iSockOpt = (-1);
139 #endif
140
141 // 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
148 #if defined(WIN32)
149 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 #endif
152
153 #ifdef DEBUG
154 lscp_socket_getopts("_lscp_client_evt_connect:", sock);
155 #endif
156
157 // Use same address of the command connection.
158 cAddr = sizeof(struct sockaddr_in);
159 memmove((char *) &addr, &(pClient->cmd.addr), cAddr);
160
161 // 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
168 // Set our socket agent struct...
169 lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr);
170
171 // And finally the service thread...
172 return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0);
173 }
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 const char *pszEvent;
180 char szQuery[LSCP_BUFSIZ];
181 int cchQuery;
182
183 if (pClient == NULL)
184 return LSCP_FAILED;
185
186 // Which (single) event?
187 pszEvent = lscp_event_to_text(event);
188 if (pszEvent == NULL)
189 return LSCP_FAILED;
190
191 // 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
199 // Wait on response.
200 lscp_cond_wait(pClient->cond, pClient->mutex);
201
202 // Update as naively as we can...
203 if (iSubscribe)
204 pClient->events |= event;
205 else
206 pClient->events &= ~event;
207
208 return LSCP_OK;
209 }
210
211
212 //-------------------------------------------------------------------------
213 // 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 lscp_client_t *pClient;
246 struct hostent *pHost;
247 lscp_socket_t sock;
248 struct sockaddr_in addr;
249 int cAddr;
250 #if defined(WIN32)
251 int iSockOpt = (-1);
252 #endif
253
254 if (pfnCallback == NULL) {
255 fprintf(stderr, "lscp_client_create: Invalid client callback function.\n");
256 return NULL;
257 }
258
259 pHost = gethostbyname(pszHost);
260 if (pHost == NULL) {
261 lscp_socket_herror("lscp_client_create: gethostbyname");
262 return NULL;
263 }
264
265 // Allocate client descriptor...
266
267 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
274 pClient->pfnCallback = pfnCallback;
275 pClient->pvData = pvData;
276
277 #ifdef DEBUG
278 fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort);
279 #endif
280
281 // Prepare the command connection socket...
282
283 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
290 #if defined(WIN32)
291 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 #endif
294
295 #ifdef DEBUG
296 lscp_socket_getopts("lscp_client_create: cmd", sock);
297 #endif
298
299 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
305 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
312 // Initialize the command socket agent struct...
313 lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr);
314
315 #ifdef DEBUG
316 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 #endif
318
319 // 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 lscp_driver_info_init(&(pClient->audio_driver_info));
331 lscp_driver_info_init(&(pClient->midi_driver_info));
332 lscp_device_info_init(&(pClient->audio_device_info));
333 lscp_device_info_init(&(pClient->midi_device_info));
334 lscp_param_info_init(&(pClient->audio_param_info));
335 lscp_param_info_init(&(pClient->midi_param_info));
336 lscp_device_port_info_init(&(pClient->audio_channel_info));
337 lscp_device_port_info_init(&(pClient->midi_port_info));
338 lscp_param_info_init(&(pClient->audio_channel_param_info));
339 lscp_param_info_init(&(pClient->midi_port_param_info));
340 lscp_engine_info_init(&(pClient->engine_info));
341 lscp_channel_info_init(&(pClient->channel_info));
342 // Initialize error stuff.
343 pClient->pszResult = NULL;
344 pClient->iErrno = -1;
345 // Stream usage stuff.
346 pClient->buffer_fill = NULL;
347 pClient->iStreamCount = 0;
348 // Default timeout value.
349 pClient->iTimeout = LSCP_TIMEOUT_MSECS;
350
351 // Initialize the transaction mutex.
352 lscp_mutex_init(pClient->mutex);
353 lscp_cond_init(pClient->cond);
354
355 // Finally we've some success...
356 return pClient;
357 }
358
359
360 /**
361 * Wait for a client instance to terminate graciously.
362 *
363 * @param pClient Pointer to client instance structure.
364 */
365 lscp_status_t lscp_client_join ( lscp_client_t *pClient )
366 {
367 if (pClient == NULL)
368 return LSCP_FAILED;
369
370 #ifdef DEBUG
371 fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient);
372 #endif
373
374 // lscp_socket_agent_join(&(pClient->evt));
375 lscp_socket_agent_join(&(pClient->cmd));
376
377 return LSCP_OK;
378 }
379
380
381 /**
382 * Terminate and destroy a client instance.
383 *
384 * @param pClient Pointer to client instance structure.
385 *
386 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
387 */
388 lscp_status_t lscp_client_destroy ( lscp_client_t *pClient )
389 {
390 if (pClient == NULL)
391 return LSCP_FAILED;
392
393 #ifdef DEBUG
394 fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient);
395 #endif
396
397 // Lock this section up.
398 lscp_mutex_lock(pClient->mutex);
399
400 // Free up all cached members.
401 lscp_channel_info_free(&(pClient->channel_info));
402 lscp_engine_info_free(&(pClient->engine_info));
403 lscp_param_info_free(&(pClient->midi_port_param_info));
404 lscp_param_info_free(&(pClient->audio_channel_param_info));
405 lscp_device_port_info_free(&(pClient->midi_port_info));
406 lscp_device_port_info_free(&(pClient->audio_channel_info));
407 lscp_param_info_free(&(pClient->midi_param_info));
408 lscp_param_info_free(&(pClient->audio_param_info));
409 lscp_device_info_free(&(pClient->midi_device_info));
410 lscp_device_info_free(&(pClient->audio_device_info));
411 lscp_driver_info_free(&(pClient->midi_driver_info));
412 lscp_driver_info_free(&(pClient->audio_driver_info));
413 // Free available engine table.
414 lscp_szsplit_destroy(pClient->audio_drivers);
415 lscp_szsplit_destroy(pClient->midi_drivers);
416 lscp_isplit_destroy(pClient->audio_devices);
417 lscp_isplit_destroy(pClient->midi_devices);
418 lscp_szsplit_destroy(pClient->engines);
419 lscp_isplit_destroy(pClient->channels);
420 // Make them null.
421 pClient->audio_drivers = NULL;
422 pClient->midi_drivers = NULL;
423 pClient->engines = NULL;
424 // Free result error stuff.
425 lscp_client_set_result(pClient, NULL, 0);
426 // Free stream usage stuff.
427 if (pClient->buffer_fill)
428 free(pClient->buffer_fill);
429 pClient->buffer_fill = NULL;
430 pClient->iStreamCount = 0;
431 pClient->iTimeout = 0;
432
433 // Free socket agents.
434 lscp_socket_agent_free(&(pClient->evt));
435 lscp_socket_agent_free(&(pClient->cmd));
436
437 // Last but not least, free good ol'transaction mutex.
438 lscp_mutex_unlock(pClient->mutex);
439 lscp_mutex_destroy(pClient->mutex);
440 lscp_cond_destroy(pClient->cond);
441
442 free(pClient);
443
444 return LSCP_OK;
445 }
446
447
448 /**
449 * Set the client transaction timeout interval.
450 *
451 * @param pClient Pointer to client instance structure.
452 * @param iTimeout Transaction timeout in milliseconds.
453 *
454 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
455 */
456 lscp_status_t lscp_client_set_timeout ( lscp_client_t *pClient, int iTimeout )
457 {
458 if (pClient == NULL)
459 return LSCP_FAILED;
460 if (iTimeout < 0)
461 return LSCP_FAILED;
462
463 pClient->iTimeout = iTimeout;
464 return LSCP_OK;
465 }
466
467
468 /**
469 * Get the client transaction timeout interval.
470 *
471 * @param pClient Pointer to client instance structure.
472 *
473 * @returns The current timeout value milliseconds, -1 in case of failure.
474 */
475 int lscp_client_get_timeout ( lscp_client_t *pClient )
476 {
477 if (pClient == NULL)
478 return -1;
479
480 return pClient->iTimeout;
481 }
482
483
484 //-------------------------------------------------------------------------
485 // Client common protocol functions.
486
487 /**
488 * Submit a command query line string to the server. The query string
489 * must be cr/lf and null terminated. Besides the return code, the
490 * specific server response to the command request is made available
491 * by the @ref lscp_client_get_result and @ref lscp_client_get_errno
492 * function calls.
493 *
494 * @param pClient Pointer to client instance structure.
495 * @param pszQuery Command request line to be sent to server,
496 * must be cr/lf and null terminated.
497 *
498 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
499 */
500 lscp_status_t lscp_client_query ( lscp_client_t *pClient, const char *pszQuery )
501 {
502 lscp_status_t ret;
503
504 // Lock this section up.
505 lscp_mutex_lock(pClient->mutex);
506
507 // Just make the now guarded call.
508 ret = lscp_client_call(pClient, pszQuery);
509
510 // Unlock this section down.
511 lscp_mutex_unlock(pClient->mutex);
512
513 return ret;
514 }
515
516 /**
517 * Get the last received result string. In case of error or warning,
518 * this is the text of the error or warning message issued.
519 *
520 * @param pClient Pointer to client instance structure.
521 *
522 * @returns A pointer to the literal null-terminated result string as
523 * of the last command request.
524 */
525 const char *lscp_client_get_result ( lscp_client_t *pClient )
526 {
527 if (pClient == NULL)
528 return NULL;
529
530 return pClient->pszResult;
531 }
532
533
534 /**
535 * Get the last error/warning number received.
536 *
537 * @param pClient Pointer to client instance structure.
538 *
539 * @returns The numerical value of the last error or warning
540 * response code received.
541 */
542 int lscp_client_get_errno ( lscp_client_t *pClient )
543 {
544 if (pClient == NULL)
545 return -1;
546
547 return pClient->iErrno;
548 }
549
550
551 //-------------------------------------------------------------------------
552 // Client registration protocol functions.
553
554 /**
555 * Register frontend for receiving event messages:
556 * SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
557 * | CHANNEL_INFO | MISCELLANEOUS
558 *
559 * @param pClient Pointer to client instance structure.
560 * @param events Bit-wise OR'ed event flags to subscribe.
561 *
562 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
563 */
564 lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient, lscp_event_t events )
565 {
566 lscp_status_t ret = LSCP_FAILED;
567
568 if (pClient == NULL)
569 return LSCP_FAILED;
570
571 // Lock this section up.
572 lscp_mutex_lock(pClient->mutex);
573
574 // If applicable, start the alternate connection...
575 if (pClient->events == LSCP_EVENT_NONE)
576 ret = _lscp_client_evt_connect(pClient);
577
578 // Send the subscription commands.
579 if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS))
580 ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNELS);
581 if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
582 ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT);
583 if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
584 ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT);
585 if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
586 ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL);
587 if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
588 ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO);
589 if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
590 ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS);
591
592 // Unlock this section down.
593 lscp_mutex_unlock(pClient->mutex);
594
595 return ret;
596 }
597
598
599 /**
600 * Deregister frontend from receiving UDP event messages anymore:
601 * SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL
602 * | CHANNEL_INFO | MISCELLANEOUS
603 *
604 * @param pClient Pointer to client instance structure.
605 * @param events Bit-wise OR'ed event flags to unsubscribe.
606 *
607 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
608 */
609 lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient, lscp_event_t events )
610 {
611 lscp_status_t ret = LSCP_OK;
612
613 if (pClient == NULL)
614 return LSCP_FAILED;
615
616 // Lock this section up.
617 lscp_mutex_lock(pClient->mutex);
618
619 // Send the unsubscription commands.
620 if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS))
621 ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNELS);
622 if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT))
623 ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT);
624 if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT))
625 ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT);
626 if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL))
627 ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL);
628 if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO))
629 ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO);
630 if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS))
631 ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS);
632
633 // If necessary, close the alternate connection...
634 if (pClient->events == LSCP_EVENT_NONE)
635 lscp_socket_agent_free(&(pClient->evt));
636
637 // Unlock this section down.
638 lscp_mutex_unlock(pClient->mutex);
639
640 return ret;
641 }
642
643
644 //-------------------------------------------------------------------------
645 // Client command protocol functions.
646
647 /**
648 * Loading an instrument:
649 * LOAD INSTRUMENT <filename> <instr-index> <sampler-channel>
650 *
651 * @param pClient Pointer to client instance structure.
652 * @param pszFileName Instrument file name.
653 * @param iInstrIndex Instrument index number.
654 * @param iSamplerChannel Sampler Channel.
655 *
656 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
657 */
658 lscp_status_t lscp_load_instrument ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )
659 {
660 char szQuery[LSCP_BUFSIZ];
661
662 if (pszFileName == NULL || iSamplerChannel < 0)
663 return LSCP_FAILED;
664
665 sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
666 return lscp_client_query(pClient, szQuery);
667 }
668
669
670 /**
671 * Loading an instrument in the background (non modal):
672 * LOAD INSTRUMENT NON_MODAL <filename> <instr-index> <sampler-channel>
673 *
674 * @param pClient Pointer to client instance structure.
675 * @param pszFileName Instrument file name.
676 * @param iInstrIndex Instrument index number.
677 * @param iSamplerChannel Sampler Channel.
678 *
679 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
680 */
681 lscp_status_t lscp_load_instrument_non_modal ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel )
682 {
683 char szQuery[LSCP_BUFSIZ];
684
685 if (pszFileName == NULL || iSamplerChannel < 0)
686 return LSCP_FAILED;
687
688 sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel);
689 return lscp_client_query(pClient, szQuery);
690 }
691
692
693 /**
694 * Loading a sampler engine:
695 * LOAD ENGINE <engine-name> <sampler-channel>
696 *
697 * @param pClient Pointer to client instance structure.
698 * @param pszEngineName Engine name.
699 * @param iSamplerChannel Sampler channel number.
700 *
701 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
702 */
703 lscp_status_t lscp_load_engine ( lscp_client_t *pClient, const char *pszEngineName, int iSamplerChannel )
704 {
705 char szQuery[LSCP_BUFSIZ];
706
707 if (pszEngineName == NULL || iSamplerChannel < 0)
708 return LSCP_FAILED;
709
710 sprintf(szQuery, "LOAD ENGINE %s %d\r\n", pszEngineName, iSamplerChannel);
711 return lscp_client_query(pClient, szQuery);
712 }
713
714
715 /**
716 * Current number of sampler channels:
717 * GET CHANNELS
718 *
719 * @param pClient Pointer to client instance structure.
720 *
721 * @returns The current total number of sampler channels on success,
722 * -1 otherwise.
723 */
724 int lscp_get_channels ( lscp_client_t *pClient )
725 {
726 int iChannels = -1;
727
728 // Lock this section up.
729 lscp_mutex_lock(pClient->mutex);
730
731 if (lscp_client_call(pClient, "GET CHANNELS\r\n") == LSCP_OK)
732 iChannels = atoi(lscp_client_get_result(pClient));
733
734 // Unlock this section doen.
735 lscp_mutex_unlock(pClient->mutex);
736
737 return iChannels;
738 }
739
740
741 /**
742 * List current sampler channels number identifiers:
743 * LIST CHANNELS
744 *
745 * @param pClient Pointer to client instance structure.
746 *
747 * @returns An array of the sampler channels identifiers as positive integers,
748 * terminated with -1 on success, NULL otherwise.
749 */
750 int *lscp_list_channels ( lscp_client_t *pClient )
751 {
752 const char *pszSeps = ",";
753
754 if (pClient == NULL)
755 return NULL;
756
757 // Lock this section up.
758 lscp_mutex_lock(pClient->mutex);
759
760 if (pClient->channels) {
761 lscp_isplit_destroy(pClient->channels);
762 pClient->channels = NULL;
763 }
764
765 if (lscp_client_call(pClient, "LIST CHANNELS\r\n") == LSCP_OK)
766 pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps);
767
768 // Unlock this section down.
769 lscp_mutex_unlock(pClient->mutex);
770
771 return pClient->channels;
772 }
773
774
775 /**
776 * Adding a new sampler channel:
777 * ADD CHANNEL
778 *
779 * @param pClient Pointer to client instance structure.
780 *
781 * @returns The new sampler channel number identifier,
782 * or -1 in case of failure.
783 */
784 int lscp_add_channel ( lscp_client_t *pClient )
785 {
786 int iSamplerChannel = -1;
787
788 // Lock this section up.
789 lscp_mutex_lock(pClient->mutex);
790
791 if (lscp_client_call(pClient, "ADD CHANNEL\r\n") == LSCP_OK)
792 iSamplerChannel = atoi(lscp_client_get_result(pClient));
793
794 // Unlock this section down.
795 lscp_mutex_unlock(pClient->mutex);
796
797 return iSamplerChannel;
798 }
799
800
801 /**
802 * Removing a sampler channel:
803 * REMOVE CHANNEL <sampler-channel>
804 *
805 * @param pClient Pointer to client instance structure.
806 * @param iSamplerChannel Sampler channel number.
807 *
808 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
809 */
810 lscp_status_t lscp_remove_channel ( lscp_client_t *pClient, int iSamplerChannel )
811 {
812 char szQuery[LSCP_BUFSIZ];
813
814 if (iSamplerChannel < 0)
815 return LSCP_FAILED;
816
817 sprintf(szQuery, "REMOVE CHANNEL %d\r\n", iSamplerChannel);
818 return lscp_client_query(pClient, szQuery);
819 }
820
821
822 /**
823 * Getting all available engines:
824 * GET AVAILABLE_ENGINES
825 *
826 * @param pClient Pointer to client instance structure.
827 *
828 * @returns A NULL terminated array of engine name strings,
829 * or NULL in case of failure.
830 */
831 const char **lscp_get_available_engines ( lscp_client_t *pClient )
832 {
833 const char *pszSeps = ",";
834
835 // Lock this section up.
836 lscp_mutex_lock(pClient->mutex);
837
838 if (pClient->engines) {
839 lscp_szsplit_destroy(pClient->engines);
840 pClient->engines = NULL;
841 }
842
843 if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n") == LSCP_OK)
844 pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps);
845
846 // Unlock this section down.
847 lscp_mutex_unlock(pClient->mutex);
848
849 return (const char **) pClient->engines;
850 }
851
852
853 /**
854 * Getting information about an engine.
855 * GET ENGINE INFO <engine-name>
856 *
857 * @param pClient Pointer to client instance structure.
858 * @param pszEngineName Engine name.
859 *
860 * @returns A pointer to a @ref lscp_engine_info_t structure, with all the
861 * information of the given sampler engine, or NULL in case of failure.
862 */
863 lscp_engine_info_t *lscp_get_engine_info ( lscp_client_t *pClient, const char *pszEngineName )
864 {
865 lscp_engine_info_t *pEngineInfo;
866 char szQuery[LSCP_BUFSIZ];
867 const char *pszResult;
868 const char *pszSeps = ":";
869 const char *pszCrlf = "\r\n";
870 char *pszToken;
871 char *pch;
872
873 if (pszEngineName == NULL)
874 return NULL;
875
876 // Lock this section up.
877 lscp_mutex_lock(pClient->mutex);
878
879 pEngineInfo = &(pClient->engine_info);
880 lscp_engine_info_reset(pEngineInfo);
881
882 sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName);
883 if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
884 pszResult = lscp_client_get_result(pClient);
885 pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
886 while (pszToken) {
887 if (strcasecmp(pszToken, "DESCRIPTION") == 0) {
888 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
889 if (pszToken)
890 pEngineInfo->description = lscp_unquote(&pszToken, 1);
891 }
892 else if (strcasecmp(pszToken, "VERSION") == 0) {
893 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
894 if (pszToken)
895 pEngineInfo->version = lscp_unquote(&pszToken, 1);
896 }
897 pszToken = lscp_strtok(NULL, pszSeps, &(pch));
898 }
899 }
900 else pEngineInfo = NULL;
901
902 // Unlock this section down.
903 lscp_mutex_unlock(pClient->mutex);
904
905 return pEngineInfo;
906 }
907
908
909 /**
910 * Getting sampler channel informations:
911 * GET CHANNEL INFO <sampler-channel>
912 *
913 * @param pClient Pointer to client instance structure.
914 * @param iSamplerChannel Sampler channel number.
915 *
916 * @returns A pointer to a @ref lscp_channel_info_t structure, with all the
917 * information of the given sampler channel, or NULL in case of failure.
918 */
919 lscp_channel_info_t *lscp_get_channel_info ( lscp_client_t *pClient, int iSamplerChannel )
920 {
921 lscp_channel_info_t *pChannelInfo;
922 char szQuery[LSCP_BUFSIZ];
923 const char *pszResult;
924 const char *pszSeps = ":";
925 const char *pszCrlf = "\r\n";
926 char *pszToken;
927 char *pch;
928
929 if (iSamplerChannel < 0)
930 return NULL;
931
932 // Lock this section up.
933 lscp_mutex_lock(pClient->mutex);
934
935 pChannelInfo = &(pClient->channel_info);
936 lscp_channel_info_reset(pChannelInfo);
937
938 sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel);
939 if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
940 pszResult = lscp_client_get_result(pClient);
941 pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
942 while (pszToken) {
943 if (strcasecmp(pszToken, "ENGINE_NAME") == 0) {
944 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
945 if (pszToken)
946 pChannelInfo->engine_name = lscp_unquote(&pszToken, 1);
947 }
948 else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) {
949 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
950 if (pszToken)
951 pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken));
952 }
953 else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) {
954 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
955 if (pszToken)
956 pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken));
957 }
958 else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) {
959 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
960 if (pszToken)
961 pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ",");
962 }
963 else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) {
964 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
965 if (pszToken)
966 pChannelInfo->instrument_file = lscp_unquote(&pszToken, 1);
967 }
968 else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) {
969 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
970 if (pszToken)
971 pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken));
972 }
973 else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) {
974 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
975 if (pszToken)
976 pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken));
977 }
978 else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) {
979 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
980 if (pszToken)
981 pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken));
982 }
983 else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) {
984 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
985 if (pszToken)
986 pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken));
987 }
988 else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) {
989 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
990 if (pszToken)
991 pChannelInfo->midi_channel = atoi(lscp_ltrim(pszToken));
992 }
993 else if (strcasecmp(pszToken, "VOLUME") == 0) {
994 pszToken = lscp_strtok(NULL, pszCrlf, &(pch));
995 if (pszToken)
996 pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken));
997 }
998 pszToken = lscp_strtok(NULL, pszSeps, &(pch));
999 }
1000 }
1001 else pChannelInfo = NULL;
1002
1003 // Unlock this section up.
1004 lscp_mutex_unlock(pClient->mutex);
1005
1006 return pChannelInfo;
1007 }
1008
1009
1010 /**
1011 * Current number of active voices:
1012 * GET CHANNEL VOICE_COUNT <sampler-channel>
1013 *
1014 * @param pClient Pointer to client instance structure.
1015 * @param iSamplerChannel Sampler channel number.
1016 *
1017 * @returns The number of voices currently active, -1 in case of failure.
1018 */
1019 int lscp_get_channel_voice_count ( lscp_client_t *pClient, int iSamplerChannel )
1020 {
1021 char szQuery[LSCP_BUFSIZ];
1022 int iVoiceCount = -1;
1023
1024 if (iSamplerChannel < 0)
1025 return iVoiceCount;
1026
1027 // Lock this section up.
1028 lscp_mutex_lock(pClient->mutex);
1029
1030 sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel);
1031 if (lscp_client_call(pClient, szQuery) == LSCP_OK)
1032 iVoiceCount = atoi(lscp_client_get_result(pClient));
1033
1034 // Unlock this section down.
1035 lscp_mutex_unlock(pClient->mutex);
1036
1037 return iVoiceCount;
1038 }
1039
1040
1041 /**
1042 * Current number of active disk streams:
1043 * GET CHANNEL STREAM_COUNT <sampler-channel>
1044 *
1045 * @param pClient Pointer to client instance structure.
1046 * @param iSamplerChannel Sampler channel number.
1047 *
1048 * @returns The number of active disk streams on success, -1 otherwise.
1049 */
1050 int lscp_get_channel_stream_count ( lscp_client_t *pClient, int iSamplerChannel )
1051 {
1052 char szQuery[LSCP_BUFSIZ];
1053 int iStreamCount = -1;
1054
1055 if (iSamplerChannel < 0)
1056 return iStreamCount;
1057
1058 // Lock this section up.
1059 lscp_mutex_lock(pClient->mutex);
1060
1061 sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel);
1062 if (lscp_client_call(pClient, szQuery) == LSCP_OK)
1063 iStreamCount = atoi(lscp_client_get_result(pClient));
1064
1065 // Unlock this section down.
1066 lscp_mutex_unlock(pClient->mutex);
1067
1068 return iStreamCount;
1069 }
1070
1071
1072 /**
1073 * Current least usage of active disk streams.
1074 *
1075 * @param pClient Pointer to client instance structure.
1076 * @param iSamplerChannel Sampler channel number.
1077 *
1078 * @returns The usage percentage of the least filled active disk stream
1079 * on success, -1 otherwise.
1080 */
1081 int lscp_get_channel_stream_usage ( lscp_client_t *pClient, int iSamplerChannel )
1082 {
1083 char szQuery[LSCP_BUFSIZ];
1084 int iStreamUsage = -1;
1085 const char *pszResult;
1086 const char *pszSeps = "[]%,";
1087 char *pszToken;
1088 char *pch;
1089 int iStream;
1090 int iPercent;
1091
1092 if (iSamplerChannel < 0)
1093 return iStreamUsage;
1094
1095 // Lock this section up.
1096 lscp_mutex_lock(pClient->mutex);
1097
1098 iStream = 0;
1099 sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel);
1100 if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
1101 pszResult = lscp_client_get_result(pClient);
1102 pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1103 while (pszToken) {
1104 if (*pszToken) {
1105 // Skip stream id.
1106 pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1107 if (pszToken == NULL)
1108 break;
1109 // Get least buffer fill percentage.
1110 iPercent = atol(pszToken);
1111 if (iStreamUsage > iPercent || iStream == 0)
1112 iStreamUsage = iPercent;
1113 iStream++;
1114 }
1115 pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1116 }
1117 }
1118
1119 // Unlock this section down.
1120 lscp_mutex_unlock(pClient->mutex);
1121
1122 return iStreamUsage;
1123 }
1124
1125
1126 /**
1127 * Current fill state of disk stream buffers:
1128 * GET CHANNEL BUFFER_FILL {BYTES|PERCENTAGE} <sampler-channel>
1129 *
1130 * @param pClient Pointer to client instance structure.
1131 * @param usage_type Usage type to be returned, either
1132 * @ref LSCP_USAGE_BYTES, or
1133 * @ref LSCP_USAGE_PERCENTAGE.
1134 * @param iSamplerChannel Sampler channel number.
1135 *
1136 * @returns A pointer to a @ref lscp_buffer_fill_t structure, with the
1137 * information of the current disk stream buffer fill usage, for the given
1138 * sampler channel, or NULL in case of failure.
1139 */
1140 lscp_buffer_fill_t *lscp_get_channel_buffer_fill ( lscp_client_t *pClient, lscp_usage_t usage_type, int iSamplerChannel )
1141 {
1142 lscp_buffer_fill_t *pBufferFill;
1143 char szQuery[LSCP_BUFSIZ];
1144 int iStreamCount;
1145 const char *pszUsageType = (usage_type == LSCP_USAGE_BYTES ? "BYTES" : "PERCENTAGE");
1146 const char *pszResult;
1147 const char *pszSeps = "[]%,";
1148 char *pszToken;
1149 char *pch;
1150 int iStream;
1151
1152 // Retrieve a channel stream estimation.
1153 iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel);
1154 if (pClient->iStreamCount < 0)
1155 return NULL;
1156
1157 // Lock this section up.
1158 lscp_mutex_lock(pClient->mutex);
1159
1160 // Check if we need to reallocate the stream usage array.
1161 if (pClient->iStreamCount != iStreamCount) {
1162 if (pClient->buffer_fill)
1163 free(pClient->buffer_fill);
1164 if (iStreamCount > 0)
1165 pClient->buffer_fill = (lscp_buffer_fill_t *) malloc(iStreamCount * sizeof(lscp_buffer_fill_t));
1166 else
1167 pClient->buffer_fill = NULL;
1168 pClient->iStreamCount = iStreamCount;
1169 }
1170
1171 // Get buffer fill usage...
1172 pBufferFill = pClient->buffer_fill;
1173 if (pBufferFill && iStreamCount > 0) {
1174 iStream = 0;
1175 pBufferFill = pClient->buffer_fill;
1176 sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel);
1177 if (lscp_client_call(pClient, szQuery) == LSCP_OK) {
1178 pszResult = lscp_client_get_result(pClient);
1179 pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch));
1180 while (pszToken && iStream < pClient->iStreamCount) {
1181 if (*pszToken) {
1182 pBufferFill[iStream].stream_id = atol(pszToken);
1183 pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1184 if (pszToken == NULL)
1185 break;
1186 pBufferFill[iStream].stream_usage = atol(pszToken);
1187 iStream++;
1188 }
1189 pszToken = lscp_strtok(NULL, pszSeps, &(pch));
1190 }
1191 } // Reset the usage, whatever it was before.
1192 else while (iStream < pClient->iStreamCount)
1193 pBufferFill[iStream++].stream_usage = 0;
1194 }
1195
1196 // Unlock this section down.
1197 lscp_mutex_unlock(pClient->mutex);
1198
1199 return pBufferFill;
1200 }
1201
1202
1203 /**
1204 * Setting audio output type:
1205 * SET CHANNEL AUDIO_OUTPUT_TYPE <sampler-channel> <audio-output-type>
1206 *
1207 * @param pClient Pointer to client instance structure.
1208 * @param iSamplerChannel Sampler channel number.
1209 * @param pszAudioDriver Audio output driver type (e.g. "ALSA" or "JACK").
1210 */
1211 lscp_status_t lscp_set_channel_audio_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszAudioDriver )
1212 {
1213 char szQuery[LSCP_BUFSIZ];
1214
1215 if (iSamplerChannel < 0 || pszAudioDriver == NULL)
1216 return LSCP_FAILED;
1217
1218 sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_TYPE %d %s\r\n", iSamplerChannel, pszAudioDriver);
1219 return lscp_client_query(pClient, szQuery);
1220 }
1221
1222
1223 /**
1224 * Setting audio output device:
1225 * SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id>
1226 *
1227 * @param pClient Pointer to client instance structure.
1228 * @param iSamplerChannel Sampler channel number.
1229 * @param iAudioDevice Audio output device number identifier.
1230 */
1231 lscp_status_t lscp_set_channel_audio_device ( lscp_client_t *pClient, int iSamplerChannel, int iAudioDevice )
1232 {
1233 char szQuery[LSCP_BUFSIZ];
1234
1235 if (iSamplerChannel < 0 || iAudioDevice < 0)
1236 return LSCP_FAILED;
1237
1238 sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice);
1239 return lscp_client_query(pClient, szQuery);
1240 }
1241
1242
1243 /**
1244 * Setting audio output channel:
1245 * SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-output-chan> <audio-input-chan>
1246 *
1247 * @param pClient Pointer to client instance structure.
1248 * @param iSamplerChannel Sampler channel number.
1249 * @param iAudioOut Audio output device channel to be routed from.
1250 * @param iAudioIn Audio output device channel to be routed into.
1251 *
1252 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1253 */
1254 lscp_status_t lscp_set_channel_audio_channel ( lscp_client_t *pClient, int iSamplerChannel, int iAudioOut, int iAudioIn )
1255 {
1256 char szQuery[LSCP_BUFSIZ];
1257
1258 if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0)
1259 return LSCP_FAILED;
1260
1261 sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNELS %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn);
1262 return lscp_client_query(pClient, szQuery);
1263 }
1264
1265
1266 /**
1267 * Setting MIDI input type:
1268 * SET CHANNEL MIDI_INPUT_TYPE <sampler-channel> <midi-input-type>
1269 *
1270 * @param pClient Pointer to client instance structure.
1271 * @param iSamplerChannel Sampler channel number.
1272 * @param pszMidiDriver MIDI input driver type (e.g. "ALSA").
1273 *
1274 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1275 */
1276 lscp_status_t lscp_set_channel_midi_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszMidiDriver )
1277 {
1278 char szQuery[LSCP_BUFSIZ];
1279
1280 if (iSamplerChannel < 0 || pszMidiDriver == NULL)
1281 return LSCP_FAILED;
1282
1283 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_TYPE %d %s\r\n", iSamplerChannel, pszMidiDriver);
1284 return lscp_client_query(pClient, szQuery);
1285 }
1286
1287
1288 /**
1289 * Setting MIDI input device:
1290 * SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id>
1291 *
1292 * @param pClient Pointer to client instance structure.
1293 * @param iSamplerChannel Sampler channel number.
1294 * @param iMidiDevice MIDI input device number identifier.
1295 */
1296 lscp_status_t lscp_set_channel_midi_device ( lscp_client_t *pClient, int iSamplerChannel, int iMidiDevice )
1297 {
1298 char szQuery[LSCP_BUFSIZ];
1299
1300 if (iSamplerChannel < 0 || iMidiDevice < 0)
1301 return LSCP_FAILED;
1302
1303 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice);
1304 return lscp_client_query(pClient, szQuery);
1305 }
1306
1307
1308 /**
1309 * Setting MIDI input port:
1310 * SET CHANNEL MIDI_INPUT_PORT <sampler-channel> <midi-input-port>
1311 *
1312 * @param pClient Pointer to client instance structure.
1313 * @param iSamplerChannel Sampler channel number.
1314 * @param iMidiPort MIDI input driver virtual port number.
1315 *
1316 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1317 */
1318 lscp_status_t lscp_set_channel_midi_port ( lscp_client_t *pClient, int iSamplerChannel, int iMidiPort )
1319 {
1320 char szQuery[LSCP_BUFSIZ];
1321
1322 if (iSamplerChannel < 0 || iMidiPort < 0)
1323 return LSCP_FAILED;
1324
1325 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_PORT %d %d\r\n", iSamplerChannel, iMidiPort);
1326 return lscp_client_query(pClient, szQuery);
1327 }
1328
1329
1330 /**
1331 * Setting MIDI input channel:
1332 * SET CHANNEL MIDI_INPUT_CHANNEL <sampler-channel> <midi-input-chan>
1333 *
1334 * @param pClient Pointer to client instance structure.
1335 * @param iSamplerChannel Sampler channel number.
1336 * @param iMidiChannel MIDI channel number to listen (1-16) or
1337 * zero (0) to listen on all channels.
1338 *
1339 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1340 */
1341 lscp_status_t lscp_set_channel_midi_channel ( lscp_client_t *pClient, int iSamplerChannel, int iMidiChannel )
1342 {
1343 char szQuery[LSCP_BUFSIZ];
1344
1345 if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16)
1346 return LSCP_FAILED;
1347
1348 if (iMidiChannel > 0)
1349 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel);
1350 else
1351 sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel);
1352 return lscp_client_query(pClient, szQuery);
1353 }
1354
1355
1356 /**
1357 * Setting channel volume:
1358 * SET CHANNEL VOLUME <sampler-channel> <volume>
1359 *
1360 * @param pClient Pointer to client instance structure.
1361 * @param iSamplerChannel Sampler channel number.
1362 * @param fVolume Sampler channel volume as a positive floating point
1363 * number, where a value less than 1.0 for attenuation,
1364 * and greater than 1.0 for amplification.
1365 *
1366 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1367 */
1368 lscp_status_t lscp_set_channel_volume ( lscp_client_t *pClient, int iSamplerChannel, float fVolume )
1369 {
1370 char szQuery[LSCP_BUFSIZ];
1371
1372 if (iSamplerChannel < 0 || fVolume < 0.0)
1373 return LSCP_FAILED;
1374
1375 sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume);
1376 return lscp_client_query(pClient, szQuery);
1377 }
1378
1379
1380 /**
1381 * Resetting a sampler channel:
1382 * RESET CHANNEL <sampler-channel>
1383 *
1384 * @param pClient Pointer to client instance structure.
1385 * @param iSamplerChannel Sampler channel number.
1386 *
1387 * @returns LSCP_OK on success, LSCP_FAILED otherwise.
1388 */
1389 lscp_status_t lscp_reset_channel ( lscp_client_t *pClient, int iSamplerChannel )
1390 {
1391 char szQuery[LSCP_BUFSIZ];
1392
1393 if (iSamplerChannel < 0)
1394 return LSCP_FAILED;
1395
1396 sprintf(szQuery, "RESET CHANNEL %d\r\n", iSamplerChannel);
1397 return lscp_client_query(pClient, szQuery);
1398 }
1399
1400
1401 // end of client.c

  ViewVC Help
Powered by ViewVC