1 |
// example_server.c |
2 |
// |
3 |
/**************************************************************************** |
4 |
liblscp - LinuxSampler Control Protocol API |
5 |
Copyright (C) 2004-2006, rncbc aka Rui Nuno Capela. All rights reserved. |
6 |
|
7 |
This program is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU General Public License |
9 |
as published by the Free Software Foundation; either version 2 |
10 |
of the License, or (at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License along |
18 |
with this program; if not, write to the Free Software Foundation, Inc., |
19 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
21 |
*****************************************************************************/ |
22 |
|
23 |
#include "server.h" |
24 |
#include "parser.h" |
25 |
|
26 |
#include <time.h> |
27 |
|
28 |
#define SERVER_PORT 8888 |
29 |
|
30 |
#if defined(WIN32) |
31 |
static WSADATA _wsaData; |
32 |
#endif |
33 |
|
34 |
//////////////////////////////////////////////////////////////////////// |
35 |
|
36 |
lscp_status_t server_callback ( lscp_connect_t *pConnect, const char *pchBuffer, int cchBuffer, void *pvData ) |
37 |
{ |
38 |
lscp_status_t ret = LSCP_OK; |
39 |
lscp_parser_t tok; |
40 |
const char *pszResult = NULL; |
41 |
char szTemp[4096]; |
42 |
int i; |
43 |
static int iSamplerChannel = 0; |
44 |
static int iAudioDevice = 0; |
45 |
static int iMidiDevice = 0; |
46 |
static int iMidiInstruments = 0; |
47 |
|
48 |
if (pchBuffer == NULL) { |
49 |
fprintf(stderr, "server_callback: addr=%s port=%d: ", |
50 |
inet_ntoa(pConnect->client.addr.sin_addr), |
51 |
htons(pConnect->client.addr.sin_port)); |
52 |
switch (cchBuffer) { |
53 |
case LSCP_CONNECT_OPEN: |
54 |
fprintf(stderr, "New client connection.\n"); |
55 |
break; |
56 |
case LSCP_CONNECT_CLOSE: |
57 |
fprintf(stderr, "Connection closed.\n"); |
58 |
break; |
59 |
} |
60 |
return ret; |
61 |
} |
62 |
|
63 |
lscp_socket_trace("server_callback", &(pConnect->client.addr), pchBuffer, cchBuffer); |
64 |
|
65 |
lscp_parser_init(&tok, pchBuffer, cchBuffer); |
66 |
|
67 |
if (lscp_parser_test(&tok, "GET")) { |
68 |
if (lscp_parser_test(&tok, "CHANNEL")) { |
69 |
if (lscp_parser_test(&tok, "INFO")) { |
70 |
// Getting sampler channel informations: |
71 |
// GET CHANNEL INFO <sampler-channel> |
72 |
pszResult = "ENGINE_NAME: DummyEngine\r\n" |
73 |
"INSTRUMENT_FILE: DummyInstrument.gig\r\n" |
74 |
"INSTRUMENT_NR: 0\r\n" |
75 |
"INSTRUMENT_NAME: Dummy Instrument\r\n" |
76 |
"INSTRUMENT_STATUS: 100\r\n" |
77 |
"AUDIO_OUTPUT_DEVICE: 0\r\n" |
78 |
"AUDIO_OUTPUT_CHANNELS: 2\r\n" |
79 |
"AUDIO_OUTPUT_ROUTING: 0,1\r\n" |
80 |
"MIDI_INPUT_DEVICE: 0\r\n" |
81 |
"MIDI_INPUT_PORT: 0\r\n" |
82 |
"MIDI_INPUT_CHANNEL: ALL\r\n" |
83 |
"VOLUME: 0.5\r\n" |
84 |
"MUTE: FALSE\r\n" |
85 |
"SOLO: TRUE\r\n" |
86 |
".\r\n"; |
87 |
} |
88 |
else if (lscp_parser_test(&tok, "VOICE_COUNT")) { |
89 |
// Current number of active voices: |
90 |
// GET CHANNEL VOICE_COUNT <sampler-channel> |
91 |
sprintf(szTemp, "%d\r\n", rand() % 100); |
92 |
pszResult = szTemp; |
93 |
} |
94 |
else if (lscp_parser_test(&tok, "STREAM_COUNT")) { |
95 |
// Current number of active disk streams: |
96 |
// GET CHANNEL STREAM_COUNT <sampler-channel> |
97 |
pszResult = "3\r\n"; |
98 |
} |
99 |
else if (lscp_parser_test(&tok, "BUFFER_FILL")) { |
100 |
if (lscp_parser_test(&tok, "BYTES")) { |
101 |
// Current fill state of disk stream buffers: |
102 |
// GET CHANNEL BUFFER_FILL BYTES <sampler-channel> |
103 |
sprintf(szTemp, "[1]%d,[2]%d,[3]%d\r\n", rand(), rand(), rand()); |
104 |
pszResult = szTemp; |
105 |
} |
106 |
else if (lscp_parser_test(&tok, "PERCENTAGE")) { |
107 |
// Current fill state of disk stream buffers: |
108 |
// GET CHANNEL BUFFER_FILL PERCENTAGE <sampler-channel> |
109 |
sprintf(szTemp, "[1]%d%%,[2]%d%%,[3]%d%%\r\n", rand() % 100, rand() % 100, rand() % 100); |
110 |
pszResult = szTemp; |
111 |
} |
112 |
else ret = LSCP_FAILED; |
113 |
} |
114 |
else ret = LSCP_FAILED; |
115 |
} |
116 |
else if (lscp_parser_test(&tok, "CHANNELS")) { |
117 |
// Current number of sampler channels: |
118 |
// GET CHANNELS |
119 |
sprintf(szTemp, "%d\r\n", iSamplerChannel); |
120 |
pszResult = szTemp; |
121 |
} |
122 |
else if (lscp_parser_test(&tok, "AVAILABLE_AUDIO_OUTPUT_DRIVERS")) { |
123 |
// Getting all available audio output driver count. |
124 |
// GET AVAILABLE_AUDIO_OUTPUT_DRIVERS |
125 |
pszResult = "2\r\n"; |
126 |
} |
127 |
else if (lscp_parser_test(&tok, "AVAILABLE_MIDI_INPUT_DRIVERS")) { |
128 |
// Getting all available MIDI input driver count. |
129 |
// GET AVAILABLE_MIDI_INPUT_DRIVERS |
130 |
pszResult = "1\r\n"; |
131 |
} |
132 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER", "INFO")) { |
133 |
// Getting informations about a specific audio output driver. |
134 |
// GET AUDIO_OUTPUT_DRIVER INFO <audio-output-type> |
135 |
if (lscp_parser_test(&tok, "Alsa")) { |
136 |
pszResult = "DESCRIPTION: 'ALSA PCM'\r\n" |
137 |
"VERSION: '1.0'\r\n" |
138 |
"PARAMETERS: channels,samplerate,active\r\n" |
139 |
".\r\n"; |
140 |
} |
141 |
else if (lscp_parser_test(&tok, "Jack")) { |
142 |
pszResult = "DESCRIPTION: JACK Audio Connection Kit\r\n" |
143 |
"VERSION: 0.98.1\r\n" |
144 |
"PARAMETERS: channels,samplerate,active\r\n" |
145 |
".\r\n"; |
146 |
} |
147 |
else ret = LSCP_FAILED; |
148 |
} |
149 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER", "INFO")) { |
150 |
// Getting informations about a specific MIDI input driver. |
151 |
// GET MIDI_INPUT_DRIVER INFO <midi-input-type> |
152 |
if (lscp_parser_test(&tok, "Alsa")) { |
153 |
pszResult = "DESCRIPTION: ALSA Sequencer\r\n" |
154 |
"VERSION: 1.0\r\n" |
155 |
"PARAMETERS: ports,active\r\n" |
156 |
".\r\n"; |
157 |
} |
158 |
else ret = LSCP_FAILED; |
159 |
} |
160 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER_PARAMETER", "INFO")) { |
161 |
// Getting informations about a specific audio output driver parameter. |
162 |
// GET AUDIO_OUTPUT_DRIVER_PARAMETER INFO <audio-output-type> <param> |
163 |
if (lscp_parser_test(&tok, "Alsa")) { |
164 |
if (lscp_parser_test(&tok, "active")) { |
165 |
pszResult = "DESCRIPTION: 'ALSA PCM active state'\r\n" |
166 |
"TYPE: BOOL\r\n" |
167 |
"MANDATORY: TRUE\r\n" |
168 |
"FIX: TRUE\r\n" |
169 |
"MULTIPLICITY: FALSE\r\n" |
170 |
"DEPENDS: channels,samplerate,card\r\n" |
171 |
"DEFAULT: TRUE\r\n" |
172 |
"RANGE_MIN: FALSE\r\n" |
173 |
"RANGE_MAX: TRUE\r\n" |
174 |
"POSSIBILITIES: FALSE,TRUE\r\n" |
175 |
".\r\n"; |
176 |
} |
177 |
else if (lscp_parser_test(&tok, "channels")) { |
178 |
pszResult = "DESCRIPTION: 'Number of ALSA PCM channels'\r\n" |
179 |
"TYPE: INT\r\n" |
180 |
"MANDATORY: TRUE\r\n" |
181 |
"FIX: TRUE\r\n" |
182 |
"MULTIPLICITY: FALSE\r\n" |
183 |
"DEFAULT: 2\r\n" |
184 |
".\r\n"; |
185 |
} |
186 |
else if (lscp_parser_test(&tok, "samplerate")) { |
187 |
pszResult = "DESCRIPTION: 'ALSA PCM sample rate'\r\n" |
188 |
"TYPE: INT\r\n" |
189 |
"MANDATORY: TRUE\r\n" |
190 |
"FIX: TRUE\r\n" |
191 |
"MULTIPLICITY: TRUE\r\n" |
192 |
"DEFAULT: 44100\r\n" |
193 |
"POSSIBILITIES: 44100,48000,96000\r\n" |
194 |
".\r\n"; |
195 |
} |
196 |
else ret = LSCP_FAILED; |
197 |
} |
198 |
else if (lscp_parser_test(&tok, "Jack")) { |
199 |
if (lscp_parser_test(&tok, "active")) { |
200 |
pszResult = "DESCRIPTION: 'JACK active state'\r\n" |
201 |
"TYPE: BOOL\r\n" |
202 |
"MANDATORY: TRUE\r\n" |
203 |
"FIX: TRUE\r\n" |
204 |
"MULTIPLICITY: FALSE\r\n" |
205 |
"DEPENDS: channels,samplerate\r\n" |
206 |
"DEFAULT: TRUE\r\n" |
207 |
"RANGE_MIN: FALSE\r\n" |
208 |
"RANGE_MAX: TRUE\r\n" |
209 |
"POSSIBILITIES: FALSE,TRUE\r\n" |
210 |
".\r\n"; |
211 |
} |
212 |
else if (lscp_parser_test(&tok, "channels")) { |
213 |
pszResult = "DESCRIPTION: 'Number of JACK audio channels'\r\n" |
214 |
"TYPE: INT\r\n" |
215 |
"MANDATORY: TRUE\r\n" |
216 |
"FIX: TRUE\r\n" |
217 |
"MULTIPLICITY: FALSE\r\n" |
218 |
"DEFAULT: 2\r\n" |
219 |
".\r\n"; |
220 |
} |
221 |
else if (lscp_parser_test(&tok, "samplerate")) { |
222 |
pszResult = "DESCRIPTION: 'JACK sample rate'\r\n" |
223 |
"TYPE: INT\r\n" |
224 |
"MANDATORY: TRUE\r\n" |
225 |
"FIX: TRUE\r\n" |
226 |
"MULTIPLICITY: TRUE\r\n" |
227 |
"DEFAULT: 44100\r\n" |
228 |
"POSSIBILITIES: 44100,48000,96000\r\n" |
229 |
".\r\n"; |
230 |
} |
231 |
else ret = LSCP_FAILED; |
232 |
} |
233 |
else ret = LSCP_FAILED; |
234 |
} |
235 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER_PARAMETER", "INFO")) { |
236 |
// Getting informations about a specific MIDI input driver parameter. |
237 |
// GET MIDI_INPUT_DRIVER_PARAMETER INFO <midi-input-type> <param> |
238 |
if (lscp_parser_test(&tok, "Alsa")) { |
239 |
if (lscp_parser_test(&tok, "active")) { |
240 |
pszResult = "DESCRIPTION: 'ALSA Sequencer device active state'\r\n" |
241 |
"TYPE: BOOL\r\n" |
242 |
"MANDATORY: TRUE\r\n" |
243 |
"FIX: TRUE\r\n" |
244 |
"MULTIPLICITY: FALSE\r\n" |
245 |
"DEPENDS: channels,ports\r\n" |
246 |
"DEFAULT: TRUE\r\n" |
247 |
"RANGE_MIN: FALSE\r\n" |
248 |
"RANGE_MAX: TRUE\r\n" |
249 |
"POSSIBILITIES: FALSE,TRUE\r\n" |
250 |
".\r\n"; |
251 |
} |
252 |
else if (lscp_parser_test(&tok, "ports")) { |
253 |
pszResult = "DESCRIPTION: 'Number of ALSA Sequencer ports'\r\n" |
254 |
"TYPE: INT\r\n" |
255 |
"MANDATORY: FALSE\r\n" |
256 |
"FIX: FALSE\r\n" |
257 |
"MULTIPLICITY: FALSE\r\n" |
258 |
"DEFAULT: 1\r\n" |
259 |
"RANGE_MIN: 1\r\n" |
260 |
"RANGE_MAX: 4\r\n" |
261 |
".\r\n"; |
262 |
} |
263 |
else ret = LSCP_FAILED; |
264 |
} |
265 |
else ret = LSCP_FAILED; |
266 |
} |
267 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DEVICE", "INFO")) { |
268 |
// Getting informations about a specific audio output device. |
269 |
// GET AUDIO_OUTPUT_DEVICE INFO <audio-device-id> |
270 |
if (lscp_parser_nextint(&tok) < iAudioDevice) { |
271 |
pszResult = "driver: Alsa\r\n" |
272 |
"active: TRUE\r\n" |
273 |
"channels: 2\r\n" |
274 |
"samplerate: 44100\r\n" |
275 |
".\r\n"; |
276 |
} |
277 |
else ret = LSCP_FAILED; |
278 |
} |
279 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_DEVICE", "INFO")) { |
280 |
// Getting informations about a specific MIDI input device. |
281 |
// GET MIDI_INPUT_DEVICE INFO <midi-device-id> |
282 |
if (lscp_parser_nextint(&tok) < iMidiDevice) { |
283 |
pszResult = "driver: Alsa\r\n" |
284 |
"active: TRUE\r\n" |
285 |
"channels: 16\r\n" |
286 |
"ports: 1\r\n" |
287 |
".\r\n"; |
288 |
} |
289 |
else ret = LSCP_FAILED; |
290 |
} |
291 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_CHANNEL", "INFO")) { |
292 |
// Getting informations about an audio channel. |
293 |
// GET AUDIO_OUTPUT_CHANNEL INFO <audio-device-id> <audio-channel> |
294 |
if (lscp_parser_nextint(&tok) < iAudioDevice) { |
295 |
pszResult = "name: DummyMonitor\r\n" |
296 |
"is_mix_channel: FALSE\r\n" |
297 |
"mix_channel_destination: 0\r\n" |
298 |
".\r\n"; |
299 |
} |
300 |
else ret = LSCP_FAILED; |
301 |
} |
302 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_PORT", "INFO")) { |
303 |
// Getting informations about a MIDI port. |
304 |
// GET MIDI_INPUT_PORT INFO <midi-device-id> <midi-port> |
305 |
if (lscp_parser_nextint(&tok) < iMidiDevice) { |
306 |
pszResult = "name: DummyKeyboard\r\n" |
307 |
"alsa_seq_bindings: '64:0'\r\n" |
308 |
".\r\n"; |
309 |
} |
310 |
else ret = LSCP_FAILED; |
311 |
} |
312 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_CHANNEL_PARAMETER", "INFO")) { |
313 |
// Getting informations about specific audio channel parameter. |
314 |
// GET AUDIO_OUTPUT_CHANNEL_PARAMETER INFO <audio-device-id> <audio-channel> <param> |
315 |
if (lscp_parser_nextint(&tok) < iAudioDevice) { |
316 |
lscp_parser_nextint(&tok); |
317 |
if (lscp_parser_test(&tok, "is_mix_channel")) { |
318 |
pszResult = "DESCRIPTION: 'Whether this is an audio mix channel'\r\n" |
319 |
"TYPE: BOOL\r\n" |
320 |
"MANDATORY: TRUE\r\n" |
321 |
"FIX: FALSE\r\n" |
322 |
"MULTIPLICITY: FALSE\r\n" |
323 |
"POSSIBILITIES: FALSE,TRUE\r\n" |
324 |
".\r\n"; |
325 |
} |
326 |
else if (lscp_parser_test(&tok, "mix_channel_destination")) { |
327 |
pszResult = "DESCRIPTION: 'Audio mix channel destination'\r\n" |
328 |
"TYPE: INT\r\n" |
329 |
"MANDATORY: TRUE\r\n" |
330 |
"FIX: FALSE\r\n" |
331 |
"MULTIPLICITY: TRUE\r\n" |
332 |
"POSSIBILITIES: 0,1\r\n" |
333 |
".\r\n"; |
334 |
} |
335 |
else ret = LSCP_FAILED; |
336 |
} |
337 |
else ret = LSCP_FAILED; |
338 |
} |
339 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_PORT_PARAMETER", "INFO")) { |
340 |
// Getting informations about specific MIDI port parameter. |
341 |
// GET MIDI_INPUT_PORT_PARAMETER INFO <midi-device-id> <midi-port> <param> |
342 |
if (lscp_parser_nextint(&tok) < iMidiDevice) { |
343 |
lscp_parser_nextint(&tok); |
344 |
if (lscp_parser_test(&tok, "alsa_seq_bindings")) { |
345 |
pszResult = "DESCRIPTION: 'Alsa sequencer port bindings'\r\n" |
346 |
"TYPE: STRING\r\n" |
347 |
"MANDATORY: TRUE\r\n" |
348 |
"FIX: FALSE\r\n" |
349 |
"MULTIPLICITY: TRUE\r\n" |
350 |
"POSSIBILITIES: '64:0','68:0','68:1'\r\n" |
351 |
".\r\n"; |
352 |
} |
353 |
else ret = LSCP_FAILED; |
354 |
} |
355 |
else ret = LSCP_FAILED; |
356 |
} |
357 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICES")) { |
358 |
// Getting all created audio output device count. |
359 |
// GET AUDIO_OUTPUT_DEVICES |
360 |
sprintf(szTemp, "%d\r\n", iAudioDevice); |
361 |
pszResult = szTemp; |
362 |
} |
363 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICES")) { |
364 |
// Getting all created MID input device count. |
365 |
// GET MIDI_INPUT_DEVICES |
366 |
sprintf(szTemp, "%d\r\n", iMidiDevice); |
367 |
pszResult = szTemp; |
368 |
} |
369 |
else if (lscp_parser_test(&tok, "AVAILABLE_ENGINES")) { |
370 |
// Getting all available engine count: |
371 |
// GET AVAILABLE_ENGINES |
372 |
pszResult = "3\r\n"; |
373 |
} |
374 |
else if (lscp_parser_test2(&tok, "SERVER", "INFO")) { |
375 |
// Getting information about the server. |
376 |
// GET SERVER INFO |
377 |
sprintf(szTemp, "DESCRIPTION: example_server (%s) %s\r\n" |
378 |
"VERSION: %s\r\n.\r\n", lscp_server_package(), |
379 |
lscp_server_build(), lscp_server_version()); |
380 |
pszResult = szTemp; |
381 |
} |
382 |
else if (lscp_parser_test2(&tok, "ENGINE", "INFO")) { |
383 |
// Getting information about an engine. |
384 |
// GET ENGINE INFO <engine-name> |
385 |
if (lscp_parser_test(&tok, "GigEngine")) { |
386 |
pszResult = "DESCRIPTION: GigaSampler Engine\r\n" |
387 |
"VERSION: 0.3\r\n" |
388 |
".\r\n"; |
389 |
} |
390 |
else if (lscp_parser_test(&tok, "DLSEngine")) { |
391 |
pszResult = "DESCRIPTION: 'DLS Generic Engine'\r\n" |
392 |
"VERSION: 0.2\r\n" |
393 |
".\r\n"; |
394 |
} |
395 |
else if (lscp_parser_test(&tok, "AkaiEngine")) { |
396 |
pszResult = "DESCRIPTION: Akai Sampler Engine\r\n" |
397 |
"VERSION: 0.1\r\n" |
398 |
".\r\n"; |
399 |
} |
400 |
else ret = LSCP_FAILED; |
401 |
} |
402 |
else if (lscp_parser_test(&tok, "TOTAL_VOICE_COUNT")) { |
403 |
// Current number of active voices: |
404 |
// GET TOTAL_VOICE_COUNT |
405 |
sprintf(szTemp, "%d\r\n", rand() % 100); |
406 |
pszResult = szTemp; |
407 |
} |
408 |
else if (lscp_parser_test(&tok, "TOTAL_VOICE_COUNT_MAX")) { |
409 |
// Maximum amount of active voices: |
410 |
// GET TOTAL_VOICE_COUNT_MAX |
411 |
sprintf(szTemp, "%d\r\n", rand() % 100); |
412 |
pszResult = szTemp; |
413 |
} |
414 |
else if (lscp_parser_test(&tok, "MIDI_INSTRUMENTS")) { |
415 |
// Get the total count of MIDI instrument map entries: |
416 |
// GET MIDI_INSTRUMENTS |
417 |
sprintf(szTemp, "%d\r\n", iMidiInstruments); |
418 |
pszResult = szTemp; |
419 |
} |
420 |
if (lscp_parser_test2(&tok, "MIDI_INSTRUMENT", "INFO")) { |
421 |
// Getting information about a MIDI instrument map entry: |
422 |
// GET MIDI_INSTRUMENT INFO <midi-bank-msb> <midi-bank-lsb> <midi-prog> |
423 |
pszResult = "NAME: DummyName\r\n" |
424 |
"ENGINE_NAME: DummyEngine\r\n" |
425 |
"INSTRUMENT_FILE: DummyInstrument.gig\r\n" |
426 |
"INSTRUMENT_NR: 0\r\n" |
427 |
"INSTRUMENT_NAME: Dummy Instrument\r\n" |
428 |
"LOAD_MODE: ON_DEMAND\r\n" |
429 |
"VOLUME: 0.5\r\n" |
430 |
".\r\n"; |
431 |
} |
432 |
else ret = LSCP_FAILED; |
433 |
} |
434 |
else if (lscp_parser_test(&tok, "LIST")) { |
435 |
if (lscp_parser_test(&tok, "CHANNELS")) { |
436 |
// Getting all created sampler channel list. |
437 |
// LIST CHANNELS |
438 |
if (iSamplerChannel > 0) { |
439 |
strcpy(szTemp, "0"); |
440 |
for (i = 1; i < iSamplerChannel && strlen(szTemp) < sizeof(szTemp) - 8; i++) |
441 |
sprintf(szTemp + strlen(szTemp), ",%d", i); |
442 |
strcat(szTemp, "\r\n"); |
443 |
pszResult = szTemp; |
444 |
} |
445 |
else ret = LSCP_FAILED; |
446 |
} |
447 |
else if (lscp_parser_test(&tok, "AVAILABLE_ENGINES")) { |
448 |
// Getting all available engines: |
449 |
// LIST AVAILABLE_ENGINES |
450 |
pszResult = "GigEngine,DLSEngine,AkaiEngine\r\n"; |
451 |
} |
452 |
else if (lscp_parser_test(&tok, "AVAILABLE_AUDIO_OUTPUT_DRIVERS")) { |
453 |
// Getting all available audio output drivers. |
454 |
// LIST AVAILABLE_AUDIO_OUTPUT_DRIVERS |
455 |
pszResult = "ALSA,JACK\r\n"; |
456 |
} |
457 |
else if (lscp_parser_test(&tok, "AVAILABLE_MIDI_INPUT_DRIVERS")) { |
458 |
// Getting all available MIDI input drivers. |
459 |
// LIST AVAILABLE_MIDI_INPUT_DRIVERS |
460 |
pszResult = "ALSA\r\n"; |
461 |
} |
462 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICES")) { |
463 |
// Getting all created audio output device list. |
464 |
// LIST AUDIO_OUTPUT_DEVICES |
465 |
if (iAudioDevice > 0) { |
466 |
strcpy(szTemp, "0"); |
467 |
for (i = 1; i < iAudioDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++) |
468 |
sprintf(szTemp + strlen(szTemp), ",%d", i); |
469 |
strcat(szTemp, "\r\n"); |
470 |
pszResult = szTemp; |
471 |
} |
472 |
else ret = LSCP_FAILED; |
473 |
} |
474 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICES")) { |
475 |
// Getting all created MID input device list. |
476 |
// LIST MIDI_INPUT_DEVICES |
477 |
if (iMidiDevice > 0) { |
478 |
strcpy(szTemp, "0"); |
479 |
for (i = 1; i < iMidiDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++) |
480 |
sprintf(szTemp + strlen(szTemp), ",%d", i); |
481 |
strcat(szTemp, "\r\n"); |
482 |
pszResult = szTemp; |
483 |
} |
484 |
else ret = LSCP_FAILED; |
485 |
} |
486 |
else if (lscp_parser_test(&tok, "MIDI_INSTRUMENTS")) { |
487 |
// Getting indeces of all MIDI instrument map entries: |
488 |
// LIST MIDI_INSTRUMENTS |
489 |
if (iMidiInstruments > 0) { |
490 |
strcpy(szTemp, "{0,0,0}"); |
491 |
for (i = 1; i < iMidiInstruments && strlen(szTemp) < sizeof(szTemp) - 16; i++) |
492 |
sprintf(szTemp + strlen(szTemp), ",{0,%d,%d}", i / 128, i % 128); |
493 |
strcat(szTemp, "\r\n"); |
494 |
pszResult = szTemp; |
495 |
} |
496 |
else ret = LSCP_FAILED; |
497 |
} |
498 |
else ret = LSCP_FAILED; |
499 |
} |
500 |
else if (lscp_parser_test(&tok, "SET")) { |
501 |
if (lscp_parser_test(&tok, "CHANNEL")) { |
502 |
if (lscp_parser_test(&tok, "VOLUME")) { |
503 |
// Setting channel volume: |
504 |
// SET CHANNEL VOLUME <sampler-channel> <volume> |
505 |
} |
506 |
else if (lscp_parser_test(&tok, "MUTE")) { |
507 |
// Muting a sampler channel: |
508 |
// SET CHANNEL MUTE <sampler-channel> <mute> |
509 |
} |
510 |
else if (lscp_parser_test(&tok, "SOLO")) { |
511 |
// Soloing a sampler channel: |
512 |
// SET CHANNEL SOLO <sampler-channel> <solo> |
513 |
} |
514 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_TYPE")) { |
515 |
// Setting audio output type: |
516 |
// SET CHANNEL AUDIO_OUTPUT_TYPE <sampler-channel> <audio-output-type> |
517 |
} |
518 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) { |
519 |
// Setting audio output device: |
520 |
// SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id> |
521 |
} |
522 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_CHANNEL")) { |
523 |
// Setting audio output channel: |
524 |
// SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-in> <audio-out> |
525 |
} |
526 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_TYPE")) { |
527 |
// Setting MIDI input type: |
528 |
// SET CHANNEL MIDI_INPUT_TYPE <sampler-channel> <midi-input-type> |
529 |
} |
530 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) { |
531 |
// Setting MIDI input device: |
532 |
// SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id> |
533 |
} |
534 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_PORT")) { |
535 |
// Setting MIDI input port: |
536 |
// SET CHANNEL MIDI_INPUT_PORT <sampler-channel> <midi-input-port> |
537 |
} |
538 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_CHANNEL")) { |
539 |
// Setting MIDI input channel: |
540 |
// SET CHANNEL MIDI_INPUT_CHANNEL <sampler-channel> <midi-input-chan> |
541 |
} |
542 |
else ret = LSCP_FAILED; |
543 |
} |
544 |
else ret = LSCP_FAILED; |
545 |
} |
546 |
else if (lscp_parser_test(&tok, "LOAD")) { |
547 |
if (lscp_parser_test(&tok, "ENGINE")) { |
548 |
// Loading a sampler engine: |
549 |
// LOAD ENGINE <engine-name> <sampler-channel> |
550 |
} |
551 |
else if (lscp_parser_test(&tok, "INSTRUMENT")) { |
552 |
// Loading an instrument: |
553 |
// LOAD INSTRUMENT [NON_MODAL] <filename> <instr-index> <sampler-channel> |
554 |
} |
555 |
else ret = LSCP_FAILED; |
556 |
} |
557 |
else if (lscp_parser_test2(&tok, "ADD", "CHANNEL")) { |
558 |
// Adding a new sampler channel: |
559 |
// ADD CHANNEL |
560 |
if (iSamplerChannel < 16) { |
561 |
sprintf(szTemp, "OK[%d]\r\n", iSamplerChannel++); |
562 |
pszResult = szTemp; |
563 |
} else { |
564 |
iSamplerChannel = 0; |
565 |
ret = LSCP_FAILED; |
566 |
} |
567 |
} |
568 |
else if (lscp_parser_test2(&tok, "REMOVE", "CHANNEL")) { |
569 |
// Removing a sampler channel: |
570 |
// REMOVE CHANNEL <sampler-channel> |
571 |
if (lscp_parser_nextint(&tok) < iSamplerChannel) |
572 |
iSamplerChannel--; |
573 |
else |
574 |
ret = LSCP_FAILED; |
575 |
} |
576 |
else if (lscp_parser_test(&tok, "RESET")) { |
577 |
if (lscp_parser_test(&tok, "CHANNEL")) { |
578 |
// Resetting a sampler channel: |
579 |
// RESET CHANNEL <sampler-channel> |
580 |
if (lscp_parser_nextint(&tok) > iSamplerChannel) |
581 |
ret = LSCP_FAILED; |
582 |
} else { |
583 |
// Reset sampler: |
584 |
// RESET |
585 |
iSamplerChannel = 0; |
586 |
iAudioDevice = 0; |
587 |
iMidiDevice = 0; |
588 |
} |
589 |
} |
590 |
else if (lscp_parser_test(&tok, "CREATE")) { |
591 |
if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) { |
592 |
// Creating an audio output device. |
593 |
// CREATE AUDIO_OUTPUT_DEVICE <audio-output-driver> [<params>] |
594 |
if (iAudioDevice < 8) { |
595 |
sprintf(szTemp, "OK[%d]\r\n", iAudioDevice++); |
596 |
pszResult = szTemp; |
597 |
} else { |
598 |
iAudioDevice = 0; |
599 |
ret = LSCP_FAILED; |
600 |
} |
601 |
} |
602 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) { |
603 |
// Creating an MIDI input device. |
604 |
// CREATE MIDI_INPUT_DEVICE <midi-input-driver> [<params>] |
605 |
if (iMidiDevice < 8) { |
606 |
sprintf(szTemp, "OK[%d]\r\n", iMidiDevice++); |
607 |
pszResult = szTemp; |
608 |
} else { |
609 |
iMidiDevice = 0; |
610 |
ret = LSCP_FAILED; |
611 |
} |
612 |
} |
613 |
else ret = LSCP_FAILED; |
614 |
} |
615 |
else if (lscp_parser_test(&tok, "DESTROY")) { |
616 |
if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) { |
617 |
// Destroying an audio output device. |
618 |
// DESTROY AUDIO_OUTPUT_DEVICE <audio-device-id> |
619 |
if (lscp_parser_nextint(&tok) < iAudioDevice) |
620 |
iAudioDevice--; |
621 |
else |
622 |
ret = LSCP_FAILED; |
623 |
} |
624 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) { |
625 |
// Destroying an MIDI intput device. |
626 |
// DESTROY MIDI_INPUT_DEVICE <midi-device-id> |
627 |
if (lscp_parser_nextint(&tok) < iMidiDevice) |
628 |
iMidiDevice--; |
629 |
else |
630 |
ret = LSCP_FAILED; |
631 |
} |
632 |
else ret = LSCP_FAILED; |
633 |
} |
634 |
else if (lscp_parser_test2(&tok, "MAP", "MIDI_INSTRUMENT")) { |
635 |
// Create or replace a MIDI instrumnet map entry: |
636 |
// MAP MIDI_INSTRUMENT <midi-bank-msb> <midi-bank-lsb> <midi-prog> |
637 |
// <engine-name> <filename> <instr-index> <volume> <load-mode> [<name>] |
638 |
iMidiInstruments++; |
639 |
} |
640 |
else if (lscp_parser_test2(&tok, "UNMAP", "MIDI_INSTRUMENT")) { |
641 |
// Remove an entry from the MIDI instrument map: |
642 |
// UNMAP MIDI_INSTRUMENT <midi-bank-msb> <midi-bank-lsb> <midi-prog> |
643 |
if (iMidiInstruments > 0) |
644 |
iMidiInstruments--; |
645 |
else |
646 |
ret = LSCP_FAILED; |
647 |
} |
648 |
else if (lscp_parser_test2(&tok, "CLEAR", "MIDI_INSTRUMENTS")) { |
649 |
// Clear the MIDI instrumnet map: |
650 |
// CLEAR MIDI_INSTRUMENTS |
651 |
} |
652 |
else if (lscp_parser_test(&tok, "SUBSCRIBE")) { |
653 |
// Register frontend for receiving event notification messages: |
654 |
// SUBSCRIBE <event> |
655 |
ret = lscp_server_subscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok))); |
656 |
} |
657 |
else if (lscp_parser_test(&tok, "UNSUBSCRIBE")) { |
658 |
// Deregister frontend for not receiving event notification messages anymore: |
659 |
// UNSUBSCRIBE <event> |
660 |
ret = lscp_server_unsubscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok))); |
661 |
} |
662 |
else if (lscp_parser_test(&tok, "QUIT")) { |
663 |
// Close client connection: |
664 |
// QUIT |
665 |
lscp_parser_free(&tok); |
666 |
return LSCP_FAILED; // Disconnect. |
667 |
} |
668 |
else ret = LSCP_FAILED; |
669 |
|
670 |
lscp_parser_free(&tok); |
671 |
|
672 |
if (pszResult == NULL) |
673 |
pszResult = (ret == LSCP_OK ? "OK\r\n" : "ERR:1:Failed\r\n"); |
674 |
|
675 |
fprintf(stderr, "> %s", pszResult); |
676 |
|
677 |
return lscp_server_result(pConnect, pszResult, strlen(pszResult)); |
678 |
} |
679 |
|
680 |
//////////////////////////////////////////////////////////////////////// |
681 |
|
682 |
|
683 |
void server_usage (void) |
684 |
{ |
685 |
printf("\n %s %s (Build: %s)\n", lscp_server_package(), lscp_server_version(), lscp_server_build()); |
686 |
|
687 |
fputs("\n Available server commands: help, exit, quit, list", stdout); |
688 |
fputs("\n (all else are broadcast verbatim to subscribers)\n\n", stdout); |
689 |
} |
690 |
|
691 |
void server_prompt (void) |
692 |
{ |
693 |
fputs("lscp_server> ", stdout); |
694 |
} |
695 |
|
696 |
int main (int argc, char *argv[] ) |
697 |
{ |
698 |
lscp_server_t *pServer; |
699 |
char szLine[200]; |
700 |
int cchLine; |
701 |
lscp_connect_t *p; |
702 |
|
703 |
#if defined(WIN32) |
704 |
if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) { |
705 |
fprintf(stderr, "lscp_server: WSAStartup failed.\n"); |
706 |
return -1; |
707 |
} |
708 |
#endif |
709 |
|
710 |
srand(time(NULL)); |
711 |
|
712 |
pServer = lscp_server_create(SERVER_PORT, server_callback, NULL); |
713 |
if (pServer == NULL) |
714 |
return -1; |
715 |
|
716 |
server_usage(); |
717 |
server_prompt(); |
718 |
|
719 |
while (fgets(szLine, sizeof(szLine), stdin)) { |
720 |
|
721 |
cchLine = strlen(szLine); |
722 |
while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r')) |
723 |
cchLine--; |
724 |
szLine[cchLine] = '\0'; |
725 |
|
726 |
if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0) |
727 |
break; |
728 |
else |
729 |
if (strcmp(szLine, "list") == 0) { |
730 |
for (p = pServer->connects.first; p; p = p->next) { |
731 |
printf("client: sock=%d addr=%s port=%d events=0x%04x.\n", |
732 |
p->client.sock, |
733 |
inet_ntoa(p->client.addr.sin_addr), |
734 |
ntohs(p->client.addr.sin_port), |
735 |
(int) p->events |
736 |
); |
737 |
} |
738 |
} |
739 |
else |
740 |
if (cchLine > 0 && strcmp(szLine, "help") != 0) |
741 |
lscp_server_broadcast(pServer, LSCP_EVENT_MISCELLANEOUS, szLine, strlen(szLine)); |
742 |
else |
743 |
server_usage(); |
744 |
|
745 |
server_prompt(); |
746 |
} |
747 |
|
748 |
lscp_server_destroy(pServer); |
749 |
|
750 |
#if defined(WIN32) |
751 |
WSACleanup(); |
752 |
#endif |
753 |
|
754 |
return 0; |
755 |
} |
756 |
|
757 |
// end of example_server.c |