--- liblscp/trunk/src/client.c 2006/11/28 15:31:20 948 +++ liblscp/trunk/src/client.c 2006/12/17 15:08:35 977 @@ -39,88 +39,88 @@ static void _lscp_client_evt_proc ( void *pvClient ) { - lscp_client_t *pClient = (lscp_client_t *) pvClient; + lscp_client_t *pClient = (lscp_client_t *) pvClient; - fd_set fds; // File descriptor list for select(). - int fd, fdmax; // Maximum file descriptor number. - struct timeval tv; // For specifying a timeout value. - int iSelect; // Holds select return status. - int iTimeout; - - char achBuffer[LSCP_BUFSIZ]; - int cchBuffer; - const char *pszSeps = ":\r\n"; - char * pszToken; - char * pch; - int cchToken; - lscp_event_t event; + fd_set fds; // File descriptor list for select(). + int fd, fdmax; // Maximum file descriptor number. + struct timeval tv; // For specifying a timeout value. + int iSelect; // Holds select return status. + int iTimeout; + + char achBuffer[LSCP_BUFSIZ]; + int cchBuffer; + const char *pszSeps = ":\r\n"; + char * pszToken; + char * pch; + int cchToken; + lscp_event_t event; #ifdef DEBUG - fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n"); + fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n"); #endif - while (pClient->evt.iState) { + while (pClient->evt.iState) { - // Prepare for waiting on select... - fd = (int) pClient->evt.sock; - FD_ZERO(&fds); - FD_SET((unsigned int) fd, &fds); - fdmax = fd; - - // Use the timeout (x10) select feature ... - iTimeout = 10 * pClient->iTimeout; - if (iTimeout >= 1000) { - tv.tv_sec = iTimeout / 1000; - iTimeout -= tv.tv_sec * 1000; - } - else tv.tv_sec = 0; - tv.tv_usec = iTimeout * 1000; - - // Wait for event... - iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv); - if (iSelect > 0 && FD_ISSET(fd, &fds)) { - // May recv now... - cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0); - if (cchBuffer > 0) { - // Make sure received buffer it's null terminated. - achBuffer[cchBuffer] = (char) 0; - // Parse for the notification event message... - pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY". - if (strcasecmp(pszToken, "NOTIFY") == 0) { - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - event = lscp_event_from_text(pszToken); - // And pick the rest of data... - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - cchToken = (pszToken == NULL ? 0 : strlen(pszToken)); - // Double-check if we're really up to it... - if (pClient->events & event) { - // Invoke the client event callback... - if ((*pClient->pfnCallback)( - pClient, - event, - pszToken, - cchToken, - pClient->pvData) != LSCP_OK) { - pClient->evt.iState = 0; - } - } - } - } else { - lscp_socket_perror("_lscp_client_evt_proc: recv"); - pClient->evt.iState = 0; - } - } // Check if select has in error. - else if (iSelect < 0) { - lscp_socket_perror("_lscp_client_evt_proc: select"); - pClient->evt.iState = 0; - } - - // Finally, always signal the event. - lscp_cond_signal(pClient->cond); - } + // Prepare for waiting on select... + fd = (int) pClient->evt.sock; + FD_ZERO(&fds); + FD_SET((unsigned int) fd, &fds); + fdmax = fd; + + // Use the timeout (x10) select feature ... + iTimeout = 10 * pClient->iTimeout; + if (iTimeout >= 1000) { + tv.tv_sec = iTimeout / 1000; + iTimeout -= tv.tv_sec * 1000; + } + else tv.tv_sec = 0; + tv.tv_usec = iTimeout * 1000; + + // Wait for event... + iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv); + if (iSelect > 0 && FD_ISSET(fd, &fds)) { + // May recv now... + cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0); + if (cchBuffer > 0) { + // Make sure received buffer it's null terminated. + achBuffer[cchBuffer] = (char) 0; + // Parse for the notification event message... + pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY". + if (strcasecmp(pszToken, "NOTIFY") == 0) { + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + event = lscp_event_from_text(pszToken); + // And pick the rest of data... + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + cchToken = (pszToken == NULL ? 0 : strlen(pszToken)); + // Double-check if we're really up to it... + if (pClient->events & event) { + // Invoke the client event callback... + if ((*pClient->pfnCallback)( + pClient, + event, + pszToken, + cchToken, + pClient->pvData) != LSCP_OK) { + pClient->evt.iState = 0; + } + } + } + } else { + lscp_socket_perror("_lscp_client_evt_proc: recv"); + pClient->evt.iState = 0; + } + } // Check if select has in error. + else if (iSelect < 0) { + lscp_socket_perror("_lscp_client_evt_proc: select"); + pClient->evt.iState = 0; + } + + // Finally, always signal the event. + lscp_cond_signal(pClient->cond); + } #ifdef DEBUG - fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n"); + fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n"); #endif } @@ -131,81 +131,81 @@ // Open the event service socket connection. static lscp_status_t _lscp_client_evt_connect ( lscp_client_t *pClient ) { - lscp_socket_t sock; - struct sockaddr_in addr; - int cAddr; + lscp_socket_t sock; + struct sockaddr_in addr; + int cAddr; #if defined(WIN32) - int iSockOpt = (-1); + int iSockOpt = (-1); #endif - // Prepare the event connection socket... - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == INVALID_SOCKET) { - lscp_socket_perror("_lscp_client_evt_connect: socket"); - return LSCP_FAILED; - } + // Prepare the event connection socket... + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == INVALID_SOCKET) { + lscp_socket_perror("_lscp_client_evt_connect: socket"); + return LSCP_FAILED; + } #if defined(WIN32) - if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) - lscp_socket_perror("lscp_client_evt_connect: setsockopt(SO_DONTLINGER)"); + if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) + lscp_socket_perror("lscp_client_evt_connect: setsockopt(SO_DONTLINGER)"); #endif #ifdef DEBUG - lscp_socket_getopts("_lscp_client_evt_connect:", sock); + lscp_socket_getopts("_lscp_client_evt_connect:", sock); #endif - // Use same address of the command connection. - cAddr = sizeof(struct sockaddr_in); - memmove((char *) &addr, &(pClient->cmd.addr), cAddr); - - // Start the connection... - if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { - lscp_socket_perror("_lscp_client_evt_connect: connect"); - closesocket(sock); - return LSCP_FAILED; - } + // Use same address of the command connection. + cAddr = sizeof(struct sockaddr_in); + memmove((char *) &addr, &(pClient->cmd.addr), cAddr); + + // Start the connection... + if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { + lscp_socket_perror("_lscp_client_evt_connect: connect"); + closesocket(sock); + return LSCP_FAILED; + } - // Set our socket agent struct... - lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr); + // Set our socket agent struct... + lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr); - // And finally the service thread... - return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0); + // And finally the service thread... + return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0); } // Subscribe to a single event. static lscp_status_t _lscp_client_evt_request ( lscp_client_t *pClient, int iSubscribe, lscp_event_t event ) { - const char *pszEvent; - char szQuery[LSCP_BUFSIZ]; - int cchQuery; - - if (pClient == NULL) - return LSCP_FAILED; - - // Which (single) event? - pszEvent = lscp_event_to_text(event); - if (pszEvent == NULL) - return LSCP_FAILED; - - // Build the query string... - cchQuery = sprintf(szQuery, "%sSUBSCRIBE %s\n\n", (iSubscribe == 0 ? "UN" : ""), pszEvent); - // Just send data, forget result... - if (send(pClient->evt.sock, szQuery, cchQuery, 0) < cchQuery) { - lscp_socket_perror("_lscp_client_evt_request: send"); - return LSCP_FAILED; - } - - // Wait on response. - lscp_cond_wait(pClient->cond, pClient->mutex); - - // Update as naively as we can... - if (iSubscribe) - pClient->events |= event; - else - pClient->events &= ~event; + const char *pszEvent; + char szQuery[LSCP_BUFSIZ]; + int cchQuery; + + if (pClient == NULL) + return LSCP_FAILED; + + // Which (single) event? + pszEvent = lscp_event_to_text(event); + if (pszEvent == NULL) + return LSCP_FAILED; + + // Build the query string... + cchQuery = sprintf(szQuery, "%sSUBSCRIBE %s\n\n", (iSubscribe == 0 ? "UN" : ""), pszEvent); + // Just send data, forget result... + if (send(pClient->evt.sock, szQuery, cchQuery, 0) < cchQuery) { + lscp_socket_perror("_lscp_client_evt_request: send"); + return LSCP_FAILED; + } + + // Wait on response. + lscp_cond_wait(pClient->cond, pClient->mutex); + + // Update as naively as we can... + if (iSubscribe) + pClient->events |= event; + else + pClient->events &= ~event; - return LSCP_OK; + return LSCP_OK; } @@ -242,122 +242,124 @@ */ lscp_client_t* lscp_client_create ( const char *pszHost, int iPort, lscp_client_proc_t pfnCallback, void *pvData ) { - lscp_client_t *pClient; - struct hostent *pHost; - lscp_socket_t sock; - struct sockaddr_in addr; - int cAddr; + lscp_client_t *pClient; + struct hostent *pHost; + lscp_socket_t sock; + struct sockaddr_in addr; + int cAddr; #if defined(WIN32) - int iSockOpt = (-1); + int iSockOpt = (-1); #endif - if (pfnCallback == NULL) { - fprintf(stderr, "lscp_client_create: Invalid client callback function.\n"); - return NULL; - } - - pHost = gethostbyname(pszHost); - if (pHost == NULL) { - lscp_socket_herror("lscp_client_create: gethostbyname"); - return NULL; - } - - // Allocate client descriptor... - - pClient = (lscp_client_t *) malloc(sizeof(lscp_client_t)); - if (pClient == NULL) { - fprintf(stderr, "lscp_client_create: Out of memory.\n"); - return NULL; - } - memset(pClient, 0, sizeof(lscp_client_t)); + if (pfnCallback == NULL) { + fprintf(stderr, "lscp_client_create: Invalid client callback function.\n"); + return NULL; + } + + pHost = gethostbyname(pszHost); + if (pHost == NULL) { + lscp_socket_herror("lscp_client_create: gethostbyname"); + return NULL; + } + + // Allocate client descriptor... + + pClient = (lscp_client_t *) malloc(sizeof(lscp_client_t)); + if (pClient == NULL) { + fprintf(stderr, "lscp_client_create: Out of memory.\n"); + return NULL; + } + memset(pClient, 0, sizeof(lscp_client_t)); - pClient->pfnCallback = pfnCallback; - pClient->pvData = pvData; + pClient->pfnCallback = pfnCallback; + pClient->pvData = pvData; #ifdef DEBUG - fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort); + fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort); #endif - // Prepare the command connection socket... + // Prepare the command connection socket... - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == INVALID_SOCKET) { - lscp_socket_perror("lscp_client_create: cmd: socket"); - free(pClient); - return NULL; - } + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == INVALID_SOCKET) { + lscp_socket_perror("lscp_client_create: cmd: socket"); + free(pClient); + return NULL; + } #if defined(WIN32) - if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) - lscp_socket_perror("lscp_client_create: cmd: setsockopt(SO_DONTLINGER)"); + if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) + lscp_socket_perror("lscp_client_create: cmd: setsockopt(SO_DONTLINGER)"); #endif #ifdef DEBUG - lscp_socket_getopts("lscp_client_create: cmd", sock); + lscp_socket_getopts("lscp_client_create: cmd", sock); #endif - cAddr = sizeof(struct sockaddr_in); - memset((char *) &addr, 0, cAddr); - addr.sin_family = pHost->h_addrtype; - memmove((char *) &(addr.sin_addr), pHost->h_addr, pHost->h_length); - addr.sin_port = htons((short) iPort); - - if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { - lscp_socket_perror("lscp_client_create: cmd: connect"); - closesocket(sock); - free(pClient); - return NULL; - } + cAddr = sizeof(struct sockaddr_in); + memset((char *) &addr, 0, cAddr); + addr.sin_family = pHost->h_addrtype; + memmove((char *) &(addr.sin_addr), pHost->h_addr, pHost->h_length); + addr.sin_port = htons((short) iPort); + + if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { + lscp_socket_perror("lscp_client_create: cmd: connect"); + closesocket(sock); + free(pClient); + return NULL; + } - // Initialize the command socket agent struct... - lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr); + // Initialize the command socket agent struct... + lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr); #ifdef DEBUG - 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)); + 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)); #endif - // Initialize the event service socket struct... - lscp_socket_agent_init(&(pClient->evt), INVALID_SOCKET, NULL, 0); - // No events subscribed, yet. - pClient->events = LSCP_EVENT_NONE; - // Initialize cached members. - pClient->audio_drivers = NULL; - pClient->midi_drivers = NULL; - pClient->audio_devices = NULL; - pClient->midi_devices = NULL; - pClient->engines = NULL; - pClient->channels = NULL; - pClient->midi_instruments = NULL; - lscp_driver_info_init(&(pClient->audio_driver_info)); - lscp_driver_info_init(&(pClient->midi_driver_info)); - lscp_device_info_init(&(pClient->audio_device_info)); - lscp_device_info_init(&(pClient->midi_device_info)); - lscp_param_info_init(&(pClient->audio_param_info)); - lscp_param_info_init(&(pClient->midi_param_info)); - lscp_device_port_info_init(&(pClient->audio_channel_info)); - lscp_device_port_info_init(&(pClient->midi_port_info)); - lscp_param_info_init(&(pClient->audio_channel_param_info)); - lscp_param_info_init(&(pClient->midi_port_param_info)); - lscp_server_info_init(&(pClient->server_info)); - lscp_engine_info_init(&(pClient->engine_info)); - lscp_channel_info_init(&(pClient->channel_info)); - lscp_midi_instrument_info_init(&(pClient->midi_instrument_info)); - // Initialize error stuff. - pClient->pszResult = NULL; - pClient->iErrno = -1; - // Stream usage stuff. - pClient->buffer_fill = NULL; - pClient->iStreamCount = 0; - // Default timeout value. - pClient->iTimeout = LSCP_TIMEOUT_MSECS; + // Initialize the event service socket struct... + lscp_socket_agent_init(&(pClient->evt), INVALID_SOCKET, NULL, 0); + // No events subscribed, yet. + pClient->events = LSCP_EVENT_NONE; + // Initialize cached members. + pClient->audio_drivers = NULL; + pClient->midi_drivers = NULL; + pClient->audio_devices = NULL; + pClient->midi_devices = NULL; + pClient->engines = NULL; + pClient->channels = NULL; + pClient->midi_instruments = NULL; + pClient->midi_maps = NULL; + pClient->midi_map_name = NULL; + lscp_driver_info_init(&(pClient->audio_driver_info)); + lscp_driver_info_init(&(pClient->midi_driver_info)); + lscp_device_info_init(&(pClient->audio_device_info)); + lscp_device_info_init(&(pClient->midi_device_info)); + lscp_param_info_init(&(pClient->audio_param_info)); + lscp_param_info_init(&(pClient->midi_param_info)); + lscp_device_port_info_init(&(pClient->audio_channel_info)); + lscp_device_port_info_init(&(pClient->midi_port_info)); + lscp_param_info_init(&(pClient->audio_channel_param_info)); + lscp_param_info_init(&(pClient->midi_port_param_info)); + lscp_server_info_init(&(pClient->server_info)); + lscp_engine_info_init(&(pClient->engine_info)); + lscp_channel_info_init(&(pClient->channel_info)); + lscp_midi_instrument_info_init(&(pClient->midi_instrument_info)); + // Initialize error stuff. + pClient->pszResult = NULL; + pClient->iErrno = -1; + // Stream usage stuff. + pClient->buffer_fill = NULL; + pClient->iStreamCount = 0; + // Default timeout value. + pClient->iTimeout = LSCP_TIMEOUT_MSECS; pClient->iTimeoutCount = 0; - // Initialize the transaction mutex. - lscp_mutex_init(pClient->mutex); - lscp_cond_init(pClient->cond); + // Initialize the transaction mutex. + lscp_mutex_init(pClient->mutex); + lscp_cond_init(pClient->cond); - // Finally we've some success... - return pClient; + // Finally we've some success... + return pClient; } @@ -368,17 +370,17 @@ */ lscp_status_t lscp_client_join ( lscp_client_t *pClient ) { - if (pClient == NULL) - return LSCP_FAILED; + if (pClient == NULL) + return LSCP_FAILED; #ifdef DEBUG - fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient); + fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient); #endif // lscp_socket_agent_join(&(pClient->evt)); - lscp_socket_agent_join(&(pClient->cmd)); + lscp_socket_agent_join(&(pClient->cmd)); - return LSCP_OK; + return LSCP_OK; } @@ -391,68 +393,73 @@ */ lscp_status_t lscp_client_destroy ( lscp_client_t *pClient ) { - if (pClient == NULL) - return LSCP_FAILED; + if (pClient == NULL) + return LSCP_FAILED; #ifdef DEBUG - fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient); + fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient); #endif - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // Free up all cached members. - lscp_midi_instrument_info_free(&(pClient->midi_instrument_info)); - lscp_channel_info_free(&(pClient->channel_info)); - lscp_engine_info_free(&(pClient->engine_info)); - lscp_server_info_free(&(pClient->server_info)); - lscp_param_info_free(&(pClient->midi_port_param_info)); - lscp_param_info_free(&(pClient->audio_channel_param_info)); - lscp_device_port_info_free(&(pClient->midi_port_info)); - lscp_device_port_info_free(&(pClient->audio_channel_info)); - lscp_param_info_free(&(pClient->midi_param_info)); - lscp_param_info_free(&(pClient->audio_param_info)); - lscp_device_info_free(&(pClient->midi_device_info)); - lscp_device_info_free(&(pClient->audio_device_info)); - lscp_driver_info_free(&(pClient->midi_driver_info)); - lscp_driver_info_free(&(pClient->audio_driver_info)); - // Free available engine table. - lscp_szsplit_destroy(pClient->audio_drivers); - lscp_szsplit_destroy(pClient->midi_drivers); - lscp_isplit_destroy(pClient->audio_devices); - lscp_isplit_destroy(pClient->midi_devices); - lscp_szsplit_destroy(pClient->engines); - lscp_isplit_destroy(pClient->channels); - lscp_midi_instruments_destroy(pClient->midi_instruments); - // Make them null. - pClient->audio_drivers = NULL; - pClient->midi_drivers = NULL; - pClient->audio_devices = NULL; - pClient->midi_devices = NULL; - pClient->engines = NULL; - pClient->channels = NULL; - pClient->midi_instruments = NULL; - // Free result error stuff. - lscp_client_set_result(pClient, NULL, 0); - // Free stream usage stuff. - if (pClient->buffer_fill) - free(pClient->buffer_fill); - pClient->buffer_fill = NULL; - pClient->iStreamCount = 0; - pClient->iTimeout = 0; - - // Free socket agents. - lscp_socket_agent_free(&(pClient->evt)); - lscp_socket_agent_free(&(pClient->cmd)); - - // Last but not least, free good ol'transaction mutex. - lscp_mutex_unlock(pClient->mutex); - lscp_mutex_destroy(pClient->mutex); - lscp_cond_destroy(pClient->cond); + // Free up all cached members. + lscp_midi_instrument_info_free(&(pClient->midi_instrument_info)); + lscp_channel_info_free(&(pClient->channel_info)); + lscp_engine_info_free(&(pClient->engine_info)); + lscp_server_info_free(&(pClient->server_info)); + lscp_param_info_free(&(pClient->midi_port_param_info)); + lscp_param_info_free(&(pClient->audio_channel_param_info)); + lscp_device_port_info_free(&(pClient->midi_port_info)); + lscp_device_port_info_free(&(pClient->audio_channel_info)); + lscp_param_info_free(&(pClient->midi_param_info)); + lscp_param_info_free(&(pClient->audio_param_info)); + lscp_device_info_free(&(pClient->midi_device_info)); + lscp_device_info_free(&(pClient->audio_device_info)); + lscp_driver_info_free(&(pClient->midi_driver_info)); + lscp_driver_info_free(&(pClient->audio_driver_info)); + // Free available engine table. + lscp_szsplit_destroy(pClient->audio_drivers); + lscp_szsplit_destroy(pClient->midi_drivers); + lscp_isplit_destroy(pClient->audio_devices); + lscp_isplit_destroy(pClient->midi_devices); + lscp_szsplit_destroy(pClient->engines); + lscp_isplit_destroy(pClient->channels); + lscp_midi_instruments_destroy(pClient->midi_instruments); + lscp_isplit_destroy(pClient->midi_maps); + if (pClient->midi_map_name) + free(pClient->midi_map_name); + // Make them null. + pClient->audio_drivers = NULL; + pClient->midi_drivers = NULL; + pClient->audio_devices = NULL; + pClient->midi_devices = NULL; + pClient->engines = NULL; + pClient->channels = NULL; + pClient->midi_instruments = NULL; + pClient->midi_maps = NULL; + pClient->midi_map_name = NULL; + // Free result error stuff. + lscp_client_set_result(pClient, NULL, 0); + // Free stream usage stuff. + if (pClient->buffer_fill) + free(pClient->buffer_fill); + pClient->buffer_fill = NULL; + pClient->iStreamCount = 0; + pClient->iTimeout = 0; + + // Free socket agents. + lscp_socket_agent_free(&(pClient->evt)); + lscp_socket_agent_free(&(pClient->cmd)); + + // Last but not least, free good ol'transaction mutex. + lscp_mutex_unlock(pClient->mutex); + lscp_mutex_destroy(pClient->mutex); + lscp_cond_destroy(pClient->cond); - free(pClient); + free(pClient); - return LSCP_OK; + return LSCP_OK; } @@ -466,13 +473,13 @@ */ lscp_status_t lscp_client_set_timeout ( lscp_client_t *pClient, int iTimeout ) { - if (pClient == NULL) - return LSCP_FAILED; - if (iTimeout < 0) - return LSCP_FAILED; + if (pClient == NULL) + return LSCP_FAILED; + if (iTimeout < 0) + return LSCP_FAILED; - pClient->iTimeout = iTimeout; - return LSCP_OK; + pClient->iTimeout = iTimeout; + return LSCP_OK; } @@ -485,10 +492,10 @@ */ int lscp_client_get_timeout ( lscp_client_t *pClient ) { - if (pClient == NULL) - return -1; + if (pClient == NULL) + return -1; - return pClient->iTimeout; + return pClient->iTimeout; } @@ -510,18 +517,21 @@ */ lscp_status_t lscp_client_query ( lscp_client_t *pClient, const char *pszQuery ) { - lscp_status_t ret; + lscp_status_t ret; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + if (pClient == NULL) + return LSCP_FAILED; - // Just make the now guarded call. - ret = lscp_client_call(pClient, pszQuery, 0); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Just make the now guarded call. + ret = lscp_client_call(pClient, pszQuery, 0); - return ret; + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return ret; } /** @@ -535,10 +545,10 @@ */ const char *lscp_client_get_result ( lscp_client_t *pClient ) { - if (pClient == NULL) - return NULL; + if (pClient == NULL) + return NULL; - return pClient->pszResult; + return pClient->pszResult; } @@ -552,10 +562,10 @@ */ int lscp_client_get_errno ( lscp_client_t *pClient ) { - if (pClient == NULL) - return -1; + if (pClient == NULL) + return -1; - return pClient->iErrno; + return pClient->iErrno; } @@ -574,36 +584,36 @@ */ lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient, lscp_event_t events ) { - lscp_status_t ret = LSCP_FAILED; + lscp_status_t ret = LSCP_FAILED; - if (pClient == NULL) - return LSCP_FAILED; + if (pClient == NULL) + return LSCP_FAILED; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // If applicable, start the alternate connection... - if (pClient->events == LSCP_EVENT_NONE) - ret = _lscp_client_evt_connect(pClient); - - // Send the subscription commands. - if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT)) - ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_COUNT); - if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT)) - ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT); - if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT)) - ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT); - if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL)) - ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL); - if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO)) - ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO); - if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS)) - ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS); + // If applicable, start the alternate connection... + if (pClient->events == LSCP_EVENT_NONE) + ret = _lscp_client_evt_connect(pClient); + + // Send the subscription commands. + if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT)) + ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_COUNT); + if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT)) + ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT); + if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT)) + ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT); + if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL)) + ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL); + if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO)) + ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO); + if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS)) + ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return ret; + return ret; } @@ -619,36 +629,36 @@ */ lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient, lscp_event_t events ) { - lscp_status_t ret = LSCP_OK; + lscp_status_t ret = LSCP_OK; - if (pClient == NULL) - return LSCP_FAILED; + if (pClient == NULL) + return LSCP_FAILED; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // Send the unsubscription commands. - if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT)) - ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_COUNT); - if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT)) - ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT); - if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT)) - ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT); - if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL)) - ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL); - if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO)) - ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO); - if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS)) - ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS); - - // If necessary, close the alternate connection... - if (pClient->events == LSCP_EVENT_NONE) - lscp_socket_agent_free(&(pClient->evt)); + // Send the unsubscription commands. + if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_COUNT)) + ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_COUNT); + if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT)) + ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT); + if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT)) + ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT); + if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL)) + ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL); + if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO)) + ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO); + if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS)) + ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS); + + // If necessary, close the alternate connection... + if (pClient->events == LSCP_EVENT_NONE) + lscp_socket_agent_free(&(pClient->evt)); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return ret; + return ret; } @@ -661,10 +671,10 @@ */ lscp_event_t lscp_client_get_events ( lscp_client_t *pClient ) { - if (pClient == NULL) - return LSCP_EVENT_NONE; + if (pClient == NULL) + return LSCP_EVENT_NONE; - return pClient->events; + return pClient->events; } @@ -684,13 +694,13 @@ */ lscp_status_t lscp_load_instrument ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (pszFileName == NULL || iSamplerChannel < 0) - return LSCP_FAILED; + if (pszFileName == NULL || iSamplerChannel < 0) + return LSCP_FAILED; - sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel); + return lscp_client_query(pClient, szQuery); } @@ -707,13 +717,13 @@ */ lscp_status_t lscp_load_instrument_non_modal ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (pszFileName == NULL || iSamplerChannel < 0) - return LSCP_FAILED; + if (pszFileName == NULL || iSamplerChannel < 0) + return LSCP_FAILED; - sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel); + return lscp_client_query(pClient, szQuery); } @@ -729,13 +739,13 @@ */ lscp_status_t lscp_load_engine ( lscp_client_t *pClient, const char *pszEngineName, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (pszEngineName == NULL || iSamplerChannel < 0) - return LSCP_FAILED; + if (pszEngineName == NULL || iSamplerChannel < 0) + return LSCP_FAILED; - sprintf(szQuery, "LOAD ENGINE %s %d\r\n", pszEngineName, iSamplerChannel); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "LOAD ENGINE %s %d\r\n", pszEngineName, iSamplerChannel); + return lscp_client_query(pClient, szQuery); } @@ -750,18 +760,21 @@ */ int lscp_get_channels ( lscp_client_t *pClient ) { - int iChannels = -1; + int iChannels = -1; + + if (pClient == NULL) + return -1; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - if (lscp_client_call(pClient, "GET CHANNELS\r\n", 0) == LSCP_OK) - iChannels = atoi(lscp_client_get_result(pClient)); + if (lscp_client_call(pClient, "GET CHANNELS\r\n", 0) == LSCP_OK) + iChannels = atoi(lscp_client_get_result(pClient)); - // Unlock this section doen. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section doen. + lscp_mutex_unlock(pClient->mutex); - return iChannels; + return iChannels; } @@ -776,26 +789,26 @@ */ int *lscp_list_channels ( lscp_client_t *pClient ) { - const char *pszSeps = ","; + const char *pszSeps = ","; - if (pClient == NULL) - return NULL; + if (pClient == NULL) + return NULL; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - if (pClient->channels) { - lscp_isplit_destroy(pClient->channels); - pClient->channels = NULL; - } + if (pClient->channels) { + lscp_isplit_destroy(pClient->channels); + pClient->channels = NULL; + } - if (lscp_client_call(pClient, "LIST CHANNELS\r\n", 0) == LSCP_OK) - pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps); + if (lscp_client_call(pClient, "LIST CHANNELS\r\n", 0) == LSCP_OK) + pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return pClient->channels; + return pClient->channels; } @@ -810,18 +823,21 @@ */ int lscp_add_channel ( lscp_client_t *pClient ) { - int iSamplerChannel = -1; + int iSamplerChannel = -1; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + if (pClient == NULL) + return -1; - if (lscp_client_call(pClient, "ADD CHANNEL\r\n", 0) == LSCP_OK) - iSamplerChannel = atoi(lscp_client_get_result(pClient)); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + if (lscp_client_call(pClient, "ADD CHANNEL\r\n", 0) == LSCP_OK) + iSamplerChannel = atoi(lscp_client_get_result(pClient)); - return iSamplerChannel; + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return iSamplerChannel; } @@ -836,13 +852,13 @@ */ lscp_status_t lscp_remove_channel ( lscp_client_t *pClient, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0) - return LSCP_FAILED; + if (iSamplerChannel < 0) + return LSCP_FAILED; - sprintf(szQuery, "REMOVE CHANNEL %d\r\n", iSamplerChannel); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "REMOVE CHANNEL %d\r\n", iSamplerChannel); + return lscp_client_query(pClient, szQuery); } @@ -857,18 +873,21 @@ */ int lscp_get_available_engines ( lscp_client_t *pClient ) { - int iAvailableEngines = -1; + int iAvailableEngines = -1; + + if (pClient == NULL) + return -1; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n", 0) == LSCP_OK) - iAvailableEngines = atoi(lscp_client_get_result(pClient)); + if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n", 0) == LSCP_OK) + iAvailableEngines = atoi(lscp_client_get_result(pClient)); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return iAvailableEngines; + return iAvailableEngines; } @@ -883,23 +902,26 @@ */ const char **lscp_list_available_engines ( lscp_client_t *pClient ) { - const char *pszSeps = ","; + const char *pszSeps = ","; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + if (pClient == NULL) + return NULL; - if (pClient->engines) { - lscp_szsplit_destroy(pClient->engines); - pClient->engines = NULL; - } + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - if (lscp_client_call(pClient, "LIST AVAILABLE_ENGINES\r\n", 0) == LSCP_OK) - pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps); + if (pClient->engines) { + lscp_szsplit_destroy(pClient->engines); + pClient->engines = NULL; + } - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + if (lscp_client_call(pClient, "LIST AVAILABLE_ENGINES\r\n", 0) == LSCP_OK) + pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps); - return (const char **) pClient->engines; + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return (const char **) pClient->engines; } @@ -915,47 +937,49 @@ */ lscp_engine_info_t *lscp_get_engine_info ( lscp_client_t *pClient, const char *pszEngineName ) { - lscp_engine_info_t *pEngineInfo; - char szQuery[LSCP_BUFSIZ]; - const char *pszResult; - const char *pszSeps = ":"; - const char *pszCrlf = "\r\n"; - char *pszToken; - char *pch; - - if (pszEngineName == NULL) - return NULL; - - // Lock this section up. - lscp_mutex_lock(pClient->mutex); - - pEngineInfo = &(pClient->engine_info); - lscp_engine_info_reset(pEngineInfo); - - sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName); - if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) { - pszResult = lscp_client_get_result(pClient); - pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); - while (pszToken) { - if (strcasecmp(pszToken, "DESCRIPTION") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pEngineInfo->description), &pszToken); - } - else if (strcasecmp(pszToken, "VERSION") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pEngineInfo->version), &pszToken); - } - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - } - } - else pEngineInfo = NULL; + lscp_engine_info_t *pEngineInfo; + char szQuery[LSCP_BUFSIZ]; + const char *pszResult; + const char *pszSeps = ":"; + const char *pszCrlf = "\r\n"; + char *pszToken; + char *pch; + + if (pClient == NULL) + return NULL; + if (pszEngineName == NULL) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + pEngineInfo = &(pClient->engine_info); + lscp_engine_info_reset(pEngineInfo); + + sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName); + if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) { + pszResult = lscp_client_get_result(pClient); + pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); + while (pszToken) { + if (strcasecmp(pszToken, "DESCRIPTION") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pEngineInfo->description), &pszToken); + } + else if (strcasecmp(pszToken, "VERSION") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pEngineInfo->version), &pszToken); + } + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + } + } + else pEngineInfo = NULL; - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return pEngineInfo; + return pEngineInfo; } @@ -971,115 +995,130 @@ */ lscp_channel_info_t *lscp_get_channel_info ( lscp_client_t *pClient, int iSamplerChannel ) { - lscp_channel_info_t *pChannelInfo; - char szQuery[LSCP_BUFSIZ]; - const char *pszResult; - const char *pszSeps = ":"; - const char *pszCrlf = "\r\n"; - char *pszToken; - char *pch; - - if (iSamplerChannel < 0) - return NULL; - - // Lock this section up. - lscp_mutex_lock(pClient->mutex); - - pChannelInfo = &(pClient->channel_info); - lscp_channel_info_reset(pChannelInfo); - - sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel); - if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) { - pszResult = lscp_client_get_result(pClient); - pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); - while (pszToken) { - if (strcasecmp(pszToken, "ENGINE_NAME") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pChannelInfo->engine_name), &pszToken); - } - else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) { - if (pChannelInfo->audio_routing) - lscp_szsplit_destroy(pChannelInfo->audio_routing); - pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ","); - } - } - else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pChannelInfo->instrument_file), &pszToken); - } - else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pChannelInfo->instrument_name), &pszToken); - } - else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) { - pszToken = lscp_ltrim(pszToken); - if (strcasecmp(pszToken, "ALL") == 0) - pChannelInfo->midi_channel = LSCP_MIDI_CHANNEL_ALL; - else - pChannelInfo->midi_channel = atoi(pszToken); - } - } - else if (strcasecmp(pszToken, "VOLUME") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "MUTE") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->mute = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0); - } - else if (strcasecmp(pszToken, "SOLO") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pChannelInfo->solo = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0); - } - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - } - } - else pChannelInfo = NULL; + lscp_channel_info_t *pChannelInfo; + char szQuery[LSCP_BUFSIZ]; + const char *pszResult; + const char *pszSeps = ":"; + const char *pszCrlf = "\r\n"; + char *pszToken; + char *pch; + + if (pClient == NULL) + return NULL; + if (iSamplerChannel < 0) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + pChannelInfo = &(pClient->channel_info); + lscp_channel_info_reset(pChannelInfo); + + sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel); + if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) { + pszResult = lscp_client_get_result(pClient); + pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); + while (pszToken) { + if (strcasecmp(pszToken, "ENGINE_NAME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pChannelInfo->engine_name), &pszToken); + } + else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) { + if (pChannelInfo->audio_routing) + lscp_szsplit_destroy(pChannelInfo->audio_routing); + pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ","); + } + } + else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pChannelInfo->instrument_file), &pszToken); + } + else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pChannelInfo->instrument_name), &pszToken); + } + else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) { + pszToken = lscp_ltrim(pszToken); + if (strcasecmp(pszToken, "ALL") == 0) + pChannelInfo->midi_channel = LSCP_MIDI_CHANNEL_ALL; + else + pChannelInfo->midi_channel = atoi(pszToken); + } + } + else if (strcasecmp(pszToken, "MIDI_INSTRUMENT_MAP") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) { + pszToken = lscp_ltrim(pszToken); + if (strcasecmp(pszToken, "NONE") == 0) + pChannelInfo->midi_map = LSCP_MIDI_MAP_NONE; + else + if (strcasecmp(pszToken, "DEFAULT") == 0) + pChannelInfo->midi_map = LSCP_MIDI_MAP_DEFAULT; + else + pChannelInfo->midi_map = atoi(pszToken); + } + } + else if (strcasecmp(pszToken, "VOLUME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "MUTE") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->mute = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0); + } + else if (strcasecmp(pszToken, "SOLO") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pChannelInfo->solo = (strcasecmp(lscp_unquote(&pszToken, 0), "TRUE") == 0); + } + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + } + } + else pChannelInfo = NULL; - // Unlock this section up. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section up. + lscp_mutex_unlock(pClient->mutex); - return pChannelInfo; + return pChannelInfo; } @@ -1094,23 +1133,25 @@ */ int lscp_get_channel_voice_count ( lscp_client_t *pClient, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; - int iVoiceCount = -1; + char szQuery[LSCP_BUFSIZ]; + int iVoiceCount = -1; - if (iSamplerChannel < 0) - return iVoiceCount; + if (pClient == NULL) + return -1; + if (iSamplerChannel < 0) + return -1; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel); - if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) - iVoiceCount = atoi(lscp_client_get_result(pClient)); + sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel); + if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) + iVoiceCount = atoi(lscp_client_get_result(pClient)); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return iVoiceCount; + return iVoiceCount; } @@ -1125,23 +1166,25 @@ */ int lscp_get_channel_stream_count ( lscp_client_t *pClient, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; - int iStreamCount = -1; + char szQuery[LSCP_BUFSIZ]; + int iStreamCount = -1; - if (iSamplerChannel < 0) - return iStreamCount; + if (pClient == NULL) + return -1; + if (iSamplerChannel < 0) + return -1; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel); - if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) - iStreamCount = atoi(lscp_client_get_result(pClient)); + sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel); + if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) + iStreamCount = atoi(lscp_client_get_result(pClient)); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return iStreamCount; + return iStreamCount; } @@ -1156,46 +1199,48 @@ */ int lscp_get_channel_stream_usage ( lscp_client_t *pClient, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; - int iStreamUsage = -1; - const char *pszResult; - const char *pszSeps = "[]%,"; - char *pszToken; - char *pch; - int iStream; - int iPercent; - - if (iSamplerChannel < 0) - return iStreamUsage; - - // Lock this section up. - lscp_mutex_lock(pClient->mutex); - - iStream = 0; - sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel); - if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) { - pszResult = lscp_client_get_result(pClient); - pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); - while (pszToken) { - if (*pszToken) { - // Skip stream id. - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - if (pszToken == NULL) - break; - // Get least buffer fill percentage. - iPercent = atol(pszToken); - if (iStreamUsage > iPercent || iStream == 0) - iStreamUsage = iPercent; - iStream++; - } - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - } - } + char szQuery[LSCP_BUFSIZ]; + int iStreamUsage = -1; + const char *pszResult; + const char *pszSeps = "[]%,"; + char *pszToken; + char *pch; + int iStream; + int iPercent; + + if (pClient == NULL) + return -1; + if (iSamplerChannel < 0) + return -1; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + iStream = 0; + sprintf(szQuery, "GET CHANNEL BUFFER_FILL PERCENTAGE %d\r\n", iSamplerChannel); + if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) { + pszResult = lscp_client_get_result(pClient); + pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); + while (pszToken) { + if (*pszToken) { + // Skip stream id. + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + if (pszToken == NULL) + break; + // Get least buffer fill percentage. + iPercent = atol(pszToken); + if (iStreamUsage > iPercent || iStream == 0) + iStreamUsage = iPercent; + iStream++; + } + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + } + } - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return iStreamUsage; + return iStreamUsage; } @@ -1215,64 +1260,64 @@ */ lscp_buffer_fill_t *lscp_get_channel_buffer_fill ( lscp_client_t *pClient, lscp_usage_t usage_type, int iSamplerChannel ) { - lscp_buffer_fill_t *pBufferFill; - char szQuery[LSCP_BUFSIZ]; - int iStreamCount; - const char *pszUsageType = (usage_type == LSCP_USAGE_BYTES ? "BYTES" : "PERCENTAGE"); - const char *pszResult; - const char *pszSeps = "[]%,"; - char *pszToken; - char *pch; - int iStream; - - // Retrieve a channel stream estimation. - iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel); - if (pClient->iStreamCount < 0) - return NULL; - - // Lock this section up. - lscp_mutex_lock(pClient->mutex); - - // Check if we need to reallocate the stream usage array. - if (pClient->iStreamCount != iStreamCount) { - if (pClient->buffer_fill) - free(pClient->buffer_fill); - if (iStreamCount > 0) - pClient->buffer_fill = (lscp_buffer_fill_t *) malloc(iStreamCount * sizeof(lscp_buffer_fill_t)); - else - pClient->buffer_fill = NULL; - pClient->iStreamCount = iStreamCount; - } - - // Get buffer fill usage... - pBufferFill = pClient->buffer_fill; - if (pBufferFill && iStreamCount > 0) { - iStream = 0; - pBufferFill = pClient->buffer_fill; - sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel); - if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) { - pszResult = lscp_client_get_result(pClient); - pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); - while (pszToken && iStream < pClient->iStreamCount) { - if (*pszToken) { - pBufferFill[iStream].stream_id = atol(pszToken); - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - if (pszToken == NULL) - break; - pBufferFill[iStream].stream_usage = atol(pszToken); - iStream++; - } - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - } - } // Reset the usage, whatever it was before. - else while (iStream < pClient->iStreamCount) - pBufferFill[iStream++].stream_usage = 0; - } + lscp_buffer_fill_t *pBufferFill; + char szQuery[LSCP_BUFSIZ]; + int iStreamCount; + const char *pszUsageType = (usage_type == LSCP_USAGE_BYTES ? "BYTES" : "PERCENTAGE"); + const char *pszResult; + const char *pszSeps = "[]%,"; + char *pszToken; + char *pch; + int iStream; + + // Retrieve a channel stream estimation. + iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel); + if (iStreamCount < 0) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + // Check if we need to reallocate the stream usage array. + if (pClient->iStreamCount != iStreamCount) { + if (pClient->buffer_fill) + free(pClient->buffer_fill); + if (iStreamCount > 0) + pClient->buffer_fill = (lscp_buffer_fill_t *) malloc(iStreamCount * sizeof(lscp_buffer_fill_t)); + else + pClient->buffer_fill = NULL; + pClient->iStreamCount = iStreamCount; + } + + // Get buffer fill usage... + pBufferFill = pClient->buffer_fill; + if (pBufferFill && iStreamCount > 0) { + iStream = 0; + pBufferFill = pClient->buffer_fill; + sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel); + if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) { + pszResult = lscp_client_get_result(pClient); + pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); + while (pszToken && iStream < pClient->iStreamCount) { + if (*pszToken) { + pBufferFill[iStream].stream_id = atol(pszToken); + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + if (pszToken == NULL) + break; + pBufferFill[iStream].stream_usage = atol(pszToken); + iStream++; + } + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + } + } // Reset the usage, whatever it was before. + else while (iStream < pClient->iStreamCount) + pBufferFill[iStream++].stream_usage = 0; + } - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return pBufferFill; + return pBufferFill; } @@ -1288,13 +1333,13 @@ */ lscp_status_t lscp_set_channel_audio_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszAudioDriver ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || pszAudioDriver == NULL) - return LSCP_FAILED; + if (iSamplerChannel < 0 || pszAudioDriver == NULL) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_TYPE %d %s\r\n", iSamplerChannel, pszAudioDriver); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_TYPE %d %s\r\n", iSamplerChannel, pszAudioDriver); + return lscp_client_query(pClient, szQuery); } @@ -1310,13 +1355,13 @@ */ lscp_status_t lscp_set_channel_audio_device ( lscp_client_t *pClient, int iSamplerChannel, int iAudioDevice ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || iAudioDevice < 0) - return LSCP_FAILED; + if (iSamplerChannel < 0 || iAudioDevice < 0) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice); + return lscp_client_query(pClient, szQuery); } @@ -1333,13 +1378,13 @@ */ lscp_status_t lscp_set_channel_audio_channel ( lscp_client_t *pClient, int iSamplerChannel, int iAudioOut, int iAudioIn ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0) - return LSCP_FAILED; + if (iSamplerChannel < 0 || iAudioOut < 0 || iAudioIn < 0) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNEL %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_CHANNEL %d %d %d\r\n", iSamplerChannel, iAudioOut, iAudioIn); + return lscp_client_query(pClient, szQuery); } @@ -1355,13 +1400,13 @@ */ lscp_status_t lscp_set_channel_midi_type ( lscp_client_t *pClient, int iSamplerChannel, const char *pszMidiDriver ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || pszMidiDriver == NULL) - return LSCP_FAILED; + if (iSamplerChannel < 0 || pszMidiDriver == NULL) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL MIDI_INPUT_TYPE %d %s\r\n", iSamplerChannel, pszMidiDriver); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL MIDI_INPUT_TYPE %d %s\r\n", iSamplerChannel, pszMidiDriver); + return lscp_client_query(pClient, szQuery); } @@ -1377,13 +1422,13 @@ */ lscp_status_t lscp_set_channel_midi_device ( lscp_client_t *pClient, int iSamplerChannel, int iMidiDevice ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || iMidiDevice < 0) - return LSCP_FAILED; + if (iSamplerChannel < 0 || iMidiDevice < 0) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice); + return lscp_client_query(pClient, szQuery); } @@ -1399,13 +1444,13 @@ */ lscp_status_t lscp_set_channel_midi_port ( lscp_client_t *pClient, int iSamplerChannel, int iMidiPort ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || iMidiPort < 0) - return LSCP_FAILED; + if (iSamplerChannel < 0 || iMidiPort < 0) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL MIDI_INPUT_PORT %d %d\r\n", iSamplerChannel, iMidiPort); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL MIDI_INPUT_PORT %d %d\r\n", iSamplerChannel, iMidiPort); + return lscp_client_query(pClient, szQuery); } @@ -1416,22 +1461,56 @@ * @param pClient Pointer to client instance structure. * @param iSamplerChannel Sampler channel number. * @param iMidiChannel MIDI channel address number to listen (0-15) or - * LSCP_MIDI_CHANNEL_ALL (16) to listen on all channels. + * @ref LSCP_MIDI_CHANNEL_ALL (16) to listen on all channels. * * @returns LSCP_OK on success, LSCP_FAILED otherwise. */ lscp_status_t lscp_set_channel_midi_channel ( lscp_client_t *pClient, int iSamplerChannel, int iMidiChannel ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; + + if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16) + return LSCP_FAILED; + + if (iMidiChannel == LSCP_MIDI_CHANNEL_ALL) + sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel); + else + sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel); + return lscp_client_query(pClient, szQuery); +} + + +/** + * Setting MIDI instrument map: + * SET CHANNEL MIDI_INSTRUMENT_MAP + * + * @param pClient Pointer to client instance structure. + * @param iSamplerChannel Sampler channel number. + * @param iMidiMap MIDI instrument map number, or either + * @ref LSCP_MIDI_MAP_NONE or + * @ref LSCP_MIDI_MAP_DEFAULT . + * + * @returns LSCP_OK on success, LSCP_FAILED otherwise. + */ +lscp_status_t lscp_set_channel_midi_map ( lscp_client_t *pClient, int iSamplerChannel, int iMidiMap ) +{ + char szQuery[LSCP_BUFSIZ]; + + if (iSamplerChannel < 0) + return LSCP_FAILED; + + sprintf(szQuery, "SET CHANNEL MIDI_INSTRUMENT_MAP %d ", iSamplerChannel); + if (iMidiMap == LSCP_MIDI_MAP_NONE) + strcat(szQuery , "NONE"); + else + if (iMidiMap == LSCP_MIDI_MAP_DEFAULT) + strcat(szQuery , "DEFAULT"); + else + sprintf(szQuery + strlen(szQuery), "%d", iMidiMap); - if (iSamplerChannel < 0 || iMidiChannel < 0 || iMidiChannel > 16) - return LSCP_FAILED; + strcat(szQuery, "\r\n"); - if (iMidiChannel == LSCP_MIDI_CHANNEL_ALL) - sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d ALL\r\n", iSamplerChannel); - else - sprintf(szQuery, "SET CHANNEL MIDI_INPUT_CHANNEL %d %d\r\n", iSamplerChannel, iMidiChannel); - return lscp_client_query(pClient, szQuery); + return lscp_client_query(pClient, szQuery); } @@ -1449,13 +1528,13 @@ */ lscp_status_t lscp_set_channel_volume ( lscp_client_t *pClient, int iSamplerChannel, float fVolume ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || fVolume < 0.0) - return LSCP_FAILED; + if (iSamplerChannel < 0 || fVolume < 0.0) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL VOLUME %d %g\r\n", iSamplerChannel, fVolume); + return lscp_client_query(pClient, szQuery); } @@ -1473,13 +1552,13 @@ */ lscp_status_t lscp_set_channel_mute ( lscp_client_t *pClient, int iSamplerChannel, int iMute ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || iMute < 0 || iMute > 1) - return LSCP_FAILED; + if (iSamplerChannel < 0 || iMute < 0 || iMute > 1) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL MUTE %d %d\r\n", iSamplerChannel, iMute); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL MUTE %d %d\r\n", iSamplerChannel, iMute); + return lscp_client_query(pClient, szQuery); } @@ -1497,13 +1576,13 @@ */ lscp_status_t lscp_set_channel_solo ( lscp_client_t *pClient, int iSamplerChannel, int iSolo ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0 || iSolo < 0 || iSolo > 1) - return LSCP_FAILED; + if (iSamplerChannel < 0 || iSolo < 0 || iSolo > 1) + return LSCP_FAILED; - sprintf(szQuery, "SET CHANNEL SOLO %d %d\r\n", iSamplerChannel, iSolo); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "SET CHANNEL SOLO %d %d\r\n", iSamplerChannel, iSolo); + return lscp_client_query(pClient, szQuery); } @@ -1518,13 +1597,13 @@ */ lscp_status_t lscp_reset_channel ( lscp_client_t *pClient, int iSamplerChannel ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (iSamplerChannel < 0) - return LSCP_FAILED; + if (iSamplerChannel < 0) + return LSCP_FAILED; - sprintf(szQuery, "RESET CHANNEL %d\r\n", iSamplerChannel); - return lscp_client_query(pClient, szQuery); + sprintf(szQuery, "RESET CHANNEL %d\r\n", iSamplerChannel); + return lscp_client_query(pClient, szQuery); } @@ -1538,7 +1617,7 @@ */ lscp_status_t lscp_reset_sampler ( lscp_client_t *pClient ) { - return lscp_client_query(pClient, "RESET\r\n"); + return lscp_client_query(pClient, "RESET\r\n"); } @@ -1553,42 +1632,50 @@ */ lscp_server_info_t *lscp_get_server_info ( lscp_client_t *pClient ) { - lscp_server_info_t *pServerInfo; - const char *pszResult; - const char *pszSeps = ":"; - const char *pszCrlf = "\r\n"; - char *pszToken; - char *pch; - - // Lock this section up. - lscp_mutex_lock(pClient->mutex); - - pServerInfo = &(pClient->server_info); - lscp_server_info_reset(pServerInfo); - - if (lscp_client_call(pClient, "GET SERVER INFO\r\n", 1) == LSCP_OK) { - pszResult = lscp_client_get_result(pClient); - pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); - while (pszToken) { - if (strcasecmp(pszToken, "DESCRIPTION") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pServerInfo->description), &pszToken); - } - else if (strcasecmp(pszToken, "VERSION") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pServerInfo->version), &pszToken); - } - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - } - } - else pServerInfo = NULL; + lscp_server_info_t *pServerInfo; + const char *pszResult; + const char *pszSeps = ":"; + const char *pszCrlf = "\r\n"; + char *pszToken; + char *pch; + + if (pClient == NULL) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + pServerInfo = &(pClient->server_info); + lscp_server_info_reset(pServerInfo); + + if (lscp_client_call(pClient, "GET SERVER INFO\r\n", 1) == LSCP_OK) { + pszResult = lscp_client_get_result(pClient); + pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); + while (pszToken) { + if (strcasecmp(pszToken, "DESCRIPTION") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pServerInfo->description), &pszToken); + } + else if (strcasecmp(pszToken, "VERSION") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pServerInfo->version), &pszToken); + } + else if (strcasecmp(pszToken, "PROTOCOL_VERSION") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pServerInfo->protocol_version), &pszToken); + } + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + } + } + else pServerInfo = NULL; - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return pServerInfo; + return pServerInfo; } @@ -1603,18 +1690,21 @@ */ int lscp_get_total_voice_count ( lscp_client_t *pClient ) { - int iVoiceCount = -1; + int iVoiceCount = -1; - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + if (pClient == NULL) + return -1; - if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT\r\n", 0) == LSCP_OK) - iVoiceCount = atoi(lscp_client_get_result(pClient)); + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT\r\n", 0) == LSCP_OK) + iVoiceCount = atoi(lscp_client_get_result(pClient)); - return iVoiceCount; + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return iVoiceCount; } @@ -1629,25 +1719,229 @@ */ int lscp_get_total_voice_count_max ( lscp_client_t *pClient ) { - int iVoiceCount = -1; + int iVoiceCount = -1; + + if (pClient == NULL) + return -1; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT_MAX\r\n", 0) == LSCP_OK) + iVoiceCount = atoi(lscp_client_get_result(pClient)); + + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return iVoiceCount; +} + + +/** + * Create a new MIDI instrument map: + * ADD MIDI_INSTRUMENT_MAP [] + * + * @param pClient Pointer to client instance structure. + * @param pszMapName MIDI instrument map name (optional) + * + * @returns The new MIDI instrument map number identifier, + * or -1 in case of failure. + */ +int lscp_add_midi_instrument_map ( lscp_client_t *pClient, const char *pszMapName ) +{ + int iMidiMap = -1; + char szQuery[LSCP_BUFSIZ]; + + if (pClient == NULL) + return -1; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + strcpy(szQuery, "ADD MIDI_INSTRUMENT_MAP"); + + if (pszMapName) + sprintf(szQuery + strlen(szQuery), " '%s'", pszMapName); + + strcat(szQuery, "\r\n"); + + if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) + iMidiMap = atoi(lscp_client_get_result(pClient)); + + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return iMidiMap; +} + + +/** + * Delete one particular or all MIDI instrument maps: + * REMOVE MIDI_INSTRUMENT_MAP + * + * @param pClient Pointer to client instance structure. + * @param iMidiMap MIDI instrument map number. + * + * @returns LSCP_OK on success, LSCP_FAILED otherwise. + */ +lscp_status_t lscp_remove_midi_instrument_map ( lscp_client_t *pClient, int iMidiMap ) +{ + char szQuery[LSCP_BUFSIZ]; + + if (iMidiMap < 0) + return LSCP_FAILED; + + sprintf(szQuery, "REMOVE MIDI_INSTRUMENT_MAP %d\r\n", iMidiMap); + + return lscp_client_query(pClient, szQuery); +} + + +/** + * Get amount of existing MIDI instrument maps: + * GET MIDI_INSTRUMENT_MAPS + * + * @param pClient Pointer to client instance structure. + * + * @returns The current total number of MIDI instrument maps + * on success, -1 otherwise. + */ +int lscp_get_midi_instrument_maps ( lscp_client_t *pClient ) +{ + int iMidiMaps = -1; + + if (pClient == NULL) + return -1; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + if (lscp_client_call(pClient, "GET MIDI_INSTRUMENT_MAPS\r\n", 0) == LSCP_OK) + iMidiMaps = atoi(lscp_client_get_result(pClient)); + + // Unlock this section doen. + lscp_mutex_unlock(pClient->mutex); + + return iMidiMaps; +} + + +/** + * Getting all created MIDI instrument maps: + * LIST MIDI_INSTRUMENT_MAPS + * + * @param pClient Pointer to client instance structure. + * + * @returns An array of the MIDI instrument map identifiers as positive + * integers, terminated with -1 on success, NULL otherwise. + */ +int *lscp_list_midi_instrument_maps ( lscp_client_t *pClient ) +{ + const char *pszSeps = ","; + + if (pClient == NULL) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + if (pClient->midi_maps) { + lscp_isplit_destroy(pClient->midi_maps); + pClient->midi_maps = NULL; + } - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + if (lscp_client_call(pClient, "LIST MIDI_INSTRUMENT_MAPS\r\n", 0) == LSCP_OK) + pClient->midi_maps = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps); - if (lscp_client_call(pClient, "GET TOTAL_VOICE_COUNT_MAX\r\n", 0) == LSCP_OK) - iVoiceCount = atoi(lscp_client_get_result(pClient)); + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + return pClient->midi_maps; +} + + +/** + * Getting a MIDI instrument map name: + * GET MIDI_INSTRUMENT_MAP INFO + * + * @param pClient Pointer to client instance structure. + * @param iMidiMap MIDI instrument map number. + * + * @returns The MIDI instrument map name on success, NULL on failure. + */ +const char *lscp_get_midi_instrument_map_name ( lscp_client_t *pClient, int iMidiMap ) +{ + char szQuery[LSCP_BUFSIZ]; + const char *pszResult; + const char *pszSeps = ":"; + const char *pszCrlf = "\r\n"; + char *pszToken; + char *pch; + + if (pClient == NULL) + return NULL; + if (iMidiMap < 0) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + if (pClient->midi_map_name) { + free(pClient->midi_map_name); + pClient->midi_map_name = NULL; + } + + sprintf(szQuery, "GET MIDI_INSTRUMENT_MAP INFO %d\r\n", iMidiMap); + if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) { + pszResult = lscp_client_get_result(pClient); + pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); + while (pszToken) { + if (strcasecmp(pszToken, "NAME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pClient->midi_map_name), &pszToken); + } + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + } + } + + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); - return iVoiceCount; + return pClient->midi_map_name; +} + + +/** + * Renaming a MIDI instrument map: + * SET MIDI_INSTRUMENT_MAP NAME + * + * @param pClient Pointer to client instance structure. + * @param iMidiMap MIDI instrument map number. + * @param pszMapName MIDI instrument map name. + * + * @returns LSCP_OK on success, LSCP_FAILED otherwise. + */ +lscp_status_t lscp_set_midi_instrument_map_name ( lscp_client_t *pClient, int iMidiMap, const char *pszMapName ) +{ + char szQuery[LSCP_BUFSIZ]; + + if (iMidiMap < 0) + return LSCP_FAILED; + if (pszMapName == NULL) + return LSCP_FAILED; + + sprintf(szQuery, "SET MIDI_INSTRUMENT_MAP NAME %d '%s'\r\n", + iMidiMap, pszMapName); + + return lscp_client_query(pClient, szQuery); } /** * Create or replace a MIDI instrumnet map entry: - * MAP MIDI_INSTRUMENT - * [] + * MAP MIDI_INSTRUMENT + * [ []} * * @param pClient Pointer to client instance structure. * @param pMidiInstr MIDI instrument bank and program parameter key. @@ -1663,57 +1957,57 @@ * @ref LSCP_LOAD_ON_DEMAND, or * @ref LSCP_LOAD_ON_DEMAND_HOLD, or * @ref LSCP_LOAD_PERSISTENT. - * @param pszName Instrument custom name for the map entry. + * @param pszName Instrument custom name for the map entry (optional). * * @returns LSCP_OK on success, LSCP_FAILED otherwise. */ 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 ) { - char szQuery[LSCP_BUFSIZ]; - - if (pMidiInstr->bank_msb < 0 || pMidiInstr->bank_msb > 127) - return LSCP_FAILED; - if (pMidiInstr->bank_lsb < 0 || pMidiInstr->bank_lsb > 127) - return LSCP_FAILED; - if (pMidiInstr->program < 0 || pMidiInstr->program > 127) - return LSCP_FAILED; - if (pszEngineName == NULL || pszFileName == NULL) - return LSCP_FAILED; - - if (fVolume < 0.0f) - fVolume = 1.0f; - - sprintf(szQuery, "MAP MIDI_INSTRUMENT %d %d %d %s '%s' %d %g", - pMidiInstr->bank_msb, pMidiInstr->bank_lsb, pMidiInstr->program, - pszEngineName, pszFileName, iInstrIndex, fVolume); - - switch (load_mode) { - case LSCP_LOAD_PERSISTENT: - strcat(szQuery, " PERSISTENT"); - break; - case LSCP_LOAD_ON_DEMAND_HOLD: - strcat(szQuery, " ON_DEMAND_HOLD"); - break; - case LSCP_LOAD_ON_DEMAND: - strcat(szQuery, " ON_DEMAND_HOLD"); - break; - case LSCP_LOAD_DEFAULT: - default: - break; - } - - if (pszName) - sprintf(szQuery + strlen(szQuery), " '%s'", pszName); - - strcat(szQuery, "\r\n"); - - return lscp_client_query(pClient, szQuery); + char szQuery[LSCP_BUFSIZ]; + + if (pMidiInstr->map < 0) + return LSCP_FAILED; + if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383) + return LSCP_FAILED; + if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127) + return LSCP_FAILED; + if (pszEngineName == NULL || pszFileName == NULL) + return LSCP_FAILED; + + if (fVolume < 0.0f) + fVolume = 1.0f; + + sprintf(szQuery, "MAP MIDI_INSTRUMENT %d %d %d %s '%s' %d %g", + pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog, + pszEngineName, pszFileName, iInstrIndex, fVolume); + + switch (load_mode) { + case LSCP_LOAD_PERSISTENT: + strcat(szQuery, " PERSISTENT"); + break; + case LSCP_LOAD_ON_DEMAND_HOLD: + strcat(szQuery, " ON_DEMAND_HOLD"); + break; + case LSCP_LOAD_ON_DEMAND: + strcat(szQuery, " ON_DEMAND"); + break; + case LSCP_LOAD_DEFAULT: + default: + break; + } + + if (pszName) + sprintf(szQuery + strlen(szQuery), " '%s'", pszName); + + strcat(szQuery, "\r\n"); + + return lscp_client_query(pClient, szQuery); } /** * Remove an entry from the MIDI instrument map: - * UNMAP MIDI_INSTRUMENT + * UNMAP MIDI_INSTRUMENT * * @param pClient Pointer to client instance structure. * @param pMidiInstr MIDI instrument bank and program parameter key. @@ -1722,83 +2016,109 @@ */ lscp_status_t lscp_unmap_midi_instrument ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr ) { - char szQuery[LSCP_BUFSIZ]; + char szQuery[LSCP_BUFSIZ]; - if (pMidiInstr->bank_msb < 0 || pMidiInstr->bank_msb > 127) - return LSCP_FAILED; - if (pMidiInstr->bank_lsb < 0 || pMidiInstr->bank_lsb > 127) - return LSCP_FAILED; - if (pMidiInstr->program < 0 || pMidiInstr->program > 127) - return LSCP_FAILED; + if (pMidiInstr->map < 0) + return LSCP_FAILED; + if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383) + return LSCP_FAILED; + if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127) + return LSCP_FAILED; - sprintf(szQuery, "UNMAP MIDI_INSTRUMENT %d %d %d\r\n", - pMidiInstr->bank_msb, pMidiInstr->bank_lsb, pMidiInstr->program); + sprintf(szQuery, "UNMAP MIDI_INSTRUMENT %d %d %d\r\n", + pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog); - return lscp_client_query(pClient, szQuery); + return lscp_client_query(pClient, szQuery); } /** * Get the total count of MIDI instrument map entries: - * GET MIDI_INSTRUMENTS + * GET MIDI_INSTRUMENTS ALL| * * @param pClient Pointer to client instance structure. + * @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL . * * @returns The current total number of MIDI instrument map entries * on success, -1 otherwise. */ -int lscp_get_midi_instruments ( lscp_client_t *pClient ) +int lscp_get_midi_instruments ( lscp_client_t *pClient, int iMidiMap ) { - int iInstruments = -1; + int iInstruments = -1; + char szQuery[LSCP_BUFSIZ]; + + if (pClient == NULL) + return -1; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + strcpy(szQuery, "GET MIDI_INSTRUMENTS "); - if (lscp_client_call(pClient, "GET MIDI_INSTRUMENTS\r\n", 0) == LSCP_OK) - iInstruments = atoi(lscp_client_get_result(pClient)); + if (iMidiMap < 0) + strcat(szQuery, "ALL"); + else + sprintf(szQuery + strlen(szQuery), "%d", iMidiMap); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + strcat(szQuery, "\r\n"); - return iInstruments; + if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) + iInstruments = atoi(lscp_client_get_result(pClient)); + + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return iInstruments; } /** * Getting indeces of all MIDI instrument map entries: - * LIST MIDI_INSTRUMENTS + * LIST MIDI_INSTRUMENTS ALL| * * @param pClient Pointer to client instance structure. + * @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL . * * @returns An array of @ref lscp_midi_instrument_t, terminated with the * {-1,-1,-1} triplet, NULL otherwise. */ -lscp_midi_instrument_t *lscp_list_midi_instruments ( lscp_client_t *pClient ) +lscp_midi_instrument_t *lscp_list_midi_instruments ( lscp_client_t *pClient, int iMidiMap ) { - if (pClient == NULL) - return NULL; + char szQuery[LSCP_BUFSIZ]; + + if (pClient == NULL) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); - // Lock this section up. - lscp_mutex_lock(pClient->mutex); + if (pClient->midi_instruments) { + lscp_midi_instruments_destroy(pClient->midi_instruments); + pClient->midi_instruments = NULL; + } - if (pClient->midi_instruments) { - lscp_midi_instruments_destroy(pClient->midi_instruments); - pClient->midi_instruments = NULL; - } + strcpy(szQuery, "LIST MIDI_INSTRUMENTS "); - if (lscp_client_call(pClient, "LIST MIDI_INSTRUMENTS\r\n", 0) == LSCP_OK) - pClient->midi_instruments = lscp_midi_instruments_create(lscp_client_get_result(pClient)); + if (iMidiMap < 0) + strcat(szQuery, "ALL"); + else + sprintf(szQuery + strlen(szQuery), "%d", iMidiMap); - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); + strcat(szQuery, "\r\n"); - return pClient->midi_instruments; + if (lscp_client_call(pClient, szQuery, 0) == LSCP_OK) + pClient->midi_instruments = lscp_midi_instruments_create(lscp_client_get_result(pClient)); + + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return pClient->midi_instruments; } /** * Getting information about a MIDI instrument map entry: - * GET MIDI_INSTRUMENT INFO + * GET MIDI_INSTRUMENT INFO * * @param pClient Pointer to client instance structure. * @param pMidiInstr MIDI instrument bank and program parameter key. @@ -1809,103 +2129,116 @@ */ lscp_midi_instrument_info_t *lscp_get_midi_instrument_info ( lscp_client_t *pClient, lscp_midi_instrument_t *pMidiInstr ) { - lscp_midi_instrument_info_t *pInstrInfo; - char szQuery[LSCP_BUFSIZ]; - const char *pszResult; - const char *pszSeps = ":"; - const char *pszCrlf = "\r\n"; - char *pszToken; - char *pch; - - if (pMidiInstr->bank_msb < 0 || pMidiInstr->bank_msb > 127) - return NULL; - if (pMidiInstr->bank_lsb < 0 || pMidiInstr->bank_lsb > 127) - return NULL; - if (pMidiInstr->program < 0 || pMidiInstr->program > 127) - return NULL; - - // Lock this section up. - lscp_mutex_lock(pClient->mutex); - - pInstrInfo = &(pClient->midi_instrument_info); - lscp_midi_instrument_info_reset(pInstrInfo); - - sprintf(szQuery, "GET MIDI_INSTRUMENT INFO %d %d %d\r\n", - pMidiInstr->bank_msb, pMidiInstr->bank_lsb, pMidiInstr->program); - if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) { - pszResult = lscp_client_get_result(pClient); - pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); - while (pszToken) { - if (strcasecmp(pszToken, "NAME") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pInstrInfo->name), &pszToken); - } - else if (strcasecmp(pszToken, "ENGINE_NAME") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pInstrInfo->engine_name), &pszToken); - } - else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pInstrInfo->instrument_file), &pszToken); - } - else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pInstrInfo->instrument_nr = atoi(lscp_ltrim(pszToken)); - } - else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - lscp_unquote_dup(&(pInstrInfo->instrument_name), &pszToken); - } - else if (strcasecmp(pszToken, "LOAD_MODE") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) { - pszToken = lscp_ltrim(pszToken); - if (strcasecmp(pszToken, "ON_DEMAND") == 0) - pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND; - else - if (strcasecmp(pszToken, "ON_DEMAND_HOLD") == 0) - pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND_HOLD; - else - if (strcasecmp(pszToken, "PERSISTENT") == 0) - pInstrInfo->load_mode = LSCP_LOAD_PERSISTENT; - else - pInstrInfo->load_mode = LSCP_LOAD_DEFAULT; - } - } - else if (strcasecmp(pszToken, "VOLUME") == 0) { - pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); - if (pszToken) - pInstrInfo->volume = (float) atof(lscp_ltrim(pszToken)); - } - pszToken = lscp_strtok(NULL, pszSeps, &(pch)); - } - } - else pInstrInfo = NULL; - - // Unlock this section down. - lscp_mutex_unlock(pClient->mutex); - - return pInstrInfo; + lscp_midi_instrument_info_t *pInstrInfo; + char szQuery[LSCP_BUFSIZ]; + const char *pszResult; + const char *pszSeps = ":"; + const char *pszCrlf = "\r\n"; + char *pszToken; + char *pch; + + if (pClient == NULL) + return NULL; + if (pMidiInstr->map < 0) + return NULL; + if (pMidiInstr->bank < 0 || pMidiInstr->bank > 16383) + return NULL; + if (pMidiInstr->prog < 0 || pMidiInstr->prog > 127) + return NULL; + + // Lock this section up. + lscp_mutex_lock(pClient->mutex); + + pInstrInfo = &(pClient->midi_instrument_info); + lscp_midi_instrument_info_reset(pInstrInfo); + + sprintf(szQuery, "GET MIDI_INSTRUMENT INFO %d %d %d\r\n", + pMidiInstr->map, pMidiInstr->bank, pMidiInstr->prog); + if (lscp_client_call(pClient, szQuery, 1) == LSCP_OK) { + pszResult = lscp_client_get_result(pClient); + pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); + while (pszToken) { + if (strcasecmp(pszToken, "NAME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pInstrInfo->name), &pszToken); + } + else if (strcasecmp(pszToken, "ENGINE_NAME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pInstrInfo->engine_name), &pszToken); + } + else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pInstrInfo->instrument_file), &pszToken); + } + else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pInstrInfo->instrument_nr = atoi(lscp_ltrim(pszToken)); + } + else if (strcasecmp(pszToken, "INSTRUMENT_NAME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + lscp_unquote_dup(&(pInstrInfo->instrument_name), &pszToken); + } + else if (strcasecmp(pszToken, "LOAD_MODE") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) { + pszToken = lscp_ltrim(pszToken); + if (strcasecmp(pszToken, "ON_DEMAND") == 0) + pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND; + else + if (strcasecmp(pszToken, "ON_DEMAND_HOLD") == 0) + pInstrInfo->load_mode = LSCP_LOAD_ON_DEMAND_HOLD; + else + if (strcasecmp(pszToken, "PERSISTENT") == 0) + pInstrInfo->load_mode = LSCP_LOAD_PERSISTENT; + else + pInstrInfo->load_mode = LSCP_LOAD_DEFAULT; + } + } + else if (strcasecmp(pszToken, "VOLUME") == 0) { + pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); + if (pszToken) + pInstrInfo->volume = (float) atof(lscp_ltrim(pszToken)); + } + pszToken = lscp_strtok(NULL, pszSeps, &(pch)); + } + } + else pInstrInfo = NULL; + + // Unlock this section down. + lscp_mutex_unlock(pClient->mutex); + + return pInstrInfo; } /** * Clear the MIDI instrumnet map: - * CLEAR MIDI_INSTRUMENTS + * CLEAR MIDI_INSTRUMENTS ALL| * - * @param pClient Pointer to client instance structure. - * @param iSamplerChannel Sampler channel number. + * @param pClient Pointer to client instance structure. + * @param iMidiMap MIDI instrument map number, or @ref LSCP_MIDI_MAP_ALL . * * @returns LSCP_OK on success, LSCP_FAILED otherwise. */ -lscp_status_t lscp_clear_midi_instruments ( lscp_client_t *pClient ) +lscp_status_t lscp_clear_midi_instruments ( lscp_client_t *pClient, int iMidiMap ) { - return lscp_client_query(pClient, "CLEAR MIDI_INSTRUMENTS\r\n"); + char szQuery[LSCP_BUFSIZ]; + + strcpy(szQuery, "CLEAR MIDI_INSTRUMENTS "); + + if (iMidiMap < 0) + strcat(szQuery, "ALL"); + else + sprintf(szQuery + strlen(szQuery), "%d", iMidiMap); + + strcat(szQuery, "\r\n"); + + return lscp_client_query(pClient, szQuery); }