summaryrefslogtreecommitdiffstats
path: root/soundserver/artsshell.cc
blob: ebda6561cbe08aac053b5574e4644a8c089b2947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*

        Copyright (C) 2000-2001 Jeff Tranter
                                tranter@kde.org

                                Stefan Westerfeld
                                stefan@space.twc.de

                           2002 Matthias Kretz <kretz@kde.org>
                           2003 Arnold Krille <arnold@arnoldarts.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


------------------------------------------------------------------------

    artsshell - command line access to aRts functions

This is a utility (similar to what was in aRts 0.3) to provide command
line access to aRts functions.

usage:

artsshell [options] <command> [command-options]

Options:

-q   - suppress all output.
-h   - display command usage.
-v   - show version.

Commands:

help, ?

Show brief summary of commands.

version

Display aRts version.

suspend

This suspends the sound server if it is not in use. It can be useful
when running programs that are not artsd-enabled, although in most
cases you should try to use artsdsp first.

Returns zero status if successful or if the sound server was already
suspended, 1 if unable to connect to the sound server (e.g. it is not
running), and 2 if unable to suspend the sound server (e.g. it is
being used by an application). The sound server will attempt to start
again if it receives any requests.

status

Display status information about the server, i.e. is it running, is it
suspended, real-time status, etc.

terminate

Terminate the sound server.

autosuspend <seconds>

Sets the autosuspend time for the specified number of seconds. A value
of zero disables the autosuspend feature.

networkbuffers <n>

When running artsd over a network connection a large buffer size is
desirable to avoid dropouts. This command allows increasing the buffer
size by a factor of <n> from the default.

stereoeffect insert [top|bottom] <module name>
stereoeffect remove <id>
stereoeffect list

Inserts or removes a stereo effect into the stereo effect stack. The
list option lists all stereo effects. When inserting, returns an
identifier that can be used for removing it. It can be installed at
the top or the bottom (the default).

e.g. stereoeffect insert bottom Arts::Synth_FREEVERB
     stereoeffect remove 1
     stereoeffect list

midi (future)

When the MIDI manager functionality is all implemented this would be a
good place to implement the commands. Possible options include:

midi list
midi route <from> <to>

execute (future)

Being able to load a module into the sound server from the command
line would be useful. The relevant code could be taken from
artbuilder. Suggested command format:

execute <module.arts>

*/

#include <unistd.h>
#include <iostream>
#include "soundserver.h"
#include "dispatcher.h"
#include "artsversion.h"
#include "tradercheck.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <math.h>

bool quiet = false;
const char *filename = 0;

using namespace std;

const char* commandsmessage = "\
Commands:\n\
  suspend             - suspend sound server\n\
  status              - display sound server status information\n\
  terminate           - terminate sound server (might confuse/kill apps using it)\n\
  autosuspend <secs>  - set autosuspend time\n\
  networkbuffers <n>  - increase network buffers by a factor of <n>\n\
  volume [<volume>]   - display/set the volume of the soundserver\n\
  volumedb [<volume>] - display/set the volume in dB (decibel).\n\
                        6dB means amplification by 2\n\
                        Note: If you want to use negative values from the commandline\n\
                        you have to use:\n\
                          artsshell -- volumedb -NN\n\
                        since otherwise the negative value gets lost...\n\
  stereoeffect insert [top|bottom] <name>\n\
                      - insert stereo effect\n\
  stereoeffect remove <id>\n\
                      - remove stereo effect\n\
  stereoeffect list   - list available effects\n\
  tradercheck         - check consistency of the trader data (information\n\
                        stored in mcopclass/mcoptype files)\n\
  traderquery [ <requirement1> <requirement2> ... <requirementN> ]\n\
                      - query the trader for implementations with given requirements\n\
  version             - show sound server version\n\
  help, ?             - show commands\n";

void help()
{
	cout << commandsmessage << endl;
}

// Display command usage and exit
void usage()
{
	cerr <<
"usage: artsshell [options] <command> [command-options]\n\
\n\
Options:\n\
  -q                  - suppress all output\n\
  -h                  - display command usage\n\
  -v                  - show version\n\
  -f <filename>       - execute commands from <filename>\n\n" << commandsmessage << endl;
	exit(0);
}

