/[svn]/liblscp/trunk/examples/example_client.c
ViewVC logotype

Contents of /liblscp/trunk/examples/example_client.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 188 - (show annotations) (download)
Thu Jul 8 09:13:36 2004 UTC (17 years, 11 months ago) by capela
File MIME type: text/plain
File size: 16343 byte(s)
- lscp_set_channel_audio_channel() is using a wrong command syntax: fixed.
- device configuration functions missing on documention main page: added.

1 // example_client.c
2 //
3 /****************************************************************************
4 Copyright (C) 2004, rncbc aka Rui Nuno Capela. All rights reserved.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 *****************************************************************************/
21
22 #include "lscp/client.h"
23 #include "lscp/device.h"
24
25 #include <time.h>
26
27 #define SERVER_PORT 8888
28
29 #if defined(WIN32)
30 static WSADATA _wsaData;
31 #endif
32
33 ////////////////////////////////////////////////////////////////////////
34
35 lscp_status_t client_callback ( lscp_client_t *pClient, lscp_event_t event, const char *pchData, int cchData, void *pvData )
36 {
37 lscp_status_t ret = LSCP_FAILED;
38
39 char *pszData = (char *) malloc(cchData + 1);
40 if (pszData) {
41 memcpy(pszData, pchData, cchData);
42 pszData[cchData] = (char) 0;
43 printf("client_callback: event=%s (0x%04x) [%s]\n", lscp_event_to_text(event), (int) event, pszData);
44 free(pszData);
45 ret = LSCP_OK;
46 }
47
48 return ret;
49 }
50
51 ////////////////////////////////////////////////////////////////////////
52
53 int client_test_int ( int i )
54 {
55 printf("%d\n", i);
56 return (i >= 0 ? 0 : 1);
57 }
58
59 int client_test_status ( lscp_status_t s )
60 {
61 const char *pszStatus;
62
63 switch (s) {
64 case LSCP_OK: pszStatus = "OK"; break;
65 case LSCP_FAILED: pszStatus = "FAILED"; break;
66 case LSCP_ERROR: pszStatus = "ERROR"; break;
67 case LSCP_WARNING: pszStatus = "WARNING"; break;
68 case LSCP_TIMEOUT: pszStatus = "TIMEOUT"; break;
69 case LSCP_QUIT: pszStatus = "QUIT"; break;
70 default: pszStatus = "NONE"; break;
71 }
72 printf("%s\n", pszStatus);
73 return (s == LSCP_OK ? 0 : 1);
74 }
75
76
77 int client_test_isplit ( int *piSplit )
78 {
79 int i;
80
81 printf("{");
82 for (i = 0; piSplit && piSplit[i] >= 0; i++) {
83 if (i > 0)
84 printf(",");
85 printf(" %d", piSplit[i]);
86 }
87 printf(" }\n");
88 return 0;
89 }
90
91 int client_test_szsplit ( char **ppszSplit )
92 {
93 int i;
94
95 printf("{");
96 for (i = 0; ppszSplit && ppszSplit[i]; i++) {
97 if (i > 0)
98 printf(",");
99 printf(" %s", ppszSplit[i]);
100 }
101 printf(" }\n");
102 return 0;
103 }
104
105 int client_test_params ( lscp_param_t *pParams )
106 {
107 int i;
108
109 printf("{");
110 for (i = 0; pParams && pParams[i].key; i++) {
111 if (i > 0)
112 printf(",");
113 printf(" %s='%s'", pParams[i].key, pParams[i].value);
114 }
115 printf(" }\n");
116 return 0;
117 }
118
119 int client_test_param_info ( lscp_param_info_t *pParamInfo )
120 {
121 const char *pszType;
122
123 if (pParamInfo == NULL) {
124 printf("(nil)\n");
125 return 1;
126 }
127 switch (pParamInfo->type) {
128 case LSCP_TYPE_BOOL: pszType = "BOOL"; break;
129 case LSCP_TYPE_INT: pszType = "INT"; break;
130 case LSCP_TYPE_FLOAT: pszType = "FLOAT"; break;
131 case LSCP_TYPE_STRING: pszType = "STRING"; break;
132 default: pszType = "NONE"; break;
133 }
134 printf("{\n");
135 printf(" param_info.type = %d (%s)\n", (int) pParamInfo->type, pszType);
136 printf(" param_info.description = %s\n", pParamInfo->description);
137 printf(" param_info.mandatory = %d\n", pParamInfo->mandatory);
138 printf(" param_info.fix = %d\n", pParamInfo->fix);
139 printf(" param_info.multiplicity = %d\n", pParamInfo->multiplicity);
140 printf(" param_info.depends = "); client_test_szsplit(pParamInfo->depends);
141 printf(" param_info.defaultv = %s\n", pParamInfo->defaultv);
142 printf(" param_info.range_min = %s\n", pParamInfo->range_min);
143 printf(" param_info.range_max = %s\n", pParamInfo->range_max);
144 printf(" param_info.possibilities = "); client_test_szsplit(pParamInfo->possibilities);
145 printf(" }\n");
146 return 0;
147 }
148
149 int client_test_driver_info ( lscp_driver_info_t *pDriverInfo )
150 {
151 if (pDriverInfo == NULL) {
152 printf("(nil)\n");
153 return 1;
154 }
155 printf("{\n");
156 printf(" driver_info.description = %s\n", pDriverInfo->description);
157 printf(" driver_info.version = %s\n", pDriverInfo->version);
158 printf(" driver_info.parameters = "); client_test_szsplit(pDriverInfo->parameters);
159 printf(" }\n");
160 return 0;
161 }
162
163 int client_test_device_info ( lscp_device_info_t *pDeviceInfo )
164 {
165 if (pDeviceInfo == NULL) {
166 printf("(nil)\n");
167 return 1;
168 }
169 printf("{\n");
170 printf(" device_info.driver = %s\n", pDeviceInfo->driver);
171 printf(" device_info.params = "); client_test_params(pDeviceInfo->params);
172 printf(" }\n");
173 return 0;
174 }
175
176 int client_test_device_port_info ( lscp_device_port_info_t *pDevicePortInfo )
177 {
178 if (pDevicePortInfo == NULL) {
179 printf("(nil)\n");
180 return 1;
181 }
182 printf("{\n");
183 printf(" device_port_info.name = %s\n", pDevicePortInfo->name);
184 printf(" device_port_info.params = "); client_test_params(pDevicePortInfo->params);
185 printf(" }\n");
186 return 0;
187 }
188
189 int client_test_engine_info ( lscp_engine_info_t *pEngineInfo )
190 {
191 if (pEngineInfo == NULL) {
192 printf("(nil)\n");
193 return 1;
194 }
195 printf("{\n");
196 printf(" engine_info.description = %s\n", pEngineInfo->description);
197 printf(" engine_info.version = %s\n", pEngineInfo->version);
198 printf(" }\n");
199 return 0;
200 }
201
202 int client_test_channel_info ( lscp_channel_info_t *pChannelInfo )
203 {
204 if (pChannelInfo == NULL) {
205 printf("(nil)\n");
206 return 1;
207 }
208 printf("{\n");
209 printf(" channel_info.engine_name = %s\n", pChannelInfo->engine_name);
210 printf(" channel_info.audio_device = %d\n", pChannelInfo->audio_device);
211 printf(" channel_info.audio_channels = %d\n", pChannelInfo->audio_channels);
212 printf(" channel_info.audio_routing = "); client_test_szsplit(pChannelInfo->audio_routing);
213 printf(" channel_info.instrument_file = %s\n", pChannelInfo->instrument_file);
214 printf(" channel_info.instrument_nr = %d\n", pChannelInfo->instrument_nr);
215 printf(" channel_info.instrument_status = %d\n", pChannelInfo->instrument_status);
216 printf(" channel_info.midi_device = %d\n", pChannelInfo->midi_device);
217 printf(" channel_info.midi_port = %d\n", pChannelInfo->midi_port);
218 printf(" channel_info.midi_channel = %d\n", pChannelInfo->midi_channel);
219 printf(" channel_info.volume = %g\n", pChannelInfo->volume);
220 printf(" }\n");
221 return 0;
222 }
223
224 int client_test_buffer_fill ( lscp_buffer_fill_t *pBufferFill )
225 {
226 if (pBufferFill == NULL) {
227 printf("(nil)\n");
228 return 1;
229 }
230 printf("{ <%p> }\n", pBufferFill);
231 return 0;
232 }
233
234 ////////////////////////////////////////////////////////////////////////
235
236 static int g_test_step = 0;
237 static int g_test_count = 0;
238 static int g_test_fails = 0;
239
240
241 void client_test_start ( clock_t *pclk ) { *pclk = clock(); }
242 float client_test_elapsed ( clock_t *pclk ) { return (float) ((long) clock() - *pclk) / (float) CLOCKS_PER_SEC; }
243
244 typedef int * isplit;
245 typedef char ** szsplit;
246 typedef lscp_status_t status;
247 typedef lscp_driver_info_t * driver_info;
248 typedef lscp_device_info_t * device_info;
249 typedef lscp_device_port_info_t * device_port_info;
250 typedef lscp_param_info_t * param_info;
251 typedef lscp_engine_info_t * engine_info;
252 typedef lscp_channel_info_t * channel_info;
253 typedef lscp_buffer_fill_t * buffer_fill;
254
255 #define CLIENT_TEST(p, t, x) { clock_t c; void *v; g_test_count++; \
256 printf("\n" #x ":\n"); client_test_start(&c); v = (void *) (x); \
257 printf(" elapsed=%gs errno=%d result='%s...' ret=", \
258 client_test_elapsed(&c), \
259 lscp_client_get_errno(p), \
260 lscp_client_get_result(p)); \
261 if (client_test_##t((t)(v))) { g_test_fails++; getchar(); } \
262 else if (g_test_step) getchar(); }
263
264
265 void client_test ( lscp_client_t *pClient )
266 {
267 const char **ppszAudioDrivers, **ppszMidiDrivers, **ppszEngines;
268 const char *pszAudioDriver, *pszMidiDriver, *pszEngine;
269 int iAudioDriver, iMidiDriver, iEngine;
270 int iAudioDevice, iMidiDevice;
271 int iSamplerChannel;
272
273 g_test_count = 0;
274 g_test_fails = 0;
275
276 CLIENT_TEST(pClient, szsplit, ppszAudioDrivers = lscp_get_available_audio_drivers(pClient));
277 if (ppszAudioDrivers == NULL) {
278 fprintf(stderr, "client_test: No audio drivers available.\n");
279 return;
280 }
281
282 CLIENT_TEST(pClient, szsplit, ppszMidiDrivers = lscp_get_available_midi_drivers(pClient));
283 if (ppszMidiDrivers == NULL) {
284 fprintf(stderr, "client_test: No MIDI drivers available.\n");
285 return;
286 }
287
288 CLIENT_TEST(pClient, szsplit, ppszEngines = lscp_get_available_engines(pClient));
289 if (ppszEngines == NULL) {
290 fprintf(stderr, "client_test: No engines available.\n");
291 return;
292 }
293
294 CLIENT_TEST(pClient, int, lscp_get_audio_devices(pClient));
295 CLIENT_TEST(pClient, isplit, lscp_list_audio_devices(pClient));
296
297 CLIENT_TEST(pClient, int, lscp_get_midi_devices(pClient));
298 CLIENT_TEST(pClient, isplit, lscp_list_midi_devices(pClient));
299
300 for (iAudioDriver = 0; ppszAudioDrivers[iAudioDriver]; iAudioDriver++) {
301 pszAudioDriver = ppszAudioDrivers[iAudioDriver];
302 printf("\n--- pszAudioDriver=\"%s\" ---\n", pszAudioDriver);
303 CLIENT_TEST(pClient, driver_info, lscp_get_audio_driver_info(pClient, pszAudioDriver));
304 CLIENT_TEST(pClient, param_info, lscp_get_audio_driver_param_info(pClient, pszAudioDriver, "active", NULL));
305 CLIENT_TEST(pClient, int, iAudioDevice = lscp_create_audio_device(pClient, pszAudioDriver, NULL));
306 CLIENT_TEST(pClient, device_info, lscp_get_audio_device_info(pClient, iAudioDevice));
307 for (iMidiDriver = 0; ppszMidiDrivers[iMidiDriver]; iMidiDriver++) {
308 pszMidiDriver = ppszMidiDrivers[iMidiDriver];
309 printf("\n--- pszMidiDriver=\"%s\" ---\n", pszMidiDriver);
310 CLIENT_TEST(pClient, driver_info, lscp_get_midi_driver_info(pClient, pszMidiDriver));
311 CLIENT_TEST(pClient, param_info, lscp_get_midi_driver_param_info(pClient, pszMidiDriver, "active", NULL));
312 CLIENT_TEST(pClient, int, iMidiDevice = lscp_create_midi_device(pClient, pszMidiDriver, NULL));
313 CLIENT_TEST(pClient, device_info, lscp_get_midi_device_info(pClient, iMidiDevice));
314 for (iEngine = 0; ppszEngines[iEngine]; iEngine++) {
315 pszEngine = ppszEngines[iEngine];
316 printf("\n--- pszEngine=\"%s\" ---\n", pszEngine);
317 CLIENT_TEST(pClient, engine_info, lscp_get_engine_info(pClient, pszEngine));
318 CLIENT_TEST(pClient, int, lscp_get_channels(pClient));
319 CLIENT_TEST(pClient, isplit, lscp_list_channels(pClient));
320 CLIENT_TEST(pClient, int, iSamplerChannel = lscp_add_channel(pClient));
321 CLIENT_TEST(pClient, channel_info, lscp_get_channel_info(pClient, iSamplerChannel));
322 CLIENT_TEST(pClient, status, lscp_load_engine(pClient, pszEngine, iSamplerChannel));
323 CLIENT_TEST(pClient, status, lscp_load_instrument(pClient, "DefaultInstrument.gig", 0, iSamplerChannel));
324 CLIENT_TEST(pClient, int, lscp_get_channel_voice_count(pClient, iSamplerChannel));
325 CLIENT_TEST(pClient, int, lscp_get_channel_stream_count(pClient, iSamplerChannel));
326 CLIENT_TEST(pClient, int, lscp_get_channel_stream_usage(pClient, iSamplerChannel));
327 CLIENT_TEST(pClient, buffer_fill, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_BYTES, iSamplerChannel));
328 CLIENT_TEST(pClient, buffer_fill, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_PERCENTAGE, iSamplerChannel));
329 CLIENT_TEST(pClient, status, lscp_set_channel_audio_type(pClient, iSamplerChannel, pszAudioDriver));
330 CLIENT_TEST(pClient, status, lscp_set_channel_audio_device(pClient, iSamplerChannel, 0));
331 CLIENT_TEST(pClient, status, lscp_set_channel_audio_channel(pClient, iSamplerChannel, 0, 1));
332 CLIENT_TEST(pClient, status, lscp_set_channel_midi_type(pClient, iSamplerChannel, pszMidiDriver));
333 CLIENT_TEST(pClient, status, lscp_set_channel_midi_device(pClient, iSamplerChannel, 0));
334 CLIENT_TEST(pClient, status, lscp_set_channel_midi_channel(pClient, iSamplerChannel, 0));
335 CLIENT_TEST(pClient, status, lscp_set_channel_midi_port(pClient, iSamplerChannel, 0));
336 CLIENT_TEST(pClient, status, lscp_set_channel_volume(pClient, iSamplerChannel, 0.5));
337 CLIENT_TEST(pClient, channel_info, lscp_get_channel_info(pClient, iSamplerChannel));
338 CLIENT_TEST(pClient, status, lscp_reset_channel(pClient, iSamplerChannel));
339 CLIENT_TEST(pClient, status, lscp_remove_channel(pClient, iSamplerChannel));
340 }
341 CLIENT_TEST(pClient, status, lscp_destroy_midi_device(pClient, iMidiDevice));
342 }
343 CLIENT_TEST(pClient, status, lscp_destroy_audio_device(pClient, iAudioDevice));
344 }
345 printf("\n");
346 printf(" Total: %d tests, %d failed.\n\n", g_test_count, g_test_fails);
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350
351 void client_usage (void)
352 {
353 printf("\n %s %s (Build: %s)\n", lscp_client_package(), lscp_client_version(), lscp_client_build());
354
355 fputs("\n Available commands: help, test[step], exit, quit, subscribe, unsubscribe", stdout);
356 fputs("\n (all else are sent verbatim to server)\n\n", stdout);
357
358 }
359
360 void client_prompt (void)
361 {
362 fputs("lscp_client> ", stdout);
363 }
364
365 int main (int argc, char *argv[] )
366 {
367 lscp_client_t *pClient;
368 char *pszHost = "localhost";
369 char szLine[1024];
370 int cchLine;
371 lscp_status_t ret;
372
373 #if defined(WIN32)
374 if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) {
375 fprintf(stderr, "lscp_client: WSAStartup failed.\n");
376 return -1;
377 }
378 #endif
379
380 if (argc > 1)
381 pszHost = argv[1];
382
383 pClient = lscp_client_create(pszHost, SERVER_PORT, client_callback, NULL);
384 if (pClient == NULL)
385 return -1;
386
387 client_usage();
388 client_prompt();
389
390 while (fgets(szLine, sizeof(szLine) - 3, stdin)) {
391
392 cchLine = strlen(szLine);
393 while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r'))
394 cchLine--;
395 szLine[cchLine] = '\0';
396
397 if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0)
398 break;
399 else
400 if (strcmp(szLine, "subscribe") == 0)
401 lscp_client_subscribe(pClient, LSCP_EVENT_MISCELLANEOUS);
402 else
403 if (strcmp(szLine, "unsubscribe") == 0)
404 lscp_client_unsubscribe(pClient, LSCP_EVENT_MISCELLANEOUS);
405 else
406 if (strcmp(szLine, "test") == 0)
407 client_test(pClient);
408 else
409 if (strcmp(szLine, "teststep") == 0 || strcmp(szLine, "test step") == 0) {
410 g_test_step = 1;
411 client_test(pClient);
412 g_test_step = 0;
413 }
414 else
415 if (cchLine > 0 && strcmp(szLine, "help") != 0) {
416 szLine[cchLine++] = '\r';
417 szLine[cchLine++] = '\n';
418 szLine[cchLine] = '\0';
419 ret = lscp_client_query(pClient, szLine);
420 printf("%s\n(errno = %d)\n", lscp_client_get_result(pClient), lscp_client_get_errno(pClient));
421 if (ret == LSCP_QUIT)
422 break;
423 }
424 else client_usage();
425
426 client_prompt();
427 }
428
429 lscp_client_destroy(pClient);
430
431 #if defined(WIN32)
432 WSACleanup();
433 #endif
434
435 return 0;
436 }
437
438 // end of example_client.c

  ViewVC Help
Powered by ViewVC