/[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 103 - (show annotations) (download)
Fri Jun 4 14:32:51 2004 UTC (19 years, 9 months ago) by capela
File MIME type: text/plain
File size: 7779 byte(s)
Initial alpha release.

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, const char *pchBuffer, int cchBuffer, void *pvData )
36 {
37 lscp_status_t ret = LSCP_OK;
38
39 char *pszBuffer = (char *) malloc(cchBuffer + 1);
40 if (pszBuffer) {
41 memcpy(pszBuffer, pchBuffer, cchBuffer);
42 pszBuffer[cchBuffer] = (char) 0;
43 printf("client_callback: [%s]\n", pszBuffer);
44 free(pszBuffer);
45 }
46 else ret = LSCP_FAILED;
47
48 return ret;
49 }
50
51 ////////////////////////////////////////////////////////////////////////
52
53 void client_test_start ( clock_t *pclk ) { *pclk = clock(); }
54 float client_test_elapsed ( clock_t *pclk ) { return (float) ((long) clock() - *pclk) / (float) CLOCKS_PER_SEC; }
55
56 #define CLIENT_TEST(p, x) { clock_t c; void *v;\
57 printf(#x ":\n"); client_test_start(&c); v = (void *) (x); \
58 printf(" elapsed=%gs\n", client_test_elapsed(&c)); \
59 printf(" ret=%p (%d)\n", v, (int) v); \
60 printf(" errno=%d\n", lscp_client_get_errno(p)); \
61 printf(" result=\"%s\"\n", lscp_client_get_result(p)); }
62
63 void client_test ( lscp_client_t *pClient )
64 {
65 const char **ppszAudioDrivers, **ppszMidiDrivers, **ppszEngines;
66 const char *pszAudioDriver, *pszMidiDriver, *pszEngine;
67 int iAudioDriver, iMidiDriver, iEngine;
68 int iSamplerChannel;
69
70 CLIENT_TEST(pClient, ppszAudioDrivers = lscp_get_available_audio_drivers(pClient));
71 if (ppszAudioDrivers == NULL) {
72 fprintf(stderr, "client_test: No audio drivers available.\n");
73 return;
74 }
75
76 CLIENT_TEST(pClient, ppszMidiDrivers = lscp_get_available_midi_drivers(pClient));
77 if (ppszMidiDrivers == NULL) {
78 fprintf(stderr, "client_test: No MIDI drivers available.\n");
79 return;
80 }
81
82 CLIENT_TEST(pClient, ppszEngines = lscp_get_available_engines(pClient));
83 if (ppszEngines == NULL) {
84 fprintf(stderr, "client_test: No engines available.\n");
85 return;
86 }
87
88 for (iAudioDriver = 0; ppszAudioDrivers[iAudioDriver]; iAudioDriver++) {
89 pszAudioDriver = ppszAudioDrivers[iAudioDriver];
90 printf("\n--- pszAudioDriver=\"%s\" ---\n", pszAudioDriver);
91 CLIENT_TEST(pClient, lscp_get_audio_driver_info(pClient, pszAudioDriver));
92 for (iMidiDriver = 0; ppszMidiDrivers[iMidiDriver]; iMidiDriver++) {
93 pszMidiDriver = ppszMidiDrivers[iMidiDriver];
94 printf("\n--- pszMidiDriver=\"%s\" ---\n", pszMidiDriver);
95 CLIENT_TEST(pClient, lscp_get_midi_driver_info(pClient, pszMidiDriver));
96 for (iEngine = 0; ppszEngines[iEngine]; iEngine++) {
97 pszEngine = ppszEngines[iEngine];
98 printf("\n--- pszEngine=\"%s\" ---\n", pszEngine);
99 CLIENT_TEST(pClient, lscp_get_engine_info(pClient, pszEngine));
100 CLIENT_TEST(pClient, lscp_get_channels(pClient));
101 CLIENT_TEST(pClient, iSamplerChannel = lscp_add_channel(pClient));
102 printf(">>> iSamplerChannel=\"%d\"\n", iSamplerChannel);
103 CLIENT_TEST(pClient, lscp_get_channel_info(pClient, iSamplerChannel));
104 CLIENT_TEST(pClient, lscp_load_engine(pClient, pszEngine, iSamplerChannel));
105 CLIENT_TEST(pClient, lscp_load_instrument(pClient, "DefaultInstrument.gig", 0, iSamplerChannel));
106 CLIENT_TEST(pClient, lscp_get_channel_voice_count(pClient, iSamplerChannel));
107 CLIENT_TEST(pClient, lscp_get_channel_stream_count(pClient, iSamplerChannel));
108 CLIENT_TEST(pClient, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_BYTES, iSamplerChannel));
109 CLIENT_TEST(pClient, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_PERCENTAGE, iSamplerChannel));
110 CLIENT_TEST(pClient, lscp_set_channel_audio_type(pClient, iSamplerChannel, pszAudioDriver));
111 CLIENT_TEST(pClient, lscp_set_channel_audio_channel(pClient, iSamplerChannel, 0, 1));
112 CLIENT_TEST(pClient, lscp_set_channel_midi_type(pClient, iSamplerChannel, pszMidiDriver));
113 CLIENT_TEST(pClient, lscp_set_channel_midi_channel(pClient, iSamplerChannel, 0));
114 CLIENT_TEST(pClient, lscp_set_channel_midi_port(pClient, iSamplerChannel, 0));
115 CLIENT_TEST(pClient, lscp_set_channel_volume(pClient, iSamplerChannel, 0.5));
116 CLIENT_TEST(pClient, lscp_get_channel_info(pClient, iSamplerChannel));
117 CLIENT_TEST(pClient, lscp_reset_channel(pClient, iSamplerChannel));
118 CLIENT_TEST(pClient, lscp_remove_channel(pClient, iSamplerChannel));
119 printf("\n");
120 }
121 }
122 }
123
124 }
125
126 ////////////////////////////////////////////////////////////////////////
127
128 void client_usage (void)
129 {
130 printf("\n %s %s (Build: %s)\n", lscp_client_package(), lscp_client_version(), lscp_client_build());
131
132 fputs("\n Available client commands: help, test, exit, quit, subscribe, unsubscribe", stdout);
133 fputs("\n (all else are sent verbatim to server)\n\n", stdout);
134
135 }
136
137 void client_prompt (void)
138 {
139 fputs("lscp_client> ", stdout);
140 }
141
142 int main (int argc, char *argv[] )
143 {
144 lscp_client_t *pClient;
145 char *pszHost = "localhost";
146 char szLine[1024];
147 int cchLine;
148 lscp_status_t ret;
149
150 #if defined(WIN32)
151 if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) {
152 fprintf(stderr, "lscp_client: WSAStartup failed.\n");
153 return -1;
154 }
155 #endif
156
157 if (argc > 1)
158 pszHost = argv[1];
159
160 pClient = lscp_client_create(pszHost, SERVER_PORT, client_callback, NULL);
161 if (pClient == NULL)
162 return -1;
163
164 client_usage();
165 client_prompt();
166
167 while (fgets(szLine, sizeof(szLine) - 3, stdin)) {
168
169 cchLine = strlen(szLine);
170 while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r'))
171 cchLine--;
172 szLine[cchLine] = '\0';
173
174 if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0)
175 break;
176 else
177 if (strcmp(szLine, "subscribe") == 0)
178 lscp_client_subscribe(pClient);
179 else
180 if (strcmp(szLine, "unsubscribe") == 0)
181 lscp_client_unsubscribe(pClient);
182 else
183 if (strcmp(szLine, "test") == 0)
184 client_test(pClient);
185 else
186 if (cchLine > 0 && strcmp(szLine, "help") != 0) {
187 szLine[cchLine++] = '\r';
188 szLine[cchLine++] = '\n';
189 szLine[cchLine] = '\0';
190 ret = lscp_client_query(pClient, szLine);
191 printf("%s\n(errno = %d)\n", lscp_client_get_result(pClient), lscp_client_get_errno(pClient));
192 if (ret == LSCP_QUIT)
193 break;
194 }
195 else client_usage();
196
197 client_prompt();
198 }
199
200 lscp_client_destroy(pClient);
201
202 #if defined(WIN32)
203 WSACleanup();
204 #endif
205
206 return 0;
207 }
208
209 // end of example_client.c

  ViewVC Help
Powered by ViewVC