// Parse command line options
void parseOptions(int argc, char **argv)
{
	int optch;

	if (argc == 0)
		usage();

	while((optch = getopt(argc, argv, "qhvf:1234567890")) > 0)
	{
		switch(optch)
		{
		  case 'q': quiet = true;
			  break;
		  case 'v':
			  printf("artsshell %s\n", ARTS_VERSION);
			  exit(0);
			  break;
		  case 'h':
			  usage();
			  break;
		  case 'f':
			  filename = optarg;
			  break;
		  case '1':
		  case '2':
		  case '3':
		  case '4':
		  case '5':
		  case '6':
		  case '7':
		  case '8':
		  case '9':
		  case '0':
			  break;
		  default:
			  usage();
			  break;
		}
	}
}


// Suspend sound server, if possible
int suspend(Arts::SoundServer server)
{
	switch (server.secondsUntilSuspend())
	{
	  case 0:
		  if (!quiet)
			  cerr << "sound server was already suspended"  << endl;
		  return 0;
		  break;
		  
	  case -1:
		  if (!quiet)
			  cerr << "sound server is busy"  << endl;
		  return 2;
		  break;

	  default:
		  if (server.suspend() == true)
		  {
			  if (!quiet)
				  cerr << "sound server suspended"  << endl;
			  return 0;
		  } else {
			  if (!quiet)
				  cerr << "unable to suspend sound server" << endl;
			  return 2;
		  }
	}
	return 0;
}


// Display server status information
void status(Arts::SoundServerV2 server)
{
	Arts::RealtimetqStatus rttqStatus = server.realtimetqStatus();
	long seconds = server.secondsUntilSuspend();

	cout << "server status: ";
	switch (seconds)
	{
	  case -1:
		  cout << "busy" << endl;
		  break;
	  case 0:
		  cout << "suspended" << endl;
		  break;
	  case -2:
		  cout << "running, autosuspend disabled" << endl;
		  break;
	  default:
		  cout << "running, will suspend in " << seconds << " s" << endl;
	}

	cout << "real-time status: ";
	switch (rttqStatus)
	{
	  case Arts::rtRealtime:
		  cout << "real-time" << endl;
		  break;
	  case Arts::rtNoSupport:
		  cout << "no real-time support" << endl;
		  break;
	  case Arts::rtNoWrapper:
		  cout << "not started through real-time wrapper" << endl;
		  break;
	  case Arts::rtNoRealtime:
		  cout << "not real-time" << endl;
		  break;
	  default:
		  cout << "unknown" << endl;
		  break;
	}

	cout << "server buffer time: " << server.serverBufferTime() << " ms" << endl;
	cout << "buffer size multiplier: " << server.bufferSizeMultiplier() << endl;
	cout << "minimum stream buffer time: " << server.minStreamBufferTime() << " ms" << endl;
	cout << "auto suspend time: " << server.autoSuspendSeconds() << " s" << endl;
	cout << "audio method: " << server.audioMethod() << endl;
	cout << "sampling rate: " << server.samplingRate() << endl;
	cout << "channels: " << server.channels() << endl;
	cout << "sample size: " << server.bits() << " bits" << endl;

	if (server.fullDuplex())
		cout << "duplex: full" << endl;
	else
		cout << "duplex: half" << endl;
	cout << "device: " << server.audioDevice() << endl;
	cout << "fragments: " << server.fragments() << endl;
	cout << "fragment size: " << server.fragmentSize() << endl;
}

// terminate the sound server
void terminate(Arts::SoundServer server)
{
	if(server.terminate())
	{
		cout << "sound server terminated" << endl;
		return;
	}
	else
	{
		cout << "there were problems terminating the sound server" << endl;
		return;
	}
}


// set autosuspend time
void autosuspend(Arts::SoundServerV2 server, int secs)
{
	server.autoSuspendSeconds(secs);
}

// set network buffers size
void networkBuffers(Arts::SoundServerV2 server, int n)
{
	if (n > 0)
		server.bufferSizeMultiplier(n);
}

