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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 983 - (show annotations) (download)
Mon Dec 18 09:39:20 2006 UTC (13 years, 3 months ago) by capela
File MIME type: text/plain
File size: 26215 byte(s)
- Updated examples.

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

  ViewVC Help
Powered by ViewVC