/[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 178 - (show annotations) (download)
Tue Jul 6 15:52:25 2004 UTC (13 years, 11 months ago) by capela
File MIME type: text/plain
File size: 9167 byte(s)
Slight change to thread join termination code.

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 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 iAudioDevice, iMidiDevice;
69 int iSamplerChannel;
70
71 CLIENT_TEST(pClient, ppszAudioDrivers = lscp_get_available_audio_drivers(pClient));
72 if (ppszAudioDrivers == NULL) {
73 fprintf(stderr, "client_test: No audio drivers available.\n");
74 return;
75 }
76
77 CLIENT_TEST(pClient, ppszMidiDrivers = lscp_get_available_midi_drivers(pClient));
78 if (ppszMidiDrivers == NULL) {
79 fprintf(stderr, "client_test: No MIDI drivers available.\n");
80 return;
81 }
82
83 CLIENT_TEST(pClient, ppszEngines = lscp_get_available_engines(pClient));
84 if (ppszEngines == NULL) {
85 fprintf(stderr, "client_test: No engines available.\n");
86 return;
87 }
88
89 CLIENT_TEST(pClient, lscp_get_audio_devices(pClient));
90 CLIENT_TEST(pClient, lscp_list_audio_devices(pClient));
91
92 CLIENT_TEST(pClient, lscp_get_midi_devices(pClient));
93 CLIENT_TEST(pClient, lscp_list_midi_devices(pClient));
94
95 for (iAudioDriver = 0; ppszAudioDrivers[iAudioDriver]; iAudioDriver++) {
96 pszAudioDriver = ppszAudioDrivers[iAudioDriver];
97 printf("\n--- pszAudioDriver=\"%s\" ---\n", pszAudioDriver);
98 CLIENT_TEST(pClient, lscp_get_audio_driver_info(pClient, pszAudioDriver));
99 CLIENT_TEST(pClient, lscp_get_audio_driver_param_info(pClient, pszAudioDriver, "active", NULL));
100 CLIENT_TEST(pClient, iAudioDevice = lscp_create_audio_device(pClient, pszAudioDriver, NULL));
101 CLIENT_TEST(pClient, lscp_get_audio_device_info(pClient, iAudioDevice));
102 for (iMidiDriver = 0; ppszMidiDrivers[iMidiDriver]; iMidiDriver++) {
103 pszMidiDriver = ppszMidiDrivers[iMidiDriver];
104 printf("\n--- pszMidiDriver=\"%s\" ---\n", pszMidiDriver);
105 CLIENT_TEST(pClient, lscp_get_midi_driver_info(pClient, pszMidiDriver));
106 CLIENT_TEST(pClient, lscp_get_midi_driver_param_info(pClient, pszMidiDriver, "active", NULL));
107 CLIENT_TEST(pClient, iMidiDevice = lscp_create_midi_device(pClient, pszMidiDriver, NULL));
108 CLIENT_TEST(pClient, lscp_get_midi_device_info(pClient, iMidiDevice));
109 for (iEngine = 0; ppszEngines[iEngine]; iEngine++) {
110 pszEngine = ppszEngines[iEngine];
111 printf("\n--- pszEngine=\"%s\" ---\n", pszEngine);
112 CLIENT_TEST(pClient, lscp_get_engine_info(pClient, pszEngine));
113 CLIENT_TEST(pClient, lscp_get_channels(pClient));
114 CLIENT_TEST(pClient, lscp_list_channels(pClient));
115 CLIENT_TEST(pClient, iSamplerChannel = lscp_add_channel(pClient));
116 printf(">>> iSamplerChannel=\"%d\"\n", iSamplerChannel);
117 CLIENT_TEST(pClient, lscp_get_channel_info(pClient, iSamplerChannel));
118 CLIENT_TEST(pClient, lscp_load_engine(pClient, pszEngine, iSamplerChannel));
119 CLIENT_TEST(pClient, lscp_load_instrument(pClient, "DefaultInstrument.gig", 0, iSamplerChannel));
120 CLIENT_TEST(pClient, lscp_get_channel_voice_count(pClient, iSamplerChannel));
121 CLIENT_TEST(pClient, lscp_get_channel_stream_count(pClient, iSamplerChannel));
122 CLIENT_TEST(pClient, lscp_get_channel_stream_usage(pClient, iSamplerChannel));
123 CLIENT_TEST(pClient, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_BYTES, iSamplerChannel));
124 CLIENT_TEST(pClient, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_PERCENTAGE, iSamplerChannel));
125 CLIENT_TEST(pClient, lscp_set_channel_audio_type(pClient, iSamplerChannel, pszAudioDriver));
126 CLIENT_TEST(pClient, lscp_set_channel_audio_device(pClient, iSamplerChannel, 0));
127 CLIENT_TEST(pClient, lscp_set_channel_audio_channel(pClient, iSamplerChannel, 0, 1));
128 CLIENT_TEST(pClient, lscp_set_channel_midi_type(pClient, iSamplerChannel, pszMidiDriver));
129 CLIENT_TEST(pClient, lscp_set_channel_midi_device(pClient, iSamplerChannel, 0));
130 CLIENT_TEST(pClient, lscp_set_channel_midi_channel(pClient, iSamplerChannel, 0));
131 CLIENT_TEST(pClient, lscp_set_channel_midi_port(pClient, iSamplerChannel, 0));
132 CLIENT_TEST(pClient, lscp_set_channel_volume(pClient, iSamplerChannel, 0.5));
133 CLIENT_TEST(pClient, lscp_get_channel_info(pClient, iSamplerChannel));
134 CLIENT_TEST(pClient, lscp_reset_channel(pClient, iSamplerChannel));
135 CLIENT_TEST(pClient, lscp_remove_channel(pClient, iSamplerChannel));
136 }
137 CLIENT_TEST(pClient, lscp_destroy_midi_device(pClient, iMidiDevice));
138 }
139 CLIENT_TEST(pClient, lscp_destroy_audio_device(pClient, iAudioDevice));
140 }
141
142 }
143
144 ////////////////////////////////////////////////////////////////////////
145
146 void client_usage (void)
147 {
148 printf("\n %s %s (Build: %s)\n", lscp_client_package(), lscp_client_version(), lscp_client_build());
149
150 fputs("\n Available client commands: help, test, exit, quit, subscribe, unsubscribe", stdout);
151 fputs("\n (all else are sent verbatim to server)\n\n", stdout);
152
153 }
154
155 void client_prompt (void)
156 {
157 fputs("lscp_client> ", stdout);
158 }
159
160 int main (int argc, char *argv[] )
161 {
162 lscp_client_t *pClient;
163 char *pszHost = "localhost";
164 char szLine[1024];
165 int cchLine;
166 lscp_status_t ret;
167
168 #if defined(WIN32)
169 if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) {
170 fprintf(stderr, "lscp_client: WSAStartup failed.\n");
171 return -1;
172 }
173 #endif
174
175 if (argc > 1)
176 pszHost = argv[1];
177
178 pClient = lscp_client_create(pszHost, SERVER_PORT, client_callback, NULL);
179 if (pClient == NULL)
180 return -1;
181
182 client_usage();
183 client_prompt();
184
185 while (fgets(szLine, sizeof(szLine) - 3, stdin)) {
186
187 cchLine = strlen(szLine);
188 while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r'))
189 cchLine--;
190 szLine[cchLine] = '\0';
191
192 if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0)
193 break;
194 else
195 if (strcmp(szLine, "subscribe") == 0)
196 lscp_client_subscribe(pClient, LSCP_EVENT_MISCELLANEOUS);
197 else
198 if (strcmp(szLine, "unsubscribe") == 0)
199 lscp_client_unsubscribe(pClient, LSCP_EVENT_MISCELLANEOUS);
200 else
201 if (strcmp(szLine, "test") == 0)
202 client_test(pClient);
203 else
204 if (cchLine > 0 && strcmp(szLine, "help") != 0) {
205 szLine[cchLine++] = '\r';
206 szLine[cchLine++] = '\n';
207 szLine[cchLine] = '\0';
208 ret = lscp_client_query(pClient, szLine);
209 printf("%s\n(errno = %d)\n", lscp_client_get_result(pClient), lscp_client_get_errno(pClient));
210 if (ret == LSCP_QUIT)
211 break;
212 }
213 else client_usage();
214
215 client_prompt();
216 }
217
218 lscp_client_destroy(pClient);
219
220 #if defined(WIN32)
221 WSACleanup();
222 #endif
223
224 return 0;
225 }
226
227 // end of example_client.c

  ViewVC Help
Powered by ViewVC