// set the output volume
void setVolume(Arts::SoundServerV2 server, float volume)
{
	server.outVolume().scaleFactor(volume);
}

// return the output volume
float getVolume(Arts::SoundServerV2 server)
{
	return server.outVolume().scaleFactor();
}

// set the output volume in dB
void setDecibelVolume( Arts::SoundServerV2 server, float volume )
{
	setVolume( server, pow( 10, volume/20 ) );
}

// get the output volume in dB
float getDecibelVolume( Arts::SoundServerV2 server )
{
	return 20*log10( getVolume( server ) );
}

// stereoeffect command
void stereoEffect(Arts::SoundServerV2 server, int argc, char **argv)
{
	// stereoeffect list
	if (!strcmp(argv[0], "list"))
	{
		Arts::TraderQuery query = Arts::DynamicCast( server.createObject( "Arts::TraderQuery" ) );
		if( query.isNull() )
		{
			cerr << "unable to create a query" << endl;
			return;
		}
		query.supports("Interface", "Arts::StereoEffect");
		vector<Arts::TraderOffer> *offers = query.query();
		vector<Arts::TraderOffer>::iterator i;
		for (i = offers->begin(); i != offers->end(); i++)
			cout << i->interfaceName() << endl;
		delete offers;
		return;
	}

	// stereoeffect insert [top|bottom] <module name>
	if (!strcmp(argv[0], "insert"))
	{
		if (argc < 2 || argc > 3)
		{
			cerr << "invalid arguments" << endl;
			return;
		}

		bool bottom = true;
		if (argc == 3)
		{
			if (!strcmp(argv[1], "bottom"))
				bottom = true;
			else if (!strcmp(argv[1], "top"))
				bottom = false;
			else
			{
				cerr << "invalid arguments" << endl;
				return;
			}
		}

		char *name;
		if (argc == 2)
			name = argv[1];
		else
			name = argv[2];

		// first check if the interface exists using the Trader
		Arts::TraderQuery query = Arts::DynamicCast( server.createObject( "Arts::TraderQuery" ) );
		if( query.isNull() )
		{
			cerr << "unable to create a query" << endl;
			return;
		}
		query.supports("Interface", name);
		vector<Arts::TraderOffer> *offers = query.query();
		if (offers->empty())
		{
			cerr << "no such interface: " << name << endl;
			delete offers;
			return;
		}
		delete offers;

		Arts::Object obj = (server.createObject(name));
		if (obj.isNull())
		{
			cerr << "unable to create: " << name << endl;
			return;
		}
		Arts::StereoEffect effect = Arts::DynamicCast(obj);
		if (effect.isNull())
		{
			cerr << "unable to load effect: " << name << endl;
			return;
		}
		effect.start();
		Arts::StereoEffectStack effectstack = server.outstack();
		long id;
		if (bottom)
			id = effectstack.insertBottom(effect, name);
		else
			id = effectstack.insertTop(effect, name);
		cout << id << endl;
		return;
	}

	// stereoeffect remove <id>
	if (!strcmp(argv[0], "remove"))
	{
		if (argc != 2)
		{
			cerr << "invalid arguments" << endl;
			return;
		}
		Arts::StereoEffectStack effectstack = server.outstack();
		long id = atoi(argv[1]);
		effectstack.remove(id);
		return;
	}

	cerr << "invalid arguments" << endl;
}

// traderquery command
void traderQuery(Arts::SoundServerV2 server, int argc, char **argv)
{
	Arts::TraderQuery query = Arts::DynamicCast( server.createObject( "Arts::TraderQuery" ) );
	if( query.isNull() )
	{
		cerr << "unable to create a query" << endl;
		return;
	}

	for(int i=0;i<argc;i++)
	{
		char *buffer = strdup(argv[i]);
		char *key = strtok(buffer,"=");
		char *value = strtok(0,"\n");

		query.supports(key, value);
	}
	vector<Arts::TraderOffer> *offers = query.query();
	vector<Arts::TraderOffer>::iterator i;
	for (i = offers->begin(); i != offers->end(); i++)
		cout << i->interfaceName() << endl;
	delete offers;
}

