/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/network/lscpserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (show annotations) (download)
Mon Jun 28 04:30:11 2004 UTC (19 years, 9 months ago) by senkov
File size: 41640 byte(s)
* Updated parser, lscp server and sampler class for new MIDI_INPUT
* Minor fixes (and major new bugs) here and there
* Consolidated 3 SET CHANNEL MIDI_xxx commands into one:
SET CHANNEL MIDI_INPUT

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (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 *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include "lscpserver.h"
24 #include "lscpresultset.h"
25
26 #include "../engines/gig/Engine.h"
27 #include "../audiodriver/AudioOutputDeviceFactory.h"
28 #include "../mididriver/MidiInputDeviceFactory.h"
29
30 LSCPServer::LSCPServer(Sampler* pSampler) : Thread(false, 0, -4) {
31 this->pSampler = pSampler;
32 }
33
34 int LSCPServer::Main() {
35 hSocket = socket(AF_INET, SOCK_STREAM, 0);
36 if (hSocket < 0) {
37 std::cerr << "LSCPServer: Could not create server socket." << std::endl;
38 //return -1;
39 exit(EXIT_FAILURE);
40 }
41
42 SocketAddress.sin_family = AF_INET;
43 SocketAddress.sin_port = htons(LSCP_PORT);
44 SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
45
46 if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
47 std::cerr << "LSCPServer: Could not bind server socket." << std::endl;
48 close(hSocket);
49 //return -1;
50 exit(EXIT_FAILURE);
51 }
52
53 listen(hSocket, 1);
54 dmsg(1,("LSCPServer: Server running.\n")); // server running
55
56 // now wait for client connections and handle their requests
57 sockaddr_in client;
58 int length = sizeof(client);
59 while (true) {
60 hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
61 if (hSession < 0) {
62 std::cerr << "LSCPServer: Client connection failed." << std::endl;
63 close(hSocket);
64 //return -1;
65 exit(EXIT_FAILURE);
66 }
67
68 dmsg(1,("LSCPServer: Client connection established.\n"));
69 //send(hSession, "Welcome!\r\n", 10, 0);
70
71 // Parser invocation
72 yyparse_param_t yyparse_param;
73 yyparse_param.pServer = this;
74 yylex_init(&yyparse_param.pScanner);
75 while (yyparse(&yyparse_param) == LSCP_SYNTAX_ERROR); // recall parser in case of syntax error
76 yylex_destroy(yyparse_param.pScanner);
77
78 close(hSession);
79 dmsg(1,("LSCPServer: Client connection terminated.\n"));
80 }
81 }
82
83 /**
84 * Will be called by the parser whenever it wants to send an answer to the
85 * client / frontend.
86 *
87 * @param ReturnMessage - message that will be send to the client
88 */
89 void LSCPServer::AnswerClient(String ReturnMessage) {
90 dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
91 send(hSession, ReturnMessage.c_str(), ReturnMessage.size(), 0);
92 }
93
94 /**
95 * Find a created audio output device index.
96 */
97 int LSCPServer::GetAudioOutputDeviceIndex ( AudioOutputDevice *pDevice )
98 {
99 // Search for the created device to get its index
100 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
101 std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
102 for (; iter != devices.end(); iter++) {
103 if (iter->second == pDevice)
104 return iter->first;
105 }
106 // Not found.
107 return -1;
108 }
109
110 /**
111 * Find a created midi input device index.
112 */
113 int LSCPServer::GetMidiInputDeviceIndex ( MidiInputDevice *pDevice )
114 {
115 // Search for the created device to get its index
116 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
117 std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
118 for (; iter != devices.end(); iter++) {
119 if (iter->second == pDevice)
120 return iter->first;
121 }
122 // Not found.
123 return -1;
124 }
125
126 String LSCPServer::CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters) {
127 dmsg(2,("LSCPServer: CreateAudioOutputDevice(Driver=%s)\n", Driver.c_str()));
128 LSCPResultSet result;
129 try {
130 AudioOutputDevice* pDevice = pSampler->CreateAudioOutputDevice(Driver, Parameters);
131 // search for the created device to get its index
132 int index = GetAudioOutputDeviceIndex(pDevice);
133 if (index == -1) throw LinuxSamplerException("Internal error: could not find created audio output device.");
134 result = index; // success
135 }
136 catch (LinuxSamplerException e) {
137 result.Error(e);
138 }
139 return result.Produce();
140 }
141
142 String LSCPServer::CreateMidiInputDevice(String Driver, std::map<String,String> Parameters) {
143 dmsg(2,("LSCPServer: CreateMidiInputDevice(Driver=%s)\n", Driver.c_str()));
144 LSCPResultSet result;
145 try {
146 MidiInputDevice* pDevice = pSampler->CreateMidiInputDevice(Driver, Parameters);
147 // search for the created device to get its index
148 int index = GetMidiInputDeviceIndex(pDevice);
149 if (index == -1) throw LinuxSamplerException("Internal error: could not find created midi input device.");
150 result = index; // success
151 }
152 catch (LinuxSamplerException e) {
153 result.Error(e);
154 }
155 return result.Produce();
156 }
157
158 String LSCPServer::DestroyAudioOutputDevice(uint DeviceIndex) {
159 dmsg(2,("LSCPServer: DestroyAudioOutputDevice(DeviceIndex=%d)\n", DeviceIndex));
160 LSCPResultSet result;
161 try {
162 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
163 if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
164 AudioOutputDevice* pDevice = devices[DeviceIndex];
165 pSampler->DestroyAudioOutputDevice(pDevice);
166 }
167 catch (LinuxSamplerException e) {
168 result.Error(e);
169 }
170 return result.Produce();
171 }
172
173 String LSCPServer::DestroyMidiInputDevice(uint DeviceIndex) {
174 dmsg(2,("LSCPServer: DestroyMidiInputDevice(DeviceIndex=%d)\n", DeviceIndex));
175 LSCPResultSet result;
176 try {
177 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
178 MidiInputDevice* pDevice = devices[DeviceIndex];
179 if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
180 pSampler->DestroyMidiInputDevice(pDevice);
181 }
182 catch (LinuxSamplerException e) {
183 result.Error(e);
184 }
185 return result.Produce();
186 }
187
188 /**
189 * Will be called by the parser to load an instrument.
190 */
191 String LSCPServer::LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground) {
192 dmsg(2,("LSCPServer: LoadInstrument(Filename=%s,Instrument=%d,SamplerChannel=%d)\n", Filename.c_str(), uiInstrument, uiSamplerChannel));
193 LSCPResultSet result;
194 try {
195 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
196 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
197 Engine* pEngine = pSamplerChannel->GetEngine();
198 if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
199 if (pSamplerChannel->GetAudioOutputDevice() == NULL)
200 throw LinuxSamplerException("No audio output device on channel");
201 if (bBackground) {
202 LSCPLoadInstrument *pLoadInstrument = new LSCPLoadInstrument(pEngine, Filename.c_str(), uiInstrument);
203 pLoadInstrument->StartThread();
204 }
205 else pEngine->LoadInstrument(Filename.c_str(), uiInstrument);
206 }
207 catch (LinuxSamplerException e) {
208 result.Error(e);
209 }
210 return result.Produce();
211 }
212
213 /**
214 * Will be called by the parser to load and deploy an engine.
215 */
216 String LSCPServer::LoadEngine(String EngineName, uint uiSamplerChannel) {
217 dmsg(2,("LSCPServer: LoadEngine(EngineName=%s,SamplerChannel=%d)\n", EngineName.c_str(), uiSamplerChannel));
218 LSCPResultSet result;
219 try {
220 Engine::type_t type;
221 if ((EngineName == "GigEngine") || (EngineName == "gig")) type = Engine::type_gig;
222 else throw LinuxSamplerException("Unknown engine type");
223 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
224 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
225 pSamplerChannel->LoadEngine(type);
226 }
227 catch (LinuxSamplerException e) {
228 result.Error(e);
229 }
230 return result.Produce();
231 }
232
233 /**
234 * Will be called by the parser to get the amount of sampler channels.
235 */
236 String LSCPServer::GetChannels() {
237 dmsg(2,("LSCPServer: GetChannels()\n"));
238 LSCPResultSet result;
239 result.Add(pSampler->SamplerChannels());
240 return result.Produce();
241 }
242
243 /**
244 * Will be called by the parser to add a sampler channel.
245 */
246 String LSCPServer::AddChannel() {
247 dmsg(2,("LSCPServer: AddChannel()\n"));
248 SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();
249 LSCPResultSet result(pSamplerChannel->Index());
250 return result.Produce();
251 }
252
253 /**
254 * Will be called by the parser to remove a sampler channel.
255 */
256 String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
257 dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
258 LSCPResultSet result;
259 pSampler->RemoveSamplerChannel(uiSamplerChannel);
260 return result.Produce();
261 }
262
263 /**
264 * Will be called by the parser to get all available engines.
265 */
266 String LSCPServer::GetAvailableEngines() {
267 dmsg(2,("LSCPServer: GetAvailableEngines()\n"));
268 LSCPResultSet result("GigEngine");
269 return result.Produce();
270 }
271
272 /**
273 * Will be called by the parser to get descriptions for a particular engine.
274 */
275 String LSCPServer::GetEngineInfo(String EngineName) {
276 dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
277 LSCPResultSet result;
278 try {
279 if ((EngineName == "GigEngine") || (EngineName == "gig")) {
280 Engine* pEngine = new LinuxSampler::gig::Engine;
281 result.Add(pEngine->Description());
282 result.Add(pEngine->Version());
283 delete pEngine;
284 }
285 else throw LinuxSamplerException("Unknown engine type");
286 }
287 catch (LinuxSamplerException e) {
288 result.Error(e);
289 }
290 return result.Produce();
291 }
292
293 /**
294 * Will be called by the parser to get informations about a particular
295 * sampler channel.
296 */
297 String LSCPServer::GetChannelInfo(uint uiSamplerChannel) {
298 dmsg(2,("LSCPServer: GetChannelInfo(SamplerChannel=%d)\n", uiSamplerChannel));
299 LSCPResultSet result;
300 try {
301 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
302 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
303 Engine* pEngine = pSamplerChannel->GetEngine();
304
305 //Defaults values
306 String EngineName = "NONE";
307 float Volume = 0;
308 String InstrumentFileName = "NONE";
309 int InstrumentIndex = -1;
310 int InstrumentStatus = -1;
311
312 if (pEngine) {
313 EngineName = pEngine->EngineName();
314 Volume = pEngine->Volume();
315 InstrumentStatus = pEngine->InstrumentStatus();
316 InstrumentIndex = pEngine->InstrumentIndex();
317 if (InstrumentIndex != -1)
318 InstrumentFileName = pEngine->InstrumentFileName();
319 }
320
321 result.Add("ENGINE_NAME", EngineName);
322 result.Add("VOLUME", Volume);
323
324 //Some not-so-hardcoded stuff to make GUI look good
325 result.Add("AUDIO_OUTPUT_DEVICE", GetAudioOutputDeviceIndex(pSamplerChannel->GetAudioOutputDevice()));
326 result.Add("AUDIO_OUTPUT_CHANNELS", "2");
327 result.Add("AUDIO_OUTPUT_ROUTING", "0,1");
328
329 result.Add("INSTRUMENT_FILE", InstrumentFileName);
330 result.Add("INSTRUMENT_NR", InstrumentIndex);
331 result.Add("INSTRUMENT_STATUS", InstrumentStatus);
332
333 MidiInputDevice *pDevice = pSamplerChannel->GetMidiInputDevice();
334 if (pDevice) {
335 result.Add("MIDI_INPUT_DEVICE", GetMidiInputDeviceIndex(pDevice));
336 MidiInputDevice::MidiInputPort *pPort = pSamplerChannel->GetMidiInputPort();
337 if (pPort) {
338 result.Add("MIDI_INPUT_PORT", (int)pPort->GetPortNumber());
339 result.Add("MIDI_INPUT_CHANNEL", (int)pSamplerChannel->GetMidiInputChannel());
340 }
341
342 }
343 }
344 catch (LinuxSamplerException e) {
345 result.Error(e);
346 }
347 return result.Produce();
348 }
349
350 /**
351 * Will be called by the parser to get the amount of active voices on a
352 * particular sampler channel.
353 */
354 String LSCPServer::GetVoiceCount(uint uiSamplerChannel) {
355 dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel));
356 LSCPResultSet result;
357 try {
358 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
359 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
360 Engine* pEngine = pSamplerChannel->GetEngine();
361 if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
362 result.Add(pEngine->VoiceCount());
363 }
364 catch (LinuxSamplerException e) {
365 result.Error(e);
366 }
367 return result.Produce();
368 }
369
370 /**
371 * Will be called by the parser to get the amount of active disk streams on a
372 * particular sampler channel.
373 */
374 String LSCPServer::GetStreamCount(uint uiSamplerChannel) {
375 dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel));
376 LSCPResultSet result;
377 try {
378 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
379 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
380 Engine* pEngine = pSamplerChannel->GetEngine();
381 if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
382 result.Add(pEngine->DiskStreamCount());
383 }
384 catch (LinuxSamplerException e) {
385 result.Error(e);
386 }
387 return result.Produce();
388 }
389
390 /**
391 * Will be called by the parser to get the buffer fill states of all disk
392 * streams on a particular sampler channel.
393 */
394 String LSCPServer::GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel) {
395 dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel));
396 LSCPResultSet result;
397 try {
398 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
399 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
400 Engine* pEngine = pSamplerChannel->GetEngine();
401 if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
402 if (!pEngine->DiskStreamSupported())
403 result.Add("NA");
404 else {
405 switch (ResponseType) {
406 case fill_response_bytes:
407 result.Add(pEngine->DiskStreamBufferFillBytes());
408 break;
409 case fill_response_percentage:
410 result.Add(pEngine->DiskStreamBufferFillPercentage());
411 break;
412 default:
413 throw LinuxSamplerException("Unknown fill response type");
414 }
415 }
416 }
417 catch (LinuxSamplerException e) {
418 result.Error(e);
419 }
420 return result.Produce();
421 }
422
423 String LSCPServer::GetAvailableAudioOutputDrivers() {
424 dmsg(2,("LSCPServer: GetAvailableAudioOutputDrivers()\n"));
425 LSCPResultSet result;
426 try {
427 String s = AudioOutputDeviceFactory::AvailableDriversAsString();
428 result.Add(s);
429 }
430 catch (LinuxSamplerException e) {
431 result.Error(e);
432 }
433 return result.Produce();
434 }
435
436 String LSCPServer::GetAvailableMidiInputDrivers() {
437 dmsg(2,("LSCPServer: GetAvailableMidiInputDrivers()\n"));
438 LSCPResultSet result;
439 try {
440 String s = MidiInputDeviceFactory::AvailableDriversAsString();
441 result.Add(s);
442 }
443 catch (LinuxSamplerException e) {
444 result.Error(e);
445 }
446 return result.Produce();
447 }
448
449 String LSCPServer::GetMidiInputDriverInfo(String Driver) {
450 dmsg(2,("LSCPServer: GetMidiInputDriverInfo(Driver=%s)\n",Driver.c_str()));
451 LSCPResultSet result;
452 try {
453 result.Add("DESCRIPTION", MidiInputDeviceFactory::GetDriverDescription(Driver));
454 result.Add("VERSION", MidiInputDeviceFactory::GetDriverVersion(Driver));
455
456 std::map<String,DeviceCreationParameter*> parameters = MidiInputDeviceFactory::GetAvailableDriverParameters(Driver);
457 if (parameters.size()) { // if there are parameters defined for this driver
458 String s;
459 std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
460 for (;iter != parameters.end(); iter++) {
461 if (s != "") s += ",";
462 s += iter->first;
463 }
464 result.Add("PARAMETERS", s);
465 }
466 }
467 catch (LinuxSamplerException e) {
468 result.Error(e);
469 }
470 return result.Produce();
471 }
472
473 String LSCPServer::GetAudioOutputDriverInfo(String Driver) {
474 dmsg(2,("LSCPServer: GetAudioOutputDriverInfo(Driver=%s)\n",Driver.c_str()));
475 LSCPResultSet result;
476 try {
477 result.Add("DESCRIPTION", AudioOutputDeviceFactory::GetDriverDescription(Driver));
478 result.Add("VERSION", AudioOutputDeviceFactory::GetDriverVersion(Driver));
479
480 std::map<String,DeviceCreationParameter*> parameters = AudioOutputDeviceFactory::GetAvailableDriverParameters(Driver);
481 if (parameters.size()) { // if there are parameters defined for this driver
482 String s;
483 std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
484 for (;iter != parameters.end(); iter++) {
485 if (s != "") s += ",";
486 s += iter->first;
487 }
488 result.Add("PARAMETERS", s);
489 }
490 }
491 catch (LinuxSamplerException e) {
492 result.Error(e);
493 }
494 return result.Produce();
495 }
496
497 String LSCPServer::GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
498 dmsg(2,("LSCPServer: GetMidiInputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str()));
499 LSCPResultSet result;
500 try {
501 DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter);
502 result.Add("TYPE", pParameter->Type());
503 result.Add("DESCRIPTION", pParameter->Description());
504 result.Add("MANDATORY", pParameter->Mandatory());
505 result.Add("FIX", pParameter->Fix());
506 result.Add("MULTIPLICITY", pParameter->Multiplicity());
507 if (pParameter->Depends()) result.Add("DEPENDS", pParameter->Depends());
508 if (pParameter->Default()) result.Add("DEFAULT", pParameter->Default());
509 if (pParameter->RangeMin()) result.Add("RANGE_MIN", pParameter->RangeMin());
510 if (pParameter->RangeMax()) result.Add("RANGE_MAX", pParameter->RangeMax());
511 if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
512 }
513 catch (LinuxSamplerException e) {
514 result.Error(e);
515 }
516 return result.Produce();
517 }
518
519 String LSCPServer::GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList) {
520 dmsg(2,("LSCPServer: GetAudioOutputDriverParameterInfo(Driver=%s,Parameter=%s)\n",Driver.c_str(),Parameter.c_str()));
521 LSCPResultSet result;
522 try {
523 DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter);
524 result.Add("TYPE", pParameter->Type());
525 result.Add("DESCRIPTION", pParameter->Description());
526 result.Add("MANDATORY", pParameter->Mandatory());
527 result.Add("FIX", pParameter->Fix());
528 result.Add("MULTIPLICITY", pParameter->Multiplicity());
529 if (pParameter->Depends()) result.Add("DEPENDS", pParameter->Depends());
530 if (pParameter->Default()) result.Add("DEFAULT", pParameter->Default());
531 if (pParameter->RangeMin()) result.Add("RANGE_MIN", pParameter->RangeMin());
532 if (pParameter->RangeMax()) result.Add("RANGE_MAX", pParameter->RangeMax());
533 if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
534 }
535 catch (LinuxSamplerException e) {
536 result.Error(e);
537 }
538 return result.Produce();
539 }
540
541 String LSCPServer::GetAudioOutputDeviceCount() {
542 dmsg(2,("LSCPServer: GetAudioOutputDeviceCount()\n"));
543 LSCPResultSet result;
544 try {
545 uint count = pSampler->AudioOutputDevices();
546 result.Add(count); // success
547 }
548 catch (LinuxSamplerException e) {
549 result.Error(e);
550 }
551 return result.Produce();
552 }
553
554 String LSCPServer::GetMidiInputDeviceCount() {
555 dmsg(2,("LSCPServer: GetMidiInputDeviceCount()\n"));
556 LSCPResultSet result;
557 try {
558 uint count = pSampler->MidiInputDevices();
559 result.Add(count); // success
560 }
561 catch (LinuxSamplerException e) {
562 result.Error(e);
563 }
564 return result.Produce();
565 }
566
567 String LSCPServer::GetAudioOutputDevices() {
568 dmsg(2,("LSCPServer: GetAudioOutputDevices()\n"));
569 LSCPResultSet result;
570 try {
571 String s;
572 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
573 std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
574 for (; iter != devices.end(); iter++) {
575 if (s != "") s += ",";
576 s += ToString(iter->first);
577 }
578 result.Add(s);
579 }
580 catch (LinuxSamplerException e) {
581 result.Error(e);
582 }
583 return result.Produce();
584 }
585
586 String LSCPServer::GetMidiInputDevices() {
587 dmsg(2,("LSCPServer: GetMidiInputDevices()\n"));
588 LSCPResultSet result;
589 try {
590 String s;
591 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
592 std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
593 for (; iter != devices.end(); iter++) {
594 if (s != "") s += ",";
595 s += ToString(iter->first);
596 }
597 result.Add(s);
598 }
599 catch (LinuxSamplerException e) {
600 result.Error(e);
601 }
602 return result.Produce();
603 }
604
605 String LSCPServer::GetAudioOutputDeviceInfo(uint DeviceIndex) {
606 dmsg(2,("LSCPServer: GetAudioOutputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
607 LSCPResultSet result;
608 try {
609 std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
610 if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
611 AudioOutputDevice* pDevice = devices[DeviceIndex];
612 result.Add("driver", pDevice->Driver());
613 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
614 std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
615 for (; iter != parameters.end(); iter++) {
616 result.Add(iter->first, iter->second->Value());
617 }
618 }
619 catch (LinuxSamplerException e) {
620 result.Error(e);
621 }
622 return result.Produce();
623 }
624
625 String LSCPServer::GetMidiInputDeviceInfo(uint DeviceIndex) {
626 dmsg(2,("LSCPServer: GetMidiInputDeviceInfo(DeviceIndex=%d)\n",DeviceIndex));
627 LSCPResultSet result;
628 try {
629 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
630 MidiInputDevice* pDevice = devices[DeviceIndex];
631 if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");
632 result.Add("driver", pDevice->Driver());
633 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
634 std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
635 for (; iter != parameters.end(); iter++) {
636 result.Add(iter->first, iter->second->Value());
637 }
638 }
639 catch (LinuxSamplerException e) {
640 result.Error(e);
641 }
642 return result.Produce();
643 }
644 String LSCPServer::GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex) {
645 dmsg(2,("LSCPServer: GetMidiInputPortInfo(DeviceIndex=%d, PortIndex=%d)\n",DeviceIndex, PortIndex));
646 LSCPResultSet result;
647 try {
648 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
649 MidiInputDevice* pDevice = devices[DeviceIndex];
650 if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");
651 MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
652 if (!pMidiInputPort) throw LinuxSamplerException("There is no midi input port with index " + ToString(PortIndex) + ".");
653 std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
654 std::map<String,DeviceCreationParameter*>::iterator iter = parameters.begin();
655 for (; iter != parameters.end(); iter++) {
656 result.Add(iter->first, iter->second->Value());
657 }
658 }
659 catch (LinuxSamplerException e) {
660 result.Error(e);
661 }
662 return result.Produce();
663 }
664
665 String LSCPServer::GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId) {
666 dmsg(2,("LSCPServer: GetAudioOutputChannelInfo(DeviceId=%d,ChannelId)\n",DeviceId,ChannelId));
667 LSCPResultSet result;
668 try {
669 // get audio output device
670 std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
671 if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
672 AudioOutputDevice* pDevice = devices[DeviceId];
673
674 // get audio channel
675 AudioChannel* pChannel = pDevice->Channel(ChannelId);
676 if (!pChannel) throw LinuxSamplerException("Audio ouotput device does not have channel " + ToString(ChannelId) + ".");
677
678 // return the values of all audio channel parameters
679 std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
680 std::map<String,DeviceRuntimeParameter*>::iterator iter = parameters.begin();
681 for (; iter != parameters.end(); iter++) {
682 result.Add(iter->first, iter->second->Value());
683 }
684 }
685 catch (LinuxSamplerException e) {
686 result.Error(e);
687 }
688 return result.Produce();
689 }
690
691 String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) {
692 dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str()));
693 LSCPResultSet result;
694 try {
695 // get audio output device
696 std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
697 if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
698 AudioOutputDevice* pDevice = devices[DeviceId];
699
700 // get audio channel
701 AudioChannel* pChannel = pDevice->Channel(ChannelId);
702 if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");
703
704 // get desired audio channel parameter
705 std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
706 if (!parameters[ParameterName]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParameterName + "'.");
707 DeviceRuntimeParameter* pParameter = parameters[ParameterName];
708
709 // return all fields of this audio channel parameter
710 result.Add("TYPE", pParameter->Type());
711 result.Add("DESCRIPTION", pParameter->Description());
712 result.Add("FIX", pParameter->Fix());
713 result.Add("MULTIPLICITY", pParameter->Multiplicity());
714 if (pParameter->RangeMin()) result.Add("RANGE_MIN", pParameter->RangeMin());
715 if (pParameter->RangeMax()) result.Add("RANGE_MAX", pParameter->RangeMax());
716 if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities());
717 }
718 catch (LinuxSamplerException e) {
719 result.Error(e);
720 }
721 return result.Produce();
722 }
723
724 String LSCPServer::SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal) {
725 dmsg(2,("LSCPServer: SetAudioOutputChannelParameter(DeviceId=%d,ChannelId=%d,ParamKey=%s,ParamVal=%s)\n",DeviceId,ChannelId,ParamKey.c_str(),ParamVal.c_str()));
726 LSCPResultSet result;
727 try {
728 // get audio output device
729 std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
730 if (!devices[DeviceId]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceId) + ".");
731 AudioOutputDevice* pDevice = devices[DeviceId];
732
733 // get audio channel
734 AudioChannel* pChannel = pDevice->Channel(ChannelId);
735 if (!pChannel) throw LinuxSamplerException("Audio output device does not have channel " + ToString(ChannelId) + ".");
736
737 // get desired audio channel parameter
738 std::map<String,DeviceRuntimeParameter*> parameters = pChannel->ChannelParameters();
739 if (!parameters[ParamKey]) throw LinuxSamplerException("Audio channel does not provide a parameter '" + ParamKey + "'.");
740 DeviceRuntimeParameter* pParameter = parameters[ParamKey];
741
742 // set new channel parameter value
743 pParameter->SetValue(ParamVal);
744 }
745 catch (LinuxSamplerException e) {
746 result.Error(e);
747 }
748 return result.Produce();
749 }
750
751 String LSCPServer::SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
752 dmsg(2,("LSCPServer: SetAudioOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
753 LSCPResultSet result;
754 try {
755 std::map<uint,AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
756 if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no audio output device with index " + ToString(DeviceIndex) + ".");
757 AudioOutputDevice* pDevice = devices[DeviceIndex];
758 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
759 if (!parameters[ParamKey]) throw LinuxSamplerException("Audio output device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
760 parameters[ParamKey]->SetValue(ParamVal);
761 }
762 catch (LinuxSamplerException e) {
763 result.Error(e);
764 }
765 return result.Produce();
766 }
767
768 String LSCPServer::SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal) {
769 dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
770 LSCPResultSet result;
771 try {
772 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
773 if (!devices[DeviceIndex]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");
774 MidiInputDevice* pDevice = devices[DeviceIndex];
775 std::map<String,DeviceCreationParameter*> parameters = pDevice->DeviceParameters();
776 if (!parameters[ParamKey]) throw LinuxSamplerException("Midi input device " + ToString(DeviceIndex) + " does not have a device parameter '" + ParamKey + "'");
777 parameters[ParamKey]->SetValue(ParamVal);
778 }
779 catch (LinuxSamplerException e) {
780 result.Error(e);
781 }
782 return result.Produce();
783 }
784
785 String LSCPServer::SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal) {
786 dmsg(2,("LSCPServer: SetMidiOutputDeviceParameter(DeviceIndex=%d,ParamKey=%s,ParamVal=%s)\n",DeviceIndex,ParamKey.c_str(),ParamVal.c_str()));
787 LSCPResultSet result;
788 try {
789 std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
790 MidiInputDevice* pDevice = devices[DeviceIndex];
791 if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceIndex) + ".");
792 MidiInputDevice::MidiInputPort* pMidiInputPort = pDevice->GetPort(PortIndex);
793 if (!pMidiInputPort) throw LinuxSamplerException("There is no midi input port with index " + ToString(PortIndex) + ".");
794 std::map<String,DeviceCreationParameter*> parameters = pMidiInputPort->DeviceParameters();
795 if (!parameters[ParamKey]) throw LinuxSamplerException("Midi input device " + ToString(PortIndex) + " does not have a parameter '" + ParamKey + "'");
796 parameters[ParamKey]->SetValue(ParamVal);
797 }
798 catch (LinuxSamplerException e) {
799 result.Error(e);
800 }
801 return result.Produce();
802 }
803
804 /**
805 * Will be called by the parser to change the audio output channel for
806 * playback on a particular sampler channel.
807 */
808 String LSCPServer::SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel) {
809 dmsg(2,("LSCPServer: SetAudioOutputChannel(ChannelAudioOutputChannel=%d, AudioOutputDeviceInputChannel=%d, SamplerChannel=%d)\n",ChannelAudioOutputChannel,AudioOutputDeviceInputChannel,uiSamplerChannel));
810 return "ERR:0:Not implemented yet.\r\n"; //FIXME: Add support for this in resultset class?
811 }
812
813 String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
814 dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
815 LSCPResultSet result;
816 try {
817 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
818 if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
819 // Driver type name aliasing...
820 if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa";
821 if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack";
822 // Check if there's one audio output device already created
823 // for the intended audio driver type (AudioOutputDriver)...
824 AudioOutputDevice *pDevice = NULL;
825 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
826 std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
827 for (; iter != devices.end(); iter++) {
828 if ((iter->second)->Driver() == AudioOutputDriver) {
829 pDevice = iter->second;
830 break;
831 }
832 }
833 // If it doesn't exist, create a new one with default parameters...
834 if (pDevice == NULL) {
835 std::map<String,String> params;
836 pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
837 }
838 // Must have a device...
839 if (pDevice == NULL)
840 throw LinuxSamplerException("Internal error: could not create audio output device.");
841 // Set it as the current channel device...
842 pSamplerChannel->SetAudioOutputDevice(pDevice);
843 }
844 catch (LinuxSamplerException e) {
845 result.Error(e);
846 }
847 return result.Produce();
848 }
849
850 String LSCPServer::SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel) {
851 dmsg(2,("LSCPServer: SetMIDIInputType(String MidiInputDriver=%s, SamplerChannel=%d)\n",MidiInputDriver.c_str(),uiSamplerChannel));
852 LSCPResultSet result;
853 try {
854 #if 1
855 throw LinuxSamplerException("Command deprecated");
856 #else
857 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
858 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
859 // FIXME: workaround until MIDI driver configuration is implemented (using a Factory class for the MIDI input drivers then, like its already done for audio output drivers)
860 if (MidiInputDriver == "ALSA") MidiInputDriver = "Alsa";
861 if (MidiInputDriver != "Alsa") throw LinuxSamplerException("Unknown MIDI input driver '" + MidiInputDriver + "'.");
862 MidiInputDevice::type_t MidiInputType = MidiInputDevice::type_alsa;
863 pSamplerChannel->SetMidiInputDevice(MidiInputType);
864 #endif
865 }
866 catch (LinuxSamplerException e) {
867 result.Error(e);
868 }
869 return result.Produce();
870 }
871
872 /**
873 * Will be called by the parser to change the MIDI input device, port and channel on which
874 * engine of a particular sampler channel should listen to.
875 */
876 String LSCPServer::SetMIDIInput(uint MIDIDevice, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel) {
877 dmsg(2,("LSCPServer: SetMIDIInput(MIDIDevice=%d, MIDIPort=%d, MIDIChannel=%d, SamplerChannel=%d)\n", MIDIDevice, MIDIPort, MIDIChannel, uiSamplerChannel));
878 LSCPResultSet result;
879 try {
880 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
881 if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
882 std::map<uint, MidiInputDevice*> devices = pSampler->GetMidiInputDevices();
883 MidiInputDevice* pDevice = devices[MIDIDevice];
884 if (!pDevice) throw LinuxSamplerException("There is no midi input device with index " + ToString(MIDIDevice));
885 pSamplerChannel->SetMidiInputPort(pDevice, MIDIPort, (MidiInputDevice::MidiInputPort::midi_chan_t) MIDIChannel);
886 }
887 catch (LinuxSamplerException e) {
888 result.Error(e);
889 }
890 return result.Produce();
891 }
892
893 String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
894 LSCPResultSet result;
895 try {
896 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
897 if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel));
898 std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
899 AudioOutputDevice* pDevice = devices[AudioDeviceId];
900 if (!pDevice) throw LinuxSamplerException("There is no audio output device with index " + ToString(AudioDeviceId));
901 pSamplerChannel->SetAudioOutputDevice(pDevice);
902 }
903 catch (LinuxSamplerException e) {
904 result.Error(e);
905 }
906 return result.Produce();
907 }
908
909 /**
910 * Will be called by the parser to change the global volume factor on a
911 * particular sampler channel.
912 */
913 String LSCPServer::SetVolume(double Volume, uint uiSamplerChannel) {
914 dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", Volume, uiSamplerChannel));
915 LSCPResultSet result;
916 try {
917 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
918 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
919 Engine* pEngine = pSamplerChannel->GetEngine();
920 if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
921 pEngine->Volume(Volume);
922 }
923 catch (LinuxSamplerException e) {
924 result.Error(e);
925 }
926 return result.Produce();
927 }
928
929 /**
930 * Will be called by the parser to reset a particular sampler channel.
931 */
932 String LSCPServer::ResetChannel(uint uiSamplerChannel) {
933 dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel));
934 LSCPResultSet result;
935 try {
936 SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
937 if (!pSamplerChannel) throw LinuxSamplerException("Index out of bounds");
938 Engine* pEngine = pSamplerChannel->GetEngine();
939 if (!pEngine) throw LinuxSamplerException("No engine loaded on channel");
940 pEngine->Reset();
941 }
942 catch (LinuxSamplerException e) {
943 result.Error(e);
944 }
945 return result.Produce();
946 }
947
948 /**
949 * Will be called by the parser to subscribe a client (frontend) on the
950 * server for receiving event messages.
951 */
952 String LSCPServer::SubscribeNotification(event_t Event) {
953 dmsg(2,("LSCPServer: SubscribeNotification(Event=%d)\n", Event));
954 return "ERR:0:Not implemented yet.\r\n";
955 }
956
957 /**
958 * Will be called by the parser to unsubscribe a client on the server
959 * for not receiving further event messages.
960 */
961 String LSCPServer::UnsubscribeNotification(event_t Event) {
962 dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%d)\n", Event));
963 return "ERR:0:Not implemented yet.\r\n";
964 }
965
966
967 // Instrument loader constructor.
968 LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)
969 : Thread(false, 0, -4)
970 {
971 this->pEngine = pEngine;
972 this->Filename = Filename;
973 this->uiInstrument = uiInstrument;
974 }
975
976 // Instrument loader process.
977 int LSCPLoadInstrument::Main()
978 {
979 try {
980 pEngine->LoadInstrument(Filename.c_str(), uiInstrument);
981 }
982
983 catch (LinuxSamplerException e) {
984 e.PrintMessage();
985 }
986
987 // Always re-enable the engine.
988 pEngine->Enable();
989
990 // FIXME: Shoot ourselves on the foot?
991 delete this;
992 }

  ViewVC Help
Powered by ViewVC