// tradercheck command
void traderCheck()
{
	Arts::TraderCheck check;
	check.run();
}

void version()
{
	cout << "aRts version " << ARTS_VERSION << endl;
}

int executeCommand(Arts::SoundServerV2 server, int argc, char **argv)
{
	if (!strcmp(argv[0], "help") || !strcmp(argv[0], "?")) {
		help();
		return 0;
	}

	if (!strcmp(argv[0], "version")) {
		version();
		return 0;
	}

	if (!strcmp(argv[0], "suspend")) {
		suspend(server);
		return 0;
	}

	if (!strcmp(argv[0], "status")) {
		status(server);
		return 0;
	}

	if(!strcmp(argv[0], "terminate")) {
		terminate(server);
		return 0;
	}

	if(!strcmp(argv[0], "volume") && (argc == 2)) {
		setVolume(server,atof(argv[1]));
		return 0;
	}
	if(!strcmp(argv[0], "volume") && (argc == 1)) {
		cout << getVolume(server) << endl;;
		return 0;
	}

	if ( !strcmp( argv[ 0 ],"volumedb" ) && ( argc==2 ) ) {
		setDecibelVolume( server, atof( argv[ 1 ] ) );
		return 0;
	}
	if ( !strcmp( argv[ 0 ],"volumedb" ) && ( argc==1 ) ) {
		cout << getDecibelVolume( server ) << endl;
		return 0;
	}

	if(!strcmp(argv[0], "cpuusage") && (argc == 1)) {
		printf("%.1f\n",server.cpuUsage());
		return 0;
	}


	if(!strcmp(argv[0], "autosuspend") && (argc == 2)) {
		int secs = atoi(argv[1]);
		autosuspend(server, secs);
		return 0;
	}

	if(!strcmp(argv[0], "networkbuffers") && (argc == 2)) {
		int n = atoi(argv[1]);
		networkBuffers(server, n);
		return 0;
	}

	if(!strcmp(argv[0], "stereoeffect") && (argc >= 2)) {
		stereoEffect(server, argc-1, &argv[1]);
		return 0;
	}

	if(!strcmp(argv[0], "traderquery")) {
		traderQuery(server, argc-1, &argv[1]);
		return 0;
	}

	if(!strcmp(argv[0], "tradercheck")) {
		traderCheck();
		return 0;
	}

	return -1;
}

int execute(Arts::SoundServerV2 server, const char *filename)
{
	char command[1024];
	FILE *input = stdin;
	bool prompt;

	if(filename)
	{
		input = fopen(filename,"r");
		if(!input)
		{
			printf("can't open file '%s'\n", filename);
			return 1;
		}
	}

	prompt = isatty(fileno(input));

	if(prompt)
	{
		printf("> ");
		fflush(stdout);
	}

	while(fgets(command, 1024, input) != 0)
	{
		char **argv = 0;
		int argc = 0;
		while(char *arg = strtok(argc?0:command, " \t\n"))
		{
			argv = (char **)realloc(argv, sizeof(char *)*(argc+1));
			argv[argc++] = arg;
		}
		if (argv != 0)
		{
			if (executeCommand(server, argc, argv) == -1)
				cerr << "Invalid command, type 'help' for a list of commands." << endl;
			free(argv);
		}

		if(prompt)
		{
			printf("> ");
			fflush(stdout);
		}
	}

	if(prompt)
		printf("\n");
	
	if(input != stdin)
		fclose(input);

	return 0;
}

int main(int argc, char *argv[])
{
	Arts::Dispatcher dispatcher;
	Arts::SoundServerV2 server(Arts::Reference("global:Arts_SoundServer"));

	parseOptions(argc, argv);

	if (server.isNull())
	{
		if (!quiet)
			cerr << "unable to connect to sound server" << endl;
		exit(1);
	}

	if (argc == optind)
		return execute (server, filename);

	if (executeCommand (server, argc-optind, &argv[optind]) == -1)
		usage();

	return 0;
}

// vim: sw=4 ts=4